[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 | * |
---|
[331] | 29 | * @param mixed $var Variable to dump. |
---|
| 30 | * @param bool $serialize Remove line-endings. Useful for logging variables. |
---|
[1] | 31 | * @return string Dump of var. |
---|
| 32 | */ |
---|
[331] | 33 | function getDump($var, $serialize=false) |
---|
[1] | 34 | { |
---|
| 35 | ob_start(); |
---|
| 36 | print_r($var); |
---|
| 37 | $d = ob_get_contents(); |
---|
| 38 | ob_end_clean(); |
---|
[331] | 39 | return $serialize ? preg_replace('/\s+/m', '', $d) : $d; |
---|
[1] | 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Return dump as cleaned text. Useful for dumping data into emails. |
---|
| 44 | * |
---|
[336] | 45 | * @param array $var Variable to dump. |
---|
[248] | 46 | * @param strong $indent A string to prepend indented lines (tab for example). |
---|
[1] | 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) { |
---|
[247] | 54 | $k = ucfirst(mb_strtolower(str_replace(array('_', ' '), ' ', $k))); |
---|
[1] | 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 | /** |
---|
[42] | 68 | * Returns text with appropriate html translations. |
---|
[1] | 69 | * |
---|
[257] | 70 | * @param string $text Text to clean. |
---|
[334] | 71 | * @param bool $preserve_html If set to true, oTxt will not translate <, >, ", or ' |
---|
| 72 | * characters into HTML entities. This allows HTML to pass through unmunged. |
---|
[1] | 73 | * @return string Cleaned text. |
---|
| 74 | */ |
---|
[257] | 75 | function oTxt($text, $preserve_html=false) |
---|
[1] | 76 | { |
---|
[136] | 77 | $app =& App::getInstance(); |
---|
| 78 | |
---|
[1] | 79 | $search = array(); |
---|
| 80 | $replace = array(); |
---|
| 81 | |
---|
| 82 | // Make converted ampersand entities into normal ampersands (they will be done manually later) to retain HTML entities. |
---|
| 83 | $search['retain_ampersand'] = '/&/'; |
---|
| 84 | $replace['retain_ampersand'] = '&'; |
---|
| 85 | |
---|
| 86 | if ($preserve_html) { |
---|
| 87 | // Convert characters that must remain non-entities for displaying HTML. |
---|
| 88 | $search['retain_left_angle'] = '/</'; |
---|
| 89 | $replace['retain_left_angle'] = '<'; |
---|
[42] | 90 | |
---|
[1] | 91 | $search['retain_right_angle'] = '/>/'; |
---|
| 92 | $replace['retain_right_angle'] = '>'; |
---|
[42] | 93 | |
---|
[1] | 94 | $search['retain_single_quote'] = '/'/'; |
---|
| 95 | $replace['retain_single_quote'] = "'"; |
---|
[42] | 96 | |
---|
[1] | 97 | $search['retain_double_quote'] = '/"/'; |
---|
| 98 | $replace['retain_double_quote'] = '"'; |
---|
| 99 | } |
---|
| 100 | |
---|
[334] | 101 | // & becomes &. Exclude any occurrence where the & is followed by a alphanum or unicode character. |
---|
[32] | 102 | $search['ampersand'] = '/&(?![\w\d#]{1,10};)/'; |
---|
| 103 | $replace['ampersand'] = '&'; |
---|
[1] | 104 | |
---|
[334] | 105 | return preg_replace($search, $replace, htmlspecialchars($text, ENT_QUOTES, $app->getParam('character_set'))); |
---|
[1] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
[334] | 109 | * Returns text with stylistic modifications. Warning: this will break some HTML attributes! |
---|
[320] | 110 | * TODO: Allow a string such as this to be passed: <a href="javascript:openPopup('/foo/bar.php')">Click here</a> |
---|
[1] | 111 | * |
---|
[257] | 112 | * @param string $text Text to clean. |
---|
[1] | 113 | * @return string Cleaned text. |
---|
| 114 | */ |
---|
[257] | 115 | function fancyTxt($text) |
---|
[1] | 116 | { |
---|
[103] | 117 | $search = array(); |
---|
| 118 | $replace = array(); |
---|
| 119 | |
---|
| 120 | // "double quoted text" becomes “double quoted text” |
---|
| 121 | $search['double_quotes'] = '/(^|[^\w=])(?:"|"|"|"|“)([^"]+?)(?:"|"|"|"|”)([^\w]|$)/ms'; // " is the same as " and " and " |
---|
| 122 | $replace['double_quotes'] = '$1“$2”$3'; |
---|
| 123 | |
---|
| 124 | // text's apostrophes become text’s apostrophes |
---|
| 125 | $search['apostrophe'] = '/(\w)(?:\'|'|')(\w)/ms'; |
---|
| 126 | $replace['apostrophe'] = '$1’$2'; |
---|
| 127 | |
---|
| 128 | // 'single quoted text' becomes ‘single quoted text’ |
---|
| 129 | $search['single_quotes'] = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/ms'; |
---|
| 130 | $replace['single_quotes'] = '$1‘$2’$3'; |
---|
| 131 | |
---|
| 132 | // plural posessives' apostrophes become posessives’ |
---|
| 133 | $search['apostrophes'] = '/(s)(?:\'|'|')(\s)/ms'; |
---|
| 134 | $replace['apostrophes'] = '$1’$2'; |
---|
| 135 | |
---|
| 136 | // em--dashes become em—dashes |
---|
| 137 | $search['em_dash'] = '/(\s*[^!<-])--([^>-]\s*)/'; |
---|
| 138 | $replace['em_dash'] = '$1—$2'; |
---|
| 139 | |
---|
[257] | 140 | return preg_replace($search, $replace, $text); |
---|
[1] | 141 | } |
---|
| 142 | |
---|
[257] | 143 | /** |
---|
[334] | 144 | * Applies a class to search terms to highlight them ala google results. |
---|
[257] | 145 | * |
---|
| 146 | * @param string $text Input text to search. |
---|
| 147 | * @param string $search String of word(s) that will be highlighted. |
---|
| 148 | * @param string $class CSS class to apply. |
---|
| 149 | * @return string Text with searched words wrapped in <span>. |
---|
| 150 | */ |
---|
| 151 | function highlightWords($text, $search, $class='sc-highlightwords') |
---|
| 152 | { |
---|
| 153 | $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY); |
---|
| 154 | |
---|
| 155 | $search = array(); |
---|
| 156 | $replace = array(); |
---|
| 157 | |
---|
| 158 | foreach ($words as $w) { |
---|
[258] | 159 | if ('' != trim($w)) { |
---|
| 160 | $search[] = '/\b(' . preg_quote($w) . ')\b/i'; |
---|
| 161 | $replace[] = '<span class="' . $class . '">$1</span>'; |
---|
| 162 | } |
---|
[257] | 163 | } |
---|
[42] | 164 | |
---|
[258] | 165 | return empty($replace) ? $text : preg_replace($search, $replace, $text); |
---|
[257] | 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[1] | 169 | /** |
---|
[334] | 170 | * Generates a hexadecimal html color based on provided word. |
---|
[1] | 171 | * |
---|
| 172 | * @access public |
---|
| 173 | * @param string $text A string for which to convert to color. |
---|
| 174 | * @return string A hexadecimal html color. |
---|
| 175 | */ |
---|
| 176 | function getTextColor($text, $method=1) |
---|
| 177 | { |
---|
[235] | 178 | $hash = md5($text); |
---|
| 179 | $rgb = array( |
---|
[247] | 180 | mb_substr($hash, 0, 1), |
---|
| 181 | mb_substr($hash, 1, 1), |
---|
| 182 | mb_substr($hash, 2, 1), |
---|
| 183 | mb_substr($hash, 3, 1), |
---|
| 184 | mb_substr($hash, 4, 1), |
---|
| 185 | mb_substr($hash, 5, 1), |
---|
[235] | 186 | ); |
---|
[1] | 187 | |
---|
| 188 | switch ($method) { |
---|
[235] | 189 | case 1 : |
---|
| 190 | default : |
---|
[334] | 191 | // Reduce all hex values slightly to avoid all white. |
---|
[235] | 192 | array_walk($rgb, create_function('&$v', '$v = dechex(round(hexdec($v) * 0.87));')); |
---|
| 193 | break; |
---|
[1] | 194 | case 2 : |
---|
[235] | 195 | foreach ($rgb as $i => $v) { |
---|
| 196 | if (hexdec($v) > hexdec('c')) { |
---|
| 197 | $rgb[$i] = dechex(hexdec('f') - hexdec($v)); |
---|
| 198 | } |
---|
[1] | 199 | } |
---|
| 200 | break; |
---|
| 201 | } |
---|
| 202 | |
---|
[235] | 203 | return join('', $rgb); |
---|
[1] | 204 | } |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * Encodes a string into unicode values 128-255. |
---|
| 208 | * Useful for hiding an email address from spambots. |
---|
| 209 | * |
---|
| 210 | * @access public |
---|
| 211 | * @param string $text A line of text to encode. |
---|
| 212 | * @return string Encoded text. |
---|
| 213 | */ |
---|
| 214 | function encodeAscii($text) |
---|
| 215 | { |
---|
[255] | 216 | $output = ''; |
---|
[247] | 217 | $num = mb_strlen($text); |
---|
[1] | 218 | for ($i=0; $i<$num; $i++) { |
---|
| 219 | $output .= sprintf('&#%03s', ord($text{$i})); |
---|
| 220 | } |
---|
| 221 | return $output; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
[84] | 225 | * Encodes an email into a "user at domain dot com" format. |
---|
[9] | 226 | * |
---|
| 227 | * @access public |
---|
| 228 | * @param string $email An email to encode. |
---|
| 229 | * @param string $at Replaces the @. |
---|
| 230 | * @param string $dot Replaces the .. |
---|
| 231 | * @return string Encoded email. |
---|
| 232 | */ |
---|
[53] | 233 | function encodeEmail($email, $at=' at ', $dot=' dot ') |
---|
[9] | 234 | { |
---|
| 235 | $search = array('/@/', '/\./'); |
---|
| 236 | $replace = array($at, $dot); |
---|
| 237 | return preg_replace($search, $replace, $email); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
[84] | 241 | * Turns "a really long string" into "a rea...string" |
---|
| 242 | * |
---|
| 243 | * @access public |
---|
| 244 | * @param string $str Input string |
---|
| 245 | * @param int $len Maximum string length. |
---|
| 246 | * @param string $where Where to cut the string. One of: 'start', 'middle', or 'end'. |
---|
| 247 | * @return string Truncated output string |
---|
| 248 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 249 | * @since 29 Mar 2006 13:48:49 |
---|
| 250 | */ |
---|
[258] | 251 | function truncate($str, $len, $where='middle', $delim='…') |
---|
[84] | 252 | { |
---|
[247] | 253 | if ($len <= 3 || mb_strlen($str) <= 3) { |
---|
[240] | 254 | return ''; |
---|
| 255 | } |
---|
[84] | 256 | $part1 = floor(($len - 3) / 2); |
---|
| 257 | $part2 = ceil(($len - 3) / 2); |
---|
| 258 | switch ($where) { |
---|
| 259 | case 'start' : |
---|
[258] | 260 | return preg_replace(array(sprintf('/^.{4,}(.{%s})$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array($delim . '$1', $delim), $str); |
---|
[84] | 261 | break; |
---|
| 262 | default : |
---|
| 263 | case 'middle' : |
---|
[258] | 264 | return preg_replace(array(sprintf('/^(.{%s}).{4,}(.{%s})$/sU', $part1, $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim . '$2', $delim), $str); |
---|
[84] | 265 | break; |
---|
| 266 | case 'end' : |
---|
[258] | 267 | return preg_replace(array(sprintf('/^(.{%s}).{4,}$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim, $delim), $str); |
---|
[338] | 268 | break; |
---|
[84] | 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
[340] | 272 | /* |
---|
| 273 | * A substitution for the missing mb_ucfirst function. |
---|
| 274 | * |
---|
| 275 | * @access public |
---|
| 276 | * @param string $strong The string |
---|
| 277 | * @return string String with uper-cased first character. |
---|
| 278 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 279 | * @version 1.0 |
---|
| 280 | * @since 06 Dec 2008 17:04:01 |
---|
| 281 | */ |
---|
| 282 | if (!function_exists('mb_ucfirst')) { |
---|
| 283 | function mb_ucfirst($string) |
---|
| 284 | { |
---|
| 285 | return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1, mb_strlen($string)); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
[84] | 289 | /** |
---|
[338] | 290 | * Return a human readable disk space measurement. Input value measured in bytes. |
---|
[1] | 291 | * |
---|
[338] | 292 | * @param int $size Size in bytes. |
---|
[1] | 293 | * @param int $unit The maximum unit |
---|
| 294 | * @param int $format The return string format |
---|
| 295 | * @author Aidan Lister <aidan@php.net> |
---|
| 296 | * @version 1.1.0 |
---|
| 297 | */ |
---|
[338] | 298 | function humanFileSize($size, $format='%01.2f %s', $max_unit=null, $multiplier=1024) |
---|
[1] | 299 | { |
---|
| 300 | // Units |
---|
| 301 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
---|
| 302 | $ii = count($units) - 1; |
---|
[42] | 303 | |
---|
[1] | 304 | // Max unit |
---|
[154] | 305 | $max_unit = array_search((string) $max_unit, $units); |
---|
| 306 | if ($max_unit === null || $max_unit === false) { |
---|
| 307 | $max_unit = $ii; |
---|
[1] | 308 | } |
---|
[42] | 309 | |
---|
[1] | 310 | // Loop |
---|
| 311 | $i = 0; |
---|
[338] | 312 | while ($max_unit != $i && $size >= $multiplier && $i < $ii) { |
---|
| 313 | $size /= $multiplier; |
---|
[1] | 314 | $i++; |
---|
| 315 | } |
---|
[42] | 316 | |
---|
[1] | 317 | return sprintf($format, $size, $units[$i]); |
---|
| 318 | } |
---|
| 319 | |
---|
[180] | 320 | /* |
---|
[189] | 321 | * Returns a human readable amount of time for the given amount of seconds. |
---|
[180] | 322 | * |
---|
| 323 | * 45 seconds |
---|
| 324 | * 12 minutes |
---|
| 325 | * 3.5 hours |
---|
| 326 | * 2 days |
---|
| 327 | * 1 week |
---|
| 328 | * 4 months |
---|
| 329 | * |
---|
| 330 | * Months are calculated using the real number of days in a year: 365.2422 / 12. |
---|
| 331 | * |
---|
| 332 | * @access public |
---|
[189] | 333 | * @param int $seconds Seconds of time. |
---|
[180] | 334 | * @param string $max_unit Key value from the $units array. |
---|
| 335 | * @param string $format Sprintf formatting string. |
---|
| 336 | * @return string Value of units elapsed. |
---|
| 337 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 338 | * @version 1.0 |
---|
| 339 | * @since 23 Jun 2006 12:15:19 |
---|
| 340 | */ |
---|
[189] | 341 | function humanTime($seconds, $max_unit=null, $format='%01.1f') |
---|
[180] | 342 | { |
---|
[202] | 343 | // Units: array of seconds in the unit, singular and plural unit names. |
---|
[180] | 344 | $units = array( |
---|
| 345 | 'second' => array(1, _("second"), _("seconds")), |
---|
| 346 | 'minute' => array(60, _("minute"), _("minutes")), |
---|
| 347 | 'hour' => array(3600, _("hour"), _("hours")), |
---|
| 348 | 'day' => array(86400, _("day"), _("days")), |
---|
| 349 | 'week' => array(604800, _("week"), _("weeks")), |
---|
| 350 | 'month' => array(2629743.84, _("month"), _("months")), |
---|
| 351 | 'year' => array(31556926.08, _("year"), _("years")), |
---|
| 352 | 'decade' => array(315569260.8, _("decade"), _("decades")), |
---|
| 353 | ); |
---|
| 354 | |
---|
[202] | 355 | // Max unit to calculate. |
---|
[180] | 356 | $max_unit = isset($units[$max_unit]) ? $max_unit : 'decade'; |
---|
| 357 | |
---|
[189] | 358 | $final_time = $seconds; |
---|
[191] | 359 | $last_unit = 'second'; |
---|
[180] | 360 | foreach ($units as $k => $v) { |
---|
[189] | 361 | if ($max_unit != $k && $seconds >= $v[0]) { |
---|
| 362 | $final_time = $seconds / $v[0]; |
---|
[180] | 363 | $last_unit = $k; |
---|
| 364 | } |
---|
| 365 | } |
---|
[189] | 366 | $final_time = sprintf($format, $final_time); |
---|
| 367 | return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$last_unit][1] : $units[$last_unit][2])); |
---|
[180] | 368 | } |
---|
| 369 | |
---|
[1] | 370 | /** |
---|
[334] | 371 | * Tests the existence of a file anywhere in the include path. |
---|
[258] | 372 | * |
---|
| 373 | * @param string $file File in include path. |
---|
| 374 | * @return mixed False if file not found, the path of the file if it is found. |
---|
| 375 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 376 | * @since 03 Dec 2005 14:23:26 |
---|
| 377 | */ |
---|
| 378 | function fileExistsIncludePath($file) |
---|
| 379 | { |
---|
| 380 | $app =& App::getInstance(); |
---|
| 381 | |
---|
| 382 | foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { |
---|
| 383 | $fullpath = $path . DIRECTORY_SEPARATOR . $file; |
---|
| 384 | if (file_exists($fullpath)) { |
---|
| 385 | $app->logMsg(sprintf('Found file "%s" at path: %s', $file, $fullpath), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 386 | return $fullpath; |
---|
| 387 | } else { |
---|
| 388 | $app->logMsg(sprintf('File "%s" not found in include_path: %s', $file, get_include_path()), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 389 | return false; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | /** |
---|
[26] | 395 | * Returns stats of a file from the include path. |
---|
| 396 | * |
---|
| 397 | * @param string $file File in include path. |
---|
[258] | 398 | * @param mixed $stat Which statistic to return (or null to return all). |
---|
| 399 | * @return mixed Value of requested key from fstat(), or false on error. |
---|
[26] | 400 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 401 | * @since 03 Dec 2005 14:23:26 |
---|
| 402 | */ |
---|
[241] | 403 | function statIncludePath($file, $stat=null) |
---|
[26] | 404 | { |
---|
| 405 | // Open file pointer read-only using include path. |
---|
| 406 | if ($fp = fopen($file, 'r', true)) { |
---|
[258] | 407 | // File opened successfully, get stats. |
---|
[26] | 408 | $stats = fstat($fp); |
---|
| 409 | fclose($fp); |
---|
| 410 | // Return specified stats. |
---|
[241] | 411 | return is_null($stat) ? $stats : $stats[$stat]; |
---|
[26] | 412 | } else { |
---|
| 413 | return false; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
[330] | 417 | /* |
---|
| 418 | * Writes content to the specified file. This function emulates the functionality of file_put_contents from PHP 5. |
---|
| 419 | * |
---|
| 420 | * @access public |
---|
| 421 | * @param string $filename Path to file. |
---|
| 422 | * @param string $content Data to write into file. |
---|
| 423 | * @return bool Success or failure. |
---|
| 424 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 425 | * @since 11 Apr 2006 22:48:30 |
---|
| 426 | */ |
---|
| 427 | function filePutContents($filename, $content) |
---|
| 428 | { |
---|
| 429 | $app =& App::getInstance(); |
---|
| 430 | |
---|
| 431 | // Open file for writing and truncate to zero length. |
---|
| 432 | if ($fp = fopen($filename, 'w')) { |
---|
| 433 | if (flock($fp, LOCK_EX)) { |
---|
| 434 | if (!fwrite($fp, $content, mb_strlen($content))) { |
---|
| 435 | $app->logMsg(sprintf('Failed writing to file: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 436 | fclose($fp); |
---|
| 437 | return false; |
---|
| 438 | } |
---|
| 439 | flock($fp, LOCK_UN); |
---|
| 440 | } else { |
---|
| 441 | $app->logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 442 | fclose($fp); |
---|
| 443 | return false; |
---|
| 444 | } |
---|
| 445 | fclose($fp); |
---|
| 446 | // Success! |
---|
| 447 | $app->logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 448 | return true; |
---|
| 449 | } else { |
---|
| 450 | $app->logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 451 | return false; |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | |
---|
[26] | 456 | /** |
---|
[1] | 457 | * If $var is net set or null, set it to $default. Otherwise leave it alone. |
---|
[334] | 458 | * Returns the final value of $var. Use to find a default value of one is not available. |
---|
[1] | 459 | * |
---|
| 460 | * @param mixed $var The variable that is being set. |
---|
| 461 | * @param mixed $default What to set it to if $val is not currently set. |
---|
[42] | 462 | * @return mixed The resulting value of $var. |
---|
[1] | 463 | */ |
---|
| 464 | function setDefault(&$var, $default='') |
---|
| 465 | { |
---|
| 466 | if (!isset($var)) { |
---|
| 467 | $var = $default; |
---|
| 468 | } |
---|
| 469 | return $var; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | /** |
---|
| 473 | * Like preg_quote() except for arrays, it takes an array of strings and puts |
---|
| 474 | * a backslash in front of every character that is part of the regular |
---|
| 475 | * expression syntax. |
---|
| 476 | * |
---|
| 477 | * @param array $array input array |
---|
[334] | 478 | * @param array $delim optional character that will also be escaped. |
---|
[1] | 479 | * @return array an array with the same values as $array1 but shuffled |
---|
| 480 | */ |
---|
| 481 | function pregQuoteArray($array, $delim='/') |
---|
| 482 | { |
---|
| 483 | if (!empty($array)) { |
---|
| 484 | if (is_array($array)) { |
---|
| 485 | foreach ($array as $key=>$val) { |
---|
| 486 | $quoted_array[$key] = preg_quote($val, $delim); |
---|
| 487 | } |
---|
| 488 | return $quoted_array; |
---|
| 489 | } else { |
---|
| 490 | return preg_quote($array, $delim); |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /** |
---|
| 496 | * Converts a PHP Array into encoded URL arguments and return them as an array. |
---|
| 497 | * |
---|
[334] | 498 | * @param mixed $data An array to transverse recursively, or a string |
---|
[1] | 499 | * to use directly to create url arguments. |
---|
| 500 | * @param string $prefix The name of the first dimension of the array. |
---|
| 501 | * If not specified, the first keys of the array will be used. |
---|
| 502 | * @return array URL with array elements as URL key=value arguments. |
---|
| 503 | */ |
---|
[235] | 504 | function urlEncodeArray($data, $prefix='', $_return=true) |
---|
| 505 | { |
---|
[42] | 506 | |
---|
[1] | 507 | // Data is stored in static variable. |
---|
| 508 | static $args; |
---|
[42] | 509 | |
---|
[1] | 510 | if (is_array($data)) { |
---|
| 511 | foreach ($data as $key => $val) { |
---|
[334] | 512 | // If the prefix is empty, use the $key as the name of the first dimension of the "array". |
---|
| 513 | // ...otherwise, append the key as a new dimension of the "array". |
---|
[1] | 514 | $new_prefix = ('' == $prefix) ? urlencode($key) : $prefix . '[' . urlencode($key) . ']'; |
---|
| 515 | // Enter recursion. |
---|
| 516 | urlEncodeArray($val, $new_prefix, false); |
---|
| 517 | } |
---|
| 518 | } else { |
---|
[334] | 519 | // We've come to the last dimension of the array, save the "array" and its value. |
---|
[1] | 520 | $args[$prefix] = urlencode($data); |
---|
| 521 | } |
---|
[42] | 522 | |
---|
[1] | 523 | if ($_return) { |
---|
| 524 | // This is not a recursive execution. All recursion is complete. |
---|
| 525 | // Reset static var and return the result. |
---|
| 526 | $ret = $args; |
---|
| 527 | $args = array(); |
---|
| 528 | return is_array($ret) ? $ret : array(); |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | /** |
---|
| 533 | * Converts a PHP Array into encoded URL arguments and return them in a string. |
---|
| 534 | * |
---|
[334] | 535 | * @param mixed $data An array to transverse recursively, or a string |
---|
[1] | 536 | * to use directly to create url arguments. |
---|
[334] | 537 | * @param string $prefix The name of the first dimension of the array. |
---|
[1] | 538 | * If not specified, the first keys of the array will be used. |
---|
| 539 | * @return string url A string ready to append to a url. |
---|
| 540 | */ |
---|
[235] | 541 | function urlEncodeArrayToString($data, $prefix='') |
---|
| 542 | { |
---|
[42] | 543 | |
---|
[1] | 544 | $array_args = urlEncodeArray($data, $prefix); |
---|
| 545 | $url_args = ''; |
---|
| 546 | $delim = ''; |
---|
| 547 | foreach ($array_args as $key=>$val) { |
---|
| 548 | $url_args .= $delim . $key . '=' . $val; |
---|
| 549 | $delim = ini_get('arg_separator.output'); |
---|
| 550 | } |
---|
| 551 | return $url_args; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /** |
---|
[334] | 555 | * Fills an array with the result from a multiple ereg search. |
---|
| 556 | * Courtesy of Bruno - rbronosky@mac.com - 10-May-2001 |
---|
[1] | 557 | * |
---|
| 558 | * @param mixed $pattern regular expression needle |
---|
| 559 | * @param mixed $string haystack |
---|
| 560 | * @return array populated with each found result |
---|
| 561 | */ |
---|
| 562 | function eregAll($pattern, $string) |
---|
| 563 | { |
---|
| 564 | do { |
---|
[247] | 565 | if (!mb_ereg($pattern, $string, $temp)) { |
---|
[1] | 566 | continue; |
---|
| 567 | } |
---|
| 568 | $string = str_replace($temp[0], '', $string); |
---|
| 569 | $results[] = $temp; |
---|
[247] | 570 | } while (mb_ereg($pattern, $string, $temp)); |
---|
[1] | 571 | return $results; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | /** |
---|
| 575 | * Prints the word "checked" if a variable is set, and optionally matches |
---|
| 576 | * the desired value, otherwise prints nothing, |
---|
[42] | 577 | * used for printing the word "checked" in a checkbox form input. |
---|
[1] | 578 | * |
---|
| 579 | * @param mixed $var the variable to compare |
---|
| 580 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
| 581 | */ |
---|
| 582 | function frmChecked($var, $value=null) |
---|
| 583 | { |
---|
| 584 | if (func_num_args() == 1 && $var) { |
---|
| 585 | // 'Checked' if var is true. |
---|
| 586 | echo ' checked="checked" '; |
---|
| 587 | } else if (func_num_args() == 2 && $var == $value) { |
---|
| 588 | // 'Checked' if var and value match. |
---|
| 589 | echo ' checked="checked" '; |
---|
| 590 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
| 591 | // 'Checked' if the value is in the key or the value of an array. |
---|
| 592 | if (isset($var[$value])) { |
---|
| 593 | echo ' checked="checked" '; |
---|
| 594 | } else if (in_array($value, $var)) { |
---|
| 595 | echo ' checked="checked" '; |
---|
| 596 | } |
---|
| 597 | } |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | /** |
---|
| 601 | * prints the word "selected" if a variable is set, and optionally matches |
---|
| 602 | * the desired value, otherwise prints nothing, |
---|
[42] | 603 | * otherwise prints nothing, used for printing the word "checked" in a |
---|
| 604 | * select form input |
---|
[1] | 605 | * |
---|
| 606 | * @param mixed $var the variable to compare |
---|
| 607 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
| 608 | */ |
---|
| 609 | function frmSelected($var, $value=null) |
---|
| 610 | { |
---|
| 611 | if (func_num_args() == 1 && $var) { |
---|
| 612 | // 'selected' if var is true. |
---|
| 613 | echo ' selected="selected" '; |
---|
| 614 | } else if (func_num_args() == 2 && $var == $value) { |
---|
| 615 | // 'selected' if var and value match. |
---|
| 616 | echo ' selected="selected" '; |
---|
| 617 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
| 618 | // 'selected' if the value is in the key or the value of an array. |
---|
| 619 | if (isset($var[$value])) { |
---|
| 620 | echo ' selected="selected" '; |
---|
| 621 | } else if (in_array($value, $var)) { |
---|
| 622 | echo ' selected="selected" '; |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | /** |
---|
[111] | 628 | * Adds slashes to values of an array and converts the array to a comma |
---|
| 629 | * delimited list. If value provided is a string return the string |
---|
| 630 | * escaped. This is useful for putting values coming in from posted |
---|
| 631 | * checkboxes into a SET column of a database. |
---|
| 632 | * |
---|
[1] | 633 | * |
---|
[111] | 634 | * @param array $in Array to convert. |
---|
[1] | 635 | * @return string Comma list of array values. |
---|
| 636 | */ |
---|
[224] | 637 | function escapedList($in, $separator="', '") |
---|
[1] | 638 | { |
---|
[136] | 639 | $db =& DB::getInstance(); |
---|
| 640 | |
---|
[111] | 641 | if (is_array($in) && !empty($in)) { |
---|
[224] | 642 | return join($separator, array_map(array($db, 'escapeString'), $in)); |
---|
[111] | 643 | } else { |
---|
[136] | 644 | return $db->escapeString($in); |
---|
[1] | 645 | } |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /** |
---|
[111] | 649 | * Converts a human string date into a SQL-safe date. Dates nearing |
---|
| 650 | * infinity use the date 2038-01-01 so conversion to unix time format |
---|
| 651 | * remain within valid range. |
---|
[1] | 652 | * |
---|
| 653 | * @param array $date String date to convert. |
---|
[42] | 654 | * @param array $format Date format to pass to date(). |
---|
[1] | 655 | * Default produces MySQL datetime: 0000-00-00 00:00:00. |
---|
| 656 | * @return string SQL-safe date. |
---|
| 657 | */ |
---|
| 658 | function strToSQLDate($date, $format='Y-m-d H:i:s') |
---|
| 659 | { |
---|
| 660 | // Translate the human string date into SQL-safe date format. |
---|
[247] | 661 | if (empty($date) || mb_strpos($date, '0000-00-00') !== false || strtotime($date) === -1 || strtotime($date) === false) { |
---|
[224] | 662 | // Return a string of zero time, formatted the same as $format. |
---|
| 663 | return strtr($format, array( |
---|
| 664 | 'Y' => '0000', |
---|
| 665 | 'm' => '00', |
---|
| 666 | 'd' => '00', |
---|
| 667 | 'H' => '00', |
---|
| 668 | 'i' => '00', |
---|
| 669 | 's' => '00', |
---|
| 670 | )); |
---|
[1] | 671 | } else { |
---|
[219] | 672 | return date($format, strtotime($date)); |
---|
[1] | 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | /** |
---|
| 677 | * If magic_quotes_gpc is in use, run stripslashes() on $var. If $var is an |
---|
[334] | 678 | * array, stripslashes is run on each value, recursively, and the stripped |
---|
[51] | 679 | * array is returned. |
---|
[1] | 680 | * |
---|
| 681 | * @param mixed $var The string or array to un-quote, if necessary. |
---|
| 682 | * @return mixed $var, minus any magic quotes. |
---|
| 683 | */ |
---|
| 684 | function dispelMagicQuotes($var) |
---|
| 685 | { |
---|
| 686 | static $magic_quotes_gpc; |
---|
[42] | 687 | |
---|
[1] | 688 | if (!isset($magic_quotes_gpc)) { |
---|
| 689 | $magic_quotes_gpc = get_magic_quotes_gpc(); |
---|
| 690 | } |
---|
[42] | 691 | |
---|
[1] | 692 | if ($magic_quotes_gpc) { |
---|
| 693 | if (!is_array($var)) { |
---|
| 694 | $var = stripslashes($var); |
---|
| 695 | } else { |
---|
| 696 | foreach ($var as $key=>$val) { |
---|
| 697 | if (is_array($val)) { |
---|
| 698 | $var[$key] = dispelMagicQuotes($val); |
---|
| 699 | } else { |
---|
| 700 | $var[$key] = stripslashes($val); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | return $var; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | /** |
---|
| 709 | * Get a form variable from GET or POST data, stripped of magic |
---|
| 710 | * quotes if necessary. |
---|
| 711 | * |
---|
| 712 | * @param string $var (optional) The name of the form variable to look for. |
---|
| 713 | * @param string $default (optional) The value to return if the |
---|
| 714 | * variable is not there. |
---|
| 715 | * @return mixed A cleaned GET or POST if no $var specified. |
---|
| 716 | * @return string A cleaned form $var if found, or $default. |
---|
| 717 | */ |
---|
| 718 | function getFormData($var=null, $default=null) |
---|
| 719 | { |
---|
| 720 | if ('POST' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
| 721 | return dispelMagicQuotes($_POST); |
---|
| 722 | } else if ('GET' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
| 723 | return dispelMagicQuotes($_GET); |
---|
| 724 | } |
---|
| 725 | if (isset($_POST[$var])) { |
---|
[51] | 726 | return dispelMagicQuotes($_POST[$var]); |
---|
[1] | 727 | } else if (isset($_GET[$var])) { |
---|
[51] | 728 | return dispelMagicQuotes($_GET[$var]); |
---|
[1] | 729 | } else { |
---|
| 730 | return $default; |
---|
| 731 | } |
---|
| 732 | } |
---|
| 733 | function getPost($var=null, $default=null) |
---|
| 734 | { |
---|
| 735 | if (is_null($var)) { |
---|
[51] | 736 | return dispelMagicQuotes($_POST); |
---|
[1] | 737 | } |
---|
| 738 | if (isset($_POST[$var])) { |
---|
[51] | 739 | return dispelMagicQuotes($_POST[$var]); |
---|
[1] | 740 | } else { |
---|
| 741 | return $default; |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | function getGet($var=null, $default=null) |
---|
| 745 | { |
---|
| 746 | if (is_null($var)) { |
---|
[51] | 747 | return dispelMagicQuotes($_GET); |
---|
[1] | 748 | } |
---|
| 749 | if (isset($_GET[$var])) { |
---|
[51] | 750 | return dispelMagicQuotes($_GET[$var]); |
---|
[1] | 751 | } else { |
---|
| 752 | return $default; |
---|
| 753 | } |
---|
| 754 | } |
---|
| 755 | |
---|
[361] | 756 | /* |
---|
| 757 | * Sets a $_GET or $_POST variable. |
---|
| 758 | * |
---|
| 759 | * @access public |
---|
| 760 | * @param string $key The key of the request array to set. |
---|
| 761 | * @param mixed $val The value to save in the request array. |
---|
| 762 | * @return void |
---|
| 763 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 764 | * @version 1.0 |
---|
| 765 | * @since 01 Nov 2009 12:25:29 |
---|
| 766 | */ |
---|
| 767 | function putFormData($key, $val) |
---|
| 768 | { |
---|
| 769 | if ('POST' == getenv('REQUEST_METHOD')) { |
---|
| 770 | $_POST[$key] = $val; |
---|
| 771 | } else if ('GET' == getenv('REQUEST_METHOD')) { |
---|
| 772 | $_GET[$key] = $val; |
---|
| 773 | } |
---|
| 774 | } |
---|
| 775 | |
---|
[1] | 776 | /** |
---|
| 777 | * Signs a value using md5 and a simple text key. In order for this |
---|
| 778 | * function to be useful (i.e. secure) the key must be kept secret, which |
---|
| 779 | * means keeping it as safe as database credentials. Putting it into an |
---|
| 780 | * environment variable set in httpd.conf is a good place. |
---|
| 781 | * |
---|
| 782 | * @access public |
---|
| 783 | * @param string $val The string to sign. |
---|
[159] | 784 | * @param string $salt (Optional) A text key to use for computing the signature. |
---|
[282] | 785 | * @param string $length (Optional) The length of the added signature. Longer signatures are safer. Must match the length passed to verifySignature() for the signatures to match. |
---|
[1] | 786 | * @return string The original value with a signature appended. |
---|
| 787 | */ |
---|
[282] | 788 | function addSignature($val, $salt=null, $length=18) |
---|
[1] | 789 | { |
---|
[159] | 790 | $app =& App::getInstance(); |
---|
| 791 | |
---|
| 792 | if ('' == trim($val)) { |
---|
[201] | 793 | $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__); |
---|
[159] | 794 | return ''; |
---|
[1] | 795 | } |
---|
[42] | 796 | |
---|
[159] | 797 | if (!isset($salt)) { |
---|
| 798 | $salt = $app->getParam('signing_key'); |
---|
[1] | 799 | } |
---|
| 800 | |
---|
[294] | 801 | return $val . '-' . mb_strtolower(mb_substr(md5($salt . md5($val . $salt)), 0, $length)); |
---|
[1] | 802 | } |
---|
| 803 | |
---|
| 804 | /** |
---|
| 805 | * Strips off the signature appended by addSignature(). |
---|
| 806 | * |
---|
| 807 | * @access public |
---|
| 808 | * @param string $signed_val The string to sign. |
---|
| 809 | * @return string The original value with a signature removed. |
---|
| 810 | */ |
---|
| 811 | function removeSignature($signed_val) |
---|
| 812 | { |
---|
[249] | 813 | if (empty($signed_val) || mb_strpos($signed_val, '-') === false) { |
---|
| 814 | return ''; |
---|
| 815 | } |
---|
[247] | 816 | return mb_substr($signed_val, 0, mb_strrpos($signed_val, '-')); |
---|
[1] | 817 | } |
---|
| 818 | |
---|
| 819 | |
---|
| 820 | /** |
---|
| 821 | * Verifies a signature appened to a value by addSignature(). |
---|
| 822 | * |
---|
| 823 | * @access public |
---|
| 824 | * @param string $signed_val A value with appended signature. |
---|
[159] | 825 | * @param string $salt (Optional) A text key to use for computing the signature. |
---|
[1] | 826 | * @return bool True if the signature matches the var. |
---|
| 827 | */ |
---|
[282] | 828 | function verifySignature($signed_val, $salt=null, $length=18) |
---|
[1] | 829 | { |
---|
[294] | 830 | // All comparisons are done using lower-case strings. |
---|
| 831 | $signed_val = mb_strtolower($signed_val); |
---|
[1] | 832 | // Strip the value from the signed value. |
---|
[22] | 833 | $val = removeSignature($signed_val); |
---|
[1] | 834 | // If the signed value matches the original signed value we consider the value safe. |
---|
[282] | 835 | if ($signed_val == addSignature($val, $salt, $length)) { |
---|
[1] | 836 | // Signature verified. |
---|
| 837 | return true; |
---|
| 838 | } else { |
---|
| 839 | return false; |
---|
| 840 | } |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | /** |
---|
| 844 | * Sends empty output to the browser and flushes the php buffer so the client |
---|
[42] | 845 | * will see data before the page is finished processing. |
---|
[1] | 846 | */ |
---|
[235] | 847 | function flushBuffer() |
---|
| 848 | { |
---|
[1] | 849 | echo str_repeat(' ', 205); |
---|
| 850 | flush(); |
---|
| 851 | } |
---|
| 852 | |
---|
| 853 | /** |
---|
| 854 | * Adds email address to mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
| 855 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
| 856 | * |
---|
| 857 | * @access public |
---|
| 858 | * @param string $email Email address to add. |
---|
| 859 | * @param string $list Name of list to add to. |
---|
| 860 | * @param bool $send_welcome_message True to send welcome message to subscriber. |
---|
| 861 | * @return bool True on success, false on failure. |
---|
| 862 | */ |
---|
| 863 | function mailmanAddMember($email, $list, $send_welcome_message=false) |
---|
| 864 | { |
---|
[136] | 865 | $app =& App::getInstance(); |
---|
| 866 | |
---|
[241] | 867 | $add_members = '/usr/lib/mailman/bin/add_members'; |
---|
[264] | 868 | /// FIXME: checking of executable is disabled. |
---|
| 869 | if (true || is_executable($add_members) && is_readable($add_members)) { |
---|
[1] | 870 | $welcome_msg = $send_welcome_message ? 'y' : 'n'; |
---|
[241] | 871 | 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); |
---|
[1] | 872 | if (0 == $return_code) { |
---|
[348] | 873 | $app->logMsg(sprintf('Mailman add member success for list: %s, user: %s', $list, $email), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 874 | return true; |
---|
| 875 | } else { |
---|
[136] | 876 | $app->logMsg(sprintf('Mailman add member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 877 | return false; |
---|
| 878 | } |
---|
| 879 | } else { |
---|
[136] | 880 | $app->logMsg(sprintf('Mailman add member program not executable: %s', $add_members), LOG_ALERT, __FILE__, __LINE__); |
---|
[1] | 881 | return false; |
---|
| 882 | } |
---|
| 883 | } |
---|
| 884 | |
---|
| 885 | /** |
---|
| 886 | * Removes email address from mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
| 887 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
| 888 | * |
---|
| 889 | * @access public |
---|
| 890 | * @param string $email Email address to add. |
---|
| 891 | * @param string $list Name of list to add to. |
---|
| 892 | * @param bool $send_user_ack True to send goodbye message to subscriber. |
---|
| 893 | * @return bool True on success, false on failure. |
---|
| 894 | */ |
---|
| 895 | function mailmanRemoveMember($email, $list, $send_user_ack=false) |
---|
| 896 | { |
---|
[136] | 897 | $app =& App::getInstance(); |
---|
| 898 | |
---|
[241] | 899 | $remove_members = '/usr/lib/mailman/bin/remove_members'; |
---|
[264] | 900 | /// FIXME: checking of executable is disabled. |
---|
| 901 | if (true || is_executable($remove_members) && is_readable($remove_members)) { |
---|
[1] | 902 | $userack = $send_user_ack ? '' : '--nouserack'; |
---|
[241] | 903 | exec(sprintf("/usr/bin/sudo %s %s --noadminack '%s' '%s'", escapeshellarg($remove_members), $userack, escapeshellarg($list), escapeshellarg($email)), $stdout, $return_code); |
---|
[1] | 904 | if (0 == $return_code) { |
---|
[348] | 905 | $app->logMsg(sprintf('Mailman remove member success for list: %s, user: %s', $list, $email), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 906 | return true; |
---|
| 907 | } else { |
---|
[136] | 908 | $app->logMsg(sprintf('Mailman remove member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 909 | return false; |
---|
| 910 | } |
---|
| 911 | } else { |
---|
[136] | 912 | $app->logMsg(sprintf('Mailman remove member program not executable: %s', $remove_members), LOG_ALERT, __FILE__, __LINE__); |
---|
[1] | 913 | return false; |
---|
| 914 | } |
---|
| 915 | } |
---|
| 916 | |
---|
| 917 | /** |
---|
[42] | 918 | * Returns the remote IP address, taking into consideration proxy servers. |
---|
[1] | 919 | * |
---|
| 920 | * @param bool $dolookup If true we resolve to IP to a host name, |
---|
| 921 | * if false we don't. |
---|
| 922 | * @return string IP address if $dolookup is false or no arg |
---|
| 923 | * Hostname if $dolookup is true |
---|
| 924 | */ |
---|
| 925 | function getRemoteAddr($dolookup=false) |
---|
| 926 | { |
---|
| 927 | $ip = getenv('HTTP_CLIENT_IP'); |
---|
[290] | 928 | if (in_array($ip, array('', 'unknown', 'localhost', '127.0.0.1'))) { |
---|
[1] | 929 | $ip = getenv('HTTP_X_FORWARDED_FOR'); |
---|
[290] | 930 | if (mb_strpos($ip, ',') !== false) { |
---|
| 931 | // If HTTP_X_FORWARDED_FOR returns a comma-delimited list of IPs then return the first one (assuming the first is the original). |
---|
| 932 | $ips = explode(',', $ip, 2); |
---|
| 933 | $ip = $ips[0]; |
---|
| 934 | } |
---|
| 935 | if (in_array($ip, array('', 'unknown', 'localhost', '127.0.0.1'))) { |
---|
[1] | 936 | $ip = getenv('REMOTE_ADDR'); |
---|
| 937 | } |
---|
| 938 | } |
---|
| 939 | return $dolookup && '' != $ip ? gethostbyaddr($ip) : $ip; |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | /** |
---|
| 943 | * Tests whether a given IP address can be found in an array of IP address networks. |
---|
| 944 | * Elements of networks array can be single IP addresses or an IP address range in CIDR notation |
---|
| 945 | * See: http://en.wikipedia.org/wiki/Classless_inter-domain_routing |
---|
| 946 | * |
---|
| 947 | * @access public |
---|
| 948 | * @param string IP address to search for. |
---|
| 949 | * @param array Array of networks to search within. |
---|
| 950 | * @return mixed Returns the network that matched on success, false on failure. |
---|
| 951 | */ |
---|
| 952 | function ipInRange($ip, $networks) |
---|
| 953 | { |
---|
| 954 | if (!is_array($networks)) { |
---|
| 955 | $networks = array($networks); |
---|
| 956 | } |
---|
[42] | 957 | |
---|
[1] | 958 | $ip_binary = sprintf('%032b', ip2long($ip)); |
---|
| 959 | foreach ($networks as $network) { |
---|
| 960 | if (preg_match('![\d\.]{7,15}/\d{1,2}!', $network)) { |
---|
| 961 | // IP is in CIDR notation. |
---|
[247] | 962 | list($cidr_ip, $cidr_bitmask) = explode('/', $network); |
---|
[1] | 963 | $cidr_ip_binary = sprintf('%032b', ip2long($cidr_ip)); |
---|
[247] | 964 | if (mb_substr($ip_binary, 0, $cidr_bitmask) === mb_substr($cidr_ip_binary, 0, $cidr_bitmask)) { |
---|
[1] | 965 | // IP address is within the specified IP range. |
---|
| 966 | return $network; |
---|
| 967 | } |
---|
| 968 | } else { |
---|
| 969 | if ($ip === $network) { |
---|
| 970 | // IP address exactly matches. |
---|
| 971 | return $network; |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | } |
---|
[42] | 975 | |
---|
[1] | 976 | return false; |
---|
| 977 | } |
---|
| 978 | |
---|
| 979 | /** |
---|
[159] | 980 | * If the given $url is on the same web site, return true. This can be used to |
---|
| 981 | * prevent from sending sensitive info in a get query (like the SID) to another |
---|
| 982 | * domain. |
---|
| 983 | * |
---|
| 984 | * @param string $url the URI to test. |
---|
| 985 | * @return bool True if given $url is our domain or has no domain (is a relative url), false if it's another. |
---|
| 986 | */ |
---|
| 987 | function isMyDomain($url) |
---|
| 988 | { |
---|
| 989 | static $urls = array(); |
---|
| 990 | |
---|
| 991 | if (!isset($urls[$url])) { |
---|
| 992 | if (!preg_match('|https?://[\w.]+/|', $url)) { |
---|
| 993 | // If we can't find a domain we assume the URL is local (i.e. "/my/url/path/" or "../img/file.jpg"). |
---|
| 994 | $urls[$url] = true; |
---|
| 995 | } else { |
---|
| 996 | $urls[$url] = preg_match('|https?://[\w.]*' . preg_quote(getenv('HTTP_HOST'), '|') . '|i', $url); |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | return $urls[$url]; |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | /** |
---|
| 1003 | * Takes a URL and returns it without the query or anchor portion |
---|
| 1004 | * |
---|
| 1005 | * @param string $url any kind of URI |
---|
| 1006 | * @return string the URI with ? or # and everything after removed |
---|
| 1007 | */ |
---|
| 1008 | function stripQuery($url) |
---|
| 1009 | { |
---|
[336] | 1010 | return preg_replace('/[?#].*$/', '', $url); |
---|
[159] | 1011 | } |
---|
| 1012 | |
---|
| 1013 | /** |
---|
[42] | 1014 | * Returns a fully qualified URL to the current script, including the query. |
---|
[1] | 1015 | * |
---|
| 1016 | * @return string a full url to the current script |
---|
| 1017 | */ |
---|
| 1018 | function absoluteMe() |
---|
| 1019 | { |
---|
| 1020 | $protocol = ('on' == getenv('HTTPS')) ? 'https://' : 'http://'; |
---|
| 1021 | return $protocol . getenv('HTTP_HOST') . getenv('REQUEST_URI'); |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | /** |
---|
| 1025 | * Compares the current url with the referring url. |
---|
| 1026 | * |
---|
[159] | 1027 | * @param bool $exclude_query Remove the query string first before comparing. |
---|
[334] | 1028 | * @return bool True if the current URL is the same as the referring URL, false otherwise. |
---|
[1] | 1029 | */ |
---|
| 1030 | function refererIsMe($exclude_query=false) |
---|
| 1031 | { |
---|
| 1032 | if ($exclude_query) { |
---|
| 1033 | return (stripQuery(absoluteMe()) == stripQuery(getenv('HTTP_REFERER'))); |
---|
| 1034 | } else { |
---|
| 1035 | return (absoluteMe() == getenv('HTTP_REFERER')); |
---|
| 1036 | } |
---|
| 1037 | } |
---|
| 1038 | |
---|
| 1039 | /** |
---|
| 1040 | * Stub functions used when installation does not have |
---|
| 1041 | * GNU gettext extension installed |
---|
| 1042 | */ |
---|
| 1043 | if (!extension_loaded('gettext')) { |
---|
| 1044 | /** |
---|
| 1045 | * Translates text |
---|
[42] | 1046 | * |
---|
[1] | 1047 | * @access public |
---|
| 1048 | * @param string $text the text to be translated |
---|
| 1049 | * @return string translated text |
---|
| 1050 | */ |
---|
| 1051 | function gettext($text) { |
---|
| 1052 | return $text; |
---|
| 1053 | } |
---|
[42] | 1054 | |
---|
[1] | 1055 | /** |
---|
| 1056 | * Translates text |
---|
[42] | 1057 | * |
---|
[1] | 1058 | * @access public |
---|
| 1059 | * @param string $text the text to be translated |
---|
| 1060 | * @return string translated text |
---|
| 1061 | */ |
---|
| 1062 | function _($text) { |
---|
| 1063 | return $text; |
---|
| 1064 | } |
---|
[42] | 1065 | |
---|
[1] | 1066 | /** |
---|
| 1067 | * Translates text by domain |
---|
[42] | 1068 | * |
---|
[1] | 1069 | * @access public |
---|
| 1070 | * @param string $domain the language to translate the text into |
---|
| 1071 | * @param string $text the text to be translated |
---|
| 1072 | * @return string translated text |
---|
| 1073 | */ |
---|
| 1074 | function dgettext($domain, $text) { |
---|
| 1075 | return $text; |
---|
| 1076 | } |
---|
[42] | 1077 | |
---|
[1] | 1078 | /** |
---|
| 1079 | * Translates text by domain and category |
---|
[42] | 1080 | * |
---|
[1] | 1081 | * @access public |
---|
| 1082 | * @param string $domain the language to translate the text into |
---|
| 1083 | * @param string $text the text to be translated |
---|
| 1084 | * @param string $category the language dialect to use |
---|
| 1085 | * @return string translated text |
---|
| 1086 | */ |
---|
| 1087 | function dcgettext($domain, $text, $category) { |
---|
| 1088 | return $text; |
---|
| 1089 | } |
---|
[42] | 1090 | |
---|
[1] | 1091 | /** |
---|
| 1092 | * Binds the text domain |
---|
[42] | 1093 | * |
---|
[1] | 1094 | * @access public |
---|
| 1095 | * @param string $domain the language to translate the text into |
---|
[42] | 1096 | * @param string |
---|
[1] | 1097 | * @return string translated text |
---|
| 1098 | */ |
---|
| 1099 | function bindtextdomain($domain, $directory) { |
---|
| 1100 | return $domain; |
---|
| 1101 | } |
---|
[42] | 1102 | |
---|
[1] | 1103 | /** |
---|
| 1104 | * Sets the text domain |
---|
[42] | 1105 | * |
---|
[1] | 1106 | * @access public |
---|
| 1107 | * @param string $domain the language to translate the text into |
---|
| 1108 | * @return string translated text |
---|
| 1109 | */ |
---|
| 1110 | function textdomain($domain) { |
---|
| 1111 | return $domain; |
---|
| 1112 | } |
---|
| 1113 | } |
---|
| 1114 | |
---|
| 1115 | |
---|
| 1116 | |
---|
[264] | 1117 | ?> |
---|