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