* (bug 8539 Enable PLURAL option for another message of Recentchanges
[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 global $wgStylePath, $wgStyleVersion;
108 $wgOut->addHTML( "
109 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
110 </script>
111 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
112 <table border='0'>
113 <tr>
114 <td align=\"right\">{$mIpaddress}:</td>
115 <td align=\"left\">
116 " . Xml::input( 'wpBlockAddress', 40, $this->BlockAddress,
117 array(
118 'tabindex' => '1',
119 'id' => 'mw-bi-target',
120 'onchange' => 'updateBlockOptions()' ) ) . "
121 </td>
122 </tr>
123 <tr>");
124 if ($showblockoptions) {
125 $wgOut->addHTML("
126 <td align=\"right\">{$mIpbexpiry}:</td>
127 <td align=\"left\">
128 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
129 $blockExpiryFormOptions
130 </select>
131 </td>
132 ");
133 }
134 $wgOut->addHTML("
135 </tr>
136 <tr id='wpBlockOther'>
137 <td align=\"right\">{$mIpbother}:</td>
138 <td align=\"left\">
139 " . Xml::input( 'wpBlockOther', 40, $this->BlockOther,
140 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
141 </td>
142 </tr>
143 <tr>
144 <td align=\"right\">{$mIpbreason}:</td>
145 <td align=\"left\">
146 " . Xml::input( 'wpBlockReason', 40, $this->BlockReason,
147 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
148 </td>
149 </tr>
150 <tr id='wpAnonOnlyRow'>
151 <td>&nbsp;</td>
152 <td align=\"left\">
153 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
154 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
155 array( 'tabindex' => 4 ) ) . "
156 </td>
157 </tr>
158 <tr id='wpCreateAccountRow'>
159 <td>&nbsp;</td>
160 <td align=\"left\">
161 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
162 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
163 array( 'tabindex' => 5 ) ) . "
164 </td>
165 </tr>
166 <tr id='wpEnableAutoblockRow'>
167 <td>&nbsp;</td>
168 <td align=\"left\">
169 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
170 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
171 array( 'tabindex' => 6 ) ) . "
172 </td>
173 </tr>
174 <tr>
175 <td style='padding-top: 1em'>&nbsp;</td>
176 <td style='padding-top: 1em' align=\"left\">
177 " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
178 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
179 </td>
180 </tr>
181 </table>" .
182 Xml::hidden( 'wpEditToken', $token ) .
183 "</form>
184 <script type=\"text/javascript\">updateBlockOptions()</script>
185 \n" );
186
187 $wgOut->addHtml( $this->getConvenienceLinks() );
188
189 $user = User::newFromName( $this->BlockAddress );
190 if( is_object( $user ) ) {
191 $this->showLogFragment( $wgOut, $user->getUserPage() );
192 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
193 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
194 }
195 }
196
197 function doSubmit() {
198 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
199
200 $userId = 0;
201 $this->BlockAddress = trim( $this->BlockAddress );
202 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
203
204 # Check for invalid specifications
205 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
206 $matches = array();
207 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
208 if ( $wgSysopRangeBans ) {
209 if ( $matches[2] > 31 || $matches[2] < 16 ) {
210 $this->showForm( wfMsg( 'ip_range_invalid' ) );
211 return;
212 }
213 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
214 } else {
215 # Range block illegal
216 $this->showForm( wfMsg( 'range_block_disabled' ) );
217 return;
218 }
219 } else {
220 # Username block
221 if ( $wgSysopUserBans ) {
222 $user = User::newFromName( $this->BlockAddress );
223 if( !is_null( $user ) && $user->getID() ) {
224 # Use canonical name
225 $this->BlockAddress = $user->getName();
226 $userId = $user->getID();
227 } else {
228 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
229 return;
230 }
231 } else {
232 $this->showForm( wfMsg( 'badipaddress' ) );
233 return;
234 }
235 }
236 }
237
238 $expirestr = $this->BlockExpiry;
239 if( $expirestr == 'other' )
240 $expirestr = $this->BlockOther;
241
242 if (strlen($expirestr) == 0) {
243 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
244 return;
245 }
246
247 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
248 $expiry = Block::infinity();
249 } else {
250 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
251 $expiry = strtotime( $expirestr );
252
253 if ( $expiry < 0 || $expiry === false ) {
254 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
255 return;
256 }
257
258 $expiry = wfTimestamp( TS_MW, $expiry );
259 }
260
261 # Create block
262 # Note: for a user block, ipb_address is only for display purposes
263
264 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
265 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
266 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
267
268 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
269
270 if ( !$block->insert() ) {
271 $this->showForm( wfMsg( 'ipb_already_blocked',
272 htmlspecialchars( $this->BlockAddress ) ) );
273 return;
274 }
275
276 wfRunHooks('BlockIpComplete', array($block, $wgUser));
277
278 # Prepare log parameters
279 $logParams = array();
280 $logParams[] = $expirestr;
281 $logParams[] = $this->blockLogFlags();
282
283 # Make log entry
284 $log = new LogPage( 'block' );
285 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
286 $this->BlockReason, $logParams );
287
288 # Report to the user
289 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
290 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
291 urlencode( $this->BlockAddress ) ) );
292 }
293 }
294
295 function showSuccess() {
296 global $wgOut;
297
298 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
299 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
300 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
301 $wgOut->addWikiText( $text );
302 }
303
304 function showLogFragment( $out, $title ) {
305 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
306 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
307 $viewer = new LogViewer( new LogReader( $request ) );
308 $viewer->showList( $out );
309 }
310
311 /**
312 * Return a comma-delimited list of "flags" to be passed to the log
313 * reader for this block, to provide more information in the logs
314 *
315 * @return array
316 */
317 private function blockLogFlags() {
318 $flags = array();
319 if( $this->BlockAnonOnly )
320 $flags[] = 'anononly';
321 if( $this->BlockCreateAccount )
322 $flags[] = 'nocreate';
323 if( !$this->BlockEnableAutoblock )
324 $flags[] = 'noautoblock';
325 return implode( ',', $flags );
326 }
327
328 /**
329 * Builds unblock and block list links
330 *
331 * @return string
332 */
333 private function getConvenienceLinks() {
334 global $wgUser;
335 $skin = $wgUser->getSkin();
336 $links[] = $this->getUnblockLink( $skin );
337 $links[] = $this->getBlockListLink( $skin );
338 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
339 }
340
341 /**
342 * Build a convenient link to unblock the given username or IP
343 * address, if available; otherwise link to a blank unblock
344 * form
345 *
346 * @param $skin Skin to use
347 * @return string
348 */
349 private function getUnblockLink( $skin ) {
350 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
351 if( $this->BlockAddress ) {
352 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
353 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
354 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
355 } else {
356 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
357 }
358 }
359
360 /**
361 * Build a convenience link to the block list
362 *
363 * @param $skin Skin to use
364 * @return string
365 */
366 private function getBlockListLink( $skin ) {
367 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
368 if( $this->BlockAddress ) {
369 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
370 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
371 'ip=' . urlencode( $this->BlockAddress ) );
372 } else {
373 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
374 }
375 }
376 }
377 ?>