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