1 | <?php |
---|
2 | /** |
---|
3 | * Utilities.inc.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | /** |
---|
9 | * Print variable dump. |
---|
10 | * |
---|
11 | * @param mixed $var Variable to dump. |
---|
12 | * @param bool $display Hide the dump in HTML comments? |
---|
13 | * @param bool $var_dump Use var_dump instead of print_r. |
---|
14 | */ |
---|
15 | function dump($var, $display=false, $var_dump=false) |
---|
16 | { |
---|
17 | echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n"; |
---|
18 | if ($var_dump) { |
---|
19 | var_dump($var); |
---|
20 | } else { |
---|
21 | print_r($var); |
---|
22 | } |
---|
23 | echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n"; |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Return dump as variable. |
---|
28 | * |
---|
29 | * @param mixed $var Variable to dump. |
---|
30 | * @return string Dump of var. |
---|
31 | */ |
---|
32 | function getDump($var) |
---|
33 | { |
---|
34 | ob_start(); |
---|
35 | print_r($var); |
---|
36 | $d = ob_get_contents(); |
---|
37 | ob_end_clean(); |
---|
38 | return $d; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Return dump as cleaned text. Useful for dumping data into emails. |
---|
43 | * |
---|
44 | * @param mixed $var Variable to dump. |
---|
45 | * @return string Dump of var. |
---|
46 | */ |
---|
47 | function fancyDump($var, $indent='') |
---|
48 | { |
---|
49 | $output = ''; |
---|
50 | if (is_array($var)) { |
---|
51 | foreach ($var as $k=>$v) { |
---|
52 | $k = ucfirst(strtolower(str_replace(array('_', ' '), ' ', $k))); |
---|
53 | if (is_array($v)) { |
---|
54 | $output .= sprintf("\n%s%s: %s\n", $indent, $k, fancyDump($v, $indent . $indent)); |
---|
55 | } else { |
---|
56 | $output .= sprintf("%s%s: %s\n", $indent, $k, $v); |
---|
57 | } |
---|
58 | } |
---|
59 | } else { |
---|
60 | $output .= sprintf("%s%s\n", $indent, $var); |
---|
61 | } |
---|
62 | return $output; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Returns text with appropriate html translations. |
---|
67 | * |
---|
68 | * @param string $txt Text to clean. |
---|
69 | * @param bool $preserve_html If set to true, oTxt will not translage <, >, ", or ' |
---|
70 | * characters into HTML entities. This allows HTML to pass |
---|
71 | * through unmunged. |
---|
72 | * @return string Cleaned text. |
---|
73 | */ |
---|
74 | function oTxt($txt, $preserve_html=false) |
---|
75 | { |
---|
76 | $search = array(); |
---|
77 | $replace = array(); |
---|
78 | |
---|
79 | // Make converted ampersand entities into normal ampersands (they will be done manually later) to retain HTML entities. |
---|
80 | $search['retain_ampersand'] = '/&/'; |
---|
81 | $replace['retain_ampersand'] = '&'; |
---|
82 | |
---|
83 | if ($preserve_html) { |
---|
84 | // Convert characters that must remain non-entities for displaying HTML. |
---|
85 | $search['retain_left_angle'] = '/</'; |
---|
86 | $replace['retain_left_angle'] = '<'; |
---|
87 | |
---|
88 | $search['retain_right_angle'] = '/>/'; |
---|
89 | $replace['retain_right_angle'] = '>'; |
---|
90 | |
---|
91 | $search['retain_single_quote'] = '/'/'; |
---|
92 | $replace['retain_single_quote'] = "'"; |
---|
93 | |
---|
94 | $search['retain_double_quote'] = '/"/'; |
---|
95 | $replace['retain_double_quote'] = '"'; |
---|
96 | } |
---|
97 | |
---|
98 | // & becomes &. Exclude any occurance where the & is followed by a alphanum or unicode caracter. |
---|
99 | $search['ampersand'] = '/&(?![\w\d#]{1,10};)/'; |
---|
100 | $replace['ampersand'] = '&'; |
---|
101 | |
---|
102 | return preg_replace($search, $replace, htmlentities($txt, ENT_QUOTES, App::getParam('character_set'))); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Returns text with stylistic modifications. Warning: this will break some HTML attibutes! |
---|
107 | * TODO: Allow a string such as this to be passted: <a href="javascript:openPopup('/foo/bar.php')">Click here</a> |
---|
108 | * |
---|
109 | * @param string $txt Text to clean. |
---|
110 | * @return string Cleaned text. |
---|
111 | */ |
---|
112 | function fancyTxt($txt) |
---|
113 | { |
---|
114 | $search = array(); |
---|
115 | $replace = array(); |
---|
116 | |
---|
117 | // "double quoted text" becomes “double quoted text” |
---|
118 | $search['double_quotes'] = '/(^|[^\w=])(?:"|"|"|"|“)([^"]+?)(?:"|"|"|"|”)([^\w]|$)/ms'; // " is the same as " and " and " |
---|
119 | $replace['double_quotes'] = '$1“$2”$3'; |
---|
120 | |
---|
121 | // text's apostrophes become text’s apostrophes |
---|
122 | $search['apostrophe'] = '/(\w)(?:\'|'|')(\w)/ms'; |
---|
123 | $replace['apostrophe'] = '$1’$2'; |
---|
124 | |
---|
125 | // 'single quoted text' becomes ‘single quoted text’ |
---|
126 | $search['single_quotes'] = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/ms'; |
---|
127 | $replace['single_quotes'] = '$1‘$2’$3'; |
---|
128 | |
---|
129 | // plural posessives' apostrophes become posessives’ |
---|
130 | $search['apostrophes'] = '/(s)(?:\'|'|')(\s)/ms'; |
---|
131 | $replace['apostrophes'] = '$1’$2'; |
---|
132 | |
---|
133 | // em--dashes become em—dashes |
---|
134 | $search['em_dash'] = '/(\s*[^!<-])--([^>-]\s*)/'; |
---|
135 | $replace['em_dash'] = '$1—$2'; |
---|
136 | |
---|
137 | return preg_replace($search, $replace, $txt); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | * Generates a hexadecibal html color based on provided word. |
---|
143 | * |
---|
144 | * @access public |
---|
145 | * @param string $text A string for which to convert to color. |
---|
146 | * @return string A hexadecimal html color. |
---|
147 | */ |
---|
148 | function getTextColor($text, $method=1) |
---|
149 | { |
---|
150 | $r = substr(md5($text), 0, 1); |
---|
151 | $g = substr(md5($text), 1, 1); |
---|
152 | $b = substr(md5($text), 2, 1); |
---|
153 | |
---|
154 | switch ($method) { |
---|
155 | case 2 : |
---|
156 | if (hexdec($r) > hexdec('c')) { |
---|
157 | $r = dechex(hexdec('f') - hexdec($r)); |
---|
158 | } |
---|
159 | if (hexdec($g) > hexdec('c')) { |
---|
160 | $g = dechex(hexdec('f') - hexdec($g)); |
---|
161 | } |
---|
162 | if (hexdec($b) > hexdec('c')) { |
---|
163 | $b = dechex(hexdec('f') - hexdec($b)); |
---|
164 | } |
---|
165 | break; |
---|
166 | |
---|
167 | case 1 : |
---|
168 | default : |
---|
169 | $r = dechex(round(hexdec($r) * .8)); |
---|
170 | $g = dechex(round(hexdec($g) * .8)); |
---|
171 | $b = dechex(round(hexdec($b) * .6)); |
---|
172 | break; |
---|
173 | } |
---|
174 | |
---|
175 | return $r . $r . $g . $g . $b . $b; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Encodes a string into unicode values 128-255. |
---|
180 | * Useful for hiding an email address from spambots. |
---|
181 | * |
---|
182 | * @access public |
---|
183 | * @param string $text A line of text to encode. |
---|
184 | * @return string Encoded text. |
---|
185 | */ |
---|
186 | function encodeAscii($text) |
---|
187 | { |
---|
188 | $ouput = ''; |
---|
189 | $num = strlen($text); |
---|
190 | for ($i=0; $i<$num; $i++) { |
---|
191 | $output .= sprintf('&#%03s', ord($text{$i})); |
---|
192 | } |
---|
193 | return $output; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Encodes an email into a "user at domain dot com" format. |
---|
198 | * |
---|
199 | * @access public |
---|
200 | * @param string $email An email to encode. |
---|
201 | * @param string $at Replaces the @. |
---|
202 | * @param string $dot Replaces the .. |
---|
203 | * @return string Encoded email. |
---|
204 | */ |
---|
205 | function encodeEmail($email, $at=' at ', $dot=' dot ') |
---|
206 | { |
---|
207 | $search = array('/@/', '/\./'); |
---|
208 | $replace = array($at, $dot); |
---|
209 | return preg_replace($search, $replace, $email); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Turns "a really long string" into "a rea...string" |
---|
214 | * |
---|
215 | * @access public |
---|
216 | * @param string $str Input string |
---|
217 | * @param int $len Maximum string length. |
---|
218 | * @param string $where Where to cut the string. One of: 'start', 'middle', or 'end'. |
---|
219 | * @return string Truncated output string |
---|
220 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
221 | * @since 29 Mar 2006 13:48:49 |
---|
222 | */ |
---|
223 | function truncate($str, $len, $where='middle') |
---|
224 | { |
---|
225 | $part1 = floor(($len - 3) / 2); |
---|
226 | $part2 = ceil(($len - 3) / 2); |
---|
227 | switch ($where) { |
---|
228 | case 'start' : |
---|
229 | return preg_replace(array(sprintf('/^.{4,}(.{%s})$/', $part1 + $part2), '/\s*\.{3,}\s*/'), array('...$1', '...'), $str); |
---|
230 | break; |
---|
231 | default : |
---|
232 | case 'middle' : |
---|
233 | return preg_replace(array(sprintf('/^(.{%s}).{4,}(.{%s})$/', $part1, $part2), '/\s*\.{3,}\s*/'), array('$1...$2', '...'), $str); |
---|
234 | break; |
---|
235 | case 'end' : |
---|
236 | return preg_replace(array(sprintf('/^(.{%s}).{4,}$/', $part1 + $part2), '/\s*\.{3,}\s*/'), array('$1...', '...'), $str); |
---|
237 | break; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Return a human readable filesize. |
---|
243 | * |
---|
244 | * @param int $size Size |
---|
245 | * @param int $unit The maximum unit |
---|
246 | * @param int $format The return string format |
---|
247 | * @author Aidan Lister <aidan@php.net> |
---|
248 | * @version 1.1.0 |
---|
249 | */ |
---|
250 | function humanFileSize($size, $unit=null, $format='%01.2f %s') |
---|
251 | { |
---|
252 | // Units |
---|
253 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
---|
254 | $ii = count($units) - 1; |
---|
255 | |
---|
256 | // Max unit |
---|
257 | $unit = array_search((string) $unit, $units); |
---|
258 | if ($unit === null || $unit === false) { |
---|
259 | $unit = $ii; |
---|
260 | } |
---|
261 | |
---|
262 | // Loop |
---|
263 | $i = 0; |
---|
264 | while ($unit != $i && $size >= 1024 && $i < $ii) { |
---|
265 | $size /= 1024; |
---|
266 | $i++; |
---|
267 | } |
---|
268 | |
---|
269 | return sprintf($format, $size, $units[$i]); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * Returns stats of a file from the include path. |
---|
274 | * |
---|
275 | * @param string $file File in include path. |
---|
276 | * @return mixed Value of requested key from fstat(), or false on error. |
---|
277 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
278 | * @since 03 Dec 2005 14:23:26 |
---|
279 | */ |
---|
280 | function statIncludePath($file, $stat) |
---|
281 | { |
---|
282 | // Open file pointer read-only using include path. |
---|
283 | if ($fp = fopen($file, 'r', true)) { |
---|
284 | // File opend successfully, get stats. |
---|
285 | $stats = fstat($fp); |
---|
286 | fclose($fp); |
---|
287 | // Return specified stats. |
---|
288 | return $stats[$stat]; |
---|
289 | } else { |
---|
290 | return false; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * If $var is net set or null, set it to $default. Otherwise leave it alone. |
---|
296 | * Returns the final value of $var. Use to find a default value of one is not avilable. |
---|
297 | * |
---|
298 | * @param mixed $var The variable that is being set. |
---|
299 | * @param mixed $default What to set it to if $val is not currently set. |
---|
300 | * @return mixed The resulting value of $var. |
---|
301 | */ |
---|
302 | function setDefault(&$var, $default='') |
---|
303 | { |
---|
304 | if (!isset($var)) { |
---|
305 | $var = $default; |
---|
306 | } |
---|
307 | return $var; |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * Like preg_quote() except for arrays, it takes an array of strings and puts |
---|
312 | * a backslash in front of every character that is part of the regular |
---|
313 | * expression syntax. |
---|
314 | * |
---|
315 | * @param array $array input array |
---|
316 | * @param array $delim optional character that will also be excaped. |
---|
317 | * @return array an array with the same values as $array1 but shuffled |
---|
318 | */ |
---|
319 | function pregQuoteArray($array, $delim='/') |
---|
320 | { |
---|
321 | if (!empty($array)) { |
---|
322 | if (is_array($array)) { |
---|
323 | foreach ($array as $key=>$val) { |
---|
324 | $quoted_array[$key] = preg_quote($val, $delim); |
---|
325 | } |
---|
326 | return $quoted_array; |
---|
327 | } else { |
---|
328 | return preg_quote($array, $delim); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Converts a PHP Array into encoded URL arguments and return them as an array. |
---|
335 | * |
---|
336 | * @param mixed $data An array to transverse recursivly, or a string |
---|
337 | * to use directly to create url arguments. |
---|
338 | * @param string $prefix The name of the first dimension of the array. |
---|
339 | * If not specified, the first keys of the array will be used. |
---|
340 | * @return array URL with array elements as URL key=value arguments. |
---|
341 | */ |
---|
342 | function urlEncodeArray($data, $prefix='', $_return=true) { |
---|
343 | |
---|
344 | // Data is stored in static variable. |
---|
345 | static $args; |
---|
346 | |
---|
347 | if (is_array($data)) { |
---|
348 | foreach ($data as $key => $val) { |
---|
349 | // If the prefix is empty, use the $key as the name of the first dimention of the "array". |
---|
350 | // ...otherwise, append the key as a new dimention of the "array". |
---|
351 | $new_prefix = ('' == $prefix) ? urlencode($key) : $prefix . '[' . urlencode($key) . ']'; |
---|
352 | // Enter recursion. |
---|
353 | urlEncodeArray($val, $new_prefix, false); |
---|
354 | } |
---|
355 | } else { |
---|
356 | // We've come to the last dimention of the array, save the "array" and its value. |
---|
357 | $args[$prefix] = urlencode($data); |
---|
358 | } |
---|
359 | |
---|
360 | if ($_return) { |
---|
361 | // This is not a recursive execution. All recursion is complete. |
---|
362 | // Reset static var and return the result. |
---|
363 | $ret = $args; |
---|
364 | $args = array(); |
---|
365 | return is_array($ret) ? $ret : array(); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * Converts a PHP Array into encoded URL arguments and return them in a string. |
---|
371 | * |
---|
372 | * @param mixed $data An array to transverse recursivly, or a string |
---|
373 | * to use directly to create url arguments. |
---|
374 | * @param string $prefix The name of the first dimention of the array. |
---|
375 | * If not specified, the first keys of the array will be used. |
---|
376 | * @return string url A string ready to append to a url. |
---|
377 | */ |
---|
378 | function urlEncodeArrayToString($data, $prefix='') { |
---|
379 | |
---|
380 | $array_args = urlEncodeArray($data, $prefix); |
---|
381 | $url_args = ''; |
---|
382 | $delim = ''; |
---|
383 | foreach ($array_args as $key=>$val) { |
---|
384 | $url_args .= $delim . $key . '=' . $val; |
---|
385 | $delim = ini_get('arg_separator.output'); |
---|
386 | } |
---|
387 | return $url_args; |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Fills an arrray with the result from a multiple ereg search. |
---|
392 | * Curtesy of Bruno - rbronosky@mac.com - 10-May-2001 |
---|
393 | * Blame him for the funky do...while loop. |
---|
394 | * |
---|
395 | * @param mixed $pattern regular expression needle |
---|
396 | * @param mixed $string haystack |
---|
397 | * @return array populated with each found result |
---|
398 | */ |
---|
399 | function eregAll($pattern, $string) |
---|
400 | { |
---|
401 | do { |
---|
402 | if (!ereg($pattern, $string, $temp)) { |
---|
403 | continue; |
---|
404 | } |
---|
405 | $string = str_replace($temp[0], '', $string); |
---|
406 | $results[] = $temp; |
---|
407 | } while (ereg($pattern, $string, $temp)); |
---|
408 | return $results; |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * Prints the word "checked" if a variable is set, and optionally matches |
---|
413 | * the desired value, otherwise prints nothing, |
---|
414 | * used for printing the word "checked" in a checkbox form input. |
---|
415 | * |
---|
416 | * @param mixed $var the variable to compare |
---|
417 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
418 | */ |
---|
419 | function frmChecked($var, $value=null) |
---|
420 | { |
---|
421 | if (func_num_args() == 1 && $var) { |
---|
422 | // 'Checked' if var is true. |
---|
423 | echo ' checked="checked" '; |
---|
424 | } else if (func_num_args() == 2 && $var == $value) { |
---|
425 | // 'Checked' if var and value match. |
---|
426 | echo ' checked="checked" '; |
---|
427 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
428 | // 'Checked' if the value is in the key or the value of an array. |
---|
429 | if (isset($var[$value])) { |
---|
430 | echo ' checked="checked" '; |
---|
431 | } else if (in_array($value, $var)) { |
---|
432 | echo ' checked="checked" '; |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * prints the word "selected" if a variable is set, and optionally matches |
---|
439 | * the desired value, otherwise prints nothing, |
---|
440 | * otherwise prints nothing, used for printing the word "checked" in a |
---|
441 | * select form input |
---|
442 | * |
---|
443 | * @param mixed $var the variable to compare |
---|
444 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
445 | */ |
---|
446 | function frmSelected($var, $value=null) |
---|
447 | { |
---|
448 | if (func_num_args() == 1 && $var) { |
---|
449 | // 'selected' if var is true. |
---|
450 | echo ' selected="selected" '; |
---|
451 | } else if (func_num_args() == 2 && $var == $value) { |
---|
452 | // 'selected' if var and value match. |
---|
453 | echo ' selected="selected" '; |
---|
454 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
455 | // 'selected' if the value is in the key or the value of an array. |
---|
456 | if (isset($var[$value])) { |
---|
457 | echo ' selected="selected" '; |
---|
458 | } else if (in_array($value, $var)) { |
---|
459 | echo ' selected="selected" '; |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | /** |
---|
465 | * Adds slashes to values of an array and converts the array to a comma |
---|
466 | * delimited list. If value provided is a string return the string |
---|
467 | * escaped. This is useful for putting values coming in from posted |
---|
468 | * checkboxes into a SET column of a database. |
---|
469 | * |
---|
470 | * |
---|
471 | * @param array $in Array to convert. |
---|
472 | * @return string Comma list of array values. |
---|
473 | */ |
---|
474 | function escapedList($in) |
---|
475 | { |
---|
476 | if (is_array($in) && !empty($in)) { |
---|
477 | return "'" . join("', '", array_map(array('DB', 'escapeString'), $in)) . "'"; |
---|
478 | } else { |
---|
479 | return DB::escapeString($in); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * Converts a human string date into a SQL-safe date. Dates nearing |
---|
485 | * infinity use the date 2038-01-01 so conversion to unix time format |
---|
486 | * remain within valid range. |
---|
487 | * |
---|
488 | * @param array $date String date to convert. |
---|
489 | * @param array $format Date format to pass to date(). |
---|
490 | * Default produces MySQL datetime: 0000-00-00 00:00:00. |
---|
491 | * @return string SQL-safe date. |
---|
492 | */ |
---|
493 | function strToSQLDate($date, $format='Y-m-d H:i:s') |
---|
494 | { |
---|
495 | // Translate the human string date into SQL-safe date format. |
---|
496 | if (empty($date) || '0000-00-00' == $date || strtotime($date) === -1) { |
---|
497 | $sql_date = '0000-00-00'; |
---|
498 | } else { |
---|
499 | $sql_date = date($format, strtotime($date)); |
---|
500 | } |
---|
501 | |
---|
502 | return $sql_date; |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | * If magic_quotes_gpc is in use, run stripslashes() on $var. If $var is an |
---|
507 | * array, stripslashes is run on each value, recursivly, and the stripped |
---|
508 | * array is returned. |
---|
509 | * |
---|
510 | * @param mixed $var The string or array to un-quote, if necessary. |
---|
511 | * @return mixed $var, minus any magic quotes. |
---|
512 | */ |
---|
513 | function dispelMagicQuotes($var) |
---|
514 | { |
---|
515 | static $magic_quotes_gpc; |
---|
516 | |
---|
517 | if (!isset($magic_quotes_gpc)) { |
---|
518 | $magic_quotes_gpc = get_magic_quotes_gpc(); |
---|
519 | } |
---|
520 | |
---|
521 | if ($magic_quotes_gpc) { |
---|
522 | if (!is_array($var)) { |
---|
523 | $var = stripslashes($var); |
---|
524 | } else { |
---|
525 | foreach ($var as $key=>$val) { |
---|
526 | if (is_array($val)) { |
---|
527 | $var[$key] = dispelMagicQuotes($val); |
---|
528 | } else { |
---|
529 | $var[$key] = stripslashes($val); |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | } |
---|
534 | return $var; |
---|
535 | } |
---|
536 | |
---|
537 | /** |
---|
538 | * Get a form variable from GET or POST data, stripped of magic |
---|
539 | * quotes if necessary. |
---|
540 | * |
---|
541 | * @param string $var (optional) The name of the form variable to look for. |
---|
542 | * @param string $default (optional) The value to return if the |
---|
543 | * variable is not there. |
---|
544 | * @return mixed A cleaned GET or POST if no $var specified. |
---|
545 | * @return string A cleaned form $var if found, or $default. |
---|
546 | */ |
---|
547 | function getFormData($var=null, $default=null) |
---|
548 | { |
---|
549 | if ('POST' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
550 | return dispelMagicQuotes($_POST); |
---|
551 | } else if ('GET' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
552 | return dispelMagicQuotes($_GET); |
---|
553 | } |
---|
554 | if (isset($_POST[$var])) { |
---|
555 | return dispelMagicQuotes($_POST[$var]); |
---|
556 | } else if (isset($_GET[$var])) { |
---|
557 | return dispelMagicQuotes($_GET[$var]); |
---|
558 | } else { |
---|
559 | return $default; |
---|
560 | } |
---|
561 | } |
---|
562 | function getPost($var=null, $default=null) |
---|
563 | { |
---|
564 | if (is_null($var)) { |
---|
565 | return dispelMagicQuotes($_POST); |
---|
566 | } |
---|
567 | if (isset($_POST[$var])) { |
---|
568 | return dispelMagicQuotes($_POST[$var]); |
---|
569 | } else { |
---|
570 | return $default; |
---|
571 | } |
---|
572 | } |
---|
573 | function getGet($var=null, $default=null) |
---|
574 | { |
---|
575 | if (is_null($var)) { |
---|
576 | return dispelMagicQuotes($_GET); |
---|
577 | } |
---|
578 | if (isset($_GET[$var])) { |
---|
579 | return dispelMagicQuotes($_GET[$var]); |
---|
580 | } else { |
---|
581 | return $default; |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | /** |
---|
586 | * Signs a value using md5 and a simple text key. In order for this |
---|
587 | * function to be useful (i.e. secure) the key must be kept secret, which |
---|
588 | * means keeping it as safe as database credentials. Putting it into an |
---|
589 | * environment variable set in httpd.conf is a good place. |
---|
590 | * |
---|
591 | * @access public |
---|
592 | * @param string $val The string to sign. |
---|
593 | * @param string $seed_key (Optional) A text key to use for computing the signature. |
---|
594 | * @return string The original value with a signature appended. |
---|
595 | */ |
---|
596 | function addSignature($val, $seed_key=null) |
---|
597 | { |
---|
598 | if ('' == $val) { |
---|
599 | App::logMsg(sprintf('Adding signature to empty string.', null), LOG_NOTICE, __FILE__, __LINE__); |
---|
600 | } |
---|
601 | |
---|
602 | if (!isset($seed_key)) { |
---|
603 | $seed_key = App::getParam('signing_key'); |
---|
604 | } |
---|
605 | |
---|
606 | return $val . '-' . substr(md5($val . $seed_key), 0, 18); |
---|
607 | } |
---|
608 | |
---|
609 | /** |
---|
610 | * Strips off the signature appended by addSignature(). |
---|
611 | * |
---|
612 | * @access public |
---|
613 | * @param string $signed_val The string to sign. |
---|
614 | * @return string The original value with a signature removed. |
---|
615 | */ |
---|
616 | function removeSignature($signed_val) |
---|
617 | { |
---|
618 | return substr($signed_val, 0, strrpos($signed_val, '-')); |
---|
619 | } |
---|
620 | |
---|
621 | |
---|
622 | /** |
---|
623 | * Verifies a signature appened to a value by addSignature(). |
---|
624 | * |
---|
625 | * @access public |
---|
626 | * @param string $signed_val A value with appended signature. |
---|
627 | * @param string $seed_key (Optional) A text key to use for computing the signature. |
---|
628 | * @return bool True if the signature matches the var. |
---|
629 | */ |
---|
630 | function verifySignature($signed_val, $seed_key=null) |
---|
631 | { |
---|
632 | // Strip the value from the signed value. |
---|
633 | $val = removeSignature($signed_val); |
---|
634 | // If the signed value matches the original signed value we consider the value safe. |
---|
635 | if ($signed_val == addSignature($val, $seed_key)) { |
---|
636 | // Signature verified. |
---|
637 | return true; |
---|
638 | } else { |
---|
639 | return false; |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | * Sends empty output to the browser and flushes the php buffer so the client |
---|
645 | * will see data before the page is finished processing. |
---|
646 | */ |
---|
647 | function flushBuffer() { |
---|
648 | echo str_repeat(' ', 205); |
---|
649 | flush(); |
---|
650 | } |
---|
651 | |
---|
652 | /** |
---|
653 | * Adds email address to mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
654 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
655 | * |
---|
656 | * @access public |
---|
657 | * @param string $email Email address to add. |
---|
658 | * @param string $list Name of list to add to. |
---|
659 | * @param bool $send_welcome_message True to send welcome message to subscriber. |
---|
660 | * @return bool True on success, false on failure. |
---|
661 | */ |
---|
662 | function mailmanAddMember($email, $list, $send_welcome_message=false) |
---|
663 | { |
---|
664 | $add_members = '/var/mailman/bin/add_members'; |
---|
665 | if (is_executable($add_members) && is_readable($add_members)) { |
---|
666 | $welcome_msg = $send_welcome_message ? 'y' : 'n'; |
---|
667 | exec(sprintf('/bin/echo %s | /usr/bin/sudo %s -r - --welcome-msg=%s --admin-notify=n %s', escapeshellarg($email), escapeshellarg($add_members), $welcome_msg, escapeshellarg($list)), $stdout, $return_code); |
---|
668 | if (0 == $return_code) { |
---|
669 | App::logMsg(sprintf('Mailman add member success for list: %s, user: %s', $list, $email, $stdout), LOG_INFO, __FILE__, __LINE__); |
---|
670 | return true; |
---|
671 | } else { |
---|
672 | App::logMsg(sprintf('Mailman add member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
673 | return false; |
---|
674 | } |
---|
675 | } else { |
---|
676 | App::logMsg(sprintf('Mailman add member program not executable: %s', $add_members), LOG_ALERT, __FILE__, __LINE__); |
---|
677 | return false; |
---|
678 | } |
---|
679 | } |
---|
680 | |
---|
681 | /** |
---|
682 | * Removes email address from mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
683 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
684 | * |
---|
685 | * @access public |
---|
686 | * @param string $email Email address to add. |
---|
687 | * @param string $list Name of list to add to. |
---|
688 | * @param bool $send_user_ack True to send goodbye message to subscriber. |
---|
689 | * @return bool True on success, false on failure. |
---|
690 | */ |
---|
691 | function mailmanRemoveMember($email, $list, $send_user_ack=false) |
---|
692 | { |
---|
693 | $remove_members = '/var/mailman/bin/remove_members'; |
---|
694 | if (is_executable($remove_members) && is_readable($remove_members)) { |
---|
695 | $userack = $send_user_ack ? '' : '--nouserack'; |
---|
696 | exec(sprintf('/usr/bin/sudo %s %s --noadminack %s %s', escapeshellarg($remove_members), $userack, escapeshellarg($list), escapeshellarg($email)), $stdout, $return_code); |
---|
697 | if (0 == $return_code) { |
---|
698 | App::logMsg(sprintf('Mailman remove member success for list: %s, user: %s', $list, $email, $stdout), LOG_INFO, __FILE__, __LINE__); |
---|
699 | return true; |
---|
700 | } else { |
---|
701 | App::logMsg(sprintf('Mailman remove member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
702 | return false; |
---|
703 | } |
---|
704 | } else { |
---|
705 | App::logMsg(sprintf('Mailman remove member program not executable: %s', $remove_members), LOG_ALERT, __FILE__, __LINE__); |
---|
706 | return false; |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | /** |
---|
711 | * If the given $url is on the same web site, return true. This can be used to |
---|
712 | * prevent from sending sensitive info in a get query (like the SID) to another |
---|
713 | * domain. |
---|
714 | * |
---|
715 | * @param string $url the URI to test. |
---|
716 | * @return bool True if given $url is this domain or has no domain (is a relative url), false if it's another. |
---|
717 | */ |
---|
718 | function isMyDomain($url) |
---|
719 | { |
---|
720 | static $urls = array(); |
---|
721 | |
---|
722 | if (!isset($urls[$url])) { |
---|
723 | if (!preg_match('|\w{1,}\.\w{2,5}/|', $url)) { |
---|
724 | // If we can't find a domain we assume the URL is relative. |
---|
725 | $urls[$url] = true; |
---|
726 | } else { |
---|
727 | $urls[$url] = preg_match('/' . preg_quote(getenv('HTTP_HOST')) . '/', $url); |
---|
728 | } |
---|
729 | } |
---|
730 | return $urls[$url]; |
---|
731 | } |
---|
732 | |
---|
733 | /** |
---|
734 | * Takes a URL and returns it without the query or anchor portion |
---|
735 | * |
---|
736 | * @param string $url any kind of URI |
---|
737 | * @return string the URI with ? or # and everything after removed |
---|
738 | */ |
---|
739 | function stripQuery($url) |
---|
740 | { |
---|
741 | return preg_replace('![?#].*!', '', $url); |
---|
742 | } |
---|
743 | |
---|
744 | /** |
---|
745 | * Returns the remote IP address, taking into consideration proxy servers. |
---|
746 | * |
---|
747 | * @param bool $dolookup If true we resolve to IP to a host name, |
---|
748 | * if false we don't. |
---|
749 | * @return string IP address if $dolookup is false or no arg |
---|
750 | * Hostname if $dolookup is true |
---|
751 | */ |
---|
752 | function getRemoteAddr($dolookup=false) |
---|
753 | { |
---|
754 | $ip = getenv('HTTP_CLIENT_IP'); |
---|
755 | if (empty($ip) || $ip == 'unknown' || $ip == 'localhost' || $ip == '127.0.0.1') { |
---|
756 | $ip = getenv('HTTP_X_FORWARDED_FOR'); |
---|
757 | if (empty($ip) || $ip == 'unknown' || $ip == 'localhost' || $ip == '127.0.0.1') { |
---|
758 | $ip = getenv('REMOTE_ADDR'); |
---|
759 | } |
---|
760 | } |
---|
761 | return $dolookup && '' != $ip ? gethostbyaddr($ip) : $ip; |
---|
762 | } |
---|
763 | |
---|
764 | /** |
---|
765 | * Tests whether a given IP address can be found in an array of IP address networks. |
---|
766 | * Elements of networks array can be single IP addresses or an IP address range in CIDR notation |
---|
767 | * See: http://en.wikipedia.org/wiki/Classless_inter-domain_routing |
---|
768 | * |
---|
769 | * @access public |
---|
770 | * @param string IP address to search for. |
---|
771 | * @param array Array of networks to search within. |
---|
772 | * @return mixed Returns the network that matched on success, false on failure. |
---|
773 | */ |
---|
774 | function ipInRange($ip, $networks) |
---|
775 | { |
---|
776 | if (!is_array($networks)) { |
---|
777 | $networks = array($networks); |
---|
778 | } |
---|
779 | |
---|
780 | $ip_binary = sprintf('%032b', ip2long($ip)); |
---|
781 | foreach ($networks as $network) { |
---|
782 | if (preg_match('![\d\.]{7,15}/\d{1,2}!', $network)) { |
---|
783 | // IP is in CIDR notation. |
---|
784 | list($cidr_ip, $cidr_bitmask) = split('/', $network); |
---|
785 | $cidr_ip_binary = sprintf('%032b', ip2long($cidr_ip)); |
---|
786 | if (substr($ip_binary, 0, $cidr_bitmask) === substr($cidr_ip_binary, 0, $cidr_bitmask)) { |
---|
787 | // IP address is within the specified IP range. |
---|
788 | return $network; |
---|
789 | } |
---|
790 | } else { |
---|
791 | if ($ip === $network) { |
---|
792 | // IP address exactly matches. |
---|
793 | return $network; |
---|
794 | } |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | return false; |
---|
799 | } |
---|
800 | |
---|
801 | /** |
---|
802 | * Returns a fully qualified URL to the current script, including the query. |
---|
803 | * |
---|
804 | * @return string a full url to the current script |
---|
805 | */ |
---|
806 | function absoluteMe() |
---|
807 | { |
---|
808 | $protocol = ('on' == getenv('HTTPS')) ? 'https://' : 'http://'; |
---|
809 | return $protocol . getenv('HTTP_HOST') . getenv('REQUEST_URI'); |
---|
810 | } |
---|
811 | |
---|
812 | /** |
---|
813 | * Compares the current url with the referring url. |
---|
814 | * |
---|
815 | * @param string $compary_query Include the query string in the comparison. |
---|
816 | * @return bool true if the current script (or specified valid_referer) |
---|
817 | * is the referrer. false otherwise. |
---|
818 | */ |
---|
819 | function refererIsMe($exclude_query=false) |
---|
820 | { |
---|
821 | if ($exclude_query) { |
---|
822 | return (stripQuery(absoluteMe()) == stripQuery(getenv('HTTP_REFERER'))); |
---|
823 | } else { |
---|
824 | return (absoluteMe() == getenv('HTTP_REFERER')); |
---|
825 | } |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * Stub functions used when installation does not have |
---|
830 | * GNU gettext extension installed |
---|
831 | */ |
---|
832 | if (!extension_loaded('gettext')) { |
---|
833 | /** |
---|
834 | * Translates text |
---|
835 | * |
---|
836 | * @access public |
---|
837 | * @param string $text the text to be translated |
---|
838 | * @return string translated text |
---|
839 | */ |
---|
840 | function gettext($text) { |
---|
841 | return $text; |
---|
842 | } |
---|
843 | |
---|
844 | /** |
---|
845 | * Translates text |
---|
846 | * |
---|
847 | * @access public |
---|
848 | * @param string $text the text to be translated |
---|
849 | * @return string translated text |
---|
850 | */ |
---|
851 | function _($text) { |
---|
852 | return $text; |
---|
853 | } |
---|
854 | |
---|
855 | /** |
---|
856 | * Translates text by domain |
---|
857 | * |
---|
858 | * @access public |
---|
859 | * @param string $domain the language to translate the text into |
---|
860 | * @param string $text the text to be translated |
---|
861 | * @return string translated text |
---|
862 | */ |
---|
863 | function dgettext($domain, $text) { |
---|
864 | return $text; |
---|
865 | } |
---|
866 | |
---|
867 | /** |
---|
868 | * Translates text by domain and category |
---|
869 | * |
---|
870 | * @access public |
---|
871 | * @param string $domain the language to translate the text into |
---|
872 | * @param string $text the text to be translated |
---|
873 | * @param string $category the language dialect to use |
---|
874 | * @return string translated text |
---|
875 | */ |
---|
876 | function dcgettext($domain, $text, $category) { |
---|
877 | return $text; |
---|
878 | } |
---|
879 | |
---|
880 | /** |
---|
881 | * Binds the text domain |
---|
882 | * |
---|
883 | * @access public |
---|
884 | * @param string $domain the language to translate the text into |
---|
885 | * @param string |
---|
886 | * @return string translated text |
---|
887 | */ |
---|
888 | function bindtextdomain($domain, $directory) { |
---|
889 | return $domain; |
---|
890 | } |
---|
891 | |
---|
892 | /** |
---|
893 | * Sets the text domain |
---|
894 | * |
---|
895 | * @access public |
---|
896 | * @param string $domain the language to translate the text into |
---|
897 | * @return string translated text |
---|
898 | */ |
---|
899 | function textdomain($domain) { |
---|
900 | return $domain; |
---|
901 | } |
---|
902 | } |
---|
903 | |
---|
904 | |
---|
905 | |
---|
906 | ?> |
---|