2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: file.php,v 1.59 2006/04/12 14:38:51 henoheno Exp $
5 // 2002-2006 PukiWiki Developers Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
9 // File related functions
12 define('PKWK_MAXSHOW_CACHE', 'recent.dat');
13 define('PKWK_MAXSHOW_ALLOWANCE', 10);
16 define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
18 // Get source(wiki text) data of the page
19 function get_source($page = NULL, $lock = TRUE)
24 $path = get_filename($page);
27 $fp = @fopen($path, 'r');
28 if ($fp == FALSE) return $array;
32 // Removing line-feeds: Because file() doesn't remove them.
33 $array = str_replace("\r", '', file($path));
44 // Get last-modified filetime of the page
45 function get_filetime($page)
47 return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
50 // Get physical file name of the page
51 function get_filename($page)
53 return DATA_DIR . encode($page) . '.txt';
56 // Put a data(wiki text) into a physical file(diff, backup, text)
57 function page_write($page, $postdata, $notimestamp = FALSE)
61 if (PKWK_READONLY) return; // Do nothing
63 $postdata = make_str_rules($postdata);
65 // Create and write diff
66 $oldpostdata = is_page($page) ? join('', get_source($page)) : '';
67 $diffdata = do_diff($oldpostdata, $postdata);
68 file_write(DIFF_DIR, $page, $diffdata);
71 make_backup($page, $postdata == ''); // Is $postdata null?
74 file_write(DATA_DIR, $page, $postdata, $notimestamp);
78 $_diff = explode("\n", $diffdata);
79 $plus = join("\n", preg_replace('/^\+/', '', preg_grep('/^\+/', $_diff)));
80 $minus = join("\n", preg_replace('/^-/', '', preg_grep('/^-/', $_diff)));
81 tb_send($page, $plus, $minus);
87 // Modify original text with user-defined / system-defined rules
88 function make_str_rules($source)
90 global $str_rules, $fixed_heading_anchor;
92 $lines = explode("\n", $source);
93 $count = count($lines);
98 for ($i = 0; $i < $count; $i++) {
99 $line = & $lines[$i]; // Modify directly
101 // Ignore null string and preformatted texts
102 if ($line == '' || $line{0} == ' ' || $line{0} == "\t") continue;
106 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
108 preg_match('/#[^{]*(\{\{+)\s*$/', $line, $matches)) {
109 // Multiline convert plugin start
111 $multiline = strlen($matches[1]); // Set specific number
114 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
116 preg_match('/^\}{' . $multiline . '}\s*$/', $line)) {
117 // Multiline convert plugin end
122 if ($modify === FALSE) continue;
124 // Replace with $str_rules
125 foreach ($str_rules as $pattern => $replacement)
126 $line = preg_replace('/' . $pattern . '/', $replacement, $line);
128 // Adding fixed anchor into headings
129 if ($fixed_heading_anchor &&
130 preg_match('/^(\*{1,3}.*?)(?:\[#([A-Za-z][\w-]*)\]\s*)?$/', $line, $matches) &&
131 (! isset($matches[2]) || $matches[2] == '')) {
132 // Generate unique id
133 $anchor = generate_fixed_heading_anchor_id($matches[1]);
134 $line = rtrim($matches[1]) . ' [#' . $anchor . ']';
138 // Multiline part has no stopper
139 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
140 $modify === FALSE && $multiline != 0)
141 $lines[] = str_repeat('}', $multiline);
143 return implode("\n", $lines);
147 function generate_fixed_heading_anchor_id($seed)
149 // A random alphabetic letter + 7 letters of random strings from md()
150 return chr(mt_rand(ord('a'), ord('z'))) .
151 substr(md5(uniqid(substr($seed, 0, 100), TRUE)),
155 // Read top N lines as an array
156 // (Use PHP file() function if you want to get ALL lines)
157 function file_head($file, $count = 1, $lock = TRUE, $buffer = 8192)
161 $fp = @fopen($file, 'r');
162 if ($fp === FALSE) return FALSE;
163 set_file_buffer($fp, 0);
164 if ($lock) flock($fp, LOCK_SH);
167 while (! feof($fp)) {
168 $line = fgets($fp, $buffer);
169 if ($line != FALSE) $array[] = $line;
170 if (++$index >= $count) break;
172 if ($lock) flock($fp, LOCK_UN);
173 if (! fclose($fp)) return FALSE;
179 function file_write($dir, $page, $str, $notimestamp = FALSE)
181 global $update_exec, $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
182 global $whatsdeleted, $maxshow_deleted;
184 if (PKWK_READONLY) return; // Do nothing
185 if ($dir != DATA_DIR && $dir != DIFF_DIR) die('file_write(): Invalid directory');
187 $page = strip_bracket($page);
188 $file = $dir . encode($page) . '.txt';
189 $file_exists = file_exists($file);
194 if ($dir == DATA_DIR && $str === '') {
196 if (! $file_exists) return; // Ignore null posting for DATA_DIR
198 // Update RecentDeleted (Add the $page)
199 add_recent($page, $whatsdeleted, '', $maxshow_deleted);
203 // Update RecentChanges (Remove the $page from RecentChanges)
206 // Clear is_page() cache
207 is_page($page, TRUE);
211 } else if ($dir == DIFF_DIR && $str === " \n") {
212 return; // Ignore null posting for DIFF_DIR
216 // File replacement (Edit)
218 if (! is_pagename($page))
219 die_message(str_replace('$1', htmlspecialchars($page),
220 str_replace('$2', 'WikiName', $_msg_invalidiwn)));
222 $str = rtrim(preg_replace('/' . "\r" . '/', '', $str)) . "\n";
223 $timestamp = ($file_exists && $notimestamp) ? filemtime($file) : FALSE;
225 $fp = fopen($file, 'a') or die('fopen() failed: ' .
226 htmlspecialchars(basename($dir) . '/' . encode($page) . '.txt') .
228 'Maybe permission is not writable or filename is too long');
229 set_file_buffer($fp, 0);
237 if ($timestamp) pkwk_touch_file($file, $timestamp);
240 if ($dir == DATA_DIR) {
241 // Update RecentChanges (Add or renew the $page)
242 if ($timestamp === FALSE) lastmodified_add($page);
244 // Execute $update_exec here
245 if ($update_exec) system($update_exec . ' > /dev/null &');
247 } else if ($dir == DIFF_DIR && $notify) {
248 if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
249 $footer['ACTION'] = 'Page update';
250 $footer['PAGE'] = & $page;
251 $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
252 $footer['USER_AGENT'] = TRUE;
253 $footer['REMOTE_ADDR'] = TRUE;
254 pkwk_mail_notify($notify_subject, $str, $footer) or
255 die('pkwk_mail_notify(): Failed');
258 is_page($page, TRUE); // Clear is_page() cache
261 // Update RecentDeleted
262 function add_recent($page, $recentpage, $subject = '', $limit = 0)
264 if (PKWK_READONLY || $limit == 0 || $page == '' || $recentpage == '' ||
265 check_non_list($page)) return;
268 $lines = $matches = array();
269 foreach (get_source($recentpage) as $line)
270 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
271 $lines[$matches[2]] = $line;
273 $_page = '[[' . $page . ']]';
275 // Remove a report about the same page
276 if (isset($lines[$_page])) unset($lines[$_page]);
279 array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
280 htmlspecialchars($subject) . "\n");
282 // Get latest $limit reports
283 $lines = array_splice($lines, 0, $limit);
286 $fp = fopen(get_filename($recentpage), 'w') or
287 die_message('Cannot write page file ' .
288 htmlspecialchars($recentpage) .
289 '<br />Maybe permission is not writable or filename is too long');
290 set_file_buffer($fp, 0);
293 fputs($fp, '#freeze' . "\n");
294 fputs($fp, '#norelated' . "\n"); // :)
295 fputs($fp, join('', $lines));
300 // Update PKWK_MAXSHOW_CACHE itself (Add or renew about the $page) (Light)
301 // Use without $autolink
302 function lastmodified_add($page = '')
304 global $maxshow, $whatsnew, $autolink;
306 $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
307 if ($autolink || ! file_exists($file)) {
308 put_lastmodified(); // Try to (re)create ALL
313 pkwk_touch_file($file);
314 $fp = fopen($file, 'r+') or
315 die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_MAXSHOW_CACHE);
316 set_file_buffer($fp, 0);
320 $recent_pages = $matches = array();
321 foreach(file_head($file, $maxshow + PKWK_MAXSHOW_ALLOWANCE, FALSE) as $line)
322 if (preg_match('/^([0-9]+)\t(.+)/', $line, $matches))
323 $recent_pages[$matches[2]] = $matches[1];
326 if (isset($recent_pages[$page])) unset($recent_pages[$page]);
328 // Add: array_unshift()
329 $recent_pages = array($page => get_filetime($page)) + $recent_pages;
334 foreach ($recent_pages as $_page=>$time)
335 fputs($fp, $time . "\t" . $_page . "\n");
342 // Update RecentChanges for the $page (VERBOSE! VERBOSE!)
344 $file = get_filename($whatsnew);
345 $s_page = htmlspecialchars($page);
348 pkwk_touch_file($file);
349 $fp = fopen($file, 'r+') or
350 die_message('Cannot open ' . htmlspecialchars($whatsnew));
351 set_file_buffer($fp, 0);
355 $recent_pages = $matches = array();
356 foreach(file_head($file, $maxshow, FALSE) as $line)
357 if (preg_match('/^(- *[0-9].* - )\[\[(.+)\]\]$/', $line, $matches))
358 $recent_pages[$matches[2]] = $matches[1];
360 // If it already exists
361 if (isset($recent_pages[$s_page])) {
362 unset($recent_pages[$s_page]); // Remove it for renewal
364 array_pop($recent_pages); // Remove the oldest one for $maxshow limit
367 // Add: array_unshift()
368 $s_lastmod = htmlspecialchars(format_date(get_filetime($page)));
369 $recent_pages = array($page => '-' . $s_lastmod . ' - ') + $recent_pages;
374 foreach ($recent_pages as $page=>$line)
375 fputs($fp, $line . '[[' . $page . ']]' . "\n");
376 fputs($fp, '#norelated' . "\n"); // :)
382 // Re-create PKWK_MAXSHOW_CACHE (Heavy)
383 function put_lastmodified()
385 global $maxshow, $whatsnew, $autolink;
387 if (PKWK_READONLY) return; // Do nothing
389 // Get WHOLE page list
390 $pages = get_existpages();
392 // Check ALL filetime
393 $recent_pages = array();
394 foreach($pages as $page)
395 if ($page != $whatsnew && ! check_non_list($page))
396 $recent_pages[$page] = get_filetime($page);
398 // Sort decending order of last-modification date
399 arsort($recent_pages, SORT_NUMERIC);
402 $recent_pages = array_splice($recent_pages, 0, $maxshow + PKWK_MAXSHOW_ALLOWANCE);
404 // Re-create PKWK_MAXSHOW_CACHE
405 $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
406 pkwk_touch_file($file);
407 $fp = fopen($file, 'r+') or
408 die_message('Cannot open' . 'CACHE_DIR/' . PKWK_MAXSHOW_CACHE);
409 set_file_buffer($fp, 0);
413 foreach ($recent_pages as $page=>$time)
414 fputs($fp, $time . "\t" . $page . "\n");
418 // Create RecentChanges
419 $file = get_filename($whatsnew);
420 pkwk_touch_file($file);
421 $fp = fopen($file, 'r+') or
422 die_message('Cannot open ' . htmlspecialchars($whatsnew));
423 set_file_buffer($fp, 0);
427 foreach (array_keys($recent_pages) as $page) {
428 $time = $recent_pages[$page];
429 $s_lastmod = htmlspecialchars(format_date($time));
430 $s_page = htmlspecialchars($page);
431 fputs($fp, '-' . $s_lastmod . ' - [[' . $s_page . ']]' . "\n");
433 fputs($fp, '#norelated' . "\n"); // :)
439 list($pattern, $pattern_a, $forceignorelist) =
440 get_autolink_pattern($pages);
442 $file = CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE;
443 pkwk_touch_file($file);
444 $fp = fopen($file, 'r+') or
445 die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_AUTOLINK_REGEX_CACHE);
446 set_file_buffer($fp, 0);
450 fputs($fp, $pattern . "\n");
451 fputs($fp, $pattern_a . "\n");
452 fputs($fp, join("\t", $forceignorelist) . "\n");
458 // Get elapsed date of the page
459 function get_pg_passage($page, $sw = TRUE)
461 global $show_passage;
462 if (! $show_passage) return '';
464 $time = get_filetime($page);
465 $pg_passage = ($time != 0) ? get_passage($time) : '';
467 return $sw ? '<small>' . $pg_passage . '</small>' : ' ' . $pg_passage;
470 // Last-Modified header
471 function header_lastmod($page = NULL)
475 if ($lastmod && is_page($page)) {
477 header('Last-Modified: ' .
478 date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
482 // Get a page list of this wiki
483 function get_existpages($dir = DATA_DIR, $ext = '.txt')
487 $pattern = '((?:[0-9A-F]{2})+)';
488 if ($ext != '') $ext = preg_quote($ext, '/');
489 $pattern = '/^' . $pattern . $ext . '$/';
491 $dp = @opendir($dir) or
492 die_message($dir . ' is not found or not readable.');
494 while ($file = readdir($dp))
495 if (preg_match($pattern, $file, $matches))
496 $aryret[$file] = decode($matches[1]);
502 // Get PageReading(pronounce-annotated) data in an array()
503 function get_readings()
505 global $pagereading_enable, $pagereading_kanji2kana_converter;
506 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
507 global $pagereading_kakasi_path, $pagereading_config_page;
508 global $pagereading_config_dict;
510 $pages = get_existpages();
513 foreach ($pages as $page)
514 $readings[$page] = '';
516 $deletedPage = FALSE;
518 foreach (get_source($pagereading_config_page) as $line) {
520 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
521 if(isset($readings[$matches[1]])) {
522 // This page is not clear how to be pronounced
523 $readings[$matches[1]] = $matches[2];
525 // This page seems deleted
531 // If enabled ChaSen/KAKASI execution
532 if($pagereading_enable) {
534 // Check there's non-clear-pronouncing page
535 $unknownPage = FALSE;
536 foreach ($readings as $page => $reading) {
543 // Execute ChaSen/KAKASI, and get annotation
545 switch(strtolower($pagereading_kanji2kana_converter)) {
547 if(! file_exists($pagereading_chasen_path))
548 die_message('ChaSen not found: ' . $pagereading_chasen_path);
550 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
551 $fp = fopen($tmpfname, 'w') or
552 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
553 foreach ($readings as $page => $reading) {
554 if($reading != '') continue;
555 fputs($fp, mb_convert_encoding($page . "\n",
556 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
560 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
561 $fp = popen($chasen, 'r');
564 die_message('ChaSen execution failed: ' . $chasen);
566 foreach ($readings as $page => $reading) {
567 if($reading != '') continue;
570 $line = mb_convert_encoding($line, SOURCE_ENCODING,
571 $pagereading_kanji2kana_encoding);
573 $readings[$page] = $line;
578 die_message('Temporary file can not be removed: ' . $tmpfname);
581 case 'kakasi': /*FALLTHROUGH*/
583 if(! file_exists($pagereading_kakasi_path))
584 die_message('KAKASI not found: ' . $pagereading_kakasi_path);
586 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
587 $fp = fopen($tmpfname, 'w') or
588 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
589 foreach ($readings as $page => $reading) {
590 if($reading != '') continue;
591 fputs($fp, mb_convert_encoding($page . "\n",
592 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
596 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
597 $fp = popen($kakasi, 'r');
600 die_message('KAKASI execution failed: ' . $kakasi);
603 foreach ($readings as $page => $reading) {
604 if($reading != '') continue;
607 $line = mb_convert_encoding($line, SOURCE_ENCODING,
608 $pagereading_kanji2kana_encoding);
610 $readings[$page] = $line;
615 die_message('Temporary file can not be removed: ' . $tmpfname);
619 $patterns = $replacements = $matches = array();
620 foreach (get_source($pagereading_config_dict) as $line) {
622 if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
623 $patterns[] = $matches[1];
624 $replacements[] = $matches[2];
627 foreach ($readings as $page => $reading) {
628 if($reading != '') continue;
630 $readings[$page] = $page;
631 foreach ($patterns as $no => $pattern)
632 $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern,
633 $replacements[$no], $readings[$page]), 'aKCV');
638 die_message('Unknown kanji-kana converter: ' . $pagereading_kanji2kana_converter . '.');
643 if($unknownPage || $deletedPage) {
645 asort($readings); // Sort by pronouncing(alphabetical/reading) order
647 foreach ($readings as $page => $reading)
648 $body .= '-[[' . $page . ']] ' . $reading . "\n";
650 page_write($pagereading_config_page, $body);
654 // Pages that are not prounouncing-clear, return pagenames of themselves
655 foreach ($pages as $page) {
656 if($readings[$page] == '')
657 $readings[$page] = $page;
663 // Get a list of encoded files (must specify a directory and a suffix)
664 function get_existfiles($dir, $ext)
666 $pattern = '/^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$/';
668 $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
669 while ($file = readdir($dp))
670 if (preg_match($pattern, $file))
671 $aryret[] = $dir . $file;
676 // Get a list of related pages of the page
677 function links_get_related($page)
679 global $vars, $related;
680 static $links = array();
682 if (isset($links[$page])) return $links[$page];
684 // If possible, merge related pages generated by make_link()
685 $links[$page] = ($page == $vars['page']) ? $related : array();
687 // Get repated pages from DB
688 $links[$page] += links_get_related_db($vars['page']);
690 return $links[$page];
693 // _If needed_, re-create the file to change/correct ownership into PHP's
694 // NOTE: Not works for Windows
695 function pkwk_chown($filename, $preserve_time = TRUE)
697 static $php_uid; // PHP's UID
699 if (! isset($php_uid)) {
700 if (extension_loaded('posix')) {
701 $php_uid = posix_getuid(); // Unix
703 $php_uid = 0; // Windows
707 // Lock for pkwk_chown()
708 $lockfile = CACHE_DIR . 'pkwk_chown.lock';
709 $flock = fopen($lockfile, 'a') or
710 die('pkwk_chown(): fopen() failed for: CACHEDIR/' .
711 basename(htmlspecialchars($lockfile)));
712 flock($flock, LOCK_EX) or die('pkwk_chown(): flock() failed for lock');
715 $stat = stat($filename) or
716 die('pkwk_chown(): stat() failed for: ' . basename(htmlspecialchars($filename)));
717 if ($stat[4] === $php_uid) {
718 // NOTE: Windows always here
719 $result = TRUE; // Seems the same UID. Nothing to do
721 $tmp = $filename . '.' . getmypid() . '.tmp';
723 // Lock source $filename to avoid file corruption
724 // NOTE: Not 'r+'. Don't check write permission here
725 $ffile = fopen($filename, 'r') or
726 die('pkwk_chown(): fopen() failed for: ' .
727 basename(htmlspecialchars($filename)));
729 // Try to chown by re-creating files
731 // * touch() before copy() is for 'rw-r--r--' instead of 'rwxr-xr-x' (with umask 022).
732 // * (PHP 4 < PHP 4.2.0) touch() with the third argument is not implemented and retuns NULL and Warn.
733 // * @unlink() before rename() is for Windows but here's for Unix only
734 flock($ffile, LOCK_EX) or die('pkwk_chown(): flock() failed');
735 $result = touch($tmp) && copy($filename, $tmp) &&
736 ($preserve_time ? (touch($tmp, $stat[9], $stat[8]) || touch($tmp, $stat[9])) : TRUE) &&
737 rename($tmp, $filename);
738 flock($ffile, LOCK_UN) or die('pkwk_chown(): flock() failed');
740 fclose($ffile) or die('pkwk_chown(): fclose() failed');
742 if ($result === FALSE) @unlink($tmp);
745 // Unlock for pkwk_chown()
746 flock($flock, LOCK_UN) or die('pkwk_chown(): flock() failed for lock');
747 fclose($flock) or die('pkwk_chown(): fclose() failed for lock');
752 // touch() with trying pkwk_chown()
753 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
755 // Is the owner incorrected and unable to correct?
756 if (! file_exists($filename) || pkwk_chown($filename)) {
757 if ($time === FALSE) {
758 $result = touch($filename);
759 } else if ($atime === FALSE) {
760 $result = touch($filename, $time);
762 $result = touch($filename, $time, $atime);
766 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
767 htmlspecialchars(basename($filename)));