Special:Log and the logging table -- unified logging scariness!
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2
3 function wfSpecialBlockip()
4 {
5 global $wgUser, $wgOut, $wgRequest;
6
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
10 }
11 $ipb = new IPBlockForm();
12
13 $action = $wgRequest->getVal( 'action' );
14 if ( "success" == $action ) { $ipb->showSuccess(); }
15 else if ( $wgRequest->wasPosted() && "submit" == $action ) { $ipb->doSubmit(); }
16 else { $ipb->showForm( "" ); }
17 }
18
19 class IPBlockForm {
20 var $BlockAddress, $BlockExpiry, $BlockReason;
21
22 function IPBlockForm() {
23 global $wgRequest;
24 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip' ) );
25 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
26 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry' );
27 }
28
29 function showForm( $err )
30 {
31 global $wgOut, $wgUser, $wgLang, $wgDefaultBlockExpiry;
32 global $wgRequest;
33
34 $wgOut->setPagetitle( htmlspecialchars( wfMsg( "blockip" ) ) );
35 $wgOut->addWikiText( htmlspecialchars( wfMsg( "blockiptext" ) ) );
36
37 if ( is_null( $this->BlockExpiry ) || $this->BlockExpiry === "" ) {
38 $this->BlockExpiry = $wgDefaultBlockExpiry;
39 }
40
41 $mIpaddress = htmlspecialchars( wfMsg( "ipaddress" ) );
42 $mIpbexpiry = htmlspecialchars( wfMsg( "ipbexpiry" ) );
43 $mIpbreason = htmlspecialchars( wfMsg( "ipbreason" ) );
44 $mIpbsubmit = htmlspecialchars( wfMsg( "ipbsubmit" ) );
45 $titleObj = Title::makeTitle( NS_SPECIAL, "Blockip" );
46 $action = $titleObj->escapeLocalURL( "action=submit" );
47
48 if ( "" != $err ) {
49 $wgOut->setSubtitle( htmlspecialchars( wfMsg( "formerror" ) ) );
50 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
51 }
52
53 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
54 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
55 $scBlockReason = htmlspecialchars( $this->BlockReason );
56
57 $wgOut->addHTML( "
58 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
59 <table border='0'>
60 <tr>
61 <td align=\"right\">{$mIpaddress}:</td>
62 <td align=\"left\">
63 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
64 </td>
65 </tr>
66 <tr>
67 <td align=\"right\">{$mIpbexpiry}:</td>
68 <td align=\"left\">
69 <input tabindex='2' type='text' size='20' name=\"wpBlockExpiry\" value=\"{$scBlockExpiry}\" />
70 </td>
71 </tr>
72 <tr>
73 <td align=\"right\">{$mIpbreason}:</td>
74 <td align=\"left\">
75 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
76 </td>
77 </tr>
78 <tr>
79 <td>&nbsp;</td>
80 <td align=\"left\">
81 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
82 </td>
83 </tr>
84 </table>
85 </form>\n" );
86
87 }
88
89 function doSubmit()
90 {
91 global $wgOut, $wgUser, $wgLang;
92 global $wgSysopUserBans, $wgSysopRangeBans;
93
94 $userId = 0;
95 $this->BlockAddress = trim( $this->BlockAddress );
96 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
97
98 # Check for invalid specifications
99 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
100 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
101 if ( $wgSysopRangeBans ) {
102 if ( $matches[2] > 31 || $matches[2] < 16 ) {
103 $this->showForm( wfMsg( "ip_range_invalid" ) );
104 return;
105 }
106 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
107 } else {
108 # Range block illegal
109 $this->showForm( wfMsg( "range_block_disabled" ) );
110 return;
111 }
112 } else {
113 # Username block
114 if ( $wgSysopUserBans ) {
115 $userId = User::idFromName( $this->BlockAddress );
116 if ( $userId == 0 ) {
117 $this->showForm( wfMsg( "nosuchuser", htmlspecialchars( $this->BlockAddress ) ) );
118 return;
119 }
120 } else {
121 $this->showForm( wfMsg( "badipaddress" ) );
122 return;
123 }
124 }
125 }
126
127 if ( $this->BlockExpiry == "infinite" || $this->BlockExpiry == "indefinite" ) {
128 $expiry = '';
129 } else {
130 # Convert GNU-style date, returns -1 on error
131 $expiry = strtotime( $this->BlockExpiry );
132
133 if ( $expiry < 0 ) {
134 $this->showForm( wfMsg( "ipb_expiry_invalid" ) );
135 return;
136 }
137
138 $expiry = wfUnix2Timestamp( $expiry );
139
140 }
141
142
143 if ( "" == $this->BlockReason ) {
144 $this->showForm( wfMsg( "noblockreason" ) );
145 return;
146 }
147
148 # Create block
149 # Note: for a user block, ipb_address is only for display purposes
150
151 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
152 wfStrencode( $this->BlockReason ), wfTimestampNow(), 0, $expiry );
153 $ban->insert();
154
155 # Make log entry
156 $log = new LogPage( 'block' );
157 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ), $this->BlockReason );
158
159 # Report to the user
160 $titleObj = Title::makeTitle( NS_SPECIAL, "Blockip" );
161 $wgOut->redirect( $titleObj->getFullURL( "action=success&ip={$this->BlockAddress}" ) );
162 }
163
164 function showSuccess()
165 {
166 global $wgOut, $wgUser;
167
168 $wgOut->setPagetitle( wfMsg( "blockip" ) );
169 $wgOut->setSubtitle( wfMsg( "blockipsuccesssub" ) );
170 $text = wfMsg( "blockipsuccesstext", $this->BlockAddress );
171 $wgOut->addWikiText( $text );
172 }
173 }
174
175 ?>