PHPDocumentor [http://en.wikipedia.org/wiki/PhpDocumentor] documentation tweaking...
[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 for the Special:Blockip page.
41 *
42 * @addtogroup SpecialPage
43 */
44 class IPBlockForm {
45 var $BlockAddress, $BlockExpiry, $BlockReason;
46
47 function IPBlockForm( $par ) {
48 global $wgRequest, $wgUser;
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 # Re-check user's rights to hide names, very serious, defaults to 0
63 $this->BlockHideName = $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' );
64 }
65
66 function showForm( $err ) {
67 global $wgOut, $wgUser, $wgSysopUserBans;
68
69 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
70 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
71
72 if($wgSysopUserBans) {
73 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
74 } else {
75 $mIpaddress = Xml::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
76 }
77 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
78 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
79 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
80 $mIpbreason = Xml::label( wfMsg( 'ipbreason' ), 'mw-bi-reason' );
81 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
82 $action = $titleObj->escapeLocalURL( "action=submit" );
83
84 if ( "" != $err ) {
85 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
86 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
87 }
88
89 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
90
91 $showblockoptions = $scBlockExpiryOptions != '-';
92 if (!$showblockoptions)
93 $mIpbother = $mIpbexpiry;
94
95 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
96 foreach (explode(',', $scBlockExpiryOptions) as $option) {
97 if ( strpos($option, ":") === false ) $option = "$option:$option";
98 list($show, $value) = explode(":", $option);
99 $show = htmlspecialchars($show);
100 $value = htmlspecialchars($value);
101 $selected = "";
102 if ($this->BlockExpiry === $value)
103 $selected = ' selected="selected"';
104 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
105 }
106
107 $token = htmlspecialchars( $wgUser->editToken() );
108
109 global $wgStylePath, $wgStyleVersion;
110 $wgOut->addHTML( "
111 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
112 </script>
113 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
114 <table border='0'>
115 <tr>
116 <td align=\"right\">{$mIpaddress}:</td>
117 <td align=\"left\">
118 " . Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
119 array(
120 'tabindex' => '1',
121 'id' => 'mw-bi-target',
122 'onchange' => 'updateBlockOptions()' ) ) . "
123 </td>
124 </tr>
125 <tr>");
126 if ($showblockoptions) {
127 $wgOut->addHTML("
128 <td align=\"right\">{$mIpbexpiry}:</td>
129 <td align=\"left\">
130 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
131 $blockExpiryFormOptions
132 </select>
133 </td>
134 ");
135 }
136
137 $wgOut->addHTML("
138 </tr>
139 <tr id='wpBlockOther'>
140 <td align=\"right\">{$mIpbother}:</td>
141 <td align=\"left\">
142 " . Xml::input( 'wpBlockOther', 45, $this->BlockOther,
143 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
144 </td>
145 </tr>
146 <tr>
147 <td align=\"right\">{$mIpbreason}:</td>
148 <td align=\"left\">
149 " . Xml::input( 'wpBlockReason', 45, $this->BlockReason,
150 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
151 </td>
152 </tr>
153 <tr id='wpAnonOnlyRow'>
154 <td>&nbsp;</td>
155 <td align=\"left\">
156 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
157 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
158 array( 'tabindex' => '4' ) ) . "
159 </td>
160 </tr>
161 <tr id='wpCreateAccountRow'>
162 <td>&nbsp;</td>
163 <td align=\"left\">
164 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
165 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
166 array( 'tabindex' => '5' ) ) . "
167 </td>
168 </tr>
169 <tr id='wpEnableAutoblockRow'>
170 <td>&nbsp;</td>
171 <td align=\"left\">
172 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
173 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
174 array( 'tabindex' => '6' ) ) . "
175 </td>
176 </tr>
177 ");
178 // Allow some users to hide name from block log, blocklist and listusers
179 if ( $wgUser->isAllowed( 'hideuser' ) ) {
180 $wgOut->addHTML("
181 <tr>
182 <td>&nbsp;</td>
183 <td align=\"left\">
184 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
185 'wpHideName', 'wpHideName', $this->BlockHideName,
186 array( 'tabindex' => '6' ) ) . "
187 </td>
188 </tr>
189 ");
190 }
191 $wgOut->addHTML("
192 <tr>
193 <td style='padding-top: 1em'>&nbsp;</td>
194 <td style='padding-top: 1em' align=\"left\">
195 " . Xml::submitButton( wfMsgHtml( 'ipbsubmit' ),
196 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
197 </td>
198 </tr>
199 </table>" .
200 Xml::hidden( 'wpEditToken', $token ) .
201 "</form>
202 <script type=\"text/javascript\">updateBlockOptions()</script>
203 \n" );
204
205 $wgOut->addHtml( $this->getConvenienceLinks() );
206
207 $user = User::newFromName( $this->BlockAddress );
208 if( is_object( $user ) ) {
209 $this->showLogFragment( $wgOut, $user->getUserPage() );
210 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
211 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
212 } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress ) ) {
213 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
214 }
215 }
216
217 function doSubmit() {
218 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
219
220 $userId = 0;
221 # Expand valid IPv6 addresses, usernames are left as is
222 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
223 # isIPv4() and IPv6() are used for final validation
224 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
225 $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
226 $rxIP = "($rxIP4|$rxIP6)";
227
228 # Check for invalid specifications
229 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
230 $matches = array();
231 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
232 # IPv4
233 if ( $wgSysopRangeBans ) {
234 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
235 $this->showForm( wfMsg( 'ip_range_invalid' ) );
236 return;
237 }
238 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
239 } else {
240 # Range block illegal
241 $this->showForm( wfMsg( 'range_block_disabled' ) );
242 return;
243 }
244 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
245 # IPv6
246 if ( $wgSysopRangeBans ) {
247 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
248 $this->showForm( wfMsg( 'ip_range_invalid' ) );
249 return;
250 }
251 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
252 } else {
253 # Range block illegal
254 $this->showForm( wfMsg( 'range_block_disabled' ) );
255 return;
256 }
257 } else {
258 # Username block
259 if ( $wgSysopUserBans ) {
260 $user = User::newFromName( $this->BlockAddress );
261 if( !is_null( $user ) && $user->getID() ) {
262 # Use canonical name
263 $this->BlockAddress = $user->getName();
264 $userId = $user->getID();
265 } else {
266 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
267 return;
268 }
269 } else {
270 $this->showForm( wfMsg( 'badipaddress' ) );
271 return;
272 }
273 }
274 }
275
276 $expirestr = $this->BlockExpiry;
277 if( $expirestr == 'other' )
278 $expirestr = $this->BlockOther;
279
280 if (strlen($expirestr) == 0) {
281 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
282 return;
283 }
284
285 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
286 $expiry = Block::infinity();
287 } else {
288 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
289 $expiry = strtotime( $expirestr );
290
291 if ( $expiry < 0 || $expiry === false ) {
292 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
293 return;
294 }
295
296 $expiry = wfTimestamp( TS_MW, $expiry );
297 }
298
299 # Create block
300 # Note: for a user block, ipb_address is only for display purposes
301
302 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
303 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
304 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName);
305
306 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
307
308 if ( !$block->insert() ) {
309 $this->showForm( wfMsg( 'ipb_already_blocked',
310 htmlspecialchars( $this->BlockAddress ) ) );
311 return;
312 }
313
314 wfRunHooks('BlockIpComplete', array($block, $wgUser));
315
316 # Prepare log parameters
317 $logParams = array();
318 $logParams[] = $expirestr;
319 $logParams[] = $this->blockLogFlags();
320
321 # Make log entry, if the name is hidden, put it in the oversight log
322 $log_type = ($this->BlockHideName) ? 'oversight' : 'block';
323 $log = new LogPage( $log_type );
324 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
325 $this->BlockReason, $logParams );
326
327 # Report to the user
328 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
329 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
330 urlencode( $this->BlockAddress ) ) );
331 }
332 }
333
334 function showSuccess() {
335 global $wgOut;
336
337 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
338 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
339 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
340 $wgOut->addWikiText( $text );
341 }
342
343 function showLogFragment( $out, $title ) {
344 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
345 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
346 $viewer = new LogViewer( new LogReader( $request ) );
347 $viewer->showList( $out );
348 }
349
350 /**
351 * Return a comma-delimited list of "flags" to be passed to the log
352 * reader for this block, to provide more information in the logs
353 *
354 * @return array
355 */
356 private function blockLogFlags() {
357 $flags = array();
358 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
359 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
360 $flags[] = 'anononly';
361 if( $this->BlockCreateAccount )
362 $flags[] = 'nocreate';
363 if( !$this->BlockEnableAutoblock )
364 $flags[] = 'noautoblock';
365 return implode( ',', $flags );
366 }
367
368 /**
369 * Builds unblock and block list links
370 *
371 * @return string
372 */
373 private function getConvenienceLinks() {
374 global $wgUser;
375 $skin = $wgUser->getSkin();
376 $links[] = $this->getUnblockLink( $skin );
377 $links[] = $this->getBlockListLink( $skin );
378 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
379 }
380
381 /**
382 * Build a convenient link to unblock the given username or IP
383 * address, if available; otherwise link to a blank unblock
384 * form
385 *
386 * @param $skin Skin to use
387 * @return string
388 */
389 private function getUnblockLink( $skin ) {
390 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
391 if( $this->BlockAddress ) {
392 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
393 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
394 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
395 } else {
396 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
397 }
398 }
399
400 /**
401 * Build a convenience link to the block list
402 *
403 * @param $skin Skin to use
404 * @return string
405 */
406 private function getBlockListLink( $skin ) {
407 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
408 if( $this->BlockAddress ) {
409 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
410 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
411 'ip=' . urlencode( $this->BlockAddress ) );
412 } else {
413 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
414 }
415 }
416 }
417 ?>