OSDN Git Service

BugTrack/2401 Locked file_get_contents/file_put_contents on bugtrack
[pukiwiki/pukiwiki.git] / lib / file.php
index 1d70c23..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,18 +84,26 @@ function page_write($page, $postdata, $notimestamp = FALSE)
        if (PKWK_READONLY) return; // Do nothing
 
        $postdata = make_str_rules($postdata);
-       $postdata = add_author_info(remove_author_info($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);
 }
@@ -161,27 +169,15 @@ function make_str_rules($source)
 
 function add_author_info($wikitext)
 {
-       global $auth_user, $auth_user_fullname, $auth_type, $ldap_user_account;
+       global $auth_user, $auth_user_fullname;
        $author = preg_replace('/"/', '', $auth_user);
-       $displayname = preg_replace('/"/', '', $auth_user_fullname);
-       $user_prefix = '';
-       switch ($auth_type) {
-               case AUTH_TYPE_BASIC:
-                       $user_prefix = AUTH_PROVIDER_USER_PREFIX_DEFAULT;
-                       break;
-               case AUTH_TYPE_EXTERNAL:
-               case AUTH_TYPE_EXTERNAL_REMOTE_USER:
-               case AUTH_TYPE_EXTERNAL_X_FORWARDED_USER:
-                       $user_prefix = AUTH_PROVIDER_USER_PREFIX_EXTERNAL;
-                       break;
-               case AUTH_TYPE_FORM:
-                       if ($ldap_user_account) {
-                               $user_prefix = AUTH_PROVIDER_USER_PREFIX_LDAP;
-                       } else {
-                               $user_prefix = AUTH_PROVIDER_USER_PREFIX_DEFAULT;
-                       }
-                       break;
+       $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 : ''),
@@ -236,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;
@@ -251,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
 
@@ -846,3 +842,18 @@ function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
        }
 }
 
+/**
+ * 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;
+}