summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialIpblocklist.php
blob: 4ba1c8113478245f35854dae11e56697921d4ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
/**
 * @file
 * @ingroup SpecialPage
 */

/**
 * @todo document
 */
function wfSpecialIpblocklist() {
	global $wgUser, $wgOut, $wgRequest;

	$ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) ) );
	$id = $wgRequest->getVal( 'id' );
	$reason = $wgRequest->getText( 'wpUnblockReason' );
	$action = $wgRequest->getText( 'action' );
	$successip = $wgRequest->getVal( 'successip' );

	$ipu = new IPUnblockForm( $ip, $id, $reason );

	if( $action == 'unblock' ) {
		# Check permissions
		if( !$wgUser->isAllowed( 'block' ) ) {
			$wgOut->permissionRequired( 'block' );
			return;
		}
		# Check for database lock
		if( wfReadOnly() ) {
			$wgOut->readOnlyPage();
			return;
		}
		# Show unblock form
		$ipu->showForm( '' );
	} elseif( $action == 'submit' && $wgRequest->wasPosted()
		&& $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
		# Check permissions
		if( !$wgUser->isAllowed( 'block' ) ) {
			$wgOut->permissionRequired( 'block' );
			return;
		}
		# Check for database lock
		if( wfReadOnly() ) {
			$wgOut->readOnlyPage();
			return;
		}
		# Remove blocks and redirect user to success page
		$ipu->doSubmit();
	} elseif( $action == 'success' ) {
		# Inform the user of a successful unblock
		# (No need to check permissions or locks here,
		# if something was done, then it's too late!)
		if ( substr( $successip, 0, 1) == '#' ) {
			// A block ID was unblocked
			$ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
		} else {
			// A username/IP was unblocked
			$ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
		}
	} else {
		# Just show the block list
		$ipu->showList( '' );
	}

}

/**
 * implements Special:ipblocklist GUI
 * @ingroup SpecialPage
 */
class IPUnblockForm {
	var $ip, $reason, $id;

	function IPUnblockForm( $ip, $id, $reason ) {
		global $wgRequest;
		$this->ip = strtr( $ip, '_', ' ' );
		$this->id = $id;
		$this->reason = $reason;
		$this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
		$this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
		$this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
	}

	/**
	 * Generates the unblock form
	 * @param $err string: error message
	 * @return $out string: HTML form
	 */
	function showForm( $err ) {
		global $wgOut, $wgUser, $wgSysopUserBans;

		$wgOut->setPagetitle( wfMsg( 'unblockip' ) );
		$wgOut->addWikiMsg( 'unblockiptext' );

		$titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
		$action = $titleObj->getLocalURL( "action=submit" );

		if ( "" != $err ) {
			$wgOut->setSubtitle( wfMsg( "formerror" ) );
			$wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
		}

		$addressPart = false;
		if ( $this->id ) {
			$block = Block::newFromID( $this->id );
			if ( $block ) {
				$encName = htmlspecialchars( $block->getRedactedName() );
				$encId = $this->id;
				$addressPart = $encName . Xml::hidden( 'id', $encId );
				$ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
			}
		}
		if ( !$addressPart ) {
			$addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
			$ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
		}

		$wgOut->addHTML(
			Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
			Xml::openElement( 'fieldset' ) .
			Xml::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
			Xml::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
			"<tr>
				<td class='mw-label'>
					{$ipa}
				</td>
				<td class='mw-input'>
					{$addressPart}
				</td>
			</tr>
			<tr>
				<td class='mw-label'>" .
					Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
				"</td>
				<td class='mw-input'>" .
					Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
				"</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td class='mw-submit'>" .
					Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
				"</td>
			</tr>" .
			Xml::closeElement( 'table' ) .
			Xml::closeElement( 'fieldset' ) .
			Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
			Xml::closeElement( 'form' ) . "\n"
		);

	}

	const UNBLOCK_SUCCESS = 0; // Success
	const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
	const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
	const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
	const UNBLOCK_UNKNOWNERR = 4; // Unknown error

