OSDN Git Service

BugTrack/2401 Locked file_get_contents/file_put_contents on bugtrack
[pukiwiki/pukiwiki.git] / lib / file.php
index 979cb5c..b90a2f8 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: file.php,v 1.95 2011/01/25 15:01:01 henoheno Exp $
+// file.php
 // Copyright (C)
-//   2002-2006 PukiWiki Developers Team
+//   2002-2016 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -44,7 +44,7 @@ function get_source($page = NULL, $lock = TRUE, $join = FALSE)
                        } else {
                                $result = fread($fp, $size);
                                if ($result !== FALSE) {
-                                       // Removing line-feeds
+                                       // Removing Carriage-Return
                                        $result = str_replace("\r", '', $result);
                                }
                        }
@@ -52,7 +52,7 @@ function get_source($page = NULL, $lock = TRUE, $join = FALSE)
                        // Returns an array
                        $result = file($path);
                        if ($result !== FALSE) {
-                               // Removing line-feeds
+                               // Removing Carriage-Return
                                $result = str_replace("\r", '', $result);
                        }
                }
@@ -84,17 +84,26 @@ function page_write($page, $postdata, $notimestamp = FALSE)
        if (PKWK_READONLY) return; // Do nothing
 
        $postdata = make_str_rules($postdata);
+       $text_without_author = remove_author_info($postdata);
+       $postdata = add_author_info($text_without_author);
+       $is_delete = empty($text_without_author);
 
-       // Create and write diff
+       // Do nothing when it has no changes
        $oldpostdata = is_page($page) ? join('', get_source($page)) : '';
+       $oldtext_without_author = remove_author_info($oldpostdata);
+       if ($text_without_author === $oldtext_without_author) {
+               // Do nothing on updating with unchanged content
+               return;
+       }
+       // Create and write diff
        $diffdata    = do_diff($oldpostdata, $postdata);
        file_write(DIFF_DIR, $page, $diffdata);
 
        // Create backup
-       make_backup($page, $postdata == ''); // Is $postdata null?
+       make_backup($page, $is_delete, $postdata); // Is $postdata null?
 
        // Create wiki text
-       file_write(DATA_DIR, $page, $postdata, $notimestamp);
+       file_write(DATA_DIR, $page, $postdata, $notimestamp, $is_delete);
 
        links_update($page);
 }
@@ -158,6 +167,38 @@ function make_str_rules($source)
        return implode("\n", $lines);
 }
 
+function add_author_info($wikitext)
+{
+       global $auth_user, $auth_user_fullname;
+       $author = preg_replace('/"/', '', $auth_user);
+       $fullname = $auth_user_fullname;
+       if (!$fullname && $author) {
+               // Fullname is empty, use $author as its fullname
+               $fullname = preg_replace('/^[^:]*:/', '', $author);
+       }
+       $displayname = preg_replace('/"/', '', $fullname);
+       $user_prefix = get_auth_user_prefix();
+       $author_text = sprintf('#author("%s","%s","%s")',
+               get_date_atom(UTIME + LOCALZONE),
+               ($author ? $user_prefix . $author : ''),
+               $displayname) . "\n";
+       return $author_text . $wikitext;
+}
+
+function remove_author_info($wikitext)
+{
+       return preg_replace('/^\s*#author\([^\n]*(\n|$)/m', '', $wikitext);
+}
+
+function get_date_atom($timestamp)
+{
+       // Compatible with DATE_ATOM format
+       // return date(DATE_ATOM, $timestamp);
+       $zmin = abs(LOCALZONE / 60);
+       return date('Y-m-d\TH:i:s', $timestamp) . sprintf('%s%02d:%02d',
+               (LOCALZONE < 0 ? '-' : '+') , $zmin / 60, $zmin % 60);
+}
+
 // Generate ID
 function generate_fixed_heading_anchor_id($seed)
 {
@@ -191,7 +232,7 @@ function file_head($file, $count = 1, $lock = TRUE, $buffer = 8192)
 }
 
 // Output to a file
-function file_write($dir, $page, $str, $notimestamp = FALSE)
+function file_write($dir, $page, $str, $notimestamp = FALSE, $is_delete = FALSE)
 {
        global $_msg_invalidiwn, $notify, $notify_diff_only, $notify_subject;
        global $whatsdeleted, $maxshow_deleted;
@@ -206,7 +247,7 @@ function file_write($dir, $page, $str, $notimestamp = FALSE)
        // ----
        // Delete?
 
-       if ($dir == DATA_DIR && $str === '') {
+       if ($dir == DATA_DIR && $is_delete) {
                // Page deletion
                if (! $file_exists) return; // Ignore null posting for DATA_DIR
 
@@ -265,7 +306,7 @@ function file_write($dir, $page, $str, $notimestamp = FALSE)
                if ($notify_diff_only) $str = preg_replace('/^[^-+].*\n/m', '', $str);
                $footer['ACTION'] = 'Page update';
                $footer['PAGE']   = & $page;
-               $footer['URI']    = get_script_uri() . '?' . rawurlencode($page);
+               $footer['URI']    = get_script_uri() . '?' . pagename_urlencode($page);
                $footer['USER_AGENT']  = TRUE;
                $footer['REMOTE_ADDR'] = TRUE;
                pkwk_mail_notify($notify_subject, $str, $footer) or
@@ -800,4 +841,19 @@ function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
                        htmlsc(basename($filename)));
        }
 }
-?>
+
+/**
+ * Lock-enabled file_get_contents
+ *
+ * Require: PHP5+
+ */
+function pkwk_file_get_contents($filename) {
+       if (! file_exists($filename)) {
+               return false;
+       }
+       $fp   = fopen($filename, 'rb');
+       flock($fp, LOCK_SH);
+       $file = file_get_contents($filename);
+       flock($fp, LOCK_UN);
+       return $file;
+}