new Block object, block expiry, optional sysop blocks
authorTim Starling <tstarling@users.mediawiki.org>
Mon, 1 Sep 2003 13:13:56 +0000 (13:13 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Mon, 1 Sep 2003 13:13:56 +0000 (13:13 +0000)
includes/Block.php [new file with mode: 0644]
includes/DefaultSettings.php
includes/MagicWord.php
includes/Setup.php
includes/SpecialBlockip.php
includes/SpecialIpblocklist.php
includes/User.php

diff --git a/includes/Block.php b/includes/Block.php
new file mode 100644 (file)
index 0000000..faffc64
--- /dev/null
@@ -0,0 +1,168 @@
+<?
+# Blocks and bans object
+
+#TODO: This could be used everywhere, but it isn't.
+
+# All the functions in this class assume the object is either explicitly 
+# loaded or filled. It is not load-on-demand.
+
+# To use delete(), you only need to fill $mAddress
+
+class Block
+{
+       var $mAddress, $mUser, $mBy, $mReason, $mTimestamp;
+       
+       function Block( $address = "", $user = "", $by = 0, $reason = "", $timestamp = "" ) 
+       {
+               $this->mAddress = $address;
+               $this->mUser = $user;
+               $this->mBy = $by;
+               $this->mReason = $reason;
+               $this->mTimestamp = $timestamp;
+       }
+
+       /*static*/ function newFromDB( $address, $user = 0, $killExpired = true ) 
+       {
+               $ban = new Block();
+               $ban->load( $address, $user, $killExpired );
+               return $ban;
+       }
+               
+       function clear()
+       {
+               $mAddress = $mReason = $mTimestamp = "";
+               $mUser = $mBy = 0;
+       }
+
+       # Get a ban from the DB, with either the given address or the given username
+       function load( $address, $user = 0, $killExpired = true ) 
+       {
+               $fname = "Block::load";
+               $ret = false;
+               $killed = false;
+               
+               if ( 0 == $user ) {
+                       $sql = "SELECT * FROM ipblocks WHERE ipb_address='$address'";
+               } else {
+                       $sql = "SELECT * FROM ipblocks WHERE (ipb_address='$address' OR ipb_user={$user})";
+               }
+               
+
+               $res = wfQuery( $sql, $fname );
+               if ( 0 == wfNumRows( $res ) ) {
+                       # User is not blocked
+                       $this->clear();
+               } else {
+                       # Get first block
+                       $row = wfFetchObject( $res );
+                       $this->initFromRow( $row );
+
+                       if ( $killExpired ) {
+
+                               # If requested, delete expired rows
+                               do {
+                                       $killed = $this->deleteIfExpired();
+                                       $row = wfFetchObject( $res );
+                               } while ( $killed && $row );
+                               
+                               # If there were any left after the killing finished, return true
+                               if ( $row == false ) {
+                                       $ret = false;
+                                       $this->clear();
+                               } else {
+                                       $ret = true;
+                               }
+                       } else {
+                               $ret = true;
+                       }
+               }
+               wfFreeResult( $res );
+               return $ret;
+       }
+       
+       function initFromRow( $row ) 
+       {
+               $this->mAddress = $row->ipb_address;
+               $this->mReason = $row->ipb_reason;
+               $this->mTimestamp = $row->ipb_timestamp;
+               $this->mUser = $row->ipb_user;
+               $this->mBy = $row->ipb_by;
+       }       
+
+       # Callback with a Block object for every block
+       /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
+       {
+               $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp";
+               $res = wfQuery( $sql, "Block::enumBans" );
+               $block = new Block();
+
+               while ( $row = wfFetchObject( $res ) ) {
+                       $block->initFromRow( $row );
+                       if ( $killExpired ) {
+                               if ( !$block->deleteIfExpired() ) {
+                                       $callback( $block, $tag );
+                               }
+                       } else {
+                               $callback( $block, $tag );
+                       }
+               }
+               wfFreeResult( $res );
+       }
+
+       function delete()
+       {
+               wfQuery( "DELETE FROM ipblocks WHERE ipb_address='{$this->mAddress}'", 
+                       "Block::delete" );
+       }
+
+       function insert()
+       {
+               $sql = "INSERT INTO ipblocks (ipb_address, ipb_user, ipb_by, " .
+                 "ipb_reason, ipb_timestamp ) VALUES ('{$this->mAddress}', {$this->mUser}, " .
+                 "{$this->mBy}, '" . wfStrencode( $this->mReason ) . "','{$this->mTimestamp}')";
+               wfQuery( $sql, "Block::insert" );
+       }
+
+       function deleteIfExpired()
+       {
+               if ( $this->isExpired() ) {
+                       $this->delete();
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       function isExpired()
+       {
+               global $wgIPBlockExpiration, $wgUserBlockExpiration;
+               
+               $period = $this->mUser ? $wgUserBlockExpiration : $wgIPBlockExpiration;
+               
+               # Period==0 means no expiry
+               if ( !$period ) { 
+                       return false;
+               }
+               $expiry = wfTimestamp2Unix( $this->mTimestamp ) + $period;
+               $now = wfTimestamp2Unix( wfTimestampNow() );
+               if ( $now > $expiry ) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       function isValid() 
+       {
+               return $this->mAddress != "";
+       }
+
+       
+       function updateTimestamp()
+       {
+               $sql = "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . "' WHERE ipb_address='{$this->mAddress}'";
+               wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . 
+                       "' WHERE ipb_address='{$this->mAddress}'", "Block::updateTimestamp" );
+       }
+}
+?>
index 031c7e5..8df71bf 100644 (file)
@@ -64,14 +64,16 @@ $wgUseDynamicDates  = true; # Allows the user to pick their preferred date format
 
 # Miscellaneous configuration settings
 #
-$wgReadOnlyFile                = "{$wgUploadDirectory}/lock_yBgMBwiR";
-$wgDebugLogFile     = "{$wgUploadDirectory}/log_dlJbnMZb";
-$wgDebugComments       = false;
-$wgReadOnly                    = false;
-$wgSqlLogFile          = "{$wgUploadDirectory}/sqllog_mFhyRe6";
-$wgLogQueries          = false;
+$wgReadOnlyFile         = "{$wgUploadDirectory}/lock_yBgMBwiR";
+$wgDebugLogFile         = "{$wgUploadDirectory}/log_dlJbnMZb";
+$wgDebugComments        = false;
+$wgReadOnly             = false;
+$wgSqlLogFile           = "{$wgUploadDirectory}/sqllog_mFhyRe6";
+$wgLogQueries           = false;
 $wgUseBetterLinksUpdate = true;
-
+$wgSysopUserBans        = true; # Allow sysops to ban logged-in users
+$wgIPBlockExpiration    = 86400; # IP blocks expire after this many seconds, 0=infinite
+$wgUserBlockExpiration  = 0; # As above, but for logged-in users
 
 # The following three config variables are used to define
 # the rights of users in your system. 
index fd1e878..714e885 100644 (file)
@@ -109,3 +109,4 @@ class MagicWord {
        return "";
 }
 
+?>
index c0cc05e..fdc3b87 100644 (file)
@@ -18,6 +18,7 @@ include_once( "$IP/Title.php" );
 include_once( "$IP/Article.php" );
 include_once( "$IP/MagicWord.php" );
 include_once( "$IP/MemCachedClient.inc.php" );
+include_once( "$IP/Block.php" );
 
 global $wgUser, $wgLang, $wgOut, $wgTitle;
 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
index 87acf24..744ceee 100644 (file)
@@ -59,18 +59,23 @@ class IPBlockForm {
        function doSubmit()
        {
                global $wgOut, $wgUser, $wgLang;
-               global $ip, $wpBlockAddress, $wpBlockReason;
+               global $ip, $wpBlockAddress, $wpBlockReason, $wgSysopUserBlocks;
                $fname = "IPBlockForm::doSubmit";
                
                $userId = 0;
                if ( ! preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/",
                  $wpBlockAddress ) ) 
                {
-                       $userId = User::idFromName( $wpBlockAddress );
-                       if ( $userId == 0 ) {
+                       if ( $wgSysopUserBlocks ) {     
+                               $userId = User::idFromName( $wpBlockAddress );
+                               if ( $userId == 0 ) {
+                                       $this->showForm( wfMsg( "badipaddress" ) );
+                                       return;
+                               }
+                       } else {
                                $this->showForm( wfMsg( "badipaddress" ) );
                                return;
-                       }
+                       }               
                }
                if ( "" == $wpBlockReason ) {
                        $this->showForm( wfMsg( "noblockreason" ) );
index 70e4b08..f98098d 100644 (file)
@@ -74,50 +74,50 @@ class IPUnblockForm {
 
        function showList( $msg )
        {
-               global $wgOut, $wgUser, $wgLang;
-               global $ip;
-
+               global $wgOut;
+               
                $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
                if ( "" != $msg ) {
                        $wgOut->setSubtitle( $msg );
                }
-               $sql = "SELECT ipb_timestamp,ipb_address,ipb_user,ipb_by,ipb_reason " .
-                 "FROM ipblocks ORDER BY ipb_timestamp";
-               $res = wfQuery( $sql, "IPUnblockForm::showList" );
-
                $wgOut->addHTML( "<ul>" );
-               $sk = $wgUser->getSkin();
-               while ( $row = wfFetchObject( $res ) ) {
-                       $addr = $row->ipb_address;
-                       $name = User::whoIs( $row->ipb_by );
-                       $ulink = $sk->makeKnownLink( $wgLang->getNsText( Namespace::getUser() ). ":{$name}", $name );
-                       $d = $wgLang->timeanddate( $row->ipb_timestamp, true );
-
-                       $line = str_replace( "$1", $d, wfMsg( "blocklistline" ) );
-                       $line = str_replace( "$2", $ulink, $line );
-                       $line = str_replace( "$3", $row->ipb_address, $line );
-
-                       $wgOut->addHTML( "<li>{$line}" );
-                       $clink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
-                         "Contributions" ), "target={$addr}" ) . "\">" .
-                         wfMsg( "contribslink" ) . "</a>";
-                       $wgOut->addHTML( " ({$clink})" );
-
-                       if ( $wgUser->isSysop() ) {
-                               $ublink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
-                                 "Ipblocklist" ), "action=unblock&ip={$addr}" ) . "\">" .
-                                 wfMsg( "unblocklink" ) . "</a>";
-                               $wgOut->addHTML( " ({$ublink})" );
-                       }
-                       if ( "" != $row->ipb_reason ) {
-                               $wgOut->addHTML( " <em>(" . wfEscapeHTML( $row->ipb_reason ) .
-                                 ")</em>" );
-                       }
-                       $wgOut->addHTML( "</li>\n" );
-               }
-               wfFreeResult( $res );
+               Block::enumBlocks( "wfAddRow", 0 );
                $wgOut->addHTML( "</ul>\n" );
        }
 }
 
+# Callback function
+function wfAddRow( $block, $tag ) {
+       global $wgOut, $wgUser, $wgLang, $ip;
+
+       $sk = $wgUser->getSkin();
+       $addr = $block->mAddress;
+       $name = User::whoIs( $block->mBy );
+       $ulink = $sk->makeKnownLink( $wgLang->getNsText( Namespace::getUser() ). ":{$name}", $name );
+       $d = $wgLang->timeanddate( $block->mTimestamp, true );
+
+       $line = str_replace( "$1", $d, wfMsg( "blocklistline" ) );
+       $line = str_replace( "$2", $ulink, $line );
+       $line = str_replace( "$3", $block->mAddress, $line );
+
+       $wgOut->addHTML( "<li>{$line}" );
+       $clink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
+         "Contributions" ), "target={$addr}" ) . "\">" .
+         wfMsg( "contribslink" ) . "</a>";
+       $wgOut->addHTML( " ({$clink})" );
+
+       if ( $wgUser->isSysop() ) {
+               $ublink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
+                 "Ipblocklist" ), "action=unblock&ip={$addr}" ) . "\">" .
+                 wfMsg( "unblocklink" ) . "</a>";
+               $wgOut->addHTML( " ({$ublink})" );
+       }
+       if ( "" != $block->mReason ) {
+               $wgOut->addHTML( " <em>(" . wfEscapeHTML( $block->mReason ) .
+                 ")</em>" );
+       }
+       $wgOut->addHTML( "</li>\n" );
+}
+
+
 ?>
