]> git.mxchange.org Git - friendica.git/blob - src/Model/Storage/Filesystem.php
Merge pull request #6515 from annando/connector-posts
[friendica.git] / src / Model / Storage / Filesystem.php
1 <?php
2 /**
3  * @file src/Model/Storage/Filesystem.php
4  * @brief Storage backend system
5  */
6
7 namespace Friendica\Model\Storage;
8
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Util\Strings;
13
14 /**
15  * @brief Filesystem based storage backend
16  *
17  * This class manage data on filesystem.
18  * Base folder for storage is set in storage.filesystem_path.
19  * Best would be for storage folder to be outside webserver folder, we are using a
20  * folder relative to code tree root as default to ease things for users in shared hostings.
21  * Each new resource gets a value as reference and is saved in a
22  * folder tree stucture created from that value.
23  */
24 class Filesystem implements IStorage
25 {
26         // Default base folder
27         const DEFAULT_BASE_FOLDER = 'storage';
28
29         private static function getBasePath()
30         {
31                 return Config::get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
32         }
33
34         /**
35          * @brief Split data ref and return file path
36          * @param string  $ref  Data reference
37          * @return string
38          */
39         private static function pathForRef($ref)
40         {
41                 $base = self::getBasePath();
42                 $fold1 = substr($ref, 0, 2);
43                 $fold2 = substr($ref, 2, 2);
44                 $file = substr($ref, 4);
45
46                 return implode('/', [$base, $fold1, $fold2, $file]);
47         }
48
49
50         /**
51          * @brief Create dirctory tree to store file, with .htaccess and index.html files
52          * @param string $file Path and filename
53          * @throws StorageException
54          */
55         private static function createFoldersForFile($file)
56         {
57                 $path = dirname($file);
58
59                 if (!is_dir($path)) {
60                         if (!mkdir($path, 0770, true)) {
61                                 Logger::log('Failed to create dirs ' . $path);
62                                 throw new StorageException(L10n::t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
63                         }
64                 }
65
66                 $base = self::getBasePath();
67
68                 while ($path !== $base) {
69                         if (!is_file($path . '/index.html')) {
70                                 file_put_contents($path . '/index.html', '');
71                         }
72                         $path = dirname($path);
73                 }
74                 if (!is_file($path . '/index.html')) {
75                         file_put_contents($path . '/index.html', '');
76                 }
77         }
78
79         public static function get($ref)
80         {
81                 $file = self::pathForRef($ref);
82                 if (!is_file($file)) {
83                         return '';
84                 }
85
86                 return file_get_contents($file);
87         }
88
89         public static function put($data, $ref = '')
90         {
91                 if ($ref === '') {
92                         $ref = Strings::getRandomHex();
93                 }
94                 $file = self::pathForRef($ref);
95
96                 self::createFoldersForFile($file);
97
98                 $r = file_put_contents($file, $data);
99                 if ($r === FALSE) {
100                         Logger::log('Failed to write data to ' . $file);
101                         throw new StorageException(L10n::t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
102                 }
103                 return $ref;
104         }
105
106         public static function delete($ref)
107         {
108                 $file = self::pathForRef($ref);
109                 // return true if file doesn't exists. we want to delete it: success with zero work!
110                 if (!is_file($file)) {
111                         return true;
112                 }
113                 return unlink($file);
114         }
115
116         public static function getOptions()
117         {
118                 return [
119                         'storagepath' => [
120                                 'input',
121                                 L10n::t('Storage base path'),
122                                 self::getBasePath(),
123                                 L10n::t('Folder were uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
124                         ]
125                 ];
126         }
127         
128         public static function saveOptions($data)
129         {
130                 $storagepath = defaults($data, 'storagepath', '');
131                 if ($storagepath === '' || !is_dir($storagepath)) {
132                         return [
133                                 'storagepath' => L10n::t('Enter a valid existing folder')
134                         ];
135                 };
136                 Config::set('storage', 'filesystem_path', $storagepath);
137                 return [];
138         }
139
140 }