Fix for r41108 (as per comment on bug 15642): Also disallow blocking anonymous users
[lhc/web/wiklou.git] / includes / Block.php
index b9459d5..7efdab9 100644 (file)
  *
  * @todo This could be used everywhere, but it isn't.
  */
-class Block
-{
+class Block {
        /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
                                $mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
-                               $mBlockEmail, $mByName, $mAngryAutoblock;
+                               $mBlockEmail, $mByName, $mAngryAutoblock, $mAllowUsertalk;
        /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster;
 
        const EB_KEEP_EXPIRED = 1;
@@ -26,7 +25,7 @@ class Block
 
        function __construct( $address = '', $user = 0, $by = 0, $reason = '',
                $timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
-               $hideName = 0, $blockEmail = 0 )
+               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
        {
                $this->mId = 0;
                # Expand valid IPv6 addresses
@@ -43,6 +42,7 @@ class Block
                $this->mEnableAutoblock = $enableAutoblock;
                $this->mHideName = $hideName;
                $this->mBlockEmail = $blockEmail;
+               $this->mAllowUsertalk = $allowUsertalk;
                $this->mForUpdate = false;
                $this->mFromMaster = false;
                $this->mByName = false;
@@ -52,16 +52,16 @@ class Block
 
        /**
         * Load a block from the database, using either the IP address or
-        * userid.
+        * user ID. Tries the user ID first, and if that doesn't work, tries
+        * the address.
         * 
         * @return Block Object
         * @param $address string IP address of user/anon
         * @param $user int User id of user
         * @param $killExpired bool Delete expired blocks on load
         */
-       static function newFromDB( $address, $user = 0, $killExpired = true )
-       {
-               $block = new Block();
+       static function newFromDB( $address, $user = 0, $killExpired = true ) {
+               $block = new Block;
                $block->load( $address, $user, $killExpired );
                if ( $block->isValid() ) {
                        return $block;
@@ -72,11 +72,11 @@ class Block
 
        /**
         * Load a blocked user from their block id.
+        *
         * @return Block object
         * @param $id int Block id to search for
         */
-       static function newFromID( $id )
-       {
+       static function newFromID( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
                        array( 'ipb_id' => $id ), __METHOD__ ) );
@@ -92,17 +92,19 @@ class Block
         * Clear all member variables in the current object. Does not clear
         * the block from the DB.
         */
-       function clear()
-       {
+       function clear() {
                $this->mAddress = $this->mReason = $this->mTimestamp = '';
                $this->mId = $this->mAnonOnly = $this->mCreateAccount =
                        $this->mEnableAutoblock = $this->mAuto = $this->mUser =
-                       $this->mBy = $this->mHideName = $this->mBlockEmail = 0;
+                       $this->mBy = $this->mHideName = $this->mBlockEmail = $this->mAllowUsertalk = 0;
                $this->mByName = false;
        }
 
        /**
-        * Get the DB object and set the reference parameter to the query options
+        * Get the DB object and set the reference parameter to the select options.
+        * The options array will contain FOR UPDATE if appropriate.
+        *
+        * @param array $options
         * @return Database
         */
        function &getDBOptions( &$options ) {
@@ -122,15 +124,15 @@ class Block
        }
 
        /**
-        * Get a ban from the DB, with either the given address or the given username
+        * Get a block from the DB, with either the given address or the given username
         *
         * @param $address string The IP address of the user, or blank to skip IP blocks
         * @param $user int The user ID, or zero for anonymous users
         * @param $killExpired bool Whether to delete expired rows while loading
+        * @return bool The user is blocked from editing
         *
         */
-       function load( $address = '', $user = 0, $killExpired = true )
-       {
+       function load( $address = '', $user = 0, $killExpired = true ) {
                wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
 
                $options = array();
@@ -161,7 +163,10 @@ class Block
                                if ( $user && $this->mAnonOnly ) {
                                        # Block is marked anon-only
                                        # Whitelist this IP address against autoblocks and range blocks
-                                       $this->clear();
+                                       # (but not account creation blocks -- bug 13611)
+                                       if( !$this->mCreateAccount ) {
+                                               $this->clear();
+                                       }
                                        return false;
                                } else {
                                        return true;
@@ -172,7 +177,10 @@ class Block
                # Try range block
                if ( $this->loadRange( $address, $killExpired, $user ) ) {
                        if ( $user && $this->mAnonOnly ) {
-                               $this->clear();
+                               # Respect account creation blocks on logged-in users -- bug 13611
+                               if( !$this->mCreateAccount ) {
+                                       $this->clear();
+                               }
                                return false;
                        } else {
                                return true;
@@ -198,12 +206,12 @@ class Block
 
        /**
         * Fill in member variables from a result wrapper
+        *
         * @return bool
         * @param $res ResultWrapper Row from the ipblocks table
         * @param $killExpired bool Whether to delete expired rows while loading
         */
-       function loadFromResult( ResultWrapper $res, $killExpired = true )
-       {
+       function loadFromResult( ResultWrapper $res, $killExpired = true ) {
                $ret = false;
                if ( 0 != $res->numRows() ) {
                        # Get first block
@@ -237,13 +245,13 @@ class Block
        /**
         * Search the database for any range blocks matching the given address, and
         * load the row if one is found.
+        *
         * @return bool
         * @param $address string IP address range
         * @param $killExpired bool Whether to delete expired rows while loading
         * @param $userid int If not 0, then sets ipb_anon_only
         */
-       function loadRange( $address, $killExpired = true, $user = 0 )
-       {
+       function loadRange( $address, $killExpired = true, $user = 0 ) {
                $iaddr = IP::toHex( $address );
                if ( $iaddr === false ) {
                        # Invalid address
@@ -282,10 +290,10 @@ class Block
        /**
         * Given a database row from the ipblocks table, initialize
         * member variables
+        *
         * @param $row ResultWrapper A row from the ipblocks table
         */
-       function initFromRow( $row )
-       {
+       function initFromRow( $row ) {
                $this->mAddress = $row->ipb_address;
                $this->mReason = $row->ipb_reason;
                $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
@@ -296,6 +304,7 @@ class Block
                $this->mCreateAccount = $row->ipb_create_account;
                $this->mEnableAutoblock = $row->ipb_enable_autoblock;
                $this->mBlockEmail = $row->ipb_block_email;
+               $this->mAllowUsertalk = $row->ipb_allow_usertalk;
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
                $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
@@ -312,8 +321,7 @@ class Block
         * Once $mAddress has been set, get the range they came from. 
         * Wrapper for IP::parseRange
         */
-       function initialiseRange()
-       {
+       function initialiseRange() {
                $this->mRangeStart = '';
                $this->mRangeEnd = '';
 
@@ -324,13 +332,15 @@ class Block
 
        /**
         * Callback with a Block object for every block
-        * @return integer number of blocks;
+        *
+        * @deprecated Unlimited row count, do your own queries instead
+        *
+        * @return integer number of blocks
         */
-       /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
-       {
+       /*static*/ function enumBlocks( $callback, $tag, $flags = 0 ) {
                global $wgAntiLockFlags;
 
-               $block = new Block();
+               $block = new Block;
                if ( $flags & Block::EB_FOR_UPDATE ) {
                        $db = wfGetDB( DB_MASTER );
                        if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
@@ -380,10 +390,10 @@ class Block
 
        /**
         * Delete the row from the IP blocks table.
+        *
         * @return bool
         */
-       function delete()
-       {
+       function delete() {
                if (wfReadOnly()) {
                        return false;
                }
@@ -397,33 +407,16 @@ class Block
        }
 
        /**
-       * Insert a block into the block table.
-       * @return bool Whether or not the insertion was successful.
-       */
-       function insert()
-       {
+        * Insert a block into the block table. Will fail if there is a conflicting
+        * block (same name and options) already in the database.
+        *
+        * @return bool Whether or not the insertion was successful.
+        */
+       function insert() {
                wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
                $dbw = wfGetDB( DB_MASTER );
 
-               # Unset ipb_anon_only for user blocks, makes no sense
-               if ( $this->mUser ) {
-                       $this->mAnonOnly = 0;
-               }
-
-               # Unset ipb_enable_autoblock for IP blocks, makes no sense
-               if ( !$this->mUser ) {
-                       $this->mEnableAutoblock = 0;
-                       $this->mBlockEmail = 0; //Same goes for email...
-               }
-
-               if( !$this->mByName ) {
-                       if( $this->mBy ) {
-                               $this->mByName = User::whoIs( $this->mBy );
-                       } else {
-                               global $wgUser;
-                               $this->mByName = $wgUser->getName();
-                       }
-               }
+               $this->validateBlockParams();
 
                # Don't collide with expired blocks
                Block::purgeExpired();
@@ -446,7 +439,8 @@ class Block
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => $this->mHideName,
-                               'ipb_block_email' => $this->mBlockEmail
+                               'ipb_block_email' => $this->mBlockEmail,
+                               'ipb_allow_usertalk' => $this->mAllowUsertalk
                        ), 'Block::insert', array( 'IGNORE' )
                );
                $affected = $dbw->affectedRows();
@@ -454,12 +448,73 @@ class Block
                if ($affected)
                        $this->doRetroactiveAutoblock();
 
-               return $affected;
+               return (bool)$affected;
+       }
+
+       /**
+        * Update a block in the DB with new parameters.
+        * The ID field needs to be loaded first.
+        */
+       public function update() {
+               wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
+               $dbw = wfGetDB( DB_MASTER );
+
+               $this->validateBlockParams();
+
+               $dbw->update( 'ipblocks',
+                       array(
+                               'ipb_user' => $this->mUser,
+                               'ipb_by' => $this->mBy,
+                               'ipb_by_text' => $this->mByName,
+                               'ipb_reason' => $this->mReason,
+                               'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
+                               'ipb_auto' => $this->mAuto,
+                               'ipb_anon_only' => $this->mAnonOnly,
+                               'ipb_create_account' => $this->mCreateAccount,
+                               'ipb_enable_autoblock' => $this->mEnableAutoblock,
+                               'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
+                               'ipb_range_start' => $this->mRangeStart,
+                               'ipb_range_end' => $this->mRangeEnd,
+                               'ipb_deleted'   => $this->mHideName,
+                               'ipb_block_email' => $this->mBlockEmail,
+                               'ipb_allow_usertalk' => $this->mAllowUsertalk ),
+                       array( 'ipb_id' => $this->mId ),
+                       'Block::update' );
+
+               return $dbw->affectedRows();
        }
+       
+       /**
+        * Make sure all the proper members are set to sane values
+        * before adding/updating a block
+        */
+       private function validateBlockParams() {
+               # Unset ipb_anon_only for user blocks, makes no sense
+               if ( $this->mUser ) {
+                       $this->mAnonOnly = 0;
+               }
 
+               # Unset ipb_enable_autoblock for IP blocks, makes no sense
+               if ( !$this->mUser ) {
+                       $this->mEnableAutoblock = 0;
+                       $this->mBlockEmail = 0; //Same goes for email...
+               }
+
+               if( !$this->mByName ) {
+                       if( $this->mBy ) {
+                               $this->mByName = User::whoIs( $this->mBy );
+                       } else {
+                               global $wgUser;
+                               $this->mByName = $wgUser->getName();
+                       }
+               }
+       }
+       
+       
        /**
        * Retroactively autoblocks the last IP used by the user (if it is a user)
        * blocked by this Block.
+       *
        * @return bool Whether or not a retroactive autoblock was made.
        */
        function doRetroactiveAutoblock() {
@@ -499,15 +554,16 @@ class Block
        
        /**
         * Checks whether a given IP is on the autoblock whitelist.
+        *
         * @return bool
-        * @param string $autoblockip The IP to check
+        * @param string $ip The IP to check
         */
        function isWhitelistedFromAutoblocks( $ip ) {
                global $wgMemc;
                
                // Try to get the autoblock_whitelist from the cache, as it's faster
                // than getting the msg raw and explode()'ing it.
-               $key = wfMemc( 'ipb', 'autoblock', 'whitelist' );
+               $key = wfMemcKey( 'ipb', 'autoblock', 'whitelist' );
                $lines = $wgMemc->get( $key );
                if ( !$lines ) {
                        $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
@@ -540,31 +596,32 @@ class Block
        }
 
        /**
-       * Autoblocks the given IP, referring to this Block.
-       * @param string $autoblockip The IP to autoblock.
-       * @param bool $justInserted The main block was just inserted
-       * @return bool Whether or not an autoblock was inserted.
-       */
-       function doAutoblock( $autoblockip, $justInserted = false ) {
+        * Autoblocks the given IP, referring to this Block.
+        *
+        * @param string $autoblockIP The IP to autoblock.
+        * @param bool $justInserted The main block was just inserted
+        * @return bool Whether or not an autoblock was inserted.
+        */
+       function doAutoblock( $autoblockIP, $justInserted = false ) {
                # If autoblocks are disabled, go away.
                if ( !$this->mEnableAutoblock ) {
                        return;
                }
 
                # Check for presence on the autoblock whitelist
-               if (Block::isWhitelistedFromAutoblocks($autoblockip)) {
+               if (Block::isWhitelistedFromAutoblocks($autoblockIP)) {
                        return;
                }
                
                ## Allow hooks to cancel the autoblock.
-               if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) {
+               if (!wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) )) {
                        wfDebug( "Autoblock aborted by hook." );
                        return false;
                }
 
                # It's okay to autoblock. Go ahead and create/insert the block.
 
-               $ipblock = Block::newFromDB( $autoblockip );
+               $ipblock = Block::newFromDB( $autoblockIP );
                if ( $ipblock ) {
                        # If the user is already blocked. Then check if the autoblock would
                        # exceed the user block. If it would exceed, then do nothing, else
@@ -583,8 +640,8 @@ class Block
                }
 
                # Make a new block object with the desired properties
-               wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockip . "\n" );
-               $ipblock->mAddress = $autoblockip;
+               wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockIP . "\n" );
+               $ipblock->mAddress = $autoblockIP;
                $ipblock->mUser = 0;
                $ipblock->mBy = $this->mBy;
                $ipblock->mByName = $this->mByName;
@@ -610,8 +667,7 @@ class Block
         * Check if a block has expired. Delete it if it is.
         * @return bool
         */
-       function deleteIfExpired()
-       {
+       function deleteIfExpired() {
                $fname = 'Block::deleteIfExpired';
                wfProfileIn( $fname );
                if ( $this->isExpired() ) {
@@ -630,8 +686,7 @@ class Block
         * Has the block expired?
         * @return bool
         */
-       function isExpired()
-       {
+       function isExpired() {
                wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
                if ( !$this->mExpiry ) {
                        return false;
@@ -644,16 +699,14 @@ class Block
         * Is the block address valid (i.e. not a null string?)
         * @return bool
         */
-       function isValid()
-       {
+       function isValid() {
                return $this->mAddress != '';
        }
 
        /**
         * Update the timestamp on autoblocks. 
         */
-       function updateTimestamp()
-       {
+       function updateTimestamp() {
                if ( $this->mAuto ) {
                        $this->mTimestamp = wfTimestamp();
                        $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
@@ -670,19 +723,9 @@ class Block
                }
        }
 
-       /*
-       function getIntegerAddr()
-       {
-               return $this->mIntegerAddr;
-       }
-
-       function getNetworkBits()
-       {
-               return $this->mNetworkBits;
-       }*/
-
        /**
         * Get the user id of the blocking sysop
+        *
         * @return int
         */
        public function getBy() {
@@ -691,21 +734,31 @@ class Block
 
        /**
         * Get the username of the blocking sysop
+        *
         * @return string
         */
-       function getByName()
-       {
+       function getByName() {
                return $this->mByName;
        }
 
+       /**
+        * Get/set the SELECT ... FOR UPDATE flag
+        */
        function forUpdate( $x = NULL ) {
                return wfSetVar( $this->mForUpdate, $x );
        }
 
+       /**
+        * Get/set a flag determining whether the master is used for reads
+        */
        function fromMaster( $x = NULL ) {
                return wfSetVar( $this->mFromMaster, $x );
        }
 
+       /**
+        * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
+        * @return string
+        */
        function getRedactedName() {
                if ( $this->mAuto ) {
                        return '#' . $this->mId;
@@ -716,6 +769,7 @@ class Block
 
        /**
         * Encode expiry for DB
+        *
         * @return string
         * @param $expiry string Timestamp for expiry, or 
         * @param $db Database object
@@ -730,6 +784,7 @@ class Block
 
        /**
         * Decode expiry which has come from the DB
+        *
         * @return string
         * @param $expiry string Database expiry format
         * @param $timestampType Requested timestamp format
@@ -744,10 +799,10 @@ class Block
 
        /**
         * Get a timestamp of the expiry for autoblocks
+        *
         * @return string
         */
-       static function getAutoblockExpiry( $timestamp )
-       {
+       static function getAutoblockExpiry( $timestamp ) {
                global $wgAutoblockExpiry;
                return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
        }
@@ -796,21 +851,23 @@ class Block
        }
 
        /**
-        * This is a special keyword for timestamps in PostgreSQL, and
-        * works with CHAR(14) as well because "i" sorts after all numbers.
+        * Get a value to insert into expiry field of the database when infinite expiry
+        * is desired. In principle this could be DBMS-dependant, but currently all 
+        * supported DBMS's support the string "infinity", so we just use that.
+        *
         * @return string
         */
        static function infinity() {
+               # This is a special keyword for timestamps in PostgreSQL, and
+               # works with CHAR(14) as well because "i" sorts after all numbers.
                return 'infinity';
        }
        
        /**
         * Convert a DB-encoded expiry into a real string that humans can read.
-        * @param 
+        *
+        * @param $encoded_expiry string Database encoded expiry time
         * @return string
-        * @todo @fixme This is nasty. Forces a msg on the expiry, which isn't what
-        * we want. Probably want to abstract out the number formatting so it can
-        * be used elsewhere (some bug I forgot and can't find at the moment)
         */
        static function formatExpiry( $encoded_expiry ) {