2019-02-03 22:22:04 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-03 22:22:04 +01:00
|
|
|
|
|
|
|
namespace Friendica\Util;
|
|
|
|
|
|
|
|
class BasePath
|
|
|
|
{
|
2019-07-21 01:22:10 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $baseDir;
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|null $baseDir The default base path
|
|
|
|
* @param array $server server arguments
|
|
|
|
*/
|
|
|
|
public function __construct(string $baseDir, array $server = [])
|
|
|
|
{
|
|
|
|
$this->baseDir = $baseDir;
|
|
|
|
$this->server = $server;
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:22:04 +01:00
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Returns the base filesystem path of the App
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* It first checks for the internal variable, then for DOCUMENT_ROOT and
|
|
|
|
* finally for PWD
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Exception if directory isn't usable
|
|
|
|
*/
|
2019-07-21 01:22:10 +02:00
|
|
|
public function getPath()
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-07-21 01:22:10 +02:00
|
|
|
$baseDir = $this->baseDir;
|
|
|
|
$server = $this->server;
|
|
|
|
|
|
|
|
if ((!$baseDir || !is_dir($baseDir)) && !empty($server['DOCUMENT_ROOT'])) {
|
|
|
|
$baseDir = $server['DOCUMENT_ROOT'];
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
if ((!$baseDir || !is_dir($baseDir)) && !empty($server['PWD'])) {
|
|
|
|
$baseDir = $server['PWD'];
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
$baseDir = self::getRealPath($baseDir);
|
2019-04-14 16:17:34 +02:00
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
if (!is_dir($baseDir)) {
|
|
|
|
throw new \Exception(sprintf('\'%s\' is not a valid basepath', $baseDir));
|
2019-04-14 16:17:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
return $baseDir;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Returns a normalized file path
|
2019-02-03 22:22:04 +01:00
|
|
|
*
|
|
|
|
* This is a wrapper for the "realpath" function.
|
|
|
|
* That function cannot detect the real path when some folders aren't readable.
|
|
|
|
* Since this could happen with some hosters we need to handle this.
|
|
|
|
*
|
|
|
|
* @param string $path The path that is about to be normalized
|
|
|
|
* @return string normalized path - when possible
|
|
|
|
*/
|
|
|
|
public static function getRealPath($path)
|
|
|
|
{
|
|
|
|
$normalized = realpath($path);
|
|
|
|
|
|
|
|
if (!is_bool($normalized)) {
|
|
|
|
return $normalized;
|
|
|
|
} else {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|