b90799f4c1437e8fd21a4b203e5eb6fa4b088333
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * @todo document
10 */
11 function wfSpecialIpblocklist() {
12 global $wgUser, $wgOut, $wgRequest;
13
14 $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
15 $id = $wgRequest->getVal( 'id' );
16 $reason = $wgRequest->getText( 'wpUnblockReason' );
17 $action = $wgRequest->getText( 'action' );
18 $successip = $wgRequest->getVal( 'successip' );
19
20 $ipu = new IPUnblockForm( $ip, $id, $reason );
21
22 if ( "success" == $action ) {
23 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
24 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
25 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
26 if ( ! $wgUser->isAllowed('block') ) {
27 $wgOut->permissionRequired( 'block' );
28 return;
29 }
30 # Can't unblock when the database is locked
31 if( wfReadOnly() ) {
32 $wgOut->readOnlyPage();
33 return;
34 }
35 $ipu->doSubmit();
36 } else if ( "unblock" == $action ) {
37 # Can't unblock when the database is locked
38 if( wfReadOnly() ) {
39 $wgOut->readOnlyPage();
40 return;
41 }
42 $ipu->showForm( "" );
43 } else {
44 $ipu->showList( "" );
45 }
46 }
47
48 /**
49 *
50 * @package MediaWiki
51 * @subpackage SpecialPage
52 */
53 class IPUnblockForm {
54 var $ip, $reason, $id;
55
56 function IPUnblockForm( $ip, $id, $reason ) {
57 $this->ip = strtr( $ip, '_', ' ' );
58 $this->id = $id;
59 $this->reason = $reason;
60 }
61
62 function showForm( $err ) {
63 global $wgOut, $wgUser, $wgSysopUserBans;
64
65 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
66 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
67
68 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
69 $ipr = wfMsgHtml( 'ipbreason' );
70 $ipus = wfMsgHtml( 'ipusubmit' );
71 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
72 $action = $titleObj->escapeLocalURL( "action=submit" );
73
74 if ( "" != $err ) {
75 $wgOut->setSubtitle( wfMsg( "formerror" ) );
76 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
77 }
78 $token = htmlspecialchars( $wgUser->editToken() );
79
80 $addressPart = false;
81 if ( $this->id ) {
82 $block = Block::newFromID( $this->id );
83 if ( $block ) {
84 $encName = htmlspecialchars( $block->getRedactedName() );
85 $encId = htmlspecialchars( $this->id );
86 $addressPart = $encName . "<input type='hidden' name=\"id\" value=\"$encId\" />";
87 }
88 }
89 if ( !$addressPart ) {
90 $addressPart = "<input tabindex='1' type='text' size='20' " .
91 "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
92 }
93
94 $wgOut->addHTML( "
95 <form id=\"unblockip\" method=\"post\" action=\"{$action}\">
96 <table border='0'>
97 <tr>
98 <td align='right'>{$ipa}:</td>
99 <td align='left'>
100 {$addressPart}
101 </td>
102 </tr>
103 <tr>
104 <td align='right'>{$ipr}:</td>
105 <td align='left'>
106 <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
107 </td>
108 </tr>
109 <tr>
110 <td>&nbsp;</td>
111 <td align='left'>
112 <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
113 </td>
114 </tr>
115 </table>
116 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
117 </form>\n" );
118
119 }
120
121 function doSubmit() {
122 global $wgOut;
123
124 if ( $this->id ) {
125 $block = Block::newFromID( $this->id );
126 if ( $block ) {
127 $this->ip = $block->getRedactedName();
128 }
129 } else {
130 $block = new Block();
131 $this->ip = trim( $this->ip );
132 if ( substr( $this->ip, 0, 1 ) == "#" ) {
133 $id = substr( $this->ip, 1 );
134 $block = Block::newFromID( $id );
135 } else {
136 $block = Block::newFromDB( $this->ip );
137 if ( !$block ) {
138 $block = null;
139 }
140 }
141 }
142 $success = false;
143 if ( $block ) {
144 # Delete block
145 if ( $block->delete() ) {
146 # Make log entry
147 $log = new LogPage( 'block' );
148 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
149 $success = true;
150 }
151 }
152
153 if ( $success ) {
154 # Report to the user
155 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
156 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
157 $wgOut->redirect( $success );
158 } else {
159 if ( !$this->ip && $this->id ) {
160 $this->ip = '#' . $this->id;
161 }
162 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
163 }
164 }
165
166 function showList( $msg ) {
167 global $wgOut;
168
169 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
170 if ( "" != $msg ) {
171 $wgOut->setSubtitle( $msg );
172 }
173
174 // Purge expired entries on one in every 10 queries
175 if ( !mt_rand( 0, 10 ) ) {
176 Block::purgeExpired();
177 }
178
179 $conds = array();
180 $matches = array();
181 if ( $this->ip == '' ) {
182 // No extra conditions
183 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
184 $conds['ipb_id'] = substr( $this->ip, 1 );
185 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
186 $conds['ipb_address'] = $this->ip;
187 $conds['ipb_auto'] = 0;
188 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
189 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
190 $conds['ipb_auto'] = 0;
191 } else {
192 $user = User::newFromName( $this->ip );
193 if ( $user && ( $id = $user->getID() ) != 0 ) {
194 $conds['ipb_user'] = $id;
195 } else {
196 // Uh...?
197 $conds['ipb_address'] = $this->ip;
198 $conds['ipb_auto'] = 0;
199 }
200 }
201
202 $pager = new IPBlocklistPager( $this, $conds );
203 $s = $pager->getNavigationBar() .
204 $this->searchForm();
205 if ( $pager->getNumRows() ) {
206 $s .= "<ul>" .
207 $pager->getBody() .
208 "</ul>";
209 } else {
210 $s .= '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
211 }
212 $s .= $pager->getNavigationBar();
213 $wgOut->addHTML( $s );
214 }
215
216 function searchForm() {
217 global $wgTitle, $wgScript, $wgRequest;
218 return
219 wfElement( 'form', array(
220 'action' => $wgScript ),
221 null ) .
222 wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
223 wfElement( 'input', array(
224 'type' => 'hidden',
225 'name' => 'action',
226 'value' => 'search' ) ).
227 wfElement( 'input', array(
228 'type' => 'hidden',
229 'name' => 'limit',
230 'value' => $wgRequest->getText( 'limit' ) ) ) .
231 wfElement( 'input', array(
232 'name' => 'ip',
233 'value' => $this->ip ) ) .
234 wfElement( 'input', array(
235 'type' => 'submit',
236 'value' => wfMsg( 'searchbutton' ) ) ) .
237 '</form>';
238 }
239
240 /**
241 * Callback function to output a block
242 */
243 function formatRow( $block ) {
244 global $wgUser, $wgLang;
245
246 wfProfileIn( __METHOD__ );
247
248 static $sk=null, $msg=null;
249
250 if( is_null( $sk ) )
251 $sk = $wgUser->getSkin();
252 if( is_null( $msg ) ) {
253 $msg = array();
254 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
255 'anononlyblock', 'createaccountblock', 'noautoblockblock' );
256 foreach( $keys as $key ) {
257 $msg[$key] = wfMsgHtml( $key );
258 }
259 $msg['blocklistline'] = wfMsg( 'blocklistline' );
260 $msg['contribslink'] = wfMsg( 'contribslink' );
261 }
262
263 # Prepare links to the blocker's user and talk pages
264 $blocker_id = $block->getBy();
265 $blocker_name = $block->getByName();
266 $blocker = $sk->userLink( $blocker_id, $blocker_name );
267 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
268
269 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
270 if( $block->mAuto ) {
271 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
272 } else {
273 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
274 $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
275 }
276
277 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
278
279 $properties = array();
280 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
281 $properties[] = $msg['infiniteblock'];
282 } else {
283 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
284 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
285 }
286 if ( $block->mAnonOnly ) {
287 $properties[] = $msg['anononlyblock'];
288 }
289 if ( $block->mCreateAccount ) {
290 $properties[] = $msg['createaccountblock'];
291 }
292 if (!$block->mEnableAutoblock && $block->mUser ) {
293 $properties[] = $msg['noautoblockblock'];
294 }
295
296 $properties = implode( ', ', $properties );
297
298 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
299
300 $s = "<li>{$line}";
301
302 if ( $wgUser->isAllowed('block') ) {
303 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
304 $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
305 }
306 $s .= $sk->commentBlock( $block->mReason );
307 $s .= "</li>\n";
308 wfProfileOut( __METHOD__ );
309 return $s;
310 }
311 }
312
313 class IPBlocklistPager extends ReverseChronologicalPager {
314 public $mForm, $mConds;
315
316 function __construct( $form, $conds = array() ) {
317 $this->mForm = $form;
318 $this->mConds = $conds;
319 parent::__construct();
320 }
321
322 function getStartBody() {
323 wfProfileIn( __METHOD__ );
324 # Do a link batch query
325 $this->mResult->seek( 0 );
326 $lb = new LinkBatch;
327
328 /*
329 while ( $row = $this->mResult->fetchObject() ) {
330 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
331 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
332 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
333 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
334 }*/
335 # Faster way
336 # Usernames and titles are in fact related by a simple substitution of space -> underscore
337 # The last few lines of Title::secureAndSplit() tell the story.
338 while ( $row = $this->mResult->fetchObject() ) {
339 $name = str_replace( ' ', '_', $row->user_name );
340 $lb->add( NS_USER, $name );
341 $lb->add( NS_USER_TALK, $name );
342 $name = str_replace( ' ', '_', $row->ipb_address );
343 $lb->add( NS_USER, $name );
344 $lb->add( NS_USER_TALK, $name );
345 }
346 $lb->execute();
347 wfProfileOut( __METHOD__ );
348 return '';
349 }
350
351 function formatRow( $row ) {
352 $block = new Block;
353 $block->initFromRow( $row );
354 return $this->mForm->formatRow( $block );
355 }
356
357 function getQueryInfo() {
358 $conds = $this->mConds;
359 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
360 $conds[] = 'ipb_by=user_id';
361 return array(
362 'tables' => array( 'ipblocks', 'user' ),
363 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
364 'conds' => $conds,
365 );
366 }
367
368 function getIndexField() {
369 return 'ipb_timestamp';
370 }
371 }
372
373 ?>