Replacing block time field with an intuitive drop-down menu
[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 ) { $ipb->showSuccess(); }
23 else if ( $wgRequest->wasPosted() && 'submit' == $action ) { $ipb->doSubmit(); }
24 else { $ipb->showForm( '' ); }
25 }
26
27 /**
28 * Form object
29 *
30 * @package MediaWiki
31 * @subpackage SpecialPage
32 */
33 class IPBlockForm {
34 var $BlockAddress, $BlockExpiry, $BlockReason;
35
36 function IPBlockForm() {
37 global $wgRequest;
38 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip' ) );
39 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
40 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry' );
41 }
42
43 function showForm( $err ) {
44 global $wgOut, $wgUser, $wgLang, $wgDefaultBlockExpiry;
45 global $wgRequest;
46
47 $wgOut->setPagetitle( htmlspecialchars( wfMsg( 'blockip' ) ) );
48 $wgOut->addWikiText( htmlspecialchars( wfMsg( 'blockiptext' ) ) );
49
50 if ( is_null( $this->BlockExpiry ) || $this->BlockExpiry === '' ) {
51 $this->BlockExpiry = $wgDefaultBlockExpiry;
52 }
53
54 $mIpaddress = htmlspecialchars( wfMsg( 'ipaddress' ) );
55 $mIpbexpiry = htmlspecialchars( wfMsg( 'ipbexpiry' ) );
56 $mIpbreason = htmlspecialchars( wfMsg( 'ipbreason' ) );
57 $mIpbsubmit = htmlspecialchars( wfMsg( 'ipbsubmit' ) );
58 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
59 $action = $titleObj->escapeLocalURL( "action=submit" );
60
61 if ( "" != $err ) {
62 $wgOut->setSubtitle( htmlspecialchars( wfMsg( 'formerror' ) ) );
63 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
64 }
65
66 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
67 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
68 $scBlockReason = htmlspecialchars( $this->BlockReason );
69
70 $wgOut->addHTML( "
71 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
72 <table border='0'>
73 <tr>
74 <td align=\"right\">{$mIpaddress}:</td>
75 <td align=\"left\">
76 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
77 </td>
78 </tr>
79 <tr>
80 <td align=\"right\">{$mIpbexpiry}:</td>
81 <td align=\"left\">
82 <select name=\"wpBlockExpiry\"/>
83 <option>2 hours</option>
84 <option>1 day</option>
85 <option>3 days</option>
86 <option>1 week</option>
87 <option>2 weeks</option>
88 <option>1 month</option>
89 <option>3 months</option>
90 <option>6 months</option>
91 <option>1 year</option>
92 <option>indefinite</option>
93 </select>
94 </td>
95 </tr>
96 <tr>
97 <td align=\"right\">{$mIpbreason}:</td>
98 <td align=\"left\">
99 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
100 </td>
101 </tr>
102 <tr>
103 <td>&nbsp;</td>
104 <td align=\"left\">
105 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
106 </td>
107 </tr>
108 </table>
109 </form>\n" );
110
111 }
112
113 function doSubmit() {
114 global $wgOut, $wgUser, $wgLang;
115 global $wgSysopUserBans, $wgSysopRangeBans;
116
117 $userId = 0;
118 $this->BlockAddress = trim( $this->BlockAddress );
119 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
120
121 # Check for invalid specifications
122 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
123 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
124 if ( $wgSysopRangeBans ) {
125 if ( $matches[2] > 31 || $matches[2] < 16 ) {
126 $this->showForm( wfMsg( 'ip_range_invalid' ) );
127 return;
128 }
129 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
130 } else {
131 # Range block illegal
132 $this->showForm( wfMsg( 'range_block_disabled' ) );
133 return;
134 }
135 } else {
136 # Username block
137 if ( $wgSysopUserBans ) {
138 $userId = User::idFromName( $this->BlockAddress );
139 if ( $userId == 0 ) {
140 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
141 return;
142 }
143 } else {
144 $this->showForm( wfMsg( 'badipaddress' ) );
145 return;
146 }
147 }
148 }
149
150 if ( $this->BlockExpiry == 'infinite' || $this->BlockExpiry == 'indefinite' ) {
151 $expiry = '';
152 } else {
153 # Convert GNU-style date, returns -1 on error
154 $expiry = strtotime( $this->BlockExpiry );
155
156 if ( $expiry < 0 ) {
157 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
158 return;
159 }
160
161 $expiry = wfTimestamp( TS_MW, $expiry );
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', $ban, $wgUser)) {
177
178 $ban->insert();
179
180 wfRunHooks('BlockIpComplete', $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 ?>