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