2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: file.php,v 1.52 2006/04/09 14:49:38 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
11 // Get source(wiki text) data of the page
12 function get_source($page = NULL, $lock = TRUE)
17 $path = get_filename($page);
20 $fp = @fopen($path, 'r');
21 if ($fp == FALSE) return $array;
25 // Removing line-feeds: Because file() doesn't remove them.
26 $array = str_replace("\r", '', file($path));
37 // Get last-modified filetime of the page
38 function get_filetime($page)
40 return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
43 // Get physical file name of the page
44 function get_filename($page)
46 return DATA_DIR . encode($page) . '.txt';
49 // Put a data(wiki text) into a physical file(diff, backup, text)
50 function page_write($page, $postdata, $notimestamp = FALSE)
54 if (PKWK_READONLY) return; // Do nothing
56 $postdata = make_str_rules($postdata);
58 // Create and write diff
59 $oldpostdata = is_page($page) ? join('', get_source($page)) : '';
60 $diffdata = do_diff($oldpostdata, $postdata);
61 file_write(DIFF_DIR, $page, $diffdata);
64 make_backup($page, $postdata == ''); // Is $postdata null?
67 file_write(DATA_DIR, $page, $postdata, $notimestamp);
71 $_diff = explode("\n", $diffdata);
72 $plus = join("\n", preg_replace('/^\+/', '', preg_grep('/^\+/', $_diff)));
73 $minus = join("\n", preg_replace('/^-/', '', preg_grep('/^-/', $_diff)));
74 tb_send($page, $plus, $minus);
80 // Modify original text with user-defined / system-defined rules
81 function make_str_rules($source)
83 global $str_rules, $fixed_heading_anchor;
85 $lines = explode("\n", $source);
86 $count = count($lines);
91 for ($i = 0; $i < $count; $i++) {
92 $line = & $lines[$i]; // Modify directly
94 // Ignore null string and preformatted texts
95 if ($line == '' || $line{0} == ' ' || $line{0} == "\t") continue;
99 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
101 preg_match('/#[^{]*(\{\{+)\s*$/', $line, $matches)) {
102 // Multiline convert plugin start
104 $multiline = strlen($matches[1]); // Set specific number
107 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
109 preg_match('/^\}{' . $multiline . '}\s*$/', $line)) {
110 // Multiline convert plugin end
115 if ($modify === FALSE) continue;
117 // Replace with $str_rules
118 foreach ($str_rules as $pattern => $replacement)
119 $line = preg_replace('/' . $pattern . '/', $replacement, $line);
121 // Adding fixed anchor into headings
122 if ($fixed_heading_anchor &&
123 preg_match('/^(\*{1,3}.*?)(?:\[#([A-Za-z][\w-]*)\]\s*)?$/', $line, $matches) &&
124 (! isset($matches[2]) || $matches[2] == '')) {
125 // Generate unique id
126 $anchor = generate_fixed_heading_anchor_id($matches[1]);
127 $line = rtrim($matches[1]) . ' [#' . $anchor . ']';
131 // Multiline part has no stopper
132 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
133 $modify === FALSE && $multiline != 0)
134 $lines[] = str_repeat('}', $multiline);
136 return implode("\n", $lines);
140 function generate_fixed_heading_anchor_id($seed)
142 // A random alphabetic letter + 7 letters of random strings from md()
143 return chr(mt_rand(ord('a'), ord('z'))) .
144 substr(md5(uniqid(substr($seed, 0, 100), TRUE)),
148 // Read top N lines as an array
149 // (Use PHP file() function if you want to get ALL lines)
150 function file_head($file, $count = 1)
156 $fp = @fopen($file, 'r');
157 if ($fp == FALSE) return FALSE;
158 set_file_buffer($fp, 0);
161 while (! feof($fp)) {
162 $line = fgets($fp, $buffer);
163 if ($line != FALSE) $array[] = $line;
164 if (++$index >= $count) break;
168 if(! fclose($fp)) return FALSE;
174 function file_write($dir, $page, $str, $notimestamp = FALSE)
176 global $update_exec, $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
177 global $whatsdeleted, $maxshow_deleted;
179 if (PKWK_READONLY) return; // Do nothing
181 if (! is_pagename($page))
182 die_message(str_replace('$1', htmlspecialchars($page),
183 str_replace('$2', 'WikiName', $_msg_invalidiwn)));
185 $page = strip_bracket($page);
186 $file = $dir . encode($page) . '.txt';
190 if ($dir == DATA_DIR) {
191 if (! file_exists($file)) return; // Ignore null posting
196 // Update RecentDeleted (Add the $page)
197 add_recent($page, $whatsdeleted, '', $maxshow_deleted);
199 // Update RecentChanges (Remove the $page from RecentChanges)
200 if (! $timestamp && $dir == DATA_DIR) put_lastmodified();
203 // File replacement (Edit)
204 $str = rtrim(preg_replace('/' . "\r" . '/', '', $str)) . "\n";
206 if ($notimestamp && file_exists($file))
207 $timestamp = filemtime($file) - LOCALZONE;
209 $fp = fopen($file, 'a') or die('fopen() failed: ' .
210 htmlspecialchars(basename($dir) . '/' . encode($page) . '.txt') .
212 'Maybe permission is not writable or filename is too long');
213 set_file_buffer($fp, 0);
226 if ($timestamp) pkwk_touch_file($file, $timestamp + LOCALZONE);
228 // Update RecentChanges (Add or renew the $page)
229 if (! $timestamp && $dir == DATA_DIR) put_lastmodified();
232 // Clear is_page() cache
233 is_page($page, TRUE);
235 // Execute $update_exec here
236 if ($update_exec && $dir == DATA_DIR)
237 system($update_exec . ' > /dev/null &');
239 if ($notify && $dir == DIFF_DIR) {
240 if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
242 $footer['ACTION'] = 'Page update';
243 $footer['PAGE'] = & $page;
244 $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
245 $footer['USER_AGENT'] = TRUE;
246 $footer['REMOTE_ADDR'] = TRUE;
248 pkwk_mail_notify($notify_subject, $str, $footer) or
249 die('pkwk_mail_notify(): Failed');
253 // Update RecentDeleted
254 function add_recent($page, $recentpage, $subject = '', $limit = 0)
256 if (PKWK_READONLY || $limit == 0 || $page == '' || $recentpage == '' ||
257 check_non_list($page)) return;
260 $lines = $matches = array();
261 foreach (get_source($recentpage) as $line)
262 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
263 $lines[$matches[2]] = $line;
265 $_page = '[[' . $page . ']]';
267 // Remove a report about the same page
268 if (isset($lines[$_page])) unset($lines[$_page]);
271 array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
272 htmlspecialchars($subject) . "\n");
274 // Get latest $limit reports
275 $lines = array_splice($lines, 0, $limit);
278 $fp = fopen(get_filename($recentpage), 'w') or
279 die_message('Cannot write page file ' .
280 htmlspecialchars($recentpage) .
281 '<br />Maybe permission is not writable or filename is too long');
282 set_file_buffer($fp, 0);
285 fputs($fp, '#freeze' . "\n");
286 fputs($fp, '#norelated' . "\n"); // :)
287 fputs($fp, join('', $lines));
292 // Update RecentChanges
293 function put_lastmodified()
295 global $maxshow, $whatsnew, $autolink;
297 if (PKWK_READONLY) return; // Do nothing
299 // Get whole page list
300 $pages = get_existpages();
302 // Check ALL filetime
303 $recent_pages = array();
304 foreach($pages as $page)
305 if ($page != $whatsnew && ! check_non_list($page))
306 $recent_pages[$page] = get_filetime($page);
308 // Sort decending order of last-modification date
309 arsort($recent_pages, SORT_NUMERIC);
312 $recent_pages = array_splice($recent_pages, 0, $maxshow);
314 // Create recent.dat (for recent.inc.php)
315 $file = 'recent.dat';
316 $fp = fopen(CACHE_DIR . $file, 'w') or
317 die_message('Cannot write file ' .
318 'CACHE_DIR/' . $file . '<br />' . "\n" .
319 'Maybe permission is not writable');
321 set_file_buffer($fp, 0);
324 foreach ($recent_pages as $page=>$time)
325 fputs($fp, $time . "\t" . $page . "\n");
329 // Create RecentChanges
330 $fp = fopen(get_filename($whatsnew), 'w') or
331 die_message('Cannot write file ' .
332 htmlspecialchars($whatsnew) . '<br />' . "\n" .
333 'Maybe permission is not writable or filename is too long');
335 set_file_buffer($fp, 0);
339 foreach (array_keys($recent_pages) as $page) {
340 $time = $recent_pages[$page];
341 $s_lastmod = htmlspecialchars(format_date($time));
342 $s_page = htmlspecialchars($page);
343 fputs($fp, '-' . $s_lastmod . ' - [[' . $s_page . ']]' . "\n");
345 fputs($fp, '#norelated' . "\n"); // :)
351 list($pattern, $pattern_a, $forceignorelist) =
352 get_autolink_pattern($pages);
354 $file = 'autolink.dat';
355 $fp = fopen(CACHE_DIR . $file, 'w') or
356 die_message('Cannot write file ' .
357 'CACHE_DIR/' . $file . '<br />' . "\n" .
358 'Maybe permission is not writable');
360 set_file_buffer($fp, 0);
363 fputs($fp, $pattern . "\n");
364 fputs($fp, $pattern_a . "\n");
365 fputs($fp, join("\t", $forceignorelist) . "\n");
371 // Get elapsed date of the page
372 function get_pg_passage($page, $sw = TRUE)
374 global $show_passage;
375 if (! $show_passage) return '';
377 $time = get_filetime($page);
378 $pg_passage = ($time != 0) ? get_passage($time) : '';
380 return $sw ? '<small>' . $pg_passage . '</small>' : ' ' . $pg_passage;
383 // Last-Modified header
384 function header_lastmod($page = NULL)
388 if ($lastmod && is_page($page)) {
390 header('Last-Modified: ' .
391 date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
395 // Get a page list of this wiki
396 function get_existpages($dir = DATA_DIR, $ext = '.txt')
400 $pattern = '((?:[0-9A-F]{2})+)';
401 if ($ext != '') $ext = preg_quote($ext, '/');
402 $pattern = '/^' . $pattern . $ext . '$/';
404 $dp = @opendir($dir) or
405 die_message($dir . ' is not found or not readable.');
407 while ($file = readdir($dp))
408 if (preg_match($pattern, $file, $matches))
409 $aryret[$file] = decode($matches[1]);
415 // Get PageReading(pronounce-annotated) data in an array()
416 function get_readings()
418 global $pagereading_enable, $pagereading_kanji2kana_converter;
419 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
420 global $pagereading_kakasi_path, $pagereading_config_page;
421 global $pagereading_config_dict;
423 $pages = get_existpages();
426 foreach ($pages as $page)
427 $readings[$page] = '';
429 $deletedPage = FALSE;
431 foreach (get_source($pagereading_config_page) as $line) {
433 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
434 if(isset($readings[$matches[1]])) {
435 // This page is not clear how to be pronounced
436 $readings[$matches[1]] = $matches[2];
438 // This page seems deleted
444 // If enabled ChaSen/KAKASI execution
445 if($pagereading_enable) {
447 // Check there's non-clear-pronouncing page
448 $unknownPage = FALSE;
449 foreach ($readings as $page => $reading) {
456 // Execute ChaSen/KAKASI, and get annotation
458 switch(strtolower($pagereading_kanji2kana_converter)) {
460 if(! file_exists($pagereading_chasen_path))
461 die_message('ChaSen not found: ' . $pagereading_chasen_path);
463 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
464 $fp = fopen($tmpfname, 'w') or
465 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
466 foreach ($readings as $page => $reading) {
467 if($reading != '') continue;
468 fputs($fp, mb_convert_encoding($page . "\n",
469 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
473 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
474 $fp = popen($chasen, 'r');
477 die_message('ChaSen execution failed: ' . $chasen);
479 foreach ($readings as $page => $reading) {
480 if($reading != '') continue;
483 $line = mb_convert_encoding($line, SOURCE_ENCODING,
484 $pagereading_kanji2kana_encoding);
486 $readings[$page] = $line;
491 die_message('Temporary file can not be removed: ' . $tmpfname);
494 case 'kakasi': /*FALLTHROUGH*/
496 if(! file_exists($pagereading_kakasi_path))
497 die_message('KAKASI not found: ' . $pagereading_kakasi_path);
499 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
500 $fp = fopen($tmpfname, 'w') or
501 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
502 foreach ($readings as $page => $reading) {
503 if($reading != '') continue;
504 fputs($fp, mb_convert_encoding($page . "\n",
505 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
509 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
510 $fp = popen($kakasi, 'r');
513 die_message('KAKASI execution failed: ' . $kakasi);
516 foreach ($readings as $page => $reading) {
517 if($reading != '') continue;
520 $line = mb_convert_encoding($line, SOURCE_ENCODING,
521 $pagereading_kanji2kana_encoding);
523 $readings[$page] = $line;
528 die_message('Temporary file can not be removed: ' . $tmpfname);
532 $patterns = $replacements = $matches = array();
533 foreach (get_source($pagereading_config_dict) as $line) {
535 if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
536 $patterns[] = $matches[1];
537 $replacements[] = $matches[2];
540 foreach ($readings as $page => $reading) {
541 if($reading != '') continue;
543 $readings[$page] = $page;
544 foreach ($patterns as $no => $pattern)
545 $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern,
546 $replacements[$no], $readings[$page]), 'aKCV');
551 die_message('Unknown kanji-kana converter: ' . $pagereading_kanji2kana_converter . '.');
556 if($unknownPage || $deletedPage) {
558 asort($readings); // Sort by pronouncing(alphabetical/reading) order
560 foreach ($readings as $page => $reading)
561 $body .= '-[[' . $page . ']] ' . $reading . "\n";
563 page_write($pagereading_config_page, $body);
567 // Pages that are not prounouncing-clear, return pagenames of themselves
568 foreach ($pages as $page) {
569 if($readings[$page] == '')
570 $readings[$page] = $page;
576 // Get a list of encoded files (must specify a directory and a suffix)
577 function get_existfiles($dir, $ext)
579 $pattern = '/^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$/';
581 $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
582 while ($file = readdir($dp))
583 if (preg_match($pattern, $file))
584 $aryret[] = $dir . $file;
589 // Get a list of related pages of the page
590 function links_get_related($page)
592 global $vars, $related;
593 static $links = array();
595 if (isset($links[$page])) return $links[$page];
597 // If possible, merge related pages generated by make_link()
598 $links[$page] = ($page == $vars['page']) ? $related : array();
600 // Get repated pages from DB
601 $links[$page] += links_get_related_db($vars['page']);
603 return $links[$page];
606 // _If needed_, re-create the file to change/correct ownership into PHP's
607 // NOTE: Not works for Windows
608 function pkwk_chown($filename, $preserve_time = TRUE)
610 static $php_uid; // PHP's UID
612 if (! isset($php_uid)) {
613 if (extension_loaded('posix')) {
614 $php_uid = posix_getuid(); // Unix
616 $php_uid = 0; // Windows
620 // Lock for pkwk_chown()
621 $lockfile = CACHE_DIR . 'pkwk_chown.lock';
622 $flock = fopen($lockfile, 'a') or
623 die('pkwk_chown(): fopen() failed for: CACHEDIR/' .
624 basename(htmlspecialchars($lockfile)));
625 flock($flock, LOCK_EX) or die('pkwk_chown(): flock() failed for lock');
628 $stat = stat($filename) or
629 die('pkwk_chown(): stat() failed for: ' . basename(htmlspecialchars($filename)));
630 if ($stat[4] === $php_uid) {
631 // NOTE: Windows always here
632 $result = TRUE; // Seems the same UID. Nothing to do
634 $tmp = $filename . '.' . getmypid() . '.tmp';
636 // Lock source $filename to avoid file corruption
637 // NOTE: Not 'r+'. Don't check write permission here
638 $ffile = fopen($filename, 'r') or
639 die('pkwk_chown(): fopen() failed for: ' .
640 basename(htmlspecialchars($filename)));
642 // Try to chown by re-creating files
644 // * touch() before copy() is for 'rw-r--r--' instead of 'rwxr-xr-x' (with umask 022).
645 // * (PHP 4 < PHP 4.2.0) touch() with the third argument is not implemented and retuns NULL and Warn.
646 // * @unlink() before rename() is for Windows but here's for Unix only
647 flock($ffile, LOCK_EX) or die('pkwk_chown(): flock() failed');
648 $result = touch($tmp) && copy($filename, $tmp) &&
649 ($preserve_time ? (touch($tmp, $stat[9], $stat[8]) || touch($tmp, $stat[9])) : TRUE) &&
650 rename($tmp, $filename);
651 flock($ffile, LOCK_UN) or die('pkwk_chown(): flock() failed');
653 fclose($ffile) or die('pkwk_chown(): fclose() failed');
655 if ($result === FALSE) @unlink($tmp);
658 // Unlock for pkwk_chown()
659 flock($flock, LOCK_UN) or die('pkwk_chown(): flock() failed for lock');
660 fclose($flock) or die('pkwk_chown(): fclose() failed for lock');
665 // touch() with trying pkwk_chown()
666 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
668 // Is the owner incorrected and unable to correct?
669 if (! file_exists($filename) || pkwk_chown($filename)) {
670 if ($time === FALSE) {
671 $result = touch($filename);
672 } else if ($atime === FALSE) {
673 $result = touch($filename, $time);
675 $result = touch($filename, $time, $atime);
679 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
680 htmlspecialchars(basename($filename)));