]> git.mxchange.org Git - friendica.git/blob - src/Util/BasePath.php
Fixed max value check, improved request value fetching
[friendica.git] / src / Util / BasePath.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Util;
23
24 class BasePath
25 {
26         /**
27          * @var string
28          */
29         private $baseDir;
30         /**
31          * @var array
32          */
33         private $server;
34
35         /**
36          * @param string|null $baseDir The default base path
37          * @param array       $server  server arguments
38          */
39         public function __construct(string $baseDir, array $server = [])
40         {
41                 $this->baseDir = $baseDir;
42                 $this->server = $server;
43         }
44
45         /**
46          * Returns the base filesystem path of the App
47          *
48          * It first checks for the internal variable, then for DOCUMENT_ROOT and
49          * finally for PWD
50          *
51          * @return string
52          *
53          * @throws \Exception if directory isn't usable
54          */
55         public function getPath()
56         {
57                 $baseDir = $this->baseDir;
58                 $server = $this->server;
59
60                 if ((!$baseDir || !is_dir($baseDir)) && !empty($server['DOCUMENT_ROOT'])) {
61                         $baseDir = $server['DOCUMENT_ROOT'];
62                 }
63
64                 if ((!$baseDir || !is_dir($baseDir)) && !empty($server['PWD'])) {
65                         $baseDir = $server['PWD'];
66                 }
67
68                 $baseDir = self::getRealPath($baseDir);
69
70                 if (!is_dir($baseDir)) {
71                         throw new \Exception(sprintf('\'%s\' is not a valid basepath', $baseDir));
72                 }
73
74                 return $baseDir;
75         }
76
77         /**
78          * Returns a normalized file path
79          *
80          * This is a wrapper for the "realpath" function.
81          * That function cannot detect the real path when some folders aren't readable.
82          * Since this could happen with some hosters we need to handle this.
83          *
84          * @param string $path The path that is about to be normalized
85          * @return string normalized path - when possible
86          */
87         public static function getRealPath($path)
88         {
89                 $normalized = realpath($path);
90
91                 if (!is_bool($normalized)) {
92                         return $normalized;
93                 } else {
94                         return $path;
95                 }
96         }
97 }