OSDN Git Service

Renamed sanitize() => input_filter(), to avoid misunderstanding
[pukiwiki/pukiwiki.git] / file.php
index d9a4af2..28eb31c 100644 (file)
--- a/file.php
+++ b/file.php
 <?php
-// PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: file.php,v 1.3 2002/11/29 00:09:00 panda Exp $
 /////////////////////////////////////////////////
+// PukiWiki - Yet another WikiWikiWeb clone.
+//
+// $Id: file.php,v 1.43 2004/07/10 10:59:38 henoheno Exp $
+//
 
 // ¥½¡¼¥¹¤ò¼èÆÀ
-function get_source($page)
-{      
-  if(page_exists($page)) {
-     return file(get_filename(encode($page)));
-  }
+function get_source($page=NULL)
+{
+       if (!is_page($page))
+       {
+               return array();
+       }
+       return str_replace("\r",'',file(get_filename($page)));
+}
 
-  return array();
+// ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
+function get_filetime($page)
+{
+       if (!is_page($page))
+       {
+               return 0;
+       }
+       return filemtime(get_filename($page)) - LOCALZONE;
 }
 
-// ¥Ú¡¼¥¸¤¬Â¸ºß¤¹¤ë¤«¡©
-function page_exists($page)
+// ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
+function get_filename($page)
 {
-       return file_exists(get_filename(encode($page)));
+       return DATA_DIR.encode($page).'.txt';
 }
 
-// ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
-function file_write($dir,$page,$str)
+// ¥Ú¡¼¥¸¤Î½ÐÎÏ
+function page_write($page,$postdata,$notimestamp=FALSE)
 {
-       global $post,$update_exec;
+       $postdata = make_str_rules($postdata);
+       
+       // º¹Ê¬¥Õ¥¡¥¤¥ë¤ÎºîÀ®
+       $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
+       $diffdata = do_diff($oldpostdata,$postdata);
+       file_write(DIFF_DIR,$page,$diffdata);
+       
+       // ¥Ð¥Ã¥¯¥¢¥Ã¥×¤ÎºîÀ®
+       make_backup($page,$postdata == '');
+       
+       // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
+       file_write(DATA_DIR,$page,$postdata,$notimestamp);
+       
+       // TrackBack Ping ¤ÎÁ÷¿®
+       // ¡ÖÄɲá׹ԤòÃê½Ð
+       $lines = join("\n",preg_replace('/^\+/','',preg_grep('/^\+/',explode("\n",$diffdata))));
+       tb_send($page,$lines);
+       
+       // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
+       links_update($page);
+}
 
-       if($str == "")
+// ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
+function make_str_rules($str)
+{
+       global $str_rules,$fixed_heading_anchor;
+       
+       $arr = explode("\n",$str);
+       
+       $retvars = array();
+       foreach ($arr as $str)
+       {
+               if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
+               {
+                       foreach ($str_rules as $rule => $replace)
+                       {
+                               $str = preg_replace("/$rule/",$replace,$str);
+                       }
+               }
+               // ¸«½Ð¤·¤Ë¸ÇÍ­ID¤òÉÕÍ¿¤¹¤ë
+               if ($fixed_heading_anchor and
+                       preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
+               {
+                       // ¸ÇÍ­ID¤òÀ¸À®¤¹¤ë
+                       // ¥é¥ó¥À¥à¤Ê±Ñ»ú(1ʸ»ú)+md5¥Ï¥Ã¥·¥å¤Î¥é¥ó¥À¥à¤ÊÉôʬʸ»úÎó(7ʸ»ú)
+                       $anchor = chr(mt_rand(ord('a'),ord('z'))).
+                               substr(md5(uniqid(substr($matches[1],0,100),1)),mt_rand(0,24),7);
+                       $str = rtrim($matches[1])." [#$anchor]";
+               }
+               $retvars[] = $str;
+       }
+       
+       return join("\n",$retvars);
+}
+
+// ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
+function file_write($dir,$page,$str,$notimestamp=FALSE)
+{
+       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))
+       {
+               die_message(
+                       str_replace('$1',htmlspecialchars($page),
+                               str_replace('$2','WikiName',$_msg_invalidiwn)
+                       )
+               );
+       }
+       $page = strip_bracket($page);
+       $timestamp = FALSE;
+       $file = $dir.encode($page).'.txt';
+       
+       if ($dir == DATA_DIR and $str == '' and file_exists($file))
        {
-               @unlink($dir.encode($page).".txt");
+               unlink($file);
+               put_recentdeleted($page);
        }
-       else
+       if ($str != '')
        {
-               $str = preg_replace("/\x0D\x0A|\x0D|\x0A/","\n",$str);
+               $str = preg_replace("/\r/",'',$str);
+               $str = rtrim($str)."\n";
                
-               if($post["notimestamp"] && is_page($page))
+               if ($notimestamp and file_exists($file))
                {
-                       $timestamp = @filemtime($dir.encode($page).".txt");
+                       $timestamp = filemtime($file) - LOCALZONE;
                }
-               $fp = fopen($dir.encode($page).".txt","w");
-               if($fp===FALSE) die_message("cannot write page file or diff file or other".htmlspecialchars($page)."<br>maybe permission is not writable or filename is too long");
-               while(!flock($fp,LOCK_EX));
+               
+               $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);
-               if($timestamp)
-                       touch($dir.encode($page).".txt",$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)
        {
-               system($update_exec." > /dev/null &");
+               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_lastmodified()
+// ºï½üÍúÎò¥Ú¡¼¥¸¤Î¹¹¿·
+function put_recentdeleted($page)
 {
-       global $script,$maxshow,$whatsnew,$date_format,$time_format,$weeklabels,$post,$non_list;
-
-       if($post["notimestamp"]) return;
-
-       $files = get_existpages();
-       foreach($files as $page) {
-               if($page == $whatsnew) continue;
-               if(preg_match("/$non_list/",$page)) continue;
-
-               if(file_exists(get_filename(encode($page))))
-                       {
-                       $page_url = rawurlencode($page);
-                       $lastmodtime = filemtime(get_filename(encode($page)));
-                       $lastmod = date($date_format,$lastmodtime)
-                                . " (" . $weeklabels[date("w",$lastmodtime)] . ") "
-                                . date($time_format,$lastmodtime);
-                       $putval[$lastmodtime][] = "-$lastmod - $page";
-               }
-       }
+       global $whatsdeleted,$maxshow_deleted;
        
-       $cnt = 1;
-       krsort($putval);
-       $fp = fopen(get_filename(encode($whatsnew)),"w");
-       if($fp===FALSE) die_message("cannot write page file ".htmlspecialchars($whatsnew)."<br>maybe permission is not writable or filename is too long");
-       flock($fp,LOCK_EX);
-       foreach($putval as $pages)
+       if ($maxshow_deleted == 0)
+       {
+               return;
+       }
+       // update RecentDeleted
+       $lines = array();
+       foreach (get_source($whatsdeleted) as $line)
        {
-               foreach($pages as $page)
+               if (preg_match('/^-(.+) - (\[\[.+\]\])$/',$line,$matches))
                {
-                       fputs($fp,$page."\n");
-                       $cnt++;
-                       if($cnt > $maxshow) break;
+                       $lines[$matches[2]] = $line;
                }
-               if($cnt > $maxshow) break;
        }
+       $_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 get_filename($pagename)
-{
-       return DATA_DIR.$pagename.".txt";
-}
-
-// ¥Ú¡¼¥¸¤¬Â¸ºß¤¹¤ë¤«¤·¤Ê¤¤¤«
-function is_page($page,$reload=false)
-{
-       global $InterWikiName,$_ispage;
-
-       if(($_ispage[$page] === true || $_ispage[$page] === false) && !$reload) return $_ispage[$page];
-
-       if(preg_match("/($InterWikiName)/",$page))
-               $_ispage[$page] = false;
-       else if(!page_exists($page))
-               $_ispage[$page] = false;
-       else
-               $_ispage[$page] = true;
-       
-       return $_ispage[$page];
-}
-
-// ¥Ú¡¼¥¸¤¬ÊÔ½¸²Äǽ¤«
-function is_editable($page)
-{
-       global $BracketName,$WikiName,$InterWikiName,$cantedit,$_editable;
-
-       if($_editable === true || $_editable === false) return $_editable;
-
-       if(preg_match("/^$InterWikiName$/",$page))
-               $_editable = false;
-       elseif(!preg_match("/^$BracketName$/",$page) && !preg_match("/^$WikiName$/",$page))
-               $_editable = false;
-       else if(in_array($page,$cantedit))
-               $_editable = false;
-       else
-               $_editable = true;
-       
-       return $_editable;
-}
-
-// ¥Ú¡¼¥¸¤¬Åà·ë¤µ¤ì¤Æ¤¤¤ë¤«
-function is_freeze($page)
+// ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
+function put_lastmodified()
 {
-       global $_freeze;
-
-       if(!is_page($page)) return false;
-       if($_freeze === true || $_freeze === false) return $_freeze;
+       global $maxshow,$whatsnew,$non_list,$autolink;
 
-       $lines = get_source($page);
+       $pages = get_existpages();
+       $recent_pages = array();
+       foreach($pages as $page)
+       {
+               if ($page != $whatsnew and !preg_match("/$non_list/",$page))
+               {
+                       $recent_pages[$page] = get_filetime($page);
+               }
+       }
        
-       if($lines[0] == "#freeze\n")
-               $_freeze = true;
-       else
-               $_freeze = false;
+       //»þ¹ï¹ß½ç¤Ç¥½¡¼¥È
+       arsort($recent_pages,SORT_NUMERIC);
        
-       return $_freeze;
+       // 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");
+       }
+       flock($fp,LOCK_UN);
+       fclose($fp);
+
+       // 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);
+       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");
+       }
+       fputs($fp,"#norelated\n"); // :)
+       flock($fp,LOCK_UN);
+       fclose($fp);
+       
+       // for autolink
+       if ($autolink)
+       {
+               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,$pattern_a."\n");
+               fputs($fp,join("\t",$forceignorelist)."\n");
+               flock($fp,LOCK_UN);
+               fclose($fp);
+       }
 }
 
 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
