Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2 /**
3 * Constructor for Special:Blockip page
4 *
5 * @addtogroup SpecialPage
6 */
7
8 /**
9 * Constructor
10 */
11 function wfSpecialBlockip( $par ) {
12 global $wgUser, $wgOut, $wgRequest;
13
14 # Can't block when the database is locked
15 if( wfReadOnly() ) {
16 $wgOut->readOnlyPage();
17 return;
18 }
19
20 # Permission check
21 if( !$wgUser->isAllowed( 'block' ) ) {
22 $wgOut->permissionRequired( 'block' );
23 return;
24 }
25
26 $ipb = new IPBlockForm( $par );
27
28 $action = $wgRequest->getVal( 'action' );
29 if ( 'success' == $action ) {
30 $ipb->showSuccess();
31 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
32 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
33 $ipb->doSubmit();
34 } else {
35 $ipb->showForm( '' );
36 }
37 }
38
39 /**
40 * Form object
41 *
42 * @addtogroup SpecialPage
43 */
44 class IPBlockForm {
45 var $BlockAddress, $BlockExpiry, $BlockReason;
46
47 function IPBlockForm( $par ) {
48 global $wgRequest;
49
50 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
51 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
52 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
53 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
54 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
55
56 # Unchecked checkboxes are not included in the form data at all, so having one
57 # that is true by default is a bit tricky
58 $byDefault = !$wgRequest->wasPosted();
59 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
60 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
61 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
62 }
63
64 function showForm( $err ) {
65 global $wgOut, $wgUser, $wgSysopUserBans;
66
67 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
68 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
69
70 if($wgSysopUserBans) {
71 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
72 } else {
73 $mIpaddress = Xml::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
74 }
75 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
76 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
77 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
78 $mIpbreason = Xml::label( wfMsg( 'ipbreason' ), 'mw-bi-reason' );
79 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
80 $action = $titleObj->escapeLocalURL( "action=submit" );
81
82 if ( "" != $err ) {
83 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
84 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
85 }
86
87 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
88
89 $showblockoptions = $scBlockExpiryOptions != '-';
90 if (!$showblockoptions)
91 $mIpbother = $mIpbexpiry;
92
93 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
94 foreach (explode(',', $scBlockExpiryOptions) as $option) {
95 if ( strpos($option, ":") === false ) $option = "$option:$option";
96 list($show, $value) = explode(":", $option);
97 $show = htmlspecialchars($show);
98 $value = htmlspecialchars($value);
99 $selected = "";
100 if ($this->BlockExpiry === $value)
101 $selected = ' selected="selected"';
102 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
103 }
104
105 $token = htmlspecialchars( $wgUser->editToken() );
106
107 $wgOut->addHTML( "
108 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
109 <table border='0'>
110 <tr>
111 <td align=\"right\">{$mIpaddress}:</td>
112 <td align=\"left\">
113 " . Xml::input( 'wpBlockAddress', 40, $this->BlockAddress,
114 array( 'tabindex' => '1', 'id' => 'mw-bi-target' ) ) . "
115 </td>
116 </tr>
117 <tr>");
118 if ($showblockoptions) {
119 $wgOut->addHTML("
120 <td align=\"right\">{$mIpbexpiry}:</td>
121 <td align=\"left\">
122 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
123 $blockExpiryFormOptions
124 </select>
125 </td>
126 ");
127 }
128 $wgOut->addHTML("
129 </tr>
130 <tr id='wpBlockOther'>
131 <td align=\"right\">{$mIpbother}:</td>
132 <td align=\"left\">
133 " . Xml::input( 'wpBlockOther', 40, $this->BlockOther,
134 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
135 </td>
136 </tr>
137 <tr>
138 <td align=\"right\">{$mIpbreason}:</td>
139 <td align=\"left\">
140 " . Xml::input( 'wpBlockReason', 40, $this->BlockReason,
141 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
142 </td>
143 </tr>
144 <tr>
145 <td>&nbsp;</td>
146 <td align=\"left\">
147 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
148 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
149 array( 'tabindex' => 4 ) ) . "
150 </td>
151 </tr>
152 <tr>
153 <td>&nbsp;</td>
154 <td align=\"left\">
155 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
156 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
157 array( 'tabindex' => 5 ) ) . "
158 </td>
159 </tr>
160 <tr>
161 <td>&nbsp;</td>
162 <td align=\"left\">
163 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
164 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
165 array( 'tabindex' => 6 ) ) . "
166 </td>
167 </tr>
168 <tr>
169 <td style='padding-top: 1em'>&nbsp;</td>
170 <td style='padding-top: 1em' align=\"left\">
171 " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
172 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
173 </td>
174 </tr>
175 </table>" .
176 Xml::hidden( 'wpEditToken', $token ) .
177 "</form>\n" );
178
179 $wgOut->addHtml( $this->getConvenienceLinks() );
180
181 $user = User::newFromName( $this->BlockAddress );
182 if( is_object( $user ) ) {
183 $this->showLogFragment( $wgOut, $user->getUserPage() );
184 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
185 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
186 }
187 }
188
189 function doSubmit() {
190 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
191
192 $userId = 0;
193 $this->BlockAddress = trim( $this->BlockAddress );
194 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
195
196 # Check for invalid specifications
197 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
198 $matches = array();
199 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
200 if ( $wgSysopRangeBans ) {
201 if ( $matches[2] > 31 || $matches[2] < 16 ) {
202 $this->showForm( wfMsg( 'ip_range_invalid' ) );
203 return;
204 }
205 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
206 } else {
207 # Range block illegal
208 $this->showForm( wfMsg( 'range_block_disabled' ) );
209 return;
210 }
211 } else {
212 # Username block
213 if ( $wgSysopUserBans ) {
214 $user = User::newFromName( $this->BlockAddress );
215 if( !is_null( $user ) && $user->getID() ) {
216 # Use canonical name
217 $this->BlockAddress = $user->getName();
218 $userId = $user->getID();
219 } else {
220 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
221 return;
222 }
223 } else {
224 $this->showForm( wfMsg( 'badipaddress' ) );
225 return;
226 }
227 }
228 }
229
230 $expirestr = $this->BlockExpiry;
231 if( $expirestr == 'other' )
232 $expirestr = $this->BlockOther;
233
234 if (strlen($expirestr) == 0) {
235 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
236 return;
237 }
238
239 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
240 $expiry = Block::infinity();
241 } else {
242 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
243 $expiry = strtotime( $expirestr );
244
245 if ( $expiry < 0 || $expiry === false ) {
246 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
247 return;
248 }
249
250 $expiry = wfTimestamp( TS_MW, $expiry );
251 }
252
253 # Create block
254 # Note: for a user block, ipb_address is only for display purposes
255
256 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
257 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
258 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
259
260 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
261
262 if ( !$block->insert() ) {
263 $this->showForm( wfMsg( 'ipb_already_blocked',
264 htmlspecialchars( $this->BlockAddress ) ) );
265 return;
266 }
267
268 wfRunHooks('BlockIpComplete', array($block, $wgUser));
269
270 # Prepare log parameters
271 $logParams = array();
272 $logParams[] = $expirestr;
273 $logParams[] = $this->blockLogFlags();
274
275 # Make log entry
276 $log = new LogPage( 'block' );
277 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
278 $this->BlockReason, $logParams );
279
280 # Report to the user
281 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
282 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
283 urlencode( $this->BlockAddress ) ) );
284 }
285 }
286
287 function showSuccess() {
288 global $wgOut;
289
290 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
291 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
292 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
293 $wgOut->addWikiText( $text );
294 }
295
296 function showLogFragment( $out, $title ) {
297 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
298 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
299 $viewer = new LogViewer( new LogReader( $request ) );
300 $viewer->showList( $out );
301 }
302
303 /**
304 * Return a comma-delimited list of "flags" to be passed to the log
305 * reader for this block, to provide more information in the logs
306 *
307 * @return array
308 */
309 private function blockLogFlags() {
310 $flags = array();
311 if( $this->BlockAnonOnly )
312 $flags[] = 'anononly';
313 if( $this->BlockCreateAccount )
314 $flags[] = 'nocreate';
315 if( $this->BlockEnableAutoblock )
316 $flags[] = 'autoblock';
317 return implode( ',', $flags );
318 }
319
320 /**
321 * Builds unblock and block list links
322 *
323 * @return string
324 */
325 private function getConvenienceLinks() {
326 global $wgUser;
327 $skin = $wgUser->getSkin();
328 $links[] = $this->getUnblockLink( $skin );
329 $links[] = $this->getBlockListLink( $skin );
330 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
331 }
332
333 /**
334 * Build a convenient link to unblock the given username or IP
335 * address, if available; otherwise link to a blank unblock
336 * form
337 *
338 * @param $skin Skin to use
339 * @return string
340 */
341 private function getUnblockLink( $skin ) {
342 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
343 if( $this->BlockAddress ) {
344 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
345 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
346 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
347 } else {
348 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
349 }
350 }
351
352 /**
353 * Build a convenience link to the block list
354 *
355 * @param $skin Skin to use
356 * @return string
357 */
358 private function getBlockListLink( $skin ) {
359 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
360 if( $this->BlockAddress ) {
361 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
362 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
363 'ip=' . urlencode( $this->BlockAddress ) );
364 } else {
365 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
366 }
367 }
368 }
369 ?>