3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util;
36 * @param string|null $baseDir The default base path
37 * @param array $server server arguments
39 public function __construct(string $baseDir, array $server = [])
41 $this->baseDir = $baseDir;
42 $this->server = $server;
46 * Returns the base filesystem path of the App
48 * It first checks for the internal variable, then for DOCUMENT_ROOT and
53 * @throws \Exception if directory isn't usable
55 public function getPath()
57 $baseDir = $this->baseDir;
58 $server = $this->server;
60 if ((!$baseDir || !is_dir($baseDir)) && !empty($server['DOCUMENT_ROOT'])) {
61 $baseDir = $server['DOCUMENT_ROOT'];
64 if ((!$baseDir || !is_dir($baseDir)) && !empty($server['PWD'])) {
65 $baseDir = $server['PWD'];
68 $baseDir = self::getRealPath($baseDir);
70 if (!is_dir($baseDir)) {
71 throw new \Exception(sprintf('\'%s\' is not a valid basepath', $baseDir));
78 * Returns a normalized file path
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.
84 * @param string $path The path that is about to be normalized
85 * @return string normalized path - when possible
87 public static function getRealPath($path)
89 $normalized = realpath($path);
91 if (!is_bool($normalized)) {