<?php
// PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: file.php,v 1.59 2006/04/12 14:38:51 henoheno Exp $
+// $Id: file.php,v 1.95 2011/01/25 15:01:01 henoheno Exp $
// Copyright (C)
// 2002-2006 PukiWiki Developers Team
// 2001-2002 Originally written by yu-ji
// File related functions
// RecentChanges
-define('PKWK_MAXSHOW_CACHE', 'recent.dat');
define('PKWK_MAXSHOW_ALLOWANCE', 10);
+define('PKWK_MAXSHOW_CACHE', 'recent.dat');
// AutoLink
define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
// Get source(wiki text) data of the page
-function get_source($page = NULL, $lock = TRUE)
+function get_source($page = NULL, $lock = TRUE, $join = FALSE)
{
- $array = array();
+ $result = $join ? '' : array();
if (is_page($page)) {
$path = get_filename($page);
if ($lock) {
$fp = @fopen($path, 'r');
- if ($fp == FALSE) return $array;
+ if ($fp == FALSE) return $result;
flock($fp, LOCK_SH);
}
- // Removing line-feeds: Because file() doesn't remove them.
- $array = str_replace("\r", '', file($path));
+ if ($join) {
+ // Returns a value
+ $size = filesize($path);
+ if ($size > 0) {
+ $result = str_replace("\r", '', fread($fp, filesize($path)));
+ } else {
+ $result = '';
+ }
+ } else {
+ // Returns an array
+ // Removing line-feeds: Because file() doesn't remove them.
+ $result = str_replace("\r", '', file($path));
+ }
if ($lock) {
flock($fp, LOCK_UN);
}
}
- return $array;
+ return $result;
}
// Get last-modified filetime of the page
// Put a data(wiki text) into a physical file(diff, backup, text)
function page_write($page, $postdata, $notimestamp = FALSE)
{
- global $trackback;
-
if (PKWK_READONLY) return; // Do nothing
$postdata = make_str_rules($postdata);
// Create wiki text
file_write(DATA_DIR, $page, $postdata, $notimestamp);
- if ($trackback) {
- // TrackBack Ping
- $_diff = explode("\n", $diffdata);
- $plus = join("\n", preg_replace('/^\+/', '', preg_grep('/^\+/', $_diff)));
- $minus = join("\n", preg_replace('/^-/', '', preg_grep('/^-/', $_diff)));
- tb_send($page, $plus, $minus);
- }
-
links_update($page);
}
// Output to a file
function file_write($dir, $page, $str, $notimestamp = FALSE)
{
- global $update_exec, $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
+ global $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
global $whatsdeleted, $maxshow_deleted;
if (PKWK_READONLY) return; // Do nothing
// Update RecentDeleted (Add the $page)
add_recent($page, $whatsdeleted, '', $maxshow_deleted);
+ // Remove the page
unlink($file);
- // Update RecentChanges (Remove the $page from RecentChanges)
- put_lastmodified();
+ // Update RecentDeleted, and remove the page from RecentChanges
+ lastmodified_add($whatsdeleted, $page);
// Clear is_page() cache
is_page($page, TRUE);
// File replacement (Edit)
if (! is_pagename($page))
- die_message(str_replace('$1', htmlspecialchars($page),
+ die_message(str_replace('$1', htmlsc($page),
str_replace('$2', 'WikiName', $_msg_invalidiwn)));
$str = rtrim(preg_replace('/' . "\r" . '/', '', $str)) . "\n";
$timestamp = ($file_exists && $notimestamp) ? filemtime($file) : FALSE;
$fp = fopen($file, 'a') or die('fopen() failed: ' .
- htmlspecialchars(basename($dir) . '/' . encode($page) . '.txt') .
+ htmlsc(basename($dir) . '/' . encode($page) . '.txt') .
'<br />' . "\n" .
'Maybe permission is not writable or filename is too long');
set_file_buffer($fp, 0);
// Update RecentChanges (Add or renew the $page)
if ($timestamp === FALSE) lastmodified_add($page);
- // Execute $update_exec here
- if ($update_exec) system($update_exec . ' > /dev/null &');
+ // Command execution per update
+ if (defined('PKWK_UPDATE_EXEC') && PKWK_UPDATE_EXEC)
+ system(PKWK_UPDATE_EXEC . ' > /dev/null &');
} else if ($dir == DIFF_DIR && $notify) {
if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
// Add
array_unshift($lines, '-' . format_date(UTIME) . ' - ' . $_page .
- htmlspecialchars($subject) . "\n");
+ htmlsc($subject) . "\n");
// Get latest $limit reports
$lines = array_splice($lines, 0, $limit);
// Update
$fp = fopen(get_filename($recentpage), 'w') or
die_message('Cannot write page file ' .
- htmlspecialchars($recentpage) .
+ htmlsc($recentpage) .
'<br />Maybe permission is not writable or filename is too long');
set_file_buffer($fp, 0);
flock($fp, LOCK_EX);
// Update PKWK_MAXSHOW_CACHE itself (Add or renew about the $page) (Light)
// Use without $autolink
-function lastmodified_add($page = '')
+function lastmodified_add($update = '', $remove = '')
{
global $maxshow, $whatsnew, $autolink;
+ // AutoLink implimentation needs everything, for now
+ if ($autolink) {
+ put_lastmodified(); // Try to (re)create ALL
+ return;
+ }
+
+ if (($update == '' || check_non_list($update)) && $remove == '')
+ return; // No need
+
$file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
- if ($autolink || ! file_exists($file)) {
+ if (! file_exists($file)) {
put_lastmodified(); // Try to (re)create ALL
return;
}
set_file_buffer($fp, 0);
flock($fp, LOCK_EX);
- // Read
+ // Read (keep the order of the lines)
$recent_pages = $matches = array();
foreach(file_head($file, $maxshow + PKWK_MAXSHOW_ALLOWANCE, FALSE) as $line)
if (preg_match('/^([0-9]+)\t(.+)/', $line, $matches))
$recent_pages[$matches[2]] = $matches[1];
- // Remove if exists
- if (isset($recent_pages[$page])) unset($recent_pages[$page]);
+ // Remove if it exists inside
+ if (isset($recent_pages[$update])) unset($recent_pages[$update]);
+ if (isset($recent_pages[$remove])) unset($recent_pages[$remove]);
- // Add: array_unshift()
- $recent_pages = array($page => get_filetime($page)) + $recent_pages;
+ // Add to the top: like array_unshift()
+ if ($update != '')
+ $recent_pages = array($update => get_filetime($update)) + $recent_pages;
- // Write
- ftruncate($fp, 0);
- rewind($fp);
- foreach ($recent_pages as $_page=>$time)
- fputs($fp, $time . "\t" . $_page . "\n");
+ // Check
+ $abort = count($recent_pages) < $maxshow;
+
+ if (! $abort) {
+ // Write
+ ftruncate($fp, 0);
+ rewind($fp);
+ foreach ($recent_pages as $_page=>$time)
+ fputs($fp, $time . "\t" . $_page . "\n");
+ }
flock($fp, LOCK_UN);
fclose($fp);
+ if ($abort) {
+ put_lastmodified(); // Try to (re)create ALL
+ return;
+ }
+
+
// ----
- // Update RecentChanges for the $page (VERBOSE! VERBOSE!)
+ // Update the page 'RecentChanges'
- $file = get_filename($whatsnew);
- $s_page = htmlspecialchars($page);
+ $recent_pages = array_splice($recent_pages, 0, $maxshow);
+ $file = get_filename($whatsnew);
// Open
pkwk_touch_file($file);
$fp = fopen($file, 'r+') or
- die_message('Cannot open ' . htmlspecialchars($whatsnew));
+ die_message('Cannot open ' . htmlsc($whatsnew));
set_file_buffer($fp, 0);
flock($fp, LOCK_EX);
- // Read
- $recent_pages = $matches = array();
- foreach(file_head($file, $maxshow, FALSE) as $line)
- if (preg_match('/^(- *[0-9].* - )\[\[(.+)\]\]$/', $line, $matches))
- $recent_pages[$matches[2]] = $matches[1];
-
- // If it already exists
- if (isset($recent_pages[$s_page])) {
- unset($recent_pages[$s_page]); // Remove it for renewal
- } else {
- array_pop($recent_pages); // Remove the oldest one for $maxshow limit
- }
-
- // Add: array_unshift()
- $s_lastmod = htmlspecialchars(format_date(get_filetime($page)));
- $recent_pages = array($page => '-' . $s_lastmod . ' - ') + $recent_pages;
-
- // Write
+ // Recreate
ftruncate($fp, 0);
rewind($fp);
- foreach ($recent_pages as $page=>$line)
- fputs($fp, $line . '[[' . $page . ']]' . "\n");
+ foreach ($recent_pages as $_page=>$time)
+ fputs($fp, '-' . htmlsc(format_date($time)) .
+ ' - ' . '[[' . htmlsc($_page) . ']]' . "\n");
fputs($fp, '#norelated' . "\n"); // :)
flock($fp, LOCK_UN);
arsort($recent_pages, SORT_NUMERIC);
// Cut unused lines
- $recent_pages = array_splice($recent_pages, 0, $maxshow + PKWK_MAXSHOW_ALLOWANCE);
+ // BugTrack2/179: array_splice() will break integer keys in hashtable
+ $count = $maxshow + PKWK_MAXSHOW_ALLOWANCE;
+ $_recent = array();
+ foreach($recent_pages as $key=>$value) {
+ unset($recent_pages[$key]);
+ $_recent[$key] = $value;
+ if (--$count < 1) break;
+ }
+ $recent_pages = & $_recent;
// Re-create PKWK_MAXSHOW_CACHE
$file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
$file = get_filename($whatsnew);
pkwk_touch_file($file);
$fp = fopen($file, 'r+') or
- die_message('Cannot open ' . htmlspecialchars($whatsnew));
+ die_message('Cannot open ' . htmlsc($whatsnew));
set_file_buffer($fp, 0);
flock($fp, LOCK_EX);
ftruncate($fp, 0);
rewind($fp);
foreach (array_keys($recent_pages) as $page) {
$time = $recent_pages[$page];
- $s_lastmod = htmlspecialchars(format_date($time));
- $s_page = htmlspecialchars($page);
+ $s_lastmod = htmlsc(format_date($time));
+ $s_page = htmlsc($page);
fputs($fp, '-' . $s_lastmod . ' - [[' . $s_page . ']]' . "\n");
}
fputs($fp, '#norelated' . "\n"); // :)
if($unknownPage || $deletedPage) {
- asort($readings); // Sort by pronouncing(alphabetical/reading) order
+ asort($readings, SORT_STRING); // Sort by pronouncing(alphabetical/reading) order
$body = '';
foreach ($readings as $page => $reading)
$body .= '-[[' . $page . ']] ' . $reading . "\n";
$lockfile = CACHE_DIR . 'pkwk_chown.lock';
$flock = fopen($lockfile, 'a') or
die('pkwk_chown(): fopen() failed for: CACHEDIR/' .
- basename(htmlspecialchars($lockfile)));
+ basename(htmlsc($lockfile)));
flock($flock, LOCK_EX) or die('pkwk_chown(): flock() failed for lock');
// Check owner
$stat = stat($filename) or
- die('pkwk_chown(): stat() failed for: ' . basename(htmlspecialchars($filename)));
+ die('pkwk_chown(): stat() failed for: ' . basename(htmlsc($filename)));
if ($stat[4] === $php_uid) {
// NOTE: Windows always here
$result = TRUE; // Seems the same UID. Nothing to do
// NOTE: Not 'r+'. Don't check write permission here
$ffile = fopen($filename, 'r') or
die('pkwk_chown(): fopen() failed for: ' .
- basename(htmlspecialchars($filename)));
+ basename(htmlsc($filename)));
// Try to chown by re-creating files
// NOTE:
return $result;
} else {
die('pkwk_touch_file(): Invalid UID and (not writable for the directory or not a flie): ' .
- htmlspecialchars(basename($filename)));
+ htmlsc(basename($filename)));
}
}
?>