]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Util/FileSystem.php
Use the owner, not the author
[friendica.git] / src / Core / Logger / 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\Core\Logger\Util;
23
24 use Friendica\Core\Logger\Exception\LoggerUnusableException;
25
26 /**
27  * Util class for filesystem manipulation for Logger classes
28  */
29 class FileSystem
30 {
31         /**
32          * @var string a error message
33          */
34         private $errorMessage;
35
36         /**
37          * Creates a directory based on a file, which gets accessed
38          *
39          * @param string $file The file
40          *
41          * @return string The directory name (empty if no directory is found, like urls)
42          *
43          * @throws LoggerUnusableException
44          */
45         public function createDir(string $file): string
46         {
47                 $dirname = null;
48                 $pos = strpos($file, '://');
49
50                 if (!$pos) {
51                         $dirname = realpath(dirname($file));
52                 }
53
54                 if (substr($file, 0, 7) === 'file://') {
55                         $dirname = realpath(dirname(substr($file, 7)));
56                 }
57
58                 if (isset($dirname) && !is_dir($dirname)) {
59                         set_error_handler([$this, 'customErrorHandler']);
60                         $status = mkdir($dirname, 0777, true);
61                         restore_error_handler();
62
63                         if (!$status && !is_dir($dirname)) {
64                                 throw new LoggerUnusableException(sprintf('Directory "%s" cannot get created: ' . $this->errorMessage, $dirname));
65                         }
66
67                         return $dirname;
68                 } elseif (isset($dirname) && is_dir($dirname)) {
69                         return $dirname;
70                 } else {
71                         return '';
72                 }
73         }
74
75         /**
76          * Creates a stream based on a URL (could be a local file or a real URL)
77          *
78          * @param string $url The file/url
79          *
80          * @return resource the open stream resource
81          *
82          * @throws LoggerUnusableException
83          */
84         public function createStream(string $url)
85         {
86                 $directory = $this->createDir($url);
87                 set_error_handler([$this, 'customErrorHandler']);
88                 if (!empty($directory)) {
89                         $url = $directory . DIRECTORY_SEPARATOR . pathinfo($url, PATHINFO_BASENAME);
90                 }
91
92                 $stream = fopen($url, 'ab');
93                 restore_error_handler();
94
95                 if (!is_resource($stream)) {
96                         throw new LoggerUnusableException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $url));
97                 }
98
99                 return $stream;
100         }
101
102         private function customErrorHandler($code, $msg)
103         {
104                 $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
105         }
106 }