-function get_pg_passage($page,$sw=true)
+function get_pg_passage($page,$sw=TRUE)
 {
-       global $_pg_passage,$show_passage;
-
-       if(!$show_passage) return "";
-
-       if(isset($_pg_passage[$page]))
-       {
-               if($sw)
-                       return $_pg_passage[$page]["str"];
-               else
-                       return $_pg_passage[$page]["label"];
-       }
-       if($pgdt = @filemtime(get_filename(encode($page))))
-       {
-               $pgdt = UTIME - $pgdt;
-               if(ceil($pgdt / 60) < 60)
-                       $_pg_passage[$page]["label"] = "(".ceil($pgdt / 60)."m)";
-               else if(ceil($pgdt / 60 / 60) < 24)
-                       $_pg_passage[$page]["label"] = "(".ceil($pgdt / 60 / 60)."h)";
-               else
-                       $_pg_passage[$page]["label"] = "(".ceil($pgdt / 60 / 60 / 24)."d)";
-               
-               $_pg_passage[$page]["str"] = "<small>".$_pg_passage[$page]["label"]."</small>";
+       global $show_passage;
+       static $pg_passage = array();
+       
+       if (!$show_passage)
+       {
+               return '';
        }
-       else
+       
+       if (!array_key_exists($page,$pg_passage))
        {
-               $_pg_passage[$page]["label"] = "";
-               $_pg_passage[$page]["str"] = "";
+               $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
+                       get_passage($time) : '';
        }
-
-       if($sw)
-               return $_pg_passage[$page]["str"];
-       else
-               return $_pg_passage[$page]["label"];
+       
+       return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
 }
 
 // Last-Modified ¥Ø¥Ã¥À
-function header_lastmod($page)
+function header_lastmod($page=NULL)
 {
        global $lastmod;
        
-       if($lastmod && is_page($page))
+       if ($lastmod and is_page($page))
        {
-               header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime(get_filename(encode($page))))." GMT");
+               header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
        }
 }
 
 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
