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