@@ -22,7 +22,8 @@ discard block |
||
| 22 | 22 | /** |
| 23 | 23 | * Filter input and escape output. |
| 24 | 24 | */ |
| 25 | -class Filter { |
|
| 25 | +class Filter |
|
| 26 | +{ |
|
| 26 | 27 | // REGEX to match a URL |
| 27 | 28 | // Some versions of RFC3987 have an appendix B which gives the following regex |
| 28 | 29 | // (([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? |
@@ -37,7 +38,8 @@ discard block |
||
| 37 | 38 | * |
| 38 | 39 | * @return string |
| 39 | 40 | */ |
| 40 | - public static function escapeHtml($string) { |
|
| 41 | + public static function escapeHtml($string) |
|
| 42 | + { |
|
| 41 | 43 | if (defined('ENT_SUBSTITUTE')) { |
| 42 | 44 | // PHP5.4 allows us to substitute invalid UTF8 sequences |
| 43 | 45 | return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
@@ -53,7 +55,8 @@ discard block |
||
| 53 | 55 | * |
| 54 | 56 | * @return string |
| 55 | 57 | */ |
| 56 | - public static function escapeUrl($string) { |
|
| 58 | + public static function escapeUrl($string) |
|
| 59 | + { |
|
| 57 | 60 | return rawurlencode($string); |
| 58 | 61 | } |
| 59 | 62 | |
@@ -64,7 +67,8 @@ discard block |
||
| 64 | 67 | * |
| 65 | 68 | * @return string |
| 66 | 69 | */ |
| 67 | - public static function escapeJs($string) { |
|
| 70 | + public static function escapeJs($string) |
|
| 71 | + { |
|
| 68 | 72 | return preg_replace_callback('/[^A-Za-z0-9,. _]/Su', function ($x) { |
| 69 | 73 | if (strlen($x[0]) == 1) { |
| 70 | 74 | return sprintf('\\x%02X', ord($x[0])); |
@@ -85,7 +89,8 @@ discard block |
||
| 85 | 89 | * |
| 86 | 90 | * @return string |
| 87 | 91 | */ |
| 88 | - public static function escapeLike($string) { |
|
| 92 | + public static function escapeLike($string) |
|
| 93 | + { |
|
| 89 | 94 | return strtr( |
| 90 | 95 | $string, |
| 91 | 96 | array( |
@@ -103,7 +108,8 @@ discard block |
||
| 103 | 108 | * |
| 104 | 109 | * @return string |
| 105 | 110 | */ |
| 106 | - public static function unescapeHtml($string) { |
|
| 111 | + public static function unescapeHtml($string) |
|
| 112 | + { |
|
| 107 | 113 | return html_entity_decode(strip_tags($string), ENT_QUOTES, 'UTF-8'); |
| 108 | 114 | } |
| 109 | 115 | |
@@ -115,7 +121,8 @@ discard block |
||
| 115 | 121 | * |
| 116 | 122 | * @return string |
| 117 | 123 | */ |
| 118 | - public static function formatText($text, Tree $WT_TREE) { |
|
| 124 | + public static function formatText($text, Tree $WT_TREE) |
|
| 125 | + { |
|
| 119 | 126 | switch ($WT_TREE->getPreference('FORMAT_TEXT')) { |
| 120 | 127 | case 'markdown': |
| 121 | 128 | return '<div class="markdown" dir="auto">' . self::markdown($text) . '</div>'; |
@@ -131,7 +138,8 @@ discard block |
||
| 131 | 138 | * |
| 132 | 139 | * @return string |
| 133 | 140 | */ |
| 134 | - public static function expandUrls($text) { |
|
| 141 | + public static function expandUrls($text) |
|
| 142 | + { |
|
| 135 | 143 | return preg_replace_callback( |
| 136 | 144 | '/' . addcslashes('(?!>)' . self::URL_REGEX . '(?!</a>)', '/') . '/i', |
| 137 | 145 | function ($m) { |
@@ -148,7 +156,8 @@ discard block |
||
| 148 | 156 | * |
| 149 | 157 | * @return string |
| 150 | 158 | */ |
| 151 | - public static function markdown($text) { |
|
| 159 | + public static function markdown($text) |
|
| 160 | + { |
|
| 152 | 161 | $parser = new MarkdownExtra; |
| 153 | 162 | $parser->empty_element_suffix = '>'; |
| 154 | 163 | $parser->no_markup = true; |
@@ -179,7 +188,8 @@ discard block |
||
| 179 | 188 | * |
| 180 | 189 | * @return string|null |
| 181 | 190 | */ |
| 182 | - private static function input($source, $variable, $regexp = null, $default = null) { |
|
| 191 | + private static function input($source, $variable, $regexp = null, $default = null) |
|
| 192 | + { |
|
| 183 | 193 | if ($regexp) { |
| 184 | 194 | return filter_input( |
| 185 | 195 | $source, |
@@ -218,7 +228,8 @@ discard block |
||
| 218 | 228 | * |
| 219 | 229 | * @return string[] |
| 220 | 230 | */ |
| 221 | - private static function inputArray($source, $variable, $regexp = null, $default = null) { |
|
| 231 | + private static function inputArray($source, $variable, $regexp = null, $default = null) |
|
| 232 | + { |
|
| 222 | 233 | if ($regexp) { |
| 223 | 234 | // PHP5.3 requires the $tmp variable |
| 224 | 235 | $tmp = filter_input_array( |
@@ -264,7 +275,8 @@ discard block |
||
| 264 | 275 | * |
| 265 | 276 | * @return null|string |
| 266 | 277 | */ |
| 267 | - public static function get($variable, $regexp = null, $default = null) { |
|
| 278 | + public static function get($variable, $regexp = null, $default = null) |
|
| 279 | + { |
|
| 268 | 280 | return self::input(INPUT_GET, $variable, $regexp, $default); |
| 269 | 281 | } |
| 270 | 282 | |
@@ -277,7 +289,8 @@ discard block |
||
| 277 | 289 | * |
| 278 | 290 | * @return string[] |
| 279 | 291 | */ |
| 280 | - public static function getArray($variable, $regexp = null, $default = null) { |
|
| 292 | + public static function getArray($variable, $regexp = null, $default = null) |
|
| 293 | + { |
|
| 281 | 294 | return self::inputArray(INPUT_GET, $variable, $regexp, $default); |
| 282 | 295 | } |
| 283 | 296 | |
@@ -288,7 +301,8 @@ discard block |
||
| 288 | 301 | * |
| 289 | 302 | * @return bool |
| 290 | 303 | */ |
| 291 | - public static function getBool($variable) { |
|
| 304 | + public static function getBool($variable) |
|
| 305 | + { |
|
| 292 | 306 | return (bool) filter_input(INPUT_GET, $variable, FILTER_VALIDATE_BOOLEAN); |
| 293 | 307 | } |
| 294 | 308 | |
@@ -302,7 +316,8 @@ discard block |
||
| 302 | 316 | * |
| 303 | 317 | * @return int |
| 304 | 318 | */ |
| 305 | - public static function getInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) { |
|
| 319 | + public static function getInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) |
|
| 320 | + { |
|
| 306 | 321 | return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default))); |
| 307 | 322 | } |
| 308 | 323 | |
@@ -314,7 +329,8 @@ discard block |
||
| 314 | 329 | * |
| 315 | 330 | * @return null|string |
| 316 | 331 | */ |
| 317 | - public static function getEmail($variable, $default = null) { |
|
| 332 | + public static function getEmail($variable, $default = null) |
|
| 333 | + { |
|
| 318 | 334 | return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_EMAIL) ?: $default; |
| 319 | 335 | } |
| 320 | 336 | |
@@ -326,7 +342,8 @@ discard block |
||
| 326 | 342 | * |
| 327 | 343 | * @return null|string |
| 328 | 344 | */ |
| 329 | - public static function getUrl($variable, $default = null) { |
|
| 345 | + public static function getUrl($variable, $default = null) |
|
| 346 | + { |
|
| 330 | 347 | return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_URL) ?: $default; |
| 331 | 348 | } |
| 332 | 349 | |
@@ -339,7 +356,8 @@ discard block |
||
| 339 | 356 | * |
| 340 | 357 | * @return null|string |
| 341 | 358 | */ |
| 342 | - public static function post($variable, $regexp = null, $default = null) { |
|
| 359 | + public static function post($variable, $regexp = null, $default = null) |
|
| 360 | + { |
|
| 343 | 361 | return self::input(INPUT_POST, $variable, $regexp, $default); |
| 344 | 362 | } |
| 345 | 363 | |
@@ -352,7 +370,8 @@ discard block |
||
| 352 | 370 | * |
| 353 | 371 | * @return string[] |
| 354 | 372 | */ |
| 355 | - public static function postArray($variable, $regexp = null, $default = null) { |
|
| 373 | + public static function postArray($variable, $regexp = null, $default = null) |
|
| 374 | + { |
|
| 356 | 375 | return self::inputArray(INPUT_POST, $variable, $regexp, $default); |
| 357 | 376 | } |
| 358 | 377 | |
@@ -363,7 +382,8 @@ discard block |
||
| 363 | 382 | * |
| 364 | 383 | * @return bool |
| 365 | 384 | */ |
| 366 | - public static function postBool($variable) { |
|
| 385 | + public static function postBool($variable) |
|
| 386 | + { |
|
| 367 | 387 | return (bool) filter_input(INPUT_POST, $variable, FILTER_VALIDATE_BOOLEAN); |
| 368 | 388 | } |
| 369 | 389 | |
@@ -377,7 +397,8 @@ discard block |
||
| 377 | 397 | * |
| 378 | 398 | * @return int |
| 379 | 399 | */ |
| 380 | - public static function postInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) { |
|
| 400 | + public static function postInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) |
|
| 401 | + { |
|
| 381 | 402 | return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default))); |
| 382 | 403 | } |
| 383 | 404 | |
@@ -389,7 +410,8 @@ discard block |
||
| 389 | 410 | * |
| 390 | 411 | * @return null|string |
| 391 | 412 | */ |
| 392 | - public static function postEmail($variable, $default = null) { |
|
| 413 | + public static function postEmail($variable, $default = null) |
|
| 414 | + { |
|
| 393 | 415 | return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_EMAIL) ?: $default; |
| 394 | 416 | } |
| 395 | 417 | |
@@ -401,7 +423,8 @@ discard block |
||
| 401 | 423 | * |
| 402 | 424 | * @return null|string |
| 403 | 425 | */ |
| 404 | - public static function postUrl($variable, $default = null) { |
|
| 426 | + public static function postUrl($variable, $default = null) |
|
| 427 | + { |
|
| 405 | 428 | return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_URL) ?: $default; |
| 406 | 429 | } |
| 407 | 430 | |
@@ -414,7 +437,8 @@ discard block |
||
| 414 | 437 | * |
| 415 | 438 | * @return null|string |
| 416 | 439 | */ |
| 417 | - public static function cookie($variable, $regexp = null, $default = null) { |
|
| 440 | + public static function cookie($variable, $regexp = null, $default = null) |
|
| 441 | + { |
|
| 418 | 442 | return self::input(INPUT_COOKIE, $variable, $regexp, $default); |
| 419 | 443 | } |
| 420 | 444 | |
@@ -427,7 +451,8 @@ discard block |
||
| 427 | 451 | * |
| 428 | 452 | * @return null|string |
| 429 | 453 | */ |
| 430 | - public static function server($variable, $regexp = null, $default = null) { |
|
| 454 | + public static function server($variable, $regexp = null, $default = null) |
|
| 455 | + { |
|
| 431 | 456 | // On some servers, variables that are present in $_SERVER cannot be |
| 432 | 457 | // found via filter_input(INPUT_SERVER). Instead, they are found via |
| 433 | 458 | // filter_input(INPUT_ENV). Since we cannot rely on filter_input(), |
@@ -446,7 +471,8 @@ discard block |
||
| 446 | 471 | * |
| 447 | 472 | * @return string |
| 448 | 473 | */ |
| 449 | - public static function getCsrfToken() { |
|
| 474 | + public static function getCsrfToken() |
|
| 475 | + { |
|
| 450 | 476 | if (!Session::has('CSRF_TOKEN')) { |
| 451 | 477 | $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789'; |
| 452 | 478 | $csrf_token = ''; |
@@ -464,7 +490,8 @@ discard block |
||
| 464 | 490 | * |
| 465 | 491 | * @return string |
| 466 | 492 | */ |
| 467 | - public static function getCsrf() { |
|
| 493 | + public static function getCsrf() |
|
| 494 | + { |
|
| 468 | 495 | return '<input type="hidden" name="csrf" value="' . self::getCsrfToken() . '">'; |
| 469 | 496 | } |
| 470 | 497 | |
@@ -473,7 +500,8 @@ discard block |
||
| 473 | 500 | * |
| 474 | 501 | * @return bool |
| 475 | 502 | */ |
| 476 | - public static function checkCsrf() { |
|
| 503 | + public static function checkCsrf() |
|
| 504 | + { |
|
| 477 | 505 | if (self::post('csrf') !== self::getCsrfToken()) { |
| 478 | 506 | // Oops. Something is not quite right |
| 479 | 507 | FlashMessages::addMessage(I18N::translate('This form has expired. Try again.'), 'error'); |