	/**
	 * 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();
	}

	function doSubmit() {
		global $wgOut, $wgUser;
		$retval = self::doUnblock($this->id, $this->ip, $this->reason, $range, $wgUser);
		if(!empty($retval))
		{
			$key = array_shift($retval);
			$this->showForm(wfMsgReal($key, $retval));
			return;
		}
		# Report to the user
		$titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
		$success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
		$wgOut->redirect( $success );
	}

	function showList( $msg ) {
		global $wgOut, $wgUser;

		$wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
		if ( "" != $msg ) {
			$wgOut->setSubtitle( $msg );
		}

		// Purge expired entries on one in every 10 queries
		if ( !mt_rand( 0, 10 ) ) {
			Block::purgeExpired();
		}

		$conds = array();
		$matches = array();
		// Is user allowed to see all the blocks?
		if ( !$wgUser->isAllowed( 'hideuser' ) )
			$conds['ipb_deleted'] = 0;
		if ( $this->ip == '' ) {
			// No extra conditions
		} elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
			$conds['ipb_id'] = substr( $this->ip, 1 );
		// Single IPs
		} elseif ( IP::isIPAddress($this->ip) && strpos($this->ip,'/') === false ) {
			if( $iaddr = IP::toHex($this->ip) ) {
				# Only scan ranges which start in this /16, this improves search speed
				# Blocks should not cross a /16 boundary.
				$range = substr( $iaddr, 0, 4 );
				// Fixme -- encapsulate this sort of query-building.
				$dbr = wfGetDB( DB_SLAVE );
				$encIp = $dbr->addQuotes( IP::sanitizeIP($this->ip) );
				$encRange = $dbr->addQuotes( "$range%" );
				$encAddr = $dbr->addQuotes( $iaddr );
				$conds[] = "(ipb_address = $encIp) OR 
					(ipb_range_start LIKE $encRange AND
					ipb_range_start <= $encAddr
					AND ipb_range_end >= $encAddr)";
			} else {
				$conds['ipb_address'] = IP::sanitizeIP($this->ip);
			}
			$conds['ipb_auto'] = 0;
		// IP range
		} elseif ( IP::isIPAddress($this->ip) ) {
			$conds['ipb_address'] = Block::normaliseRange( $this->ip );
			$conds['ipb_auto'] = 0;
		} else {
			$user = User::newFromName( $this->ip );
			if ( $user && ( $id = $user->getId() ) != 0 ) {
				$conds['ipb_user'] = $id;
			} else {
				// Uh...?
				$conds['ipb_address'] = $this->ip;
				$conds['ipb_auto'] = 0;
			}
		}
		// Apply filters
		if( $this->hideuserblocks ) {
			$conds['ipb_user'] = 0;
		}
		if( $this->hidetempblocks ) {
			$conds['ipb_expiry'] = 'infinity';
		}
		if( $this->hideaddressblocks ) {
			$conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
		}

		$pager = new IPBlocklistPager( $this, $conds );
		if ( $pager->getNumRows() ) {
			$wgOut->addHTML(
				$this->searchForm() .
				$pager->getNavigationBar() .
				Xml::tags( 'ul', null, $pager->getBody() ) .
				$pager->getNavigationBar()
			);
		} elseif ( $this->ip != '') {
			$wgOut->addHTML( $this->searchForm() );
			$wgOut->addWikiMsg( 'ipblocklist-no-results' );
		} else {
			$wgOut->addHTML( $this->searchForm() );
			$wgOut->addWikiMsg( 'ipblocklist-empty' );
		}
	}

	function searchForm() {
		global $wgTitle, $wgScript, $wgRequest, $wgLang;

		$showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
		$nondefaults = array();
		if( $this->hideuserblocks ) {
			$nondefaults['hideuserblocks'] = $this->hideuserblocks;
		}
		if( $this->hidetempblocks ) {
			$nondefaults['hidetempblocks'] = $this->hidetempblocks;
		}
		if( $this->hideaddressblocks ) {
			$nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
		}
		$ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
			array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
		$tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
			array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
		$sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
			array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);

		$links = array();
		$links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
		$links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
		$links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
		$hl = $wgLang->pipeList( $links );

		return
			Xml::tags( 'form', array( 'action' => $wgScript ),
				Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
				Xml::openElement( 'fieldset' ) .
				Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
				Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
				'&nbsp;' .
				Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
				$hl .
				Xml::closeElement( 'fieldset' )
			);
	}

	/**
	 * Makes change an option link which carries all the other options
	 * @param $title see Title
	 * @param $override
	 * @param $options
	 */
	function makeOptionsLink( $title, $override, $options, $active = false ) {
		global $wgUser;
		$sk = $wgUser->getSkin();
		$params = $override + $options;
		$ipblocklist = SpecialPage::getTitleFor( 'IPBlockList' );
		return $sk->link( $ipblocklist, htmlspecialchars( $title ),
			( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
	}

	/**
	 * Callback function to output a block
	 */
	function formatRow( $block ) {
		global $wgUser, $wgLang, $wgBlockAllowsUTEdit;

		wfProfileIn( __METHOD__ );

		static $sk=null, $msg=null;

		if( is_null( $sk ) )
			$sk = $wgUser->getSkin();
		if( is_null( $msg ) ) {
			$msg = array();
			$keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
				'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk' );
			foreach( $keys as $key ) {
				$msg[$key] = wfMsgHtml( $key );
			}
			$msg['blocklistline'] = wfMsg( 'blocklistline' );
		}

		# Prepare links to the blocker's user and talk pages
		$blocker_id = $block->getBy();
		$blocker_name = $block->getByName();
		$blocker = $sk->userLink( $blocker_id, $blocker_name );
		$blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );

		# Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
		if( $block->mAuto ) {
			$target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
		} else {
			$target = $sk->userLink( $block->mUser, $block->mAddress )
				. $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
		}

		$formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );

