]> git.mxchange.org Git - friendica.git/blob - src/Util/BasePath.php
Revert PR 7158 since it breaks umlauts
[friendica.git] / src / Util / BasePath.php
1 <?php
2
3 namespace Friendica\Util;
4
5 class BasePath
6 {
7         /**
8          * @brief Returns the base filesystem path of the App
9          *
10          * It first checks for the internal variable, then for DOCUMENT_ROOT and
11          * finally for PWD
12          *
13          * @param string|null $basePath The default base path
14          * @param array       $server   server arguments
15          *
16          * @return string
17          *
18          * @throws \Exception if directory isn't usable
19          */
20         public static function create($basePath, array $server = [])
21         {
22                 if ((!$basePath || !is_dir($basePath)) && !empty($server['DOCUMENT_ROOT'])) {
23                         $basePath = $server['DOCUMENT_ROOT'];
24                 }
25
26                 if ((!$basePath || !is_dir($basePath)) && !empty($server['PWD'])) {
27                         $basePath = $server['PWD'];
28                 }
29
30                 $basePath = self::getRealPath($basePath);
31
32                 if (!is_dir($basePath)) {
33                         throw new \Exception(sprintf('\'%s\' is not a valid basepath', $basePath));
34                 }
35
36                 return $basePath;
37         }
38
39         /**
40          * @brief Returns a normalized file path
41          *
42          * This is a wrapper for the "realpath" function.
43          * That function cannot detect the real path when some folders aren't readable.
44          * Since this could happen with some hosters we need to handle this.
45          *
46          * @param string $path The path that is about to be normalized
47          * @return string normalized path - when possible
48          */
49         public static function getRealPath($path)
50         {
51                 $normalized = realpath($path);
52
53                 if (!is_bool($normalized)) {
54                         return $normalized;
55                 } else {
56                         return $path;
57                 }
58         }
59 }