c7f25b964b95ab3fa6fad3c696d7597cca6a6213
[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 $mIpbanononly = wfMsgHtml( 'ipbanononly' );
77 $mIpbcreateaccount = wfMsgHtml( 'ipbcreateaccount' );
78 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
79 $action = $titleObj->escapeLocalURL( "action=submit" );
80
81 if ( "" != $err ) {
82 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
83 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
84 }
85
86 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
87 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
88 $scBlockReason = htmlspecialchars( $this->BlockReason );
89 $scBlockOtherTime = htmlspecialchars( $this->BlockOther );
90 $scBlockExpiryOptions = htmlspecialchars( wfMsgForContent( 'ipboptions' ) );
91 $anonOnlyChecked = $this->BlockAnonOnly ? 'checked' : '';
92 $createAccountChecked = $this->BlockCreateAccount ? 'checked' : '';
93
94 $showblockoptions = $scBlockExpiryOptions != '-';
95 if (!$showblockoptions)
96 $mIpbother = $mIpbexpiry;
97
98 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
99 foreach (explode(',', $scBlockExpiryOptions) as $option) {
100 if ( strpos($option, ":") === false ) $option = "$option:$option";
101 list($show, $value) = explode(":", $option);
102 $show = htmlspecialchars($show);
103 $value = htmlspecialchars($value);
104 $selected = "";
105 if ($this->BlockExpiry === $value)
106 $selected = ' selected="selected"';
107 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
108 }
109
110 $token = htmlspecialchars( $wgUser->editToken() );
111
112 $wgOut->addHTML( "
113 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
114 <table border='0'>
115 <tr>
116 <td align=\"right\">{$mIpaddress}:</td>
117 <td align=\"left\">
118 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
119 </td>
120 </tr>
121 <tr>");
122 if ($showblockoptions) {
123 $wgOut->addHTML("
124 <td align=\"right\">{$mIpbexpiry}:</td>
125 <td align=\"left\">
126 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
127 $blockExpiryFormOptions
128 </select>
129 </td>
130 ");
131 }
132 $wgOut->addHTML("
133 </tr>
134 <tr id='wpBlockOther'>
135 <td align=\"right\">{$mIpbother}:</td>
136 <td align=\"left\">
137 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
138 </td>
139 </tr>
140 <tr>
141 <td align=\"right\">{$mIpbreason}:</td>
142 <td align=\"left\">
143 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
144 </td>
145 </tr>
146 <tr>
147 <td>&nbsp;</td>
148 <td align=\"left\">
149 <label>
150 <input type='checkbox' name='wpAnonOnly' value='1' $anonOnlyChecked />
151 {$mIpbanononly}
152 </label>
153 </td>
154 </tr>
155 <tr>
156 <td>&nbsp;</td>
157 <td align=\"left\">
158 <label>
159 <input type='checkbox' name='wpCreateAccount' value='1' $createAccountChecked />
160 {$mIpbcreateaccount}
161 </label>
162 </td>
163 </tr>
164 <tr>
165 <td style='padding-top: 1em'>&nbsp;</td>
166 <td style='padding-top: 1em' align=\"left\">
167 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
168 </td>
169 </tr>
170 </table>
171 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
172 </form>\n" );
173
174 }
175
176 function doSubmit() {
177 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
178
179 $userId = 0;
180 $this->BlockAddress = trim( $this->BlockAddress );
181 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
182
183 # Check for invalid specifications
184 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
185 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
186 if ( $wgSysopRangeBans ) {
187 if ( $matches[2] > 31 || $matches[2] < 16 ) {
188 $this->showForm( wfMsg( 'ip_range_invalid' ) );
189 return;
190 }
191 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
192 } else {
193 # Range block illegal
194 $this->showForm( wfMsg( 'range_block_disabled' ) );
195 return;
196 }
197 } else {
198 # Username block
199 if ( $wgSysopUserBans ) {
200 $user = User::newFromName( $this->BlockAddress );
201 if ( $user->getID() ) {
202 # Use canonical name
203 $this->BlockAddress = $user->getName();
204 } else {
205 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
206 return;
207 }
208 } else {
209 $this->showForm( wfMsg( 'badipaddress' ) );
210 return;
211 }
212 }
213 }
214
215 $expirestr = $this->BlockExpiry;
216 if( $expirestr == 'other' )
217 $expirestr = $this->BlockOther;
218
219 if (strlen($expirestr) == 0) {
220 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
221 return;
222 }
223
224 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
225 $expiry = Block::infinity();
226 } else {
227 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
228 $expiry = strtotime( $expirestr );
229
230 if ( $expiry < 0 || $expiry === false ) {
231 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
232 return;
233 }
234
235 $expiry = wfTimestamp( TS_MW, $expiry );
236 }
237
238 # Create block
239 # Note: for a user block, ipb_address is only for display purposes
240
241 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
242 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
243 $this->BlockCreateAccount );
244
245 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
246
247 if ( !$block->insert() ) {
248 $this->showForm( wfMsg( 'ipb_already_blocked',
249 htmlspecialchars( $this->BlockAddress ) ) );
250 return;
251 }
252
253 wfRunHooks('BlockIpComplete', array($block, $wgUser));
254
255 # Make log entry
256 $log = new LogPage( 'block' );
257 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
258 $this->BlockReason, $expirestr );
259
260 # Report to the user
261 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
262 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
263 urlencode( $this->BlockAddress ) ) );
264 }
265 }
266
267 function showSuccess() {
268 global $wgOut;
269
270 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
271 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
272 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
273 $wgOut->addWikiText( $text );
274 }
275 }
276
277 ?>