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