OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / tb.inc.php
1 <?php
2 /**
3  * tbプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2008 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: tb.inc.php 1156 2008-10-29 10:13:32Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 /*
17  * PukiWiki/TrackBack: TrackBack Ping receiver and viewer
18  * (C) 2003-2005 PukiWiki Developers Team
19  * (C) 2003 Katsumi Saito <katsumi@jo1upk.ymt.prug.or.jp>
20  * License: GPL
21  *
22  * plugin_tb_action()    action
23  * plugin_tb_save($url, $tb_id)          Save or update TrackBack Ping data
24  * plugin_tb_output_response($rc, $msg)  Show a response code of the ping via HTTP/XML (then exit)
25  * plugin_tb_output_rsslist($tb_id)      Show pings for the page via RSS
26  * plugin_tb_output_htmllist($tb_id)     Show pings for the page via XHTML
27  */
28
29 switch(LANG){
30 case 'ja': define('PLUGIN_TB_LANGUAGE', 'ja-jp'); break;
31 default  : define('PLUGIN_TB_LANGUAGE', 'en-us'); break;
32 }
33
34 define('PLUGIN_TB_ERROR',   1);
35 define('PLUGIN_TB_NOERROR', 0);
36
37 function plugin_tb_action()
38 {
39         //global $trackback, $vars;
40         global $trackback;
41
42         $url = WikiParam::getVar('url');
43         $tb_id = WikiParam::getVar('tb_id');
44         //if ($trackback && isset($vars['url'])) {
45         if ($trackback && ($url != '')) {
46                 // Receive and save a TrackBack Ping (both GET and POST)
47                 //$url   = $vars['url'];
48                 //$tb_id = isset($vars['tb_id']) ? $vars['tb_id'] : '';
49                 list($error, $message) = plugin_tb_save($url, $tb_id);
50
51                 // Output the response
52                 plugin_tb_output_response($error, $message);
53                 exit;
54
55         } else {
56                 $mode = WikiParam::getVar('__mode');
57                 //if ($trackback && isset($vars['__mode']) && isset($vars['tb_id'])) {
58                 if ($trackback && ($mode != '') && ($tb_id != '')) {
59                         // Show TrackBacks received (and exit)
60                         /*switch ($vars['__mode']) {
61                         case 'rss' : plugin_tb_output_rsslist($vars['tb_id']);  break;
62                         case 'view': plugin_tb_output_htmllist($vars['tb_id']); break;
63                         }*/
64                         switch ($mode) {
65                                 case 'rss' : plugin_tb_output_rsslist($tb_id);  break;
66                                 case 'view': plugin_tb_output_htmllist($tb_id); break;
67                         }
68                         exit;
69
70                 } else {
71                         // Show List of pages that TrackBacks reached
72                         //$pages = get_existpages(TRACKBACK_DIR, '.txt');
73                         $pages = WikiPage::getTrackbackPages();
74                         if (! empty($pages)) {
75                                 return array('msg'=>'Trackback list',
76                                         'body'=>page_list($pages, 'read', FALSE));
77                         } else {
78                                 return array('msg'=>'', 'body'=>'');
79                         }
80                 }
81         }
82 }
83
84 // Save or update TrackBack Ping data
85 function plugin_tb_save($url, $tb_id)
86 {
87         //global $vars, $trackback;
88         global $trackback;
89         static $fields = array( /* UTIME, */ 'url', 'title', 'excerpt', 'blog_name');
90
91         $die = '';
92         if (! $trackback) $die .= 'TrackBack feature disabled. ';
93         if ($url   == '') $die .= 'URL parameter is not set. ';
94         if ($tb_id == '') $die .= 'TrackBack Ping ID is not set. ';
95         if ($die != '') return array(PLUGIN_TB_ERROR, $die);
96
97         if (! file_exists(TRACKBACK_DIR)) return array(PLUGIN_TB_ERROR, 'No such directory: TRACKBACK_DIR');
98         if (! is_writable(TRACKBACK_DIR)) return array(PLUGIN_TB_ERROR, 'Permission denied: TRACKBACK_DIR');
99
100         $page = tb_id2page($tb_id);
101         if ($page === FALSE) return array(PLUGIN_TB_ERROR, 'TrackBack ID is invalid.');
102
103         // URL validation (maybe worse of processing time limit)
104         $result = http_request($url, 'HEAD');
105         if ($result['rc'] !== 200) return array(PLUGIN_TB_ERROR, 'URL is fictitious.');
106
107         // Update TrackBack Ping data
108         $filename = tb_get_filename($page);
109         $data     = tb_get($filename);
110
111         $items = array(UTIME);
112         foreach ($fields as $key) {
113                 //$value = isset($vars[$key]) ? $vars[$key] : '';
114                 $value = WikiParam::getVar($key);
115                 if (preg_match('/[,"' . "\n\r" . ']/', $value))
116                         $value = '"' . str_replace('"', '""', $value) . '"';
117                 $items[$key] = $value;
118         }
119         $data[rawurldecode($items['url'])] = $items;
120
121         $fp = fopen($filename, 'w');
122         set_file_buffer($fp, 0);
123         flock($fp, LOCK_EX);
124         rewind($fp);
125         foreach ($data as $line) {
126                 $line = preg_replace('/[\r\n]/s', '', $line); // One line, one ping
127                 fwrite($fp, join(',', $line) . "\n");
128         }
129         flock($fp, LOCK_UN);
130         fclose($fp);
131
132         return array(PLUGIN_TB_NOERROR, '');
133 }
134
135 // Show a response code of the ping via HTTP/XML (then exit)
136 function plugin_tb_output_response($rc, $msg = '')
137 {
138         if ($rc == PLUGIN_TB_NOERROR) {
139                 $rc = 0; // for PLUGIN_TB_NOERROR
140         } else {
141                 $rc = 1; // for PLUGIN_TB_ERROR
142         }
143
144         pkwk_common_headers();
145         header('Content-Type: text/xml');
146         echo '<?xml version="1.0" encoding="iso-8859-1"?>';
147         echo '<response>';
148         echo ' <error>' . $rc . '</error>';
149         if ($rc) echo '<message>' . $msg . '</message>';
150         echo '</response>';
151         exit;
152 }
153
154 // Show pings for the page via RSS
155 function plugin_tb_output_rsslist($tb_id)
156 {
157         //global $script, $vars, $entity_pattern;
158         global $script, $entity_pattern;
159
160         $page = tb_id2page($tb_id);
161         if ($page === FALSE) return FALSE;
162
163         $items = '';
164         foreach (tb_get(tb_get_filename($page)) as $arr) {
165                 // _utime_, title, excerpt, _blog_name_
166                 array_shift($arr); // Cut utime
167                 list ($url, $title, $excerpt) = array_map(
168                         create_function('$a', 'return htmlspecialchars($a);'), $arr);
169                 $items .= <<<EOD
170
171    <item>
172     <title>$title</title>
173     <link>$url</link>
174     <description>$excerpt</description>
175    </item>
176 EOD;
177         }
178
179         $title = htmlspecialchars($page);
180         $link  = $script . '?' . rawurlencode($page);
181         //$vars['page'] = $page;
182         WikiParam::setPage($page);
183         $excerpt = strip_htmltag(convert_html(get_source($page)));
184         $excerpt = preg_replace("/&$entity_pattern;/", '', $excerpt);
185         $excerpt = mb_strimwidth(preg_replace("/[\r\n]/", ' ', $excerpt), 0, 255, '...');
186         $lang    = PLUGIN_TB_LANGUAGE;
187
188         $rc = <<<EOD
189 <?xml version="1.0" encoding="utf-8" ?>
190 <response>
191  <error>0</error>
192  <rss version="0.91">
193   <channel>
194    <title>$title</title>
195    <link>$link</link>
196    <description>$excerpt</description>
197    <language>$lang</language>$items
198   </channel>
199  </rss>
200 </response>
201 EOD;
202
203         pkwk_common_headers();
204         header('Content-Type: text/xml');
205         echo mb_convert_encoding($rc, 'UTF-8', SOURCE_ENCODING);
206         exit;
207 }
208
209 // Show pings for the page via XHTML
210 function plugin_tb_output_htmllist($tb_id)
211 {
212         pkwk_common_headers();
213         echo 'This function had been removed now. It will be created soon.<br />' . "\n";
214         echo 'Sorry for your inconvenience.';
215         exit;
216
217         // ----
218         // Skeleton Logic
219
220         global $script;
221         global $_tb_date;
222
223         $page = tb_id2page($tb_id);
224         if ($page === FALSE) return FALSE;
225
226         $data = tb_get(tb_get_filename($page));
227
228         // Sort: The first is the latest
229         usort($data, create_function('$a,$b', 'return $b[0] - $a[0];'));
230
231         $tb_body = '';
232         foreach ($data as $x) {
233                 if (count($x) != 5) continue; // Ignore incorrect record
234
235                 list ($time, $url, $title, $excerpt, $blog_name) = $x;
236                 if ($title == '') $title = 'no title';
237
238                 $time = date($_tb_date, $time + LOCALZONE); // May 2, 2003 11:25 AM
239                 $tb_body .= <<<EOD
240 EOD;
241         }
242
243         // Output start
244         pkwk_common_headers();
245
246         // BugTrack/466 Care for MSIE trouble
247         // Logically correct, but MSIE will treat the data like 'file downloading'
248         //header('Content-type: application/xhtml+xml; charset=UTF-8');
249         header('Content-type: text/html; charset=UTF-8'); // Works well
250
251         $meta_content_type = pkwk_output_dtd(PKWK_DTD_XHTML_1_0_TRANSITIONAL, 'UTF-8');
252         $msg = <<<EOD
253 <head>
254  $meta_content_type
255 </head>
256 <body>
257  $script?tb_id=$tb_id<br /><br />
258  $tb_body
259 </body>
260 </html>
261 EOD;
262         echo mb_convert_encoding($msg, 'UTF-8', SOURCE_ENCODING);
263         exit;
264 }
265 ?>