summaryrefslogtreecommitdiff
path: root/www/wiki/includes/specials/SpecialUnblock.php
blob: b2d5a1633b96cd217fc3e16bc42e2304e92a0150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
 * Implements Special:Unblock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup SpecialPage
 */

/**
 * A special page for unblocking users
 *
 * @ingroup SpecialPage
 */
class SpecialUnblock extends SpecialPage {

	protected $target;
	protected $type;
	protected $block;

	public function __construct() {
		parent::__construct( 'Unblock', 'block' );
	}

	public function doesWrites() {
		return true;
	}

	public function execute( $par ) {
		$this->checkPermissions();
		$this->checkReadOnly();

		list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
		$this->block = Block::newFromTarget( $this->target );
		if ( $this->target instanceof User ) {
			# Set the 'relevant user' in the skin, so it displays links like Contributions,
			# User logs, UserRights, etc.
			$this->getSkin()->setRelevantUser( $this->target );
		}

		$this->setHeaders();
		$this->outputHeader();

		$out = $this->getOutput();
		$out->setPageTitle( $this->msg( 'unblockip' ) );
		$out->addModules( [ 'mediawiki.userSuggest' ] );

		$form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() );
		$form->setWrapperLegendMsg( 'unblockip' );
		$form->setSubmitCallback( [ __CLASS__, 'processUIUnblock' ] );
		$form->setSubmitTextMsg( 'ipusubmit' );
		$form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );

		if ( $form->show() ) {
			switch ( $this->type ) {
				case Block::TYPE_IP:
					$out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
					break;
				case Block::TYPE_USER:
					$out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
					break;
				case Block::TYPE_RANGE:
					$out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
					break;
				case Block::TYPE_ID:
				case Block::TYPE_AUTO:
					$out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
					break;
			}
		}
	}

	protected function getFields() {
		$fields = [
			'Target' => [
				'type' => 'text',
				'label-message' => 'ipaddressorusername',
				'autofocus' => true,
				'size' => '45',
				'required' => true,
				'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
			],
			'Name' => [
				'type' => 'info',
				'label-message' => 'ipaddressorusername',
			],
			'Reason' => [
				'type' => 'text',
				'label-message' => 'ipbreason',
			]
		];

		if ( $this->block instanceof Block ) {
			list( $target, $type ) = $this->block->getTargetAndType();

			# Autoblocks are logged as "autoblock #123 because the IP was recently used by
			# User:Foo, and we've just got any block, auto or not, that applies to a target
			# the user has specified.  Someone could be fishing to connect IPs to autoblocks,
			# so don't show any distinction between unblocked IPs and autoblocked IPs
			if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) {
				$fields['Target']['default'] = $this->target;
				unset( $fields['Name'] );
			} else {
				$fields['Target']['default'] = $target;
				$fields['Target']['type'] = 'hidden';
				switch ( $type ) {
					case Block::TYPE_IP:
						$fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
							SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
							$target->getName()
						);
						$fields['Name']['raw'] = true;
						break;
					case Block::TYPE_USER:
						$fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
							$target->getUserPage(),
							$target->getName()
						);
						$fields['Name']['raw'] = true;
						break;

					case Block::TYPE_RANGE:
						$fields['Name']['default'] = $target;
						break;

					case Block::TYPE_AUTO:
						$fields['Name']['default'] = $this->block->getRedactedName();
						$fields['Name']['raw'] = true;
						# Don't expose the real target of the autoblock
						$fields['Target']['default'] = "#{$this->target}";
						break;
				}
				// target is hidden, so the reason is the first element
				$fields['Target']['autofocus'] = false;
				$fields['Reason']['autofocus'] = true;
			}
		} else {
			$fields['Target']['default'] = $this->target;
			unset( $fields['Name'] );
		}

		return $fields;
	}

	/**
	 * Submit callback for an HTMLForm object
	 * @param array $data
	 * @param HTMLForm $form
	 * @return array|bool Array(message key, parameters)
	 */
	public static function processUIUnblock( array $data, HTMLForm $form ) {
		return self::processUnblock( $data, $form->getContext() );
	}

	/**
	 * Process the form
	 *
	 * Change tags can be provided via $data['Tags'], but the calling function
	 * must check if the tags can be added by the user prior to this function.
	 *
	 * @param array $data
	 * @param IContextSource $context
	 * @throws ErrorPageError
	 * @return array|bool Array( Array( message key, parameters ) ) on failure, True on success
	 */
	public static function processUnblock( array $data, IContextSource $context ) {
		$performer = $context->getUser();
		$target = $data['Target'];
		$block = Block::newFromTarget( $data['Target'] );

		if ( !$block instanceof Block ) {
			return [ [ 'ipb_cant_unblock', $target ] ];
		}

		# T17810: blocked admins should have limited access here.  This
		# won't allow sysops to remove autoblocks on themselves, but they
		# should have ipblock-exempt anyway
		$status = SpecialBlock::checkUnblockSelf( $target, $performer );
		if ( $status !== true ) {
			throw new ErrorPageError( 'badaccess', $status );
		}

		# If the specified IP is a single address, and the block is a range block, don't
		# unblock the whole range.
		list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
		if ( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
			$range = $block->getTarget();

			return [ [ 'ipb_blocked_as_range', $target, $range ] ];
		}

		# If the name was hidden and the blocking user cannot hide
		# names, then don't allow any block removals...
		if ( !$performer->isAllowed( 'hideuser' ) && $block->mHideName ) {
			return [ 'unblock-hideuser' ];
		}

		$reason = [ 'hookaborted' ];
		if ( !Hooks::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
			return $reason;
		}

		# Delete block
		if ( !$block->delete() ) {
			return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
		}

		Hooks::run( 'UnblockUserComplete', [ $block, $performer ] );

		# Unset _deleted fields as needed
		if ( $block->mHideName ) {
			# Something is deeply FUBAR if this is not a User object, but who knows?
			$id = $block->getTarget() instanceof User
				? $block->getTarget()->getId()
				: User::idFromName( $block->getTarget() );

			RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
		}

		# Redact the name (IP address) for autoblocks
		if ( $block->getType() == Block::TYPE_AUTO ) {
			$page = Title::makeTitle( NS_USER, '#' . $block->getId() );
		} else {
			$page = $block->getTarget() instanceof User
				? $block->getTarget()->getUserPage()
				: Title::makeTitle( NS_USER, $block->getTarget() );
		}

		# Make log entry
		$logEntry = new ManualLogEntry( 'block', 'unblock' );
		$logEntry->setTarget( $page );
		$logEntry->setComment( $data['Reason'] );
		$logEntry->setPerformer( $performer );
		if ( isset( $data['Tags'] ) ) {
			$logEntry->setTags( $data['Tags'] );
		}
		$logId = $logEntry->insert();
		$logEntry->publish( $logId );

		return true;
	}

	/**
	 * Return an array of subpages beginning with $search that this special page will accept.
	 *
	 * @param string $search Prefix to search for
	 * @param int $limit Maximum number of results to return (usually 10)
	 * @param int $offset Number of results to skip (usually 0)
	 * @return string[] Matching subpages
	 */
	public function prefixSearchSubpages( $search, $limit, $offset ) {
		$user = User::newFromName( $search );
		if ( !$user ) {
			// No prefix suggestion for invalid user
			return [];
		}
		// Autocomplete subpage as user list - public to allow caching
		return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
	}

	protected function getGroupName() {
		return 'users';
	}
}