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