2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: file.php,v 1.20 2005/04/17 06:16:17 henoheno Exp $
5 // File related functions
7 // Get source(wiki text) data of the page
8 function get_source($page = NULL)
10 // Removing line-feeds: Because file() doesn't remove them.
11 return is_page($page) ? str_replace("\r", '', file(get_filename($page))) : array();
14 // Get last-modified filetime of the page
15 function get_filetime($page)
17 return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
20 // Get physical file name of the page
21 function get_filename($page)
23 return DATA_DIR . encode($page) . '.txt';
26 // Put a data(wiki text) into a physical file(diff, backup, text)
27 function page_write($page, $postdata, $notimestamp = FALSE)
31 if (PKWK_READONLY) return; // Do nothing
33 $postdata = make_str_rules($postdata);
35 // Create and write diff
36 $oldpostdata = is_page($page) ? join('', get_source($page)) : '';
37 $diffdata = do_diff($oldpostdata, $postdata);
38 file_write(DIFF_DIR, $page, $diffdata);
41 make_backup($page, $postdata == ''); // Is $postdata null?
44 file_write(DATA_DIR, $page, $postdata, $notimestamp);
48 $_diff = explode("\n", $diffdata);
49 $plus = join("\n", preg_replace('/^\+/', '', preg_grep('/^\+/', $_diff)));
50 $minus = join("\n", preg_replace('/^-/', '', preg_grep('/^-/', $_diff)));
51 tb_send($page, $plus, $minus);
57 // User-defined rules (replace the source)
58 function make_str_rules($str)
60 global $str_rules, $fixed_heading_anchor;
62 $arr = explode("\n", $str);
64 $retvars = $matches = array();
65 foreach ($arr as $str) {
66 if ($str != '' && $str{0} != ' ' && $str{0} != "\t")
67 foreach ($str_rules as $rule => $replace)
68 $str = preg_replace('/' . $rule . '/', $replace, $str);
70 // Adding fixed anchor into headings
71 if ($fixed_heading_anchor &&
72 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/', $str, $matches))
75 // A random alphabetic letter + 7 letters of random strings from md()
76 $anchor = chr(mt_rand(ord('a'), ord('z'))) .
77 substr(md5(uniqid(substr($matches[1], 0, 100), 1)), mt_rand(0, 24), 7);
78 $str = rtrim($matches[1]) . ' [#' . $anchor . ']';
83 return join("\n", $retvars);
87 function file_write($dir, $page, $str, $notimestamp = FALSE)
89 global $update_exec, $_msg_invalidiwn;
90 global $notify, $notify_diff_only, $notify_to, $notify_subject, $notify_header;
91 global $smtp_server, $smtp_auth;
92 global $whatsdeleted, $maxshow_deleted;
94 if (PKWK_READONLY) return; // Do nothing
96 if (! is_pagename($page))
97 die_message(str_replace('$1', htmlspecialchars($page),
98 str_replace('$2', 'WikiName', $_msg_invalidiwn)));
100 $page = strip_bracket($page);
102 $file = $dir . encode($page) . '.txt';
104 if ($dir == DATA_DIR && $str == '' && file_exists($file)) {
106 add_recent($page, $whatsdeleted, '', $maxshow_deleted); // RecentDeleted
110 $str = preg_replace('/' . "\r" . '/', '', $str);
111 $str = rtrim($str) . "\n";
113 if ($notimestamp && file_exists($file))
114 $timestamp = filemtime($file) - LOCALZONE;
116 $fp = fopen($file, 'w') or
117 die_message('Cannot write page file or diff file or other ' .
118 htmlspecialchars($page) .
119 '<br />Maybe permission is not writable or filename is too long');
121 set_file_buffer($fp, 0);
128 if ($timestamp) pkwk_touch_file($file, $timestamp + LOCALZONE);
131 // Clear is_page() cache
132 is_page($page, TRUE);
134 if (! $timestamp && $dir == DATA_DIR)
137 // Execute $update_exec here
138 if ($update_exec && $dir == DATA_DIR)
139 system($update_exec . ' > /dev/null &');
141 if ($notify && $dir == DIFF_DIR) {
142 if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
144 str_repeat('-', 30) . "\n" .
145 'URI: ' . get_script_uri() . '?' . rawurlencode($page) . "\n" .
146 'REMOTE_ADDR: ' . $_SERVER['REMOTE_ADDR'] . "\n";
148 $subject = str_replace('$page', $page, $notify_subject);
149 ini_set('SMTP', $smtp_server);
152 if ($smtp_auth) pop_before_smtp();
153 mb_send_mail($notify_to, $subject, $str, $notify_header);
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 // * @unlink() before rename() is for Windows but here's for Unix only
539 // * (PHP 4 < PHP 4.2.0) touch() with the third argument is not implemented and retuns NULL and Warn.
540 flock($ffile, LOCK_EX) or die('pkwk_chown(): flock() failed');
541 $result = copy($filename, $tmp) &&
542 ($preserve_time ? (touch($tmp, $stat[9], $stat[8]) || touch($tmp, $stat[9])) : TRUE) &&
543 rename($tmp, $filename);
544 flock($ffile, LOCK_UN) or die('pkwk_chown(): flock() failed');
546 fclose($ffile) or die('pkwk_chown(): fclose() failed');
548 if ($result === FALSE) @unlink($tmp);
551 // Unlock for pkwk_chown()
552 flock($flock, LOCK_UN) or die('pkwk_chown(): flock() failed for lock');
553 fclose($flock) or die('pkwk_chown(): fclose() failed for lock');
558 // touch() with trying pkwk_chown()
559 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
561 // Is the owner incorrected and unable to correct?
562 if (! file_exists($filename) || pkwk_chown($filename)) {
563 if ($time === FALSE) {
564 $result = touch($filename);
565 } else if ($atime === FALSE) {
566 $result = touch($filename, $time);
568 $result = touch($filename, $time, $atime);
572 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
573 htmlspecialchars(basename($filename)));