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 | * |
---|
31 | * @return string Dump of var. |
---|
32 | */ |
---|
33 | function getDump($var) |
---|
34 | { |
---|
35 | ob_start(); |
---|
36 | print_r($var); |
---|
37 | $d = ob_get_contents(); |
---|
38 | ob_end_clean(); |
---|
39 | return $d; |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Return dump as cleaned text. Useful for dumping data into emails. |
---|
44 | * |
---|
45 | * @param mixed $var Variable to dump. |
---|
46 | * |
---|
47 | * @return string Dump of var. |
---|
48 | */ |
---|
49 | function fancyDump($var, $indent='') |
---|
50 | { |
---|
51 | $output = ''; |
---|
52 | if (is_array($var)) { |
---|
53 | foreach ($var as $k=>$v) { |
---|
54 | $k = ucfirst(strtolower(str_replace(array('_', ' '), ' ', $k))); |
---|
55 | if (is_array($v)) { |
---|
56 | $output .= sprintf("\n%s%s: %s\n", $indent, $k, fancyDump($v, $indent . $indent)); |
---|
57 | } else { |
---|
58 | $output .= sprintf("%s%s: %s\n", $indent, $k, $v); |
---|
59 | } |
---|
60 | } |
---|
61 | } else { |
---|
62 | $output .= sprintf("%s%s\n", $indent, $var); |
---|
63 | } |
---|
64 | return $output; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Returns text with appropriate html translations. |
---|
69 | * |
---|
70 | * @param string $txt Text to clean. |
---|
71 | * @param bool $preserve_html If set to true, oTxt will not translage <, >, ", or ' |
---|
72 | * characters into HTML entities. This allows HTML to pass |
---|
73 | * through unmunged. |
---|
74 | * |
---|
75 | * @return string Cleaned text. |
---|
76 | */ |
---|
77 | function oTxt($txt, $preserve_html=false) |
---|
78 | { |
---|
79 | global $CFG; |
---|
80 | |
---|
81 | $search = array(); |
---|
82 | $replace = array(); |
---|
83 | |
---|
84 | // Make converted ampersand entities into normal ampersands (they will be done manually later) to retain HTML entities. |
---|
85 | $search['retain_ampersand'] = '/&/'; |
---|
86 | $replace['retain_ampersand'] = '&'; |
---|
87 | |
---|
88 | if ($preserve_html) { |
---|
89 | // Convert characters that must remain non-entities for displaying HTML. |
---|
90 | $search['retain_left_angle'] = '/</'; |
---|
91 | $replace['retain_left_angle'] = '<'; |
---|
92 | |
---|
93 | $search['retain_right_angle'] = '/>/'; |
---|
94 | $replace['retain_right_angle'] = '>'; |
---|
95 | |
---|
96 | $search['retain_single_quote'] = '/'/'; |
---|
97 | $replace['retain_single_quote'] = "'"; |
---|
98 | |
---|
99 | $search['retain_double_quote'] = '/"/'; |
---|
100 | $replace['retain_double_quote'] = '"'; |
---|
101 | } |
---|
102 | |
---|
103 | // & becomes & |
---|
104 | $search['ampersand'] = '/&((?![\w\d#]{1,10};))/'; |
---|
105 | $replace['ampersand'] = '&\\1'; |
---|
106 | |
---|
107 | return preg_replace($search, $replace, htmlentities($txt, ENT_QUOTES, $CFG->character_set)); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Returns text with stylistic modifications. |
---|
112 | * |
---|
113 | * @param string $txt Text to clean. |
---|
114 | * |
---|
115 | * @return string Cleaned text. |
---|
116 | */ |
---|
117 | function fancyTxt($txt) |
---|
118 | { |
---|
119 | global $CFG; |
---|
120 | |
---|
121 | $search = array(); |
---|
122 | $replace = array(); |
---|
123 | |
---|
124 | // "double quoted text" becomes “double quoted text” |
---|
125 | $search['double_quotes'] = '/(^|[^\w=])(?:"|"|"|"|“)([^"]+?)(?:"|"|"|"|”)([^\w]|$)/'; // " is the same as " and " and " |
---|
126 | $replace['double_quotes'] = '\\1“\\2”\\3'; |
---|
127 | |
---|
128 | // text's apostrophes become text’s apostrophes |
---|
129 | $search['apostrophe'] = '/(\w)(?:\'|')(\w)/'; |
---|
130 | $replace['apostrophe'] = '\\1’\\2'; |
---|
131 | |
---|
132 | // "single quoted text" becomes ‘single quoted text’ |
---|
133 | $search['single_quotes'] = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/'; |
---|
134 | $replace['single_quotes'] = '\\1‘\\2’\\3'; |
---|
135 | |
---|
136 | // em--dashes become em—dashes |
---|
137 | $search['em_dash'] = '/(\s*[^!<-])--([^>-]\s*)/'; |
---|
138 | $replace['em_dash'] = '\\1—\\2'; |
---|
139 | |
---|
140 | // & becomes & |
---|
141 | $search['ampersand'] = '/&((?![\w\d#]{1,10};))/'; |
---|
142 | $replace['ampersand'] = '&\\1'; |
---|
143 | |
---|
144 | return preg_replace($search, $replace, $txt); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | * Generates a hexadecibal html color based on provided word. |
---|
150 | * |
---|
151 | * @access public |
---|
152 | * |
---|
153 | * @param string $text A string for which to convert to color. |
---|
154 | * |
---|
155 | * @return string A hexadecimal html color. |
---|
156 | */ |
---|
157 | function getTextColor($text, $method=1) |
---|
158 | { |
---|
159 | $r = substr(md5($text), 0, 1); |
---|
160 | $g = substr(md5($text), 1, 1); |
---|
161 | $b = substr(md5($text), 2, 1); |
---|
162 | |
---|
163 | switch ($method) { |
---|
164 | case 2 : |
---|
165 | if (hexdec($r) > hexdec('c')) { |
---|
166 | $r = dechex(hexdec('f') - hexdec($r)); |
---|
167 | } |
---|
168 | if (hexdec($g) > hexdec('c')) { |
---|
169 | $g = dechex(hexdec('f') - hexdec($g)); |
---|
170 | } |
---|
171 | if (hexdec($b) > hexdec('c')) { |
---|
172 | $b = dechex(hexdec('f') - hexdec($b)); |
---|
173 | } |
---|
174 | break; |
---|
175 | |
---|
176 | case 1 : |
---|
177 | default : |
---|
178 | $r = dechex(round(hexdec($r) * .8)); |
---|
179 | $g = dechex(round(hexdec($g) * .8)); |
---|
180 | $b = dechex(round(hexdec($b) * .6)); |
---|
181 | break; |
---|
182 | } |
---|
183 | |
---|
184 | return $r . $r . $g . $g . $b . $b; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Encodes a string into unicode values 128-255. |
---|
189 | * Useful for hiding an email address from spambots. |
---|
190 | * |
---|
191 | * @access public |
---|
192 | * |
---|
193 | * @param string $text A line of text to encode. |
---|
194 | * |
---|
195 | * @return string Encoded text. |
---|
196 | */ |
---|
197 | function encodeAscii($text) |
---|
198 | { |
---|
199 | $ouput = ''; |
---|
200 | $num = strlen($text); |
---|
201 | for ($i=0; $i<$num; $i++) { |
---|
202 | $output .= sprintf('&#%03s', ord($text{$i})); |
---|
203 | } |
---|
204 | return $output; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Return a human readable filesize. |
---|
209 | * |
---|
210 | * @param int $size Size |
---|
211 | * @param int $unit The maximum unit |
---|
212 | * @param int $format The return string format |
---|
213 | * @author Aidan Lister <aidan@php.net> |
---|
214 | * @version 1.1.0 |
---|
215 | */ |
---|
216 | function humanFileSize($size, $unit=null, $format='%01.2f %s') |
---|
217 | { |
---|
218 | // Units |
---|
219 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
---|
220 | $ii = count($units) - 1; |
---|
221 | |
---|
222 | // Max unit |
---|
223 | $unit = array_search((string) $unit, $units); |
---|
224 | if ($unit === null || $unit === false) { |
---|
225 | $unit = $ii; |
---|
226 | } |
---|
227 | |
---|
228 | // Loop |
---|
229 | $i = 0; |
---|
230 | while ($unit != $i && $size >= 1024 && $i < $ii) { |
---|
231 | $size /= 1024; |
---|
232 | $i++; |
---|
233 | } |
---|
234 | |
---|
235 | return sprintf($format, $size, $units[$i]); |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * If $var is net set or null, set it to $default. Otherwise leave it alone. |
---|
240 | * Returns the final value of $var. Use to find a default value of one is not avilable. |
---|
241 | * |
---|
242 | * @param mixed $var The variable that is being set. |
---|
243 | * @param mixed $default What to set it to if $val is not currently set. |
---|
244 | * @return mixed The resulting value of $var. |
---|
245 | */ |
---|
246 | function setDefault(&$var, $default='') |
---|
247 | { |
---|
248 | if (!isset($var)) { |
---|
249 | $var = $default; |
---|
250 | } |
---|
251 | return $var; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Like preg_quote() except for arrays, it takes an array of strings and puts |
---|
256 | * a backslash in front of every character that is part of the regular |
---|
257 | * expression syntax. |
---|
258 | * |
---|
259 | * @param array $array input array |
---|
260 | * @param array $delim optional character that will also be excaped. |
---|
261 | * |
---|
262 | * @return array an array with the same values as $array1 but shuffled |
---|
263 | */ |
---|
264 | function pregQuoteArray($array, $delim='/') |
---|
265 | { |
---|
266 | if (!empty($array)) { |
---|
267 | if (is_array($array)) { |
---|
268 | foreach ($array as $key=>$val) { |
---|
269 | $quoted_array[$key] = preg_quote($val, $delim); |
---|
270 | } |
---|
271 | return $quoted_array; |
---|
272 | } else { |
---|
273 | return preg_quote($array, $delim); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * Converts a PHP Array into encoded URL arguments and return them as an array. |
---|
280 | * |
---|
281 | * @param mixed $data An array to transverse recursivly, or a string |
---|
282 | * to use directly to create url arguments. |
---|
283 | * @param string $prefix The name of the first dimension of the array. |
---|
284 | * If not specified, the first keys of the array will be used. |
---|
285 | * |
---|
286 | * @return array URL with array elements as URL key=value arguments. |
---|
287 | */ |
---|
288 | function urlEncodeArray($data, $prefix='', $_return=true) { |
---|
289 | |
---|
290 | // Data is stored in static variable. |
---|
291 | static $args; |
---|
292 | |
---|
293 | if (is_array($data)) { |
---|
294 | foreach ($data as $key => $val) { |
---|
295 | // If the prefix is empty, use the $key as the name of the first dimention of the "array". |
---|
296 | // ...otherwise, append the key as a new dimention of the "array". |
---|
297 | $new_prefix = ('' == $prefix) ? urlencode($key) : $prefix . '[' . urlencode($key) . ']'; |
---|
298 | // Enter recursion. |
---|
299 | urlEncodeArray($val, $new_prefix, false); |
---|
300 | } |
---|
301 | } else { |
---|
302 | // We've come to the last dimention of the array, save the "array" and it's value. |
---|
303 | $args[$prefix] = urlencode($data); |
---|
304 | } |
---|
305 | |
---|
306 | if ($_return) { |
---|
307 | // This is not a recursive execution. All recursion is complete. |
---|
308 | // Reset static var and return the result. |
---|
309 | $ret = $args; |
---|
310 | $args = array(); |
---|
311 | return is_array($ret) ? $ret : array(); |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * Converts a PHP Array into encoded URL arguments and return them in a string. |
---|
317 | * |
---|
318 | * @param mixed $data An array to transverse recursivly, or a string |
---|
319 | * to use directly to create url arguments. |
---|
320 | * @param string $prefix The name of the first dimention of the array. |
---|
321 | * If not specified, the first keys of the array will be used. |
---|
322 | * |
---|
323 | * @return string url A string ready to append to a url. |
---|
324 | */ |
---|
325 | function urlEncodeArrayToString($data, $prefix='') { |
---|
326 | |
---|
327 | $array_args = urlEncodeArray($data, $prefix); |
---|
328 | $url_args = ''; |
---|
329 | $delim = ''; |
---|
330 | foreach ($array_args as $key=>$val) { |
---|
331 | $url_args .= $delim . $key . '=' . $val; |
---|
332 | $delim = ini_get('arg_separator.output'); |
---|
333 | } |
---|
334 | return $url_args; |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * An alternative to "shuffle()" that actually works. |
---|
339 | * |
---|
340 | * @param array $array1 input array |
---|
341 | * @param array $array2 argument used internally through recursion |
---|
342 | * |
---|
343 | * @return array an array with the same values as $array1 but shuffled |
---|
344 | */ |
---|
345 | function arrayRand(&$array1, $array2 = array()) |
---|
346 | { |
---|
347 | if (!sizeof($array1)) { |
---|
348 | return array(); |
---|
349 | } |
---|
350 | |
---|
351 | srand((double) microtime() * 10000000); |
---|
352 | $rand = array_rand($array1); |
---|
353 | |
---|
354 | $array2[] = $array1[$rand]; |
---|
355 | unset ($array1[$rand]); |
---|
356 | |
---|
357 | return $array2 + arrayRand($array1, $array2); |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * Fills an arrray with the result from a multiple ereg search. |
---|
362 | * Curtesy of Bruno - rbronosky@mac.com - 10-May-2001 |
---|
363 | * Blame him for the funky do...while loop. |
---|
364 | * |
---|
365 | * @param mixed $pattern regular expression needle |
---|
366 | * @param mixed $string haystack |
---|
367 | * |
---|
368 | * @return array populated with each found result |
---|
369 | */ |
---|
370 | function eregAll($pattern, $string) |
---|
371 | { |
---|
372 | do { |
---|
373 | if (!ereg($pattern, $string, $temp)) { |
---|
374 | continue; |
---|
375 | } |
---|
376 | $string = str_replace($temp[0], '', $string); |
---|
377 | $results[] = $temp; |
---|
378 | } while (ereg($pattern, $string, $temp)); |
---|
379 | return $results; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Prints the word "checked" if a variable is set, and optionally matches |
---|
384 | * the desired value, otherwise prints nothing, |
---|
385 | * used for printing the word "checked" in a checkbox form input. |
---|
386 | * |
---|
387 | * @param mixed $var the variable to compare |
---|
388 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
389 | */ |
---|
390 | function frmChecked($var, $value=null) |
---|
391 | { |
---|
392 | if (func_num_args() == 1 && $var) { |
---|
393 | // 'Checked' if var is true. |
---|
394 | echo ' checked="checked" '; |
---|
395 | } else if (func_num_args() == 2 && $var == $value) { |
---|
396 | // 'Checked' if var and value match. |
---|
397 | echo ' checked="checked" '; |
---|
398 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
399 | // 'Checked' if the value is in the key or the value of an array. |
---|
400 | if (isset($var[$value])) { |
---|
401 | echo ' checked="checked" '; |
---|
402 | } else if (in_array($value, $var)) { |
---|
403 | echo ' checked="checked" '; |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * prints the word "selected" if a variable is set, and optionally matches |
---|
410 | * the desired value, otherwise prints nothing, |
---|
411 | * otherwise prints nothing, used for printing the word "checked" in a |
---|
412 | * select form input |
---|
413 | * |
---|
414 | * @param mixed $var the variable to compare |
---|
415 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
416 | */ |
---|
417 | function frmSelected($var, $value=null) |
---|
418 | { |
---|
419 | if (func_num_args() == 1 && $var) { |
---|
420 | // 'selected' if var is true. |
---|
421 | echo ' selected="selected" '; |
---|
422 | } else if (func_num_args() == 2 && $var == $value) { |
---|
423 | // 'selected' if var and value match. |
---|
424 | echo ' selected="selected" '; |
---|
425 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
426 | // 'selected' if the value is in the key or the value of an array. |
---|
427 | if (isset($var[$value])) { |
---|
428 | echo ' selected="selected" '; |
---|
429 | } else if (in_array($value, $var)) { |
---|
430 | echo ' selected="selected" '; |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * Adds slashes to values of an array and converts the array to a |
---|
437 | * comma delimited list. If value provided is not an array or is empty |
---|
438 | * return nothing. This is useful for putting values coming in from |
---|
439 | * posted checkboxes into a SET column of a database. |
---|
440 | * |
---|
441 | * @param array $array Array to convert. |
---|
442 | * @return string Comma list of array values. |
---|
443 | */ |
---|
444 | function dbArrayToList($array) |
---|
445 | { |
---|
446 | if (is_array($array) && !empty($array)) { |
---|
447 | return join(',', array_map('addslashes', array_keys($array))); |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | /** |
---|
452 | * Converts a human string date into a SQL-safe date. |
---|
453 | * Dates nearing infinity use the date 2038-01-01 so conversion to unix time |
---|
454 | * format remain within valid range. |
---|
455 | * |
---|
456 | * @param array $date String date to convert. |
---|
457 | * @param array $format Date format to pass to date(). |
---|
458 | * Default produces MySQL datetime: 0000-00-00 00:00:00. |
---|
459 | * |
---|
460 | * @return string SQL-safe date. |
---|
461 | */ |
---|
462 | function strToSQLDate($date, $format='Y-m-d H:i:s') |
---|
463 | { |
---|
464 | // Translate the human string date into SQL-safe date format. |
---|
465 | if (empty($date) || '0000-00-00' == $date || strtotime($date) === -1) { |
---|
466 | $sql_date = '0000-00-00'; |
---|
467 | } else { |
---|
468 | $sql_date = date($format, strtotime($date)); |
---|
469 | } |
---|
470 | |
---|
471 | return $sql_date; |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * If magic_quotes_gpc is in use, run stripslashes() on $var. If $var is an |
---|
476 | * array, stripslashes is run on each value, recursivly, and the stripped |
---|
477 | * array is returned |
---|
478 | * |
---|
479 | * @param mixed $var The string or array to un-quote, if necessary. |
---|
480 | * @return mixed $var, minus any magic quotes. |
---|
481 | */ |
---|
482 | function dispelMagicQuotes($var) |
---|
483 | { |
---|
484 | static $magic_quotes_gpc; |
---|
485 | |
---|
486 | if (!isset($magic_quotes_gpc)) { |
---|
487 | $magic_quotes_gpc = get_magic_quotes_gpc(); |
---|
488 | } |
---|
489 | |
---|
490 | if ($magic_quotes_gpc) { |
---|
491 | if (!is_array($var)) { |
---|
492 | $var = stripslashes($var); |
---|
493 | } else { |
---|
494 | foreach ($var as $key=>$val) { |
---|
495 | if (is_array($val)) { |
---|
496 | $var[$key] = dispelMagicQuotes($val); |
---|
497 | } else { |
---|
498 | $var[$key] = stripslashes($val); |
---|
499 | } |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | return $var; |
---|
504 | } |
---|
505 | |
---|
506 | /** |
---|
507 | * Get a form variable from GET or POST data, stripped of magic |
---|
508 | * quotes if necessary. |
---|
509 | * |
---|
510 | * @param string $var (optional) The name of the form variable to look for. |
---|
511 | * @param string $default (optional) The value to return if the |
---|
512 | * variable is not there. |
---|
513 | * |
---|
514 | * @return mixed A cleaned GET or POST if no $var specified. |
---|
515 | * @return string A cleaned form $var if found, or $default. |
---|
516 | */ |
---|
517 | function getFormData($var=null, $default=null) |
---|
518 | { |
---|
519 | if ('POST' == $_SERVER['REQUEST_METHOD'] && is_null($var)) { |
---|
520 | return dispelMagicQuotes($_POST); |
---|
521 | } else if ('GET' == $_SERVER['REQUEST_METHOD'] && is_null($var)) { |
---|
522 | return dispelMagicQuotes($_GET); |
---|
523 | } |
---|
524 | if (isset($_POST[$var])) { |
---|
525 | return dispelMagicQuotes($_POST[$var]); |
---|
526 | } else if (isset($_GET[$var])) { |
---|
527 | return dispelMagicQuotes($_GET[$var]); |
---|
528 | } else { |
---|
529 | return $default; |
---|
530 | } |
---|
531 | } |
---|
532 | function getPost($var=null, $default=null) |
---|
533 | { |
---|
534 | if (is_null($var)) { |
---|
535 | return dispelMagicQuotes($_POST); |
---|
536 | } |
---|
537 | if (isset($_POST[$var])) { |
---|
538 | return dispelMagicQuotes($_POST[$var]); |
---|
539 | } else { |
---|
540 | return $default; |
---|
541 | } |
---|
542 | } |
---|
543 | function getGet($var=null, $default=null) |
---|
544 | { |
---|
545 | if (is_null($var)) { |
---|
546 | return dispelMagicQuotes($_GET); |
---|
547 | } |
---|
548 | if (isset($_GET[$var])) { |
---|
549 | return dispelMagicQuotes($_GET[$var]); |
---|
550 | } else { |
---|
551 | return $default; |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | /** |
---|
556 | * Signs a value using md5 and a simple text key. In order for this |
---|
557 | * function to be useful (i.e. secure) the key must be kept secret, which |
---|
558 | * means keeping it as safe as database credentials. Putting it into an |
---|
559 | * environment variable set in httpd.conf is a good place. |
---|
560 | * |
---|
561 | * @access public |
---|
562 | * |
---|
563 | * @param string $val The string to sign. |
---|
564 | * @param string $key (Optional) A text key to use for computing the signature. |
---|
565 | * |
---|
566 | * @return string The original value with a signature appended. |
---|
567 | */ |
---|
568 | function addSignature($val, $key=null) |
---|
569 | { |
---|
570 | global $CFG; |
---|
571 | |
---|
572 | if ('' == $val) { |
---|
573 | logMsg(sprintf('Adding signature to empty string.', null), LOG_NOTICE, __FILE__, __LINE__); |
---|
574 | } |
---|
575 | |
---|
576 | if (!isset($key)) { |
---|
577 | $key = $CFG->signing_key; |
---|
578 | } |
---|
579 | |
---|
580 | return $val . '-' . substr(md5($val . $key), 0, 18); |
---|
581 | } |
---|
582 | |
---|
583 | /** |
---|
584 | * Strips off the signature appended by addSignature(). |
---|
585 | * |
---|
586 | * @access public |
---|
587 | * |
---|
588 | * @param string $signed_val The string to sign. |
---|
589 | * |
---|
590 | * @return string The original value with a signature removed. |
---|
591 | */ |
---|
592 | function removeSignature($signed_val) |
---|
593 | { |
---|
594 | return substr($signed_val, 0, strrpos($signed_val, '-')); |
---|
595 | } |
---|
596 | |
---|
597 | |
---|
598 | /** |
---|
599 | * Verifies a signature appened to a value by addSignature(). |
---|
600 | * |
---|
601 | * @access public |
---|
602 | * |
---|
603 | * @param string $signed_val A value with appended signature. |
---|
604 | * @param string $key (Optional) A text key to use for computing the signature. |
---|
605 | * |
---|
606 | * @return bool True if the signature matches the var. |
---|
607 | */ |
---|
608 | function verifySignature($signed_val, $key=null) |
---|
609 | { |
---|
610 | // Strip the value from the signed value. |
---|
611 | $val = substr($signed_val, 0, strrpos($signed_val, '-')); |
---|
612 | // If the signed value matches the original signed value we consider the value safe. |
---|
613 | if ($signed_val == addSignature($val, $key)) { |
---|
614 | // Signature verified. |
---|
615 | return true; |
---|
616 | } else { |
---|
617 | return false; |
---|
618 | } |
---|
619 | } |
---|
620 | |
---|
621 | /** |
---|
622 | * Stub functions used when installation does not have |
---|
623 | * GNU gettext extension installed |
---|
624 | */ |
---|
625 | if (!extension_loaded('gettext')) { |
---|
626 | /** |
---|
627 | * Translates text |
---|
628 | * |
---|
629 | * @access public |
---|
630 | * @param string $text the text to be translated |
---|
631 | * @return string translated text |
---|
632 | */ |
---|
633 | function gettext($text) { |
---|
634 | return $text; |
---|
635 | } |
---|
636 | |
---|
637 | /** |
---|
638 | * Translates text |
---|
639 | * |
---|
640 | * @access public |
---|
641 | * @param string $text the text to be translated |
---|
642 | * @return string translated text |
---|
643 | */ |
---|
644 | function _($text) { |
---|
645 | return $text; |
---|
646 | } |
---|
647 | |
---|
648 | /** |
---|
649 | * Translates text by domain |
---|
650 | * |
---|
651 | * @access public |
---|
652 | * @param string $domain the language to translate the text into |
---|
653 | * @param string $text the text to be translated |
---|
654 | * @return string translated text |
---|
655 | */ |
---|
656 | function dgettext($domain, $text) { |
---|
657 | return $text; |
---|
658 | } |
---|
659 | |
---|
660 | /** |
---|
661 | * Translates text by domain and category |
---|
662 | * |
---|
663 | * @access public |
---|
664 | * @param string $domain the language to translate the text into |
---|
665 | * @param string $text the text to be translated |
---|
666 | * @param string $category the language dialect to use |
---|
667 | * @return string translated text |
---|
668 | */ |
---|
669 | function dcgettext($domain, $text, $category) { |
---|
670 | return $text; |
---|
671 | } |
---|
672 | |
---|
673 | /** |
---|
674 | * Binds the text domain |
---|
675 | * |
---|
676 | * @access public |
---|
677 | * @param string $domain the language to translate the text into |
---|
678 | * @param string |
---|
679 | * @return string translated text |
---|
680 | */ |
---|
681 | function bindtextdomain($domain, $directory) { |
---|
682 | return $domain; |
---|
683 | } |
---|
684 | |
---|
685 | /** |
---|
686 | * Sets the text domain |
---|
687 | * |
---|
688 | * @access public |
---|
689 | * @param string $domain the language to translate the text into |
---|
690 | * @return string translated text |
---|
691 | */ |
---|
692 | function textdomain($domain) { |
---|
693 | return $domain; |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | /** |
---|
698 | * Sends empty output to the browser and flushes the php buffer so the client |
---|
699 | * will see data before the page is finished processing. |
---|
700 | */ |
---|
701 | function flushBuffer() { |
---|
702 | echo str_repeat(' ', 205); |
---|
703 | flush(); |
---|
704 | } |
---|
705 | |
---|
706 | /** |
---|
707 | * Adds email address to mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
708 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
709 | * |
---|
710 | * @access public |
---|
711 | * |
---|
712 | * @param string $email Email address to add. |
---|
713 | * @param string $list Name of list to add to. |
---|
714 | * @param bool $send_welcome_message True to send welcome message to subscriber. |
---|
715 | * |
---|
716 | * @return bool True on success, false on failure. |
---|
717 | */ |
---|
718 | function mailmanAddMember($email, $list, $send_welcome_message=false) |
---|
719 | { |
---|
720 | $add_members = '/var/mailman/bin/add_members'; |
---|
721 | if (is_executable($add_members) && is_readable($add_members)) { |
---|
722 | $welcome_msg = $send_welcome_message ? 'y' : 'n'; |
---|
723 | 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); |
---|
724 | if (0 == $return_code) { |
---|
725 | logMsg(sprintf('Mailman add member success for list: %s, user: %s', $list, $email, $stdout), LOG_INFO, __FILE__, __LINE__); |
---|
726 | return true; |
---|
727 | } else { |
---|
728 | logMsg(sprintf('Mailman add member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
729 | return false; |
---|
730 | } |
---|
731 | } else { |
---|
732 | logMsg(sprintf('Mailman add member program not executable: %s', $add_members), LOG_ALERT, __FILE__, __LINE__); |
---|
733 | return false; |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | /** |
---|
738 | * Removes email address from mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
739 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
740 | * |
---|
741 | * @access public |
---|
742 | * |
---|
743 | * @param string $email Email address to add. |
---|
744 | * @param string $list Name of list to add to. |
---|
745 | * @param bool $send_user_ack True to send goodbye message to subscriber. |
---|
746 | * |
---|
747 | * @return bool True on success, false on failure. |
---|
748 | */ |
---|
749 | function mailmanRemoveMember($email, $list, $send_user_ack=false) |
---|
750 | { |
---|
751 | $remove_members = '/var/mailman/bin/remove_members'; |
---|
752 | if (is_executable($remove_members) && is_readable($remove_members)) { |
---|
753 | $userack = $send_user_ack ? '' : '--nouserack'; |
---|
754 | exec(sprintf('/usr/bin/sudo %s %s --noadminack %s %s', escapeshellarg($remove_members), $userack, escapeshellarg($list), escapeshellarg($email)), $stdout, $return_code); |
---|
755 | if (0 == $return_code) { |
---|
756 | logMsg(sprintf('Mailman remove member success for list: %s, user: %s', $list, $email, $stdout), LOG_INFO, __FILE__, __LINE__); |
---|
757 | return true; |
---|
758 | } else { |
---|
759 | logMsg(sprintf('Mailman remove member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
760 | return false; |
---|
761 | } |
---|
762 | } else { |
---|
763 | logMsg(sprintf('Mailman remove member program not executable: %s', $remove_members), LOG_ALERT, __FILE__, __LINE__); |
---|
764 | return false; |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | |
---|
769 | |
---|
770 | ?> |
---|