]> git.mxchange.org Git - friendica.git/blob - src/Util/FileSystem.php
Merge pull request #7726 from tobiasd/20191010-uexport
[friendica.git] / src / Util / FileSystem.php
1 <?php
2
3 namespace Friendica\Util;
4
5 /**
6  * Util class for filesystem manipulation
7  */
8 final class FileSystem
9 {
10         /**
11          * @var string a error message
12          */
13         private $errorMessage;
14
15         /**
16          * Creates a directory based on a file, which gets accessed
17          *
18          * @param string $file The file
19          *
20          * @return string The directory name (empty if no directory is found, like urls)
21          */
22         public function createDir(string $file)
23         {
24                 $dirname = null;
25                 $pos = strpos($file, '://');
26
27                 if (!$pos) {
28                         $dirname = realpath(dirname($file));
29                 }
30
31                 if (substr($file, 0, 7) === 'file://') {
32                         $dirname = realpath(dirname(substr($file, 7)));
33                 }
34
35                 if (isset($dirname) && !is_dir($dirname)) {
36                         set_error_handler([$this, 'customErrorHandler']);
37                         $status = mkdir($dirname, 0777, true);
38                         restore_error_handler();
39
40                         if (!$status && !is_dir($dirname)) {
41                                 throw new \UnexpectedValueException(sprintf('Directory "%s" cannot get created: ' . $this->errorMessage, $dirname));
42                         }
43
44                         return $dirname;
45                 } elseif (isset($dirname) && is_dir($dirname)) {
46                         return $dirname;
47                 } else {
48                         return '';
49                 }
50         }
51
52         /**
53          * Creates a stream based on a URL (could be a local file or a real URL)
54          *
55          * @param string $url The file/url
56          *
57          * @return false|resource the open stream ressource
58          */
59         public function createStream(string $url)
60         {
61                 $directory = $this->createDir($url);
62                 set_error_handler([$this, 'customErrorHandler']);
63                 if (!empty($directory)) {
64                         $url = $directory . DIRECTORY_SEPARATOR . pathinfo($url, PATHINFO_BASENAME);
65                 }
66
67                 $stream = fopen($url, 'ab');
68                 restore_error_handler();
69
70                 if (!is_resource($stream)) {
71                         throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $url));
72                 }
73
74                 return $stream;
75         }
76
77         private function customErrorHandler($code, $msg)
78         {
79                 $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
80         }
81 }