Przeglądaj źródła

refactoring as recommended by Scrutinizer

El RIDO 8 lat temu
rodzic
commit
a5d5f6066a

+ 4 - 25
lib/Configuration.php

@@ -12,6 +12,7 @@
 
 namespace PrivateBin;
 
+use PrivateBin\Persistence\DataStore;
 use Exception;
 use PDO;
 
@@ -22,13 +23,6 @@ use PDO;
  */
 class Configuration
 {
-    /**
-     * First line in INI file, to hide contents
-     *
-     * @const string
-     */
-    const PROTECTION_LINE = ';<?php http_response_code(403); /*';
-
     /**
      * parsed configuration
      *
@@ -112,27 +106,12 @@ class Configuration
 
         // rename INI files to avoid configuration leakage
         if (is_readable($configIni)) {
-            $context = stream_context_create();
-            // don't overwrite already converted file
-            if (!is_file($configFile)) {
-                $iniHandle = fopen($configIni, 'r', false, $context);
-                file_put_contents($configFile, self::PROTECTION_LINE . PHP_EOL);
-                file_put_contents($configFile, $iniHandle, FILE_APPEND);
-                fclose($iniHandle);
-            }
-            unlink($configIni);
+            DataStore::prependRename($configIni, $configFile, ';');
 
             // cleanup sample, too
-            $configSample    = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php';
-            $configIniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
+            $configIniSample = $configIni . '.sample';
             if (is_readable($configIniSample)) {
-                if (!is_readable($configSample)) {
-                    $iniSampleHandle = fopen($configIniSample, 'r', false, $context);
-                    file_put_contents($configSample, self::PROTECTION_LINE . PHP_EOL);
-                    file_put_contents($configSample, $iniSampleHandle, FILE_APPEND);
-                    fclose($iniSampleHandle);
-                }
-                unlink($configIniSample);
+                DataStore::prependRename($configIniSample, PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php', ';');
             }
         }
 

+ 3 - 18
lib/Data/Filesystem.php

@@ -135,31 +135,16 @@ class Filesystem extends AbstractData
         $pastePath = $basePath . '.php';
         // convert to PHP protected files if needed
         if (is_readable($basePath)) {
-            $context = stream_context_create();
-            // don't overwrite already converted file
-            if (!is_file($pastePath)) {
-                $handle = fopen($basePath, 'r', false, $context);
-                file_put_contents($pastePath, DataStore::PROTECTION_LINE . PHP_EOL);
-                file_put_contents($pastePath, $handle, FILE_APPEND);
-                fclose($handle);
-            }
-            unlink($basePath);
+            DataStore::prependRename($basePath, $pastePath);
 
             // convert comments, too
-            $discdir  = self::_dataid2discussionpath($pasteid);
+            $discdir = self::_dataid2discussionpath($pasteid);
             if (is_dir($discdir)) {
                 $dir = dir($discdir);
                 while (false !== ($filename = $dir->read())) {
                     if (substr($filename, -4) !== '.php' && strlen($filename) >= 16) {
                         $commentFilename = $discdir . $filename . '.php';
-                        // don't overwrite already converted file
-                        if (!is_file($commentFilename)) {
-                            $handle = fopen($discdir . $filename, 'r', false, $context);
-                            file_put_contents($commentFilename, DataStore::PROTECTION_LINE . PHP_EOL);
-                            file_put_contents($commentFilename, $handle, FILE_APPEND);
-                            fclose($handle);
-                        }
-                        unlink($discdir . $filename);
+                        DataStore::prependRename($discdir . $filename, $commentFilename);
                     }
                 }
                 $dir->close();

+ 24 - 2
lib/Persistence/DataStore.php

@@ -23,7 +23,7 @@ use PrivateBin\Json;
 class DataStore extends AbstractPersistence
 {
     /**
-     * First line in JSON files, to hide contents
+     * first line in file, to protect its contents
      *
      * @const string
      */
@@ -58,10 +58,32 @@ class DataStore extends AbstractPersistence
      * @access public
      * @static
      * @param  string $filename
-     * @return array  $data
+     * @return stdClass|false  $data
      */
     public static function get($filename)
     {
         return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL)));
     }
+
+    /**
+     * rename a file, prepending the protection line at the beginning
+     *
+     * @access public
+     * @static
+     * @param  string $srcFile
+     * @param  string $destFile
+     * @param  string $prefix (optional)
+     * @return void
+     */
+    public static function prependRename($srcFile, $destFile, $prefix = '')
+    {
+        // don't overwrite already converted file
+        if (!is_readable($destFile)) {
+            $handle = fopen($srcFile, 'r', false, stream_context_create());
+            file_put_contents($destFile, $prefix . DataStore::PROTECTION_LINE . PHP_EOL);
+            file_put_contents($destFile, $handle, FILE_APPEND);
+            fclose($handle);
+        }
+        unlink($srcFile);
+    }
 }

+ 0 - 1
tst/PrivateBinTest.php

@@ -39,7 +39,6 @@ class PrivateBinTest extends PHPUnit_Framework_TestCase
         $options['purge']['dir']         = $this->_path;
         $options['traffic']['dir']       = $this->_path;
         $options['model_options']['dir'] = $this->_path;
-        Helper::confBackup();
         Helper::createIniFile(CONF, $options);
         ServerSalt::setPath($this->_path);
     }