X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FBasePath.php;h=06fa5246797028fb1bbee1dc75a5b7184db7726c;hb=3940e804e3ee4ac921e109f62a73fac2becaa611;hp=56b0fa1fe9f78685c3e28a76bc0dc672c6195618;hpb=4af0119b7310e9731e60b11a14fd917580b95715;p=friendica.git diff --git a/src/Util/BasePath.php b/src/Util/BasePath.php index 56b0fa1fe9..06fa524679 100644 --- a/src/Util/BasePath.php +++ b/src/Util/BasePath.php @@ -2,33 +2,57 @@ namespace Friendica\Util; -use Friendica\Core; - class BasePath { + /** + * @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; + } + /** * @brief Returns the base filesystem path of the App * * It first checks for the internal variable, then for DOCUMENT_ROOT and * finally for PWD * - * @param string|null $basepath - * * @return string * * @throws \Exception if directory isn't usable */ - public static function create($basepath) + public function getPath() { - if (!$basepath && !empty($_SERVER['DOCUMENT_ROOT'])) { - $basepath = $_SERVER['DOCUMENT_ROOT']; + $baseDir = $this->baseDir; + $server = $this->server; + + if ((!$baseDir || !is_dir($baseDir)) && !empty($server['DOCUMENT_ROOT'])) { + $baseDir = $server['DOCUMENT_ROOT']; } - if (!$basepath && !empty($_SERVER['PWD'])) { - $basepath = $_SERVER['PWD']; + if ((!$baseDir || !is_dir($baseDir)) && !empty($server['PWD'])) { + $baseDir = $server['PWD']; } - return self::getRealPath($basepath); + $baseDir = self::getRealPath($baseDir); + + if (!is_dir($baseDir)) { + throw new \Exception(sprintf('\'%s\' is not a valid basepath', $baseDir)); + } + + return $baseDir; } /** @@ -51,43 +75,4 @@ class BasePath return $path; } } - - - /** - * @brief Checks if a given directory is usable for the system - * - * @param $directory - * @param bool $check_writable - * - * @return boolean the directory is usable - */ - public static function isDirectoryUsable($directory, $check_writable = true) - { - if ($directory == '') { - Core\Logger::log('Directory is empty. This shouldn\'t happen.', Core\Logger::DEBUG); - return false; - } - - if (!file_exists($directory)) { - Core\Logger::log('Path "' . $directory . '" does not exist for user ' . Core\System::getUser(), Core\Logger::DEBUG); - return false; - } - - if (is_file($directory)) { - Core\Logger::log('Path "' . $directory . '" is a file for user ' . Core\System::getUser(), Core\Logger::DEBUG); - return false; - } - - if (!is_dir($directory)) { - Core\Logger::log('Path "' . $directory . '" is not a directory for user ' . Core\System::getUser(), Core\Logger::DEBUG); - return false; - } - - if ($check_writable && !is_writable($directory)) { - Core\Logger::log('Path "' . $directory . '" is not writable for user ' . Core\System::getUser(), Core\Logger::DEBUG); - return false; - } - - return true; - } }