5802a982e5acfa5c349e1c758e71b02b77c794c6
[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 /**
28 * API module that facilitates the unblocking of users. Requires API write mode
29 * to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiUnblock extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 /**
40 * Unblocks the specified user or provides the reason the unblock failed.
41 */
42 public function execute() {
43 $user = $this->getUser();
44 $params = $this->extractRequestParams();
45
46 if ( $params['gettoken'] ) {
47 $res['unblocktoken'] = $user->getEditToken();
48 $this->getResult()->addValue( null, $this->getModuleName(), $res );
49 return;
50 }
51
52 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) {
53 $this->dieUsageMsg( 'unblock-notarget' );
54 }
55 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) {
56 $this->dieUsageMsg( 'unblock-idanduser' );
57 }
58
59 if ( !$user->isAllowed( 'block' ) ) {
60 $this->dieUsageMsg( 'cantunblock' );
61 }
62 # bug 15810: blocked admins should have limited access here
63 if ( $user->isBlocked() ) {
64 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
65 if ( $status !== true ) {
66 $this->dieUsageMsg( $status );
67 }
68 }
69
70 $data = array(
71 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
72 'Reason' => $params['reason']
73 );
74 $block = Block::newFromTarget( $data['Target'] );
75 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
76 if ( $retval !== true ) {
77 $this->dieUsageMsg( $retval[0] );
78 }
79
80 $res['id'] = $block->getId();
81 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
82 $res['user'] = $target;
83 $res['userid'] = $target instanceof User ? $target->getId() : 0;
84 $res['reason'] = $params['reason'];
85 $this->getResult()->addValue( null, $this->getModuleName(), $res );
86 }
87
88 public function mustBePosted() {
89 return true;
90 }
91
92 public function isWriteMode() {
93 return true;
94 }
95
96 public function getAllowedParams() {
97 return array(
98 'id' => array(
99 ApiBase::PARAM_TYPE => 'integer',
100 ),
101 'user' => null,
102 'token' => null,
103 'gettoken' => array(
104 ApiBase::PARAM_DFLT => false,
105 ApiBase::PARAM_DEPRECATED => true,
106 ),
107 'reason' => '',
108 );
109 }
110
111 public function getParamDescription() {
112 $p = $this->getModulePrefix();
113 return array(
114 'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
115 'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
116 'token' => "An unblock token previously obtained through prop=info",
117 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
118 'reason' => 'Reason for unblock',
119 );
120 }
121
122 public function getResultProperties() {
123 return array(
124 '' => array(
125 'unblocktoken' => array(
126 ApiBase::PROP_TYPE => 'string',
127 ApiBase::PROP_NULLABLE => true
128 ),
129 'id' => array(
130 ApiBase::PROP_TYPE => 'integer',
131 ApiBase::PROP_NULLABLE => true
132 ),
133 'user' => array(
134 ApiBase::PROP_TYPE => 'string',
135 ApiBase::PROP_NULLABLE => true
136 ),
137 'userid' => array(
138 ApiBase::PROP_TYPE => 'integer',
139 ApiBase::PROP_NULLABLE => true
140 ),
141 'reason' => array(
142 ApiBase::PROP_TYPE => 'string',
143 ApiBase::PROP_NULLABLE => true
144 )
145 )
146 );
147 }
148
149 public function getDescription() {
150 return 'Unblock a user';
151 }
152
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'unblock-notarget' ),
156 array( 'unblock-idanduser' ),
157 array( 'cantunblock' ),
158 array( 'ipbblocked' ),
159 array( 'ipbnounblockself' ),
160 ) );
161 }
162
163 public function needsToken() {
164 return true;
165 }
166
167 public function getTokenSalt() {
168 return '';
169 }
170
171 public function getExamples() {
172 return array(
173 'api.php?action=unblock&id=105',
174 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
175 );
176 }
177
178 public function getHelpUrls() {
179 return 'https://www.mediawiki.org/wiki/API:Block';
180 }
181 }