* Allow blocks on anonymous users only.
[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->sysopRequired();
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 = Title::makeTitle( NS_SPECIAL, "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 = Title::makeTitle( NS_SPECIAL, "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 ( wfIP2Unsigned( $this->ip ) !== false ) {
175 $conds['ipb_address'] = $this->ip;
176 $conds['ipb_auto'] = 0;
177 } else {
178 $user = User::newFromName( $this->ip );
179 if ( ( $id = $user->getID() ) != 0 ) {
180 $conds['ipb_user'] = $id;
181 }
182 }
183
184 $pager = new IPBlocklistPager( $this, $conds );
185 $s = $pager->getNavigationBar() .
186 $this->searchForm();
187 if ( $pager->getNumRows() ) {
188 $s .= "<ul>" .
189 $pager->getBody() .
190 "</ul>";
191 } else {
192 $s .= '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
193 }
194 $s .= $pager->getNavigationBar();
195 $wgOut->addHTML( $s );
196 }
197
198 function searchForm() {
199 global $wgTitle, $wgRequest;
200 return
201 wfElement( 'form', array(
202 'action' => $wgTitle->getLocalUrl() ),
203 null ) .
204 wfElement( 'input', array(
205 'type' => 'hidden',
206 'name' => 'action',
207 'value' => 'search' ) ).
208 wfElement( 'input', array(
209 'type' => 'hidden',
210 'name' => 'limit',
211 'value' => $wgRequest->getText( 'limit' ) ) ) .
212 wfElement( 'input', array(
213 'name' => 'ip',
214 'value' => $this->ip ) ) .
215 wfElement( 'input', array(
216 'type' => 'submit',
217 'value' => wfMsg( 'search' ) ) ) .
218 '</form>';
219 }
220
221 /**
222 * Callback function to output a block
223 */
224 function formatRow( $block ) {
225 global $wgUser, $wgLang;
226
227 wfProfileIn( __METHOD__ );
228
229 static $sk=null, $msg=null;
230
231 if( is_null( $sk ) )
232 $sk = $wgUser->getSkin();
233 if( is_null( $msg ) ) {
234 $msg = array();
235 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
236 'anononlyblock', 'createaccountblock' );
237 foreach( $keys as $key ) {
238 $msg[$key] = wfMsgHtml( $key );
239 }
240 $msg['blocklistline'] = wfMsg( 'blocklistline' );
241 $msg['contribslink'] = wfMsg( 'contribslink' );
242 }
243
244 # Prepare links to the blocker's user and talk pages
245 $blocker_name = $block->getByName();
246 $blocker = $sk->MakeLinkObj( Title::makeTitle( NS_USER, $blocker_name ), $blocker_name );
247 $blocker .= ' (' . $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $blocker_name ), $wgLang->getNsText( NS_TALK ) ) . ')';
248
249 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
250 if( $block->mAuto ) {
251 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
252 } else {
253 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
254 $target .= ' (' . $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $msg['contribslink'], 'target=' . urlencode( $block->mAddress ) ) . ')';
255 }
256
257 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
258
259 $properties = array();
260 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
261 $properties[] = $msg['infiniteblock'];
262 } else {
263 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
264 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
265 }
266 if ( $block->mAnonOnly ) {
267 $properties[] = $msg['anononlyblock'];
268 }
269 if ( $block->mCreateAccount ) {
270 $properties[] = $msg['createaccountblock'];
271 }
272 $properties = implode( ', ', $properties );
273
274 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
275
276 $s = "<li>{$line}";
277
278 if ( $wgUser->isAllowed('block') ) {
279 $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
280 $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
281 }
282 $s .= $sk->commentBlock( $block->mReason );
283 $s .= "</li>\n";
284 wfProfileOut( __METHOD__ );
285 return $s;
286 }
287 }
288
289 class IPBlocklistPager extends ReverseChronologicalPager {
290 public $mForm, $mConds;
291
292 function __construct( $form, $conds = array() ) {
293 $this->mForm = $form;
294 $this->mConds = $conds;
295 parent::__construct();
296 }
297
298 function getStartBody() {
299 wfProfileIn( __METHOD__ );
300 # Do a link batch query
301 $this->mResult->seek( 0 );
302 $lb = new LinkBatch;
303
304 /*
305 while ( $row = $this->mResult->fetchObject() ) {
306 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
307 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
308 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
309 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
310 }*/
311 # Faster way
312 # Usernames and titles are in fact related by a simple substitution of space -> underscore
313 # The last few lines of Title::secureAndSplit() tell the story.
314 while ( $row = $this->mResult->fetchObject() ) {
315 $name = str_replace( ' ', '_', $row->user_name );
316 $lb->add( NS_USER, $name );
317 $lb->add( NS_USER_TALK, $name );
318 $name = str_replace( ' ', '_', $row->ipb_address );
319 $lb->add( NS_USER, $name );
320 $lb->add( NS_USER_TALK, $name );
321 }
322 $lb->execute();
323 wfProfileOut( __METHOD__ );
324 return '';
325 }
326
327 function formatRow( $row ) {
328 $block = new Block;
329 $block->initFromRow( $row );
330 return $this->mForm->formatRow( $block );
331 }
332
333 function getQueryInfo() {
334 $conds = $this->mConds;
335 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
336 $conds[] = 'ipb_by=user_id';
337 return array(
338 'tables' => array( 'ipblocks', 'user' ),
339 'fields' => 'ipblocks.*,user_name',
340 'conds' => $conds,
341 );
342 }
343
344 function getIndexField() {
345 return 'ipb_timestamp';
346 }
347 }
348
349 ?>