2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: file.php,v 1.26 2005/06/07 14:37:46 henoheno Exp $
5 // 2002-2005 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)
14 // Removing line-feeds: Because file() doesn't remove them.
15 return is_page($page) ? str_replace("\r", '', file(get_filename($page))) : array();
18 // Get last-modified filetime of the page
19 function get_filetime($page)
21 return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
24 // Get physical file name of the page
25 function get_filename($page)
27 return DATA_DIR . encode($page) . '.txt';
30 // Put a data(wiki text) into a physical file(diff, backup, text)
31 function page_write($page, $postdata, $notimestamp = FALSE)
35 if (PKWK_READONLY) return; // Do nothing
37 $postdata = make_str_rules($postdata);
39 // Create and write diff
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 == ''); // Is $postdata null?
48 file_write(DATA_DIR, $page, $postdata, $notimestamp);
52 $_diff = explode("\n", $diffdata);
53 $plus = join("\n", preg_replace('/^\+/', '', preg_grep('/^\+/', $_diff)));
54 $minus = join("\n", preg_replace('/^-/', '', preg_grep('/^-/', $_diff)));
55 tb_send($page, $plus, $minus);
61 // User-defined rules (replace the source)
62 function make_str_rules($str)
64 global $str_rules, $fixed_heading_anchor;
66 $arr = explode("\n", $str);
68 $retvars = $matches = array();
69 foreach ($arr as $str) {
70 if ($str != '' && $str{0} != ' ' && $str{0} != "\t")
71 foreach ($str_rules as $rule => $replace)
72 $str = preg_replace('/' . $rule . '/', $replace, $str);
74 // Adding fixed anchor into headings
75 if ($fixed_heading_anchor &&
76 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/', $str, $matches))
79 // A random alphabetic letter + 7 letters of random strings from md()
80 $anchor = chr(mt_rand(ord('a'), ord('z'))) .
81 substr(md5(uniqid(substr($matches[1], 0, 100), 1)), mt_rand(0, 24), 7);
82 $str = rtrim($matches[1]) . ' [#' . $anchor . ']';
87 return join("\n", $retvars);
91 function file_write($dir, $page, $str, $notimestamp = FALSE)
93 global $update_exec, $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
94 global $whatsdeleted, $maxshow_deleted;
96 if (PKWK_READONLY) return; // Do nothing
98 if (! is_pagename($page))
99 die_message(str_replace('$1', htmlspecialchars($page),
100 str_replace('$2', 'WikiName', $_msg_invalidiwn)));
102 $page = strip_bracket($page);
104 $file = $dir . encode($page) . '.txt';
106 if ($dir == DATA_DIR && $str == '' && file_exists($file)) {
108 add_recent($page, $whatsdeleted, '', $maxshow_deleted); // RecentDeleted
112 $str = preg_replace('/' . "\r" . '/', '', $str);
113 $str = rtrim($str) . "\n";
115 if ($notimestamp && file_exists($file))
116 $timestamp = filemtime($file) - LOCALZONE;
118 $fp = fopen($file, 'w') or die('fopen() failed: ' .
119 htmlspecialchars(basename($dir) . '/' . encode($page) . '.txt') .
121 'Maybe permission is not writable or filename is too long');
123 set_file_buffer($fp, 0);
130 if ($timestamp) pkwk_touch_file($file, $timestamp + LOCALZONE);
133 // Clear is_page() cache
134 is_page($page, TRUE);
136 if (! $timestamp && $dir == DATA_DIR)
139 // Execute $update_exec here
140 if ($update_exec && $dir == DATA_DIR)
141 system($update_exec . ' > /dev/null &');
143 if ($notify && $dir == DIFF_DIR) {
144 if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
146 $footer['ACTION'] = 'Page update';
147 $footer['PAGE'] = $page;
148 $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
149 $footer['REMOTE_ADDR'] = TRUE;
150 $footer['USER_AGENT'] = TRUE;
152 pkwk_mail_notify($notify_subject, $str, $footer) or
153 die('pkwk_mail_notify(): Failed');
157 // Update RecentDeleted
158 function add_recent($page, $recentpage, $subject = '', $limit = 0)
160 if (PKWK_READONLY || $limit == 0 || $page == '' || $recentpage == '') return;
163 $lines = $matches = array();
164 foreach (get_source($recentpage) as $line)
165 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
166 $lines[$matches[2]] = $line;
168 $_page = '[[' . $page . ']]';
170 // Remove a report about the same page
171 if (isset($lines[$_page])) unset($lines[$_page]);
174 array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
175 htmlspecialchars($subject) . "\n");
177 // Get latest $limit reports
178 $lines = array_splice($lines, 0, $limit);
181 $fp = fopen(get_filename($recentpage), 'w') or
182 die_message('Cannot write page file ' .
183 htmlspecialchars($recentpage) .
184 '<br />Maybe permission is not writable or filename is too long');
185 set_file_buffer($fp, 0);
188 fputs($fp, '#freeze' . "\n");
189 fputs($fp, '#norelated' . "\n"); // :)
190 fputs($fp, join('', $lines));
195 // Update RecentChanges
196 function put_lastmodified()
198 global $maxshow, $whatsnew, $non_list, $autolink;
200 if (PKWK_READONLY) return; // Do nothing
202 $pages = get_existpages();
203 $recent_pages = array();
204 $non_list_pattern = '/' . $non_list . '/';
205 foreach($pages as $page)
206 if ($page != $whatsnew && ! preg_match($non_list_pattern, $page))
207 $recent_pages[$page] = get_filetime($page);
209 // Sort decending order of last-modification date
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");
226 // Create RecentChanges
227 $fp = fopen(get_filename($whatsnew), 'w') or
228 die_message('Cannot write page file ' .
229 htmlspecialchars($whatsnew) .
230 '<br />Maybe permission is not writable or filename is too long');
232 set_file_buffer($fp, 0);
235 foreach (array_splice(array_keys($recent_pages), 0, $maxshow) as $page) {
236 $time = $recent_pages[$page];
237 $s_lastmod = htmlspecialchars(format_date($time));
238 $s_page = htmlspecialchars($page);
239 fputs($fp, '-' . $s_lastmod . ' - [[' . $s_page . ']]' . "\n");
241 fputs($fp, '#norelated' . "\n"); // :)
247 list($pattern, $pattern_a, $forceignorelist) =
248 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 // Get elapsed date of the pate
266 function get_pg_passage($page, $sw = TRUE)
268 global $show_passage;
269 if (! $show_passage) return '';
271 $time = get_filetime($page);
272 $pg_passage = ($time != 0) ? get_passage($time) : '';
274 return $sw ? '<small>' . $pg_passage . '</small>' : ' ' . $pg_passage;
277 // Last-Modified header
278 function header_lastmod($page = NULL)
282 if ($lastmod && is_page($page)) {
284 header('Last-Modified: ' .
285 date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
289 // Get a page list of this wiki
290 function get_existpages($dir = DATA_DIR, $ext = '.txt')
294 $pattern = '((?:[0-9A-F]{2})+)';
295 if ($ext != '') $ext = preg_quote($ext, '/');
296 $pattern = '/^' . $pattern . $ext . '$/';
298 $dp = @opendir($dir) or
299 die_message($dir . ' is not found or not readable.');
301 while ($file = readdir($dp))
302 if (preg_match($pattern, $file, $matches))
303 $aryret[$file] = decode($matches[1]);
309 // Get PageReading(pronounce-annotated) data in an array()
310 function get_readings()
312 global $pagereading_enable, $pagereading_kanji2kana_converter;
313 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
314 global $pagereading_kakasi_path, $pagereading_config_page;
315 global $pagereading_config_dict;
317 $pages = get_existpages();
320 foreach ($pages as $page)
321 $readings[$page] = '';
323 $deletedPage = FALSE;
325 foreach (get_source($pagereading_config_page) as $line) {
327 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
328 if(isset($readings[$matches[1]])) {
329 // This page is not clear how to be pronounced
330 $readings[$matches[1]] = $matches[2];
332 // This page seems deleted
338 // If enabled ChaSen/KAKASI execution
339 if($pagereading_enable) {
341 // Check there's non-clear-pronouncing page
342 $unknownPage = FALSE;
343 foreach ($readings as $page => $reading) {
350 // Execute ChaSen/KAKASI, and get annotation
352 switch(strtolower($pagereading_kanji2kana_converter)) {
354 if(! file_exists($pagereading_chasen_path))
355 die_message('ChaSen not found: ' . $pagereading_chasen_path);
357 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
358 $fp = fopen($tmpfname, 'w') or
359 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
360 foreach ($readings as $page => $reading) {
361 if($reading != '') continue;
362 fputs($fp, mb_convert_encoding($page . "\n",
363 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
367 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
368 $fp = popen($chasen, 'r');
371 die_message('ChaSen execution failed: ' . $chasen);
373 foreach ($readings as $page => $reading) {
374 if($reading != '') continue;
377 $line = mb_convert_encoding($line, SOURCE_ENCODING,
378 $pagereading_kanji2kana_encoding);
380 $readings[$page] = $line;
385 die_message('Temporary file can not be removed: ' . $tmpfname);
388 case 'kakasi': /*FALLTHROUGH*/
390 if(! file_exists($pagereading_kakasi_path))
391 die_message('KAKASI not found: ' . $pagereading_kakasi_path);
393 $tmpfname = tempnam(CACHE_DIR, 'PageReading');
394 $fp = fopen($tmpfname, 'w') or
395 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
396 foreach ($readings as $page => $reading) {
397 if($reading != '') continue;
398 fputs($fp, mb_convert_encoding($page . "\n",
399 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
403 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
404 $fp = popen($kakasi, 'r');
407 die_message('KAKASI execution failed: ' . $kakasi);
410 foreach ($readings as $page => $reading) {
411 if($reading != '') continue;
414 $line = mb_convert_encoding($line, SOURCE_ENCODING,
415 $pagereading_kanji2kana_encoding);
417 $readings[$page] = $line;
422 die_message('Temporary file can not be removed: ' . $tmpfname);
426 $patterns = $replacements = $matches = array();
427 foreach (get_source($pagereading_config_dict) as $line) {
429 if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
430 $patterns[] = $matches[1];
431 $replacements[] = $matches[2];
434 foreach ($readings as $page => $reading) {
435 if($reading != '') continue;
437 $readings[$page] = $page;
438 foreach ($patterns as $no => $pattern)
439 $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern,
440 $replacements[$no], $readings[$page]), 'aKCV');
445 die_message('Unknown kanji-kana converter: ' . $pagereading_kanji2kana_converter . '.');
450 if($unknownPage || $deletedPage) {
452 asort($readings); // Sort by pronouncing(alphabetical/reading) order
454 foreach ($readings as $page => $reading)
455 $body .= '-[[' . $page . ']] ' . $reading . "\n";
457 page_write($pagereading_config_page, $body);
461 // Pages that are not prounouncing-clear, return pagenames of themselves
462 foreach ($pages as $page) {
463 if($readings[$page] == '')
464 $readings[$page] = $page;
470 // Get a list of encoded files (must specify a directory and a suffix)
471 function get_existfiles($dir, $ext)
473 $pattern = '/^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$/';
475 $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
476 while ($file = readdir($dp))
477 if (preg_match($pattern, $file))
478 $aryret[] = $dir . $file;
483 // Get a list of related pages of the page
484 function links_get_related($page)
486 global $vars, $related;
487 static $links = array();
489 if (isset($links[$page])) return $links[$page];
491 // If possible, merge related pages generated by make_link()
492 $links[$page] = ($page == $vars['page']) ? $related : array();
494 // Get repated pages from DB
495 $links[$page] += links_get_related_db($vars['page']);
497 return $links[$page];
500 // _If needed_, re-create the file to change/correct ownership into PHP's
501 // NOTE: Not works for Windows
502 function pkwk_chown($filename, $preserve_time = TRUE)
504 static $php_uid; // PHP's UID
506 if (! isset($php_uid)) {
507 if (extension_loaded('posix')) {
508 $php_uid = posix_getuid(); // Unix
510 $php_uid = 0; // Windows
514 // Lock for pkwk_chown()
515 $lockfile = CACHE_DIR . 'pkwk_chown.lock';
516 $flock = fopen($lockfile, 'a') or
517 die('pkwk_chown(): fopen() failed for: CACHEDIR/' .
518 basename(htmlspecialchars($lockfile)));
519 flock($flock, LOCK_EX) or die('pkwk_chown(): flock() failed for lock');
522 $stat = stat($filename) or
523 die('pkwk_chown(): stat() failed for: ' . basename(htmlspecialchars($filename)));
524 if ($stat[4] === $php_uid) {
525 // NOTE: Windows always here
526 $result = TRUE; // Seems the same UID. Nothing to do
528 $tmp = $filename . '.' . getmypid() . '.tmp';
530 // Lock source $filename to avoid file corruption
531 // NOTE: Not 'r+'. Don't check write permission here
532 $ffile = fopen($filename, 'r') or
533 die('pkwk_chown(): fopen() failed for: ' .
534 basename(htmlspecialchars($filename)));
536 // Try to chown by re-creating files
538 // * touch() before copy() is for 'rw-r--r--' instead of 'rwxr-xr-x' (with umask 022).
539 // * (PHP 4 < PHP 4.2.0) touch() with the third argument is not implemented and retuns NULL and Warn.
540 // * @unlink() before rename() is for Windows but here's for Unix only
541 flock($ffile, LOCK_EX) or die('pkwk_chown(): flock() failed');
542 $result = touch($tmp) && copy($filename, $tmp) &&
543 ($preserve_time ? (touch($tmp, $stat[9], $stat[8]) || touch($tmp, $stat[9])) : TRUE) &&
544 rename($tmp, $filename);
545 flock($ffile, LOCK_UN) or die('pkwk_chown(): flock() failed');
547 fclose($ffile) or die('pkwk_chown(): fclose() failed');
549 if ($result === FALSE) @unlink($tmp);
552 // Unlock for pkwk_chown()
553 flock($flock, LOCK_UN) or die('pkwk_chown(): flock() failed for lock');
554 fclose($flock) or die('pkwk_chown(): fclose() failed for lock');
559 // touch() with trying pkwk_chown()
560 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
562 // Is the owner incorrected and unable to correct?
563 if (! file_exists($filename) || pkwk_chown($filename)) {
564 if ($time === FALSE) {
565 $result = touch($filename);
566 } else if ($atime === FALSE) {
567 $result = touch($filename, $time);
569 $result = touch($filename, $time, $atime);
573 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
574 htmlspecialchars(basename($filename)));