[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[454] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[454] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[454] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[1] | 24 | * Utilities.inc.php |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Print variable dump. |
---|
| 30 | * |
---|
[479] | 31 | * @param mixed $var The variable to dump. |
---|
| 32 | * @param bool $display Print the dump in <pre> tags or hide it in html comments (non-CLI only). |
---|
| 33 | * @param bool $var_dump Use var_dump instead of print_r. |
---|
| 34 | * @param string $file Value of __FILE__. |
---|
| 35 | * @param string $line Value of __LINE__ |
---|
[1] | 36 | */ |
---|
[465] | 37 | function dump($var, $display=false, $var_dump=false, $file='', $line='') |
---|
[1] | 38 | { |
---|
[380] | 39 | if (defined('_CLI')) { |
---|
[477] | 40 | echo "DUMP FROM: $file $line\n"; |
---|
[454] | 41 | } else { |
---|
[477] | 42 | echo $display ? "\n<br />DUMP <strong>$file $line</strong><br /><pre>\n" : "\n<!-- DUMP $file $line\n"; |
---|
[380] | 43 | } |
---|
[1] | 44 | if ($var_dump) { |
---|
| 45 | var_dump($var); |
---|
| 46 | } else { |
---|
[479] | 47 | // Print human-readable descriptions of invisible types. |
---|
| 48 | if (null === $var) { |
---|
| 49 | echo '(null)'; |
---|
| 50 | } else if (true === $var) { |
---|
| 51 | echo '(bool: true)'; |
---|
| 52 | } else if (false === $var) { |
---|
| 53 | echo '(bool: false)'; |
---|
| 54 | } else if (is_scalar($var) && '' === $var) { |
---|
| 55 | echo '(empty string)'; |
---|
| 56 | } else if (is_scalar($var) && preg_match('/^\s+$/', $var)) { |
---|
| 57 | echo '(only white space)'; |
---|
| 58 | } else { |
---|
| 59 | print_r($var); |
---|
| 60 | } |
---|
[1] | 61 | } |
---|
[380] | 62 | if (defined('_CLI')) { |
---|
| 63 | echo "\n"; |
---|
[454] | 64 | } else { |
---|
[477] | 65 | echo $display ? "\n</pre><br />\n" : "\n-->\n"; |
---|
[380] | 66 | } |
---|
[1] | 67 | } |
---|
| 68 | |
---|
[464] | 69 | /* |
---|
| 70 | * Log a PHP variable to javascript console. Relies on getDump(), below. |
---|
| 71 | * |
---|
| 72 | * @access public |
---|
| 73 | * @param mixed $var The variable to dump. |
---|
| 74 | * @param string $prefix A short note to print before the output to make identifying output easier. |
---|
| 75 | * @param string $file The value of __FILE__. |
---|
| 76 | * @param string $line The value of __LINE__. |
---|
| 77 | * @return null |
---|
| 78 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 79 | */ |
---|
| 80 | function jsDump($var, $prefix='jsDump', $file='-', $line='-') |
---|
| 81 | { |
---|
| 82 | if (!empty($var)) { |
---|
| 83 | ?> |
---|
[518] | 84 | <script type="text/javascript"> |
---|
[464] | 85 | /* <![CDATA[ */ |
---|
[518] | 86 | console.log('<?php printf('%s: %s (on line %s of %s)', $prefix, str_replace("'", "\\'", getDump($var, true)), $line, $file); ?>'); |
---|
[464] | 87 | /* ]]> */ |
---|
| 88 | </script> |
---|
| 89 | <?php |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /* |
---|
| 94 | * Return a string version of any variable, optionally serialized on one line. |
---|
| 95 | * |
---|
| 96 | * @access public |
---|
| 97 | * @param mixed $var The variable to dump. |
---|
| 98 | * @param bool $serialize If true, remove line-endings. Useful for logging variables. |
---|
| 99 | * @return string The dumped variable. |
---|
| 100 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 101 | */ |
---|
[331] | 102 | function getDump($var, $serialize=false) |
---|
[1] | 103 | { |
---|
| 104 | ob_start(); |
---|
| 105 | print_r($var); |
---|
| 106 | $d = ob_get_contents(); |
---|
| 107 | ob_end_clean(); |
---|
[457] | 108 | return $serialize ? preg_replace('/\s+/m', ' ', $d) : $d; |
---|
[1] | 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Return dump as cleaned text. Useful for dumping data into emails. |
---|
| 113 | * |
---|
[336] | 114 | * @param array $var Variable to dump. |
---|
[248] | 115 | * @param strong $indent A string to prepend indented lines (tab for example). |
---|
[1] | 116 | * @return string Dump of var. |
---|
| 117 | */ |
---|
| 118 | function fancyDump($var, $indent='') |
---|
| 119 | { |
---|
| 120 | $output = ''; |
---|
| 121 | if (is_array($var)) { |
---|
| 122 | foreach ($var as $k=>$v) { |
---|
[247] | 123 | $k = ucfirst(mb_strtolower(str_replace(array('_', ' '), ' ', $k))); |
---|
[1] | 124 | if (is_array($v)) { |
---|
| 125 | $output .= sprintf("\n%s%s: %s\n", $indent, $k, fancyDump($v, $indent . $indent)); |
---|
| 126 | } else { |
---|
| 127 | $output .= sprintf("%s%s: %s\n", $indent, $k, $v); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } else { |
---|
| 131 | $output .= sprintf("%s%s\n", $indent, $var); |
---|
| 132 | } |
---|
| 133 | return $output; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
[505] | 137 | * Returns text with appropriate html translations (a smart wrapper for htmlspecialchars()). |
---|
[1] | 138 | * |
---|
[257] | 139 | * @param string $text Text to clean. |
---|
[334] | 140 | * @param bool $preserve_html If set to true, oTxt will not translate <, >, ", or ' |
---|
[485] | 141 | * characters into HTML entities. This allows HTML to pass through undisturbed. |
---|
| 142 | * @return string HTML-safe text. |
---|
[1] | 143 | */ |
---|
[257] | 144 | function oTxt($text, $preserve_html=false) |
---|
[1] | 145 | { |
---|
[479] | 146 | $app =& App::getInstance(); |
---|
[136] | 147 | |
---|
[1] | 148 | $search = array(); |
---|
| 149 | $replace = array(); |
---|
| 150 | |
---|
| 151 | // Make converted ampersand entities into normal ampersands (they will be done manually later) to retain HTML entities. |
---|
| 152 | $search['retain_ampersand'] = '/&/'; |
---|
| 153 | $replace['retain_ampersand'] = '&'; |
---|
| 154 | |
---|
| 155 | if ($preserve_html) { |
---|
| 156 | // Convert characters that must remain non-entities for displaying HTML. |
---|
| 157 | $search['retain_left_angle'] = '/</'; |
---|
| 158 | $replace['retain_left_angle'] = '<'; |
---|
[42] | 159 | |
---|
[1] | 160 | $search['retain_right_angle'] = '/>/'; |
---|
| 161 | $replace['retain_right_angle'] = '>'; |
---|
[42] | 162 | |
---|
[1] | 163 | $search['retain_single_quote'] = '/'/'; |
---|
| 164 | $replace['retain_single_quote'] = "'"; |
---|
[42] | 165 | |
---|
[1] | 166 | $search['retain_double_quote'] = '/"/'; |
---|
| 167 | $replace['retain_double_quote'] = '"'; |
---|
| 168 | } |
---|
| 169 | |
---|
[334] | 170 | // & becomes &. Exclude any occurrence where the & is followed by a alphanum or unicode character. |
---|
[32] | 171 | $search['ampersand'] = '/&(?![\w\d#]{1,10};)/'; |
---|
| 172 | $replace['ampersand'] = '&'; |
---|
[1] | 173 | |
---|
[334] | 174 | return preg_replace($search, $replace, htmlspecialchars($text, ENT_QUOTES, $app->getParam('character_set'))); |
---|
[1] | 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
[334] | 178 | * Returns text with stylistic modifications. Warning: this will break some HTML attributes! |
---|
[320] | 179 | * TODO: Allow a string such as this to be passed: <a href="javascript:openPopup('/foo/bar.php')">Click here</a> |
---|
[1] | 180 | * |
---|
[257] | 181 | * @param string $text Text to clean. |
---|
[1] | 182 | * @return string Cleaned text. |
---|
| 183 | */ |
---|
[257] | 184 | function fancyTxt($text) |
---|
[1] | 185 | { |
---|
[103] | 186 | $search = array(); |
---|
| 187 | $replace = array(); |
---|
| 188 | |
---|
| 189 | // "double quoted text" becomes “double quoted text” |
---|
| 190 | $search['double_quotes'] = '/(^|[^\w=])(?:"|"|"|"|“)([^"]+?)(?:"|"|"|"|”)([^\w]|$)/ms'; // " is the same as " and " and " |
---|
| 191 | $replace['double_quotes'] = '$1“$2”$3'; |
---|
| 192 | |
---|
| 193 | // text's apostrophes become text’s apostrophes |
---|
| 194 | $search['apostrophe'] = '/(\w)(?:\'|'|')(\w)/ms'; |
---|
| 195 | $replace['apostrophe'] = '$1’$2'; |
---|
| 196 | |
---|
| 197 | // 'single quoted text' becomes ‘single quoted text’ |
---|
| 198 | $search['single_quotes'] = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/ms'; |
---|
| 199 | $replace['single_quotes'] = '$1‘$2’$3'; |
---|
| 200 | |
---|
| 201 | // plural posessives' apostrophes become posessives’ |
---|
| 202 | $search['apostrophes'] = '/(s)(?:\'|'|')(\s)/ms'; |
---|
| 203 | $replace['apostrophes'] = '$1’$2'; |
---|
| 204 | |
---|
| 205 | // em--dashes become em—dashes |
---|
| 206 | $search['em_dash'] = '/(\s*[^!<-])--([^>-]\s*)/'; |
---|
| 207 | $replace['em_dash'] = '$1—$2'; |
---|
| 208 | |
---|
[257] | 209 | return preg_replace($search, $replace, $text); |
---|
[1] | 210 | } |
---|
| 211 | |
---|
[505] | 212 | /* |
---|
| 213 | * Finds all URLs in text and hyperlinks them. |
---|
| 214 | * |
---|
| 215 | * @access public |
---|
| 216 | * @param string $text Text to search for URLs. |
---|
| 217 | * @param mixed $length Number of characters to truncate URL, or NULL to disable truncating. |
---|
| 218 | * @param string $delim Delimiter to append, indicate truncation. |
---|
| 219 | * @return string Same input text, but URLs hyperlinked. |
---|
| 220 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 221 | * @version 1.0 |
---|
| 222 | * @since 22 Mar 2015 23:29:04 |
---|
| 223 | */ |
---|
| 224 | function hyperlinkTxt($text, $length=null, $delim='âŠ') |
---|
| 225 | { |
---|
| 226 | return preg_replace_callback( |
---|
| 227 | // Inspired by @stephenhay's regex from https://mathiasbynens.be/demo/url-regex |
---|
| 228 | // Here we capture the full URL into the first match and only the first X characters into the second match. |
---|
| 229 | sprintf('@\b(?<!")(?<!\')(?<!=)(((?:https?|s?ftps?)://[^\s/$.?#].[^\s]{0,%s})[^\s]*)@iS', $length), |
---|
| 230 | // Use an anonymous function to decide when to append the delim. |
---|
| 231 | // Also encode special chars with oTxt(). |
---|
| 232 | function ($m) use ($length, $delim) { |
---|
| 233 | if (is_null($length) || $m[1] == $m[2]) { |
---|
| 234 | // If not truncating, or URL was not truncated. |
---|
| 235 | return sprintf('<a href="%s">%s</a>', oTxt($m[1]), oTxt($m[1])); |
---|
| 236 | } else { |
---|
| 237 | // Truncated URL. |
---|
| 238 | return sprintf('<a href="%s">%s%s</a>', oTxt($m[1]), oTxt(trim($m[2])), $delim); |
---|
| 239 | } |
---|
| 240 | }, |
---|
| 241 | $text |
---|
| 242 | ); |
---|
| 243 | } |
---|
| 244 | |
---|
[257] | 245 | /** |
---|
[334] | 246 | * Applies a class to search terms to highlight them ala google results. |
---|
[257] | 247 | * |
---|
| 248 | * @param string $text Input text to search. |
---|
| 249 | * @param string $search String of word(s) that will be highlighted. |
---|
| 250 | * @param string $class CSS class to apply. |
---|
| 251 | * @return string Text with searched words wrapped in <span>. |
---|
| 252 | */ |
---|
| 253 | function highlightWords($text, $search, $class='sc-highlightwords') |
---|
| 254 | { |
---|
| 255 | $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY); |
---|
[454] | 256 | |
---|
[257] | 257 | $search = array(); |
---|
| 258 | $replace = array(); |
---|
[454] | 259 | |
---|
[257] | 260 | foreach ($words as $w) { |
---|
[258] | 261 | if ('' != trim($w)) { |
---|
| 262 | $search[] = '/\b(' . preg_quote($w) . ')\b/i'; |
---|
| 263 | $replace[] = '<span class="' . $class . '">$1</span>'; |
---|
| 264 | } |
---|
[257] | 265 | } |
---|
[42] | 266 | |
---|
[258] | 267 | return empty($replace) ? $text : preg_replace($search, $replace, $text); |
---|
[257] | 268 | } |
---|
| 269 | |
---|
[1] | 270 | /** |
---|
[334] | 271 | * Generates a hexadecimal html color based on provided word. |
---|
[1] | 272 | * |
---|
| 273 | * @access public |
---|
| 274 | * @param string $text A string for which to convert to color. |
---|
| 275 | * @return string A hexadecimal html color. |
---|
| 276 | */ |
---|
| 277 | function getTextColor($text, $method=1) |
---|
| 278 | { |
---|
[235] | 279 | $hash = md5($text); |
---|
| 280 | $rgb = array( |
---|
[247] | 281 | mb_substr($hash, 0, 1), |
---|
| 282 | mb_substr($hash, 1, 1), |
---|
| 283 | mb_substr($hash, 2, 1), |
---|
| 284 | mb_substr($hash, 3, 1), |
---|
| 285 | mb_substr($hash, 4, 1), |
---|
| 286 | mb_substr($hash, 5, 1), |
---|
[235] | 287 | ); |
---|
[1] | 288 | |
---|
| 289 | switch ($method) { |
---|
[235] | 290 | case 1 : |
---|
| 291 | default : |
---|
[334] | 292 | // Reduce all hex values slightly to avoid all white. |
---|
[235] | 293 | array_walk($rgb, create_function('&$v', '$v = dechex(round(hexdec($v) * 0.87));')); |
---|
| 294 | break; |
---|
[1] | 295 | case 2 : |
---|
[235] | 296 | foreach ($rgb as $i => $v) { |
---|
| 297 | if (hexdec($v) > hexdec('c')) { |
---|
| 298 | $rgb[$i] = dechex(hexdec('f') - hexdec($v)); |
---|
| 299 | } |
---|
[1] | 300 | } |
---|
| 301 | break; |
---|
| 302 | } |
---|
| 303 | |
---|
[235] | 304 | return join('', $rgb); |
---|
[1] | 305 | } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | * Encodes a string into unicode values 128-255. |
---|
| 309 | * Useful for hiding an email address from spambots. |
---|
| 310 | * |
---|
| 311 | * @access public |
---|
| 312 | * @param string $text A line of text to encode. |
---|
| 313 | * @return string Encoded text. |
---|
| 314 | */ |
---|
| 315 | function encodeAscii($text) |
---|
| 316 | { |
---|
[255] | 317 | $output = ''; |
---|
[247] | 318 | $num = mb_strlen($text); |
---|
[1] | 319 | for ($i=0; $i<$num; $i++) { |
---|
| 320 | $output .= sprintf('&#%03s', ord($text{$i})); |
---|
| 321 | } |
---|
| 322 | return $output; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
[84] | 326 | * Encodes an email into a "user at domain dot com" format. |
---|
[9] | 327 | * |
---|
| 328 | * @access public |
---|
| 329 | * @param string $email An email to encode. |
---|
| 330 | * @param string $at Replaces the @. |
---|
| 331 | * @param string $dot Replaces the .. |
---|
| 332 | * @return string Encoded email. |
---|
| 333 | */ |
---|
[53] | 334 | function encodeEmail($email, $at=' at ', $dot=' dot ') |
---|
[9] | 335 | { |
---|
| 336 | $search = array('/@/', '/\./'); |
---|
| 337 | $replace = array($at, $dot); |
---|
| 338 | return preg_replace($search, $replace, $email); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | /** |
---|
[454] | 342 | * Truncates "a really long string" into a string of specified length |
---|
| 343 | * at the beginning: "âŠlong string" |
---|
| 344 | * at the middle: "a reaâŠstring" |
---|
| 345 | * or at the end: "a reallyâŠ". |
---|
[84] | 346 | * |
---|
[454] | 347 | * The regular expressions below first match and replace the string to the specified length and position, |
---|
| 348 | * and secondly they remove any whitespace from around the delimiter (to avoid "this ⊠" from happening). |
---|
| 349 | * |
---|
[84] | 350 | * @access public |
---|
| 351 | * @param string $str Input string |
---|
| 352 | * @param int $len Maximum string length. |
---|
| 353 | * @param string $where Where to cut the string. One of: 'start', 'middle', or 'end'. |
---|
[454] | 354 | * @return string Truncated output string. |
---|
[84] | 355 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 356 | * @since 29 Mar 2006 13:48:49 |
---|
| 357 | */ |
---|
[454] | 358 | function truncate($str, $len=50, $where='end', $delim='âŠ') |
---|
[84] | 359 | { |
---|
[454] | 360 | $dlen = mb_strlen($delim); |
---|
| 361 | if ($len <= $dlen || mb_strlen($str) <= $dlen) { |
---|
| 362 | return substr($str, 0, $len); |
---|
[240] | 363 | } |
---|
[454] | 364 | $part1 = floor(($len - $dlen) / 2); |
---|
| 365 | $part2 = ceil(($len - $dlen) / 2); |
---|
[84] | 366 | switch ($where) { |
---|
| 367 | case 'start' : |
---|
[454] | 368 | return preg_replace(array(sprintf('/^.{%s,}(.{%s})$/sU', $dlen + 1, $part1 + $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array($delim . '$1', $delim), $str); |
---|
| 369 | |
---|
[84] | 370 | case 'middle' : |
---|
[454] | 371 | return preg_replace(array(sprintf('/^(.{%s}).{%s,}(.{%s})$/sU', $part1, $dlen + 1, $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim . '$2', $delim), $str); |
---|
| 372 | |
---|
[84] | 373 | case 'end' : |
---|
[454] | 374 | default : |
---|
| 375 | return preg_replace(array(sprintf('/^(.{%s}).{%s,}$/sU', $part1 + $part2, $dlen + 1), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim, $delim), $str); |
---|
[84] | 376 | } |
---|
| 377 | } |
---|
| 378 | |
---|
[340] | 379 | /* |
---|
| 380 | * A substitution for the missing mb_ucfirst function. |
---|
| 381 | * |
---|
| 382 | * @access public |
---|
[414] | 383 | * @param string $string The string |
---|
| 384 | * @return string String with upper-cased first character. |
---|
[340] | 385 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 386 | * @version 1.0 |
---|
| 387 | * @since 06 Dec 2008 17:04:01 |
---|
| 388 | */ |
---|
[454] | 389 | if (!function_exists('mb_ucfirst')) { |
---|
[340] | 390 | function mb_ucfirst($string) |
---|
| 391 | { |
---|
| 392 | return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1, mb_strlen($string)); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | |
---|
[414] | 396 | /* |
---|
| 397 | * A substitution for the missing mb_strtr function. |
---|
| 398 | * |
---|
| 399 | * @access public |
---|
| 400 | * @param string $string The string |
---|
| 401 | * @param string $from String of characters to translate from |
---|
| 402 | * @param string $to String of characters to translate to |
---|
| 403 | * @return string String with translated characters. |
---|
| 404 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 405 | * @version 1.0 |
---|
| 406 | * @since 20 Jan 2013 12:33:26 |
---|
| 407 | */ |
---|
[454] | 408 | if (!function_exists('mb_strtr')) { |
---|
[414] | 409 | function mb_strtr($string, $from, $to) |
---|
| 410 | { |
---|
| 411 | return str_replace(mb_split('.', $from), mb_split('.', $to), $string); |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | |
---|
[474] | 415 | /* |
---|
| 416 | * A substitution for the missing mb_str_pad function. |
---|
| 417 | * |
---|
| 418 | * @access public |
---|
| 419 | * @param string $input The string that receives padding. |
---|
| 420 | * @param string $pad_length Total length of resultant string. |
---|
| 421 | * @param string $pad_string The string to use for padding |
---|
| 422 | * @param string $pad_type Flags STR_PAD_RIGHT or STR_PAD_LEFT or STR_PAD_BOTH |
---|
| 423 | * @return string String with translated characters. |
---|
| 424 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 425 | * @version 1.0 |
---|
| 426 | * @since 20 Jan 2013 12:33:26 |
---|
| 427 | */ |
---|
| 428 | if (!function_exists('mb_str_pad')) { |
---|
| 429 | function mb_str_pad($input, $pad_length, $pad_string=' ', $pad_type=STR_PAD_RIGHT) { |
---|
| 430 | $diff = strlen($input) - mb_strlen($input); |
---|
| 431 | return str_pad($input, $pad_length + $diff, $pad_string, $pad_type); |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
[486] | 435 | /* |
---|
| 436 | * Converts a string into a URL-safe slug, removing spaces and non word characters. |
---|
| 437 | * |
---|
| 438 | * @access public |
---|
| 439 | * @param string $str String to convert. |
---|
| 440 | * @return string URL-safe slug. |
---|
| 441 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 442 | * @version 1.0 |
---|
| 443 | * @since 18 Aug 2014 12:54:29 |
---|
| 444 | */ |
---|
| 445 | function URLSlug($str) |
---|
| 446 | { |
---|
| 447 | $slug = preg_replace(array('/[^\w]+/', '/^-+|-+$/'), array('-', ''), $str); |
---|
| 448 | $slug = strtolower($slug); |
---|
| 449 | return $slug; |
---|
| 450 | } |
---|
| 451 | |
---|
[84] | 452 | /** |
---|
[338] | 453 | * Return a human readable disk space measurement. Input value measured in bytes. |
---|
[1] | 454 | * |
---|
[338] | 455 | * @param int $size Size in bytes. |
---|
[1] | 456 | * @param int $unit The maximum unit |
---|
| 457 | * @param int $format The return string format |
---|
| 458 | * @author Aidan Lister <aidan@php.net> |
---|
[362] | 459 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 460 | * @version 1.2.0 |
---|
[1] | 461 | */ |
---|
[338] | 462 | function humanFileSize($size, $format='%01.2f %s', $max_unit=null, $multiplier=1024) |
---|
[1] | 463 | { |
---|
| 464 | // Units |
---|
| 465 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
---|
| 466 | $ii = count($units) - 1; |
---|
[42] | 467 | |
---|
[1] | 468 | // Max unit |
---|
[154] | 469 | $max_unit = array_search((string) $max_unit, $units); |
---|
| 470 | if ($max_unit === null || $max_unit === false) { |
---|
| 471 | $max_unit = $ii; |
---|
[1] | 472 | } |
---|
[42] | 473 | |
---|
[1] | 474 | // Loop |
---|
| 475 | $i = 0; |
---|
[338] | 476 | while ($max_unit != $i && $size >= $multiplier && $i < $ii) { |
---|
| 477 | $size /= $multiplier; |
---|
[1] | 478 | $i++; |
---|
| 479 | } |
---|
[42] | 480 | |
---|
[1] | 481 | return sprintf($format, $size, $units[$i]); |
---|
| 482 | } |
---|
| 483 | |
---|
[180] | 484 | /* |
---|
[189] | 485 | * Returns a human readable amount of time for the given amount of seconds. |
---|
[454] | 486 | * |
---|
[180] | 487 | * 45 seconds |
---|
| 488 | * 12 minutes |
---|
| 489 | * 3.5 hours |
---|
| 490 | * 2 days |
---|
| 491 | * 1 week |
---|
| 492 | * 4 months |
---|
[454] | 493 | * |
---|
[180] | 494 | * Months are calculated using the real number of days in a year: 365.2422 / 12. |
---|
| 495 | * |
---|
| 496 | * @access public |
---|
[189] | 497 | * @param int $seconds Seconds of time. |
---|
[180] | 498 | * @param string $max_unit Key value from the $units array. |
---|
| 499 | * @param string $format Sprintf formatting string. |
---|
| 500 | * @return string Value of units elapsed. |
---|
| 501 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 502 | * @version 1.0 |
---|
| 503 | * @since 23 Jun 2006 12:15:19 |
---|
| 504 | */ |
---|
[189] | 505 | function humanTime($seconds, $max_unit=null, $format='%01.1f') |
---|
[180] | 506 | { |
---|
[202] | 507 | // Units: array of seconds in the unit, singular and plural unit names. |
---|
[180] | 508 | $units = array( |
---|
| 509 | 'second' => array(1, _("second"), _("seconds")), |
---|
| 510 | 'minute' => array(60, _("minute"), _("minutes")), |
---|
| 511 | 'hour' => array(3600, _("hour"), _("hours")), |
---|
| 512 | 'day' => array(86400, _("day"), _("days")), |
---|
| 513 | 'week' => array(604800, _("week"), _("weeks")), |
---|
| 514 | 'month' => array(2629743.84, _("month"), _("months")), |
---|
| 515 | 'year' => array(31556926.08, _("year"), _("years")), |
---|
| 516 | 'decade' => array(315569260.8, _("decade"), _("decades")), |
---|
[362] | 517 | 'century' => array(3155692608, _("century"), _("centuries")), |
---|
[180] | 518 | ); |
---|
[454] | 519 | |
---|
[202] | 520 | // Max unit to calculate. |
---|
[362] | 521 | $max_unit = isset($units[$max_unit]) ? $max_unit : 'year'; |
---|
[180] | 522 | |
---|
[189] | 523 | $final_time = $seconds; |
---|
[363] | 524 | $final_unit = 'second'; |
---|
[180] | 525 | foreach ($units as $k => $v) { |
---|
[363] | 526 | if ($seconds >= $v[0]) { |
---|
[189] | 527 | $final_time = $seconds / $v[0]; |
---|
[363] | 528 | $final_unit = $k; |
---|
[180] | 529 | } |
---|
[363] | 530 | if ($max_unit == $final_unit) { |
---|
| 531 | break; |
---|
| 532 | } |
---|
[180] | 533 | } |
---|
[189] | 534 | $final_time = sprintf($format, $final_time); |
---|
[454] | 535 | return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2])); |
---|
[180] | 536 | } |
---|
| 537 | |
---|
[518] | 538 | /** |
---|
| 539 | * Removes non-latin characters from file name, using htmlentities to convert known weirdos into regular squares. |
---|
| 540 | * |
---|
| 541 | * @access public |
---|
| 542 | * @param string $file_name A name of a file. |
---|
| 543 | * @return string The same name, but cleaned. |
---|
| 544 | */ |
---|
| 545 | function cleanFileName($file_name) |
---|
| 546 | { |
---|
| 547 | $app =& App::getInstance(); |
---|
| 548 | |
---|
| 549 | $file_name = preg_replace(array( |
---|
| 550 | '/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/ui', |
---|
| 551 | '/&(?:amp);/ui', |
---|
| 552 | '/[&;]+/u', |
---|
| 553 | '/[^a-zA-Z0-9()@._=+-]+/u', |
---|
| 554 | '/^_+|_+$/u' |
---|
| 555 | ), array( |
---|
| 556 | '$1', |
---|
| 557 | 'and', |
---|
| 558 | '', |
---|
| 559 | '_', |
---|
| 560 | '' |
---|
| 561 | ), htmlentities($file_name, ENT_NOQUOTES | ENT_IGNORE, $app->getParam('character_set'))); |
---|
| 562 | return mb_substr($file_name, 0, 250); |
---|
| 563 | } |
---|
| 564 | |
---|
[519] | 565 | /** |
---|
| 566 | * Returns the extension of a file name, or an empty string if none exists. |
---|
| 567 | * |
---|
| 568 | * @access public |
---|
| 569 | * @param string $file_name A name of a file, with extension after a dot. |
---|
| 570 | * @return string The value found after the dot |
---|
| 571 | */ |
---|
| 572 | function getFilenameExtension($file_name) |
---|
| 573 | { |
---|
| 574 | preg_match('/.*?\.(\w+)$/i', trim($file_name), $ext); |
---|
| 575 | return isset($ext[1]) ? $ext[1] : ''; |
---|
| 576 | } |
---|
| 577 | |
---|
[487] | 578 | /* |
---|
| 579 | * Convert a php.ini value (8M, 512K, etc), into integer value of bytes. |
---|
| 580 | * |
---|
| 581 | * @access public |
---|
| 582 | * @param string $val Value from php config, e.g., upload_max_filesize. |
---|
| 583 | * @return int Value converted to bytes as an integer. |
---|
| 584 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 585 | * @version 1.0 |
---|
| 586 | * @since 20 Aug 2014 14:32:41 |
---|
| 587 | */ |
---|
| 588 | function phpIniGetBytes($val) |
---|
| 589 | { |
---|
| 590 | $val = trim(ini_get($val)); |
---|
| 591 | if ($val != '') { |
---|
| 592 | $last = strtolower($val{strlen($val) - 1}); |
---|
| 593 | } else { |
---|
| 594 | $last = ''; |
---|
| 595 | } |
---|
| 596 | switch ($last) { |
---|
| 597 | // The 'G' modifier is available since PHP 5.1.0 |
---|
| 598 | case 'g': |
---|
| 599 | $val *= 1024; |
---|
| 600 | case 'm': |
---|
| 601 | $val *= 1024; |
---|
| 602 | case 'k': |
---|
| 603 | $val *= 1024; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | return (int)$val; |
---|
| 607 | } |
---|
| 608 | |
---|
[1] | 609 | /** |
---|
[334] | 610 | * Tests the existence of a file anywhere in the include path. |
---|
[258] | 611 | * |
---|
| 612 | * @param string $file File in include path. |
---|
| 613 | * @return mixed False if file not found, the path of the file if it is found. |
---|
| 614 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 615 | * @since 03 Dec 2005 14:23:26 |
---|
| 616 | */ |
---|
| 617 | function fileExistsIncludePath($file) |
---|
| 618 | { |
---|
| 619 | $app =& App::getInstance(); |
---|
[454] | 620 | |
---|
[258] | 621 | foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { |
---|
| 622 | $fullpath = $path . DIRECTORY_SEPARATOR . $file; |
---|
| 623 | if (file_exists($fullpath)) { |
---|
| 624 | $app->logMsg(sprintf('Found file "%s" at path: %s', $file, $fullpath), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 625 | return $fullpath; |
---|
| 626 | } else { |
---|
| 627 | $app->logMsg(sprintf('File "%s" not found in include_path: %s', $file, get_include_path()), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 628 | return false; |
---|
| 629 | } |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | /** |
---|
[26] | 634 | * Returns stats of a file from the include path. |
---|
| 635 | * |
---|
| 636 | * @param string $file File in include path. |
---|
[258] | 637 | * @param mixed $stat Which statistic to return (or null to return all). |
---|
| 638 | * @return mixed Value of requested key from fstat(), or false on error. |
---|
[26] | 639 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 640 | * @since 03 Dec 2005 14:23:26 |
---|
| 641 | */ |
---|
[241] | 642 | function statIncludePath($file, $stat=null) |
---|
[26] | 643 | { |
---|
| 644 | // Open file pointer read-only using include path. |
---|
| 645 | if ($fp = fopen($file, 'r', true)) { |
---|
[258] | 646 | // File opened successfully, get stats. |
---|
[26] | 647 | $stats = fstat($fp); |
---|
| 648 | fclose($fp); |
---|
| 649 | // Return specified stats. |
---|
[241] | 650 | return is_null($stat) ? $stats : $stats[$stat]; |
---|
[26] | 651 | } else { |
---|
| 652 | return false; |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | |
---|
[330] | 656 | /* |
---|
| 657 | * Writes content to the specified file. This function emulates the functionality of file_put_contents from PHP 5. |
---|
[400] | 658 | * It makes an exclusive lock on the file while writing. |
---|
[330] | 659 | * |
---|
| 660 | * @access public |
---|
| 661 | * @param string $filename Path to file. |
---|
| 662 | * @param string $content Data to write into file. |
---|
| 663 | * @return bool Success or failure. |
---|
| 664 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 665 | * @since 11 Apr 2006 22:48:30 |
---|
| 666 | */ |
---|
| 667 | function filePutContents($filename, $content) |
---|
| 668 | { |
---|
[479] | 669 | $app =& App::getInstance(); |
---|
[330] | 670 | |
---|
| 671 | // Open file for writing and truncate to zero length. |
---|
| 672 | if ($fp = fopen($filename, 'w')) { |
---|
| 673 | if (flock($fp, LOCK_EX)) { |
---|
| 674 | if (!fwrite($fp, $content, mb_strlen($content))) { |
---|
| 675 | $app->logMsg(sprintf('Failed writing to file: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 676 | fclose($fp); |
---|
| 677 | return false; |
---|
| 678 | } |
---|
| 679 | flock($fp, LOCK_UN); |
---|
| 680 | } else { |
---|
| 681 | $app->logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 682 | fclose($fp); |
---|
| 683 | return false; |
---|
| 684 | } |
---|
| 685 | fclose($fp); |
---|
| 686 | // Success! |
---|
| 687 | $app->logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 688 | return true; |
---|
| 689 | } else { |
---|
| 690 | $app->logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
| 691 | return false; |
---|
| 692 | } |
---|
| 693 | } |
---|
| 694 | |
---|
[26] | 695 | /** |
---|
[1] | 696 | * If $var is net set or null, set it to $default. Otherwise leave it alone. |
---|
[334] | 697 | * Returns the final value of $var. Use to find a default value of one is not available. |
---|
[1] | 698 | * |
---|
| 699 | * @param mixed $var The variable that is being set. |
---|
| 700 | * @param mixed $default What to set it to if $val is not currently set. |
---|
[42] | 701 | * @return mixed The resulting value of $var. |
---|
[1] | 702 | */ |
---|
| 703 | function setDefault(&$var, $default='') |
---|
| 704 | { |
---|
| 705 | if (!isset($var)) { |
---|
| 706 | $var = $default; |
---|
| 707 | } |
---|
| 708 | return $var; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | /** |
---|
| 712 | * Like preg_quote() except for arrays, it takes an array of strings and puts |
---|
| 713 | * a backslash in front of every character that is part of the regular |
---|
| 714 | * expression syntax. |
---|
| 715 | * |
---|
| 716 | * @param array $array input array |
---|
[334] | 717 | * @param array $delim optional character that will also be escaped. |
---|
[1] | 718 | * @return array an array with the same values as $array1 but shuffled |
---|
| 719 | */ |
---|
| 720 | function pregQuoteArray($array, $delim='/') |
---|
| 721 | { |
---|
| 722 | if (!empty($array)) { |
---|
| 723 | if (is_array($array)) { |
---|
| 724 | foreach ($array as $key=>$val) { |
---|
| 725 | $quoted_array[$key] = preg_quote($val, $delim); |
---|
| 726 | } |
---|
| 727 | return $quoted_array; |
---|
| 728 | } else { |
---|
| 729 | return preg_quote($array, $delim); |
---|
| 730 | } |
---|
| 731 | } |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | /** |
---|
| 735 | * Converts a PHP Array into encoded URL arguments and return them as an array. |
---|
| 736 | * |
---|
[334] | 737 | * @param mixed $data An array to transverse recursively, or a string |
---|
[1] | 738 | * to use directly to create url arguments. |
---|
| 739 | * @param string $prefix The name of the first dimension of the array. |
---|
| 740 | * If not specified, the first keys of the array will be used. |
---|
| 741 | * @return array URL with array elements as URL key=value arguments. |
---|
| 742 | */ |
---|
[235] | 743 | function urlEncodeArray($data, $prefix='', $_return=true) |
---|
| 744 | { |
---|
[1] | 745 | // Data is stored in static variable. |
---|
| 746 | static $args; |
---|
[42] | 747 | |
---|
[1] | 748 | if (is_array($data)) { |
---|
| 749 | foreach ($data as $key => $val) { |
---|
[334] | 750 | // If the prefix is empty, use the $key as the name of the first dimension of the "array". |
---|
| 751 | // ...otherwise, append the key as a new dimension of the "array". |
---|
[1] | 752 | $new_prefix = ('' == $prefix) ? urlencode($key) : $prefix . '[' . urlencode($key) . ']'; |
---|
| 753 | // Enter recursion. |
---|
| 754 | urlEncodeArray($val, $new_prefix, false); |
---|
| 755 | } |
---|
| 756 | } else { |
---|
[334] | 757 | // We've come to the last dimension of the array, save the "array" and its value. |
---|
[1] | 758 | $args[$prefix] = urlencode($data); |
---|
| 759 | } |
---|
[42] | 760 | |
---|
[1] | 761 | if ($_return) { |
---|
| 762 | // This is not a recursive execution. All recursion is complete. |
---|
| 763 | // Reset static var and return the result. |
---|
| 764 | $ret = $args; |
---|
| 765 | $args = array(); |
---|
| 766 | return is_array($ret) ? $ret : array(); |
---|
| 767 | } |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | /** |
---|
| 771 | * Converts a PHP Array into encoded URL arguments and return them in a string. |
---|
| 772 | * |
---|
[334] | 773 | * @param mixed $data An array to transverse recursively, or a string |
---|
[1] | 774 | * to use directly to create url arguments. |
---|
[334] | 775 | * @param string $prefix The name of the first dimension of the array. |
---|
[1] | 776 | * If not specified, the first keys of the array will be used. |
---|
| 777 | * @return string url A string ready to append to a url. |
---|
| 778 | */ |
---|
[235] | 779 | function urlEncodeArrayToString($data, $prefix='') |
---|
| 780 | { |
---|
[42] | 781 | |
---|
[1] | 782 | $array_args = urlEncodeArray($data, $prefix); |
---|
| 783 | $url_args = ''; |
---|
| 784 | $delim = ''; |
---|
| 785 | foreach ($array_args as $key=>$val) { |
---|
| 786 | $url_args .= $delim . $key . '=' . $val; |
---|
| 787 | $delim = ini_get('arg_separator.output'); |
---|
| 788 | } |
---|
| 789 | return $url_args; |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | /** |
---|
[334] | 793 | * Fills an array with the result from a multiple ereg search. |
---|
| 794 | * Courtesy of Bruno - rbronosky@mac.com - 10-May-2001 |
---|
[1] | 795 | * |
---|
| 796 | * @param mixed $pattern regular expression needle |
---|
| 797 | * @param mixed $string haystack |
---|
| 798 | * @return array populated with each found result |
---|
| 799 | */ |
---|
| 800 | function eregAll($pattern, $string) |
---|
| 801 | { |
---|
| 802 | do { |
---|
[247] | 803 | if (!mb_ereg($pattern, $string, $temp)) { |
---|
[1] | 804 | continue; |
---|
| 805 | } |
---|
| 806 | $string = str_replace($temp[0], '', $string); |
---|
| 807 | $results[] = $temp; |
---|
[247] | 808 | } while (mb_ereg($pattern, $string, $temp)); |
---|
[1] | 809 | return $results; |
---|
| 810 | } |
---|
| 811 | |
---|
| 812 | /** |
---|
| 813 | * Prints the word "checked" if a variable is set, and optionally matches |
---|
| 814 | * the desired value, otherwise prints nothing, |
---|
[42] | 815 | * used for printing the word "checked" in a checkbox form input. |
---|
[1] | 816 | * |
---|
| 817 | * @param mixed $var the variable to compare |
---|
| 818 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
| 819 | */ |
---|
| 820 | function frmChecked($var, $value=null) |
---|
| 821 | { |
---|
| 822 | if (func_num_args() == 1 && $var) { |
---|
| 823 | // 'Checked' if var is true. |
---|
| 824 | echo ' checked="checked" '; |
---|
| 825 | } else if (func_num_args() == 2 && $var == $value) { |
---|
| 826 | // 'Checked' if var and value match. |
---|
| 827 | echo ' checked="checked" '; |
---|
| 828 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
| 829 | // 'Checked' if the value is in the key or the value of an array. |
---|
| 830 | if (isset($var[$value])) { |
---|
| 831 | echo ' checked="checked" '; |
---|
| 832 | } else if (in_array($value, $var)) { |
---|
| 833 | echo ' checked="checked" '; |
---|
| 834 | } |
---|
| 835 | } |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | /** |
---|
| 839 | * prints the word "selected" if a variable is set, and optionally matches |
---|
| 840 | * the desired value, otherwise prints nothing, |
---|
[42] | 841 | * otherwise prints nothing, used for printing the word "checked" in a |
---|
| 842 | * select form input |
---|
[1] | 843 | * |
---|
| 844 | * @param mixed $var the variable to compare |
---|
| 845 | * @param mixed $value optional, what to compare with if a specific value is required. |
---|
| 846 | */ |
---|
| 847 | function frmSelected($var, $value=null) |
---|
| 848 | { |
---|
| 849 | if (func_num_args() == 1 && $var) { |
---|
| 850 | // 'selected' if var is true. |
---|
| 851 | echo ' selected="selected" '; |
---|
| 852 | } else if (func_num_args() == 2 && $var == $value) { |
---|
| 853 | // 'selected' if var and value match. |
---|
| 854 | echo ' selected="selected" '; |
---|
| 855 | } else if (func_num_args() == 2 && is_array($var)) { |
---|
| 856 | // 'selected' if the value is in the key or the value of an array. |
---|
| 857 | if (isset($var[$value])) { |
---|
| 858 | echo ' selected="selected" '; |
---|
| 859 | } else if (in_array($value, $var)) { |
---|
| 860 | echo ' selected="selected" '; |
---|
| 861 | } |
---|
| 862 | } |
---|
| 863 | } |
---|
| 864 | |
---|
| 865 | /** |
---|
[111] | 866 | * Adds slashes to values of an array and converts the array to a comma |
---|
| 867 | * delimited list. If value provided is a string return the string |
---|
| 868 | * escaped. This is useful for putting values coming in from posted |
---|
| 869 | * checkboxes into a SET column of a database. |
---|
[1] | 870 | * |
---|
[454] | 871 | * |
---|
[111] | 872 | * @param array $in Array to convert. |
---|
[1] | 873 | * @return string Comma list of array values. |
---|
| 874 | */ |
---|
[224] | 875 | function escapedList($in, $separator="', '") |
---|
[1] | 876 | { |
---|
[479] | 877 | $db =& DB::getInstance(); |
---|
[454] | 878 | |
---|
[111] | 879 | if (is_array($in) && !empty($in)) { |
---|
[224] | 880 | return join($separator, array_map(array($db, 'escapeString'), $in)); |
---|
[111] | 881 | } else { |
---|
[136] | 882 | return $db->escapeString($in); |
---|
[1] | 883 | } |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | /** |
---|
[111] | 887 | * Converts a human string date into a SQL-safe date. Dates nearing |
---|
| 888 | * infinity use the date 2038-01-01 so conversion to unix time format |
---|
| 889 | * remain within valid range. |
---|
[1] | 890 | * |
---|
| 891 | * @param array $date String date to convert. |
---|
[42] | 892 | * @param array $format Date format to pass to date(). |
---|
[1] | 893 | * Default produces MySQL datetime: 0000-00-00 00:00:00. |
---|
| 894 | * @return string SQL-safe date. |
---|
| 895 | */ |
---|
| 896 | function strToSQLDate($date, $format='Y-m-d H:i:s') |
---|
| 897 | { |
---|
| 898 | // Translate the human string date into SQL-safe date format. |
---|
[247] | 899 | if (empty($date) || mb_strpos($date, '0000-00-00') !== false || strtotime($date) === -1 || strtotime($date) === false) { |
---|
[224] | 900 | // Return a string of zero time, formatted the same as $format. |
---|
| 901 | return strtr($format, array( |
---|
| 902 | 'Y' => '0000', |
---|
| 903 | 'm' => '00', |
---|
| 904 | 'd' => '00', |
---|
| 905 | 'H' => '00', |
---|
| 906 | 'i' => '00', |
---|
| 907 | 's' => '00', |
---|
| 908 | )); |
---|
[1] | 909 | } else { |
---|
[219] | 910 | return date($format, strtotime($date)); |
---|
[1] | 911 | } |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | /** |
---|
| 915 | * If magic_quotes_gpc is in use, run stripslashes() on $var. If $var is an |
---|
[334] | 916 | * array, stripslashes is run on each value, recursively, and the stripped |
---|
[51] | 917 | * array is returned. |
---|
[1] | 918 | * |
---|
| 919 | * @param mixed $var The string or array to un-quote, if necessary. |
---|
| 920 | * @return mixed $var, minus any magic quotes. |
---|
| 921 | */ |
---|
| 922 | function dispelMagicQuotes($var) |
---|
| 923 | { |
---|
| 924 | static $magic_quotes_gpc; |
---|
[42] | 925 | |
---|
[1] | 926 | if (!isset($magic_quotes_gpc)) { |
---|
| 927 | $magic_quotes_gpc = get_magic_quotes_gpc(); |
---|
| 928 | } |
---|
[42] | 929 | |
---|
[1] | 930 | if ($magic_quotes_gpc) { |
---|
| 931 | if (!is_array($var)) { |
---|
| 932 | $var = stripslashes($var); |
---|
| 933 | } else { |
---|
| 934 | foreach ($var as $key=>$val) { |
---|
| 935 | if (is_array($val)) { |
---|
| 936 | $var[$key] = dispelMagicQuotes($val); |
---|
| 937 | } else { |
---|
| 938 | $var[$key] = stripslashes($val); |
---|
| 939 | } |
---|
| 940 | } |
---|
| 941 | } |
---|
| 942 | } |
---|
| 943 | return $var; |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | /** |
---|
| 947 | * Get a form variable from GET or POST data, stripped of magic |
---|
| 948 | * quotes if necessary. |
---|
| 949 | * |
---|
| 950 | * @param string $var (optional) The name of the form variable to look for. |
---|
| 951 | * @param string $default (optional) The value to return if the |
---|
| 952 | * variable is not there. |
---|
| 953 | * @return mixed A cleaned GET or POST if no $var specified. |
---|
| 954 | * @return string A cleaned form $var if found, or $default. |
---|
| 955 | */ |
---|
| 956 | function getFormData($var=null, $default=null) |
---|
| 957 | { |
---|
| 958 | if ('POST' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
| 959 | return dispelMagicQuotes($_POST); |
---|
| 960 | } else if ('GET' == getenv('REQUEST_METHOD') && is_null($var)) { |
---|
| 961 | return dispelMagicQuotes($_GET); |
---|
| 962 | } |
---|
| 963 | if (isset($_POST[$var])) { |
---|
[51] | 964 | return dispelMagicQuotes($_POST[$var]); |
---|
[1] | 965 | } else if (isset($_GET[$var])) { |
---|
[51] | 966 | return dispelMagicQuotes($_GET[$var]); |
---|
[1] | 967 | } else { |
---|
| 968 | return $default; |
---|
| 969 | } |
---|
| 970 | } |
---|
| 971 | function getPost($var=null, $default=null) |
---|
| 972 | { |
---|
| 973 | if (is_null($var)) { |
---|
[51] | 974 | return dispelMagicQuotes($_POST); |
---|
[1] | 975 | } |
---|
| 976 | if (isset($_POST[$var])) { |
---|
[51] | 977 | return dispelMagicQuotes($_POST[$var]); |
---|
[1] | 978 | } else { |
---|
| 979 | return $default; |
---|
| 980 | } |
---|
| 981 | } |
---|
| 982 | function getGet($var=null, $default=null) |
---|
| 983 | { |
---|
| 984 | if (is_null($var)) { |
---|
[51] | 985 | return dispelMagicQuotes($_GET); |
---|
[1] | 986 | } |
---|
| 987 | if (isset($_GET[$var])) { |
---|
[51] | 988 | return dispelMagicQuotes($_GET[$var]); |
---|
[1] | 989 | } else { |
---|
| 990 | return $default; |
---|
| 991 | } |
---|
| 992 | } |
---|
| 993 | |
---|
[361] | 994 | /* |
---|
| 995 | * Sets a $_GET or $_POST variable. |
---|
| 996 | * |
---|
| 997 | * @access public |
---|
| 998 | * @param string $key The key of the request array to set. |
---|
| 999 | * @param mixed $val The value to save in the request array. |
---|
| 1000 | * @return void |
---|
| 1001 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 1002 | * @version 1.0 |
---|
| 1003 | * @since 01 Nov 2009 12:25:29 |
---|
| 1004 | */ |
---|
| 1005 | function putFormData($key, $val) |
---|
| 1006 | { |
---|
| 1007 | if ('POST' == getenv('REQUEST_METHOD')) { |
---|
| 1008 | $_POST[$key] = $val; |
---|
| 1009 | } else if ('GET' == getenv('REQUEST_METHOD')) { |
---|
| 1010 | $_GET[$key] = $val; |
---|
| 1011 | } |
---|
| 1012 | } |
---|
| 1013 | |
---|
[1] | 1014 | /** |
---|
| 1015 | * Signs a value using md5 and a simple text key. In order for this |
---|
[502] | 1016 | * function to be useful (i.e. secure) the salt must be kept secret, which |
---|
[1] | 1017 | * means keeping it as safe as database credentials. Putting it into an |
---|
| 1018 | * environment variable set in httpd.conf is a good place. |
---|
| 1019 | * |
---|
| 1020 | * @access public |
---|
| 1021 | * @param string $val The string to sign. |
---|
[159] | 1022 | * @param string $salt (Optional) A text key to use for computing the signature. |
---|
[282] | 1023 | * @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] | 1024 | * @return string The original value with a signature appended. |
---|
| 1025 | */ |
---|
[282] | 1026 | function addSignature($val, $salt=null, $length=18) |
---|
[1] | 1027 | { |
---|
[159] | 1028 | $app =& App::getInstance(); |
---|
[454] | 1029 | |
---|
[159] | 1030 | if ('' == trim($val)) { |
---|
[201] | 1031 | $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__); |
---|
[159] | 1032 | return ''; |
---|
[1] | 1033 | } |
---|
[42] | 1034 | |
---|
[159] | 1035 | if (!isset($salt)) { |
---|
| 1036 | $salt = $app->getParam('signing_key'); |
---|
[1] | 1037 | } |
---|
[454] | 1038 | |
---|
[500] | 1039 | switch ($app->getParam('signing_method')) { |
---|
| 1040 | case 'sha512+base64': |
---|
| 1041 | return $val . '-' . mb_substr(preg_replace('/[^\w]/', '', base64_encode(hash('sha512', $val . $salt, true))), 0, $length); |
---|
| 1042 | |
---|
| 1043 | case 'md5': |
---|
| 1044 | default: |
---|
| 1045 | return $val . '-' . mb_strtolower(mb_substr(md5($salt . md5($val . $salt)), 0, $length)); |
---|
| 1046 | } |
---|
[1] | 1047 | } |
---|
| 1048 | |
---|
| 1049 | /** |
---|
| 1050 | * Strips off the signature appended by addSignature(). |
---|
| 1051 | * |
---|
| 1052 | * @access public |
---|
| 1053 | * @param string $signed_val The string to sign. |
---|
| 1054 | * @return string The original value with a signature removed. |
---|
| 1055 | */ |
---|
| 1056 | function removeSignature($signed_val) |
---|
| 1057 | { |
---|
[249] | 1058 | if (empty($signed_val) || mb_strpos($signed_val, '-') === false) { |
---|
| 1059 | return ''; |
---|
| 1060 | } |
---|
[247] | 1061 | return mb_substr($signed_val, 0, mb_strrpos($signed_val, '-')); |
---|
[1] | 1062 | } |
---|
| 1063 | |
---|
| 1064 | /** |
---|
[500] | 1065 | * Verifies a signature appended to a value by addSignature(). |
---|
[1] | 1066 | * |
---|
| 1067 | * @access public |
---|
| 1068 | * @param string $signed_val A value with appended signature. |
---|
[159] | 1069 | * @param string $salt (Optional) A text key to use for computing the signature. |
---|
[502] | 1070 | * @param string $length (Optional) The length of the added signature. |
---|
[1] | 1071 | * @return bool True if the signature matches the var. |
---|
| 1072 | */ |
---|
[282] | 1073 | function verifySignature($signed_val, $salt=null, $length=18) |
---|
[1] | 1074 | { |
---|
| 1075 | // Strip the value from the signed value. |
---|
[22] | 1076 | $val = removeSignature($signed_val); |
---|
[1] | 1077 | // If the signed value matches the original signed value we consider the value safe. |
---|
[282] | 1078 | if ($signed_val == addSignature($val, $salt, $length)) { |
---|
[1] | 1079 | // Signature verified. |
---|
| 1080 | return true; |
---|
| 1081 | } else { |
---|
[500] | 1082 | $app =& App::getInstance(); |
---|
| 1083 | $app->logMsg(sprintf('Failed signature (%s should be %s)', $signed_val, addSignature($val, $salt, $length)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1084 | return false; |
---|
| 1085 | } |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | /** |
---|
| 1089 | * Sends empty output to the browser and flushes the php buffer so the client |
---|
[42] | 1090 | * will see data before the page is finished processing. |
---|
[1] | 1091 | */ |
---|
[235] | 1092 | function flushBuffer() |
---|
| 1093 | { |
---|
[1] | 1094 | echo str_repeat(' ', 205); |
---|
| 1095 | flush(); |
---|
| 1096 | } |
---|
| 1097 | |
---|
| 1098 | /** |
---|
| 1099 | * Adds email address to mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
| 1100 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
| 1101 | * |
---|
| 1102 | * @access public |
---|
| 1103 | * @param string $email Email address to add. |
---|
| 1104 | * @param string $list Name of list to add to. |
---|
| 1105 | * @param bool $send_welcome_message True to send welcome message to subscriber. |
---|
| 1106 | * @return bool True on success, false on failure. |
---|
| 1107 | */ |
---|
| 1108 | function mailmanAddMember($email, $list, $send_welcome_message=false) |
---|
| 1109 | { |
---|
[479] | 1110 | $app =& App::getInstance(); |
---|
[454] | 1111 | |
---|
[479] | 1112 | $add_members = '/usr/lib/mailman/bin/add_members'; |
---|
[468] | 1113 | // FIXME: checking of executable is disabled. |
---|
[264] | 1114 | if (true || is_executable($add_members) && is_readable($add_members)) { |
---|
[1] | 1115 | $welcome_msg = $send_welcome_message ? 'y' : 'n'; |
---|
[241] | 1116 | 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] | 1117 | if (0 == $return_code) { |
---|
[348] | 1118 | $app->logMsg(sprintf('Mailman add member success for list: %s, user: %s', $list, $email), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 1119 | return true; |
---|
| 1120 | } else { |
---|
[369] | 1121 | $app->logMsg(sprintf('Mailman add member failed for list: %s, user: %s, with message: %s', $list, $email, getDump($stdout)), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 1122 | return false; |
---|
| 1123 | } |
---|
| 1124 | } else { |
---|
[136] | 1125 | $app->logMsg(sprintf('Mailman add member program not executable: %s', $add_members), LOG_ALERT, __FILE__, __LINE__); |
---|
[1] | 1126 | return false; |
---|
| 1127 | } |
---|
| 1128 | } |
---|
| 1129 | |
---|
| 1130 | /** |
---|
| 1131 | * Removes email address from mailman mailing list. Requires /etc/sudoers entry for apache to sudo execute add_members. |
---|
| 1132 | * Don't forget to allow php_admin_value open_basedir access to "/var/mailman/bin". |
---|
| 1133 | * |
---|
| 1134 | * @access public |
---|
| 1135 | * @param string $email Email address to add. |
---|
| 1136 | * @param string $list Name of list to add to. |
---|
| 1137 | * @param bool $send_user_ack True to send goodbye message to subscriber. |
---|
| 1138 | * @return bool True on success, false on failure. |
---|
| 1139 | */ |
---|
| 1140 | function mailmanRemoveMember($email, $list, $send_user_ack=false) |
---|
| 1141 | { |
---|
[479] | 1142 | $app =& App::getInstance(); |
---|
[454] | 1143 | |
---|
[241] | 1144 | $remove_members = '/usr/lib/mailman/bin/remove_members'; |
---|
[468] | 1145 | // FIXME: checking of executable is disabled. |
---|
[264] | 1146 | if (true || is_executable($remove_members) && is_readable($remove_members)) { |
---|
[1] | 1147 | $userack = $send_user_ack ? '' : '--nouserack'; |
---|
[241] | 1148 | exec(sprintf("/usr/bin/sudo %s %s --noadminack '%s' '%s'", escapeshellarg($remove_members), $userack, escapeshellarg($list), escapeshellarg($email)), $stdout, $return_code); |
---|
[1] | 1149 | if (0 == $return_code) { |
---|
[348] | 1150 | $app->logMsg(sprintf('Mailman remove member success for list: %s, user: %s', $list, $email), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 1151 | return true; |
---|
| 1152 | } else { |
---|
[136] | 1153 | $app->logMsg(sprintf('Mailman remove member failed for list: %s, user: %s, with message: %s', $list, $email, $stdout), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 1154 | return false; |
---|
| 1155 | } |
---|
| 1156 | } else { |
---|
[136] | 1157 | $app->logMsg(sprintf('Mailman remove member program not executable: %s', $remove_members), LOG_ALERT, __FILE__, __LINE__); |
---|
[1] | 1158 | return false; |
---|
| 1159 | } |
---|
| 1160 | } |
---|
| 1161 | |
---|
[497] | 1162 | /* |
---|
| 1163 | * Returns the remote IP address, taking into consideration proxy servers. |
---|
| 1164 | * |
---|
| 1165 | * If strict checking is enabled, we will only trust REMOTE_ADDR or an HTTP header |
---|
| 1166 | * value if REMOTE_ADDR is a trusted proxy (configured as an array in $cfg['trusted_proxies']). |
---|
| 1167 | * |
---|
| 1168 | * @access public |
---|
| 1169 | * @param bool $dolookup Resolve to IP to a hostname? |
---|
| 1170 | * @param bool $trust_all_proxies Should we trust any IP address set in HTTP_* variables? Set to FALSE for secure usage. |
---|
| 1171 | * @return mixed Canonicalized IP address (or a corresponding hostname if $dolookup is true), or false if no IP was found. |
---|
| 1172 | * @author Alix Axel <http://stackoverflow.com/a/2031935/277303> |
---|
| 1173 | * @author Corey Ballou <http://blackbe.lt/advanced-method-to-obtain-the-client-ip-in-php/> |
---|
| 1174 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 1175 | * @version 1.0 |
---|
| 1176 | * @since 12 Sep 2014 19:07:46 |
---|
| 1177 | */ |
---|
| 1178 | function getRemoteAddr($dolookup=false, $trust_all_proxies=true) |
---|
[1] | 1179 | { |
---|
[497] | 1180 | global $cfg; |
---|
| 1181 | |
---|
| 1182 | if (!isset($_SERVER['REMOTE_ADDR'])) { |
---|
[507] | 1183 | // In some cases this won't be set, e.g., CLI scripts. |
---|
| 1184 | return null; |
---|
[497] | 1185 | } |
---|
| 1186 | |
---|
| 1187 | // Use an HTTP header value only if $trust_all_proxies is true or when REMOTE_ADDR is in our $cfg['trusted_proxies'] array. |
---|
| 1188 | // $cfg['trusted_proxies'] is an array of proxy server addresses we expect to see in REMOTE_ADDR. |
---|
| 1189 | if ($trust_all_proxies || isset($cfg['trusted_proxies']) && is_array($cfg['trusted_proxies']) && in_array($_SERVER['REMOTE_ADDR'], $cfg['trusted_proxies'], true)) { |
---|
| 1190 | // Then it's probably safe to use an IP address value set in an HTTP header. |
---|
| 1191 | // Loop through possible IP address headers. |
---|
| 1192 | foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED') as $key) { |
---|
| 1193 | // Loop through and if |
---|
| 1194 | if (array_key_exists($key, $_SERVER)) { |
---|
| 1195 | foreach (explode(',', $_SERVER[$key]) as $addr) { |
---|
| 1196 | $addr = canonicalIPAddr(trim($addr)); |
---|
| 1197 | if (false !== filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
---|
| 1198 | return $dolookup && '' != $addr ? gethostbyaddr($addr) : $addr; |
---|
| 1199 | } |
---|
| 1200 | } |
---|
| 1201 | } |
---|
[290] | 1202 | } |
---|
[1] | 1203 | } |
---|
[497] | 1204 | |
---|
| 1205 | $addr = canonicalIPAddr(trim($_SERVER['REMOTE_ADDR'])); |
---|
| 1206 | return $dolookup && $addr ? gethostbyaddr($addr) : $addr; |
---|
[1] | 1207 | } |
---|
| 1208 | |
---|
[497] | 1209 | /* |
---|
| 1210 | * Converts an ipv4 IP address in hexadecimal form into canonical form (i.e., it removes the prefix). |
---|
| 1211 | * |
---|
| 1212 | * @access public |
---|
| 1213 | * @param string $addr IP address. |
---|
| 1214 | * @return string Canonical IP address. |
---|
| 1215 | * @author Sander Steffann <http://stackoverflow.com/a/12436099/277303> |
---|
| 1216 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 1217 | * @version 1.0 |
---|
| 1218 | * @since 15 Sep 2012 |
---|
| 1219 | */ |
---|
| 1220 | function canonicalIPAddr($addr) |
---|
| 1221 | { |
---|
| 1222 | // Known prefix |
---|
| 1223 | $v4mapped_prefix_bin = pack('H*', '00000000000000000000ffff'); |
---|
| 1224 | |
---|
| 1225 | // Parse |
---|
| 1226 | $addr_bin = inet_pton($addr); |
---|
| 1227 | |
---|
| 1228 | // Check prefix |
---|
| 1229 | if (substr($addr_bin, 0, strlen($v4mapped_prefix_bin)) == $v4mapped_prefix_bin) { |
---|
| 1230 | // Strip prefix |
---|
| 1231 | $addr_bin = substr($addr_bin, strlen($v4mapped_prefix_bin)); |
---|
| 1232 | } |
---|
| 1233 | |
---|
| 1234 | // Convert back to printable address in canonical form |
---|
| 1235 | return inet_ntop($addr_bin); |
---|
| 1236 | } |
---|
| 1237 | |
---|
[1] | 1238 | /** |
---|
| 1239 | * Tests whether a given IP address can be found in an array of IP address networks. |
---|
| 1240 | * Elements of networks array can be single IP addresses or an IP address range in CIDR notation |
---|
| 1241 | * See: http://en.wikipedia.org/wiki/Classless_inter-domain_routing |
---|
| 1242 | * |
---|
| 1243 | * @access public |
---|
| 1244 | * @param string IP address to search for. |
---|
| 1245 | * @param array Array of networks to search within. |
---|
| 1246 | * @return mixed Returns the network that matched on success, false on failure. |
---|
| 1247 | */ |
---|
[497] | 1248 | function ipInRange($addr, $networks) |
---|
[1] | 1249 | { |
---|
| 1250 | if (!is_array($networks)) { |
---|
| 1251 | $networks = array($networks); |
---|
| 1252 | } |
---|
[42] | 1253 | |
---|
[497] | 1254 | $addr_binary = sprintf('%032b', ip2long($addr)); |
---|
[1] | 1255 | foreach ($networks as $network) { |
---|
| 1256 | if (preg_match('![\d\.]{7,15}/\d{1,2}!', $network)) { |
---|
| 1257 | // IP is in CIDR notation. |
---|
[247] | 1258 | list($cidr_ip, $cidr_bitmask) = explode('/', $network); |
---|
[1] | 1259 | $cidr_ip_binary = sprintf('%032b', ip2long($cidr_ip)); |
---|
[497] | 1260 | if (mb_substr($addr_binary, 0, $cidr_bitmask) === mb_substr($cidr_ip_binary, 0, $cidr_bitmask)) { |
---|
[1] | 1261 | // IP address is within the specified IP range. |
---|
| 1262 | return $network; |
---|
| 1263 | } |
---|
| 1264 | } else { |
---|
[497] | 1265 | if ($addr === $network) { |
---|
[1] | 1266 | // IP address exactly matches. |
---|
| 1267 | return $network; |
---|
| 1268 | } |
---|
| 1269 | } |
---|
| 1270 | } |
---|
[42] | 1271 | |
---|
[1] | 1272 | return false; |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | /** |
---|
[159] | 1276 | * If the given $url is on the same web site, return true. This can be used to |
---|
| 1277 | * prevent from sending sensitive info in a get query (like the SID) to another |
---|
| 1278 | * domain. |
---|
| 1279 | * |
---|
| 1280 | * @param string $url the URI to test. |
---|
| 1281 | * @return bool True if given $url is our domain or has no domain (is a relative url), false if it's another. |
---|
| 1282 | */ |
---|
| 1283 | function isMyDomain($url) |
---|
| 1284 | { |
---|
| 1285 | static $urls = array(); |
---|
| 1286 | |
---|
| 1287 | if (!isset($urls[$url])) { |
---|
| 1288 | if (!preg_match('|https?://[\w.]+/|', $url)) { |
---|
| 1289 | // If we can't find a domain we assume the URL is local (i.e. "/my/url/path/" or "../img/file.jpg"). |
---|
| 1290 | $urls[$url] = true; |
---|
| 1291 | } else { |
---|
| 1292 | $urls[$url] = preg_match('|https?://[\w.]*' . preg_quote(getenv('HTTP_HOST'), '|') . '|i', $url); |
---|
| 1293 | } |
---|
| 1294 | } |
---|
| 1295 | return $urls[$url]; |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | /** |
---|
| 1299 | * Takes a URL and returns it without the query or anchor portion |
---|
| 1300 | * |
---|
| 1301 | * @param string $url any kind of URI |
---|
| 1302 | * @return string the URI with ? or # and everything after removed |
---|
| 1303 | */ |
---|
| 1304 | function stripQuery($url) |
---|
| 1305 | { |
---|
[336] | 1306 | return preg_replace('/[?#].*$/', '', $url); |
---|
[159] | 1307 | } |
---|
| 1308 | |
---|
| 1309 | /** |
---|
[42] | 1310 | * Returns a fully qualified URL to the current script, including the query. |
---|
[1] | 1311 | * |
---|
| 1312 | * @return string a full url to the current script |
---|
| 1313 | */ |
---|
| 1314 | function absoluteMe() |
---|
| 1315 | { |
---|
| 1316 | $protocol = ('on' == getenv('HTTPS')) ? 'https://' : 'http://'; |
---|
| 1317 | return $protocol . getenv('HTTP_HOST') . getenv('REQUEST_URI'); |
---|
| 1318 | } |
---|
| 1319 | |
---|
| 1320 | /** |
---|
| 1321 | * Compares the current url with the referring url. |
---|
| 1322 | * |
---|
[159] | 1323 | * @param bool $exclude_query Remove the query string first before comparing. |
---|
[334] | 1324 | * @return bool True if the current URL is the same as the referring URL, false otherwise. |
---|
[1] | 1325 | */ |
---|
| 1326 | function refererIsMe($exclude_query=false) |
---|
| 1327 | { |
---|
| 1328 | if ($exclude_query) { |
---|
| 1329 | return (stripQuery(absoluteMe()) == stripQuery(getenv('HTTP_REFERER'))); |
---|
| 1330 | } else { |
---|
| 1331 | return (absoluteMe() == getenv('HTTP_REFERER')); |
---|
| 1332 | } |
---|
| 1333 | } |
---|
[520] | 1334 | |
---|
| 1335 | /* |
---|
| 1336 | * Returns true if the given URL resolves to a resource with a HTTP 200 header response. |
---|
| 1337 | * |
---|
| 1338 | * @access public |
---|
| 1339 | * @param string $url URL to a file. |
---|
| 1340 | * @return bool True if the resource exists, false otherwise. |
---|
| 1341 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 1342 | * @version 1.0 |
---|
| 1343 | * @since 02 May 2015 15:10:09 |
---|
| 1344 | */ |
---|
| 1345 | function httpExists($url) |
---|
| 1346 | { |
---|
| 1347 | $ch = curl_init($url); |
---|
| 1348 | curl_setopt($ch, CURLOPT_NOBODY, true); |
---|
| 1349 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
---|
| 1350 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
---|
| 1351 | curl_exec($ch); |
---|
| 1352 | return '200' == curl_getinfo($ch, CURLINFO_HTTP_CODE); |
---|
| 1353 | } |
---|