db94fd5b93d62927fd102ce34d78b20081f544e9
[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( '', $this->getMain()->getRequest() );
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' => is_null( $params['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 $res['user'] = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
82 $res['reason'] = $params['reason'];
83 $this->getResult()->addValue( null, $this->getModuleName(), $res );
84 }
85
86 public function mustBePosted() {
87 return true;
88 }
89
90 public function isWriteMode() {
91 return true;
92 }
93
94 public function getAllowedParams() {
95 return array(
96 'id' => array(
97 ApiBase::PARAM_TYPE => 'integer',
98 ),
99 'user' => null,
100 'token' => null,
101 'gettoken' => false,
102 'reason' => null,
103 );
104 }
105
106 public function getParamDescription() {
107 $p = $this->getModulePrefix();
108 return array(
109 'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
110 'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
111 'token' => "An unblock token previously obtained through the gettoken parameter or {$p}prop=info",
112 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
113 'reason' => 'Reason for unblock (optional)',
114 );
115 }
116
117 public function getDescription() {
118 return 'Unblock a user';
119 }
120
121 public function getPossibleErrors() {
122 return array_merge( parent::getPossibleErrors(), array(
123 array( 'unblock-notarget' ),
124 array( 'unblock-idanduser' ),
125 array( 'cantunblock' ),
126 array( 'ipbblocked' ),
127 array( 'ipbnounblockself' ),
128 ) );
129 }
130
131 public function needsToken() {
132 return true;
133 }
134
135 public function getTokenSalt() {
136 return '';
137 }
138
139 public function getExamples() {
140 return array(
141 'api.php?action=unblock&id=105',
142 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
143 );
144 }
145
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/API:Block';
148 }
149
150 public function getVersion() {
151 return __CLASS__ . ': $Id$';
152 }
153 }