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