]> git.mxchange.org Git - friendica.git/blob - src/Model/Storage/Filesystem.php
de3e88be080c85864910465aa54003fe6053dd31
[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 "{$base}/{$fold1}/{$fold2}/{$file}";
47         }
48
49         public static function get($ref)
50         {
51                 $file = self::pathForRef($ref);
52                 if (!is_file($file)) {
53                         return "";
54                 }
55
56                 return file_get_contents($file);
57         }
58
59         public static function put($data, $ref = "")
60         {
61                 if ($ref === "") {
62                         $ref = Strings::getRandomHex();
63                 }
64
65                 $file = self::pathForRef($ref);
66                 $path = dirname($file);
67
68                 if (!is_dir($path)) {
69                         if (!mkdir($path, 0770, true)) {
70                                 Logger::log("Failed to create dirs {$path}");
71                                 echo L10n::t("Filesystem storage failed to create '%s'. Check you write permissions.", $path);
72                                 killme();
73                         }
74                 }
75
76                 $r = file_put_contents($file, $data);
77                 if ($r === FALSE) {
78                         Logger::log("Failed to write data to {$file}");
79                         echo L10n::t("Filesystem storage failed to save data to '%s'. Check your write permissions", $file);
80                         killme();
81                 }
82                 return $ref;
83         }
84
85         public static function delete($ref)
86         {
87                 $file = self::pathForRef($ref);
88                 // return true if file doesn't exists. we want to delete it: success with zero work!
89                 if (!is_file($file)) { 
90                         return true;
91                 }
92                 return unlink($file);
93         }
94
95 }