OSDN Git Service

Renamed sanitize() => input_filter(), to avoid misunderstanding
[pukiwiki/pukiwiki.git] / file.php
index 283779d..28eb31c 100644 (file)
--- a/file.php
+++ b/file.php
@@ -2,13 +2,14 @@
 /////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
 //
-// $Id: file.php,v 1.19 2003/05/26 13:46:31 arino Exp $
+// $Id: file.php,v 1.43 2004/07/10 10:59:38 henoheno Exp $
 //
 
 // ¥½¡¼¥¹¤ò¼èÆÀ
-function get_source($page)
+function get_source($page=NULL)
 {
-       if (!is_page($page)) {
+       if (!is_page($page))
+       {
                return array();
        }
        return str_replace("\r",'',file(get_filename($page)));
@@ -17,6 +18,10 @@ function get_source($page)
 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
 function get_filetime($page)
 {
+       if (!is_page($page))
+       {
+               return 0;
+       }
        return filemtime(get_filename($page)) - LOCALZONE;
 }
 
@@ -27,7 +32,7 @@ function get_filename($page)
 }
 
 // ¥Ú¡¼¥¸¤Î½ÐÎÏ
-function page_write($page,$postdata)
+function page_write($page,$postdata,$notimestamp=FALSE)
 {
        $postdata = make_str_rules($postdata);
        
@@ -40,10 +45,12 @@ function page_write($page,$postdata)
        make_backup($page,$postdata == '');
        
        // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
-       file_write(DATA_DIR,$page,$postdata);
+       file_write(DATA_DIR,$page,$postdata,$notimestamp);
        
-       // is_page¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
-       is_page($page,TRUE);
+       // TrackBack Ping ¤ÎÁ÷¿®
+       // ¡ÖÄɲá׹ԤòÃê½Ð
+       $lines = join("\n",preg_replace('/^\+/','',preg_grep('/^\+/',explode("\n",$diffdata))));
+       tb_send($page,$lines);
        
        // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
        links_update($page);
@@ -56,6 +63,7 @@ function make_str_rules($str)
        
        $arr = explode("\n",$str);
        
+       $retvars = array();
        foreach ($arr as $str)
        {
                if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
@@ -82,10 +90,12 @@ function make_str_rules($str)
 }
 
 // ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
-function file_write($dir,$page,$str)
+function file_write($dir,$page,$str,$notimestamp=FALSE)
 {
-       global $post,$update_exec;
+       global $update_exec;
        global $_msg_invalidiwn;
+       global $notify, $notify_diff_only, $notify_to, $notify_subject, $notify_header;
+       global $smtp_server, $smtp_auth;
        
        if (!is_pagename($page))
        {
@@ -99,41 +109,106 @@ function file_write($dir,$page,$str)
        $timestamp = FALSE;
        $file = $dir.encode($page).'.txt';
        
-       if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
+       if ($dir == DATA_DIR and $str == '' and file_exists($file))
+       {
                unlink($file);
+               put_recentdeleted($page);
        }
-       if ($str != '') {
+       if ($str != '')
+       {
                $str = preg_replace("/\r/",'',$str);
                $str = rtrim($str)."\n";
                
-               if (!empty($post['notimestamp']) and file_exists($file)) {
+               if ($notimestamp and file_exists($file))
+               {
                        $timestamp = filemtime($file) - LOCALZONE;
                }
                
                $fp = fopen($file,'w')
-                       or die_message('cannot write page file or diff file or other'.htmlspecialchars($page).'<br>maybe permission is not writable or filename is too long');
+                       or die_message('cannot write page file or diff file or other'.htmlspecialchars($page).'<br />maybe permission is not writable or filename is too long');
+               set_file_buffer($fp, 0);
                flock($fp,LOCK_EX);
+               rewind($fp);
                fputs($fp,$str);
                flock($fp,LOCK_UN);
                fclose($fp);
-               if ($timestamp) {
+               if ($timestamp)
+               {
                        touch($file,$timestamp + LOCALZONE);
                }
        }
        
-       if (!$timestamp) {
+       // is_page¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
+       is_page($page,TRUE);
+       
+       if (!$timestamp and $dir == DATA_DIR)
+       {
                put_lastmodified();
        }
        
-       if ($update_exec and $dir == DATA_DIR) {
+       if ($update_exec and $dir == DATA_DIR)
+       {
                system($update_exec.' > /dev/null &');
        }
+       
+       if ($notify and $dir == DIFF_DIR)
+       {
+               if ($notify_diff_only)
+               {
+                       // º¹Ê¬¤À¤±¤òÁ÷¿®¤¹¤ë
+                       $str = preg_replace('/^[^-+].*\n/m','',$str);
+               }
+               if ($smtp_auth)
+               {
+                       pop_before_smtp();
+               }
+               $subject = str_replace('$page',$page,$notify_subject);
+               ini_set('SMTP',$smtp_server);
+               mb_language(LANG);
+               mb_send_mail($notify_to,$subject,$str,$notify_header);
+       }
+}
+
+// ºï½üÍúÎò¥Ú¡¼¥¸¤Î¹¹¿·
+function put_recentdeleted($page)
+{
+       global $whatsdeleted,$maxshow_deleted;
+       
+       if ($maxshow_deleted == 0)
+       {
+               return;
+       }
+       // update RecentDeleted
+       $lines = array();
+       foreach (get_source($whatsdeleted) as $line)
+       {
+               if (preg_match('/^-(.+) - (\[\[.+\]\])$/',$line,$matches))
+               {
+                       $lines[$matches[2]] = $line;
+               }
+       }
+       $_page = "[[$page]]";
+       if (array_key_exists($_page,$lines))
+       {
+               unset($lines[$_page]);
+       }
+       array_unshift($lines,'-'.format_date(UTIME)." - $_page\n");
+       $lines = array_splice($lines,0,$maxshow_deleted);
+       $fp = fopen(get_filename($whatsdeleted),'w')
+               or die_message('cannot write page file '.htmlspecialchars($whatsdeleted).'<br />maybe permission is not writable or filename is too long');
+       set_file_buffer($fp, 0);
+       flock($fp,LOCK_EX);
+       rewind($fp);
+       fputs($fp,join('',$lines));
+       fputs($fp,"#norelated\n"); // :)
+       flock($fp,LOCK_UN);
+       fclose($fp);
 }
 
 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
 function put_lastmodified()
 {
-       global $script,$post,$maxshow,$whatsnew,$non_list,$autolink;
+       global $maxshow,$whatsnew,$non_list,$autolink;
 
        $pages = get_existpages();
        $recent_pages = array();
@@ -150,8 +225,10 @@ function put_lastmodified()
        
        // create recent.dat (for recent.inc.php)
        $fp = fopen(CACHE_DIR.'recent.dat','w')
-               or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br>maybe permission is not writable or filename is too long');
+               or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br />maybe permission is not writable or filename is too long');
+       set_file_buffer($fp, 0);
        flock($fp,LOCK_EX);
+       rewind($fp);
        foreach ($recent_pages as $page=>$time)
        {
                fputs($fp,"$time\t$page\n");
@@ -161,10 +238,13 @@ function put_lastmodified()
 
        // create RecentChanges
        $fp = fopen(get_filename($whatsnew),'w')
-               or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br>maybe permission is not writable or filename is too long');
+               or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br />maybe permission is not writable or filename is too long');
+       set_file_buffer($fp, 0);
        flock($fp,LOCK_EX);
-       foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
+       rewind($fp);
+       foreach (array_splice(array_keys($recent_pages),0,$maxshow) as $page)
        {
+               $time = $recent_pages[$page];
                $s_lastmod = htmlspecialchars(format_date($time));
                $s_page = htmlspecialchars($page);
                fputs($fp, "-$s_lastmod - [[$s_page]]\n");
@@ -176,13 +256,16 @@ function put_lastmodified()
        // for autolink
        if ($autolink)
        {
-               list($pattern,$forceignorelist) = get_autolink_pattern($pages);
+               list($pattern,$pattern_a,$forceignorelist) = get_autolink_pattern($pages);
                
                $fp = fopen(CACHE_DIR.'autolink.dat','w')
-                       or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br>maybe permission is not writable');
+                       or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br />maybe permission is not writable');
+               set_file_buffer($fp, 0);
                flock($fp,LOCK_EX);
+               rewind($fp);
                fputs($fp,$pattern."\n");
-               fputs($fp,join("\t",$forceignorelist));
+               fputs($fp,$pattern_a."\n");
+               fputs($fp,join("\t",$forceignorelist)."\n");
                flock($fp,LOCK_UN);
                fclose($fp);
        }
@@ -209,11 +292,12 @@ function get_pg_passage($page,$sw=TRUE)
 }
 
 // Last-Modified ¥Ø¥Ã¥À
-function header_lastmod()
+function header_lastmod($page=NULL)
 {
        global $lastmod;
        
-       if ($lastmod and is_page($page)) {
+       if ($lastmod and is_page($page))
+       {
                header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
        }
 }
@@ -223,7 +307,7 @@ function get_existpages($dir=DATA_DIR,$ext='.txt')
 {
        $aryret = array();
        
-       $pattern = '^([0-9A-F]+)';
+       $pattern = '^((?:[0-9A-F]{2})+)';
        if ($ext != '')
        {
                $pattern .= preg_quote($ext,'/').'$';
@@ -240,12 +324,161 @@ function get_existpages($dir=DATA_DIR,$ext='.txt')
        closedir($dp);
        return $aryret;
 }
+// ¥Ú¡¼¥¸Ì¾¤ÎÆɤߤòÇÛÎó¤Ë
+function get_readings()
+{
+       global $pagereading_enable, $pagereading_kanji2kana_converter;
+       global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
+       global $pagereading_kakasi_path, $pagereading_config_page;
+       global $pagereading_config_dict;
+       
+       $pages = get_existpages();
+       
+       $readings = array();
+       foreach ($pages as $page) {
+               $readings[$page] = '';
+       }
+       $deletedPage = FALSE;
+       foreach (get_source($pagereading_config_page) as $line) {
+               $line = chop($line);
+               if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
+                       if(isset($readings[$matches[1]])) {
+                               // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸
+                               $readings[$matches[1]] = $matches[2];
+                       } else {
+                               // ºï½ü¤µ¤ì¤¿¥Ú¡¼¥¸
+                               $deletedPage = TRUE;
+                       }
+               }
+       }
+       if($pagereading_enable) {
+               // ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤¬Í­¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
+               $unknownPage = FALSE;
+               // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¤«¥Á¥§¥Ã¥¯
+               foreach ($readings as $page => $reading) {
+                       if($reading=='') {
+                               $unknownPage = TRUE;
+                               break;
+                       }
+               }
+               if($unknownPage) {
+                       // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç¡¢ChaSen/KAKASI ¤ò¼Â¹Ô
+                       switch(strtolower($pagereading_kanji2kana_converter)) {
+                       case 'chasen':
+                               $tmpfname = tempnam(CACHE_DIR, 'PageReading');
+                               $fp = fopen($tmpfname, "w")
+                                       or die_message("cannot write temporary file '$tmpfname'.\n");
+                               foreach ($readings as $page => $reading) {
+                                       if($reading=='') {
+                                               fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
+                                       }
+                               }
+                               fclose($fp);
+                               if(!file_exists($pagereading_chasen_path)) {
+                                       unlink($tmpfname);
+                                       die_message("CHASEN not found: $pagereading_chasen_path");
+                               }
+                               $fp = popen("$pagereading_chasen_path -F %y $tmpfname", "r");
+                               if(!$fp) {
+                                       unlink($tmpfname);
+                                       die_message("ChaSen execution failed: $pagereading_chasen_path -F %y $tmpfname");
+                               }
+                               foreach ($readings as $page => $reading) {
+                                       if($reading=='') {
+                                               $line = fgets($fp);
+                                               $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
+                                               $line = chop($line);
+                                               $readings[$page] = $line;
+                                       }
+                               }
+                               pclose($fp);
+                               unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
+                               break;
+                       case 'kakasi':
+                       case 'kakashi':
+                               $tmpfname = tempnam(CACHE_DIR, 'PageReading');
+                               $fp = fopen($tmpfname, "w")
+                                       or die_message("cannot write temporary file '$tmpfname'.\n");
+                               foreach ($readings as $page => $reading) {
+                                       if($reading=='') {
+                                               fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
+                                       }
+                               }
+                               fclose($fp);
+                               if(!file_exists($pagereading_kakasi_path)) {
+                                       unlink($tmpfname);
+                                       die_message("KAKASI not found: $pagereading_kakasi_path");
+                               }                                       
+                               $fp = popen("$pagereading_kakasi_path -kK -HK -JK <$tmpfname", "r");
+                               if(!$fp) {
+                                       unlink($tmpfname);
+                                       die_message("KAKASI execution failed: $pagereading_kakasi_path -kK -HK -JK <$tmpfname");
+                               }
+                               foreach ($readings as $page => $reading) {
+                                       if($reading=='') {
+                                               $line = fgets($fp);
+                                               $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
+                                               $line = chop($line);
+                                               $readings[$page] = $line;
+                                       }
+                               }
+                               pclose($fp);
+                               unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
+                               break;
+                       case 'none':
+                               $patterns = array();
+                               $replacements = array();
+                               foreach (get_source($pagereading_config_dict) as $line) {
+                                       $line = chop($line);
+                                       if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
+                                               $patterns[] = $matches[1];
+                                               $replacements[] = $matches[2];
+                                       }
+                               }
+                               foreach ($readings as $page => $reading) {
+                                       if($reading=='') {
+                                               $readings[$page] = $page;
+                                               foreach ($patterns as $no => $pattern) {
+                                                       $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern, $replacements[$no], $readings[$page]), "aKCV");
+                                               }
+                                       }
+                               }
+                               break;
+                       default:
+                               die_message("unknown kanji-kana converter: $pagereading_kanji2kana_converter.");
+                               break;
+                       }
+               }
+               if($unknownPage or $deletedPage) {
+                       // Æɤߤǥ½¡¼¥È
+                       asort($readings);
+                       
+                       // ¥Ú¡¼¥¸¤ò½ñ¤­¹þ¤ß
+                       $body = '';
+                       foreach ($readings as $page => $reading) {
+                               $body .= "-[[$page]] $reading\n";
+                       }
+                       page_write($pagereading_config_page, $body);
+               }
+       }
+       
+       // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (ChaSen/KAKASI ¸Æ
+       // ¤Ó½Ð¤·¤¬Ìµ¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤Ë
+       // ¼ºÇÔ¤·¤¿»þ¤Î°Ù)
+       foreach ($pages as $page) {
+               if($readings[$page]=='') {
+                       $readings[$page] = $page;
+               }
+       }
+       
+       return $readings;
+}
 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
 function get_existfiles($dir,$ext)
 {
        $aryret = array();
        
-       $pattern = '^[0-9A-F]+'.preg_quote($ext,'/').'$';
+       $pattern = '^(?:[0-9A-F]{2})+'.preg_quote($ext,'/').'$';
        $dp = @opendir($dir)
                or die_message($dir. ' is not found or not readable.');
        while ($file = readdir($dp)) {