OSDN Git Service

BugTrack2/151: Added lastmodified_add().
[pukiwiki/pukiwiki.git] / lib / file.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: file.php,v 1.59 2006/04/12 14:38:51 henoheno Exp $
4 // Copyright (C)
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
8 //
9 // File related functions
10
11 // RecentChanges
12 define('PKWK_MAXSHOW_CACHE', 'recent.dat');
13 define('PKWK_MAXSHOW_ALLOWANCE', 10);
14
15 // AutoLink
16 define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
17
18 // Get source(wiki text) data of the page
19 function get_source($page = NULL, $lock = TRUE)
20 {
21         $array = array();
22
23         if (is_page($page)) {
24                 $path  = get_filename($page);
25
26                 if ($lock) {
27                         $fp = @fopen($path, 'r');
28                         if ($fp == FALSE) return $array;
29                         flock($fp, LOCK_SH);
30                 }
31
32                 // Removing line-feeds: Because file() doesn't remove them.
33                 $array = str_replace("\r", '', file($path));
34
35                 if ($lock) {
36                         flock($fp, LOCK_UN);
37                         @fclose($fp);
38                 }
39         }
40
41         return $array;
42 }
43
44 // Get last-modified filetime of the page
45 function get_filetime($page)
46 {
47         return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
48 }
49
50 // Get physical file name of the page
51 function get_filename($page)
52 {
53         return DATA_DIR . encode($page) . '.txt';
54 }
55
56 // Put a data(wiki text) into a physical file(diff, backup, text)
57 function page_write($page, $postdata, $notimestamp = FALSE)
58 {
59         global $trackback;
60
61         if (PKWK_READONLY) return; // Do nothing
62
63         $postdata = make_str_rules($postdata);
64
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);
69
70         // Create backup
71         make_backup($page, $postdata == ''); // Is $postdata null?
72
73         // Create wiki text
74         file_write(DATA_DIR, $page, $postdata, $notimestamp);
75
76         if ($trackback) {
77                 // TrackBack Ping
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);
82         }
83
84         links_update($page);
85 }
86
87 // Modify original text with user-defined / system-defined rules
88 function make_str_rules($source)
89 {
90         global $str_rules, $fixed_heading_anchor;
91
92         $lines = explode("\n", $source);
93         $count = count($lines);
94
95         $modify    = TRUE;
96         $multiline = 0;
97         $matches   = array();
98         for ($i = 0; $i < $count; $i++) {
99                 $line = & $lines[$i]; // Modify directly
100
101                 // Ignore null string and preformatted texts
102                 if ($line == '' || $line{0} == ' ' || $line{0} == "\t") continue;
103
104                 // Modify this line?
105                 if ($modify) {
106                         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
107                             $multiline == 0 &&
108                             preg_match('/#[^{]*(\{\{+)\s*$/', $line, $matches)) {
109                                 // Multiline convert plugin start
110                                 $modify    = FALSE;
111                                 $multiline = strlen($matches[1]); // Set specific number
112                         }
113                 } else {
114                         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
115                             $multiline != 0 &&
116                             preg_match('/^\}{' . $multiline . '}\s*$/', $line)) {
117                                 // Multiline convert plugin end
118                                 $modify    = TRUE;
119                                 $multiline = 0;
120                         }
121                 }
122                 if ($modify === FALSE) continue;
123
124                 // Replace with $str_rules
125                 foreach ($str_rules as $pattern => $replacement)
126                         $line = preg_replace('/' . $pattern . '/', $replacement, $line);
127                 
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 . ']';
135                 }
136         }
137
138         // Multiline part has no stopper
139         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
140             $modify === FALSE && $multiline != 0)
141                 $lines[] = str_repeat('}', $multiline);
142
143         return implode("\n", $lines);
144 }
145
146 // Generate ID
147 function generate_fixed_heading_anchor_id($seed)
148 {
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)),
152                 mt_rand(0, 24), 7);
153 }
154
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)
158 {
159         $array = array();
160
161         $fp = @fopen($file, 'r');
162         if ($fp === FALSE) return FALSE;
163         set_file_buffer($fp, 0);
164         if ($lock) flock($fp, LOCK_SH);
165         rewind($fp);
166         $index = 0;
167         while (! feof($fp)) {
168                 $line = fgets($fp, $buffer);
169                 if ($line != FALSE) $array[] = $line;
170                 if (++$index >= $count) break;
171         }
172         if ($lock) flock($fp, LOCK_UN);
173         if (! fclose($fp)) return FALSE;
174
175         return $array;
176 }
177
178 // Output to a file
179 function file_write($dir, $page, $str, $notimestamp = FALSE)
180 {
181         global $update_exec, $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
182         global $whatsdeleted, $maxshow_deleted;
183
184         if (PKWK_READONLY) return; // Do nothing
185         if ($dir != DATA_DIR && $dir != DIFF_DIR) die('file_write(): Invalid directory');
186
187         $page = strip_bracket($page);
188         $file = $dir . encode($page) . '.txt';
189         $file_exists = file_exists($file);
190
191         // ----
192         // Delete?
193
194         if ($dir == DATA_DIR && $str === '') {
195                 // Page deletion
196                 if (! $file_exists) return; // Ignore null posting for DATA_DIR
197
198                 // Update RecentDeleted (Add the $page)
199                 add_recent($page, $whatsdeleted, '', $maxshow_deleted);
200
201                 unlink($file);
202
203                 // Update RecentChanges (Remove the $page from RecentChanges)
204                 put_lastmodified();
205
206                 // Clear is_page() cache
207                 is_page($page, TRUE);
208
209                 return;
210
211         } else if ($dir == DIFF_DIR && $str === " \n") {
212                 return; // Ignore null posting for DIFF_DIR
213         }
214
215         // ----
216         // File replacement (Edit)
217
218         if (! is_pagename($page))
219                 die_message(str_replace('$1', htmlspecialchars($page),
220                             str_replace('$2', 'WikiName', $_msg_invalidiwn)));
221
222         $str = rtrim(preg_replace('/' . "\r" . '/', '', $str)) . "\n";
223         $timestamp = ($file_exists && $notimestamp) ? filemtime($file) : FALSE;
224
225         $fp = fopen($file, 'a') or die('fopen() failed: ' .
226                 htmlspecialchars(basename($dir) . '/' . encode($page) . '.txt') .       
227                 '<br />' . "\n" .
228                 'Maybe permission is not writable or filename is too long');
229         set_file_buffer($fp, 0);
230         flock($fp, LOCK_EX);
231         ftruncate($fp, 0);
232         rewind($fp);
233         fputs($fp, $str);
234         flock($fp, LOCK_UN);
235         fclose($fp);
236
237         if ($timestamp) pkwk_touch_file($file, $timestamp);
238
239         // Optional actions
240         if ($dir == DATA_DIR) {
241                 // Update RecentChanges (Add or renew the $page)
242                 if ($timestamp === FALSE) lastmodified_add($page);
243
244                 // Execute $update_exec here
245                 if ($update_exec) system($update_exec . ' > /dev/null &');
246
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');
256         }
257
258         is_page($page, TRUE); // Clear is_page() cache
259 }
260
261 // Update RecentDeleted
262 function add_recent($page, $recentpage, $subject = '', $limit = 0)
263 {
264         if (PKWK_READONLY || $limit == 0 || $page == '' || $recentpage == '' ||
265             check_non_list($page)) return;
266
267         // Load
268         $lines = $matches = array();
269         foreach (get_source($recentpage) as $line)
270                 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
271                         $lines[$matches[2]] = $line;
272
273         $_page = '[[' . $page . ']]';
274
275         // Remove a report about the same page
276         if (isset($lines[$_page])) unset($lines[$_page]);
277
278         // Add
279         array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
280                 htmlspecialchars($subject) . "\n");
281
282         // Get latest $limit reports
283         $lines = array_splice($lines, 0, $limit);
284
285         // Update
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);
291         flock($fp, LOCK_EX);
292         rewind($fp);
293         fputs($fp, '#freeze'    . "\n");
294         fputs($fp, '#norelated' . "\n"); // :)
295         fputs($fp, join('', $lines));
296         flock($fp, LOCK_UN);
297         fclose($fp);
298 }
299
300 // Update PKWK_MAXSHOW_CACHE itself (Add or renew about the $page) (Light)
301 // Use without $autolink
302 function lastmodified_add($page = '')
303 {
304         global $maxshow, $whatsnew, $autolink;
305
306         $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
307         if ($autolink || ! file_exists($file)) {
308                 put_lastmodified(); // Try to (re)create ALL
309                 return;
310         }
311
312         // Open
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);
317         flock($fp, LOCK_EX);
318
319         // Read
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];
324
325         // Remove if exists
326         if (isset($recent_pages[$page])) unset($recent_pages[$page]);
327
328         // Add: array_unshift()
329         $recent_pages = array($page => get_filetime($page)) + $recent_pages;
330
331         // Write
332         ftruncate($fp, 0);
333         rewind($fp);
334         foreach ($recent_pages as $_page=>$time)
335                 fputs($fp, $time . "\t" . $_page . "\n");
336
337         flock($fp, LOCK_UN);
338         fclose($fp);
339
340
341         // ----
342         // Update RecentChanges for the $page (VERBOSE! VERBOSE!)
343
344         $file   = get_filename($whatsnew);
345         $s_page = htmlspecialchars($page);
346
347         // Open
348         pkwk_touch_file($file);
349         $fp = fopen($file, 'r+') or
350                 die_message('Cannot open ' . htmlspecialchars($whatsnew));
351         set_file_buffer($fp, 0);
352         flock($fp, LOCK_EX);
353
354         // Read
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];
359
360         // If it already exists
361         if (isset($recent_pages[$s_page])) {
362                 unset($recent_pages[$s_page]); // Remove it for renewal
363         } else {
364                 array_pop($recent_pages);      // Remove the oldest one for $maxshow limit
365         }
366
367         // Add: array_unshift()
368         $s_lastmod = htmlspecialchars(format_date(get_filetime($page)));
369         $recent_pages = array($page => '-' . $s_lastmod . ' - ') + $recent_pages;
370
371         // Write
372         ftruncate($fp, 0);
373         rewind($fp);
374         foreach ($recent_pages as $page=>$line)
375                 fputs($fp, $line . '[[' . $page . ']]' . "\n");
376         fputs($fp, '#norelated' . "\n"); // :)
377
378         flock($fp, LOCK_UN);
379         fclose($fp);
380 }
381
382 // Re-create PKWK_MAXSHOW_CACHE (Heavy)
383 function put_lastmodified()
384 {
385         global $maxshow, $whatsnew, $autolink;
386
387         if (PKWK_READONLY) return; // Do nothing
388
389         // Get WHOLE page list
390         $pages = get_existpages();
391
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);
397
398         // Sort decending order of last-modification date
399         arsort($recent_pages, SORT_NUMERIC);
400
401         // Cut unused lines
402         $recent_pages = array_splice($recent_pages, 0, $maxshow + PKWK_MAXSHOW_ALLOWANCE);
403
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);
410         flock($fp, LOCK_EX);
411         ftruncate($fp, 0);
412         rewind($fp);
413         foreach ($recent_pages as $page=>$time)
414                 fputs($fp, $time . "\t" . $page . "\n");
415         flock($fp, LOCK_UN);
416         fclose($fp);
417
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);
424         flock($fp, LOCK_EX);
425         ftruncate($fp, 0);
426         rewind($fp);
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");
432         }
433         fputs($fp, '#norelated' . "\n"); // :)
434         flock($fp, LOCK_UN);
435         fclose($fp);
436
437         // For AutoLink
438         if ($autolink) {
439                 list($pattern, $pattern_a, $forceignorelist) =
440                         get_autolink_pattern($pages);
441
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);
447                 flock($fp, LOCK_EX);
448                 ftruncate($fp, 0);
449                 rewind($fp);
450                 fputs($fp, $pattern   . "\n");
451                 fputs($fp, $pattern_a . "\n");
452                 fputs($fp, join("\t", $forceignorelist) . "\n");
453                 flock($fp, LOCK_UN);
454                 fclose($fp);
455         }
456 }
457
458 // Get elapsed date of the page
459 function get_pg_passage($page, $sw = TRUE)
460 {
461         global $show_passage;
462         if (! $show_passage) return '';
463
464         $time = get_filetime($page);
465         $pg_passage = ($time != 0) ? get_passage($time) : '';
466
467         return $sw ? '<small>' . $pg_passage . '</small>' : ' ' . $pg_passage;
468 }
469
470 // Last-Modified header
471 function header_lastmod($page = NULL)
472 {
473         global $lastmod;
474
475         if ($lastmod && is_page($page)) {
476                 pkwk_headers_sent();
477                 header('Last-Modified: ' .
478                         date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
479         }
480 }
481
482 // Get a page list of this wiki
483 function get_existpages($dir = DATA_DIR, $ext = '.txt')
484 {
485         $aryret = array();
486
487         $pattern = '((?:[0-9A-F]{2})+)';
488         if ($ext != '') $ext = preg_quote($ext, '/');
489         $pattern = '/^' . $pattern . $ext . '$/';
490
491         $dp = @opendir($dir) or
492                 die_message($dir . ' is not found or not readable.');
493         $matches = array();
494         while ($file = readdir($dp))
495                 if (preg_match($pattern, $file, $matches))
496                         $aryret[$file] = decode($matches[1]);
497         closedir($dp);
498
499         return $aryret;
500 }
501
502 // Get PageReading(pronounce-annotated) data in an array()
503 function get_readings()
504 {
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;
509
510         $pages = get_existpages();
511
512         $readings = array();
513         foreach ($pages as $page) 
514                 $readings[$page] = '';
515
516         $deletedPage = FALSE;
517         $matches = array();
518         foreach (get_source($pagereading_config_page) as $line) {
519                 $line = chop($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];
524                         } else {
525                                 // This page seems deleted
526                                 $deletedPage = TRUE;
527                         }
528                 }
529         }
530
531         // If enabled ChaSen/KAKASI execution
532         if($pagereading_enable) {
533
534                 // Check there's non-clear-pronouncing page
535                 $unknownPage = FALSE;
536                 foreach ($readings as $page => $reading) {
537                         if($reading == '') {
538                                 $unknownPage = TRUE;
539                                 break;
540                         }
541                 }
542
543                 // Execute ChaSen/KAKASI, and get annotation
544                 if($unknownPage) {
545                         switch(strtolower($pagereading_kanji2kana_converter)) {
546                         case 'chasen':
547                                 if(! file_exists($pagereading_chasen_path))
548                                         die_message('ChaSen not found: ' . $pagereading_chasen_path);
549
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));
557                                 }
558                                 fclose($fp);
559
560                                 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
561                                 $fp     = popen($chasen, 'r');
562                                 if($fp === FALSE) {
563                                         unlink($tmpfname);
564                                         die_message('ChaSen execution failed: ' . $chasen);
565                                 }
566                                 foreach ($readings as $page => $reading) {
567                                         if($reading != '') continue;
568
569                                         $line = fgets($fp);
570                                         $line = mb_convert_encoding($line, SOURCE_ENCODING,
571                                                 $pagereading_kanji2kana_encoding);
572                                         $line = chop($line);
573                                         $readings[$page] = $line;
574                                 }
575                                 pclose($fp);
576
577                                 unlink($tmpfname) or
578                                         die_message('Temporary file can not be removed: ' . $tmpfname);
579                                 break;
580
581                         case 'kakasi':  /*FALLTHROUGH*/
582                         case 'kakashi':
583                                 if(! file_exists($pagereading_kakasi_path))
584                                         die_message('KAKASI not found: ' . $pagereading_kakasi_path);
585
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));
593                                 }
594                                 fclose($fp);
595
596                                 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
597                                 $fp     = popen($kakasi, 'r');
598                                 if($fp === FALSE) {
599                                         unlink($tmpfname);
600                                         die_message('KAKASI execution failed: ' . $kakasi);
601                                 }
602
603                                 foreach ($readings as $page => $reading) {
604                                         if($reading != '') continue;
605
606                                         $line = fgets($fp);
607                                         $line = mb_convert_encoding($line, SOURCE_ENCODING,
608                                                 $pagereading_kanji2kana_encoding);
609                                         $line = chop($line);
610                                         $readings[$page] = $line;
611                                 }
612                                 pclose($fp);
613
614                                 unlink($tmpfname) or
615                                         die_message('Temporary file can not be removed: ' . $tmpfname);
616                                 break;
617
618                         case 'none':
619                                 $patterns = $replacements = $matches = array();
620                                 foreach (get_source($pagereading_config_dict) as $line) {
621                                         $line = chop($line);
622                                         if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
623                                                 $patterns[]     = $matches[1];
624                                                 $replacements[] = $matches[2];
625                                         }
626                                 }
627                                 foreach ($readings as $page => $reading) {
628                                         if($reading != '') continue;
629
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');
634                                 }
635                                 break;
636
637                         default:
638                                 die_message('Unknown kanji-kana converter: ' . $pagereading_kanji2kana_converter . '.');
639                                 break;
640                         }
641                 }
642
643                 if($unknownPage || $deletedPage) {
644
645                         asort($readings); // Sort by pronouncing(alphabetical/reading) order
646                         $body = '';
647                         foreach ($readings as $page => $reading)
648                                 $body .= '-[[' . $page . ']] ' . $reading . "\n";
649
650                         page_write($pagereading_config_page, $body);
651                 }
652         }
653
654         // Pages that are not prounouncing-clear, return pagenames of themselves
655         foreach ($pages as $page) {
656                 if($readings[$page] == '')
657                         $readings[$page] = $page;
658         }
659
660         return $readings;
661 }
662
663 // Get a list of encoded files (must specify a directory and a suffix)
664 function get_existfiles($dir, $ext)
665 {
666         $pattern = '/^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$/';
667         $aryret = array();
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;
672         closedir($dp);
673         return $aryret;
674 }
675
676 // Get a list of related pages of the page
677 function links_get_related($page)
678 {
679         global $vars, $related;
680         static $links = array();
681
682         if (isset($links[$page])) return $links[$page];
683
684         // If possible, merge related pages generated by make_link()
685         $links[$page] = ($page == $vars['page']) ? $related : array();
686
687         // Get repated pages from DB
688         $links[$page] += links_get_related_db($vars['page']);
689
690         return $links[$page];
691 }
692
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)
696 {
697         static $php_uid; // PHP's UID
698
699         if (! isset($php_uid)) {
700                 if (extension_loaded('posix')) {
701                         $php_uid = posix_getuid(); // Unix
702                 } else {
703                         $php_uid = 0; // Windows
704                 }
705         }
706
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');
713
714         // Check owner
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
720         } else {
721                 $tmp = $filename . '.' . getmypid() . '.tmp';
722
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)));
728
729                 // Try to chown by re-creating files
730                 // NOTE:
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');
739
740                 fclose($ffile) or die('pkwk_chown(): fclose() failed');
741
742                 if ($result === FALSE) @unlink($tmp);
743         }
744
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');
748
749         return $result;
750 }
751
752 // touch() with trying pkwk_chown()
753 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
754 {
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);
761                 } else {
762                         $result = touch($filename, $time, $atime);
763                 }
764                 return $result;
765         } else {
766                 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
767                         htmlspecialchars(basename($filename)));
768         }
769 }
770 ?>