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