3 * Constructor for Special:Blockip page
6 * @subpackage SpecialPage
12 function wfSpecialBlockip( $par ) {
13 global $wgUser, $wgOut, $wgRequest;
15 if( !$wgUser->isAllowed( 'block' ) ) {
16 $wgOut->permissionRequired( 'block' );
20 $ipb = new IPBlockForm( $par );
22 $action = $wgRequest->getVal( 'action' );
23 if ( 'success' == $action ) {
25 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
26 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
37 * @subpackage SpecialPage
40 var $BlockAddress, $BlockExpiry, $BlockReason;
42 function IPBlockForm( $par ) {
45 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
46 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
47 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
48 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
49 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
51 # Unchecked checkboxes are not included in the form data at all, so having one
52 # that is true by default is a bit tricky
53 $byDefault = !$wgRequest->wasPosted();
54 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
55 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
56 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
59 function showForm( $err ) {
60 global $wgOut, $wgUser, $wgSysopUserBans;
62 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
63 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
65 if($wgSysopUserBans) {
66 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
68 $mIpaddress = Xml
::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
70 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
71 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
72 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
73 $mIpbreason = Xml
::label( wfMsg( 'ipbreason' ), 'mw-bi-reason' );
74 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
75 $action = $titleObj->escapeLocalURL( "action=submit" );
78 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
79 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
82 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
84 $showblockoptions = $scBlockExpiryOptions != '-';
85 if (!$showblockoptions)
86 $mIpbother = $mIpbexpiry;
88 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
89 foreach (explode(',', $scBlockExpiryOptions) as $option) {
90 if ( strpos($option, ":") === false ) $option = "$option:$option";
91 list($show, $value) = explode(":", $option);
92 $show = htmlspecialchars($show);
93 $value = htmlspecialchars($value);
95 if ($this->BlockExpiry
=== $value)
96 $selected = ' selected="selected"';
97 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
100 $token = htmlspecialchars( $wgUser->editToken() );
103 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
106 <td align=\"right\">{$mIpaddress}:</td>
108 " . Xml
::input( 'wpBlockAddress', 40, $this->BlockAddress
,
109 array( 'tabindex' => '1', 'id' => 'mw-bi-target' ) ) . "
113 if ($showblockoptions) {
115 <td align=\"right\">{$mIpbexpiry}:</td>
117 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
118 $blockExpiryFormOptions
125 <tr id='wpBlockOther'>
126 <td align=\"right\">{$mIpbother}:</td>
128 " . Xml
::input( 'wpBlockOther', 40, $this->BlockOther
,
129 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
133 <td align=\"right\">{$mIpbreason}:</td>
135 " . Xml
::input( 'wpBlockReason', 40, $this->BlockReason
,
136 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
142 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
143 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
144 array( 'tabindex' => 4 ) ) . "
150 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
151 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
152 array( 'tabindex' => 5 ) ) . "
158 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
159 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
160 array( 'tabindex' => 6 ) ) . "
164 <td style='padding-top: 1em'> </td>
165 <td style='padding-top: 1em' align=\"left\">
166 " . Xml
::submitButton( wfMsg( 'ipbsubmit' ),
167 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
171 Xml
::hidden( 'wpEditToken', $token ) .
174 $wgOut->addHtml( $this->getConvenienceLinks() );
176 $user = User
::newFromName( $this->BlockAddress
);
177 if( is_object( $user ) ) {
178 $this->showLogFragment( $wgOut, $user->getUserPage() );
179 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
180 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
184 function doSubmit() {
185 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
188 $this->BlockAddress
= trim( $this->BlockAddress
);
189 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
191 # Check for invalid specifications
192 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
194 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
195 if ( $wgSysopRangeBans ) {
196 if ( $matches[2] > 31 ||
$matches[2] < 16 ) {
197 $this->showForm( wfMsg( 'ip_range_invalid' ) );
200 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
202 # Range block illegal
203 $this->showForm( wfMsg( 'range_block_disabled' ) );
208 if ( $wgSysopUserBans ) {
209 $user = User
::newFromName( $this->BlockAddress
);
210 if( !is_null( $user ) && $user->getID() ) {
212 $this->BlockAddress
= $user->getName();
213 $userId = $user->getID();
215 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
219 $this->showForm( wfMsg( 'badipaddress' ) );
225 $expirestr = $this->BlockExpiry
;
226 if( $expirestr == 'other' )
227 $expirestr = $this->BlockOther
;
229 if (strlen($expirestr) == 0) {
230 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
234 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
235 $expiry = Block
::infinity();
237 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
238 $expiry = strtotime( $expirestr );
240 if ( $expiry < 0 ||
$expiry === false ) {
241 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
245 $expiry = wfTimestamp( TS_MW
, $expiry );
249 # Note: for a user block, ipb_address is only for display purposes
251 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
252 $this->BlockReason
, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
253 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
);
255 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
257 if ( !$block->insert() ) {
258 $this->showForm( wfMsg( 'ipb_already_blocked',
259 htmlspecialchars( $this->BlockAddress
) ) );
263 wfRunHooks('BlockIpComplete', array($block, $wgUser));
265 # Prepare log parameters
266 $logParams = array();
267 $logParams[] = $expirestr;
268 $logParams[] = $this->blockLogFlags();
271 $log = new LogPage( 'block' );
272 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
273 $this->BlockReason
, $logParams );
276 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
277 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
278 urlencode( $this->BlockAddress
) ) );
282 function showSuccess() {
285 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
286 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
287 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
288 $wgOut->addWikiText( $text );
291 function showLogFragment( $out, $title ) {
292 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
293 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
294 $viewer = new LogViewer( new LogReader( $request ) );
295 $viewer->showList( $out );
299 * Return a comma-delimited list of "flags" to be passed to the log
300 * reader for this block, to provide more information in the logs
304 private function blockLogFlags() {
306 if( $this->BlockAnonOnly
)
307 $flags[] = 'anononly';
308 if( $this->BlockCreateAccount
)
309 $flags[] = 'nocreate';
310 if( $this->BlockEnableAutoblock
)
311 $flags[] = 'autoblock';
312 return implode( ',', $flags );
316 * Builds unblock and block list links
320 private function getConvenienceLinks() {
322 $skin = $wgUser->getSkin();
323 $links[] = $this->getUnblockLink( $skin );
324 $links[] = $this->getBlockListLink( $skin );
325 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
329 * Build a convenient link to unblock the given username or IP
330 * address, if available; otherwise link to a blank unblock
333 * @param $skin Skin to use
336 private function getUnblockLink( $skin ) {
337 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
338 if( $this->BlockAddress
) {
339 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
340 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
341 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
343 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
348 * Build a convenience link to the block list
350 * @param $skin Skin to use
353 private function getBlockListLink( $skin ) {
354 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
355 if( $this->BlockAddress
) {
356 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
357 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
358 'ip=' . urlencode( $this->BlockAddress
) );
360 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );