3 * Validate - A bunch of static methods used to validate user input
4 * Split from Special:Preferences in 1.14 for more general use
9 * Given an inputed integer, determine if it's within a given
10 * range. If not, return the upper or lower boundary, whichever
12 * @param int $val A user inputted integer
13 * @param int $min The lower limit
14 * @param int $max The upper limit
17 public static function int( &$val, $min=0, $max=0x7fffffff ) {
19 $val = min($val, $max);
20 $val = max($val, $min);
25 * Given an inputed float, determine if it's within a given
26 * range. If not, return the upper or lower boundary, whichever
28 * @param float $val User inputed value
29 * @param float $min Lower limit
30 * @param float $max Uppser limit
33 public static function float( &$val, $min, $max=0x7fffffff ) {
34 $val = floatval( $val );
35 $val = min( $val, $max );
36 $val = max( $val, $min );
41 * Like int(), only return null if it's not a string
42 * @see Validate::int()
43 * @param int $val User given integer
44 * @param int $min Lower limit
45 * @param int $max Upper limit
46 * @return mixed [int or null]
48 public static function intOrNull( &$val, $min=0, $max=0x7fffffff ) {
53 return self
:: int( $val, $min, $max );
58 * Given a date string, validate to see if it's an acceptable type. If
59 * not, return an acceptable one.
60 * @param string $val User inputed date
63 public static function dateFormat( $val ) {
64 global $wgLang, $wgContLang;
65 if ( $val !== false && (
66 in_array( $val, (array)$wgLang->getDatePreferences() ) ||
67 in_array( $val, (array)$wgContLang->getDatePreferences() ) ) )
71 return $wgLang->getDefaultDateFormat();
76 * Used to validate the user inputed timezone before saving it as
77 * 'timecorrection', will return '00:00' if fed bogus data.
78 * Note: It's not a 100% correct implementation timezone-wise, it will
79 * accept stuff like '14:30',
80 * @param string $s the user input
83 public static function timeZone( $s ) {
85 if ( strpos( $s, ':' ) ) {
87 $array = explode( ':' , $s );
88 $hour = intval( $array[0] );
89 $minute = intval( $array[1] );
91 $minute = intval( $s * 60 );
92 $hour = intval( $minute / 60 );
93 $minute = abs( $minute ) %
60;
95 # Max is +14:00 and min is -12:00, see:
96 # http://en.wikipedia.org/wiki/Timezone
97 $hour = min( $hour, 14 );
98 $hour = max( $hour, -12 );
99 $minute = min( $minute, 59 );
100 $minute = max( $minute, 0 );
101 $s = sprintf( "%02d:%02d", $hour, $minute );