-function get_existpages()
+function get_existpages($dir=DATA_DIR,$ext='.txt')
 {
        $aryret = array();
-
-       if ($dir = @opendir(DATA_DIR))
+       
+       $pattern = '^((?:[0-9A-F]{2})+)';
+       if ($ext != '')
+       {
+               $pattern .= preg_quote($ext,'/').'$';
+       }
+       $dp = @opendir($dir)
+               or die_message($dir. ' is not found or not readable.');
+       while ($file = readdir($dp))
        {
-               while($file = readdir($dir))
+               if (preg_match("/$pattern/",$file,$matches))
                {
-                       if($file == ".." || $file == "." || strstr($file,".txt")===FALSE) continue;
-                       $page = decode(trim(preg_replace("/\.txt$/"," ",$file)));
-                       array_push($aryret,$page);
+                       $aryret[$file] = decode($matches[1]);
+               }
+       }
+       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);
                }
-               closedir($dir);
        }
        
+       // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (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]{2})+'.preg_quote($ext,'/').'$';
+       $dp = @opendir($dir)
+               or die_message($dir. ' is not found or not readable.');
+       while ($file = readdir($dp)) {
+               if (preg_match("/$pattern/",$file)) {
+                       $aryret[] = $dir.$file;
+               }
+       }
+       closedir($dp);
        return $aryret;
 }
+//¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
+function links_get_related($page)
+{
+       global $vars,$related;
+       static $links = array();
+       
+       if (array_key_exists($page,$links))
+       {
+               return $links[$page];
+       }
+       
+       // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
+       $links[$page] = ($page == $vars['page']) ? $related : array();
+       
+       // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
+       $links[$page] += links_get_related_db($vars['page']);
+       
+       return $links[$page];
+}
 ?>