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->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
47 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
48 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
49 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly' );
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 if ( $wgRequest->wasPosted() ) {
54 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', false );
56 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', true );
60 function showForm( $err ) {
61 global $wgOut, $wgUser, $wgSysopUserBans;
63 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
64 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
66 if($wgSysopUserBans) {
67 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
69 $mIpaddress = wfMsgHtml( 'ipaddress' );
71 $mIpbexpiry = wfMsgHtml( 'ipbexpiry' );
72 $mIpbother = wfMsgHtml( 'ipbother' );
73 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
74 $mIpbreason = wfMsgHtml( 'ipbreason' );
75 $mIpbsubmit = wfMsgHtml( 'ipbsubmit' );
76 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Blockip' );
77 $action = $titleObj->escapeLocalURL( "action=submit" );
80 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
81 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
84 $scBlockAddress = htmlspecialchars( $this->BlockAddress
);
85 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry
);
86 $scBlockReason = htmlspecialchars( $this->BlockReason
);
87 $scBlockOtherTime = htmlspecialchars( $this->BlockOther
);
88 $scBlockExpiryOptions = htmlspecialchars( wfMsgForContent( 'ipboptions' ) );
90 $showblockoptions = $scBlockExpiryOptions != '-';
91 if (!$showblockoptions)
92 $mIpbother = $mIpbexpiry;
94 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
95 foreach (explode(',', $scBlockExpiryOptions) as $option) {
96 if ( strpos($option, ":") === false ) $option = "$option:$option";
97 list($show, $value) = explode(":", $option);
98 $show = htmlspecialchars($show);
99 $value = htmlspecialchars($value);
101 if ($this->BlockExpiry
=== $value)
102 $selected = ' selected="selected"';
103 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
106 $token = htmlspecialchars( $wgUser->editToken() );
109 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
112 <td align=\"right\">{$mIpaddress}:</td>
114 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
118 if ($showblockoptions) {
120 <td align=\"right\">{$mIpbexpiry}:</td>
122 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
123 $blockExpiryFormOptions
130 <tr id='wpBlockOther'>
131 <td align=\"right\">{$mIpbother}:</td>
133 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
137 <td align=\"right\">{$mIpbreason}:</td>
139 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
145 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
146 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
) . "
152 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
153 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
) . "
157 <td style='padding-top: 1em'> </td>
158 <td style='padding-top: 1em' align=\"left\">
159 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
163 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
168 function doSubmit() {
169 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
172 $this->BlockAddress
= trim( $this->BlockAddress
);
173 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
175 # Check for invalid specifications
176 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
177 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
178 if ( $wgSysopRangeBans ) {
179 if ( $matches[2] > 31 ||
$matches[2] < 16 ) {
180 $this->showForm( wfMsg( 'ip_range_invalid' ) );
183 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
185 # Range block illegal
186 $this->showForm( wfMsg( 'range_block_disabled' ) );
191 if ( $wgSysopUserBans ) {
192 $user = User
::newFromName( $this->BlockAddress
);
193 if( !is_null( $user ) && $user->getID() ) {
195 $this->BlockAddress
= $user->getName();
196 $userId = $user->getID();
198 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
202 $this->showForm( wfMsg( 'badipaddress' ) );
208 $expirestr = $this->BlockExpiry
;
209 if( $expirestr == 'other' )
210 $expirestr = $this->BlockOther
;
212 if (strlen($expirestr) == 0) {
213 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
217 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
218 $expiry = Block
::infinity();
220 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
221 $expiry = strtotime( $expirestr );
223 if ( $expiry < 0 ||
$expiry === false ) {
224 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
228 $expiry = wfTimestamp( TS_MW
, $expiry );
232 # Note: for a user block, ipb_address is only for display purposes
234 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
235 $this->BlockReason
, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
236 $this->BlockCreateAccount
);
238 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
240 if ( !$block->insert() ) {
241 $this->showForm( wfMsg( 'ipb_already_blocked',
242 htmlspecialchars( $this->BlockAddress
) ) );
246 wfRunHooks('BlockIpComplete', array($block, $wgUser));
249 $log = new LogPage( 'block' );
250 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
251 $this->BlockReason
, $expirestr );
254 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Blockip' );
255 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
256 urlencode( $this->BlockAddress
) ) );
260 function showSuccess() {
263 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
264 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
265 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
266 $wgOut->addWikiText( $text );