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