Adding support for domains in SpecialPasswordReset.php.
[lhc/web/wiklou.git] / includes / specials / SpecialUnblock.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup SpecialPage
20 */
21
22 /**
23 * A special page for unblocking users
24 *
25 * @ingroup SpecialPage
26 */
27 class SpecialUnblock extends SpecialPage {
28
29 protected $target;
30 protected $type;
31 protected $block;
32
33 public function __construct(){
34 parent::__construct( 'Unblock', 'block' );
35 }
36
37 public function execute( $par ){
38 # Check permissions
39 if( !$this->userCanExecute( $this->getUser() ) ) {
40 $this->displayRestrictionError();
41 return;
42 }
43
44 # Check for database lock
45 if( wfReadOnly() ) {
46 throw new ReadOnlyError;
47 }
48
49 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
50 $this->block = Block::newFromTarget( $this->target );
51
52 $out = $this->getOutput();
53 $out->setPageTitle( wfMsg( 'unblockip' ) );
54 $out->addModules( 'mediawiki.special' );
55
56 $form = new HTMLForm( $this->getFields(), $this->getContext() );
57 $form->setWrapperLegend( wfMsg( 'unblockip' ) );
58 $form->setSubmitCallback( array( __CLASS__, 'processUnblock' ) );
59 $form->setSubmitText( wfMsg( 'ipusubmit' ) );
60 $form->addPreText( wfMsgExt( 'unblockiptext', 'parse' ) );
61
62 if( $form->show() ){
63 switch( $this->type ){
64 case Block::TYPE_USER:
65 case Block::TYPE_IP:
66 $out->addWikiMsg( 'unblocked', $this->target );
67 break;
68 case Block::TYPE_RANGE:
69 $out->addWikiMsg( 'unblocked-range', $this->target );
70 break;
71 case Block::TYPE_ID:
72 case Block::TYPE_AUTO:
73 $out->addWikiMsg( 'unblocked-id', $this->target );
74 break;
75 }
76 }
77 }
78
79 protected function getFields(){
80 $fields = array(
81 'Target' => array(
82 'type' => 'text',
83 'label-message' => 'ipadressorusername',
84 'tabindex' => '1',
85 'size' => '45',
86 'required' => true,
87 ),
88 'Name' => array(
89 'type' => 'info',
90 'label-message' => 'ipadressorusername',
91 ),
92 'Reason' => array(
93 'type' => 'text',
94 'label-message' => 'ipbreason',
95 )
96 );
97
98 if( $this->block instanceof Block ){
99 list( $target, $type ) = $this->block->getTargetAndType();
100
101 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
102 # User:Foo, and we've just got any block, auto or not, that applies to a target
103 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
104 # so don't show any distinction between unblocked IPs and autoblocked IPs
105 if( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ){
106 $fields['Target']['default'] = $this->target;
107 unset( $fields['Name'] );
108
109 } else {
110 $fields['Target']['default'] = $target;
111 $fields['Target']['type'] = 'hidden';
112 switch( $type ){
113 case Block::TYPE_USER:
114 case Block::TYPE_IP:
115 $fields['Name']['default'] = Linker::link(
116 $target->getUserPage(),
117 $target->getName()
118 );
119 $fields['Name']['raw'] = true;
120 break;
121
122 case Block::TYPE_RANGE:
123 $fields['Name']['default'] = $target;
124 break;
125
126 case Block::TYPE_AUTO:
127 $fields['Name']['default'] = $this->block->getRedactedName();
128 $fields['Name']['raw'] = true;
129 # Don't expose the real target of the autoblock
130 $fields['Target']['default'] = "#{$this->target}";
131 break;
132 }
133 }
134
135 } else {
136 $fields['Target']['default'] = $this->target;
137 unset( $fields['Name'] );
138 }
139 return $fields;
140 }
141
142 /**
143 * Process the form
144 * @return Array( Array(message key, parameters) ) on failure, True on success
145 */
146 public static function processUnblock( array $data ){
147 global $wgUser;
148
149 $target = $data['Target'];
150 $block = Block::newFromTarget( $data['Target'] );
151
152 if( !$block instanceof Block ){
153 return array( array( 'ipb_cant_unblock', $target ) );
154 }
155
156 # bug 15810: blocked admins should have limited access here. This
157 # won't allow sysops to remove autoblocks on themselves, but they
158 # should have ipblock-exempt anyway
159 $status = SpecialBlock::checkUnblockSelf( $target );
160 if ( $status !== true ) {
161 throw new ErrorPageError( 'badaccess', $status );
162 }
163
164 # If the specified IP is a single address, and the block is a range block, don't
165 # unblock the whole range.
166 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
167 if( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
168 $range = $block->getTarget();
169 return array( array( 'ipb_blocked_as_range', $target, $range ) );
170 }
171
172 # If the name was hidden and the blocking user cannot hide
173 # names, then don't allow any block removals...
174 if( !$wgUser->isAllowed( 'hideuser' ) && $block->mHideName ) {
175 return array( 'unblock-hideuser' );
176 }
177
178 # Delete block
179 if ( !$block->delete() ) {
180 return array( 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) );
181 }
182
183 # Unset _deleted fields as needed
184 if( $block->mHideName ) {
185 # Something is deeply FUBAR if this is not a User object, but who knows?
186 $id = $block->getTarget() instanceof User
187 ? $block->getTarget()->getID()
188 : User::idFromName( $block->getTarget() );
189
190 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
191 }
192
193 # Make log entry
194 $log = new LogPage( 'block' );
195 $page = $block->getTarget() instanceof User
196 ? $block->getTarget()->getUserpage()
197 : Title::makeTitle( NS_USER, $block->getTarget() );
198 $log->addEntry( 'unblock', $page, $data['Reason'] );
199
200 return true;
201 }
202 }