Revert patch that was scapped into production without database changes
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2 /**
3 * Constructor for Special:Blockip page
4 *
5 * @package MediaWiki
6 * @subpackage SpecialPage
7 */
8
9 /**
10 * Constructor
11 */
12 function wfSpecialBlockip( $par ) {
13 global $wgUser, $wgOut, $wgRequest;
14
15 if( !$wgUser->isAllowed( 'block' ) ) {
16 $wgOut->permissionRequired( 'block' );
17 return;
18 }
19
20 $ipb = new IPBlockForm( $par );
21
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( '' );
30 }
31 }
32
33 /**
34 * Form object
35 *
36 * @package MediaWiki
37 * @subpackage SpecialPage
38 */
39 class IPBlockForm {
40 var $BlockAddress, $BlockExpiry, $BlockReason;
41
42 function IPBlockForm( $par ) {
43 global $wgRequest;
44
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' );
50
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 );
57 }
58 }
59
60 function showForm( $err ) {
61 global $wgOut, $wgUser, $wgSysopUserBans;
62
63 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
64 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
65
66 if($wgSysopUserBans) {
67 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
68 } else {
69 $mIpaddress = wfMsgHtml( 'ipaddress' );
70 }
71 $mIpbexpiry = wfMsgHtml( 'ipbexpiry' );
72 $mIpbother = wfMsgHtml( 'ipbother' );
73 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
74 $mIpbreason = wfMsgHtml( 'ipbreason' );
75 $mIpbsubmit = wfMsgHtml( 'ipbsubmit' );
76 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
77 $action = $titleObj->escapeLocalURL( "action=submit" );
78
79 if ( "" != $err ) {
80 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
81 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
82 }
83
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' ) );
89
90 $showblockoptions = $scBlockExpiryOptions != '-';
91 if (!$showblockoptions)
92 $mIpbother = $mIpbexpiry;
93
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>";
104 }
105
106 $token = htmlspecialchars( $wgUser->editToken() );
107
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>
126 ");
127 }
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" );
167
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 ) );
173 }
174
175 }
176
177 function doSubmit() {
178 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
179
180 $userId = 0;
181 $this->BlockAddress = trim( $this->BlockAddress );
182 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
183
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;
191 }
192 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
193 } else {
194 # Range block illegal
195 $this->showForm( wfMsg( 'range_block_disabled' ) );
196 return;
197 }
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;
209 }
210 } else {
211 $this->showForm( wfMsg( 'badipaddress' ) );
212 return;
213 }
214 }
215 }
216
217 $expirestr = $this->BlockExpiry;
218 if( $expirestr == 'other' )
219 $expirestr = $this->BlockOther;
220
221 if (strlen($expirestr) == 0) {
222 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
223 return;
224 }
225
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 );
231
232 if ( $expiry < 0 || $expiry === false ) {
233 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
234 return;
235 }
236
237 $expiry = wfTimestamp( TS_MW, $expiry );
238 }
239
240 # Create block
241 # Note: for a user block, ipb_address is only for display purposes
242
243 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
244 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
245 $this->BlockCreateAccount );
246
247 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
248
249 if ( !$block->insert() ) {
250 $this->showForm( wfMsg( 'ipb_already_blocked',
251 htmlspecialchars( $this->BlockAddress ) ) );
252 return;
253 }
254
255 wfRunHooks('BlockIpComplete', array($block, $wgUser));
256
257 # Make log entry
258 $log = new LogPage( 'block' );
259 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
260 $this->BlockReason, $expirestr );
261
262 # Report to the user
263 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
264 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
265 urlencode( $this->BlockAddress ) ) );
266 }
267 }
268
269 function showSuccess() {
270 global $wgOut;
271
272 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
273 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
274 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
275 $wgOut->addWikiText( $text );
276 }
277
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 );
283 }
284
285 }
286
287 ?>