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