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