Selaa lähdekoodia

Forwardporting safer abook/prefs saving from stable

Thijs Kinkhorst 23 vuotta sitten
vanhempi
commit
b0f4b511cd
2 muutettua tiedostoa jossa 13 lisäystä ja 7 poistoa
  1. 5 3
      functions/abook_local_file.php
  2. 8 4
      functions/file_prefs.php

+ 5 - 3
functions/abook_local_file.php

@@ -135,9 +135,9 @@ class abook_local_file extends addressbook_backend {
      * NOTE! Previous locks are broken by this function */
     function overwrite(&$rows) {
         $this->unlock();
-        $newfh = @fopen($this->filename, 'w');
+        $newfh = @fopen($this->filename .'.tmp', 'w');
         if(!$newfh) {
-            return $this->set_error("$file: " . _("Open failed"));
+            return $this->set_error($this->filename .'.tmp: '. _("Open failed"));
         }
         
         for($i = 0 ; $i < sizeof($rows) ; $i++) {
@@ -150,6 +150,8 @@ class abook_local_file extends addressbook_backend {
         }       
 
         fclose($newfh);
+        copy( $this->filename .'.tmp' , $this->filename);
+        unlink( $this->filename .'.tmp');
         $this->unlock();
         $this->open(true);
         return true;
@@ -376,4 +378,4 @@ class abook_local_file extends addressbook_backend {
     }
 
 } /* End of class abook_local_file */
-?>
+?>

+ 8 - 4
functions/file_prefs.php

@@ -106,10 +106,10 @@ function savePrefValues($data_dir, $username) {
     $filename = getHashedFile($username, $data_dir, "$username.pref");
 
     /* Open the file for writing, or else display an error to the user. */
-    if(!$file = @fopen($filename, 'w'))
+    if(!$file = @fopen($filename.'.tmp', 'w'))
     {
         include_once(SM_PATH . 'functions/display_messages.php');
-        logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
+        logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
         exit;
     }
 
@@ -119,6 +119,8 @@ function savePrefValues($data_dir, $username) {
         }
     }
     fclose($file);
+    copy($filename.'.tmp', $filename);
+    unlink($filename.'.tmp');
     chmod($filename, 0600);
 }
 
@@ -208,14 +210,16 @@ function checkForPrefs($data_dir, $username, $filename = '') {
 function setSig($data_dir, $username, $number, $value) {
     $filename = getHashedFile($username, $data_dir, "$username.si$number");
     /* Open the file for writing, or else display an error to the user. */
-    if(!$file = @fopen($filename, 'w'))
+    if(!$file = @fopen($filename.'.tmp', 'w'))
     {
         include_once(SM_PATH . '/functions/display_messages.php' );
-        logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
+        logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
         exit;
     }
     fwrite($file, $value);
     fclose($file);
+    copy($filename.'.tmp',$filename);
+    unlink($filename.'.tmp');
 }
 
 /**