* Keep message keys lowercase
[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;
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
91 if ( "" != $err ) {
92 $wgOut->setSubtitle( wfMsg( "formerror" ) );
93 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
94 }
95 $token = htmlspecialchars( $wgUser->editToken() );
96
97 $addressPart = false;
98 if ( $this->id ) {
99 $block = Block::newFromID( $this->id );
100 if ( $block ) {
101 $encName = htmlspecialchars( $block->getRedactedName() );
102 $encId = $this->id;
103 $addressPart = $encName . Xml::hidden( 'id', $encId );
104 }
105 }
106 if ( !$addressPart ) {
107 $addressPart = Xml::input( 'wpUnblockAddress', 20, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
108 }
109
110 $wgOut->addHTML(
111 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
112 Xml::openElement( 'table', array( 'border' => '0' ) ).
113 "<tr>
114 <td align='right'>
115 {$ipa}:
116 </td>
117 <td align='left'>
118 {$addressPart}
119 </td>
120 </tr>
121 <tr>
122 <td align='right'>
123 {$ipr}:
124 </td>
125 <td align='left'>" .
126 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
127 "</td>
128 </tr>
129 <tr>
130 <td>&nbsp;</td>
131 <td align='left'>" .
132 Xml::submitButton( $ipus, array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
133 "</td>
134 </tr>" .
135 Xml::closeElement( 'table' ) .
136 Xml::hidden( 'wpEditToken', $token ) .
137 Xml::closeElement( 'form' ) . "\n"
138 );
139
140 }
141
142 function doSubmit() {
143 global $wgOut;
144
145 if ( $this->id ) {
146 $block = Block::newFromID( $this->id );
147 if ( $block ) {
148 $this->ip = $block->getRedactedName();
149 }
150 } else {
151 $block = new Block();
152 $this->ip = trim( $this->ip );
153 if ( substr( $this->ip, 0, 1 ) == "#" ) {
154 $id = substr( $this->ip, 1 );
155 $block = Block::newFromID( $id );
156 } else {
157 $block = Block::newFromDB( $this->ip );
158 if ( !$block ) {
159 $block = null;
160 }
161 }
162 }
163 $success = false;
164 if ( $block ) {
165 # Delete block
166 if ( $block->delete() ) {
167 # Make log entry
168 $log = new LogPage( 'block' );
169 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
170 $success = true;
171 }
172 }
173
174 if ( $success ) {
175 # Report to the user
176 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
177 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
178 $wgOut->redirect( $success );
179 } else {
180 if ( !$this->ip && $this->id ) {
181 $this->ip = '#' . $this->id;
182 }
183 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
184 }
185 }
186
187 function showList( $msg ) {
188 global $wgOut, $wgUser;
189
190 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
191 if ( "" != $msg ) {
192 $wgOut->setSubtitle( $msg );
193 }
194
195 // Purge expired entries on one in every 10 queries
196 if ( !mt_rand( 0, 10 ) ) {
197 Block::purgeExpired();
198 }
199
200 $conds = array();
201 $matches = array();
202 // Is user allowed to see all the blocks?
203 if ( !$wgUser->isAllowed( 'oversight' ) )
204 $conds['ipb_deleted'] = 0;
205 if ( $this->ip == '' ) {
206 // No extra conditions
207 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
208 $conds['ipb_id'] = substr( $this->ip, 1 );
209 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
210 $conds['ipb_address'] = $this->ip;
211 $conds['ipb_auto'] = 0;
212 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
213 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
214 $conds['ipb_auto'] = 0;
215 } else {
216 $user = User::newFromName( $this->ip );
217 if ( $user && ( $id = $user->getID() ) != 0 ) {
218 $conds['ipb_user'] = $id;
219 } else {
220 // Uh...?
221 $conds['ipb_address'] = $this->ip;
222 $conds['ipb_auto'] = 0;
223 }
224 }
225
226 # TODO: difference message between
227 # a) an real empty list and
228 # b) requested ip/username not on list
229 $pager = new IPBlocklistPager( $this, $conds );
230 if ( $pager->getNumRows() ) {
231 $s = $this->searchForm() .
232 $pager->getNavigationBar();
233 $s .= "<ul>" .
234 $pager->getBody() .
235 "</ul>";
236 $s .= $pager->getNavigationBar();
237 } else {
238 $s = $this->searchForm() .
239 '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
240 }
241 $wgOut->addHTML( $s );
242 }
243
244 function searchForm() {
245 global $wgTitle, $wgScript, $wgRequest;
246 return
247 wfElement( 'form', array(
248 'action' => $wgScript ),
249 null ) .
250 wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
251 wfElement( 'input', array(
252 'type' => 'hidden',
253 'name' => 'action',
254 'value' => 'search' ) ).
255 wfElement( 'input', array(
256 'type' => 'hidden',
257 'name' => 'limit',
258 'value' => $wgRequest->getText( 'limit' ) ) ) .
259 wfElement( 'input', array(
260 'name' => 'ip',
261 'value' => $this->ip ) ) .
262 wfElement( 'input', array(
263 'type' => 'submit',
264 'value' => wfMsg( 'ipblocklist-submit' ) ) ) .
265 '</form>';
266 }
267
268 /**
269 * Callback function to output a block
270 */
271 function formatRow( $block ) {
272 global $wgUser, $wgLang;
273
274 wfProfileIn( __METHOD__ );
275
276 static $sk=null, $msg=null;
277
278 if( is_null( $sk ) )
279 $sk = $wgUser->getSkin();
280 if( is_null( $msg ) ) {
281 $msg = array();
282 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
283 'anononlyblock', 'createaccountblock', 'noautoblockblock' );
284 foreach( $keys as $key ) {
285 $msg[$key] = wfMsgHtml( $key );
286 }
287 $msg['blocklistline'] = wfMsg( 'blocklistline' );
288 $msg['contribslink'] = wfMsg( 'contribslink' );
289 }
290
291 # Prepare links to the blocker's user and talk pages
292 $blocker_id = $block->getBy();
293 $blocker_name = $block->getByName();
294 $blocker = $sk->userLink( $blocker_id, $blocker_name );
295 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
296
297 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
298 if( $block->mAuto ) {
299 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
300 } else {
301 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
302 $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
303 }
304
305 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
306
307 $properties = array();
308 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
309 $properties[] = $msg['infiniteblock'];
310 } else {
311 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
312 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
313 }
314 if ( $block->mAnonOnly ) {
315 $properties[] = $msg['anononlyblock'];
316 }
317 if ( $block->mCreateAccount ) {
318 $properties[] = $msg['createaccountblock'];
319 }
320 if (!$block->mEnableAutoblock && $block->mUser ) {
321 $properties[] = $msg['noautoblockblock'];
322 }
323
324 $properties = implode( ', ', $properties );
325
326 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
327
328 $unblocklink = '';
329 if ( $wgUser->isAllowed('block') ) {
330 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
331 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
332 }
333
334 $comment = $sk->commentBlock( $block->mReason );
335
336 $s = "{$line} $comment";
337 if ( $block->mHideName )
338 $s = '<span class="history-deleted">' . $s . '</span>';
339
340 wfProfileOut( __METHOD__ );
341 return "<li>$s $unblocklink</li>\n";
342 }
343 }
344
345 /**
346 * @todo document
347 * @addtogroup Pager
348 */
349 class IPBlocklistPager extends ReverseChronologicalPager {
350 public $mForm, $mConds;
351
352 function __construct( $form, $conds = array() ) {
353 $this->mForm = $form;
354 $this->mConds = $conds;
355 parent::__construct();
356 }
357
358 function getStartBody() {
359 wfProfileIn( __METHOD__ );
360 # Do a link batch query
361 $this->mResult->seek( 0 );
362 $lb = new LinkBatch;
363
364 /*
365 while ( $row = $this->mResult->fetchObject() ) {
366 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
367 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
368 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
369 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
370 }*/
371 # Faster way
372 # Usernames and titles are in fact related by a simple substitution of space -> underscore
373 # The last few lines of Title::secureAndSplit() tell the story.
374 while ( $row = $this->mResult->fetchObject() ) {
375 $name = str_replace( ' ', '_', $row->user_name );
376 $lb->add( NS_USER, $name );
377 $lb->add( NS_USER_TALK, $name );
378 $name = str_replace( ' ', '_', $row->ipb_address );
379 $lb->add( NS_USER, $name );
380 $lb->add( NS_USER_TALK, $name );
381 }
382 $lb->execute();
383 wfProfileOut( __METHOD__ );
384 return '';
385 }
386
387 function formatRow( $row ) {
388 $block = new Block;
389 $block->initFromRow( $row );
390 return $this->mForm->formatRow( $block );
391 }
392
393 function getQueryInfo() {
394 $conds = $this->mConds;
395 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
396 $conds[] = 'ipb_by=user_id';
397 return array(
398 'tables' => array( 'ipblocks', 'user' ),
399 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
400 'conds' => $conds,
401 );
402 }
403
404 function getIndexField() {
405 return 'ipb_timestamp';
406 }
407 }
408
409 ?>