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 );
59 if ( $wgRequest->wasPosted() ) {
60 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', false );
62 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', true );
67 function showForm( $err ) {
68 global $wgOut, $wgUser, $wgSysopUserBans;
70 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
71 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
73 if($wgSysopUserBans) {
74 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
76 $mIpaddress = wfMsgHtml( 'ipaddress' );
78 $mIpbexpiry = wfMsgHtml( 'ipbexpiry' );
79 $mIpbother = wfMsgHtml( 'ipbother' );
80 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
81 $mIpbreason = wfMsgHtml( 'ipbreason' );
82 $mIpbsubmit = wfMsgHtml( 'ipbsubmit' );
83 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
84 $action = $titleObj->escapeLocalURL( "action=submit" );
87 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
88 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
91 $scBlockAddress = htmlspecialchars( $this->BlockAddress
);
92 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry
);
93 $scBlockReason = htmlspecialchars( $this->BlockReason
);
94 $scBlockOtherTime = htmlspecialchars( $this->BlockOther
);
95 $scBlockExpiryOptions = htmlspecialchars( wfMsgForContent( 'ipboptions' ) );
97 $showblockoptions = $scBlockExpiryOptions != '-';
98 if (!$showblockoptions)
99 $mIpbother = $mIpbexpiry;
101 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
102 foreach (explode(',', $scBlockExpiryOptions) as $option) {
103 if ( strpos($option, ":") === false ) $option = "$option:$option";
104 list($show, $value) = explode(":", $option);
105 $show = htmlspecialchars($show);
106 $value = htmlspecialchars($value);
108 if ($this->BlockExpiry
=== $value)
109 $selected = ' selected="selected"';
110 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
113 $token = htmlspecialchars( $wgUser->editToken() );
116 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
119 <td align=\"right\">{$mIpaddress}:</td>
121 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
125 if ($showblockoptions) {
127 <td align=\"right\">{$mIpbexpiry}:</td>
129 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
130 $blockExpiryFormOptions
137 <tr id='wpBlockOther'>
138 <td align=\"right\">{$mIpbother}:</td>
140 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
144 <td align=\"right\">{$mIpbreason}:</td>
146 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
152 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
153 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
154 array( 'tabindex' => 4 ) ) . "
160 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
161 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
162 array( 'tabindex' => 5 ) ) . "
168 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
169 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
170 array( 'tabindex' => 6 ) ) . "
174 <td style='padding-top: 1em'> </td>
175 <td style='padding-top: 1em' align=\"left\">
176 <input tabindex='7' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
180 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
183 $user = User
::newFromName( $this->BlockAddress
);
184 if( is_object( $user ) ) {
185 $this->showLogFragment( $wgOut, $user->getUserPage() );
186 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
187 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
192 function doSubmit() {
193 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
196 $this->BlockAddress
= trim( $this->BlockAddress
);
197 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
199 # Check for invalid specifications
200 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
201 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
202 if ( $wgSysopRangeBans ) {
203 if ( $matches[2] > 31 ||
$matches[2] < 16 ) {
204 $this->showForm( wfMsg( 'ip_range_invalid' ) );
207 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
209 # Range block illegal
210 $this->showForm( wfMsg( 'range_block_disabled' ) );
215 if ( $wgSysopUserBans ) {
216 $user = User
::newFromName( $this->BlockAddress
);
217 if( !is_null( $user ) && $user->getID() ) {
219 $this->BlockAddress
= $user->getName();
220 $userId = $user->getID();
222 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
226 $this->showForm( wfMsg( 'badipaddress' ) );
232 $expirestr = $this->BlockExpiry
;
233 if( $expirestr == 'other' )
234 $expirestr = $this->BlockOther
;
236 if (strlen($expirestr) == 0) {
237 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
241 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
242 $expiry = Block
::infinity();
244 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
245 $expiry = strtotime( $expirestr );
247 if ( $expiry < 0 ||
$expiry === false ) {
248 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
252 $expiry = wfTimestamp( TS_MW
, $expiry );
256 # Note: for a user block, ipb_address is only for display purposes
258 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
259 $this->BlockReason
, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
260 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
);
262 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
264 if ( !$block->insert() ) {
265 $this->showForm( wfMsg( 'ipb_already_blocked',
266 htmlspecialchars( $this->BlockAddress
) ) );
270 wfRunHooks('BlockIpComplete', array($block, $wgUser));
273 $log = new LogPage( 'block' );
274 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
275 $this->BlockReason
, $expirestr );
278 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
279 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
280 urlencode( $this->BlockAddress
) ) );
284 function showSuccess() {
287 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
288 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
289 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
290 $wgOut->addWikiText( $text );
293 function showLogFragment( &$out, &$title ) {
294 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
295 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
296 $viewer = new LogViewer( new LogReader( $request ) );
297 $viewer->showList( $out );