Replace wfRunHooks calls with direct Hooks::run calls
[lhc/web/wiklou.git] / includes / User.php
index e6425f8..bda825f 100644 (file)
@@ -58,6 +58,12 @@ class User implements IDBAccessObject {
         */
        const MAX_WATCHED_ITEMS_CACHE = 100;
 
+       /**
+        * Exclude user options that are set to their default value.
+        * @since 1.25
+        */
+       const GETOPTIONS_EXCLUDE_DEFAULTS = 1;
+
        /**
         * @var PasswordFactory Lazily loaded factory object for passwords
         */
@@ -112,6 +118,7 @@ class User implements IDBAccessObject {
                'deletelogentry',
                'deleterevision',
                'edit',
+               'editcontentmodel',
                'editinterface',
                'editprotected',
                'editmyoptions',
@@ -343,7 +350,7 @@ class User implements IDBAccessObject {
                                        // Loading from session failed. Load defaults.
                                        $this->loadDefaults();
                                }
-                               wfRunHooks( 'UserLoadAfterLoadFromSession', array( $this ) );
+                               Hooks::run( 'UserLoadAfterLoadFromSession', array( $this ) );
                                break;
                        default:
                                wfProfileOut( __METHOD__ );
@@ -641,10 +648,11 @@ class User implements IDBAccessObject {
                global $wgContLang, $wgMaxNameChars;
 
                if ( $name == ''
-               || User::isIP( $name )
-               || strpos( $name, '/' ) !== false
-               || strlen( $name ) > $wgMaxNameChars
-               || $name != $wgContLang->ucfirst( $name ) ) {
+                       || User::isIP( $name )
+                       || strpos( $name, '/' ) !== false
+                       || strlen( $name ) > $wgMaxNameChars
+                       || $name != $wgContLang->ucfirst( $name )
+               ) {
                        wfDebugLog( 'username', __METHOD__ .
                                ": '$name' invalid due to empty, IP, slash, length, or lowercase" );
                        return false;
@@ -701,7 +709,7 @@ class User implements IDBAccessObject {
                static $reservedUsernames = false;
                if ( !$reservedUsernames ) {
                        $reservedUsernames = $wgReservedUsernames;
-                       wfRunHooks( 'UserGetReservedNames', array( &$reservedUsernames ) );
+                       Hooks::run( 'UserGetReservedNames', array( &$reservedUsernames ) );
                }
 
                // Certain names may be reserved for batch processes.
@@ -809,7 +817,7 @@ class User implements IDBAccessObject {
 
                $result = false; //init $result to false for the internal checks
 
-               if ( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) ) {
+               if ( !Hooks::run( 'isValidPassword', array( $password, &$result, $this ) ) ) {
                        $status->error( $result );
                        return $status;
                }
@@ -871,7 +879,7 @@ class User implements IDBAccessObject {
                        );
                }
                // Give extensions a chance to force an expiration
-               wfRunHooks( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
+               Hooks::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
                $this->mPasswordExpires = $newExpire;
        }
 
@@ -1041,7 +1049,7 @@ class User implements IDBAccessObject {
                $this->mRegistration = wfTimestamp( TS_MW );
                $this->mGroups = array();
 
-               wfRunHooks( 'UserLoadDefaults', array( $this, $name ) );
+               Hooks::run( 'UserLoadDefaults', array( $this, $name ) );
 
                wfProfileOut( __METHOD__ );
        }
@@ -1080,7 +1088,7 @@ class User implements IDBAccessObject {
         */
        private function loadFromSession() {
                $result = null;
-               wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
+               Hooks::run( 'UserLoadFromSession', array( $this, &$result ) );
                if ( $result !== null ) {
                        return $result;
                }
@@ -1182,7 +1190,7 @@ class User implements IDBAccessObject {
                                : array()
                );
 
-               wfRunHooks( 'UserLoadFromDatabase', array( $this, &$s ) );
+               Hooks::run( 'UserLoadFromDatabase', array( $this, &$s ) );
 
                if ( $s !== false ) {
                        // Initialise user table data
@@ -1448,7 +1456,7 @@ class User implements IDBAccessObject {
                }
                $defOpt['skin'] = Skin::normalizeKey( $wgDefaultSkin );
 
-               wfRunHooks( 'UserGetDefaultOptions', array( &$defOpt ) );
+               Hooks::run( 'UserGetDefaultOptions', array( &$defOpt ) );
 
                return $defOpt;
        }
@@ -1554,7 +1562,7 @@ class User implements IDBAccessObject {
                }
 
                // Extensions
-               wfRunHooks( 'GetBlockedStatus', array( &$this ) );
+               Hooks::run( 'GetBlockedStatus', array( &$this ) );
 
                wfProfileOut( __METHOD__ );
        }
@@ -1694,7 +1702,7 @@ class User implements IDBAccessObject {
        public function pingLimiter( $action = 'edit', $incrBy = 1 ) {
                // Call the 'PingLimiter' hook
                $result = false;
-               if ( !wfRunHooks( 'PingLimiter', array( &$this, $action, &$result, $incrBy ) ) ) {
+               if ( !Hooks::run( 'PingLimiter', array( &$this, $action, &$result, $incrBy ) ) ) {
                        return $result;
                }
 
@@ -1838,7 +1846,7 @@ class User implements IDBAccessObject {
                        wfDebug( __METHOD__ . ": self-talk page, ignoring any blocks\n" );
                }
 
-               wfRunHooks( 'UserIsBlockedFrom', array( $this, $title, &$blocked, &$allowUsertalk ) );
+               Hooks::run( 'UserIsBlockedFrom', array( $this, $title, &$blocked, &$allowUsertalk ) );
 
                wfProfileOut( __METHOD__ );
                return $blocked;
@@ -1890,7 +1898,7 @@ class User implements IDBAccessObject {
                        $ip = $this->getRequest()->getIP();
                }
                $blocked = false;
-               wfRunHooks( 'UserIsBlockedGlobally', array( &$this, $ip, &$blocked ) );
+               Hooks::run( 'UserIsBlockedGlobally', array( &$this, $ip, &$blocked ) );
                $this->mBlockedGlobally = (bool)$blocked;
                return $this->mBlockedGlobally;
        }
@@ -2050,7 +2058,7 @@ class User implements IDBAccessObject {
         */
        public function getNewMessageLinks() {
                $talks = array();
-               if ( !wfRunHooks( 'UserRetrieveNewTalks', array( &$this, &$talks ) ) ) {
+               if ( !Hooks::run( 'UserRetrieveNewTalks', array( &$this, &$talks ) ) ) {
                        return $talks;
                } elseif ( !$this->getNewtalk() ) {
                        return array();
@@ -2427,7 +2435,7 @@ class User implements IDBAccessObject {
         */
        public function getEmail() {
                $this->load();
-               wfRunHooks( 'UserGetEmail', array( $this, &$this->mEmail ) );
+               Hooks::run( 'UserGetEmail', array( $this, &$this->mEmail ) );
                return $this->mEmail;
        }
 
@@ -2437,7 +2445,7 @@ class User implements IDBAccessObject {
         */
        public function getEmailAuthenticationTimestamp() {
                $this->load();
-               wfRunHooks( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
+               Hooks::run( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
                return $this->mEmailAuthenticated;
        }
 
@@ -2452,7 +2460,7 @@ class User implements IDBAccessObject {
                }
                $this->invalidateEmail();
                $this->mEmail = $str;
-               wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
+               Hooks::run( 'UserSetEmail', array( $this, &$this->mEmail ) );
        }
 
        /**
@@ -2545,9 +2553,12 @@ class User implements IDBAccessObject {
        /**
         * Get all user's options
         *
+        * @param int $flags Bitwise combination of:
+        *   User::GETOPTIONS_EXCLUDE_DEFAULTS  Exclude user options that are set
+        *                                      to the default value. (Since 1.25)
         * @return array
         */
-       public function getOptions() {
+       public function getOptions( $flags = 0 ) {
                global $wgHiddenPrefs;
                $this->loadOptions();
                $options = $this->mOptions;
@@ -2564,6 +2575,10 @@ class User implements IDBAccessObject {
                        }
                }
 
+               if ( $flags & self::GETOPTIONS_EXCLUDE_DEFAULTS ) {
+                       $options = array_diff_assoc( $options, self::getDefaultOptions() );
+               }
+
                return $options;
        }
 
@@ -2821,7 +2836,7 @@ class User implements IDBAccessObject {
                        }
                }
 
-               wfRunHooks( 'UserResetAllOptions', array( $this, &$newOptions, $this->mOptions, $resetKinds ) );
+               Hooks::run( 'UserResetAllOptions', array( $this, &$newOptions, $this->mOptions, $resetKinds ) );
 
                $this->mOptions = $newOptions;
                $this->mOptionsLoaded = true;
@@ -2857,7 +2872,7 @@ class User implements IDBAccessObject {
                        return false;
                } else {
                        $https = $this->getBoolOption( 'prefershttps' );
-                       wfRunHooks( 'UserRequiresHTTPS', array( $this, &$https ) );
+                       Hooks::run( 'UserRequiresHTTPS', array( $this, &$https ) );
                        if ( $https ) {
                                $https = wfCanIPUseHTTPS( $this->getRequest()->getIP() );
                        }
@@ -2888,7 +2903,7 @@ class User implements IDBAccessObject {
        public function getRights() {
                if ( is_null( $this->mRights ) ) {
                        $this->mRights = self::getGroupPermissions( $this->getEffectiveGroups() );
-                       wfRunHooks( 'UserGetRights', array( $this, &$this->mRights ) );
+                       Hooks::run( 'UserGetRights', array( $this, &$this->mRights ) );
                        // Force reindexation of rights when a hook has unset one of them
                        $this->mRights = array_values( array_unique( $this->mRights ) );
                }
@@ -2921,7 +2936,7 @@ class User implements IDBAccessObject {
                                $this->getAutomaticGroups( $recache ) // implicit groups
                        ) );
                        // Hook for additional groups
-                       wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
+                       Hooks::run( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
                        // Force reindexation of groups when a hook has unset one of them
                        $this->mEffectiveGroups = array_values( array_unique( $this->mEffectiveGroups ) );
                        wfProfileOut( __METHOD__ );
@@ -3018,7 +3033,7 @@ class User implements IDBAccessObject {
         * @param string $group Name of the group to add
         */
        public function addGroup( $group ) {
-               if ( wfRunHooks( 'UserAddGroup', array( $this, &$group ) ) ) {
+               if ( Hooks::run( 'UserAddGroup', array( $this, &$group ) ) ) {
                        $dbw = wfGetDB( DB_MASTER );
                        if ( $this->getId() ) {
                                $dbw->insert( 'user_groups',
@@ -3051,7 +3066,7 @@ class User implements IDBAccessObject {
         */
        public function removeGroup( $group ) {
                $this->load();
-               if ( wfRunHooks( 'UserRemoveGroup', array( $this, &$group ) ) ) {
+               if ( Hooks::run( 'UserRemoveGroup', array( $this, &$group ) ) ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->delete( 'user_groups',
                                array(
@@ -3275,7 +3290,7 @@ class User implements IDBAccessObject {
 
                // If we're working on user's talk page, we should update the talk page message indicator
                if ( $title->getNamespace() == NS_USER_TALK && $title->getText() == $this->getName() ) {
-                       if ( !wfRunHooks( 'UserClearNewTalkNotification', array( &$this, $oldid ) ) ) {
+                       if ( !Hooks::run( 'UserClearNewTalkNotification', array( &$this, $oldid ) ) ) {
                                return;
                        }
 
@@ -3420,7 +3435,7 @@ class User implements IDBAccessObject {
                        $cookies['Token'] = false;
                }
 
-               wfRunHooks( 'UserSetCookies', array( $this, &$session, &$cookies ) );
+               Hooks::run( 'UserSetCookies', array( $this, &$session, &$cookies ) );
 
                foreach ( $session as $name => $value ) {
                        $request->setSessionData( $name, $value );
@@ -3455,7 +3470,7 @@ class User implements IDBAccessObject {
         * Log this user out.
         */
        public function logout() {
-               if ( wfRunHooks( 'UserLogout', array( &$this ) ) ) {
+               if ( Hooks::run( 'UserLogout', array( &$this ) ) ) {
                        $this->doLogout();
                }
        }
@@ -3520,7 +3535,7 @@ class User implements IDBAccessObject {
 
                $this->saveOptions();
 
-               wfRunHooks( 'UserSaveSettings', array( $this ) );
+               Hooks::run( 'UserSaveSettings', array( $this ) );
                $this->clearSharedCache();
                $this->getUserPage()->invalidateCache();
        }
@@ -3817,7 +3832,6 @@ class User implements IDBAccessObject {
                        return false;
                }
 
-               $passwordFactory = self::getPasswordFactory();
                if ( !$this->mPassword->equals( $password ) ) {
                        if ( $wgLegacyEncoding ) {
                                // Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
@@ -3831,6 +3845,7 @@ class User implements IDBAccessObject {
                        }
                }
 
+               $passwordFactory = self::getPasswordFactory();
                if ( $passwordFactory->needsUpdate( $this->mPassword ) ) {
                        $this->mPassword = $passwordFactory->newFromPlaintext( $password );
                        $this->saveSettings();
@@ -3877,22 +3892,15 @@ class User implements IDBAccessObject {
        }
 
        /**
-        * Initialize (if necessary) and return a session token value
-        * which can be used in edit forms to show that the user's
-        * login credentials aren't being hijacked with a foreign form
-        * submission.
+        * Internal implementation for self::getEditToken() and
+        * self::matchEditToken().
         *
-        * @since 1.19
-        *
-        * @param string|array $salt Array of Strings Optional function-specific data for hashing
-        * @param WebRequest|null $request WebRequest object to use or null to use $wgRequest
-        * @return string The new edit token
+        * @param string|array $salt
+        * @param WebRequest $request
+        * @param string|int $timestamp
+        * @return string
         */
-       public function getEditToken( $salt = '', $request = null ) {
-               if ( $request == null ) {
-                       $request = $this->getRequest();
-               }
-
+       private function getEditTokenAtTimestamp( $salt, $request, $timestamp ) {
                if ( $this->isAnon() ) {
                        return self::EDIT_TOKEN_SUFFIX;
                } else {
@@ -3904,10 +3912,30 @@ class User implements IDBAccessObject {
                        if ( is_array( $salt ) ) {
                                $salt = implode( '|', $salt );
                        }
-                       return md5( $token . $salt ) . self::EDIT_TOKEN_SUFFIX;
+                       return hash_hmac( 'md5', $timestamp . $salt, $token, false ) .
+                               dechex( $timestamp ) .
+                               self::EDIT_TOKEN_SUFFIX;
                }
        }
 
+       /**
+        * Initialize (if necessary) and return a session token value
+        * which can be used in edit forms to show that the user's
+        * login credentials aren't being hijacked with a foreign form
+        * submission.
+        *
+        * @since 1.19
+        *
+        * @param string|array $salt Array of Strings Optional function-specific data for hashing
+        * @param WebRequest|null $request WebRequest object to use or null to use $wgRequest
+        * @return string The new edit token
+        */
+       public function getEditToken( $salt = '', $request = null ) {
+               return $this->getEditTokenAtTimestamp(
+                       $salt, $request ?: $this->getRequest(), wfTimestamp()
+               );
+       }
+
        /**
         * Generate a looking random token for various uses.
         *
@@ -3928,15 +3956,34 @@ class User implements IDBAccessObject {
         * @param string $val Input value to compare
         * @param string $salt Optional function-specific data for hashing
         * @param WebRequest|null $request Object to use or null to use $wgRequest
+        * @param int $maxage Fail tokens older than this, in seconds
         * @return bool Whether the token matches
         */
-       public function matchEditToken( $val, $salt = '', $request = null ) {
-               $sessionToken = $this->getEditToken( $salt, $request );
+       public function matchEditToken( $val, $salt = '', $request = null, $maxage = null ) {
+               if ( $this->isAnon() ) {
+                       return $val === self::EDIT_TOKEN_SUFFIX;
+               }
+
+               $suffixLen = strlen( self::EDIT_TOKEN_SUFFIX );
+               if ( strlen( $val ) <= 32 + $suffixLen ) {
+                       return false;
+               }
+
+               $timestamp = hexdec( substr( $val, 32, -$suffixLen ) );
+               if ( $maxage !== null && $timestamp < wfTimestamp() - $maxage ) {
+                       // Expired token
+                       return false;
+               }
+
+               $sessionToken = $this->getEditTokenAtTimestamp(
+                       $salt, $request ?: $this->getRequest(), $timestamp
+               );
+
                if ( $val != $sessionToken ) {
                        wfDebug( "User::matchEditToken: broken session data\n" );
                }
 
-               return $val == $sessionToken;
+               return hash_equals( $sessionToken, $val );
        }
 
        /**
@@ -3946,11 +3993,12 @@ class User implements IDBAccessObject {
         * @param string $val Input value to compare
         * @param string $salt Optional function-specific data for hashing
         * @param WebRequest|null $request Object to use or null to use $wgRequest
+        * @param int $maxage Fail tokens older than this, in seconds
         * @return bool Whether the token matches
         */
-       public function matchEditTokenNoSuffix( $val, $salt = '', $request = null ) {
-               $sessionToken = $this->getEditToken( $salt, $request );
-               return substr( $sessionToken, 0, 32 ) == substr( $val, 0, 32 );
+       public function matchEditTokenNoSuffix( $val, $salt = '', $request = null, $maxage = null ) {
+               $val = substr( $val, 0, strspn( $val, '0123456789abcdef' ) ) . self::EDIT_TOKEN_SUFFIX;
+               return $this->matchEditToken( $val, $salt, $request, $maxage );
        }
 
        /**
@@ -4085,7 +4133,7 @@ class User implements IDBAccessObject {
                // and fire the ConfirmEmailComplete hook on redundant confirmations.
                if ( !$this->isEmailConfirmed() ) {
                        $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
-                       wfRunHooks( 'ConfirmEmailComplete', array( $this ) );
+                       Hooks::run( 'ConfirmEmailComplete', array( $this ) );
                }
                return true;
        }
@@ -4103,7 +4151,7 @@ class User implements IDBAccessObject {
                $this->mEmailTokenExpires = null;
                $this->setEmailAuthenticationTimestamp( null );
                $this->mEmail = '';
-               wfRunHooks( 'InvalidateEmailComplete', array( $this ) );
+               Hooks::run( 'InvalidateEmailComplete', array( $this ) );
                return true;
        }
 
@@ -4114,7 +4162,7 @@ class User implements IDBAccessObject {
        public function setEmailAuthenticationTimestamp( $timestamp ) {
                $this->load();
                $this->mEmailAuthenticated = $timestamp;
-               wfRunHooks( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
+               Hooks::run( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
        }
 
        /**
@@ -4128,7 +4176,7 @@ class User implements IDBAccessObject {
                        return false;
                }
                $canSend = $this->isEmailConfirmed();
-               wfRunHooks( 'UserCanSendEmail', array( &$this, &$canSend ) );
+               Hooks::run( 'UserCanSendEmail', array( &$this, &$canSend ) );
                return $canSend;
        }
 
@@ -4155,7 +4203,7 @@ class User implements IDBAccessObject {
                global $wgEmailAuthentication;
                $this->load();
                $confirmed = true;
-               if ( wfRunHooks( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
+               if ( Hooks::run( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
                        if ( $this->isAnon() ) {
                                return false;
                        }
@@ -4313,7 +4361,7 @@ class User implements IDBAccessObject {
                }
 
                // Allow extensions (e.g. OAuth) to say false
-               if ( !wfRunHooks( 'UserIsEveryoneAllowed', array( $right ) ) ) {
+               if ( !Hooks::run( 'UserIsEveryoneAllowed', array( $right ) ) ) {
                        $cache[$right] = false;
                        return false;
                }
@@ -4371,7 +4419,7 @@ class User implements IDBAccessObject {
                        } else {
                                self::$mAllRights = self::$mCoreRights;
                        }
-                       wfRunHooks( 'UserGetAllRights', array( &self::$mAllRights ) );
+                       Hooks::run( 'UserGetAllRights', array( &self::$mAllRights ) );
                }
                return self::$mAllRights;
        }
@@ -4384,8 +4432,8 @@ class User implements IDBAccessObject {
                global $wgImplicitGroups;
 
                $groups = $wgImplicitGroups;
-               # Deprecated, use $wgImplictGroups instead
-               wfRunHooks( 'UserGetImplicitGroups', array( &$groups ) );
+               # Deprecated, use $wgImplicitGroups instead
+               Hooks::run( 'UserGetImplicitGroups', array( &$groups ), '1.25' );
 
                return $groups;
        }
@@ -4441,7 +4489,7 @@ class User implements IDBAccessObject {
                }
                $title = self::getGroupPage( $group );
                if ( $title ) {
-                       $page = $title->getPrefixedText();
+                       $page = $title->getFullText();
                        return "[[$page|$text]]";
                } else {
                        return $text;
@@ -4478,6 +4526,7 @@ class User implements IDBAccessObject {
 
                // Same thing for remove
                if ( empty( $wgRemoveGroups[$group] ) ) {
+                       // Do nothing
                } elseif ( $wgRemoveGroups[$group] === true ) {
                        $groups['remove'] = self::getAllGroups();
                } elseif ( is_array( $wgRemoveGroups[$group] ) ) {
@@ -4503,6 +4552,7 @@ class User implements IDBAccessObject {
 
                // Now figure out what groups the user can add to him/herself
                if ( empty( $wgGroupsAddToSelf[$group] ) ) {
+                       // Do nothing
                } elseif ( $wgGroupsAddToSelf[$group] === true ) {
                        // No idea WHY this would be used, but it's there
                        $groups['add-self'] = User::getAllGroups();
@@ -4511,6 +4561,7 @@ class User implements IDBAccessObject {
                }
 
                if ( empty( $wgGroupsRemoveFromSelf[$group] ) ) {
+                       // Do nothing
                } elseif ( $wgGroupsRemoveFromSelf[$group] === true ) {
                        $groups['remove-self'] = User::getAllGroups();
                } elseif ( is_array( $wgGroupsRemoveFromSelf[$group] ) ) {
@@ -4816,7 +4867,7 @@ class User implements IDBAccessObject {
 
                $this->mOptionsLoaded = true;
 
-               wfRunHooks( 'UserLoadOptions', array( $this, &$this->mOptions ) );
+               Hooks::run( 'UserLoadOptions', array( $this, &$this->mOptions ) );
        }
 
        /**
@@ -4832,7 +4883,7 @@ class User implements IDBAccessObject {
 
                // Allow hooks to abort, for instance to save to a global profile.
                // Reset options to default state before saving.
-               if ( !wfRunHooks( 'UserSaveOptions', array( $this, &$saveOptions ) ) ) {
+               if ( !Hooks::run( 'UserSaveOptions', array( $this, &$saveOptions ) ) ) {
                        return;
                }