Block form fixlets:
[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 = Title::makeTitle( NS_SPECIAL, '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 </td>
148 </tr>
149 <tr>
150 <td>&nbsp;</td>
151 <td align=\"left\">
152 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
153 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount ) . "
154 </td>
155 </tr>
156 <tr>
157 <td style='padding-top: 1em'>&nbsp;</td>
158 <td style='padding-top: 1em' align=\"left\">
159 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
160 </td>
161 </tr>
162 </table>
163 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
164 </form>\n" );
165
166 }
167
168 function doSubmit() {
169 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
170
171 $userId = 0;
172 $this->BlockAddress = trim( $this->BlockAddress );
173 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
174
175 # Check for invalid specifications
176 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
177 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
178 if ( $wgSysopRangeBans ) {
179 if ( $matches[2] > 31 || $matches[2] < 16 ) {
180 $this->showForm( wfMsg( 'ip_range_invalid' ) );
181 return;
182 }
183 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
184 } else {
185 # Range block illegal
186 $this->showForm( wfMsg( 'range_block_disabled' ) );
187 return;
188 }
189 } else {
190 # Username block
191 if ( $wgSysopUserBans ) {
192 $user = User::newFromName( $this->BlockAddress );
193 if( !is_null( $user ) && $user->getID() ) {
194 # Use canonical name
195 $this->BlockAddress = $user->getName();
196 } else {
197 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
198 return;
199 }
200 } else {
201 $this->showForm( wfMsg( 'badipaddress' ) );
202 return;
203 }
204 }
205 }
206
207 $expirestr = $this->BlockExpiry;
208 if( $expirestr == 'other' )
209 $expirestr = $this->BlockOther;
210
211 if (strlen($expirestr) == 0) {
212 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
213 return;
214 }
215
216 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
217 $expiry = Block::infinity();
218 } else {
219 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
220 $expiry = strtotime( $expirestr );
221
222 if ( $expiry < 0 || $expiry === false ) {
223 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
224 return;
225 }
226
227 $expiry = wfTimestamp( TS_MW, $expiry );
228 }
229
230 # Create block
231 # Note: for a user block, ipb_address is only for display purposes
232
233 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
234 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
235 $this->BlockCreateAccount );
236
237 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
238
239 if ( !$block->insert() ) {
240 $this->showForm( wfMsg( 'ipb_already_blocked',
241 htmlspecialchars( $this->BlockAddress ) ) );
242 return;
243 }
244
245 wfRunHooks('BlockIpComplete', array($block, $wgUser));
246
247 # Make log entry
248 $log = new LogPage( 'block' );
249 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
250 $this->BlockReason, $expirestr );
251
252 # Report to the user
253 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
254 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
255 urlencode( $this->BlockAddress ) ) );
256 }
257 }
258
259 function showSuccess() {
260 global $wgOut;
261
262 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
263 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
264 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
265 $wgOut->addWikiText( $text );
266 }
267 }
268
269 ?>