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