when a value is selected, hide the 'time' entry field
[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( wfMsgForContent( '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 if (strstr($option, ":")) $option = "$option:$option";
87 list($show, $value) = explode(":", $option);
88 $show = htmlspecialchars($show);
89 $value = htmlspecialchars($value);
90 $selected = "";
91 if ($this->BlockExpiry === $value)
92 $selected = ' selected="selected"';
93 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
94 }
95
96 $token = htmlspecialchars( $wgUser->editToken() );
97
98 $wgOut->addHTML( "
99 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
100 <table border='0'>
101 <tr>
102 <td align=\"right\">{$mIpaddress}:</td>
103 <td align=\"left\">
104 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
105 </td>
106 </tr>
107 <tr>");
108 if ($showblockoptions) {
109 $wgOut->addHTML("
110 <td align=\"right\">{$mIpbexpiry}:</td>
111 <td align=\"left\">
112 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
113 $blockExpiryFormOptions
114 </select>
115 </td>
116 ");
117 }
118 $wgOut->addHTML("
119 </tr>
120 <tr id='wpBlockOther'>
121 <td align=\"right\">{$mIpbother}:</td>
122 <td align=\"left\">
123 <input tabindex='3' type='text' size='40' name=\"wpBlockOther\" value=\"{$scBlockOtherTime}\" />
124 </td>
125 </tr>
126 <tr>
127 <td align=\"right\">{$mIpbreason}:</td>
128 <td align=\"left\">
129 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
130 </td>
131 </tr>
132 <tr>
133 <td>&nbsp;</td>
134 <td align=\"left\">
135 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
136 </td>
137 </tr>
138 </table>
139 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
140 </form>\n" );
141
142 }
143
144 function doSubmit() {
145 global $wgOut, $wgUser, $wgLang;
146 global $wgSysopUserBans, $wgSysopRangeBans;
147
148 $userId = 0;
149 $this->BlockAddress = trim( $this->BlockAddress );
150 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
151
152 # Check for invalid specifications
153 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
154 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
155 if ( $wgSysopRangeBans ) {
156 if ( $matches[2] > 31 || $matches[2] < 16 ) {
157 $this->showForm( wfMsg( 'ip_range_invalid' ) );
158 return;
159 }
160 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
161 } else {
162 # Range block illegal
163 $this->showForm( wfMsg( 'range_block_disabled' ) );
164 return;
165 }
166 } else {
167 # Username block
168 if ( $wgSysopUserBans ) {
169 $userId = User::idFromName( $this->BlockAddress );
170 if ( $userId == 0 ) {
171 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
172 return;
173 }
174 } else {
175 $this->showForm( wfMsg( 'badipaddress' ) );
176 return;
177 }
178 }
179 }
180
181 $expirestr = $this->BlockExpiry;
182 if ($expirestr == wfMsg('ipbotheroption'))
183 $expirestr = $this->BlockOther;
184
185 if (strlen($expirestr) == 0) {
186 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
187 return;
188 }
189
190 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
191 $expiry = '';
192 } else {
193 # Convert GNU-style date, returns -1 on error
194 $expiry = strtotime( $expirestr );
195
196 if ( $expiry < 0 ) {
197 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
198 return;
199 }
200
201 $expiry = wfTimestamp( TS_MW, $expiry );
202
203 }
204
205 # Create block
206 # Note: for a user block, ipb_address is only for display purposes
207
208 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
209 $this->BlockReason, wfTimestampNow(), 0, $expiry );
210
211 if (wfRunHooks('BlockIp', array(&$ban, &$wgUser))) {
212
213 $ban->insert();
214
215 wfRunHooks('BlockIpComplete', array($ban, $wgUser));
216
217 # Make log entry
218 $log = new LogPage( 'block' );
219 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
220 $this->BlockReason, $expirestr );
221
222 # Report to the user
223 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
224 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
225 urlencode( $this->BlockAddress ) ) );
226 }
227 }
228
229 function showSuccess() {
230 global $wgOut, $wgUser;
231
232 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
233 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
234 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
235 $wgOut->addWikiText( $text );
236 }
237 }
238
239 ?>