index e1dc80e..0b52737 100644 (file)
@@ -94,22 +94,14 @@ class User {
        {
                if ( -1 != $this->mBlockedby ) { return; }
 
-               $remaddr = getenv( "REMOTE_ADDR" );
-               if ( 0 == $this->mId ) {
-                       $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
-                         "ipb_address='$remaddr'";
-               } else {
-                       $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
-                         "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
-               }
-               $res = wfQuery( $sql, "User::getBlockedStatus" );
-               if ( 0 == wfNumRows( $res ) ) {
+               $ban = new Ban();
+               if ( $ban->load( getenv( "REMOTE_ADDR" ), $this->mId ) ) {
                        $this->mBlockedby = 0;
                        return;
                }
-               $s = wfFetchObject( $res );
-               $this->mBlockedby = $s->ipb_by;
-               $this->mBlockreason = $s->ipb_reason;
+               
+               $this->mBlockedby = $ban->by;
+               $this->mBlockreason = $ban->reason;
        }
 
        function isBlocked()
@@ -528,8 +520,7 @@ class User {
                  "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
                  "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
                  "user_options= '" . $this->encodeOptions() . "', " .
-                 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " 
-.
+                 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
                  "user_touched= '" . wfStrencode( $this->mTouched ) .
                  "' WHERE user_id={$this->mId}";
                wfQuery( $sql, "User::saveSettings" );
@@ -578,36 +569,37 @@ class User {
                # that they successfully log on from.
                $fname = "User::spreadBlock";
                
-               if ( $this->mId == 0 || !$this->isBlocked()) {
+               wfDebug( "User:spreadBlock()\n" );
+               if ( $this->mId == 0 ) {
                        return;
                }
                
-               $sql = "SELECT * FROM ipblocks WHERE ipb_user={$this->mId}";
-               $res = wfQuery( $sql, $fname );
-               if ( wfNumRows( $res ) == 0 ) {
+               $userblock = Block::newFromDB( "", $this->mId );
+               if ( !$userblock->isValid() ) {
                        return;
                }
                
                # Check if this IP address is already blocked
                $addr = getenv( "REMOTE_ADDR" );
-               $sql = "SELECT * FROM ipblocks WHERE ipb_address='{$addr}'";
-               $res2 = wfQuery( $sql, $fname );
-               if ( wfNumRows( $res2 ) != 0 ) {
+               $ipblock = Block::newFromDB( $addr );
+               if ( $ipblock->isValid() ) {
+                       # Just update the timestamp
+                       $ipblock->updateTimestamp();
                        return;
                }
                
-               $row = wfFetchObject( $res );
-               $reason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
-               $reason = str_replace( "$2", $row->ipb_reason, $reason );
-               
-               $addr = getenv( "REMOTE_ADDR" );
-               $sql = "INSERT INTO ipblocks(ipb_address, ipb_user, ipb_by, " .
-                 "ipb_reason, ipb_timestamp) VALUES ('{$addr}', 0, {$row->ipb_by}," . 
-                 "'{$reason}', '" . wfTimestampNow() . "')";
-               wfQuery( $sql, $fname );
-               
-               wfFreeResult( $res );
-               wfFreeResult( $res2 );
+               # Make a new ban object with the desired properties
+               wfDebug( "Autoblocking {$this->mUserName}@{$addr}\n" );
+               $ipblock->mAddress = $addr;
+               $ipblock->mUser = 0;
+               $ipblock->mBy = $userblock->mBy;
+               $ipblock->mReason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
+               $ipblock->mReason = str_replace( "$2", $userblock->mReason, $ipblock->mReason );
+               $ipblock->mTimestamp = wfTimestampNow();
+
+               # Insert it
+               $ipblock->insert();
+       
        }
 }