2012-04-13 23:45:28 +02:00
|
|
|
<?php
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
class Application_Common_OsPath
|
|
|
|
{
|
2012-04-13 23:45:28 +02:00
|
|
|
// this function is from http://stackoverflow.com/questions/2670299/is-there-a-php-equivalent-function-to-the-python-os-path-normpath
|
|
|
|
public static function normpath($path)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if (empty($path)) {
|
2012-04-13 23:45:28 +02:00
|
|
|
return '.';
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($path, '/') === 0) {
|
2012-04-13 23:45:28 +02:00
|
|
|
$initial_slashes = true;
|
2021-10-11 16:10:47 +02:00
|
|
|
} else {
|
2012-04-13 23:45:28 +02:00
|
|
|
$initial_slashes = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
2012-04-13 23:45:28 +02:00
|
|
|
if (
|
2021-10-11 16:10:47 +02:00
|
|
|
($initial_slashes)
|
|
|
|
&& (strpos($path, '//') === 0)
|
|
|
|
&& (strpos($path, '///') === false)
|
|
|
|
) {
|
2012-04-13 23:45:28 +02:00
|
|
|
$initial_slashes = 2;
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
2012-04-13 23:45:28 +02:00
|
|
|
$initial_slashes = (int) $initial_slashes;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-04-13 23:45:28 +02:00
|
|
|
$comps = explode('/', $path);
|
2021-10-11 16:10:47 +02:00
|
|
|
$new_comps = [];
|
|
|
|
foreach ($comps as $comp) {
|
|
|
|
if (in_array($comp, ['', '.'])) {
|
2012-04-13 23:45:28 +02:00
|
|
|
continue;
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
2012-04-13 23:45:28 +02:00
|
|
|
if (
|
2021-10-11 16:10:47 +02:00
|
|
|
($comp != '..')
|
|
|
|
|| (!$initial_slashes && !$new_comps)
|
|
|
|
|| ($new_comps && (end($new_comps) == '..'))
|
|
|
|
) {
|
2012-04-13 23:45:28 +02:00
|
|
|
array_push($new_comps, $comp);
|
2021-10-11 16:10:47 +02:00
|
|
|
} elseif ($new_comps) {
|
2012-04-13 23:45:28 +02:00
|
|
|
array_pop($new_comps);
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
2012-04-13 23:45:28 +02:00
|
|
|
}
|
|
|
|
$comps = $new_comps;
|
|
|
|
$path = implode('/', $comps);
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($initial_slashes) {
|
2012-04-13 23:45:28 +02:00
|
|
|
$path = str_repeat('/', $initial_slashes) . $path;
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
|
|
|
if ($path) {
|
2012-04-13 23:45:28 +02:00
|
|
|
return $path;
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return '.';
|
2012-04-13 23:45:28 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-04-24 22:15:22 +02:00
|
|
|
/* Similar to the os.path.join python method
|
|
|
|
* http://stackoverflow.com/a/1782990/276949 */
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function join()
|
|
|
|
{
|
2012-04-24 22:15:22 +02:00
|
|
|
$args = func_get_args();
|
2021-10-11 16:10:47 +02:00
|
|
|
$paths = [];
|
2012-04-24 22:15:22 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
foreach ($args as $arg) {
|
|
|
|
$paths = array_merge($paths, (array) $arg);
|
2012-04-24 22:15:22 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
foreach ($paths as &$path) {
|
2012-04-24 22:15:22 +02:00
|
|
|
$path = trim($path, DIRECTORY_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($args[0], 0, 1) == DIRECTORY_SEPARATOR) {
|
|
|
|
$paths[0] = DIRECTORY_SEPARATOR . $paths[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(DIRECTORY_SEPARATOR, $paths);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
public static function getBaseDir()
|
|
|
|
{
|
2014-08-14 23:22:01 +02:00
|
|
|
$CC_CONFIG = Config::getConfig();
|
|
|
|
$baseUrl = $CC_CONFIG['baseDir'];
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if ($baseUrl[0] != '/') {
|
|
|
|
$baseUrl = '/' . $baseUrl;
|
2013-01-14 22:00:38 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($baseUrl[strlen($baseUrl) - 1] != '/') {
|
|
|
|
$baseUrl = $baseUrl . '/';
|
2013-01-14 22:00:38 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-10-19 17:09:34 +02:00
|
|
|
return $baseUrl;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
public static function formatDirectoryWithDirectorySeparators($dir)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($dir[0] != '/') {
|
|
|
|
$dir = '/' . $dir;
|
2014-10-24 21:11:27 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if ($dir[strlen($dir) - 1] != '/') {
|
|
|
|
$dir = $dir . '/';
|
2014-10-24 21:11:27 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $dir;
|
|
|
|
}
|
2012-04-13 23:45:28 +02:00
|
|
|
}
|