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