9b8022b7c2be5feb441df56c04cb2ffe4a6211c2
[lhc/web/wiklou.git] / includes / specials / SpecialUnblock.php
1 <?php
2 /**
3 * Implements Special:Unblock
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\Block\DatabaseBlock;
25
26 /**
27 * A special page for unblocking users
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialUnblock extends SpecialPage {
32
33 protected $target;
34 protected $type;
35 protected $block;
36
37 public function __construct() {
38 parent::__construct( 'Unblock', 'block' );
39 }
40
41 public function doesWrites() {
42 return true;
43 }
44
45 public function execute( $par ) {
46 $this->checkPermissions();
47 $this->checkReadOnly();
48
49 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
50 $this->block = DatabaseBlock::newFromTarget( $this->target );
51 if ( $this->target instanceof User ) {
52 # Set the 'relevant user' in the skin, so it displays links like Contributions,
53 # User logs, UserRights, etc.
54 $this->getSkin()->setRelevantUser( $this->target );
55 }
56
57 $this->setHeaders();
58 $this->outputHeader();
59 $this->addHelpLink( 'Help:Blocking users' );
60
61 $out = $this->getOutput();
62 $out->setPageTitle( $this->msg( 'unblockip' ) );
63 $out->addModules( [ 'mediawiki.userSuggest' ] );
64
65 $form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() );
66 $form->setWrapperLegendMsg( 'unblockip' );
67 $form->setSubmitCallback( [ __CLASS__, 'processUIUnblock' ] );
68 $form->setSubmitTextMsg( 'ipusubmit' );
69 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
70
71 if ( $form->show() ) {
72 switch ( $this->type ) {
73 case DatabaseBlock::TYPE_IP:
74 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
75 break;
76 case DatabaseBlock::TYPE_USER:
77 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
78 break;
79 case DatabaseBlock::TYPE_RANGE:
80 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
81 break;
82 case DatabaseBlock::TYPE_ID:
83 case DatabaseBlock::TYPE_AUTO:
84 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
85 break;
86 }
87 }
88 }
89
90 protected function getFields() {
91 $fields = [
92 'Target' => [
93 'type' => 'text',
94 'label-message' => 'ipaddressorusername',
95 'autofocus' => true,
96 'size' => '45',
97 'required' => true,
98 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
99 ],
100 'Name' => [
101 'type' => 'info',
102 'label-message' => 'ipaddressorusername',
103 ],
104 'Reason' => [
105 'type' => 'text',
106 'label-message' => 'ipbreason',
107 ]
108 ];
109
110 if ( $this->block instanceof DatabaseBlock ) {
111 list( $target, $type ) = $this->block->getTargetAndType();
112
113 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
114 # User:Foo, and we've just got any block, auto or not, that applies to a target
115 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
116 # so don't show any distinction between unblocked IPs and autoblocked IPs
117 if ( $type == DatabaseBlock::TYPE_AUTO && $this->type == DatabaseBlock::TYPE_IP ) {
118 $fields['Target']['default'] = $this->target;
119 unset( $fields['Name'] );
120 } else {
121 $fields['Target']['default'] = $target;
122 $fields['Target']['type'] = 'hidden';
123 switch ( $type ) {
124 case DatabaseBlock::TYPE_IP:
125 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
126 SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
127 $target->getName()
128 );
129 $fields['Name']['raw'] = true;
130 break;
131 case DatabaseBlock::TYPE_USER:
132 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
133 $target->getUserPage(),
134 $target->getName()
135 );
136 $fields['Name']['raw'] = true;
137 break;
138
139 case DatabaseBlock::TYPE_RANGE:
140 $fields['Name']['default'] = $target;
141 break;
142
143 case DatabaseBlock::TYPE_AUTO:
144 $fields['Name']['default'] = $this->block->getRedactedName();
145 $fields['Name']['raw'] = true;
146 # Don't expose the real target of the autoblock
147 $fields['Target']['default'] = "#{$this->target}";
148 break;
149 }
150 // target is hidden, so the reason is the first element
151 $fields['Target']['autofocus'] = false;
152 $fields['Reason']['autofocus'] = true;
153 }
154 } else {
155 $fields['Target']['default'] = $this->target;
156 unset( $fields['Name'] );
157 }
158
159 return $fields;
160 }
161
162 /**
163 * Submit callback for an HTMLForm object
164 * @param array $data
165 * @param HTMLForm $form
166 * @return array|bool [ message key, parameters ]
167 */
168 public static function processUIUnblock( array $data, HTMLForm $form ) {
169 return self::processUnblock( $data, $form->getContext() );
170 }
171
172 /**
173 * Process the form
174 *
175 * Change tags can be provided via $data['Tags'], but the calling function
176 * must check if the tags can be added by the user prior to this function.
177 *
178 * @param array $data
179 * @param IContextSource $context
180 * @throws ErrorPageError
181 * @return array|bool [ [ message key, parameters ] ] on failure, True on success
182 */
183 public static function processUnblock( array $data, IContextSource $context ) {
184 $performer = $context->getUser();
185 $target = $data['Target'];
186 $block = DatabaseBlock::newFromTarget( $data['Target'] );
187
188 if ( !$block instanceof DatabaseBlock ) {
189 return [ [ 'ipb_cant_unblock', $target ] ];
190 }
191
192 # T17810: blocked admins should have limited access here. This
193 # won't allow sysops to remove autoblocks on themselves, but they
194 # should have ipblock-exempt anyway
195 $status = SpecialBlock::checkUnblockSelf( $target, $performer );
196 if ( $status !== true ) {
197 throw new ErrorPageError( 'badaccess', $status );
198 }
199
200 # If the specified IP is a single address, and the block is a range block, don't
201 # unblock the whole range.
202 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
203 if ( $block->getType() == DatabaseBlock::TYPE_RANGE && $type == DatabaseBlock::TYPE_IP ) {
204 $range = $block->getTarget();
205
206 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
207 }
208
209 # If the name was hidden and the blocking user cannot hide
210 # names, then don't allow any block removals...
211 if ( !$performer->isAllowed( 'hideuser' ) && $block->getHideName() ) {
212 return [ 'unblock-hideuser' ];
213 }
214
215 $reason = [ 'hookaborted' ];
216 if ( !Hooks::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
217 return $reason;
218 }
219
220 # Delete block
221 if ( !$block->delete() ) {
222 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
223 }
224
225 Hooks::run( 'UnblockUserComplete', [ $block, $performer ] );
226
227 # Unset _deleted fields as needed
228 if ( $block->getHideName() ) {
229 # Something is deeply FUBAR if this is not a User object, but who knows?
230 $id = $block->getTarget() instanceof User
231 ? $block->getTarget()->getId()
232 : User::idFromName( $block->getTarget() );
233
234 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
235 }
236
237 # Redact the name (IP address) for autoblocks
238 if ( $block->getType() == DatabaseBlock::TYPE_AUTO ) {
239 $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
240 } else {
241 $page = $block->getTarget() instanceof User
242 ? $block->getTarget()->getUserPage()
243 : Title::makeTitle( NS_USER, $block->getTarget() );
244 }
245
246 # Make log entry
247 $logEntry = new ManualLogEntry( 'block', 'unblock' );
248 $logEntry->setTarget( $page );
249 $logEntry->setComment( $data['Reason'] );
250 $logEntry->setPerformer( $performer );
251 if ( isset( $data['Tags'] ) ) {
252 $logEntry->addTags( $data['Tags'] );
253 }
254 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
255 $logId = $logEntry->insert();
256 $logEntry->publish( $logId );
257
258 return true;
259 }
260
261 /**
262 * Return an array of subpages beginning with $search that this special page will accept.
263 *
264 * @param string $search Prefix to search for
265 * @param int $limit Maximum number of results to return (usually 10)
266 * @param int $offset Number of results to skip (usually 0)
267 * @return string[] Matching subpages
268 */
269 public function prefixSearchSubpages( $search, $limit, $offset ) {
270 $user = User::newFromName( $search );
271 if ( !$user ) {
272 // No prefix suggestion for invalid user
273 return [];
274 }
275 // Autocomplete subpage as user list - public to allow caching
276 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
277 }
278
279 protected function getGroupName() {
280 return 'users';
281 }
282 }