From d5cbc0e43a7c184157fea3191aa33cea68d98aa3 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 1 Sep 2003 13:13:56 +0000 Subject: [PATCH] new Block object, block expiry, optional sysop blocks --- includes/Block.php | 168 ++++++++++++++++++++++++++++++++ includes/DefaultSettings.php | 16 +-- includes/MagicWord.php | 1 + includes/Setup.php | 1 + includes/SpecialBlockip.php | 13 ++- includes/SpecialIpblocklist.php | 74 +++++++------- includes/User.php | 60 +++++------- 7 files changed, 251 insertions(+), 82 deletions(-) create mode 100644 includes/Block.php diff --git a/includes/Block.php b/includes/Block.php new file mode 100644 index 0000000000..faffc641ac --- /dev/null +++ b/includes/Block.php @@ -0,0 +1,168 @@ +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" ); + } +} +?> diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 031c7e5b78..8df71bf4a9 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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. diff --git a/includes/MagicWord.php b/includes/MagicWord.php index fd1e878b8e..714e8850a1 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -109,3 +109,4 @@ class MagicWord { return ""; } +?> diff --git a/includes/Setup.php b/includes/Setup.php index c0cc05e9e5..fdc3b8711f 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -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; diff --git a/includes/SpecialBlockip.php b/includes/SpecialBlockip.php index 87acf24660..744ceee13c 100644 --- a/includes/SpecialBlockip.php +++ b/includes/SpecialBlockip.php @@ -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" ) ); diff --git a/includes/SpecialIpblocklist.php b/includes/SpecialIpblocklist.php index 70e4b0821c..f98098d401 100644 --- a/includes/SpecialIpblocklist.php +++ b/includes/SpecialIpblocklist.php @@ -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( "\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( "
  • {$line}" ); + $clink = "specialPage( + "Contributions" ), "target={$addr}" ) . "\">" . + wfMsg( "contribslink" ) . ""; + $wgOut->addHTML( " ({$clink})" ); + + if ( $wgUser->isSysop() ) { + $ublink = "specialPage( + "Ipblocklist" ), "action=unblock&ip={$addr}" ) . "\">" . + wfMsg( "unblocklink" ) . ""; + $wgOut->addHTML( " ({$ublink})" ); + } + if ( "" != $block->mReason ) { + $wgOut->addHTML( " (" . wfEscapeHTML( $block->mReason ) . + ")" ); + } + $wgOut->addHTML( "
  • \n" ); +} + + ?> diff --git a/includes/User.php b/includes/User.php index e1dc80eb37..0b527370b8 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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(); + } } -- 2.20.1