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