Bug 7474, CACHE_DB/CACHE_MEMCACHED confusion
[mediawiki.git] / includes / SpecialBlockip.php
blob4eb4957a55b351f468b4a0f240251fc5c273ca6c
1 <?php
2 /**
3 * Constructor for Special:Blockip page
5 * @package MediaWiki
6 * @subpackage SpecialPage
7 */
9 /**
10 * Constructor
12 function wfSpecialBlockip( $par ) {
13 global $wgUser, $wgOut, $wgRequest;
15 if( !$wgUser->isAllowed( 'block' ) ) {
16 $wgOut->permissionRequired( 'block' );
17 return;
20 $ipb = new IPBlockForm( $par );
22 $action = $wgRequest->getVal( 'action' );
23 if ( 'success' == $action ) {
24 $ipb->showSuccess();
25 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
26 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
27 $ipb->doSubmit();
28 } else {
29 $ipb->showForm( '' );
33 /**
34 * Form object
36 * @package MediaWiki
37 * @subpackage SpecialPage
39 class IPBlockForm {
40 var $BlockAddress, $BlockExpiry, $BlockReason;
42 function IPBlockForm( $par ) {
43 global $wgRequest;
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 );
55 } else {
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' );
68 } else {
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" );
79 if ( "" != $err ) {
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);
100 $selected = "";
101 if ($this->BlockExpiry === $value)
102 $selected = ' selected="selected"';
103 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
106 $token = htmlspecialchars( $wgUser->editToken() );
108 $wgOut->addHTML( "
109 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
110 <table border='0'>
111 <tr>
112 <td align=\"right\">{$mIpaddress}:</td>
113 <td align=\"left\">
114 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
115 </td>
116 </tr>
117 <tr>");
118 if ($showblockoptions) {
119 $wgOut->addHTML("
120 <td align=\"right\">{$mIpbexpiry}:</td>
121 <td align=\"left\">
122 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
123 $blockExpiryFormOptions
124 </select>
125 </td>
128 $wgOut->addHTML("
129 </tr>
130 <tr id='wpBlockOther'>
131 <td align=\"right\">{$mIpbother}:</td>
132 <td align=\"left\">
133 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
134 </td>
135 </tr>
136 <tr>
137 <td align=\"right\">{$mIpbreason}:</td>
138 <td align=\"left\">
139 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
140 </td>
141 </tr>
142 <tr>
143 <td>&nbsp;</td>
144 <td align=\"left\">
145 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
146 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
147 array( 'tabindex' => 4 ) ) . "
148 </td>
149 </tr>
150 <tr>
151 <td>&nbsp;</td>
152 <td align=\"left\">
153 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
154 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
155 array( 'tabindex' => 5 ) ) . "
156 </td>
157 </tr>
158 <tr>
159 <td style='padding-top: 1em'>&nbsp;</td>
160 <td style='padding-top: 1em' align=\"left\">
161 <input tabindex='5' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
162 </td>
163 </tr>
164 </table>
165 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
166 </form>\n" );
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;
180 $userId = 0;
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' ) );
190 return;
192 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
193 } else {
194 # Range block illegal
195 $this->showForm( wfMsg( 'range_block_disabled' ) );
196 return;
198 } else {
199 # Username block
200 if ( $wgSysopUserBans ) {
201 $user = User::newFromName( $this->BlockAddress );
202 if( !is_null( $user ) && $user->getID() ) {
203 # Use canonical name
204 $this->BlockAddress = $user->getName();
205 $userId = $user->getID();
206 } else {
207 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
208 return;
210 } else {
211 $this->showForm( wfMsg( 'badipaddress' ) );
212 return;
217 $expirestr = $this->BlockExpiry;
218 if( $expirestr == 'other' )
219 $expirestr = $this->BlockOther;
221 if (strlen($expirestr) == 0) {
222 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
223 return;
226 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
227 $expiry = Block::infinity();
228 } else {
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' ) );
234 return;
237 $expiry = wfTimestamp( TS_MW, $expiry );
240 # Create block
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 ) ) );
252 return;
255 wfRunHooks('BlockIpComplete', array($block, $wgUser));
257 # Make log entry
258 $log = new LogPage( 'block' );
259 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
260 $this->BlockReason, $expirestr );
262 # Report to the user
263 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
264 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
265 urlencode( $this->BlockAddress ) ) );
269 function showSuccess() {
270 global $wgOut;
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 );