]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Storage/Filesystem.php
Merge pull request #7372 from nupplaphil/task/simplify_config
[friendica.git] / src / Model / Storage / Filesystem.php
index 1cfb5effe831fcebe928cac90dd1ad8896a97b44..82e638158c57933125ee435e820246bb0e60a692 100644 (file)
@@ -24,11 +24,12 @@ use Friendica\Util\Strings;
 class Filesystem implements IStorage
 {
        // Default base folder
-       const DEFAULT_BASE_FOLDER = "storage";
+       const DEFAULT_BASE_FOLDER = 'storage';
 
        private static function getBasePath()
        {
-               return Config::get("storage", "filesystem_path", self::DEFAULT_BASE_FOLDER);
+               $path = Config::get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
+               return rtrim($path, '/');
        }
 
        /**
@@ -43,13 +44,14 @@ class Filesystem implements IStorage
                $fold2 = substr($ref, 2, 2);
                $file = substr($ref, 4);
 
-               return "{$base}/{$fold1}/{$fold2}/{$file}";
+               return implode('/', [$base, $fold1, $fold2, $file]);
        }
 
 
        /**
         * @brief Create dirctory tree to store file, with .htaccess and index.html files
-        * @param string  $file  Path and filename
+        * @param string $file Path and filename
+        * @throws StorageException
         */
        private static function createFoldersForFile($file)
        {
@@ -57,22 +59,24 @@ class Filesystem implements IStorage
 
                if (!is_dir($path)) {
                        if (!mkdir($path, 0770, true)) {
-                               Logger::log("Failed to create dirs {$path}");
-                               throw new StorageException(L10n::t("Filesystem storage failed to create '%s'. Check you write permissions.", $path));
-                               killme();
+                               Logger::log('Failed to create dirs ' . $path);
+                               throw new StorageException(L10n::t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
                        }
                }
 
                $base = self::getBasePath();
 
                while ($path !== $base) {
-                       if (!is_file($path . "/index.html")) {
-                               file_put_contents($path . "/index.html", "");
+                       if (!is_file($path . '/index.html')) {
+                               file_put_contents($path . '/index.html', '');
                        }
+                       chmod($path . '/index.html', 0660);
+                       chmod($path, 0770);
                        $path = dirname($path);
                }
-               if (!is_file($path . "/index.html")) {
-                       file_put_contents($path . "/index.html", "");
+               if (!is_file($path . '/index.html')) {
+                       file_put_contents($path . '/index.html', '');
+                       chmod($path . '/index.html', 0660);
                }
        }
 
@@ -80,15 +84,15 @@ class Filesystem implements IStorage
        {
                $file = self::pathForRef($ref);
                if (!is_file($file)) {
-                       return "";
+                       return '';
                }
 
                return file_get_contents($file);
        }
 
-       public static function put($data, $ref = "")
+       public static function put($data, $ref = '')
        {
-               if ($ref === "") {
+               if ($ref === '') {
                        $ref = Strings::getRandomHex();
                }
                $file = self::pathForRef($ref);
@@ -97,10 +101,10 @@ class Filesystem implements IStorage
 
                $r = file_put_contents($file, $data);
                if ($r === FALSE) {
-                       Logger::log("Failed to write data to {$file}");
-                       throw new StorageException(L10n::t("Filesystem storage failed to save data to '%s'. Check your write permissions", $file));
-                       killme();
+                       Logger::log('Failed to write data to ' . $file);
+                       throw new StorageException(L10n::t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
                }
+               chmod($file, 0660);
                return $ref;
        }
 
@@ -114,4 +118,28 @@ class Filesystem implements IStorage
                return unlink($file);
        }
 
+       public static function getOptions()
+       {
+               return [
+                       'storagepath' => [
+                               'input',
+                               L10n::t('Storage base path'),
+                               self::getBasePath(),
+                               L10n::t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
+                       ]
+               ];
+       }
+       
+       public static function saveOptions($data)
+       {
+               $storagepath = defaults($data, 'storagepath', '');
+               if ($storagepath === '' || !is_dir($storagepath)) {
+                       return [
+                               'storagepath' => L10n::t('Enter a valid existing folder')
+                       ];
+               };
+               Config::set('storage', 'filesystem_path', $storagepath);
+               return [];
+       }
+
 }