|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** This file is part of KCFinder project |
|
4
|
|
|
* |
|
5
|
|
|
* @desc Path helper class |
|
6
|
|
|
* @package KCFinder |
|
7
|
|
|
* @version 3.12 |
|
8
|
|
|
* @author Pavel Tzonkov <[email protected]> |
|
9
|
|
|
* @copyright 2010-2014 KCFinder Project |
|
10
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 |
|
11
|
|
|
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3 |
|
12
|
|
|
* @link http://kcfinder.sunhater.com |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace kcfinder; |
|
16
|
|
|
|
|
17
|
|
|
class path { |
|
18
|
|
|
|
|
19
|
|
|
/** Get the absolute URL path of the given one. Returns FALSE if the URL |
|
20
|
|
|
* is not valid or the current directory cannot be resolved (getcwd()) |
|
21
|
|
|
* @param string $path |
|
22
|
|
|
* @return string */ |
|
23
|
|
|
|
|
24
|
|
|
static function rel2abs_url($path) { |
|
|
|
|
|
|
25
|
|
|
if (substr($path, 0, 1) == "/") return $path; |
|
26
|
|
|
$dir = @getcwd(); |
|
27
|
|
|
|
|
28
|
|
|
if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false)) |
|
29
|
|
|
return false; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$dir = self::normalize($dir); |
|
32
|
|
|
$doc_root = self::normalize(realpath($_SERVER['DOCUMENT_ROOT'])); |
|
33
|
|
|
|
|
34
|
|
|
if (substr($dir, 0, strlen($doc_root)) != $doc_root) |
|
35
|
|
|
return false; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$return = self::normalize(substr($dir, strlen($doc_root)) . "/$path"); |
|
38
|
|
|
if (substr($return, 0, 1) !== "/") |
|
39
|
|
|
$return = "/$return"; |
|
40
|
|
|
|
|
41
|
|
|
return $return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** Resolve full filesystem path of given URL. Returns FALSE if the URL |
|
45
|
|
|
* cannot be resolved |
|
46
|
|
|
* @param string $url |
|
47
|
|
|
* @return string */ |
|
48
|
|
|
|
|
49
|
|
|
static function url2fullPath($url) { |
|
|
|
|
|
|
50
|
|
|
$url = self::normalize($url); |
|
51
|
|
|
|
|
52
|
|
|
$uri = isset($_SERVER['SCRIPT_NAME']) |
|
53
|
|
|
? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF']) |
|
54
|
|
|
? $_SERVER['PHP_SELF'] |
|
55
|
|
|
: false); |
|
56
|
|
|
|
|
57
|
|
|
$uri = self::normalize($uri); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
if (substr($url, 0, 1) !== "/") { |
|
60
|
|
|
if ($uri === false) return false; |
|
|
|
|
|
|
61
|
|
|
$url = dirname($uri) . "/$url"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (isset($_SERVER['DOCUMENT_ROOT'])) { |
|
65
|
|
|
return self::normalize(realpath($_SERVER['DOCUMENT_ROOT']) . "/$url"); |
|
66
|
|
|
|
|
67
|
|
|
} else { |
|
68
|
|
|
if ($uri === false) return false; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
if (isset($_SERVER['SCRIPT_FILENAME'])) { |
|
71
|
|
|
$scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']); |
|
72
|
|
|
return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url"); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$count = count(explode('/', $uri)) - 1; |
|
76
|
|
|
for ($i = 0, $chdir = ""; $i < $count; $i++) |
|
77
|
|
|
$chdir .= "../"; |
|
78
|
|
|
$chdir = self::normalize($chdir); |
|
79
|
|
|
|
|
80
|
|
|
$dir = getcwd(); |
|
81
|
|
|
if (($dir === false) || !@chdir($chdir)) |
|
82
|
|
|
return false; |
|
|
|
|
|
|
83
|
|
|
$rdir = getcwd(); |
|
84
|
|
|
chdir($dir); |
|
85
|
|
|
return ($rdir !== false) ? self::normalize($rdir . "/$url") : false; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** Normalize the given path. On Windows servers backslash will be replaced |
|
90
|
|
|
* with slash. Removes unnecessary double slashes and double dots. Removes |
|
91
|
|
|
* last slash if it exists. Examples: |
|
92
|
|
|
* path::normalize("C:\\any\\path\\") returns "C:/any/path" |
|
93
|
|
|
* path::normalize("/your/path/..//home/") returns "/your/home" |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @return string */ |
|
96
|
|
|
|
|
97
|
|
|
static function normalize($path) { |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
// Backslash to slash convert |
|
100
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") { |
|
101
|
|
|
$path = preg_replace('/([^\\\])\\\+([^\\\])/s', "$1/$2", $path); |
|
102
|
|
|
if (substr($path, -1) == "\\") $path = substr($path, 0, -1); |
|
103
|
|
|
if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$path = preg_replace('/\/+/s', "/", $path); |
|
107
|
|
|
|
|
108
|
|
|
$path = "/$path"; |
|
109
|
|
|
if (substr($path, -1) != "/") |
|
110
|
|
|
$path .= "/"; |
|
111
|
|
|
|
|
112
|
|
|
$expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s'; |
|
113
|
|
|
while (preg_match($expr, $path)) |
|
114
|
|
|
$path = preg_replace($expr, "/", $path); |
|
115
|
|
|
|
|
116
|
|
|
$path = substr($path, 0, -1); |
|
117
|
|
|
$path = substr($path, 1); |
|
118
|
|
|
return $path; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** Encode URL Path |
|
122
|
|
|
* @param string $path |
|
123
|
|
|
* @return string */ |
|
124
|
|
|
|
|
125
|
|
|
static function urlPathEncode($path) { |
|
|
|
|
|
|
126
|
|
|
$path = self::normalize($path); |
|
127
|
|
|
$encoded = ""; |
|
128
|
|
|
foreach (explode("/", $path) as $dir) |
|
129
|
|
|
$encoded .= rawurlencode($dir) . "/"; |
|
130
|
|
|
$encoded = substr($encoded, 0, -1); |
|
131
|
|
|
return $encoded; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** Decode URL Path |
|
135
|
|
|
* @param string $path |
|
136
|
|
|
* @return string */ |
|
137
|
|
|
|
|
138
|
|
|
static function urlPathDecode($path) { |
|
|
|
|
|
|
139
|
|
|
$path = self::normalize($path); |
|
140
|
|
|
$decoded = ""; |
|
141
|
|
|
foreach (explode("/", $path) as $dir) |
|
142
|
|
|
$decoded .= rawurldecode($dir) . "/"; |
|
143
|
|
|
$decoded = substr($decoded, 0, -1); |
|
144
|
|
|
return $decoded; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.