65f9bb1518547ff30676bd1b1d1a24d734173e91
[lhc/web/wiklou.git] / includes / specials / SpecialUnblock.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup SpecialPage
20 */
21
22 /**
23 * A special page for unblocking users
24 *
25 * @ingroup SpecialPage
26 */
27 class SpecialUnblock extends SpecialPage {
28
29 protected $target;
30 protected $type;
31 protected $block;
32
33 public function __construct(){
34 parent::__construct( 'Unblock', 'block' );
35 }
36
37 public function execute( $par ){
38 global $wgUser, $wgOut, $wgRequest;
39
40 # Check permissions
41 if( !$this->userCanExecute( $wgUser ) ) {
42 $this->displayRestrictionError();
43 return;
44 }
45
46 # Check for database lock
47 if( wfReadOnly() ) {
48 throw new ReadOnlyError;
49 }
50
51 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $wgRequest );
52 $this->block = Block::newFromTarget( $this->target );
53
54 $wgOut->setPageTitle( wfMsg( 'unblockip' ) );
55 $wgOut->addModules( 'mediawiki.special' );
56
57 $form = new HTMLForm( $this->getFields(), $this->getContext() );
58 $form->setWrapperLegend( wfMsg( 'unblockip' ) );
59 $form->setSubmitCallback( array( __CLASS__, 'processUnblock' ) );
60 $form->setSubmitText( wfMsg( 'ipusubmit' ) );
61 $form->addPreText( wfMsgExt( 'unblockiptext', 'parse' ) );
62
63 if( $form->show() ){
64 switch( $this->type ){
65 case Block::TYPE_USER:
66 case Block::TYPE_IP:
67 $wgOut->addWikiMsg( 'unblocked', $this->target );
68 break;
69 case Block::TYPE_RANGE:
70 $wgOut->addWikiMsg( 'unblocked-range', $this->target );
71 break;
72 case Block::TYPE_ID:
73 case Block::TYPE_AUTO:
74 $wgOut->addWikiMsg( 'unblocked-id', $this->target );
75 break;
76 }
77 }
78 }
79
80 protected function getFields(){
81 $fields = array(
82 'Target' => array(
83 'type' => 'text',
84 'label-message' => 'ipadressorusername',
85 'tabindex' => '1',
86 'size' => '45',
87 'required' => true,
88 ),
89 'Name' => array(
90 'type' => 'info',
91 'label-message' => 'ipadressorusername',
92 ),
93 'Reason' => array(
94 'type' => 'text',
95 'label-message' => 'ipbreason',
96 )
97 );
98
99 if( $this->block instanceof Block ){
100 list( $target, $type ) = $this->block->getTargetAndType();
101
102 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
103 # User:Foo, and we've just got any block, auto or not, that applies to a target
104 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
105 # so don't show any distinction between unblocked IPs and autoblocked IPs
106 if( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ){
107 $fields['Target']['default'] = $this->target;
108 unset( $fields['Name'] );
109
110 } else {
111 $fields['Target']['default'] = $target;
112 $fields['Target']['type'] = 'hidden';
113 switch( $type ){
114 case Block::TYPE_USER:
115 case Block::TYPE_IP:
116 $skin = $this->getSkin();
117 $fields['Name']['default'] = $skin->link(
118 $target->getUserPage(),
119 $target->getName()
120 );
121 $fields['Name']['raw'] = true;
122 break;
123
124 case Block::TYPE_RANGE:
125 $fields['Name']['default'] = $target;
126 break;
127
128 case Block::TYPE_AUTO:
129 $fields['Name']['default'] = $this->block->getRedactedName();
130 $fields['Name']['raw'] = true;
131 # Don't expose the real target of the autoblock
132 $fields['Target']['default'] = "#{$this->target}";
133 break;
134 }
135 }
136
137 } else {
138 $fields['Target']['default'] = $this->target;
139 unset( $fields['Name'] );
140 }
141 return $fields;
142 }
143
144 /**
145 * Process the form
146 * @return Array( Array(message key, parameters) ) on failure, True on success
147 */
148 public static function processUnblock( array $data ){
149 global $wgUser;
150
151 $target = $data['Target'];
152 $block = Block::newFromTarget( $data['Target'] );
153
154 if( !$block instanceof Block ){
155 return array( array( 'ipb_cant_unblock', $target ) );
156 }
157
158 # bug 15810: blocked admins should have limited access here. This
159 # won't allow sysops to remove autoblocks on themselves, but they
160 # should have ipblock-exempt anyway
161 $status = SpecialBlock::checkUnblockSelf( $target );
162 if ( $status !== true ) {
163 throw new ErrorPageError( 'badaccess', $status );
164 }
165
166 # If the specified IP is a single address, and the block is a range block, don't
167 # unblock the whole range.
168 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
169 if( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
170 $range = $block->getTarget();
171 return array( array( 'ipb_blocked_as_range', $target, $range ) );
172 }
173
174 # If the name was hidden and the blocking user cannot hide
175 # names, then don't allow any block removals...
176 if( !$wgUser->isAllowed( 'hideuser' ) && $block->mHideName ) {
177 return array( 'unblock-hideuser' );
178 }
179
180 # Delete block
181 if ( !$block->delete() ) {
182 return array( 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) );
183 }
184
185 # Unset _deleted fields as needed
186 if( $block->mHideName ) {
187 # Something is deeply FUBAR if this is not a User object, but who knows?
188 $id = $block->getTarget() instanceof User
189 ? $block->getTarget()->getID()
190 : User::idFromName( $block->getTarget() );
191
192 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
193 }
194
195 # Make log entry
196 $log = new LogPage( 'block' );
197 $page = $block->getTarget() instanceof User
198 ? $block->getTarget()->getUserpage()
199 : Title::makeTitle( NS_USER, $block->getTarget() );
200 $log->addEntry( 'unblock', $page, $data['Reason'] );
201
202 return true;
203 }
204 }