follow up to r114081: qqq, and to r114082: match rename
[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 $this->checkPermissions();
39 $this->checkReadOnly();
40
41 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
42 $this->block = Block::newFromTarget( $this->target );
43
44 $this->setHeaders();
45 $this->outputHeader();
46
47 $out = $this->getOutput();
48 $out->setPageTitle( $this->msg( 'unblockip' ) );
49 $out->addModules( 'mediawiki.special' );
50
51 $form = new HTMLForm( $this->getFields(), $this->getContext() );
52 $form->setWrapperLegendMsg( 'unblockip' );
53 $form->setSubmitCallback( array( __CLASS__, 'processUIUnblock' ) );
54 $form->setSubmitTextMsg( 'ipusubmit' );
55 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
56
57 if( $form->show() ){
58 switch( $this->type ){
59 case Block::TYPE_USER:
60 case Block::TYPE_IP:
61 $out->addWikiMsg( 'unblocked', $this->target );
62 break;
63 case Block::TYPE_RANGE:
64 $out->addWikiMsg( 'unblocked-range', $this->target );
65 break;
66 case Block::TYPE_ID:
67 case Block::TYPE_AUTO:
68 $out->addWikiMsg( 'unblocked-id', $this->target );
69 break;
70 }
71 }
72 }
73
74 protected function getFields(){
75 $fields = array(
76 'Target' => array(
77 'type' => 'text',
78 'label-message' => 'ipadressorusername',
79 'tabindex' => '1',
80 'size' => '45',
81 'required' => true,
82 ),
83 'Name' => array(
84 'type' => 'info',
85 'label-message' => 'ipadressorusername',
86 ),
87 'Reason' => array(
88 'type' => 'text',
89 'label-message' => 'ipbreason',
90 )
91 );
92
93 if( $this->block instanceof Block ){
94 list( $target, $type ) = $this->block->getTargetAndType();
95
96 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
97 # User:Foo, and we've just got any block, auto or not, that applies to a target
98 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
99 # so don't show any distinction between unblocked IPs and autoblocked IPs
100 if( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ){
101 $fields['Target']['default'] = $this->target;
102 unset( $fields['Name'] );
103
104 } else {
105 $fields['Target']['default'] = $target;
106 $fields['Target']['type'] = 'hidden';
107 switch( $type ){
108 case Block::TYPE_USER:
109 case Block::TYPE_IP:
110 $fields['Name']['default'] = Linker::link(
111 $target->getUserPage(),
112 $target->getName()
113 );
114 $fields['Name']['raw'] = true;
115 break;
116
117 case Block::TYPE_RANGE:
118 $fields['Name']['default'] = $target;
119 break;
120
121 case Block::TYPE_AUTO:
122 $fields['Name']['default'] = $this->block->getRedactedName();
123 $fields['Name']['raw'] = true;
124 # Don't expose the real target of the autoblock
125 $fields['Target']['default'] = "#{$this->target}";
126 break;
127 }
128 }
129
130 } else {
131 $fields['Target']['default'] = $this->target;
132 unset( $fields['Name'] );
133 }
134 return $fields;
135 }
136
137 /**
138 * Submit callback for an HTMLForm object
139 * @return Array( Array(message key, parameters)
140 */
141 public static function processUIUnblock( array $data, HTMLForm $form ) {
142 return self::processUnblock( $data, $form->getContext() );
143 }
144
145 /**
146 * Process the form
147 *
148 * @param $data Array
149 * @param $context IContextSource
150 * @return Array( Array(message key, parameters) ) on failure, True on success
151 */
152 public static function processUnblock( array $data, IContextSource $context ){
153 $performer = $context->getUser();
154 $target = $data['Target'];
155 $block = Block::newFromTarget( $data['Target'] );
156
157 if( !$block instanceof Block ){
158 return array( array( 'ipb_cant_unblock', $target ) );
159 }
160
161 # bug 15810: blocked admins should have limited access here. This
162 # won't allow sysops to remove autoblocks on themselves, but they
163 # should have ipblock-exempt anyway
164 $status = SpecialBlock::checkUnblockSelf( $target, $performer );
165 if ( $status !== true ) {
166 throw new ErrorPageError( 'badaccess', $status );
167 }
168
169 # If the specified IP is a single address, and the block is a range block, don't
170 # unblock the whole range.
171 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
172 if( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
173 $range = $block->getTarget();
174 return array( array( 'ipb_blocked_as_range', $target, $range ) );
175 }
176
177 # If the name was hidden and the blocking user cannot hide
178 # names, then don't allow any block removals...
179 if( !$performer->isAllowed( 'hideuser' ) && $block->mHideName ) {
180 return array( 'unblock-hideuser' );
181 }
182
183 # Delete block
184 if ( !$block->delete() ) {
185 return array( 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) );
186 }
187
188 # Unset _deleted fields as needed
189 if( $block->mHideName ) {
190 # Something is deeply FUBAR if this is not a User object, but who knows?
191 $id = $block->getTarget() instanceof User
192 ? $block->getTarget()->getID()
193 : User::idFromName( $block->getTarget() );
194
195 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
196 }
197
198 # Redact the name (IP address) for autoblocks
199 if ( $block->getType() == Block::TYPE_AUTO ) {
200 $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
201 } else {
202 $page = $block->getTarget() instanceof User
203 ? $block->getTarget()->getUserpage()
204 : Title::makeTitle( NS_USER, $block->getTarget() );
205 }
206
207 # Make log entry
208 $log = new LogPage( 'block' );
209 $log->addEntry( 'unblock', $page, $data['Reason'] );
210
211 return true;
212 }
213 }