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