]> git.mxchange.org Git - friendica.git/blob - src/Util/BasePath.php
fecc63a2ab60e2a6c28f21306b822d74a67db66e
[friendica.git] / src / Util / BasePath.php
1 <?php
2
3 namespace Friendica\Util;
4
5 use Friendica\Core;
6
7 class BasePath
8 {
9         /**
10          * @brief Returns the base filesystem path of the App
11          *
12          * It first checks for the internal variable, then for DOCUMENT_ROOT and
13          * finally for PWD
14          *
15          * @param string|null $basePath The default base path
16          * @param array       $server   server arguments
17          *
18          * @return string
19          *
20          * @throws \Exception if directory isn't usable
21          */
22         public static function create($basePath, $server = [])
23         {
24                 if (!$basePath && !empty($server['DOCUMENT_ROOT'])) {
25                         $basePath = $server['DOCUMENT_ROOT'];
26                 }
27
28                 if (!$basePath && !empty($server['PWD'])) {
29                         $basePath = $server['PWD'];
30                 }
31
32                 return self::getRealPath($basePath);
33         }
34
35         /**
36          * @brief Returns a normalized file path
37          *
38          * This is a wrapper for the "realpath" function.
39          * That function cannot detect the real path when some folders aren't readable.
40          * Since this could happen with some hosters we need to handle this.
41          *
42          * @param string $path The path that is about to be normalized
43          * @return string normalized path - when possible
44          */
45         public static function getRealPath($path)
46         {
47                 $normalized = realpath($path);
48
49                 if (!is_bool($normalized)) {
50                         return $normalized;
51                 } else {
52                         return $path;
53                 }
54         }
55
56         /**
57          * @brief Checks if a given directory is usable for the system
58          *
59          * @param      $directory
60          * @param bool $check_writable
61          *
62          * @return boolean the directory is usable
63          */
64         public static function isDirectoryUsable($directory, $check_writable = true)
65         {
66                 if ($directory == '') {
67                         Core\Logger::log('Directory is empty. This shouldn\'t happen.', Core\Logger::DEBUG);
68                         return false;
69                 }
70
71                 if (!file_exists($directory)) {
72                         Core\Logger::log('Path "' . $directory . '" does not exist for user ' . Core\System::getUser(), Core\Logger::DEBUG);
73                         return false;
74                 }
75
76                 if (is_file($directory)) {
77                         Core\Logger::log('Path "' . $directory . '" is a file for user ' . Core\System::getUser(), Core\Logger::DEBUG);
78                         return false;
79                 }
80
81                 if (!is_dir($directory)) {
82                         Core\Logger::log('Path "' . $directory . '" is not a directory for user ' . Core\System::getUser(), Core\Logger::DEBUG);
83                         return false;
84                 }
85
86                 if ($check_writable && !is_writable($directory)) {
87                         Core\Logger::log('Path "' . $directory . '" is not writable for user ' . Core\System::getUser(), Core\Logger::DEBUG);
88                         return false;
89                 }
90
91                 return true;
92         }
93 }