/** * Given a target and the target's type, get an existing Block object if possible. * @param string|User|int $specificTarget A block target, which may be one of several types: * * A user to block, in which case $target will be a User * * An IP to block, in which case $target will be a User generated by using * User::newFromName( $ip, false ) to turn off name validation * * An IP range, in which case $target will be a String "123.123.123.123/18" etc * * The ID of an existing block, in the format "#12345" (since pure numbers are valid * usernames * Calling this with a user, IP address or range will not select autoblocks, and will * only select a block where the targets match exactly (so looking for blocks on * 1.2.3.4 will not select 1.2.0.0/16 or even 1.2.3.4/32) * @param string|User|int $vagueTarget As above, but we will search for *any* block which * affects that target (so for an IP address, get ranges containing that IP; and also * get any relevant autoblocks). Leave empty or blank to skip IP-based lookups. * @param bool $fromMaster Whether to use the DB_MASTER database * @return Block|null (null if no relevant block could be found). The target and type * of the returned Block will refer to the actual block which was found, which might * not be the same as the target you gave if you used $vagueTarget! */ public static function newFromTarget($specificTarget, $vagueTarget = null, $fromMaster = false) { list($target, $type) = self::parseTarget($specificTarget); if ($type == Block::TYPE_ID || $type == Block::TYPE_AUTO) { return Block::newFromID($target); } elseif ($target === null && $vagueTarget == '') { # We're not going to find anything useful here # Be aware that the == '' check is explicit, since empty values will be # passed by some callers (bug 29116) return null; } elseif (in_array($type, array(Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null))) { $block = new Block(); $block->fromMaster($fromMaster); if ($type !== null) { $block->setTarget($target); } if ($block->newLoad($vagueTarget)) { return $block; } } return null; }
/** * @covers Block::insert */ public function testCrappyCrossWikiBlocks() { // Delete the last round's block if it's still there $oldBlock = Block::newFromTarget('UserOnForeignWiki'); if ($oldBlock) { // An old block will prevent our new one from saving. $oldBlock->delete(); } // Foreign perspective (blockee not on current wiki)... $block = new Block('UserOnForeignWiki', 14146, 0, 'crosswiki block...', wfTimestampNow(), false, $this->db->getInfinity(), false, true, true, true, true, false, 'MetaWikiUser'); $res = $block->insert($this->db); $this->assertTrue((bool) $res['id'], 'Block succeeded'); // Local perspective (blockee on current wiki)... $user = User::newFromName('UserOnForeignWiki'); $user->addToDatabase(); // Set user ID to match the test value $this->db->update('user', array('user_id' => 14146), array('user_id' => $user->getId())); $user = null; // clear $block = Block::newFromID($res['id']); $this->assertEquals('UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name'); $this->assertEquals('14146', $block->getTarget()->getId(), 'Correct blockee id'); $this->assertEquals('MetaWikiUser', $block->getBlocker(), 'Correct blocker name'); $this->assertEquals('MetaWikiUser', $block->getByName(), 'Correct blocker name'); $this->assertEquals(0, $block->getBy(), 'Correct blocker id'); }
/** * @covers Block::insert */ public function testCrappyCrossWikiBlocks() { // Delete the last round's block if it's still there $oldBlock = Block::newFromTarget('UserOnForeignWiki'); if ($oldBlock) { // An old block will prevent our new one from saving. $oldBlock->delete(); } // Local perspective (blockee on current wiki)... $user = User::newFromName('UserOnForeignWiki'); $user->addToDatabase(); $userId = $user->getId(); $this->assertNotEquals(0, $userId, 'sanity'); // Foreign perspective (blockee not on current wiki)... $blockOptions = array('address' => 'UserOnForeignWiki', 'user' => $user->getId(), 'reason' => 'crosswiki block...', 'timestamp' => wfTimestampNow(), 'expiry' => $this->db->getInfinity(), 'createAccount' => true, 'enableAutoblock' => true, 'hideName' => true, 'blockEmail' => true, 'byText' => 'MetaWikiUser'); $block = new Block($blockOptions); $res = $block->insert($this->db); $this->assertTrue((bool) $res['id'], 'Block succeeded'); $user = null; // clear $block = Block::newFromID($res['id']); $this->assertEquals('UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name'); $this->assertEquals($userId, $block->getTarget()->getId(), 'Correct blockee id'); $this->assertEquals('MetaWikiUser', $block->getBlocker(), 'Correct blocker name'); $this->assertEquals('MetaWikiUser', $block->getByName(), 'Correct blocker name'); $this->assertEquals(0, $block->getBy(), 'Correct blocker id'); }
function doSubmit() { global $wgOut; if ($this->id) { $block = Block::newFromID($this->id); if ($block) { $this->ip = $block->getRedactedName(); } } else { $block = new Block(); $this->ip = trim($this->ip); if (substr($this->ip, 0, 1) == "#") { $id = substr($this->ip, 1); $block = Block::newFromID($id); } else { $block = Block::newFromDB($this->ip); if (!$block) { $block = null; } } } $success = false; if ($block) { # Delete block if ($block->delete()) { # Make log entry $log = new LogPage('block'); $log->addEntry('unblock', Title::makeTitle(NS_USER, $this->ip), $this->reason); $success = true; } } if ($success) { # Report to the user $titleObj = Title::makeTitle(NS_SPECIAL, "Ipblocklist"); $success = $titleObj->getFullURL("action=success&successip=" . urlencode($this->ip)); $wgOut->redirect($success); } else { if (!$this->ip && $this->id) { $this->ip = '#' . $this->id; } $this->showForm(wfMsg('ipb_cant_unblock', htmlspecialchars($this->id))); } }
function testInitializerFunctionsReturnCorrectBlock() { // $this->dumpBlocks(); $this->assertTrue($this->block->equals(Block::newFromTarget('UTBlockee')), "newFromTarget() returns the same block as the one that was made"); $this->assertTrue($this->block->equals(Block::newFromID($this->blockId)), "newFromID() returns the same block as the one that was made"); }
/** * Backend code for unblocking. doSubmit() wraps around this. * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which * case it contains the range $ip is part of. * @return array array(message key, parameters) on failure, empty array on success */ static function doUnblock(&$id, &$ip, &$reason, &$range = null) { if ($id) { $block = Block::newFromID($id); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } $ip = $block->getRedactedName(); } else { $block = new Block(); $ip = trim($ip); if (substr($ip, 0, 1) == "#") { $id = substr($ip, 1); $block = Block::newFromID($id); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } $ip = $block->getRedactedName(); } else { $block = Block::newFromDB($ip); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } if ($block->mRangeStart != $block->mRangeEnd && !strstr($ip, "/")) { /* If the specified IP is a single address, and the block is * a range block, don't unblock the range. */ $range = $block->mAddress; return array('ipb_blocked_as_range', $ip, $range); } } } // Yes, this is really necessary $id = $block->mId; # Delete block if (!$block->delete()) { return array('ipb_cant_unblock', htmlspecialchars($id)); } # Make log entry $log = new LogPage('block'); $log->addEntry('unblock', Title::makeTitle(NS_USER, $ip), $reason); return array(); }
/** * Backend code for unblocking. doSubmit() wraps around this. * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which * case it contains the range $ip is part of. * @return array array(message key, parameters) on failure, empty array on success */ static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker = null) { if ($id) { $block = Block::newFromID($id); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } $ip = $block->getRedactedName(); } else { $block = new Block(); $ip = trim($ip); if (substr($ip, 0, 1) == "#") { $id = substr($ip, 1); $block = Block::newFromID($id); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } $ip = $block->getRedactedName(); } else { $block = Block::newFromDB($ip); if (!$block) { return array('ipb_cant_unblock', htmlspecialchars($id)); } if ($block->mRangeStart != $block->mRangeEnd && !strstr($ip, "/")) { /* If the specified IP is a single address, and the block is * a range block, don't unblock the range. */ $range = $block->mAddress; return array('ipb_blocked_as_range', $ip, $range); } } } // Yes, this is really necessary $id = $block->mId; # If the name was hidden and the blocking user cannot hide # names, then don't allow any block removals... if ($blocker && $block->mHideName && !$blocker->isAllowed('hideuser')) { return array('ipb_cant_unblock', htmlspecialchars($id)); } # Delete block if (!$block->delete()) { return array('ipb_cant_unblock', htmlspecialchars($id)); } # Unset _deleted fields as needed if ($block->mHideName) { IPBlockForm::unsuppressUserName($block->mAddress, $block->mUser); } # Make log entry $log = new LogPage('block'); $log->addEntry('unblock', Title::makeTitle(NS_USER, $ip), $reason); return array(); }