OSDN Git Service

BugTrack2/122: tempnam() fails when open_basedir is specified in php.ini
[pukiwiki/pukiwiki.git] / plugin / tb.inc.php
index 45259d9..1726e40 100644 (file)
 <?php
-// $Id: tb.inc.php,v 1.4 2003/08/20 10:55:36 arino Exp $
+// $Id: tb.inc.php,v 1.21 2005/06/15 15:57:11 henoheno Exp $
 /*
- * PukiWiki TrackBack ¥×¥í¥°¥é¥à
- * (C) 2003, Katsumi Saito <katsumi@jo1upk.ymt.prug.or.jp>
+ * PukiWiki/TrackBack: TrackBack Ping receiver and viewer
+ * (C) 2003-2005 PukiWiki Developers Team
+ * (C) 2003 Katsumi Saito <katsumi@jo1upk.ymt.prug.or.jp>
  * License: GPL
  *
- * plugin_tb_action()   action
- * tb_save()            TrackBack Ping ¥Ç¡¼¥¿Êݸ(¹¹¿·)
- * tb_xml_msg($rc,$msg) XML ·ë²Ì½ÐÎÏ
- * tb_mode_rss($tb_id)  ?__mode=rss ½èÍý
- * tb_mode_view($tb_id) ?__mode=view ½èÍý
+ * plugin_tb_action()    action
+ * plugin_tb_save($url, $tb_id)          Save or update TrackBack Ping data
+ * plugin_tb_output_response($rc, $msg)  Show a response code of the ping via HTTP/XML (then exit)
+ * plugin_tb_output_rsslist($tb_id)      Show pings for the page via RSS
+ * plugin_tb_output_htmllist($tb_id)     Show pings for the page via XHTML
  */
 
