Re-establishing validation feature (the beginnings)
[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 }
47
48 function showForm( $err ) {
49 global $wgOut, $wgUser, $wgLang, $wgBlockExpiryOptions;
50 global $wgRequest;
51
52 $wgOut->setPagetitle( htmlspecialchars( wfMsg( 'blockip' ) ) );
53 $wgOut->addWikiText( htmlspecialchars( wfMsg( 'blockiptext' ) ) );
54
55 $mIpaddress = htmlspecialchars( wfMsg( 'ipaddress' ) );
56 $mIpbexpiry = htmlspecialchars( wfMsg( 'ipbexpiry' ) );
57 $mIpbreason = htmlspecialchars( wfMsg( 'ipbreason' ) );
58 $mIpbsubmit = htmlspecialchars( wfMsg( 'ipbsubmit' ) );
59 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
60 $action = $titleObj->escapeLocalURL( "action=submit" );
61
62 if ( "" != $err ) {
63 $wgOut->setSubtitle( htmlspecialchars( wfMsg( 'formerror' ) ) );
64 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
65 }
66
67 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
68 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
69 $scBlockReason = htmlspecialchars( $this->BlockReason );
70
71 $blockExpiryFormOptions = join("</option><option>", split(",", $wgBlockExpiryOptions));
72 $blockExpiryFormOptions = "<option>" . $blockExpiryFormOptions . "</option>";
73
74 $token = htmlspecialchars( $wgUser->editToken() );
75
76 $wgOut->addHTML( "
77 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
78 <table border='0'>
79 <tr>
80 <td align=\"right\">{$mIpaddress}:</td>
81 <td align=\"left\">
82 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
83 </td>
84 </tr>
85 <tr>
86 <td align=\"right\">{$mIpbexpiry}:</td>
87 <td align=\"left\">
88 <select tabindex='2' name=\"wpBlockExpiry\"/>
89 $blockExpiryFormOptions
90 </select>
91 </td>
92 </tr>
93 <tr>
94 <td align=\"right\">{$mIpbreason}:</td>
95 <td align=\"left\">
96 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
97 </td>
98 </tr>
99 <tr>
100 <td>&nbsp;</td>
101 <td align=\"left\">
102 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
103 </td>
104 </tr>
105 </table>
106 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
107 </form>\n" );
108
109 }
110
111 function doSubmit() {
112 global $wgOut, $wgUser, $wgLang;
113 global $wgSysopUserBans, $wgSysopRangeBans;
114
115 $userId = 0;
116 $this->BlockAddress = trim( $this->BlockAddress );
117 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
118
119 # Check for invalid specifications
120 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
121 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
122 if ( $wgSysopRangeBans ) {
123 if ( $matches[2] > 31 || $matches[2] < 16 ) {
124 $this->showForm( wfMsg( 'ip_range_invalid' ) );
125 return;
126 }
127 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
128 } else {
129 # Range block illegal
130 $this->showForm( wfMsg( 'range_block_disabled' ) );
131 return;
132 }
133 } else {
134 # Username block
135 if ( $wgSysopUserBans ) {
136 $userId = User::idFromName( $this->BlockAddress );
137 if ( $userId == 0 ) {
138 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
139 return;
140 }
141 } else {
142 $this->showForm( wfMsg( 'badipaddress' ) );
143 return;
144 }
145 }
146 }
147
148 if ( $this->BlockExpiry == 'infinite' || $this->BlockExpiry == 'indefinite' ) {
149 $expiry = '';
150 } else {
151 # Convert GNU-style date, returns -1 on error
152 $expiry = strtotime( $this->BlockExpiry );
153
154 if ( $expiry < 0 ) {
155 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
156 return;
157 }
158
159 $expiry = wfTimestamp( TS_MW, $expiry );
160
161 }
162
163
164 if ( $this->BlockReason == '') {
165 $this->showForm( wfMsg( 'noblockreason' ) );
166 return;
167 }
168
169 # Create block
170 # Note: for a user block, ipb_address is only for display purposes
171
172 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
173 $this->BlockReason, wfTimestampNow(), 0, $expiry );
174
175 if (wfRunHooks('BlockIp', array(&$ban, &$wgUser))) {
176
177 $ban->insert();
178
179 wfRunHooks('BlockIpComplete', array($ban, $wgUser));
180
181 # Make log entry
182 $log = new LogPage( 'block' );
183 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
184 $this->BlockReason, $this->BlockExpiry );
185
186 # Report to the user
187 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
188 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
189 urlencode( $this->BlockAddress ) ) );
190 }
191 }
192
193 function showSuccess() {
194 global $wgOut, $wgUser;
195
196 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
197 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
198 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
199 $wgOut->addWikiText( $text );
200 }
201 }
202
203 ?>