Re-encode in utf-8 and removed trailing whitespaces
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 */
10 function wfSpecialIpblocklist() {
11 global $wgUser, $wgOut, $wgRequest;
12
13 $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
14 $id = $wgRequest->getVal( 'id' );
15 $reason = $wgRequest->getText( 'wpUnblockReason' );
16 $action = $wgRequest->getText( 'action' );
17 $successip = $wgRequest->getVal( 'successip' );
18
19 $ipu = new IPUnblockForm( $ip, $id, $reason );
20
21 if( $action == 'unblock' ) {
22 # Check permissions
23 if( !$wgUser->isAllowed( 'block' ) ) {
24 $wgOut->permissionRequired( 'block' );
25 return;
26 }
27 # Check for database lock
28 if( wfReadOnly() ) {
29 $wgOut->readOnlyPage();
30 return;
31 }
32 # Show unblock form
33 $ipu->showForm( '' );
34 } elseif( $action == 'submit' && $wgRequest->wasPosted()
35 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
36 # Check permissions
37 if( !$wgUser->isAllowed( 'block' ) ) {
38 $wgOut->permissionRequired( 'block' );
39 return;
40 }
41 # Check for database lock
42 if( wfReadOnly() ) {
43 $wgOut->readOnlyPage();
44 return;
45 }
46 # Remove blocks and redirect user to success page
47 $ipu->doSubmit();
48 } elseif( $action == 'success' ) {
49 # Inform the user of a successful unblock
50 # (No need to check permissions or locks here,
51 # if something was done, then it's too late!)
52 if ( substr( $successip, 0, 1) == '#' ) {
53 // A block ID was unblocked
54 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
55 } else {
56 // A username/IP was unblocked
57 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
58 }
59 } else {
60 # Just show the block list
61 $ipu->showList( '' );
62 }
63
64 }
65
66 /**
67 * implements Special:ipblocklist GUI
68 * @ingroup SpecialPage
69 */
70 class IPUnblockForm {
71 var $ip, $reason, $id;
72
73 function IPUnblockForm( $ip, $id, $reason ) {
74 $this->ip = strtr( $ip, '_', ' ' );
75 $this->id = $id;
76 $this->reason = $reason;
77 }
78
79 /**
80 * Generates the unblock form
81 * @param $err string: error message
82 * @return $out string: HTML form
83 */
84 function showForm( $err ) {
85 global $wgOut, $wgUser, $wgSysopUserBans;
86
87 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
88 $wgOut->addWikiMsg( 'unblockiptext' );
89
90 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
91 $action = $titleObj->getLocalURL( "action=submit" );
92
93 if ( "" != $err ) {
94 $wgOut->setSubtitle( wfMsg( "formerror" ) );
95 $wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
96 }
97
98 $addressPart = false;
99 if ( $this->id ) {
100 $block = Block::newFromID( $this->id );
101 if ( $block ) {
102 $encName = htmlspecialchars( $block->getRedactedName() );
103 $encId = $this->id;
104 $addressPart = $encName . Xml::hidden( 'id', $encId );
105 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
106 }
107 }
108 if ( !$addressPart ) {
109 $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
110 $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
111 }
112
113 $wgOut->addHTML(
114 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
115 Xml::openElement( 'fieldset' ) .
116 Xml::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
117 Xml::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
118 "<tr>
119 <td class='mw-label'>
120 {$ipa}
121 </td>
122 <td class='mw-input'>
123 {$addressPart}
124 </td>
125 </tr>
126 <tr>
127 <td class='mw-label'>" .
128 Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
129 "</td>
130 <td class='mw-input'>" .
131 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
132 "</td>
133 </tr>
134 <tr>
135 <td>&nbsp;</td>
136 <td class='mw-submit'>" .
137 Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
138 "</td>
139 </tr>" .
140 Xml::closeElement( 'table' ) .
141 Xml::closeElement( 'fieldset' ) .
142 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
143 Xml::closeElement( 'form' ) . "\n"
144 );
145
146 }
147
148 const UNBLOCK_SUCCESS = 0; // Success
149 const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
150 const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
151 const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
152 const UNBLOCK_UNKNOWNERR = 4; // Unknown error
153
154 /**
155 * Backend code for unblocking. doSubmit() wraps around this.
156 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
157 * case it contains the range $ip is part of.
158 * @return array array(message key, parameters) on failure, empty array on success
159 */
160
161 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
162 {
163 if ( $id ) {
164 $block = Block::newFromID( $id );
165 if ( !$block ) {
166 return array('ipb_cant_unblock', htmlspecialchars($id));
167 }
168 $ip = $block->getRedactedName();
169 } else {
170 $block = new Block();
171 $ip = trim( $ip );
172 if ( substr( $ip, 0, 1 ) == "#" ) {
173 $id = substr( $ip, 1 );
174 $block = Block::newFromID( $id );
175 if( !$block ) {
176 return array('ipb_cant_unblock', htmlspecialchars($id));
177 }
178 $ip = $block->getRedactedName();
179 } else {
180 $block = Block::newFromDB( $ip );
181 if ( !$block ) {
182 return array('ipb_cant_unblock', htmlspecialchars($id));
183 }
184 if( $block->mRangeStart != $block->mRangeEnd
185 && !strstr( $ip, "/" ) ) {
186 /* If the specified IP is a single address, and the block is
187 * a range block, don't unblock the range. */
188 $range = $block->mAddress;
189 return array('ipb_blocked_as_range', $ip, $range);
190 }
191 }
192 }
193 // Yes, this is really necessary
194 $id = $block->mId;
195
196 # Delete block
197 if ( !$block->delete() ) {
198 return array('ipb_cant_unblock', htmlspecialchars($id));
199 }
200
201 # Make log entry
202 $log = new LogPage( 'block' );
203 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
204 return array();
205 }
206
207 function doSubmit() {
208 global $wgOut;
209 $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
210 if(!empty($retval))
211 {
212 $key = array_shift($retval);
213 $this->showForm(wfMsgReal($key, $retval));
214 return;
215 }
216 # Report to the user
217 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
218 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
219 $wgOut->redirect( $success );
220 }
221
222 function showList( $msg ) {
223 global $wgOut, $wgUser;
224
225 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
226 if ( "" != $msg ) {
227 $wgOut->setSubtitle( $msg );
228 }
229
230 // Purge expired entries on one in every 10 queries
231 if ( !mt_rand( 0, 10 ) ) {
232 Block::purgeExpired();
233 }
234
235 $conds = array();
236 $matches = array();
237 // Is user allowed to see all the blocks?
238 if ( !$wgUser->isAllowed( 'suppress' ) )
239 $conds['ipb_deleted'] = 0;
240 if ( $this->ip == '' ) {
241 // No extra conditions
242 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
243 $conds['ipb_id'] = substr( $this->ip, 1 );
244 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
245 $conds['ipb_address'] = $this->ip;
246 $conds['ipb_auto'] = 0;
247 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
248 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
249 $conds['ipb_auto'] = 0;
250 } else {
251 $user = User::newFromName( $this->ip );
252 if ( $user && ( $id = $user->getId() ) != 0 ) {
253 $conds['ipb_user'] = $id;
254 } else {
255 // Uh...?
256 $conds['ipb_address'] = $this->ip;
257 $conds['ipb_auto'] = 0;
258 }
259 }
260
261 $pager = new IPBlocklistPager( $this, $conds );
262 if ( $pager->getNumRows() ) {
263 $wgOut->addHTML(
264 $this->searchForm() .
265 $pager->getNavigationBar() .
266 Xml::tags( 'ul', null, $pager->getBody() ) .
267 $pager->getNavigationBar()
268 );
269 } elseif ( $this->ip != '') {
270 $wgOut->addHTML( $this->searchForm() );
271 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
272 } else {
273 $wgOut->addWikiMsg( 'ipblocklist-empty' );
274 }
275 }
276
277 function searchForm() {
278 global $wgTitle, $wgScript, $wgRequest;
279 return
280 Xml::tags( 'form', array( 'action' => $wgScript ),
281 Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
282 Xml::openElement( 'fieldset' ) .
283 Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
284 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
285 '&nbsp;' .
286 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
287 Xml::closeElement( 'fieldset' )
288 );
289 }
290
291 /**
292 * Callback function to output a block
293 */
294 function formatRow( $block ) {
295 global $wgUser, $wgLang;
296
297 wfProfileIn( __METHOD__ );
298
299 static $sk=null, $msg=null;
300
301 if( is_null( $sk ) )
302 $sk = $wgUser->getSkin();
303 if( is_null( $msg ) ) {
304 $msg = array();
305 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
306 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
307 foreach( $keys as $key ) {
308 $msg[$key] = wfMsgHtml( $key );
309 }
310 $msg['blocklistline'] = wfMsg( 'blocklistline' );
311 }
312
313 # Prepare links to the blocker's user and talk pages
314 $blocker_id = $block->getBy();
315 $blocker_name = $block->getByName();
316 $blocker = $sk->userLink( $blocker_id, $blocker_name );
317 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
318
319 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
320 if( $block->mAuto ) {
321 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
322 } else {
323 $target = $sk->userLink( $block->mUser, $block->mAddress )
324 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
325 }
326
327 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
328
329 $properties = array();
330 $properties[] = Block::formatExpiry( $block->mExpiry );
331 if ( $block->mAnonOnly ) {
332 $properties[] = $msg['anononlyblock'];
333 }
334 if ( $block->mCreateAccount ) {
335 $properties[] = $msg['createaccountblock'];
336 }
337 if (!$block->mEnableAutoblock && $block->mUser ) {
338 $properties[] = $msg['noautoblockblock'];
339 }
340
341 if ( $block->mBlockEmail && $block->mUser ) {
342 $properties[] = $msg['emailblock'];
343 }
344
345 $properties = implode( ', ', $properties );
346
347 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
348
349 $unblocklink = '';
350 if ( $wgUser->isAllowed('block') ) {
351 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
352 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
353 }
354
355 $comment = $sk->commentBlock( $block->mReason );
356
357 $s = "{$line} $comment";
358 if ( $block->mHideName )
359 $s = '<span class="history-deleted">' . $s . '</span>';
360
361 wfProfileOut( __METHOD__ );
362 return "<li>$s $unblocklink</li>\n";
363 }
364 }
365
366 /**
367 * @todo document
368 * @ingroup Pager
369 */
370 class IPBlocklistPager extends ReverseChronologicalPager {
371 public $mForm, $mConds;
372
373 function __construct( $form, $conds = array() ) {
374 $this->mForm = $form;
375 $this->mConds = $conds;
376 parent::__construct();
377 }
378
379 function getStartBody() {
380 wfProfileIn( __METHOD__ );
381 # Do a link batch query
382 $this->mResult->seek( 0 );
383 $lb = new LinkBatch;
384
385 /*
386 while ( $row = $this->mResult->fetchObject() ) {
387 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
388 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
389 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
390 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
391 }*/
392 # Faster way
393 # Usernames and titles are in fact related by a simple substitution of space -> underscore
394 # The last few lines of Title::secureAndSplit() tell the story.
395 while ( $row = $this->mResult->fetchObject() ) {
396 $name = str_replace( ' ', '_', $row->ipb_by_text );
397 $lb->add( NS_USER, $name );
398 $lb->add( NS_USER_TALK, $name );
399 $name = str_replace( ' ', '_', $row->ipb_address );
400 $lb->add( NS_USER, $name );
401 $lb->add( NS_USER_TALK, $name );
402 }
403 $lb->execute();
404 wfProfileOut( __METHOD__ );
405 return '';
406 }
407
408 function formatRow( $row ) {
409 $block = new Block;
410 $block->initFromRow( $row );
411 return $this->mForm->formatRow( $block );
412 }
413
414 function getQueryInfo() {
415 $conds = $this->mConds;
416 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
417 return array(
418 'tables' => 'ipblocks',
419 'fields' => '*',
420 'conds' => $conds,
421 );
422 }
423
424 function getIndexField() {
425 return 'ipb_timestamp';
426 }
427 }