+switch(LANG){
+case 'ja': define('PLUGIN_TB_LANGUAGE', 'ja-jp'); break;
+default  : define('PLUGIN_TB_LANGUAGE', 'en-us'); break;
+}
+
+// ----
+
+define('PLUGIN_TB_ERROR',   1);
+define('PLUGIN_TB_NOERROR', 0);
+
 function plugin_tb_action()
 {
-       global $script,$vars,$post,$trackback;
-       
-       // POST: TrackBack Ping ¤òÊݸ¤¹¤ë
-       if (!empty($post['url']))
-       {
-               tb_save();
-       }
-       
-       if ($trackback and !empty($vars['__mode']) and !empty($vars['tb_id']))
-       {
-               switch ($vars['__mode'])
-               {
-                       case 'rss':
-                               tb_mode_rss($vars['tb_id']);
-                               break;
-                       case 'view':
-                               tb_mode_view($vars['tb_id']);
-                               break;
+       global $trackback, $vars;
+
+       if ($trackback && isset($vars['url'])) {
+               // Receive and save a TrackBack Ping (both GET and POST)
+               $url   = $vars['url'];
+               $tb_id = isset($vars['tb_id']) ? $vars['tb_id'] : '';
+               list($error, $message) = plugin_tb_save($url, $tb_id);
+
+               // Output the response
+               plugin_tb_output_response($error, $message);
+               exit;
+
+       } else {
+               if ($trackback && isset($vars['__mode']) && isset($vars['tb_id'])) {
+                       // Show TrackBacks received (and exit)
+                       switch ($vars['__mode']) {
+                       case 'rss' : plugin_tb_output_rsslist($vars['tb_id']);  break;
+                       case 'view': plugin_tb_output_htmllist($vars['tb_id']); break;
+                       }
+                       exit;
+
+               } else {
+                       // Show List of pages that TrackBacks reached
+                       $pages = get_existpages(TRACKBACK_DIR, '.txt');
+                       if (! empty($pages)) {
+                               return array('msg'=>'Trackback list',
+                                       'body'=>page_list($pages, 'read', FALSE));
+                       } else {
+                               return array('msg'=>'', 'body'=>'');
+                       }
                }
        }
-       $pages = get_existpages(TRACKBACK_DIR,'.txt');
-       
-       if (count($pages) == 0)
-       {
-               return array('msg'=>'','body'=>'');
-       }
-       
-       return array(
-               'msg' => 'trackback list',
-               'body' => page_list($pages,'read',FALSE)
-       );
 }
 
-// TrackBack Ping ¥Ç¡¼¥¿Êݸ(¹¹¿·)
-function tb_save()
+// Save or update TrackBack Ping data
+function plugin_tb_save($url, $tb_id)
 {
-       global $script,$post,$vars,$trackback;
-       static $fields = array(/* UTIME, */'url','title','excerpt','blog_name');
-       
-       // µö²Ä¤·¤Æ¤¤¤Ê¤¤¤Î¤Ë¸Æ¤Ð¤ì¤¿¾ì¹ç¤ÎÂбþ
-       if (!$trackback)
-       {
-               tb_xml_msg(1,'Feature inactive.');
-       }
-       // TrackBack Ping ¤Ë¤ª¤±¤ë URL ¥Ñ¥é¥á¡¼¥¿¤Ïɬ¿Ü¤Ç¤¢¤ë¡£
-       if (empty($post['url']))
-       {
-               tb_xml_msg(1,'It is an indispensable parameter. URL is not set up.');
-       }
-       // Query String ¤òÆÀ¤ë
-       if (empty($vars['tb_id']))
-       {
-               tb_xml_msg(1,'TrackBack Ping URL is inaccurate.');
-       }
-       
-       $url = $post['url'];
-       $tb_id = $vars['tb_id'];
-       
-       // ¥Ú¡¼¥¸Â¸ºß¥Á¥§¥Ã¥¯
+       global $vars, $trackback;
+       static $fields = array( /* UTIME, */ 'url', 'title', 'excerpt', 'blog_name');
+
+       $die = '';
+       if (! $trackback) $die .= 'TrackBack feature disabled. ';
+       if ($url   == '') $die .= 'URL parameter is not set. ';
+       if ($tb_id == '') $die .= 'TrackBack Ping ID is not set. ';
+       if ($die != '') return array(PLUGIN_TB_ERROR, $die);
+
+       if (! file_exists(TRACKBACK_DIR)) return array(PLUGIN_TB_ERROR, 'No such directory: TRACKBACK_DIR');
+       if (! is_writable(TRACKBACK_DIR)) return array(PLUGIN_TB_ERROR, 'Permission denied: TRACKBACK_DIR');
+
        $page = tb_id2page($tb_id);
-       if ($page === FALSE)
-       {
-               tb_xml_msg(1,'TrackBack ID is invalid.');
-       }
-       
-       // URL ÂÅÅöÀ­¥Á¥§¥Ã¥¯ (¤³¤ì¤òÆþ¤ì¤ë¤È½èÍý»þ´Ö¤ËÌäÂ꤬¤Ç¤ë)
-       $result = http_request($url,'HEAD');
-       if ($result['rc'] !== 200)
-       {
-               tb_xml_msg(1,'URL is fictitious.');
-       }
-       
-       // TRACKBACK_DIR ¤Î¸ºß¤È½ñ¤­¹þ¤ß²Äǽ¤«¤Î³Îǧ
-       if (!file_exists(TRACKBACK_DIR))
-       {
-               tb_xml_msg(1,'No such directory');
-       }
-       if (!is_writable(TRACKBACK_DIR))
-       {
-               tb_xml_msg(1,'Permission denied');
-       }
-       
-       // TrackBack Ping ¤Î¥Ç¡¼¥¿¤ò¹¹¿·
+       if ($page === FALSE) return array(PLUGIN_TB_ERROR, 'TrackBack ID is invalid.');
+
+       // URL validation (maybe worse of processing time limit)
+       $result = http_request($url, 'HEAD');
+       if ($result['rc'] !== 200) return array(PLUGIN_TB_ERROR, 'URL is fictitious.');
+
+       // Update TrackBack Ping data
        $filename = tb_get_filename($page);
-       $data = tb_get($filename);
-       
+       $data     = tb_get($filename);
+
        $items = array(UTIME);
-       foreach ($fields as $field)
-       {
-               $value = array_key_exists($field,$post) ? $post[$field] : '';
-               if (ereg("[,\"\n\r]",$value))
-               {
-                       $value = '"'.str_replace('"', '""', $value).'"';
-               }
-               $items[$field] = $value;
-       }
-       $data[$items['url']] = $items;
-       
-       $fp = fopen($filename,'w');
-       flock($fp,LOCK_EX);
-       foreach ($data as $line)
-       {
-               fwrite($fp,join(',',$line)."\n");
+       foreach ($fields as $key) {
+               $value = isset($vars[$key]) ? $vars[$key] : '';
+               if (preg_match('/[,"' . "\n\r" . ']/', $value))
+                       $value = '"' . str_replace('"', '""', $value) . '"';
+               $items[$key] = $value;
        }
-       flock($fp,LOCK_UN);
+       $data[rawurldecode($items['url'])] = $items;
+
+       $fp = fopen($filename, 'w');
+       set_file_buffer($fp, 0);
+       flock($fp, LOCK_EX);
+       rewind($fp);
+       foreach ($data as $line) {
+               $line = preg_replace('/[\r\n]/s', '', $line); // One line, one ping
+               fwrite($fp, join(',', $line) . "\n");
+       }
+       flock($fp, LOCK_UN);
        fclose($fp);
-       
-       tb_xml_msg(0,'');
+
+       return array(PLUGIN_TB_NOERROR, '');
 }
 
-// XML ·ë²Ì½ÐÎÏ
-function tb_xml_msg($rc,$msg)
+// Show a response code of the ping via HTTP/XML (then exit)
+function plugin_tb_output_response($rc, $msg = '')
 {
+       if ($rc == PLUGIN_TB_NOERROR) {
+               $rc = 0; // for PLUGIN_TB_NOERROR
+       } else {
+               $rc = 1; // for PLUGIN_TB_ERROR
+       }
+
+       pkwk_common_headers();
        header('Content-Type: text/xml');
        echo '<?xml version="1.0" encoding="iso-8859-1"?>';
-       echo <<<EOD
-
-<response>
- <error>$rc</error>
- <message>$msg</message>
-</response>
-EOD;
-       die;
+       echo '<response>';
+       echo ' <error>' . $rc . '</error>';
+       if ($rc) echo '<message>' . $msg . '</message>';
+       echo '</response>';
+       exit;
 }
 
-// ?__mode=rss ½èÍý
-function tb_mode_rss($tb_id)
+// Show pings for the page via RSS
+function plugin_tb_output_rsslist($tb_id)
 {
-       global $script,$vars,$entity_pattern;
-       
+       global $script, $vars, $entity_pattern;
+
        $page = tb_id2page($tb_id);
-       if ($page === FALSE)
-       {
-               return FALSE;
-       }
-       
+       if ($page === FALSE) return FALSE;
+
        $items = '';
-       foreach (tb_get(tb_get_filename($page)) as $arr)
-       {
-               $utime = array_shift($arr);
-               list ($url,$title,$excerpt,$blog_name) = array_map(
-                       create_function('$a','return htmlspecialchars($a);'),$arr);
+       foreach (tb_get(tb_get_filename($page)) as $arr) {
+               // _utime_, title, excerpt, _blog_name_
+               array_shift($arr); // Cut utime
+               list ($url, $title, $excerpt) = array_map(
+                       create_function('$a', 'return htmlspecialchars($a);'), $arr);
                $items .= <<<EOD
 
    <item>
@@ -165,16 +150,17 @@ function tb_mode_rss($tb_id)
    </item>
 EOD;
        }
-       
+
        $title = htmlspecialchars($page);
-       $link = "$script?".rawurlencode($page);
+       $link  = $script . '?' . rawurlencode($page);
        $vars['page'] = $page;
-       $excerpt = strip_htmltag(convert_html(join('',get_source($page))));
-       $excerpt = preg_replace("/&$entity_pattern;/",'',$excerpt);
-       $excerpt = mb_strimwidth(preg_replace("/[\r\n]/",' ',$excerpt),0,255,'...');
+       $excerpt = strip_htmltag(convert_html(get_source($page)));
+       $excerpt = preg_replace("/&$entity_pattern;/", '', $excerpt);
+       $excerpt = mb_strimwidth(preg_replace("/[\r\n]/", ' ', $excerpt), 0, 255, '...');
+       $lang    = PLUGIN_TB_LANGUAGE;
 
        $rc = <<<EOD
-
+<?xml version="1.0" encoding="utf-8" ?>
 <response>
  <error>0</error>
  <rss version="0.91">
@@ -182,83 +168,72 @@ EOD;
    <title>$title</title>
    <link>$link</link>
    <description>$excerpt</description>
-   <language>ja-Jp</language>$items
+   <language>$lang</language>$items
   </channel>
  </rss>
 </response>
 EOD;
-       $rc = mb_convert_encoding($rc,'UTF-8',SOURCE_ENCODING);
+
+       pkwk_common_headers();
        header('Content-Type: text/xml');
-       echo '<?xml version="1.0" encoding="utf-8" ?>';
-       echo $rc;
-       die;
+       echo mb_convert_encoding($rc, 'UTF-8', SOURCE_ENCODING);
+       exit;
 }
-// ?__mode=view ½èÍý
-function tb_mode_view($tb_id)
+
+// Show pings for the page via XHTML
+function plugin_tb_output_htmllist($tb_id)
 {
-       global $script,$page_title;
-       global $_tb_title,$_tb_header,$_tb_entry,$_tb_refer,$_tb_date;
-       global $_tb_header_Excerpt,$_tb_header_Weblog,$_tb_header_Tracked;
-       
-       // TrackBack ID ¤«¤é¥Ú¡¼¥¸Ì¾¤ò¼èÆÀ
+       pkwk_common_headers();
+       echo 'This function had been removed now. It will be created soon.<br />' . "\n";
+       echo 'Sorry for your inconvenience.';
+       exit;
+
+       // ----
+       // Skeleton Logic
+
+       global $script;
+       global $_tb_date;
+
        $page = tb_id2page($tb_id);
-       if ($page === FALSE)
-       {
-               return FALSE;
-       }
-       $r_page = rawurlencode($page);
-       
-       $tb_title = sprintf($_tb_title,$page);
-       $tb_refer = sprintf($_tb_refer,"<a href=\"$script?$r_page\">'$page'</a>","<a href=\"$script\">$page_title</a>");
+       if ($page === FALSE) return FALSE;
 
-       
        $data = tb_get(tb_get_filename($page));
-       
-       // ºÇ¿·ÈǤ«¤éÀ°Îó
-       usort($data,create_function('$a,$b','return $b[0] - $a[0];'));
-       
+
+       // Sort: The first is the latest
+       usort($data, create_function('$a,$b', 'return $b[0] - $a[0];'));
+
        $tb_body = '';
-       foreach ($data as $x)
-       {
-               list ($time,$url,$title,$excerpt,$blog_name) = $x;
-               if ($title == '')
-               {
-                       $title = 'no title';
-               }
+       foreach ($data as $x) {
+               if (count($x) != 5) continue; // Ignore incorrect record
+
+               list ($time, $url, $title, $excerpt, $blog_name) = $x;
+               if ($title == '') $title = 'no title';
+
                $time = date($_tb_date, $time + LOCALZONE); // May 2, 2003 11:25 AM
                $tb_body .= <<<EOD
-<div class="trackback-body">
- <span class="trackback-post"><a href="$url" target="new">$title</a><br />
-  <strong>$_tb_header_Excerpt</strong> $excerpt<br />
-  <strong>$_tb_header_Weblog</strong> $blog_name<br />
-  <strong>$_tb_header_Tracked</strong> $time
- </span>
-</div>
 EOD;
        }
+
+       // Output start
+       pkwk_common_headers();
+
+       // BugTrack/466 Care for MSIE trouble
+       // Logically correct, but MSIE will treat the data like 'file downloading'
+       //header('Content-type: application/xhtml+xml; charset=UTF-8');
+       header('Content-type: text/html; charset=UTF-8'); // Works well
+
+       $meta_content_type = pkwk_output_dtd(PKWK_DTD_XHTML_1_0_TRANSITIONAL, 'UTF-8');
        $msg = <<<EOD
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
 <head>
- <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
- <title>$tb_title</title>
- <link rel="stylesheet" href="skin/trackback.css" type="text/css" />
+ $meta_content_type
 </head>
 <body>
- <div id="banner-commentspop">$_tb_header</div>
- <div class="blog">
-  <div class="trackback-url">
-   $_tb_entry<br />
-   $script?plugin=tb&amp;tb_id=$tb_id<br /><br />
-   $tb_refer
-  </div>
-  $tb_body
- </div>
+ $script?tb_id=$tb_id<br /><br />
+ $tb_body
 </body>
 </html>
 EOD;
-       echo '<?xml version="1.0" encoding="UTF-8"?>';
-       echo mb_convert_encoding($msg,'UTF-8',SOURCE_ENCODING);
-       die;
+       echo mb_convert_encoding($msg, 'UTF-8', SOURCE_ENCODING);
+       exit;
 }
 ?>