2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2002-2016 PukiWiki Development 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_ALLOWANCE', 10);
13 define('PKWK_MAXSHOW_CACHE', 'recent.dat');
16 define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
18 // Get source(wiki text) data of the page
19 // Returns FALSE if error occurerd
20 function get_source($page = NULL, $lock = TRUE, $join = FALSE)
22 //$result = NULL; // File is not found
23 $result = $join ? '' : array();
24 // Compat for "implode('', get_source($file))",
25 // -- this is slower than "get_source($file, TRUE, TRUE)"
26 // Compat for foreach(get_source($file) as $line) {} not to warns
28 $path = get_filename($page);
29 if (file_exists($path)) {
32 $fp = @fopen($path, 'r');
33 if ($fp === FALSE) return FALSE;
39 $size = filesize($path);
40 if ($size === FALSE) {
42 } else if ($size == 0) {
45 $result = fread($fp, $size);
46 if ($result !== FALSE) {
47 // Removing line-feeds
48 $result = str_replace("\r", '', $result);
53 $result = file($path);
54 if ($result !== FALSE) {
55 // Removing line-feeds
56 $result = str_replace("\r", '', $result);
69 // Get last-modified filetime of the page
70 function get_filetime($page)
72 return is_page($page) ? filemtime(get_filename($page)) - LOCALZONE : 0;
75 // Get physical file name of the page
76 function get_filename($page)
78 return DATA_DIR . encode($page) . '.txt';
81 // Put a data(wiki text) into a physical file(diff, backup, text)
82 function page_write($page, $postdata, $notimestamp = FALSE)
84 if (PKWK_READONLY) return; // Do nothing
86 $postdata = make_str_rules($postdata);
87 $text_without_author = remove_author_info($postdata);
88 $postdata = add_author_info($text_without_author);
89 $is_delete = empty($text_without_author);
91 // Do nothing when it has no changes
92 $oldpostdata = is_page($page) ? join('', get_source($page)) : '';
93 $oldtext_without_author = remove_author_info($oldpostdata);
94 if ($text_without_author === $oldtext_without_author) {
95 // Do nothing on updating with unchanged content
98 // Create and write diff
99 $diffdata = do_diff($oldpostdata, $postdata);
100 file_write(DIFF_DIR, $page, $diffdata);
103 make_backup($page, $is_delete, $postdata); // Is $postdata null?
106 file_write(DATA_DIR, $page, $postdata, $notimestamp, $is_delete);
111 // Modify original text with user-defined / system-defined rules
112 function make_str_rules($source)
114 global $str_rules, $fixed_heading_anchor;
116 $lines = explode("\n", $source);
117 $count = count($lines);
122 for ($i = 0; $i < $count; $i++) {
123 $line = & $lines[$i]; // Modify directly
125 // Ignore null string and preformatted texts
126 if ($line == '' || $line{0} == ' ' || $line{0} == "\t") continue;
130 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
132 preg_match('/#[^{]*(\{\{+)\s*$/', $line, $matches)) {
133 // Multiline convert plugin start
135 $multiline = strlen($matches[1]); // Set specific number
138 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
140 preg_match('/^\}{' . $multiline . '}\s*$/', $line)) {
141 // Multiline convert plugin end
146 if ($modify === FALSE) continue;
148 // Replace with $str_rules
149 foreach ($str_rules as $pattern => $replacement)
150 $line = preg_replace('/' . $pattern . '/', $replacement, $line);
152 // Adding fixed anchor into headings
153 if ($fixed_heading_anchor &&
154 preg_match('/^(\*{1,3}.*?)(?:\[#([A-Za-z][\w-]*)\]\s*)?$/', $line, $matches) &&
155 (! isset($matches[2]) || $matches[2] == '')) {
156 // Generate unique id
157 $anchor = generate_fixed_heading_anchor_id($matches[1]);
158 $line = rtrim($matches[1]) . ' [#' . $anchor . ']';
162 // Multiline part has no stopper
163 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
164 $modify === FALSE && $multiline != 0)
165 $lines[] = str_repeat('}', $multiline);
167 return implode("\n", $lines);
170 function add_author_info($wikitext)
172 global $auth_user, $auth_user_fullname, $auth_type, $ldap_user_account;
173 global $auth_provider_user_prefix_default;
174 global $auth_provider_user_prefix_ldap;
175 global $auth_provider_user_prefix_external;
176 $author = preg_replace('/"/', '', $auth_user);
177 $fullname = $auth_user_fullname;
178 if (!$fullname && $author) {
179 // Fullname is empty, use $author as its fullname
180 $fullname = preg_replace('/^[^:]*:/', '', $author);
182 $displayname = preg_replace('/"/', '', $fullname);
184 switch ($auth_type) {
185 case AUTH_TYPE_BASIC:
186 $user_prefix = $auth_provider_user_prefix_default;
188 case AUTH_TYPE_EXTERNAL:
189 case AUTH_TYPE_EXTERNAL_REMOTE_USER:
190 case AUTH_TYPE_EXTERNAL_X_FORWARDED_USER:
191 $user_prefix = $auth_provider_user_prefix_external;
194 if ($ldap_user_account) {
195 $user_prefix = $auth_provider_user_prefix_ldap;
197 $user_prefix = $auth_provider_user_prefix_default;
201 $author_text = sprintf('#author("%s","%s","%s")',
202 get_date_atom(UTIME + LOCALZONE),
203 ($author ? $user_prefix . $author : ''),
204 $displayname) . "\n";
205 return $author_text . $wikitext;
208 function remove_author_info($wikitext)
210 return preg_replace('/^\s*#author\([^\n]*(\n|$)/m', '', $wikitext);
213 function get_date_atom($timestamp)
215 // Compatible with DATE_ATOM format
216 // return date(DATE_ATOM, $timestamp);
217 $zmin = abs(LOCALZONE / 60);
218 return date('Y-m-d\TH:i:s', $timestamp) . sprintf('%s%02d:%02d',
219 (LOCALZONE < 0 ? '-' : '+') , $zmin / 60, $zmin % 60);
223 function generate_fixed_heading_anchor_id($seed)
225 // A random alphabetic letter + 7 letters of random strings from md()
226 return chr(mt_rand(ord('a'), ord('z'))) .
227 substr(md5(uniqid(substr($seed, 0, 100), TRUE)),
231 // Read top N lines as an array
232 // (Use PHP file() function if you want to get ALL lines)
233 function file_head($file, $count = 1, $lock = TRUE, $buffer = 8192)
237 $fp = @fopen($file, 'r');
238 if ($fp === FALSE) return FALSE;
239 set_file_buffer($fp, 0);
240 if ($lock) flock($fp, LOCK_SH);
243 while (! feof($fp)) {
244 $line = fgets($fp, $buffer);
245 if ($line != FALSE) $array[] = $line;
246 if (++$index >= $count) break;
248 if ($lock) flock($fp, LOCK_UN);
249 if (! fclose($fp)) return FALSE;
255 function file_write($dir, $page, $str, $notimestamp = FALSE, $is_delete = FALSE)
257 global $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
258 global $whatsdeleted, $maxshow_deleted;
260 if (PKWK_READONLY) return; // Do nothing
261 if ($dir != DATA_DIR && $dir != DIFF_DIR) die('file_write(): Invalid directory');
263 $page = strip_bracket($page);
264 $file = $dir . encode($page) . '.txt';
265 $file_exists = file_exists($file);
270 if ($dir == DATA_DIR && $is_delete) {
272 if (! $file_exists) return; // Ignore null posting for DATA_DIR
274 // Update RecentDeleted (Add the $page)
275 add_recent($page, $whatsdeleted, '', $maxshow_deleted);
280 // Update RecentDeleted, and remove the page from RecentChanges
281 lastmodified_add($whatsdeleted, $page);
283 // Clear is_page() cache
284 is_page($page, TRUE);
288 } else if ($dir == DIFF_DIR && $str === " \n") {
289 return; // Ignore null posting for DIFF_DIR
293 // File replacement (Edit)
295 if (! is_pagename($page))
296 die_message(str_replace('$1', htmlsc($page),
297 str_replace('$2', 'WikiName', $_msg_invalidiwn)));
299 $str = rtrim(preg_replace('/' . "\r" . '/', '', $str)) . "\n";
300 $timestamp = ($file_exists && $notimestamp) ? filemtime($file) : FALSE;
302 $fp = fopen($file, 'a') or die('fopen() failed: ' .
303 htmlsc(basename($dir) . '/' . encode($page) . '.txt') .
305 'Maybe permission is not writable or filename is too long');
306 set_file_buffer($fp, 0);
314 if ($timestamp) pkwk_touch_file($file, $timestamp);
317 if ($dir == DATA_DIR) {
318 // Update RecentChanges (Add or renew the $page)
319 if ($timestamp === FALSE) lastmodified_add($page);
321 // Command execution per update
322 if (defined('PKWK_UPDATE_EXEC') && PKWK_UPDATE_EXEC)
323 system(PKWK_UPDATE_EXEC . ' > /dev/null &');
325 } else if ($dir == DIFF_DIR && $notify) {
326 if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
327 $footer['ACTION'] = 'Page update';
328 $footer['PAGE'] = & $page;
329 $footer['URI'] = get_script_uri() . '?' . pagename_urlencode($page);
330 $footer['USER_AGENT'] = TRUE;
331 $footer['REMOTE_ADDR'] = TRUE;
332 pkwk_mail_notify($notify_subject, $str, $footer) or
333 die('pkwk_mail_notify(): Failed');
336 is_page($page, TRUE); // Clear is_page() cache
339 // Update RecentDeleted
340 function add_recent($page, $recentpage, $subject = '', $limit = 0)
342 if (PKWK_READONLY || $limit == 0 || $page == '' || $recentpage == '' ||
343 check_non_list($page)) return;
346 $lines = $matches = array();
347 foreach (get_source($recentpage) as $line)
348 if (preg_match('/^-(.+) - (\[\[.+\]\])$/', $line, $matches))
349 $lines[$matches[2]] = $line;
351 $_page = '[[' . $page . ']]';
353 // Remove a report about the same page
354 if (isset($lines[$_page])) unset($lines[$_page]);
357 array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
358 htmlsc($subject) . "\n");
360 // Get latest $limit reports
361 $lines = array_splice($lines, 0, $limit);
364 $fp = fopen(get_filename($recentpage), 'w') or
365 die_message('Cannot write page file ' .
366 htmlsc($recentpage) .
367 '<br />Maybe permission is not writable or filename is too long');
368 set_file_buffer($fp, 0);
371 fputs($fp, '#freeze' . "\n");
372 fputs($fp, '#norelated' . "\n"); // :)
373 fputs($fp, join('', $lines));
378 // Update PKWK_MAXSHOW_CACHE itself (Add or renew about the $page) (Light)
379 // Use without $autolink
380 function lastmodified_add($update = '', $remove = '')
382 global $maxshow, $whatsnew, $autolink;
384 // AutoLink implimentation needs everything, for now
386 put_lastmodified(); // Try to (re)create ALL
390 if (($update == '' || check_non_list($update)) && $remove == '')
393 $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
394 if (! file_exists($file)) {
395 put_lastmodified(); // Try to (re)create ALL
400 pkwk_touch_file($file);
401 $fp = fopen($file, 'r+') or
402 die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_MAXSHOW_CACHE);
403 set_file_buffer($fp, 0);
406 // Read (keep the order of the lines)
407 $recent_pages = $matches = array();
408 foreach(file_head($file, $maxshow + PKWK_MAXSHOW_ALLOWANCE, FALSE) as $line)
409 if (preg_match('/^([0-9]+)\t(.+)/', $line, $matches))
410 $recent_pages[$matches[2]] = $matches[1];
412 // Remove if it exists inside
413 if (isset($recent_pages[$update])) unset($recent_pages[$update]);
414 if (isset($recent_pages[$remove])) unset($recent_pages[$remove]);
416 // Add to the top: like array_unshift()
418 $recent_pages = array($update => get_filetime($update)) + $recent_pages;
421 $abort = count($recent_pages) < $maxshow;
427 foreach ($recent_pages as $_page=>$time)
428 fputs($fp, $time . "\t" . $_page . "\n");
435 put_lastmodified(); // Try to (re)create ALL
442 // Update the page 'RecentChanges'
444 $recent_pages = array_splice($recent_pages, 0, $maxshow);
445 $file = get_filename($whatsnew);
448 pkwk_touch_file($file);
449 $fp = fopen($file, 'r+') or
450 die_message('Cannot open ' . htmlsc($whatsnew));
451 set_file_buffer($fp, 0);
457 foreach ($recent_pages as $_page=>$time)
458 fputs($fp, '-' . htmlsc(format_date($time)) .
459 ' - ' . '[[' . htmlsc($_page) . ']]' . "\n");
460 fputs($fp, '#norelated' . "\n"); // :)
466 // Re-create PKWK_MAXSHOW_CACHE (Heavy)
467 function put_lastmodified()
469 global $maxshow, $whatsnew, $autolink;
471 if (PKWK_READONLY) return; // Do nothing
473 // Get WHOLE page list
474 $pages = get_existpages();
476 // Check ALL filetime
477 $recent_pages = array();
478 foreach($pages as $page)
479 if ($page !== $whatsnew && ! check_non_list($page))
480 $recent_pages[$page] = get_filetime($page);
482 // Sort decending order of last-modification date
483 arsort($recent_pages, SORT_NUMERIC);
486 // BugTrack2/179: array_splice() will break integer keys in hashtable
487 $count = $maxshow + PKWK_MAXSHOW_ALLOWANCE;
489 foreach($recent_pages as $key=>$value) {
490 unset($recent_pages[$key]);
491 $_recent[$key] = $value;
492 if (--$count < 1) break;
494 $recent_pages = & $_recent;
496 // Re-create PKWK_MAXSHOW_CACHE
497 $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
498 pkwk_touch_file($file);
499 $fp = fopen($file, 'r+') or
500 die_message('Cannot open' . 'CACHE_DIR/' . PKWK_MAXSHOW_CACHE);
501 set_file_buffer($fp, 0);
505 foreach ($recent_pages as $page=>$time)
506 fputs($fp, $time . "\t" . $page . "\n");
510 // Create RecentChanges
511 $file = get_filename($whatsnew);
512 pkwk_touch_file($file);
513 $fp = fopen($file, 'r+') or
514 die_message('Cannot open ' . htmlsc($whatsnew));
515 set_file_buffer($fp, 0);
519 foreach (array_keys($recent_pages) as $page) {
520 $time = $recent_pages[$page];
521 $s_lastmod = htmlsc(format_date($time));
522 $s_page = htmlsc($page);
523 fputs($fp, '-' . $s_lastmod . ' - [[' . $s_page . ']]' . "\n");
525 fputs($fp, '#norelated' . "\n"); // :)
531 list($pattern, $pattern_a, $forceignorelist) =
532 get_autolink_pattern($pages);
534 $file = CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE;
535 pkwk_touch_file($file);
536 $fp = fopen($file, 'r+') or
537 die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_AUTOLINK_REGEX_CACHE);
538 set_file_buffer($fp, 0);
542 fputs($fp, $pattern . "\n");
543 fputs($fp, $pattern_a . "\n");
544 fputs($fp, join("\t", $forceignorelist) . "\n");
550 // Get elapsed date of the page
551 function get_pg_passage($page, $sw = TRUE)
553 global $show_passage;
554 if (! $show_passage) return '';
556 $time = get_filetime($page);
557 $pg_passage = ($time != 0) ? get_passage($time) : '';
559 return $sw ? '<small>' . $pg_passage . '</small>' : ' ' . $pg_passage;
562 // Last-Modified header
563 function header_lastmod($page = NULL)
567 if ($lastmod && is_page($page)) {
569 header('Last-Modified: ' .
570 date('D, d M Y H:i:s', get_filetime($page)) . ' GMT');
574 // Get a list of encoded files (must specify a directory and a suffix)
575 function get_existfiles($dir = DATA_DIR, $ext = '.txt')
578 $pattern = '/^(?:[0-9A-F]{2})+' . preg_quote($ext, '/') . '$/';
580 $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
581 while (($file = readdir($dp)) !== FALSE) {
582 if (preg_match($pattern, $file)) {
583 $aryret[] = $dir . $file;
591 // Get a page list of this wiki
592 function get_existpages($dir = DATA_DIR, $ext = '.txt')
595 $pattern = '/^((?:[0-9A-F]{2})+)' . preg_quote($ext, '/') . '$/';
597 $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
599 while (($file = readdir($dp)) !== FALSE) {
600 if (preg_match($pattern, $file, $matches)) {
601 $aryret[$file] = decode($matches[1]);
609 // Get PageReading(pronounce-annotated) data in an array()
610 function get_readings()
612 global $pagereading_enable, $pagereading_kanji2kana_converter;
613 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
614 global $pagereading_kakasi_path, $pagereading_config_page;
615 global $pagereading_config_dict;
617 $pages = get_existpages();
620 foreach ($pages as $page)
621 $readings[$page] = '';
623 $deletedPage = FALSE;
625 foreach (get_source($pagereading_config_page) as $line) {
627 if(preg_match('/^-\[\[([^]]+)\]\]\s+(.+)$/', $line, $matches)) {
628 if(isset($readings[$matches[1]])) {
629 // This page is not clear how to be pronounced
630 $readings[$matches[1]] = $matches[2];
632 // This page seems deleted
638 // If enabled ChaSen/KAKASI execution
639 if($pagereading_enable) {
641 // Check there's non-clear-pronouncing page
642 $unknownPage = FALSE;
643 foreach ($readings as $page => $reading) {
650 // Execute ChaSen/KAKASI, and get annotation
652 switch(strtolower($pagereading_kanji2kana_converter)) {
654 if(! file_exists($pagereading_chasen_path))
655 die_message('ChaSen not found: ' . $pagereading_chasen_path);
657 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
658 $fp = fopen($tmpfname, 'w') or
659 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
660 foreach ($readings as $page => $reading) {
661 if($reading != '') continue;
662 fputs($fp, mb_convert_encoding($page . "\n",
663 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
667 $chasen = "$pagereading_chasen_path -F %y $tmpfname";
668 $fp = popen($chasen, 'r');
671 die_message('ChaSen execution failed: ' . $chasen);
673 foreach ($readings as $page => $reading) {
674 if($reading != '') continue;
677 $line = mb_convert_encoding($line, SOURCE_ENCODING,
678 $pagereading_kanji2kana_encoding);
680 $readings[$page] = $line;
685 die_message('Temporary file can not be removed: ' . $tmpfname);
688 case 'kakasi': /*FALLTHROUGH*/
690 if(! file_exists($pagereading_kakasi_path))
691 die_message('KAKASI not found: ' . $pagereading_kakasi_path);
693 $tmpfname = tempnam(realpath(CACHE_DIR), 'PageReading');
694 $fp = fopen($tmpfname, 'w') or
695 die_message('Cannot write temporary file "' . $tmpfname . '".' . "\n");
696 foreach ($readings as $page => $reading) {
697 if($reading != '') continue;
698 fputs($fp, mb_convert_encoding($page . "\n",
699 $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
703 $kakasi = "$pagereading_kakasi_path -kK -HK -JK < $tmpfname";
704 $fp = popen($kakasi, 'r');
707 die_message('KAKASI execution failed: ' . $kakasi);
710 foreach ($readings as $page => $reading) {
711 if($reading != '') continue;
714 $line = mb_convert_encoding($line, SOURCE_ENCODING,
715 $pagereading_kanji2kana_encoding);
717 $readings[$page] = $line;
722 die_message('Temporary file can not be removed: ' . $tmpfname);
726 $patterns = $replacements = $matches = array();
727 foreach (get_source($pagereading_config_dict) as $line) {
729 if(preg_match('|^ /([^/]+)/,\s*(.+)$|', $line, $matches)) {
730 $patterns[] = $matches[1];
731 $replacements[] = $matches[2];
734 foreach ($readings as $page => $reading) {
735 if($reading != '') continue;
737 $readings[$page] = $page;
738 foreach ($patterns as $no => $pattern)
739 $readings[$page] = mb_convert_kana(mb_ereg_replace($pattern,
740 $replacements[$no], $readings[$page]), 'aKCV');
745 die_message('Unknown kanji-kana converter: ' . $pagereading_kanji2kana_converter . '.');
750 if($unknownPage || $deletedPage) {
752 asort($readings, SORT_STRING); // Sort by pronouncing(alphabetical/reading) order
754 foreach ($readings as $page => $reading)
755 $body .= '-[[' . $page . ']] ' . $reading . "\n";
757 page_write($pagereading_config_page, $body);
761 // Pages that are not prounouncing-clear, return pagenames of themselves
762 foreach ($pages as $page) {
763 if($readings[$page] == '')
764 $readings[$page] = $page;
770 // Get a list of related pages of the page
771 function links_get_related($page)
773 global $vars, $related;
774 static $links = array();
776 if (isset($links[$page])) return $links[$page];
778 // If possible, merge related pages generated by make_link()
779 $links[$page] = ($page === $vars['page']) ? $related : array();
781 // Get repated pages from DB
782 $links[$page] += links_get_related_db($vars['page']);
784 return $links[$page];
787 // _If needed_, re-create the file to change/correct ownership into PHP's
788 // NOTE: Not works for Windows
789 function pkwk_chown($filename, $preserve_time = TRUE)
791 static $php_uid; // PHP's UID
793 if (! isset($php_uid)) {
794 if (extension_loaded('posix')) {
795 $php_uid = posix_getuid(); // Unix
797 $php_uid = 0; // Windows
801 // Lock for pkwk_chown()
802 $lockfile = CACHE_DIR . 'pkwk_chown.lock';
803 $flock = fopen($lockfile, 'a') or
804 die('pkwk_chown(): fopen() failed for: CACHEDIR/' .
805 basename(htmlsc($lockfile)));
806 flock($flock, LOCK_EX) or die('pkwk_chown(): flock() failed for lock');
809 $stat = stat($filename) or
810 die('pkwk_chown(): stat() failed for: ' . basename(htmlsc($filename)));
811 if ($stat[4] === $php_uid) {
812 // NOTE: Windows always here
813 $result = TRUE; // Seems the same UID. Nothing to do
815 $tmp = $filename . '.' . getmypid() . '.tmp';
817 // Lock source $filename to avoid file corruption
818 // NOTE: Not 'r+'. Don't check write permission here
819 $ffile = fopen($filename, 'r') or
820 die('pkwk_chown(): fopen() failed for: ' .
821 basename(htmlsc($filename)));
823 // Try to chown by re-creating files
825 // * touch() before copy() is for 'rw-r--r--' instead of 'rwxr-xr-x' (with umask 022).
826 // * (PHP 4 < PHP 4.2.0) touch() with the third argument is not implemented and retuns NULL and Warn.
827 // * @unlink() before rename() is for Windows but here's for Unix only
828 flock($ffile, LOCK_EX) or die('pkwk_chown(): flock() failed');
829 $result = touch($tmp) && copy($filename, $tmp) &&
830 ($preserve_time ? (touch($tmp, $stat[9], $stat[8]) || touch($tmp, $stat[9])) : TRUE) &&
831 rename($tmp, $filename);
832 flock($ffile, LOCK_UN) or die('pkwk_chown(): flock() failed');
834 fclose($ffile) or die('pkwk_chown(): fclose() failed');
836 if ($result === FALSE) @unlink($tmp);
839 // Unlock for pkwk_chown()
840 flock($flock, LOCK_UN) or die('pkwk_chown(): flock() failed for lock');
841 fclose($flock) or die('pkwk_chown(): fclose() failed for lock');
846 // touch() with trying pkwk_chown()
847 function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
849 // Is the owner incorrected and unable to correct?
850 if (! file_exists($filename) || pkwk_chown($filename)) {
851 if ($time === FALSE) {
852 $result = touch($filename);
853 } else if ($atime === FALSE) {
854 $result = touch($filename, $time);
856 $result = touch($filename, $time, $atime);
860 die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
861 htmlsc(basename($filename)));