* Refactored IPUnblockForm::doUnblock() to return an array of message keys and parameters
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * @addtogroup 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 * @addtogroup 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 function showForm( $err ) {
80 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
81
82 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
83 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
84
85 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
86 $ipr = wfMsgHtml( 'ipbreason' );
87 $ipus = wfMsgHtml( 'ipusubmit' );
88 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
89 $action = $titleObj->getLocalURL( "action=submit" );
90 $alignRight = $wgContLang->isRtl() ? 'left' : 'right';
91
92 if ( "" != $err ) {
93 $wgOut->setSubtitle( wfMsg( "formerror" ) );
94 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
95 }
96 $token = htmlspecialchars( $wgUser->editToken() );
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 }
106 }
107 if ( !$addressPart ) {
108 $addressPart = Xml::input( 'wpUnblockAddress', 20, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
109 }
110
111 $wgOut->addHTML(
112 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
113 Xml::openElement( 'table', array( 'border' => '0' ) ).
114 "<tr>
115 <td align='$alignRight'>
116 {$ipa}
117 </td>
118 <td>
119 {$addressPart}
120 </td>
121 </tr>
122 <tr>
123 <td align='$alignRight'>
124 {$ipr}
125 </td>
126 <td>" .
127 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
128 "</td>
129 </tr>
130 <tr>
131 <td>&nbsp;</td>
132 <td>" .
133 Xml::submitButton( $ipus, array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
134 "</td>
135 </tr>" .
136 Xml::closeElement( 'table' ) .
137 Xml::hidden( 'wpEditToken', $token ) .
138 Xml::closeElement( 'form' ) . "\n"
139 );
140
141 }
142
143 const UNBLOCK_SUCCESS = 0; // Success
144 const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
145 const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
146 const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
147 const UNBLOCK_UNKNOWNERR = 4; // Unknown error
148
149 /**
150 * Backend code for unblocking. doSubmit() wraps around this.
151 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
152 * case it contains the range $ip is part of.
153 * @return array array(message key, parameters) on failure, empty array on success
154 */
155
156 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
157 {
158 if ( $id ) {
159 $block = Block::newFromID( $id );
160 if ( !$block ) {
161 return array('ipb_cant_unblock', htmlspecialchars($id));
162 }
163 $ip = $block->getRedactedName();
164 } else {
165 $block = new Block();
166 $ip = trim( $ip );
167 if ( substr( $ip, 0, 1 ) == "#" ) {
168 $id = substr( $ip, 1 );
169 $block = Block::newFromID( $id );
170 if( !$block ) {
171 return array('ipb_cant_unblock', htmlspecialchars($id));
172 }
173 $ip = $block->getRedactedName();
174 } else {
175 $block = Block::newFromDB( $ip );
176 if ( !$block ) {
177 return array('ipb_cant_unblock', htmlspecialchars($id));
178 }
179 if( $block->mRangeStart != $block->mRangeEnd
180 && !strstr( $ip, "/" ) ) {
181 /* If the specified IP is a single address, and the block is
182 * a range block, don't unblock the range. */
183 $range = $block->mAddress;
184 return array('ipb_blocked_as_range', $ip, $range);
185 }
186 }
187 }
188 // Yes, this is really necessary
189 $id = $block->mId;
190
191 # Delete block
192 if ( !$block->delete() ) {
193 return array('ipb_cant_unblock', htmlspecialchars($id));
194 }
195
196 # Make log entry
197 $log = new LogPage( 'block' );
198 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
199 return array();
200 }
201
202 function doSubmit() {
203 global $wgOut;
204 $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
205 if(!empty($retval))
206 {
207 $key = array_shift($retval);
208 $this->showForm(wfMsgReal($key, $retval));
209 return;
210 }
211 # Report to the user
212 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
213 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
214 $wgOut->redirect( $success );
215 }
216
217 function showList( $msg ) {
218 global $wgOut, $wgUser;
219
220 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
221 if ( "" != $msg ) {
222 $wgOut->setSubtitle( $msg );
223 }
224
225 // Purge expired entries on one in every 10 queries
226 if ( !mt_rand( 0, 10 ) ) {
227 Block::purgeExpired();
228 }
229
230 $conds = array();
231 $matches = array();
232 // Is user allowed to see all the blocks?
233 if ( !$wgUser->isAllowed( 'oversight' ) )
234 $conds['ipb_deleted'] = 0;
235 if ( $this->ip == '' ) {
236 // No extra conditions
237 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
238 $conds['ipb_id'] = substr( $this->ip, 1 );
239 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
240 $conds['ipb_address'] = $this->ip;
241 $conds['ipb_auto'] = 0;
242 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
243 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
244 $conds['ipb_auto'] = 0;
245 } else {
246 $user = User::newFromName( $this->ip );
247 if ( $user && ( $id = $user->getID() ) != 0 ) {
248 $conds['ipb_user'] = $id;
249 } else {
250 // Uh...?
251 $conds['ipb_address'] = $this->ip;
252 $conds['ipb_auto'] = 0;
253 }
254 }
255
256 $pager = new IPBlocklistPager( $this, $conds );
257 if ( $pager->getNumRows() ) {
258 $wgOut->addHTML(
259 $this->searchForm() .
260 $pager->getNavigationBar() .
261 Xml::tags( 'ul', null, $pager->getBody() ) .
262 $pager->getNavigationBar()
263 );
264 } elseif ( $this->ip != '') {
265 $wgOut->addHTML( $this->searchForm() );
266 $wgOut->addWikiText( wfMsg( 'ipblocklist-no-results' ) );
267 } else {
268 $wgOut->addWikiText( wfMsg( 'ipblocklist-empty' ) );
269 }
270 }
271
272 function searchForm() {
273 global $wgTitle, $wgScript, $wgRequest;
274 return
275 Xml::tags( 'form', array( 'action' => $wgScript ),
276 Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
277 Xml::openElement( 'fieldset' ) .
278 Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
279 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
280 '&nbsp;' .
281 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
282 Xml::closeElement( 'fieldset' )
283 );
284 }
285
286 /**
287 * Callback function to output a block
288 */
289 function formatRow( $block ) {
290 global $wgUser, $wgLang;
291
292 wfProfileIn( __METHOD__ );
293
294 static $sk=null, $msg=null;
295
296 if( is_null( $sk ) )
297 $sk = $wgUser->getSkin();
298 if( is_null( $msg ) ) {
299 $msg = array();
300 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
301 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
302 foreach( $keys as $key ) {
303 $msg[$key] = wfMsgHtml( $key );
304 }
305 $msg['blocklistline'] = wfMsg( 'blocklistline' );
306 }
307
308 # Prepare links to the blocker's user and talk pages
309 $blocker_id = $block->getBy();
310 $blocker_name = $block->getByName();
311 $blocker = $sk->userLink( $blocker_id, $blocker_name );
312 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
313
314 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
315 if( $block->mAuto ) {
316 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
317 } else {
318 $target = $sk->userLink( $block->mUser, $block->mAddress )
319 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
320 }
321
322 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
323
324 $properties = array();
325 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
326 $properties[] = $msg['infiniteblock'];
327 } else {
328 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
329 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
330 }
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 * @addtogroup 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->user_name );
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 $conds[] = 'ipb_by=user_id';
418 return array(
419 'tables' => array( 'ipblocks', 'user' ),
420 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
421 'conds' => $conds,
422 );
423 }
424
425 function getIndexField() {
426 return 'ipb_timestamp';
427 }
428 }
429
430