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