Merge "Add checkDependencies.php"
[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
60 $out = $this->getOutput();
61 $out->setPageTitle( $this->msg( 'unblockip' ) );
62 $out->addModules( [ 'mediawiki.userSuggest' ] );
63
64 $form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() );
65 $form->setWrapperLegendMsg( 'unblockip' );
66 $form->setSubmitCallback( [ __CLASS__, 'processUIUnblock' ] );
67 $form->setSubmitTextMsg( 'ipusubmit' );
68 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
69
70 if ( $form->show() ) {
71 switch ( $this->type ) {
72 case DatabaseBlock::TYPE_IP:
73 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
74 break;
75 case DatabaseBlock::TYPE_USER:
76 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
77 break;
78 case DatabaseBlock::TYPE_RANGE:
79 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
80 break;
81 case DatabaseBlock::TYPE_ID:
82 case DatabaseBlock::TYPE_AUTO:
83 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
84 break;
85 }
86 }
87 }
88
89 protected function getFields() {
90 $fields = [
91 'Target' => [
92 'type' => 'text',
93 'label-message' => 'ipaddressorusername',
94 'autofocus' => true,
95 'size' => '45',
96 'required' => true,
97 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
98 ],
99 'Name' => [
100 'type' => 'info',
101 'label-message' => 'ipaddressorusername',
102 ],
103 'Reason' => [
104 'type' => 'text',
105 'label-message' => 'ipbreason',
106 ]
107 ];
108
109 if ( $this->block instanceof DatabaseBlock ) {
110 list( $target, $type ) = $this->block->getTargetAndType();
111
112 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
113 # User:Foo, and we've just got any block, auto or not, that applies to a target
114 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
115 # so don't show any distinction between unblocked IPs and autoblocked IPs
116 if ( $type == DatabaseBlock::TYPE_AUTO && $this->type == DatabaseBlock::TYPE_IP ) {
117 $fields['Target']['default'] = $this->target;
118 unset( $fields['Name'] );
119 } else {
120 $fields['Target']['default'] = $target;
121 $fields['Target']['type'] = 'hidden';
122 switch ( $type ) {
123 case DatabaseBlock::TYPE_IP:
124 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
125 SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
126 $target->getName()
127 );
128 $fields['Name']['raw'] = true;
129 break;
130 case DatabaseBlock::TYPE_USER:
131 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
132 $target->getUserPage(),
133 $target->getName()
134 );
135 $fields['Name']['raw'] = true;
136 break;
137
138 case DatabaseBlock::TYPE_RANGE:
139 $fields['Name']['default'] = $target;
140 break;
141
142 case DatabaseBlock::TYPE_AUTO:
143 $fields['Name']['default'] = $this->block->getRedactedName();
144 $fields['Name']['raw'] = true;
145 # Don't expose the real target of the autoblock
146 $fields['Target']['default'] = "#{$this->target}";
147 break;
148 }
149 // target is hidden, so the reason is the first element
150 $fields['Target']['autofocus'] = false;
151 $fields['Reason']['autofocus'] = true;
152 }
153 } else {
154 $fields['Target']['default'] = $this->target;
155 unset( $fields['Name'] );
156 }
157
158 return $fields;
159 }
160
161 /**
162 * Submit callback for an HTMLForm object
163 * @param array $data
164 * @param HTMLForm $form
165 * @return array|bool Array(message key, parameters)
166 */
167 public static function processUIUnblock( array $data, HTMLForm $form ) {
168 return self::processUnblock( $data, $form->getContext() );
169 }
170
171 /**
172 * Process the form
173 *
174 * Change tags can be provided via $data['Tags'], but the calling function
175 * must check if the tags can be added by the user prior to this function.
176 *
177 * @param array $data
178 * @param IContextSource $context
179 * @throws ErrorPageError
180 * @return array|bool Array( Array( message key, parameters ) ) on failure, True on success
181 */
182 public static function processUnblock( array $data, IContextSource $context ) {
183 $performer = $context->getUser();
184 $target = $data['Target'];
185 $block = DatabaseBlock::newFromTarget( $data['Target'] );
186
187 if ( !$block instanceof DatabaseBlock ) {
188 return [ [ 'ipb_cant_unblock', $target ] ];
189 }
190
191 # T17810: blocked admins should have limited access here. This
192 # won't allow sysops to remove autoblocks on themselves, but they
193 # should have ipblock-exempt anyway
194 $status = SpecialBlock::checkUnblockSelf( $target, $performer );
195 if ( $status !== true ) {
196 throw new ErrorPageError( 'badaccess', $status );
197 }
198
199 # If the specified IP is a single address, and the block is a range block, don't
200 # unblock the whole range.
201 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
202 if ( $block->getType() == DatabaseBlock::TYPE_RANGE && $type == DatabaseBlock::TYPE_IP ) {
203 $range = $block->getTarget();
204
205 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
206 }
207
208 # If the name was hidden and the blocking user cannot hide
209 # names, then don't allow any block removals...
210 if ( !$performer->isAllowed( 'hideuser' ) && $block->getHideName() ) {
211 return [ 'unblock-hideuser' ];
212 }
213
214 $reason = [ 'hookaborted' ];
215 if ( !Hooks::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
216 return $reason;
217 }
218
219 # Delete block
220 if ( !$block->delete() ) {
221 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
222 }
223
224 Hooks::run( 'UnblockUserComplete', [ $block, $performer ] );
225
226 # Unset _deleted fields as needed
227 if ( $block->getHideName() ) {
228 # Something is deeply FUBAR if this is not a User object, but who knows?
229 $id = $block->getTarget() instanceof User
230 ? $block->getTarget()->getId()
231 : User::idFromName( $block->getTarget() );
232
233 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
234 }
235
236 # Redact the name (IP address) for autoblocks
237 if ( $block->getType() == DatabaseBlock::TYPE_AUTO ) {
238 $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
239 } else {
240 $page = $block->getTarget() instanceof User
241 ? $block->getTarget()->getUserPage()
242 : Title::makeTitle( NS_USER, $block->getTarget() );
243 }
244
245 # Make log entry
246 $logEntry = new ManualLogEntry( 'block', 'unblock' );
247 $logEntry->setTarget( $page );
248 $logEntry->setComment( $data['Reason'] );
249 $logEntry->setPerformer( $performer );
250 if ( isset( $data['Tags'] ) ) {
251 $logEntry->setTags( $data['Tags'] );
252 }
253 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
254 $logId = $logEntry->insert();
255 $logEntry->publish( $logId );
256
257 return true;
258 }
259
260 /**
261 * Return an array of subpages beginning with $search that this special page will accept.
262 *
263 * @param string $search Prefix to search for
264 * @param int $limit Maximum number of results to return (usually 10)
265 * @param int $offset Number of results to skip (usually 0)
266 * @return string[] Matching subpages
267 */
268 public function prefixSearchSubpages( $search, $limit, $offset ) {
269 $user = User::newFromName( $search );
270 if ( !$user ) {
271 // No prefix suggestion for invalid user
272 return [];
273 }
274 // Autocomplete subpage as user list - public to allow caching
275 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
276 }
277
278 protected function getGroupName() {
279 return 'users';
280 }
281 }