]> git.mxchange.org Git - friendica.git/blob - src/Util/BasePath.php
56b0fa1fe9f78685c3e28a76bc0dc672c6195618
[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
16          *
17          * @return string
18          *
19          * @throws \Exception if directory isn't usable
20          */
21         public static function create($basepath)
22         {
23                 if (!$basepath && !empty($_SERVER['DOCUMENT_ROOT'])) {
24                         $basepath = $_SERVER['DOCUMENT_ROOT'];
25                 }
26
27                 if (!$basepath && !empty($_SERVER['PWD'])) {
28                         $basepath = $_SERVER['PWD'];
29                 }
30
31                 return self::getRealPath($basepath);
32         }
33
34         /**
35          * @brief Returns a normalized file path
36          *
37          * This is a wrapper for the "realpath" function.
38          * That function cannot detect the real path when some folders aren't readable.
39          * Since this could happen with some hosters we need to handle this.
40          *
41          * @param string $path The path that is about to be normalized
42          * @return string normalized path - when possible
43          */
44         public static function getRealPath($path)
45         {
46                 $normalized = realpath($path);
47
48                 if (!is_bool($normalized)) {
49                         return $normalized;
50                 } else {
51                         return $path;
52                 }
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 }