From c76d2ee33ce5387846cfd24ab002f8f32a046210 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 3 Oct 2008 13:32:55 +0000 Subject: [PATCH] Split off some of these validation methods into their own class called Validate so they can be useful elsewhere :) --- includes/AutoLoader.php | 1 + includes/Validate.php | 105 +++++++++++++++++++++++ includes/specials/SpecialPreferences.php | 104 +++------------------- 3 files changed, 119 insertions(+), 91 deletions(-) create mode 100644 includes/Validate.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index c1f9a2ef2f..f9897e2180 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -199,6 +199,7 @@ $wgAutoloadLocalClasses = array( 'UserArrayFromResult' => 'includes/UserArray.php', 'UserMailer' => 'includes/UserMailer.php', 'UserRightsProxy' => 'includes/UserRightsProxy.php', + 'Validate' => 'includes/Validate.php', 'WatchedItem' => 'includes/WatchedItem.php', 'WatchlistEditor' => 'includes/WatchlistEditor.php', 'WebRequest' => 'includes/WebRequest.php', diff --git a/includes/Validate.php b/includes/Validate.php new file mode 100644 index 0000000000..9dc3f37670 --- /dev/null +++ b/includes/Validate.php @@ -0,0 +1,105 @@ +getDatePreferences() ) || + in_array( $val, (array)$wgContLang->getDatePreferences() ) ) ) + { + return $val; + } else { + return $wgLang->getDefaultDateFormat(); + } + } + + /** + * Used to validate the user inputed timezone before saving it as + * 'timecorrection', will return '00:00' if fed bogus data. + * Note: It's not a 100% correct implementation timezone-wise, it will + * accept stuff like '14:30', + * @param string $s the user input + * @return string + */ + public static function timeZone( $s ) { + if ( $s !== '' ) { + if ( strpos( $s, ':' ) ) { + # HH:MM + $array = explode( ':' , $s ); + $hour = intval( $array[0] ); + $minute = intval( $array[1] ); + } else { + $minute = intval( $s * 60 ); + $hour = intval( $minute / 60 ); + $minute = abs( $minute ) % 60; + } + # Max is +14:00 and min is -12:00, see: + # http://en.wikipedia.org/wiki/Timezone + $hour = min( $hour, 14 ); + $hour = max( $hour, -12 ); + $minute = min( $minute, 59 ); + $minute = max( $minute, 0 ); + $s = sprintf( "%02d:%02d", $hour, $minute ); + } + return $s; + } +} diff --git a/includes/specials/SpecialPreferences.php b/includes/specials/SpecialPreferences.php index fa5f7930dc..ca1e3d33bd 100644 --- a/includes/specials/SpecialPreferences.php +++ b/includes/specials/SpecialPreferences.php @@ -124,84 +124,6 @@ class PreferencesForm { $this->mainPrefsForm( '' ); } } - /** - * @access private - */ - function validateInt( &$val, $min=0, $max=0x7fffffff ) { - $val = intval($val); - $val = min($val, $max); - $val = max($val, $min); - return $val; - } - - /** - * @access private - */ - function validateFloat( &$val, $min, $max=0x7fffffff ) { - $val = floatval( $val ); - $val = min( $val, $max ); - $val = max( $val, $min ); - return( $val ); - } - - /** - * @access private - */ - function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) { - $val = trim($val); - if($val === '') { - return null; - } else { - return $this->validateInt( $val, $min, $max ); - } - } - - /** - * @access private - */ - function validateDate( $val ) { - global $wgLang, $wgContLang; - if ( $val !== false && ( - in_array( $val, (array)$wgLang->getDatePreferences() ) || - in_array( $val, (array)$wgContLang->getDatePreferences() ) ) ) - { - return $val; - } else { - return $wgLang->getDefaultDateFormat(); - } - } - - /** - * Used to validate the user inputed timezone before saving it as - * 'timecorrection', will return '00:00' if fed bogus data. - * Note: It's not a 100% correct implementation timezone-wise, it will - * accept stuff like '14:30', - * @access private - * @param string $s the user input - * @return string - */ - function validateTimeZone( $s ) { - if ( $s !== '' ) { - if ( strpos( $s, ':' ) ) { - # HH:MM - $array = explode( ':' , $s ); - $hour = intval( $array[0] ); - $minute = intval( $array[1] ); - } else { - $minute = intval( $s * 60 ); - $hour = intval( $minute / 60 ); - $minute = abs( $minute ) % 60; - } - # Max is +14:00 and min is -12:00, see: - # http://en.wikipedia.org/wiki/Timezone - $hour = min( $hour, 14 ); - $hour = max( $hour, -12 ); - $minute = min( $minute, 59 ); - $minute = max( $minute, 0 ); - $s = sprintf( "%02d:%02d", $hour, $minute ); - } - return $s; - } /** * @access private @@ -273,21 +195,21 @@ class PreferencesForm { if( $wgUseTeX ) { $wgUser->setOption( 'math', $this->mMath ); } - $wgUser->setOption( 'date', $this->validateDate( $this->mDate ) ); - $wgUser->setOption( 'searchlimit', $this->validateIntOrNull( $this->mSearch ) ); - $wgUser->setOption( 'contextlines', $this->validateIntOrNull( $this->mSearchLines ) ); - $wgUser->setOption( 'contextchars', $this->validateIntOrNull( $this->mSearchChars ) ); - $wgUser->setOption( 'rclimit', $this->validateIntOrNull( $this->mRecent ) ); - $wgUser->setOption( 'rcdays', $this->validateInt($this->mRecentDays, 1, ceil($wgRCMaxAge / (3600*24)))); - $wgUser->setOption( 'wllimit', $this->validateIntOrNull( $this->mWatchlistEdits, 0, 1000 ) ); - $wgUser->setOption( 'rows', $this->validateInt( $this->mRows, 4, 1000 ) ); - $wgUser->setOption( 'cols', $this->validateInt( $this->mCols, 4, 1000 ) ); - $wgUser->setOption( 'stubthreshold', $this->validateIntOrNull( $this->mStubs ) ); - $wgUser->setOption( 'timecorrection', $this->validateTimeZone( $this->mHourDiff, -12, 14 ) ); + $wgUser->setOption( 'date', Validate::dateFormat( $this->mDate ) ); + $wgUser->setOption( 'searchlimit', Validate::intOrNull( $this->mSearch ) ); + $wgUser->setOption( 'contextlines', Validate::intOrNull( $this->mSearchLines ) ); + $wgUser->setOption( 'contextchars', Validate::intOrNull( $this->mSearchChars ) ); + $wgUser->setOption( 'rclimit', Validate::intOrNull( $this->mRecent ) ); + $wgUser->setOption( 'rcdays', Validate::int($this->mRecentDays, 1, ceil($wgRCMaxAge / (3600*24)))); + $wgUser->setOption( 'wllimit', Validate::intOrNull( $this->mWatchlistEdits, 0, 1000 ) ); + $wgUser->setOption( 'rows', Validate::int( $this->mRows, 4, 1000 ) ); + $wgUser->setOption( 'cols', Validate::int( $this->mCols, 4, 1000 ) ); + $wgUser->setOption( 'stubthreshold', Validate::intOrNull( $this->mStubs ) ); + $wgUser->setOption( 'timecorrection', Validate::timeZone( $this->mHourDiff, -12, 14 ) ); $wgUser->setOption( 'imagesize', $this->mImageSize ); $wgUser->setOption( 'thumbsize', $this->mThumbSize ); - $wgUser->setOption( 'underline', $this->validateInt($this->mUnderline, 0, 2) ); - $wgUser->setOption( 'watchlistdays', $this->validateFloat( $this->mWatchlistDays, 0, 7 ) ); + $wgUser->setOption( 'underline', Validate::int($this->mUnderline, 0, 2) ); + $wgUser->setOption( 'watchlistdays', Validate::float( $this->mWatchlistDays, 0, 7 ) ); $wgUser->setOption( 'disablesuggest', $this->mDisableMWSuggest ); # Set search namespace options -- 2.20.1