API * Minor fixes to user contribs and logevents. Paging is still broken.
[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 if ( $wgRequest->wasPosted() ) {
60 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', false );
61 } else {
62 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', true );
63 }
64
65 }
66
67 function showForm( $err ) {
68 global $wgOut, $wgUser, $wgSysopUserBans;
69
70 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
71 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
72
73 if($wgSysopUserBans) {
74 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
75 } else {
76 $mIpaddress = wfMsgHtml( 'ipaddress' );
77 }
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" );
85
86 if ( "" != $err ) {
87 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
88 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
89 }
90
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' ) );
96
97 $showblockoptions = $scBlockExpiryOptions != '-';
98 if (!$showblockoptions)
99 $mIpbother = $mIpbexpiry;
100
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);
107 $selected = "";
108 if ($this->BlockExpiry === $value)
109 $selected = ' selected="selected"';
110 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
111 }
112
113 $token = htmlspecialchars( $wgUser->editToken() );
114
115 $wgOut->addHTML( "
116 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
117 <table border='0'>
118 <tr>
119 <td align=\"right\">{$mIpaddress}:</td>
120 <td align=\"left\">
121 <input tabindex='1' type='text' size='40' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
122 </td>
123 </tr>
124 <tr>");
125 if ($showblockoptions) {
126 $wgOut->addHTML("
127 <td align=\"right\">{$mIpbexpiry}:</td>
128 <td align=\"left\">
129 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
130 $blockExpiryFormOptions
131 </select>
132 </td>
133 ");
134 }
135 $wgOut->addHTML("
136 </tr>
137 <tr id='wpBlockOther'>
138 <td align=\"right\">{$mIpbother}:</td>
139 <td align=\"left\">
140 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
141 </td>
142 </tr>
143 <tr>
144 <td align=\"right\">{$mIpbreason}:</td>
145 <td align=\"left\">
146 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
147 </td>
148 </tr>
149 <tr>
150 <td>&nbsp;</td>
151 <td align=\"left\">
152 " . wfCheckLabel( wfMsg( 'ipbanononly' ),
153 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
154 array( 'tabindex' => 4 ) ) . "
155 </td>
156 </tr>
157 <tr>
158 <td>&nbsp;</td>
159 <td align=\"left\">
160 " . wfCheckLabel( wfMsg( 'ipbcreateaccount' ),
161 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
162 array( 'tabindex' => 5 ) ) . "
163 </td>
164 </tr>
165 <tr>
166 <td>&nbsp;</td>
167 <td align=\"left\">
168 " . wfCheckLabel( wfMsg( 'ipbenableautoblock' ),
169 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
170 array( 'tabindex' => 6 ) ) . "
171 </td>
172 </tr>
173 <tr>
174 <td style='padding-top: 1em'>&nbsp;</td>
175 <td style='padding-top: 1em' align=\"left\">
176 <input tabindex='7' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
177 </td>
178 </tr>
179 </table>
180 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
181 </form>\n" );
182
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 ) );
188 }
189
190 }
191
192 function doSubmit() {
193 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
194
195 $userId = 0;
196 $this->BlockAddress = trim( $this->BlockAddress );
197 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
198
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' ) );
205 return;
206 }
207 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
208 } else {
209 # Range block illegal
210 $this->showForm( wfMsg( 'range_block_disabled' ) );
211 return;
212 }
213 } else {
214 # Username block
215 if ( $wgSysopUserBans ) {
216 $user = User::newFromName( $this->BlockAddress );
217 if( !is_null( $user ) && $user->getID() ) {
218 # Use canonical name
219 $this->BlockAddress = $user->getName();
220 $userId = $user->getID();
221 } else {
222 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
223 return;
224 }
225 } else {
226 $this->showForm( wfMsg( 'badipaddress' ) );
227 return;
228 }
229 }
230 }
231
232 $expirestr = $this->BlockExpiry;
233 if( $expirestr == 'other' )
234 $expirestr = $this->BlockOther;
235
236 if (strlen($expirestr) == 0) {
237 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
238 return;
239 }
240
241 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
242 $expiry = Block::infinity();
243 } else {
244 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
245 $expiry = strtotime( $expirestr );
246
247 if ( $expiry < 0 || $expiry === false ) {
248 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
249 return;
250 }
251
252 $expiry = wfTimestamp( TS_MW, $expiry );
253 }
254
255 # Create block
256 # Note: for a user block, ipb_address is only for display purposes
257
258 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
259 $this->BlockReason, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
260 $this->BlockCreateAccount, $this->BlockEnableAutoblock );
261
262 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
263
264 if ( !$block->insert() ) {
265 $this->showForm( wfMsg( 'ipb_already_blocked',
266 htmlspecialchars( $this->BlockAddress ) ) );
267 return;
268 }
269
270 wfRunHooks('BlockIpComplete', array($block, $wgUser));
271
272 # Make log entry
273 $log = new LogPage( 'block' );
274 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
275 $this->BlockReason, $expirestr );
276
277 # Report to the user
278 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
279 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
280 urlencode( $this->BlockAddress ) ) );
281 }
282 }
283
284 function showSuccess() {
285 global $wgOut;
286
287 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
288 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
289 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
290 $wgOut->addWikiText( $text );
291 }
292
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 );
298 }
299
300 }
301
302 ?>