		$properties = array();
		$properties[] = Block::formatExpiry( $block->mExpiry );
		if ( $block->mAnonOnly ) {
			$properties[] = $msg['anononlyblock'];
		}
		if ( $block->mCreateAccount ) {
			$properties[] = $msg['createaccountblock'];
		}
		if (!$block->mEnableAutoblock && $block->mUser ) {
			$properties[] = $msg['noautoblockblock'];
		}

		if ( $block->mBlockEmail && $block->mUser ) {
			$properties[] = $msg['emailblock'];
		}
		
		if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
			$properties[] = $msg['blocklist-nousertalk'];
		}

		$properties = $wgLang->commaList( $properties );

		$line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );

		$unblocklink = '';
		$changeblocklink = '';
		$toolLinks = '';
		if ( $wgUser->isAllowed( 'block' ) ) {
			$unblocklink = $sk->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
					$msg['unblocklink'],
					array(),
					array( 'action' => 'unblock', 'id' => $block->mId ),
					'known' );

			# Create changeblocklink for all blocks with exception of autoblocks
			if( !$block->mAuto ) {
				$changeblocklink = wfMsg( 'pipe-separator' ) .
					$sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ), 
						$msg['change-blocklink'],
						array(), array(), 'known' );
			}
			$toolLinks = "($unblocklink$changeblocklink)";
		}

		$comment = $sk->commentBlock( $block->mReason );

		$s = "{$line} $comment";
		if ( $block->mHideName )
			$s = '<span class="history-deleted">' . $s . '</span>';

		wfProfileOut( __METHOD__ );
		return "<li>$s $toolLinks</li>\n";
	}
}

/**
 * @todo document
 * @ingroup Pager
 */
class IPBlocklistPager extends ReverseChronologicalPager {
	public $mForm, $mConds;

	function __construct( $form, $conds = array() ) {
		$this->mForm = $form;
		$this->mConds = $conds;
		parent::__construct();
	}

	function getStartBody() {
		wfProfileIn( __METHOD__ );
		# Do a link batch query
		$this->mResult->seek( 0 );
		$lb = new LinkBatch;

		/*
		while ( $row = $this->mResult->fetchObject() ) {
			$lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
			$lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
			$lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
			$lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
		}*/
		# Faster way
		# Usernames and titles are in fact related by a simple substitution of space -> underscore
		# The last few lines of Title::secureAndSplit() tell the story.
		while ( $row = $this->mResult->fetchObject() ) {
			$name = str_replace( ' ', '_', $row->ipb_by_text );
			$lb->add( NS_USER, $name );
			$lb->add( NS_USER_TALK, $name );
			$name = str_replace( ' ', '_', $row->ipb_address );
			$lb->add( NS_USER, $name );
			$lb->add( NS_USER_TALK, $name );
		}
		$lb->execute();
		wfProfileOut( __METHOD__ );
		return '';
	}

	function formatRow( $row ) {
		$block = new Block;
		$block->initFromRow( $row );
		return $this->mForm->formatRow( $block );
	}

	function getQueryInfo() {
		$conds = $this->mConds;
		$conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
		return array(
			'tables' => 'ipblocks',
			'fields' => '*',
			'conds' => $conds,
		);
	}

	function getIndexField() {
		return 'ipb_timestamp';
	}
}