]> git.mxchange.org Git - friendica.git/blob - src/Util/FileSystem.php
Continued:
[friendica.git] / src / Util / FileSystem.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 /**
25  * Util class for filesystem manipulation
26  */
27 class FileSystem
28 {
29         /**
30          * @var string a error message
31          */
32         private $errorMessage;
33
34         /**
35          * Creates a directory based on a file, which gets accessed
36          *
37          * @param string $file The file
38          *
39          * @return string The directory name (empty if no directory is found, like urls)
40          */
41         public function createDir(string $file)
42         {
43                 $dirname = null;
44                 $pos = strpos($file, '://');
45
46                 if (!$pos) {
47                         $dirname = realpath(dirname($file));
48                 }
49
50                 if (substr($file, 0, 7) === 'file://') {
51                         $dirname = realpath(dirname(substr($file, 7)));
52                 }
53
54                 if (isset($dirname) && !is_dir($dirname)) {
55                         set_error_handler([$this, 'customErrorHandler']);
56                         $status = mkdir($dirname, 0777, true);
57                         restore_error_handler();
58
59                         if (!$status && !is_dir($dirname)) {
60                                 throw new \UnexpectedValueException(sprintf('Directory "%s" cannot get created: ' . $this->errorMessage, $dirname));
61                         }
62
63                         return $dirname;
64                 } elseif (isset($dirname) && is_dir($dirname)) {
65                         return $dirname;
66                 } else {
67                         return '';
68                 }
69         }
70
71         /**
72          * Creates a stream based on a URL (could be a local file or a real URL)
73          *
74          * @param string $url The file/url
75          *
76          * @return resource the open stream resource
77          *
78          * @throws \UnexpectedValueException
79          */
80         public function createStream(string $url)
81         {
82                 $directory = $this->createDir($url);
83                 set_error_handler([$this, 'customErrorHandler']);
84                 if (!empty($directory)) {
85                         $url = $directory . DIRECTORY_SEPARATOR . pathinfo($url, PATHINFO_BASENAME);
86                 }
87
88                 $stream = fopen($url, 'ab');
89                 restore_error_handler();
90
91                 if (!is_resource($stream)) {
92                         throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $url));
93                 }
94
95                 return $stream;
96         }
97
98         private function customErrorHandler($code, $msg)
99         {
100                 $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
101         }
102 }