7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
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>
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
30 case 'ja': define('PLUGIN_TB_LANGUAGE', 'ja-jp'); break;
31 default : define('PLUGIN_TB_LANGUAGE', 'en-us'); break;
34 define('PLUGIN_TB_ERROR', 1);
35 define('PLUGIN_TB_NOERROR', 0);
37 function plugin_tb_action()
39 //global $trackback, $vars;
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);
51 // Output the response
52 plugin_tb_output_response($error, $message);
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;
65 case 'rss' : plugin_tb_output_rsslist($tb_id); break;
66 case 'view': plugin_tb_output_htmllist($tb_id); break;
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));
78 return array('msg'=>'', 'body'=>'');
84 // Save or update TrackBack Ping data
85 function plugin_tb_save($url, $tb_id)
87 //global $vars, $trackback;
89 static $fields = array( /* UTIME, */ 'url', 'title', 'excerpt', 'blog_name');
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);
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');
100 $page = tb_id2page($tb_id);
101 if ($page === FALSE) return array(PLUGIN_TB_ERROR, 'TrackBack ID is invalid.');
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.');
107 // Update TrackBack Ping data
108 $filename = tb_get_filename($page);
109 $data = tb_get($filename);
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;
119 $data[rawurldecode($items['url'])] = $items;
121 $fp = fopen($filename, 'w');
122 set_file_buffer($fp, 0);
125 foreach ($data as $line) {
126 $line = preg_replace('/[\r\n]/s', '', $line); // One line, one ping
127 fwrite($fp, join(',', $line) . "\n");
132 return array(PLUGIN_TB_NOERROR, '');
135 // Show a response code of the ping via HTTP/XML (then exit)
136 function plugin_tb_output_response($rc, $msg = '')
138 if ($rc == PLUGIN_TB_NOERROR) {
139 $rc = 0; // for PLUGIN_TB_NOERROR
141 $rc = 1; // for PLUGIN_TB_ERROR
144 pkwk_common_headers();
145 header('Content-Type: text/xml');
146 echo '<?xml version="1.0" encoding="iso-8859-1"?>';
148 echo ' <error>' . $rc . '</error>';
149 if ($rc) echo '<message>' . $msg . '</message>';
154 // Show pings for the page via RSS
155 function plugin_tb_output_rsslist($tb_id)
157 //global $script, $vars, $entity_pattern;
158 global $script, $entity_pattern;
160 $page = tb_id2page($tb_id);
161 if ($page === FALSE) return FALSE;
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);
172 <title>$title</title>
174 <description>$excerpt</description>
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;
189 <?xml version="1.0" encoding="utf-8" ?>
194 <title>$title</title>
196 <description>$excerpt</description>
197 <language>$lang</language>$items
203 pkwk_common_headers();
204 header('Content-Type: text/xml');
205 echo mb_convert_encoding($rc, 'UTF-8', SOURCE_ENCODING);
209 // Show pings for the page via XHTML
210 function plugin_tb_output_htmllist($tb_id)
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.';
223 $page = tb_id2page($tb_id);
224 if ($page === FALSE) return FALSE;
226 $data = tb_get(tb_get_filename($page));
228 // Sort: The first is the latest
229 usort($data, create_function('$a,$b', 'return $b[0] - $a[0];'));
232 foreach ($data as $x) {
233 if (count($x) != 5) continue; // Ignore incorrect record
235 list ($time, $url, $title, $excerpt, $blog_name) = $x;
236 if ($title == '') $title = 'no title';
238 $time = date($_tb_date, $time + LOCALZONE); // May 2, 2003 11:25 AM
244 pkwk_common_headers();
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
251 $meta_content_type = pkwk_output_dtd(PKWK_DTD_XHTML_1_0_TRANSITIONAL, 'UTF-8');
257 $script?tb_id=$tb_id<br /><br />
262 echo mb_convert_encoding($msg, 'UTF-8', SOURCE_ENCODING);