Some more PHP5 stuff
[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 private
41 $BlockAddress,
42 $BlockExpiry,
43 $BlockReason;
44
45 function IPBlockForm( $par ) {
46 global $wgRequest;
47
48 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
49 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
50 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
51 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
52 }
53
54 function showForm( $err ) {
55 global $wgOut, $wgUser, $wgSysopUserBans;
56
57 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
58 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
59
60 if($wgSysopUserBans) {
61 $mIpaddress = wfMsgHtml( 'ipadressorusername' );
62 } else {
63 $mIpaddress = wfMsgHtml( 'ipaddress' );
64 }
65 $mIpbexpiry = wfMsgHtml( 'ipbexpiry' );
66 $mIpbother = wfMsgHtml( 'ipbother' );
67 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
68 $mIpbreason = wfMsgHtml( 'ipbreason' );
69 $mIpbsubmit = wfMsgHtml( 'ipbsubmit' );
70 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
71 $action = $titleObj->escapeLocalURL( "action=submit" );
72
73 if ( "" != $err ) {
74 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
75 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
76 }
77
78 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
79 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
80 $scBlockReason = htmlspecialchars( $this->BlockReason );
81 $scBlockOtherTime = htmlspecialchars( $this->BlockOther );
82 $scBlockExpiryOptions = htmlspecialchars( wfMsgForContent( 'ipboptions' ) );
83
84 $showblockoptions = $scBlockExpiryOptions != '-';
85 if (!$showblockoptions)
86 $mIpbother = $mIpbexpiry;
87
88 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
89 foreach (explode(',', $scBlockExpiryOptions) as $option) {
90 if ( strpos($option, ":") === false ) $option = "$option:$option";
91 list($show, $value) = explode(":", $option);
92 $show = htmlspecialchars($show);
93 $value = htmlspecialchars($value);
94 $selected = "";
95 if ($this->BlockExpiry === $value)
96 $selected = ' selected="selected"';
97 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
98 }
99
100 $token = htmlspecialchars( $wgUser->editToken() );
101
102 $wgOut->addHTML( "
103 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
104 <table border='0'>
105 <tr>
106 <td align=\"right\">{$mIpaddress}:</td>
107 <td align=\"left\">
108 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
109 </td>
110 </tr>
111 <tr>");
112 if ($showblockoptions) {
113 $wgOut->addHTML("
114 <td align=\"right\">{$mIpbexpiry}:</td>
115 <td align=\"left\">
116 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
117 $blockExpiryFormOptions
118 </select>
119 </td>
120 ");
121 }
122 $wgOut->addHTML("
123 </tr>
124 <tr id='wpBlockOther'>
125 <td align=\"right\">{$mIpbother}:</td>
126 <td align=\"left\">
127 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
128 </td>
129 </tr>
130 <tr>
131 <td align=\"right\">{$mIpbreason}:</td>
132 <td align=\"left\">
133 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
134 </td>
135 </tr>
136 <tr>
137 <td>&nbsp;</td>
138 <td align=\"left\">
139 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
140 </td>
141 </tr>
142 </table>
143 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
144 </form>\n" );
145
146 }
147
148 function doSubmit() {
149 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
150
151 $userId = 0;
152 $this->BlockAddress = trim( $this->BlockAddress );
153 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
154
155 # Check for invalid specifications
156 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
157 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
158 if ( $wgSysopRangeBans ) {
159 if ( $matches[2] > 31 || $matches[2] < 16 ) {
160 $this->showForm( wfMsg( 'ip_range_invalid' ) );
161 return;
162 }
163 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
164 } else {
165 # Range block illegal
166 $this->showForm( wfMsg( 'range_block_disabled' ) );
167 return;
168 }
169 } else {
170 # Username block
171 if ( $wgSysopUserBans ) {
172 $userId = User::idFromName( $this->BlockAddress );
173 if ( $userId == 0 ) {
174 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
175 return;
176 }
177 } else {
178 $this->showForm( wfMsg( 'badipaddress' ) );
179 return;
180 }
181 }
182 }
183
184 $expirestr = $this->BlockExpiry;
185 if( $expirestr == 'other' )
186 $expirestr = $this->BlockOther;
187
188 if (strlen($expirestr) == 0) {
189 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
190 return;
191 }
192
193 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
194 $expiry = '';
195 } else {
196 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
197 $expiry = strtotime( $expirestr );
198
199 if ( $expiry < 0 || $expiry === false ) {
200 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
201 return;
202 }
203
204 $expiry = wfTimestamp( TS_MW, $expiry );
205
206 }
207
208 # Create block
209 # Note: for a user block, ipb_address is only for display purposes
210
211 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
212 $this->BlockReason, wfTimestampNow(), 0, $expiry );
213
214 if (wfRunHooks('BlockIp', array(&$ban, &$wgUser))) {
215
216 $ban->insert();
217
218 wfRunHooks('BlockIpComplete', array($ban, $wgUser));
219
220 # Make log entry
221 $log = new LogPage( 'block' );
222 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
223 $this->BlockReason, $expirestr );
224
225 # Report to the user
226 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
227 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
228 urlencode( $this->BlockAddress ) ) );
229 }
230 }
231
232 function showSuccess() {
233 global $wgOut;
234
235 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
236 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
237 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
238 $wgOut->addWikiText( $text );
239 }
240 }
241
242 ?>