single quotes
[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->isSysop() ) {
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 <input tabindex='2' type='text' size='20' name=\"wpBlockExpiry\" value=\"{$scBlockExpiry}\" />
83 </td>
84 </tr>
85 <tr>
86 <td align=\"right\">{$mIpbreason}:</td>
87 <td align=\"left\">
88 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
89 </td>
90 </tr>
91 <tr>
92 <td>&nbsp;</td>
93 <td align=\"left\">
94 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
95 </td>
96 </tr>
97 </table>
98 </form>\n" );
99
100 }
101
102 function doSubmit() {
103 global $wgOut, $wgUser, $wgLang;
104 global $wgSysopUserBans, $wgSysopRangeBans;
105
106 $userId = 0;
107 $this->BlockAddress = trim( $this->BlockAddress );
108 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
109
110 # Check for invalid specifications
111 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
112 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
113 if ( $wgSysopRangeBans ) {
114 if ( $matches[2] > 31 || $matches[2] < 16 ) {
115 $this->showForm( wfMsg( 'ip_range_invalid' ) );
116 return;
117 }
118 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
119 } else {
120 # Range block illegal
121 $this->showForm( wfMsg( 'range_block_disabled' ) );
122 return;
123 }
124 } else {
125 # Username block
126 if ( $wgSysopUserBans ) {
127 $userId = User::idFromName( $this->BlockAddress );
128 if ( $userId == 0 ) {
129 $this->showForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->BlockAddress ) ) );
130 return;
131 }
132 } else {
133 $this->showForm( wfMsg( 'badipaddress' ) );
134 return;
135 }
136 }
137 }
138
139 if ( $this->BlockExpiry == 'infinite' || $this->BlockExpiry == 'indefinite' ) {
140 $expiry = '';
141 } else {
142 # Convert GNU-style date, returns -1 on error
143 $expiry = strtotime( $this->BlockExpiry );
144
145 if ( $expiry < 0 ) {
146 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
147 return;
148 }
149
150 $expiry = wfUnix2Timestamp( $expiry );
151
152 }
153
154 if ( $this->BlockReason == '') {
155 $this->showForm( wfMsg( 'noblockreason' ) );
156 return;
157 }
158
159 # Create block
160 # Note: for a user block, ipb_address is only for display purposes
161
162 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
163 wfStrencode( $this->BlockReason ), wfTimestampNow(), 0, $expiry );
164 $ban->insert();
165
166 # Make log entry
167 $log = new LogPage( 'block' );
168 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ), $this->BlockReason );
169
170 # Report to the user
171 $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
172 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip='.$this->BlockAddress ) );
173 }
174
175 function showSuccess() {
176 global $wgOut, $wgUser;
177
178 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
179 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
180 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
181 $wgOut->addWikiText( $text );
182 }
183 }
184
185 ?>