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
,
147 array( 'tabindex' => 4 ) ) . "
153 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
154 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
155 array( 'tabindex' => 5 ) ) . "
159 <td style='padding-top: 1em'> </td>
160 <td style='padding-top: 1em' align=\"left\">
161 <input tabindex='5' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
165 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
168 $user = User
::newFromName( $this->BlockAddress
);
169 if( is_object( $user ) ) {
170 $this->showLogFragment( $wgOut, $user->getUserPage() );
171 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
172 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
177 function doSubmit() {
178 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
181 $this->BlockAddress
= trim( $this->BlockAddress
);
182 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
184 # Check for invalid specifications
185 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
186 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
187 if ( $wgSysopRangeBans ) {
188 if ( $matches[2] > 31 ||
$matches[2] < 16 ) {
189 $this->showForm( wfMsg( 'ip_range_invalid' ) );
192 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
194 # Range block illegal
195 $this->showForm( wfMsg( 'range_block_disabled' ) );
200 if ( $wgSysopUserBans ) {
201 $user = User
::newFromName( $this->BlockAddress
);
202 if( !is_null( $user ) && $user->getID() ) {
204 $this->BlockAddress
= $user->getName();
205 $userId = $user->getID();
207 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
211 $this->showForm( wfMsg( 'badipaddress' ) );
217 $expirestr = $this->BlockExpiry
;
218 if( $expirestr == 'other' )
219 $expirestr = $this->BlockOther
;
221 if (strlen($expirestr) == 0) {
222 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
226 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
227 $expiry = Block
::infinity();
229 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
230 $expiry = strtotime( $expirestr );
232 if ( $expiry < 0 ||
$expiry === false ) {
233 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
237 $expiry = wfTimestamp( TS_MW
, $expiry );
241 # Note: for a user block, ipb_address is only for display purposes
243 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
244 $this->BlockReason
, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
245 $this->BlockCreateAccount
);
247 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
249 if ( !$block->insert() ) {
250 $this->showForm( wfMsg( 'ipb_already_blocked',
251 htmlspecialchars( $this->BlockAddress
) ) );
255 wfRunHooks('BlockIpComplete', array($block, $wgUser));
258 $log = new LogPage( 'block' );
259 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
260 $this->BlockReason
, $expirestr );
263 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Blockip' );
264 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
265 urlencode( $this->BlockAddress
) ) );
269 function showSuccess() {
272 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
273 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
274 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
275 $wgOut->addWikiText( $text );
278 function showLogFragment( &$out, &$title ) {
279 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
280 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
281 $viewer = new LogViewer( new LogReader( $request ) );
282 $viewer->showList( $out );