friendica/src/Util/BasePath.php

60 lines
1.4 KiB
PHP
Raw Normal View History

2019-02-03 22:22:04 +01:00
<?php
namespace Friendica\Util;
class BasePath
{
/**
* @brief Returns the base filesystem path of the App
*
* It first checks for the internal variable, then for DOCUMENT_ROOT and
* finally for PWD
*
2019-02-05 22:27:57 +01:00
* @param string|null $basePath The default base path
* @param array $server server arguments
2019-02-03 22:22:04 +01:00
*
* @return string
*
* @throws \Exception if directory isn't usable
*/
2019-04-08 23:12:34 +02:00
public static function create($basePath, array $server = [])
2019-02-03 22:22:04 +01:00
{
2019-04-14 16:17:34 +02:00
if ((!$basePath || !is_dir($basePath)) && !empty($server['DOCUMENT_ROOT'])) {
2019-02-05 22:27:57 +01:00
$basePath = $server['DOCUMENT_ROOT'];
2019-02-03 22:22:04 +01:00
}
2019-04-14 16:17:34 +02:00
if ((!$basePath || !is_dir($basePath)) && !empty($server['PWD'])) {
2019-02-05 22:27:57 +01:00
$basePath = $server['PWD'];
2019-02-03 22:22:04 +01:00
}
2019-04-14 16:17:34 +02:00
$basePath = self::getRealPath($basePath);
if (!is_dir($basePath)) {
throw new \Exception(sprintf('\'%s\' is not a valid basepath', $basePath));
}
return $basePath;
2019-02-03 22:22:04 +01:00
}
/**
* @brief 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.
* 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;
}
}
}