-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
utilities.inc.php
278 lines (241 loc) · 6.86 KB
/
utilities.inc.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* @file utilities.inc.php
* Miscellaneous utility functions.
*
* @section License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Copyright © 2009-2012 Daniel Swanson <danij@dengine.net>
*/
require_once(DIR_CLASSES.'/url.class.php');
includeGuard('Utils');
function checkImagePath($path, $formats)
{
if(!isset($path) || !isset($formats) || !is_array($formats))
return -1;
$idx = 0;
foreach($formats as $fmt)
{
$fullPath = $path . '.' . $fmt;
if(file_exists($fullPath))
return $idx;
$idx++;
}
}
function includeHTML($page, $pluginName=NULL)
{
$page = trim($page);
$page = stripslashes($page);
$filepath = '';
if(!is_null($pluginName))
{
$pluginName = trim($pluginName);
$pluginName = stripslashes($pluginName);
$filepath .= DIR_PLUGINS.'/'.$pluginName .'/';
}
$filepath .= 'html/' . $page . '.html';
if(file_exists($filepath))
{
readfile($filepath);
}
else
{
echo "Internal error: Unknown page \"$filepath\"";
}
}
function fopen_recursive($path, $mode, $chmod=0755)
{
$matches = NULL;
preg_match('`^(.+)/([a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)$`i', $path, $matches);
$directory = $matches[1];
$file = $matches[2];
if(!is_dir($directory))
{
if(!mkdir($directory, $chmod, 1))
{
return false;
}
}
return fopen($path, $mode);
}
function file_get_contents_utf8($fn, $contentType, $charset)
{
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type:$contentType; charset=$charset"
)
);
$context = stream_context_create($opts);
$result = @file_get_contents($fn,false,$context);
return $result;
}
function implode_keys($glue="", $pieces=array())
{
$keys = array_keys($pieces);
return implode($glue, $keys);
}
/**
* Case insensitive version of array_key_exists.
* Returns the matching key on success, else false.
*
* @param key (string)
* @param search (array)
* @return (bool) false if not found | (string) found key
*/
function array_casekey_exists($key, $search)
{
if(array_key_exists($key, $search))
{
return $key;
}
if(!(is_string($key) && is_array($search) && count($search)))
{
return false;
}
$key = strtolower($key);
foreach($search as $k => $v)
{
if(strtolower($k) == $key)
{
return $k;
}
}
return false;
}
function clean_text($text, $length = 0)
{
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
if($length > 0 && strlen($text) > $length)
{
$cut_point = strrpos(substr($text, 0, $length), ' ');
$text = substr($text, 0, $cut_point) . '…';
}
return $text;
}
function safe_url($raw_url)
{
$url_scheme = parse_url($raw_url, PHP_URL_SCHEME);
if($url_scheme == 'http' || $url_scheme == 'https')
{
return htmlspecialchars($raw_url, ENT_QUOTES, 'UTF-8', false);
}
// parse_url failed, or the scheme was not hypertext-based.
return false;
}
/**
* Given an associative array generate a textual representation
* using JSON markup.
*
* @note json_encode is practically useless for real world data
* in PHP <= 5.3 hence this hand-rolled imitation.
*
* @param array (Array) Array to be interpreted.
* @param flags (Integer) Reserved for future use.
* @param indent_level (Integer) Level of indent to add to the output.
* @return (Mixed) Textual representation in JSON format, else Boolean @c FALSE
*/
function json_encode_clean(&$array, $flags=0, $indent_level=0)
{
if(!is_array($array)) return FALSE;
$staged = NULL;
foreach($array as $key => $value)
{
$key = '"'. addslashes($key) .'"';
// Format the value:
if(is_array($value))
{
// Descend to the subobject.
$value = json_encode_clean($value, $flags, $indent_level+1);
}
else if(is_bool($value))
{
$value = ((boolean)$value)? 'true' : 'false';
}
else if(!is_numeric($value) || is_string($value))
{
$value = '"'. addslashes($value) .'"';
}
// Time to construct the staging array?
if(is_null($staged))
{
$staged = array();
}
$staged[] = "$key: $value";
}
if(is_null($staged)) return '';
// Collapse into JSON comma-delimited form.
$indent = ' ';
$glue = ", \n";
$tmp = '';
foreach($staged as $item)
{
$tmp .= $indent . $item . $glue;
}
$result = "{\n" . substr($tmp, 0, -strlen($glue)) . "\n}";
// Determine scope-level indent depth.
$indent_level = (integer)$indent_level;
if($indent_level < 0) $indent_level = 0;
// Apply a scope-level indent?
if($indent_level != 0)
{
$result = str_repeat($indent, $indent_level) . $result;
}
return $result;
}
function generateHyperlinkHTML($uri, $attributes = array(), $maxLength = 40)
{
$shortUri = NULL;
if($uri instanceof Url)
{
if(!strcmp($uri->host(), 'sourceforge.net') &&
!substr_compare($uri->path(), '/p/deng/bugs/', 0, 13))
{
$bugStr = substr($uri->path(), 13);
$bugNum = intval(substr($bugStr, 0, strpos($bugStr, '/')));
$shortUri = "Bug#$bugNum";
if(is_null($attributes))
$attributes = array();
$attributes['title'] = "View bug report #$bugNum in the tracker";
}
$uri = $uri->toString();
}
else
{
$uri = strval($uri);
}
$attribs = '';
if(is_array($attributes))
{
foreach($attributes as $attribute => $value)
{
$attribs .= " {$attribute}=\"{$value}\"";
}
}
if(!isset($shortUri))
{
$maxLength = (integer)$maxLength;
if($maxLength < 0) $maxLength = 0;
if($maxLength > 0 && strlen($uri) > $maxLength)
$shortUri = substr($uri, 0, $maxLength).'...';
else
$shortUri = $uri;
}
$html = "<a href=\"{$uri}\"{$attribs}>". htmlspecialchars($shortUri) .'</a>';
return $html;
}