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