3 /** This file is part of KCFinder project
5 * @desc Path helper class
8 * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
9 * @copyright 2010, 2011 KCFinder Project
10 * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
11 * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
12 * @link http://kcfinder.sunhater.com
17 /** Get the absolute URL path of the given one. Returns FALSE if the URL
18 * is not valid or the current directory cannot be resolved (getcwd())
22 static function rel2abs_url($path) {
23 if (substr($path, 0, 1) == "/") return $path;
26 if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false))
29 $dir = self::normalize($dir);
30 $doc_root = self::normalize($_SERVER['DOCUMENT_ROOT']);
32 if (substr($dir, 0, strlen($doc_root)) != $doc_root)
35 $return = self::normalize(substr($dir, strlen($doc_root)) . "/$path");
36 if (substr($return, 0, 1) !== "/")
42 /** Resolve full filesystem path of given URL. Returns FALSE if the URL
47 static function url2fullPath($url) {
48 $url = self::normalize($url);
50 $uri = isset($_SERVER['SCRIPT_NAME'])
51 ? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF'])
52 ? $_SERVER['PHP_SELF']
55 $uri = self::normalize($uri);
57 if (substr($url, 0, 1) !== "/") {
58 if ($uri === false) return false;
59 $url = dirname($uri) . "/$url";
62 if (isset($_SERVER['DOCUMENT_ROOT'])) {
63 return self::normalize($_SERVER['DOCUMENT_ROOT'] . "/$url");
66 if ($uri === false) return false;
68 if (isset($_SERVER['SCRIPT_FILENAME'])) {
69 $scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']);
70 return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url");
73 $count = count(explode('/', $uri)) - 1;
74 for ($i = 0, $chdir = ""; $i < $count; $i++)
76 $chdir = self::normalize($chdir);
79 if (($dir === false) || !@chdir($chdir))
83 return ($rdir !== false) ? self::normalize($rdir . "/$url") : false;
87 /** Normalize the given path. On Windows servers backslash will be replaced
88 * with slash. Remobes unnecessary doble slashes and double dots. Removes
89 * last slash if it exists. Examples:
90 * path::normalize("C:\\any\\path\\") returns "C:/any/path"
91 * path::normalize("/your/path/..//home/") returns "/your/home"
95 static function normalize($path) {
96 if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
97 $path = preg_replace('/([^\\\])\\\([^\\\])/', "$1/$2", $path);
98 if (substr($path, -1) == "\\") $path = substr($path, 0, -1);
99 if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1);
102 $path = preg_replace('/\/+/s', "/", $path);
105 if (substr($path, -1) != "/")
108 $expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s';
109 while (preg_match($expr, $path))
110 $path = preg_replace($expr, "/", $path);
112 $path = substr($path, 0, -1);
113 $path = substr($path, 1);
118 * @param string $path
121 static function urlPathEncode($path) {
122 $path = self::normalize($path);
124 foreach (explode("/", $path) as $dir)
125 $encoded .= rawurlencode($dir) . "/";
126 $encoded = substr($encoded, 0, -1);
131 * @param string $path
134 static function urlPathDecode($path) {
135 $path = self::normalize($path);
137 foreach (explode("/", $path) as $dir)
138 $decoded .= rawurldecode($dir) . "/";
139 $decoded = substr($decoded, 0, -1);