OSDN Git Service

Renamed sanitize() => input_filter(), to avoid misunderstanding
[pukiwiki/pukiwiki.git] / file.php
index 3899a1b..28eb31c 100644 (file)
--- a/file.php
+++ b/file.php
@@ -2,7 +2,7 @@
 /////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
 //
-// $Id: file.php,v 1.35 2003/10/27 13:07:16 arino Exp $
+// $Id: file.php,v 1.43 2004/07/10 10:59:38 henoheno Exp $
 //
 
 // ¥½¡¼¥¹¤ò¼èÆÀ
@@ -92,10 +92,10 @@ function make_str_rules($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_from,$notify_subject,$notify_header;
-       global $smtp_server,$smtp_auth;
+       global $notify, $notify_diff_only, $notify_to, $notify_subject, $notify_header;
+       global $smtp_server, $smtp_auth;
        
        if (!is_pagename($page))
        {
@@ -112,6 +112,7 @@ function file_write($dir,$page,$str,$notimestamp=FALSE)
        if ($dir == DATA_DIR and $str == '' and file_exists($file))
        {
                unlink($file);
+               put_recentdeleted($page);
        }
        if ($str != '')
        {
@@ -125,7 +126,9 @@ function file_write($dir,$page,$str,$notimestamp=FALSE)
                
                $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');
+               set_file_buffer($fp, 0);
                flock($fp,LOCK_EX);
+               rewind($fp);
                fputs($fp,$str);
                flock($fp,LOCK_UN);
                fclose($fp);
@@ -166,6 +169,42 @@ function file_write($dir,$page,$str,$notimestamp=FALSE)
        }
 }
 
+// ºï½üÍúÎò¥Ú¡¼¥¸¤Î¹¹¿·
+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()
 {
@@ -187,7 +226,9 @@ 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');
+       set_file_buffer($fp, 0);
        flock($fp,LOCK_EX);
+       rewind($fp);
        foreach ($recent_pages as $page=>$time)
        {
                fputs($fp,"$time\t$page\n");
@@ -198,9 +239,12 @@ 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');
+       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");
@@ -212,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');
+               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);
        }
@@ -283,6 +330,7 @@ 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();
        
@@ -290,11 +338,17 @@ function get_readings()
        foreach ($pages as $page) {
                $readings[$page] = '';
        }
+       $deletedPage = FALSE;
        foreach (get_source($pagereading_config_page) as $line) {
-               $line = preg_replace('/[\s\r\n]+$/', '', $line);
-               if(preg_match('/^-\[\[([^]]+)\]\]\s(.+)$/', $line, $matches)
-                  and isset($readings[$matches[1]])) {
-                       $readings[$matches[1]] = $matches[2];
+               $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) {
@@ -308,20 +362,18 @@ function get_readings()
                        }
                }
                if($unknownPage) {
-                       // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç
-                       //                      $tmpfname = tempnam(CACHE_DIR, 'PageReading');
-                       $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);
-                       // ChaSen/KAKASI ¤ò¼Â¹Ô
+                       // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç¡¢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");
@@ -331,9 +383,28 @@ function get_readings()
                                        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");
@@ -343,21 +414,42 @@ function get_readings()
                                        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;
                        }
-                       foreach ($readings as $page => $reading) {
-                               if($reading=='') {
-                                       $line = fgets($fp);
-                                       $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
-                                       $line = preg_replace('/[\s\r\n]+$/', '', $line);
-                                       $readings[$page] = $line;
-                               }
-                       }
-                       pclose($fp);
-                       unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
+               }
+               if($unknownPage or $deletedPage) {
                        // Æɤߤǥ½¡¼¥È
                        asort($readings);