Passed
Push — 1.7 ( 400c71...151717 )
by Greg
06:23
created
app/Filter.php 2 patches
Indentation   +459 added lines, -459 removed lines patch added patch discarded remove patch
@@ -23,464 +23,464 @@
 block discarded – undo
23 23
  * Filter input and escape output.
24 24
  */
25 25
 class Filter {
26
-	// REGEX to match a URL
27
-	// Some versions of RFC3987 have an appendix B which gives the following regex
28
-	// (([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
29
-	// This matches far too much while a “precise” regex is several pages long.
30
-	// This is a compromise.
31
-	const URL_REGEX = '((https?|ftp]):)(//([^\s/?#<>]*))?([^\s?#<>]*)(\?([^\s#<>]*))?(#[^\s?#<>]+)?';
32
-
33
-	/**
34
-	 * Escape a string for use in HTML
35
-	 *
36
-	 * @param string $string
37
-	 *
38
-	 * @return string
39
-	 */
40
-	public static function escapeHtml($string) {
41
-		if (defined('ENT_SUBSTITUTE')) {
42
-			// PHP5.4 allows us to substitute invalid UTF8 sequences
43
-			return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
44
-		} else {
45
-			return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
46
-		}
47
-	}
48
-
49
-	/**
50
-	 * Escape a string for use in a URL
51
-	 *
52
-	 * @param string $string
53
-	 *
54
-	 * @return string
55
-	 */
56
-	public static function escapeUrl($string) {
57
-		return rawurlencode($string);
58
-	}
59
-
60
-	/**
61
-	 * Escape a string for use in Javascript
62
-	 *
63
-	 * @param string $string
64
-	 *
65
-	 * @return string
66
-	 */
67
-	public static function escapeJs($string) {
68
-		return preg_replace_callback('/[^A-Za-z0-9,. _]/Su', function ($x) {
69
-			if (strlen($x[0]) == 1) {
70
-				return sprintf('\\x%02X', ord($x[0]));
71
-			} elseif (function_exists('iconv')) {
72
-				return sprintf('\\u%04s', strtoupper(bin2hex(iconv('UTF-8', 'UTF-16BE', $x[0]))));
73
-			} elseif (function_exists('mb_convert_encoding')) {
74
-				return sprintf('\\u%04s', strtoupper(bin2hex(mb_convert_encoding($x[0], 'UTF-16BE', 'UTF-8'))));
75
-			} else {
76
-				return $x[0];
77
-			}
78
-		}, $string);
79
-	}
80
-
81
-	/**
82
-	 * Escape a string for use in a SQL "LIKE" clause
83
-	 *
84
-	 * @param string $string
85
-	 *
86
-	 * @return string
87
-	 */
88
-	public static function escapeLike($string) {
89
-		return strtr(
90
-			$string,
91
-			array(
92
-				'\\' => '\\\\',
93
-				'%'  => '\%',
94
-				'_'  => '\_',
95
-			)
96
-		);
97
-	}
98
-
99
-	/**
100
-	 * Unescape an HTML string, giving just the literal text
101
-	 *
102
-	 * @param string $string
103
-	 *
104
-	 * @return string
105
-	 */
106
-	public static function unescapeHtml($string) {
107
-		return html_entity_decode(strip_tags($string), ENT_QUOTES, 'UTF-8');
108
-	}
109
-
110
-	/**
111
-	 * Format block-level text such as notes or transcripts, etc.
112
-	 *
113
-	 * @param string  $text
114
-	 * @param Tree $WT_TREE
115
-	 *
116
-	 * @return string
117
-	 */
118
-	public static function formatText($text, Tree $WT_TREE) {
119
-		switch ($WT_TREE->getPreference('FORMAT_TEXT')) {
120
-		case 'markdown':
121
-			return '<div class="markdown" dir="auto">' . self::markdown($text) . '</div>';
122
-		default:
123
-			return '<div style="white-space: pre-wrap;" dir="auto">' . self::expandUrls($text) . '</div>';
124
-		}
125
-	}
126
-
127
-	/**
128
-	 * Escape a string for use in HTML, and additionally convert URLs to links.
129
-	 *
130
-	 * @param string $text
131
-	 *
132
-	 * @return string
133
-	 */
134
-	public static function expandUrls($text) {
135
-		return preg_replace_callback(
136
-			'/' . addcslashes('(?!>)' . self::URL_REGEX . '(?!</a>)', '/') . '/i',
137
-			function ($m) {
138
-				return '<a href="' . $m[0] . '">' . $m[0] . '</a>';
139
-			},
140
-			self::escapeHtml($text)
141
-		);
142
-	}
143
-
144
-	/**
145
-	 * Format a block of text, using "Markdown".
146
-	 *
147
-	 * @param string $text
148
-	 *
149
-	 * @return string
150
-	 */
151
-	public static function markdown($text) {
152
-		$parser                       = new MarkdownExtra;
153
-		$parser->empty_element_suffix = '>';
154
-		$parser->no_markup            = true;
155
-		$text                         = $parser->transform($text);
156
-
157
-		// HTMLPurifier needs somewhere to write temporary files
158
-		$HTML_PURIFIER_CACHE_DIR = WT_DATA_DIR . 'html_purifier_cache';
159
-
160
-		if (!is_dir($HTML_PURIFIER_CACHE_DIR)) {
161
-			mkdir($HTML_PURIFIER_CACHE_DIR);
162
-		}
163
-
164
-		$config = HTMLPurifier_Config::createDefault();
165
-		$config->set('Cache.SerializerPath', $HTML_PURIFIER_CACHE_DIR);
166
-		$purifier = new HTMLPurifier($config);
167
-		$text     = $purifier->purify($text);
168
-
169
-		return $text;
170
-	}
171
-
172
-	/**
173
-	 * Validate INPUT parameters
174
-	 *
175
-	 * @param string      $source
176
-	 * @param string      $variable
177
-	 * @param string|null $regexp
178
-	 * @param string|null $default
179
-	 *
180
-	 * @return string|null
181
-	 */
182
-	private static function input($source, $variable, $regexp = null, $default = null) {
183
-		if ($regexp) {
184
-			return filter_input(
185
-				$source,
186
-				$variable,
187
-				FILTER_VALIDATE_REGEXP,
188
-				array(
189
-					'options' => array(
190
-						'regexp'  => '/^(' . $regexp . ')$/u',
191
-						'default' => $default,
192
-					),
193
-				)
194
-			);
195
-		} else {
196
-			$tmp = filter_input(
197
-				$source,
198
-				$variable,
199
-				FILTER_CALLBACK,
200
-				array(
201
-					'options' => function ($x) {
202
-						return mb_check_encoding($x, 'UTF-8') ? $x : false;
203
-					},
204
-				)
205
-			);
206
-
207
-			return ($tmp === null || $tmp === false) ? $default : $tmp;
208
-		}
209
-	}
210
-
211
-	/**
212
-	 * Validate array INPUT parameters
213
-	 *
214
-	 * @param string      $source
215
-	 * @param string      $variable
216
-	 * @param string|null $regexp
217
-	 * @param string|null $default
218
-	 *
219
-	 * @return string[]
220
-	 */
221
-	private static function inputArray($source, $variable, $regexp = null, $default = null) {
222
-		if ($regexp) {
223
-			// PHP5.3 requires the $tmp variable
224
-			$tmp = filter_input_array(
225
-				$source,
226
-				array(
227
-					$variable => array(
228
-						'flags'   => FILTER_REQUIRE_ARRAY,
229
-						'filter'  => FILTER_VALIDATE_REGEXP,
230
-						'options' => array(
231
-							'regexp'  => '/^(' . $regexp . ')$/u',
232
-							'default' => $default,
233
-						),
234
-					),
235
-				)
236
-			);
237
-
238
-			return $tmp[$variable] ?: array();
239
-		} else {
240
-			// PHP5.3 requires the $tmp variable
241
-			$tmp = filter_input_array(
242
-				$source,
243
-				array(
244
-					$variable => array(
245
-						'flags'   => FILTER_REQUIRE_ARRAY,
246
-						'filter'  => FILTER_CALLBACK,
247
-						'options' => function ($x) {
248
-							return !function_exists('mb_convert_encoding') || mb_check_encoding($x, 'UTF-8') ? $x : false;
249
-						},
250
-					),
251
-				)
252
-			);
253
-
254
-			return $tmp[$variable] ?: array();
255
-		}
256
-	}
257
-
258
-	/**
259
-	 * Validate GET parameters
260
-	 *
261
-	 * @param string      $variable
262
-	 * @param string|null $regexp
263
-	 * @param string|null $default
264
-	 *
265
-	 * @return null|string
266
-	 */
267
-	public static function get($variable, $regexp = null, $default = null) {
268
-		return self::input(INPUT_GET, $variable, $regexp, $default);
269
-	}
270
-
271
-	/**
272
-	 * Validate array GET parameters
273
-	 *
274
-	 * @param string      $variable
275
-	 * @param string|null $regexp
276
-	 * @param string|null $default
277
-	 *
278
-	 * @return string[]
279
-	 */
280
-	public static function getArray($variable, $regexp = null, $default = null) {
281
-		return self::inputArray(INPUT_GET, $variable, $regexp, $default);
282
-	}
283
-
284
-	/**
285
-	 * Validate boolean GET parameters
286
-	 *
287
-	 * @param string $variable
288
-	 *
289
-	 * @return bool
290
-	 */
291
-	public static function getBool($variable) {
292
-		return (bool) filter_input(INPUT_GET, $variable, FILTER_VALIDATE_BOOLEAN);
293
-	}
294
-
295
-	/**
296
-	 * Validate integer GET parameters
297
-	 *
298
-	 * @param string $variable
299
-	 * @param int    $min
300
-	 * @param int    $max
301
-	 * @param int    $default
302
-	 *
303
-	 * @return int
304
-	 */
305
-	public static function getInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) {
306
-		return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default)));
307
-	}
308
-
309
-	/**
310
-	 * Validate email GET parameters
311
-	 *
312
-	 * @param string      $variable
313
-	 * @param string|null $default
314
-	 *
315
-	 * @return null|string
316
-	 */
317
-	public static function getEmail($variable, $default = null) {
318
-		return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_EMAIL) ?: $default;
319
-	}
320
-
321
-	/**
322
-	 * Validate URL GET parameters
323
-	 *
324
-	 * @param string      $variable
325
-	 * @param string|null $default
326
-	 *
327
-	 * @return null|string
328
-	 */
329
-	public static function getUrl($variable, $default = null) {
330
-		return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_URL) ?: $default;
331
-	}
332
-
333
-	/**
334
-	 * Validate POST parameters
335
-	 *
336
-	 * @param string      $variable
337
-	 * @param string|null $regexp
338
-	 * @param string|null $default
339
-	 *
340
-	 * @return null|string
341
-	 */
342
-	public static function post($variable, $regexp = null, $default = null) {
343
-		return self::input(INPUT_POST, $variable, $regexp, $default);
344
-	}
345
-
346
-	/**
347
-	 * Validate array POST parameters
348
-	 *
349
-	 * @param string      $variable
350
-	 * @param string|null $regexp
351
-	 * @param string|null $default
352
-	 *
353
-	 * @return string[]
354
-	 */
355
-	public static function postArray($variable, $regexp = null, $default = null) {
356
-		return self::inputArray(INPUT_POST, $variable, $regexp, $default);
357
-	}
358
-
359
-	/**
360
-	 * Validate boolean POST parameters
361
-	 *
362
-	 * @param string $variable
363
-	 *
364
-	 * @return bool
365
-	 */
366
-	public static function postBool($variable) {
367
-		return (bool) filter_input(INPUT_POST, $variable, FILTER_VALIDATE_BOOLEAN);
368
-	}
369
-
370
-	/**
371
-	 * Validate integer POST parameters
372
-	 *
373
-	 * @param string $variable
374
-	 * @param int    $min
375
-	 * @param int    $max
376
-	 * @param int    $default
377
-	 *
378
-	 * @return int
379
-	 */
380
-	public static function postInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) {
381
-		return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default)));
382
-	}
383
-
384
-	/**
385
-	 * Validate email POST parameters
386
-	 *
387
-	 * @param string      $variable
388
-	 * @param string|null $default
389
-	 *
390
-	 * @return null|string
391
-	 */
392
-	public static function postEmail($variable, $default = null) {
393
-		return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_EMAIL) ?: $default;
394
-	}
395
-
396
-	/**
397
-	 * Validate URL GET parameters
398
-	 *
399
-	 * @param string      $variable
400
-	 * @param string|null $default
401
-	 *
402
-	 * @return null|string
403
-	 */
404
-	public static function postUrl($variable, $default = null) {
405
-		return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_URL) ?: $default;
406
-	}
407
-
408
-	/**
409
-	 * Validate COOKIE parameters
410
-	 *
411
-	 * @param string      $variable
412
-	 * @param string|null $regexp
413
-	 * @param string|null $default
414
-	 *
415
-	 * @return null|string
416
-	 */
417
-	public static function cookie($variable, $regexp = null, $default = null) {
418
-		return self::input(INPUT_COOKIE, $variable, $regexp, $default);
419
-	}
420
-
421
-	/**
422
-	 * Validate SERVER parameters
423
-	 *
424
-	 * @param string      $variable
425
-	 * @param string|null $regexp
426
-	 * @param string|null $default
427
-	 *
428
-	 * @return null|string
429
-	 */
430
-	public static function server($variable, $regexp = null, $default = null) {
431
-		// On some servers, variables that are present in $_SERVER cannot be
432
-		// found via filter_input(INPUT_SERVER). Instead, they are found via
433
-		// filter_input(INPUT_ENV). Since we cannot rely on filter_input(),
434
-		// we must use the superglobal directly.
435
-		if (array_key_exists($variable, $_SERVER) && ($regexp === null || preg_match('/^(' . $regexp . ')$/',
26
+    // REGEX to match a URL
27
+    // Some versions of RFC3987 have an appendix B which gives the following regex
28
+    // (([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
29
+    // This matches far too much while a “precise” regex is several pages long.
30
+    // This is a compromise.
31
+    const URL_REGEX = '((https?|ftp]):)(//([^\s/?#<>]*))?([^\s?#<>]*)(\?([^\s#<>]*))?(#[^\s?#<>]+)?';
32
+
33
+    /**
34
+     * Escape a string for use in HTML
35
+     *
36
+     * @param string $string
37
+     *
38
+     * @return string
39
+     */
40
+    public static function escapeHtml($string) {
41
+        if (defined('ENT_SUBSTITUTE')) {
42
+            // PHP5.4 allows us to substitute invalid UTF8 sequences
43
+            return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
44
+        } else {
45
+            return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
46
+        }
47
+    }
48
+
49
+    /**
50
+     * Escape a string for use in a URL
51
+     *
52
+     * @param string $string
53
+     *
54
+     * @return string
55
+     */
56
+    public static function escapeUrl($string) {
57
+        return rawurlencode($string);
58
+    }
59
+
60
+    /**
61
+     * Escape a string for use in Javascript
62
+     *
63
+     * @param string $string
64
+     *
65
+     * @return string
66
+     */
67
+    public static function escapeJs($string) {
68
+        return preg_replace_callback('/[^A-Za-z0-9,. _]/Su', function ($x) {
69
+            if (strlen($x[0]) == 1) {
70
+                return sprintf('\\x%02X', ord($x[0]));
71
+            } elseif (function_exists('iconv')) {
72
+                return sprintf('\\u%04s', strtoupper(bin2hex(iconv('UTF-8', 'UTF-16BE', $x[0]))));
73
+            } elseif (function_exists('mb_convert_encoding')) {
74
+                return sprintf('\\u%04s', strtoupper(bin2hex(mb_convert_encoding($x[0], 'UTF-16BE', 'UTF-8'))));
75
+            } else {
76
+                return $x[0];
77
+            }
78
+        }, $string);
79
+    }
80
+
81
+    /**
82
+     * Escape a string for use in a SQL "LIKE" clause
83
+     *
84
+     * @param string $string
85
+     *
86
+     * @return string
87
+     */
88
+    public static function escapeLike($string) {
89
+        return strtr(
90
+            $string,
91
+            array(
92
+                '\\' => '\\\\',
93
+                '%'  => '\%',
94
+                '_'  => '\_',
95
+            )
96
+        );
97
+    }
98
+
99
+    /**
100
+     * Unescape an HTML string, giving just the literal text
101
+     *
102
+     * @param string $string
103
+     *
104
+     * @return string
105
+     */
106
+    public static function unescapeHtml($string) {
107
+        return html_entity_decode(strip_tags($string), ENT_QUOTES, 'UTF-8');
108
+    }
109
+
110
+    /**
111
+     * Format block-level text such as notes or transcripts, etc.
112
+     *
113
+     * @param string  $text
114
+     * @param Tree $WT_TREE
115
+     *
116
+     * @return string
117
+     */
118
+    public static function formatText($text, Tree $WT_TREE) {
119
+        switch ($WT_TREE->getPreference('FORMAT_TEXT')) {
120
+        case 'markdown':
121
+            return '<div class="markdown" dir="auto">' . self::markdown($text) . '</div>';
122
+        default:
123
+            return '<div style="white-space: pre-wrap;" dir="auto">' . self::expandUrls($text) . '</div>';
124
+        }
125
+    }
126
+
127
+    /**
128
+     * Escape a string for use in HTML, and additionally convert URLs to links.
129
+     *
130
+     * @param string $text
131
+     *
132
+     * @return string
133
+     */
134
+    public static function expandUrls($text) {
135
+        return preg_replace_callback(
136
+            '/' . addcslashes('(?!>)' . self::URL_REGEX . '(?!</a>)', '/') . '/i',
137
+            function ($m) {
138
+                return '<a href="' . $m[0] . '">' . $m[0] . '</a>';
139
+            },
140
+            self::escapeHtml($text)
141
+        );
142
+    }
143
+
144
+    /**
145
+     * Format a block of text, using "Markdown".
146
+     *
147
+     * @param string $text
148
+     *
149
+     * @return string
150
+     */
151
+    public static function markdown($text) {
152
+        $parser                       = new MarkdownExtra;
153
+        $parser->empty_element_suffix = '>';
154
+        $parser->no_markup            = true;
155
+        $text                         = $parser->transform($text);
156
+
157
+        // HTMLPurifier needs somewhere to write temporary files
158
+        $HTML_PURIFIER_CACHE_DIR = WT_DATA_DIR . 'html_purifier_cache';
159
+
160
+        if (!is_dir($HTML_PURIFIER_CACHE_DIR)) {
161
+            mkdir($HTML_PURIFIER_CACHE_DIR);
162
+        }
163
+
164
+        $config = HTMLPurifier_Config::createDefault();
165
+        $config->set('Cache.SerializerPath', $HTML_PURIFIER_CACHE_DIR);
166
+        $purifier = new HTMLPurifier($config);
167
+        $text     = $purifier->purify($text);
168
+
169
+        return $text;
170
+    }
171
+
172
+    /**
173
+     * Validate INPUT parameters
174
+     *
175
+     * @param string      $source
176
+     * @param string      $variable
177
+     * @param string|null $regexp
178
+     * @param string|null $default
179
+     *
180
+     * @return string|null
181
+     */
182
+    private static function input($source, $variable, $regexp = null, $default = null) {
183
+        if ($regexp) {
184
+            return filter_input(
185
+                $source,
186
+                $variable,
187
+                FILTER_VALIDATE_REGEXP,
188
+                array(
189
+                    'options' => array(
190
+                        'regexp'  => '/^(' . $regexp . ')$/u',
191
+                        'default' => $default,
192
+                    ),
193
+                )
194
+            );
195
+        } else {
196
+            $tmp = filter_input(
197
+                $source,
198
+                $variable,
199
+                FILTER_CALLBACK,
200
+                array(
201
+                    'options' => function ($x) {
202
+                        return mb_check_encoding($x, 'UTF-8') ? $x : false;
203
+                    },
204
+                )
205
+            );
206
+
207
+            return ($tmp === null || $tmp === false) ? $default : $tmp;
208
+        }
209
+    }
210
+
211
+    /**
212
+     * Validate array INPUT parameters
213
+     *
214
+     * @param string      $source
215
+     * @param string      $variable
216
+     * @param string|null $regexp
217
+     * @param string|null $default
218
+     *
219
+     * @return string[]
220
+     */
221
+    private static function inputArray($source, $variable, $regexp = null, $default = null) {
222
+        if ($regexp) {
223
+            // PHP5.3 requires the $tmp variable
224
+            $tmp = filter_input_array(
225
+                $source,
226
+                array(
227
+                    $variable => array(
228
+                        'flags'   => FILTER_REQUIRE_ARRAY,
229
+                        'filter'  => FILTER_VALIDATE_REGEXP,
230
+                        'options' => array(
231
+                            'regexp'  => '/^(' . $regexp . ')$/u',
232
+                            'default' => $default,
233
+                        ),
234
+                    ),
235
+                )
236
+            );
237
+
238
+            return $tmp[$variable] ?: array();
239
+        } else {
240
+            // PHP5.3 requires the $tmp variable
241
+            $tmp = filter_input_array(
242
+                $source,
243
+                array(
244
+                    $variable => array(
245
+                        'flags'   => FILTER_REQUIRE_ARRAY,
246
+                        'filter'  => FILTER_CALLBACK,
247
+                        'options' => function ($x) {
248
+                            return !function_exists('mb_convert_encoding') || mb_check_encoding($x, 'UTF-8') ? $x : false;
249
+                        },
250
+                    ),
251
+                )
252
+            );
253
+
254
+            return $tmp[$variable] ?: array();
255
+        }
256
+    }
257
+
258
+    /**
259
+     * Validate GET parameters
260
+     *
261
+     * @param string      $variable
262
+     * @param string|null $regexp
263
+     * @param string|null $default
264
+     *
265
+     * @return null|string
266
+     */
267
+    public static function get($variable, $regexp = null, $default = null) {
268
+        return self::input(INPUT_GET, $variable, $regexp, $default);
269
+    }
270
+
271
+    /**
272
+     * Validate array GET parameters
273
+     *
274
+     * @param string      $variable
275
+     * @param string|null $regexp
276
+     * @param string|null $default
277
+     *
278
+     * @return string[]
279
+     */
280
+    public static function getArray($variable, $regexp = null, $default = null) {
281
+        return self::inputArray(INPUT_GET, $variable, $regexp, $default);
282
+    }
283
+
284
+    /**
285
+     * Validate boolean GET parameters
286
+     *
287
+     * @param string $variable
288
+     *
289
+     * @return bool
290
+     */
291
+    public static function getBool($variable) {
292
+        return (bool) filter_input(INPUT_GET, $variable, FILTER_VALIDATE_BOOLEAN);
293
+    }
294
+
295
+    /**
296
+     * Validate integer GET parameters
297
+     *
298
+     * @param string $variable
299
+     * @param int    $min
300
+     * @param int    $max
301
+     * @param int    $default
302
+     *
303
+     * @return int
304
+     */
305
+    public static function getInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) {
306
+        return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default)));
307
+    }
308
+
309
+    /**
310
+     * Validate email GET parameters
311
+     *
312
+     * @param string      $variable
313
+     * @param string|null $default
314
+     *
315
+     * @return null|string
316
+     */
317
+    public static function getEmail($variable, $default = null) {
318
+        return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_EMAIL) ?: $default;
319
+    }
320
+
321
+    /**
322
+     * Validate URL GET parameters
323
+     *
324
+     * @param string      $variable
325
+     * @param string|null $default
326
+     *
327
+     * @return null|string
328
+     */
329
+    public static function getUrl($variable, $default = null) {
330
+        return filter_input(INPUT_GET, $variable, FILTER_VALIDATE_URL) ?: $default;
331
+    }
332
+
333
+    /**
334
+     * Validate POST parameters
335
+     *
336
+     * @param string      $variable
337
+     * @param string|null $regexp
338
+     * @param string|null $default
339
+     *
340
+     * @return null|string
341
+     */
342
+    public static function post($variable, $regexp = null, $default = null) {
343
+        return self::input(INPUT_POST, $variable, $regexp, $default);
344
+    }
345
+
346
+    /**
347
+     * Validate array POST parameters
348
+     *
349
+     * @param string      $variable
350
+     * @param string|null $regexp
351
+     * @param string|null $default
352
+     *
353
+     * @return string[]
354
+     */
355
+    public static function postArray($variable, $regexp = null, $default = null) {
356
+        return self::inputArray(INPUT_POST, $variable, $regexp, $default);
357
+    }
358
+
359
+    /**
360
+     * Validate boolean POST parameters
361
+     *
362
+     * @param string $variable
363
+     *
364
+     * @return bool
365
+     */
366
+    public static function postBool($variable) {
367
+        return (bool) filter_input(INPUT_POST, $variable, FILTER_VALIDATE_BOOLEAN);
368
+    }
369
+
370
+    /**
371
+     * Validate integer POST parameters
372
+     *
373
+     * @param string $variable
374
+     * @param int    $min
375
+     * @param int    $max
376
+     * @param int    $default
377
+     *
378
+     * @return int
379
+     */
380
+    public static function postInteger($variable, $min = 0, $max = PHP_INT_MAX, $default = 0) {
381
+        return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max, 'default' => $default)));
382
+    }
383
+
384
+    /**
385
+     * Validate email POST parameters
386
+     *
387
+     * @param string      $variable
388
+     * @param string|null $default
389
+     *
390
+     * @return null|string
391
+     */
392
+    public static function postEmail($variable, $default = null) {
393
+        return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_EMAIL) ?: $default;
394
+    }
395
+
396
+    /**
397
+     * Validate URL GET parameters
398
+     *
399
+     * @param string      $variable
400
+     * @param string|null $default
401
+     *
402
+     * @return null|string
403
+     */
404
+    public static function postUrl($variable, $default = null) {
405
+        return filter_input(INPUT_POST, $variable, FILTER_VALIDATE_URL) ?: $default;
406
+    }
407
+
408
+    /**
409
+     * Validate COOKIE parameters
410
+     *
411
+     * @param string      $variable
412
+     * @param string|null $regexp
413
+     * @param string|null $default
414
+     *
415
+     * @return null|string
416
+     */
417
+    public static function cookie($variable, $regexp = null, $default = null) {
418
+        return self::input(INPUT_COOKIE, $variable, $regexp, $default);
419
+    }
420
+
421
+    /**
422
+     * Validate SERVER parameters
423
+     *
424
+     * @param string      $variable
425
+     * @param string|null $regexp
426
+     * @param string|null $default
427
+     *
428
+     * @return null|string
429
+     */
430
+    public static function server($variable, $regexp = null, $default = null) {
431
+        // On some servers, variables that are present in $_SERVER cannot be
432
+        // found via filter_input(INPUT_SERVER). Instead, they are found via
433
+        // filter_input(INPUT_ENV). Since we cannot rely on filter_input(),
434
+        // we must use the superglobal directly.
435
+        if (array_key_exists($variable, $_SERVER) && ($regexp === null || preg_match('/^(' . $regexp . ')$/',
436 436
 $_SERVER[$variable]))) {
437
-			return $_SERVER[$variable];
438
-		} else {
439
-			return $default;
440
-		}
441
-	}
442
-
443
-	/**
444
-	 * Cross-Site Request Forgery tokens - ensure that the user is submitting
445
-	 * a form that was generated by the current session.
446
-	 *
447
-	 * @return string
448
-	 */
449
-	public static function getCsrfToken() {
450
-		if (!Session::has('CSRF_TOKEN')) {
451
-			$charset    = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789';
452
-			$csrf_token = '';
453
-			for ($n = 0; $n < 32; ++$n) {
454
-				$csrf_token .= substr($charset, mt_rand(0, 61), 1);
455
-			}
456
-			Session::put('CSRF_TOKEN', $csrf_token);
457
-		}
458
-
459
-		return Session::get('CSRF_TOKEN');
460
-	}
461
-
462
-	/**
463
-	 * Generate an <input> element - to protect the current form from CSRF attacks.
464
-	 *
465
-	 * @return string
466
-	 */
467
-	public static function getCsrf() {
468
-		return '<input type="hidden" name="csrf" value="' . self::getCsrfToken() . '">';
469
-	}
470
-
471
-	/**
472
-	 * Check that the POST request contains the CSRF token generated above.
473
-	 *
474
-	 * @return bool
475
-	 */
476
-	public static function checkCsrf() {
477
-		if (self::post('csrf') !== self::getCsrfToken()) {
478
-			// Oops. Something is not quite right
479
-			FlashMessages::addMessage(I18N::translate('This form has expired. Try again.'), 'error');
480
-
481
-			return false;
482
-		}
483
-
484
-		return true;
485
-	}
437
+            return $_SERVER[$variable];
438
+        } else {
439
+            return $default;
440
+        }
441
+    }
442
+
443
+    /**
444
+     * Cross-Site Request Forgery tokens - ensure that the user is submitting
445
+     * a form that was generated by the current session.
446
+     *
447
+     * @return string
448
+     */
449
+    public static function getCsrfToken() {
450
+        if (!Session::has('CSRF_TOKEN')) {
451
+            $charset    = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789';
452
+            $csrf_token = '';
453
+            for ($n = 0; $n < 32; ++$n) {
454
+                $csrf_token .= substr($charset, mt_rand(0, 61), 1);
455
+            }
456
+            Session::put('CSRF_TOKEN', $csrf_token);
457
+        }
458
+
459
+        return Session::get('CSRF_TOKEN');
460
+    }
461
+
462
+    /**
463
+     * Generate an <input> element - to protect the current form from CSRF attacks.
464
+     *
465
+     * @return string
466
+     */
467
+    public static function getCsrf() {
468
+        return '<input type="hidden" name="csrf" value="' . self::getCsrfToken() . '">';
469
+    }
470
+
471
+    /**
472
+     * Check that the POST request contains the CSRF token generated above.
473
+     *
474
+     * @return bool
475
+     */
476
+    public static function checkCsrf() {
477
+        if (self::post('csrf') !== self::getCsrfToken()) {
478
+            // Oops. Something is not quite right
479
+            FlashMessages::addMessage(I18N::translate('This form has expired. Try again.'), 'error');
480
+
481
+            return false;
482
+        }
483
+
484
+        return true;
485
+    }
486 486
 }
Please login to merge, or discard this patch.
Braces   +56 added lines, -28 removed lines patch added patch discarded remove patch
@@ -22,7 +22,8 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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');
Please login to merge, or discard this patch.