doc fix
[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->permissionRequired( 'block' );
17 return;
18 }
19
20 $ipb = new IPBlockForm( $par );
21
22 $action = $wgRequest->getVal( 'action' );
23 if ( 'success' == $action ) {
24 $ipb->showSuccess();
25 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
26 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
27 $ipb->doSubmit();
28 } else {
29 $ipb->showForm( '' );
30 }
31 }
32
33 /**
34 * Form object
35 *
36 * @package MediaWiki
37 * @subpackage SpecialPage
38 */
39 class IPBlockForm {
40 var $BlockAddress, $BlockExpiry, $BlockReason;
41
42 function IPBlockForm( $par ) {
43 global $wgRequest;
44
45 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
46 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
47 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
48 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
49 }
50
51 function showForm( $err ) {
52 global $wgOut, $wgUser, $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, $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 == 'other' )
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, on error returns -1 for PHP <5.1 and false for PHP >=5.1
194 $expiry = strtotime( $expirestr );
195
196 if ( $expiry < 0 || $expiry === false ) {
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;
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 ?>