<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @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/>.
+ *
+ */
namespace Friendica\Util;
-use Friendica\Core;
-
class BasePath
{
/**
- * @brief Returns the base filesystem path of the App
+ * @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;
+ }
+
+ /**
+ * 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 The default base path
- * @param array $server server arguments
- *
* @return string
*
* @throws \Exception if directory isn't usable
*/
- public static function create($basePath, $server = [])
+ 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;
}
/**
- * @brief Returns a normalized file path
+ * Returns a normalized file path
*
* This is a wrapper for the "realpath" function.
* That function cannot detect the real path when some folders aren't readable.
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;
- }
}