2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.2 2004/08/01 13:00:01 henoheno Exp $
9 function get_source($page = NULL)
11 if (! is_page($page)) {
14 return str_replace("\r", '', file(get_filename($page)));
18 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
19 function get_filetime($page)
21 if (! is_page($page)) {
24 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);
66 $retvars = $matches = array();
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_subject, $notify_header;
98 global $smtp_server, $smtp_auth;
100 if (! is_pagename($page))
101 die_message(str_replace('$1', htmlspecialchars($page),
102 str_replace('$2', 'WikiName', $_msg_invalidiwn))
105 $page = strip_bracket($page);
107 $file = $dir . encode($page) . '.txt';
109 if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
111 put_recentdeleted($page);
115 $str = preg_replace("/\r/", '', $str);
116 $str = rtrim($str) . "\n";
118 if ($notimestamp and file_exists($file)) {
119 $timestamp = filemtime($file) - LOCALZONE;
122 $fp = fopen($file, 'w') or
123 die_message('Cannot write page file or diff file or other ' .
124 htmlspecialchars($page) .
125 '<br />Maybe permission is not writable or filename is too long');
127 set_file_buffer($fp, 0);
134 touch($file, $timestamp + LOCALZONE);
138 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë
139 is_page($page, TRUE);
141 if (! $timestamp and $dir == DATA_DIR) {
145 if ($update_exec and $dir == DATA_DIR) {
146 system($update_exec . ' > /dev/null &');
149 if ($notify and $dir == DIFF_DIR) {
150 if ($notify_diff_only) { // º¹Ê¬¤À¤±¤òÁ÷¿®¤¹¤ë
151 $str = preg_replace('/^[^-+].*\n/m', '', $str);
156 $subject = str_replace('$page', $page, $notify_subject);
157 ini_set('SMTP', $smtp_server);
159 mb_send_mail($notify_to, $subject, $str, $notify_header);
163 // ºï½üÍúÎò¥Ú¡¼¥¸¤Î¹¹¿·
164 function put_recentdeleted($page)
166 global $whatsdeleted, $maxshow_deleted;
168 if ($maxshow_deleted == 0) return;
170 // Update RecentDeleted
171 $lines = $matches = array();
172 foreach (get_source($whatsdeleted) as $line) {
173 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
174 $lines[$matches[2]] = $line;
177 $_page = "[[$page]]";
178 if (isset($lines[$_page])) unset($lines[$_page]);
180 array_unshift($lines, '-' . format_date(UTIME) . " - $_page\n");
181 $lines = array_splice($lines, 0, $maxshow_deleted);
183 $fp = fopen(get_filename($whatsdeleted), 'w') or
184 die_message('Cannot write page file ' .
185 htmlspecialchars($whatsdeleted) .
186 '<br />Maybe permission is not writable or filename is too long');
188 set_file_buffer($fp, 0);
191 fputs($fp, join('', $lines));
192 fputs($fp, "#norelated\n"); // :)
197 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
198 function put_lastmodified()
200 global $maxshow, $whatsnew, $non_list, $autolink;
202 $pages = get_existpages();
203 $recent_pages = array();
204 foreach($pages as $page) {
205 if ($page != $whatsnew and ! preg_match("/$non_list/", $page))
206 $recent_pages[$page] = get_filetime($page);
210 arsort($recent_pages, SORT_NUMERIC);
212 // create recent.dat (for recent.inc.php)
213 $fp = fopen(CACHE_DIR . 'recent.dat', 'w') or
214 die_message('Cannot write cache file ' .
215 CACHE_DIR . 'recent.dat' .
216 '<br />Maybe permission is not writable or filename is too long');
218 set_file_buffer($fp, 0);
221 foreach ($recent_pages as $page=>$time) {
222 fputs($fp, "$time\t$page\n");
227 // create RecentChanges
228 $fp = fopen(get_filename($whatsnew), 'w') or
229 die_message('Cannot write page file ' .
230 htmlspecialchars($whatsnew) .
231 '<br />Maybe permission is not writable or filename is too long');
233 set_file_buffer($fp, 0);
236 foreach (array_splice(array_keys($recent_pages), 0, $maxshow) as $page) {
237 $time = $recent_pages[$page];
238 $s_lastmod = htmlspecialchars(format_date($time));
239 $s_page = htmlspecialchars($page);
240 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
242 fputs($fp, "#norelated\n"); // :)
248 list($pattern, $pattern_a, $forceignorelist) = get_autolink_pattern($pages);
250 $fp = fopen(CACHE_DIR . 'autolink.dat', 'w') or
251 die_message('Cannot write autolink file ' .
252 CACHE_DIR . '/autolink.dat' .
253 '<br />Maybe permission is not writable');
254 set_file_buffer($fp, 0);
257 fputs($fp, $pattern . "\n");
258 fputs($fp, $pattern_a . "\n");
259 fputs($fp, join("\t", $forceignorelist) . "\n");
265 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
266 function get_pg_passage($page, $sw = TRUE)
268 global $show_passage;
269 static $pg_passage = array();
271 if (! $show_passage) return '';
273 if (! array_key_exists($page, $pg_passage)) {
274 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
275 get_passage($time) : '';
278 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
281 // Last-Modified ¥Ø¥Ã¥À
282 function header_lastmod($page = NULL)
286 if ($lastmod and is_page($page)) {
287 header('Last-Modified: ' .
288 date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
292 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
293 function get_existpages($dir = DATA_DIR, $ext = '.txt')
297 $pattern = '^((?:[0-9A-F]{2})+)';
299 $pattern .= preg_quote($ext, '/') . '$';
301 $dp = @opendir($dir) or
302 die_message($dir . ' is not found or not readable.');
304 while ($file = readdir($dp)) {
305 if (preg_match("/$pattern/", $file, $matches))
306 $aryret[$file] = decode($matches[1]);
313 // ¥Ú¡¼¥¸Ì¾¤ÎÆɤߤòÇÛÎó¤Ë
314 function get_readings()
316 global $pagereading_enable, $pagereading_kanji2kana_converter;
317 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
318 global $pagereading_kakasi_path, $pagereading_config_page;
319 global $pagereading_config_dict;
321 $pages = get_existpages();
324 foreach ($pages as $page) {
325 $readings[$page] = '';
328 $deletedPage = FALSE;
330 foreach (get_source($pagereading_config_page) as $line) {
332 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
333 if(isset($readings[$matches[1]])) {
334 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸
335 $readings[$matches[1]] = $matches[2];
343 // ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤¬Í¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
344 if($pagereading_enable) {
346 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¤«¥Á¥§¥Ã¥¯
347 $unknownPage = FALSE;
348 foreach ($readings as $page => $reading) {
355 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç¡¢ChaSen/KAKASI ¤ò¼Â¹Ô
357 switch(strtolower($pagereading_kanji2kana_converter)) {
359 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
360 $fp = fopen($tmpfname, "w") or
361 die_message("Cannot write temporary file '$tmpfname'.\n");
362 foreach ($readings as $page => $reading) {
364 fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
369 if(! file_exists($pagereading_chasen_path)) {
371 die_message("ChaSen not found: $pagereading_chasen_path");
374 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
375 $fp = popen($chasen, "r");
378 die_message("ChaSen execution failed: $chasen");
380 foreach ($readings as $page => $reading) {
383 $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
385 $readings[$page] = $line;
390 unlink($tmpfname) or die_message("Temporary file can not be removed: $tmpfname");
395 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
396 $fp = fopen($tmpfname, "w") or
397 die_message("Cannot write temporary file '$tmpfname'.\n");
398 foreach ($readings as $page => $reading) {
400 fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
405 if(! file_exists($pagereading_kakasi_path)) {
407 die_message("KAKASI not found: $pagereading_kakasi_path");
410 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
411 $fp = popen($kakasi, "r");
414 die_message("KAKASI execution failed: $kakasi");
416 foreach ($readings as $page => $reading) {
419 $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
421 $readings[$page] = $line;
426 unlink($tmpfname) or die_message("Temporary file can not be removed: $tmpfname");
430 $patterns = $replacements = $matches = 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");
449 die_message("Unknown kanji-kana converter: $pagereading_kanji2kana_converter.");
454 if($unknownPage or $deletedPage) {
456 asort($readings); // Æɤߤǥ½¡¼¥È
460 foreach ($readings as $page => $reading) {
461 $body .= "-[[$page]] $reading\n";
463 page_write($pagereading_config_page, $body);
467 // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (ChaSen/KAKASI ¸Æ
468 // ¤Ó½Ð¤·¤¬Ìµ¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤Ë
470 foreach ($pages as $page) {
471 if($readings[$page] == '') {
472 $readings[$page] = $page;
479 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
480 function get_existfiles($dir, $ext)
484 $pattern = '^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$';
485 $dp = @opendir($dir) or
486 die_message($dir . ' is not found or not readable.');
487 while ($file = readdir($dp)) {
488 if (preg_match("/$pattern/", $file))
489 $aryret[] = $dir . $file;
495 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
496 function links_get_related($page)
498 global $vars, $related;
499 static $links = array();
501 if (isset($links[$page])) 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];