From a5d5f6066a531f631a4a9d00396abe5f403fc47e Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 8 Oct 2017 19:16:09 +0200 Subject: [PATCH] refactoring as recommended by Scrutinizer --- lib/Configuration.php | 29 ++++------------------------- lib/Data/Filesystem.php | 21 +++------------------ lib/Persistence/DataStore.php | 26 ++++++++++++++++++++++++-- tst/PrivateBinTest.php | 1 - 4 files changed, 31 insertions(+), 46 deletions(-) diff --git a/lib/Configuration.php b/lib/Configuration.php index 5b8813d6..571ef562 100644 --- a/lib/Configuration.php +++ b/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 = ';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(); diff --git a/lib/Persistence/DataStore.php b/lib/Persistence/DataStore.php index c5f14ff1..d14453aa 100644 --- a/lib/Persistence/DataStore.php +++ b/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); + } } diff --git a/tst/PrivateBinTest.php b/tst/PrivateBinTest.php index 5c5e1453..72ee679a 100644 --- a/tst/PrivateBinTest.php +++ b/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); }