-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.php
210 lines (187 loc) · 6.82 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
PHP-Mesh - A page meshing framework for PHP.
Copyright (C) 2003-2009 Trejkaz
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
You can contact the author by electronic mail, which is presently
at the following address: trejkaz@trypticon.org
*/
/**
* Resolves a path relative to a given base path, taking possible absolute
* paths into account.
*
* If $path is absolute, it is returned as-is.
* If $path is relative, it is treated as a relative path with respect to $basepath,
* and the resulting path is returned.
*
* In both cases, the path returned, will have had the realpath() function applied.
*
* @param $basepath the base path, must be absolute.
* @param $path the path, relative or absolute.
* @return $path if it was absolute, otherwise the result of $path relative to $basepath.
*/
function resolve_path($basepath, $path)
{
$basepath = str_replace("\\", "/", $basepath);
$path = str_replace("\\", "/", $path);
if (is_absolute($path))
{
return realpath($path);
}
else
{
return realpath($basepath . '/' . $path);
}
}
/**
* Tests whether a pathname is absolute.
*
* @param $path the path.
* @param $windows (used by tests only) TRUE to use Windows logic for determining this, otherwise use UNIX logic.
* @return TRUE if the path was absolute, FALSE otherwise.
*/
function is_absolute($path, $windows = NULL)
{
if (!isset($windows))
{
$windows = (PHP_OS == 'WINNT' || PHP_OS == 'WIN32');
}
$regex = $windows ? '#^([a-z]:)?[/\\\\]#i' : '#^/#';
return (boolean) preg_match($regex, $path);
}
/**
* Finds the nearest copy of a given filename by searching in the current path,
* then the parent path, and so on up to the document root.
*
* On non-Apache systems, only searches the current path and the document root.
*
* @param $filename the filename to search for.
* @return the full path to the nearest file with the given filename, or NULL if none was found.
*/
function find_nearest($filename)
{
if (function_exists('apache_lookup_uri'))
{
if (isset($_SERVER['SCRIPT_NAME']))
{
$path_url = chop_file($_SERVER['SCRIPT_NAME']);
}
// Some PHPs appeared to have this instead.
if (isset($_SERVER['SCRIPT_URL']) && !isset($path_url))
{
$path_url = chop_file($_SERVER['SCRIPT_URL']);
}
while ($path_url != "")
{
// Use Apache to find out where the actual file is for that URL.
$config_info = apache_lookup_uri($path_url . $filename);
if (file_exists($config_info->filename))
{
// Success!
return $config_info->filename;
}
$path_url = chop_last($path_url);
}
}
else
{
// If not on Apache we use SCRIPT_FILENAME instead, which is the full
// path to the script as given to us by PHP.
if (isset($_SERVER['SCRIPT_FILENAME']))
{
$path = chop_file(realpath($_SERVER['SCRIPT_FILENAME']));
}
// We can't just stop at DOCUMENT_ROOT as on some systems the script
// is running from an aliased directory instead of inside the docroot.
while ($path != "")
{
$file = $path . $filename;
if (file_exists($file))
{
// Success!
return $file;
}
$path = chop_last($path);
}
}
// Failure.
return NULL;
}
/**
* Chops the final component off a path. Differs from the built-in dirname() function because
* this doesn't require the path to be an actual file.
*
* @param $path the original path.
* @return the path with one less path component from the end.
*/
function chop_last($path)
{
// Having a trailing slash at the end defeats the purpose of finding the last one.
$path = rtrim($path, "/");
if ($path == "")
{
return "";
}
// For some lame reason, strrchr in PHP returns the string instead of the offset.
$tail = strrchr($path, "/");
return substr($path, 0, strlen($path) - strlen($tail) + 1);
}
/**
* Chops the final component off a path, but only if the final component is a file, which is
* to say that if the path ends in "/", it remains unaffected.
*
* The returned path will include the slash in all cases.
*
* @param $path the original path.
* @return the path with one less path component from the end, if the path didn't end in "/".
*/
function chop_file($path)
{
if (strrchr($path, "/") == "/")
{
return $path;
}
else
{
return chop_last($path);
}
}
/**
* Parses the HTTP_ACCEPT_LANGUAGE header and returns a list of languages / locale names
* in the requesting browser's preferred order.
*
* @param $header (used by tests only) header value to parse.
* @return a list of languages the browser accepts, with the better ones listed first.
*/
function parse_http_accept_language($header = NULL)
{
if (!isset($header))
{
$header = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
$langs = explode(',', $header);
$qvalues = array();
foreach ($langs as $lang)
{
if (preg_match('/^([a-z]{1,2}(-[a-z]+)?)(;q=\d+\.\d+)?$/i', $lang, &$found))
{
$code = $found[1];
$qvalue = $found[3] ? $found[3] : 1.0;
$qvalues[$code] = $qvalue;
}
}
// Maintains the key mappings while reordering based on the values.
arsort($qvalues);
return array_keys($qvalues);
}
?>