2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.42 2004/07/01 14:52:30 henoheno Exp $
9 function get_source($page=NULL)
15 return str_replace("\r",'',file(get_filename($page)));
18 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
19 function get_filetime($page)
25 return filemtime(get_filename($page)) - LOCALZONE;
28 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
29 function get_filename($page)
31 return DATA_DIR.encode($page).'.txt';
35 function page_write($page,$postdata,$notimestamp=FALSE)
37 $postdata = make_str_rules($postdata);
40 $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
41 $diffdata = do_diff($oldpostdata,$postdata);
42 file_write(DIFF_DIR,$page,$diffdata);
45 make_backup($page,$postdata == '');
48 file_write(DATA_DIR,$page,$postdata,$notimestamp);
50 // TrackBack Ping ¤ÎÁ÷¿®
52 $lines = join("\n",preg_replace('/^\+/','',preg_grep('/^\+/',explode("\n",$diffdata))));
53 tb_send($page,$lines);
55 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
59 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
60 function make_str_rules($str)
62 global $str_rules,$fixed_heading_anchor;
64 $arr = explode("\n",$str);
67 foreach ($arr as $str)
69 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
71 foreach ($str_rules as $rule => $replace)
73 $str = preg_replace("/$rule/",$replace,$str);
76 // ¸«½Ð¤·¤Ë¸ÇÍID¤òÉÕÍ¿¤¹¤ë
77 if ($fixed_heading_anchor and
78 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
81 // ¥é¥ó¥À¥à¤Ê±Ñ»ú(1ʸ»ú)+md5¥Ï¥Ã¥·¥å¤Î¥é¥ó¥À¥à¤ÊÉôʬʸ»úÎó(7ʸ»ú)
82 $anchor = chr(mt_rand(ord('a'),ord('z'))).
83 substr(md5(uniqid(substr($matches[1],0,100),1)),mt_rand(0,24),7);
84 $str = rtrim($matches[1])." [#$anchor]";
89 return join("\n",$retvars);
93 function file_write($dir,$page,$str,$notimestamp=FALSE)
96 global $_msg_invalidiwn;
97 global $notify,$notify_diff_only,$notify_to,$notify_from,$notify_subject,$notify_header;
98 global $smtp_server,$smtp_auth;
100 if (!is_pagename($page))
103 str_replace('$1',htmlspecialchars($page),
104 str_replace('$2','WikiName',$_msg_invalidiwn)
108 $page = strip_bracket($page);
110 $file = $dir.encode($page).'.txt';
112 if ($dir == DATA_DIR and $str == '' and file_exists($file))
115 put_recentdeleted($page);
119 $str = preg_replace("/\r/",'',$str);
120 $str = rtrim($str)."\n";
122 if ($notimestamp and file_exists($file))
124 $timestamp = filemtime($file) - LOCALZONE;
127 $fp = fopen($file,'w')
128 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');
129 set_file_buffer($fp, 0);
137 touch($file,$timestamp + LOCALZONE);
141 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
144 if (!$timestamp and $dir == DATA_DIR)
149 if ($update_exec and $dir == DATA_DIR)
151 system($update_exec.' > /dev/null &');
154 if ($notify and $dir == DIFF_DIR)
156 if ($notify_diff_only)
158 // º¹Ê¬¤À¤±¤òÁ÷¿®¤¹¤ë
159 $str = preg_replace('/^[^-+].*\n/m','',$str);
165 $subject = str_replace('$page',$page,$notify_subject);
166 ini_set('SMTP',$smtp_server);
168 mb_send_mail($notify_to,$subject,$str,$notify_header);
172 // ºï½üÍúÎò¥Ú¡¼¥¸¤Î¹¹¿·
173 function put_recentdeleted($page)
175 global $whatsdeleted,$maxshow_deleted;
177 if ($maxshow_deleted == 0)
181 // update RecentDeleted
183 foreach (get_source($whatsdeleted) as $line)
185 if (preg_match('/^-(.+) - (\[\[.+\]\])$/',$line,$matches))
187 $lines[$matches[2]] = $line;
190 $_page = "[[$page]]";
191 if (array_key_exists($_page,$lines))
193 unset($lines[$_page]);
195 array_unshift($lines,'-'.format_date(UTIME)." - $_page\n");
196 $lines = array_splice($lines,0,$maxshow_deleted);
197 $fp = fopen(get_filename($whatsdeleted),'w')
198 or die_message('cannot write page file '.htmlspecialchars($whatsdeleted).'<br />maybe permission is not writable or filename is too long');
199 set_file_buffer($fp, 0);
202 fputs($fp,join('',$lines));
203 fputs($fp,"#norelated\n"); // :)
208 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
209 function put_lastmodified()
211 global $maxshow,$whatsnew,$non_list,$autolink;
213 $pages = get_existpages();
214 $recent_pages = array();
215 foreach($pages as $page)
217 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
219 $recent_pages[$page] = get_filetime($page);
224 arsort($recent_pages,SORT_NUMERIC);
226 // create recent.dat (for recent.inc.php)
227 $fp = fopen(CACHE_DIR.'recent.dat','w')
228 or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br />maybe permission is not writable or filename is too long');
229 set_file_buffer($fp, 0);
232 foreach ($recent_pages as $page=>$time)
234 fputs($fp,"$time\t$page\n");
239 // create RecentChanges
240 $fp = fopen(get_filename($whatsnew),'w')
241 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br />maybe permission is not writable or filename is too long');
242 set_file_buffer($fp, 0);
245 foreach (array_splice(array_keys($recent_pages),0,$maxshow) as $page)
247 $time = $recent_pages[$page];
248 $s_lastmod = htmlspecialchars(format_date($time));
249 $s_page = htmlspecialchars($page);
250 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
252 fputs($fp,"#norelated\n"); // :)
259 list($pattern,$pattern_a,$forceignorelist) = get_autolink_pattern($pages);
261 $fp = fopen(CACHE_DIR.'autolink.dat','w')
262 or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br />maybe permission is not writable');
263 set_file_buffer($fp, 0);
266 fputs($fp,$pattern."\n");
267 fputs($fp,$pattern_a."\n");
268 fputs($fp,join("\t",$forceignorelist)."\n");
274 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
275 function get_pg_passage($page,$sw=TRUE)
277 global $show_passage;
278 static $pg_passage = array();
285 if (!array_key_exists($page,$pg_passage))
287 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
288 get_passage($time) : '';
291 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
294 // Last-Modified ¥Ø¥Ã¥À
295 function header_lastmod($page=NULL)
299 if ($lastmod and is_page($page))
301 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
305 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
306 function get_existpages($dir=DATA_DIR,$ext='.txt')
310 $pattern = '^((?:[0-9A-F]{2})+)';
313 $pattern .= preg_quote($ext,'/').'$';
316 or die_message($dir. ' is not found or not readable.');
317 while ($file = readdir($dp))
319 if (preg_match("/$pattern/",$file,$matches))
321 $aryret[$file] = decode($matches[1]);
327 // ¥Ú¡¼¥¸Ì¾¤ÎÆɤߤòÇÛÎó¤Ë
328 function get_readings()
330 global $pagereading_enable, $pagereading_kanji2kana_converter;
331 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
332 global $pagereading_kakasi_path, $pagereading_config_page;
333 global $pagereading_config_dict;
335 $pages = get_existpages();
338 foreach ($pages as $page) {
339 $readings[$page] = '';
341 $deletedPage = FALSE;
342 foreach (get_source($pagereading_config_page) as $line) {
344 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
345 if(isset($readings[$matches[1]])) {
346 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸
347 $readings[$matches[1]] = $matches[2];
354 if($pagereading_enable) {
355 // ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤¬Í¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
356 $unknownPage = FALSE;
357 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¤«¥Á¥§¥Ã¥¯
358 foreach ($readings as $page => $reading) {
365 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç¡¢ChaSen/KAKASI ¤ò¼Â¹Ô
366 switch(strtolower($pagereading_kanji2kana_converter)) {
368 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
369 $fp = fopen($tmpfname, "w")
370 or die_message("cannot write temporary file '$tmpfname'.\n");
371 foreach ($readings as $page => $reading) {
373 fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
377 if(!file_exists($pagereading_chasen_path)) {
379 die_message("CHASEN not found: $pagereading_chasen_path");
381 $fp = popen("$pagereading_chasen_path -F %y $tmpfname", "r");
384 die_message("ChaSen execution failed: $pagereading_chasen_path -F %y $tmpfname");
386 foreach ($readings as $page => $reading) {
389 $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
391 $readings[$page] = $line;
395 unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
399 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
400 $fp = fopen($tmpfname, "w")
401 or die_message("cannot write temporary file '$tmpfname'.\n");
402 foreach ($readings as $page => $reading) {
404 fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
408 if(!file_exists($pagereading_kakasi_path)) {
410 die_message("KAKASI not found: $pagereading_kakasi_path");
412 $fp = popen("$pagereading_kakasi_path -kK -HK -JK <$tmpfname", "r");
415 die_message("KAKASI execution failed: $pagereading_kakasi_path -kK -HK -JK <$tmpfname");
417 foreach ($readings as $page => $reading) {
420 $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
422 $readings[$page] = $line;
426 unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
430 $replacements = array();
431 foreach (get_source($pagereading_config_dict) as $line) {
433 if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
434 $patterns[] = $matches[1];
435 $replacements[] = $matches[2];
438 foreach ($readings as $page => $reading) {
440 $readings[$page] = $page;
441 foreach ($patterns as $no => $pattern) {
442 $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern, $replacements[$no], $readings[$page]), "aKCV");
448 die_message("unknown kanji-kana converter: $pagereading_kanji2kana_converter.");
452 if($unknownPage or $deletedPage) {
458 foreach ($readings as $page => $reading) {
459 $body .= "-[[$page]] $reading\n";
461 page_write($pagereading_config_page, $body);
465 // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (ChaSen/KAKASI ¸Æ
466 // ¤Ó½Ð¤·¤¬Ìµ¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤Ë
468 foreach ($pages as $page) {
469 if($readings[$page]=='') {
470 $readings[$page] = $page;
476 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
477 function get_existfiles($dir,$ext)
481 $pattern = '^(?:[0-9A-F]{2})+'.preg_quote($ext,'/').'$';
483 or die_message($dir. ' is not found or not readable.');
484 while ($file = readdir($dp)) {
485 if (preg_match("/$pattern/",$file)) {
486 $aryret[] = $dir.$file;
492 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
493 function links_get_related($page)
495 global $vars,$related;
496 static $links = array();
498 if (array_key_exists($page,$links))
500 return $links[$page];
503 // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
504 $links[$page] = ($page == $vars['page']) ? $related : array();
506 // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
507 $links[$page] += links_get_related_db($vars['page']);
509 return $links[$page];