Don't pass $this by reference
[lhc/web/wiklou.git] / includes / api / ApiUnblock.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API module that facilitates the unblocking of users. Requires API write mode
34 * to be enabled.
35 *
36 * @ingroup API
37 */
38 class ApiUnblock extends ApiBase {
39
40 public function __construct( $main, $action ) {
41 parent::__construct( $main, $action );
42 }
43
44 /**
45 * Unblocks the specified user or provides the reason the unblock failed.
46 */
47 public function execute() {
48 global $wgUser;
49 $params = $this->extractRequestParams();
50
51 if ( $params['gettoken'] ) {
52 $res['unblocktoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
53 $this->getResult()->addValue( null, $this->getModuleName(), $res );
54 return;
55 }
56
57 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) {
58 $this->dieUsageMsg( array( 'unblock-notarget' ) );
59 }
60 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) {
61 $this->dieUsageMsg( array( 'unblock-idanduser' ) );
62 }
63
64 if ( !$wgUser->isAllowed( 'block' ) ) {
65 $this->dieUsageMsg( array( 'cantunblock' ) );
66 }
67 # bug 15810: blocked admins should have limited access here
68 if ( $wgUser->isBlocked() ) {
69 $status = SpecialBlock::checkUnblockSelf( $params['user'] );
70 if ( $status !== true ) {
71 $this->dieUsageMsg( array( $status ) );
72 }
73 }
74
75 $data = array(
76 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
77 'Reason' => is_null( $params['reason'] ) ? '' : $params['reason']
78 );
79 $block = Block::newFromTarget( $data['Target'] );
80 $retval = SpecialUnblock::processUnblock( $data );
81 if ( $retval !== true ) {
82 $this->dieUsageMsg( $retval[0] );
83 }
84
85 $res['id'] = $block->getId();
86 $res['user'] = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
87 $res['reason'] = $params['reason'];
88 $this->getResult()->addValue( null, $this->getModuleName(), $res );
89 }
90
91 public function mustBePosted() {
92 return true;
93 }
94
95 public function isWriteMode() {
96 return true;
97 }
98
99 public function getAllowedParams() {
100 return array(
101 'id' => null,
102 'user' => null,
103 'token' => null,
104 'gettoken' => false,
105 'reason' => null,
106 );
107 }
108
109 public function getParamDescription() {
110 $p = $this->getModulePrefix();
111 return array(
112 'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
113 'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
114 'token' => "An unblock token previously obtained through the gettoken parameter or {$p}prop=info",
115 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
116 'reason' => 'Reason for unblock (optional)',
117 );
118 }
119
120 public function getDescription() {
121 return 'Unblock a user';
122 }
123
124 public function getPossibleErrors() {
125 return array_merge( parent::getPossibleErrors(), array(
126 array( 'unblock-notarget' ),
127 array( 'unblock-idanduser' ),
128 array( 'cantunblock' ),
129 array( 'ipbblocked' ),
130 array( 'ipbnounblockself' ),
131 ) );
132 }
133
134 public function needsToken() {
135 return true;
136 }
137
138 public function getTokenSalt() {
139 return '';
140 }
141
142 protected function getExamples() {
143 return array(
144 'api.php?action=unblock&id=105',
145 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
146 );
147 }
148
149 public function getVersion() {
150 return __CLASS__ . ': $Id$';
151 }
152 }