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