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