From 36a67e1cf74880f38f1478239e219755f1477382 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Sat, 11 Oct 2008 16:38:43 +0000 Subject: [PATCH] Revert r41603, r41751 (splitting Preferences validation into its own class). Breaks a lot of things per comments on Code Review by Tim. --- includes/AutoLoader.php | 1 - includes/Validate.php | 105 ----------------------- includes/specials/SpecialPreferences.php | 104 +++++++++++++++++++--- 3 files changed, 91 insertions(+), 119 deletions(-) delete mode 100644 includes/Validate.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 16c80139b6..49e763cb0f 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -200,7 +200,6 @@ $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 deleted file mode 100644 index 476df32b93..0000000000 --- a/includes/Validate.php +++ /dev/null @@ -1,105 +0,0 @@ -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 ea98e00197..b18a9fa0b9 100644 --- a/includes/specials/SpecialPreferences.php +++ b/includes/specials/SpecialPreferences.php @@ -124,6 +124,84 @@ 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 @@ -195,21 +273,21 @@ class PreferencesForm { if( $wgUseTeX ) { $wgUser->setOption( 'math', $this->mMath ); } - $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( '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( 'imagesize', $this->mImageSize ); $wgUser->setOption( 'thumbsize', $this->mThumbSize ); - $wgUser->setOption( 'underline', Validate::int($this->mUnderline, 0, 2) ); - $wgUser->setOption( 'watchlistdays', Validate::float( $this->mWatchlistDays, 0, 7 ) ); + $wgUser->setOption( 'underline', $this->validateInt($this->mUnderline, 0, 2) ); + $wgUser->setOption( 'watchlistdays', $this->validateFloat( $this->mWatchlistDays, 0, 7 ) ); $wgUser->setOption( 'disablesuggest', $this->mDisableMWSuggest ); # Set search namespace options -- 2.20.1