Followup to r64228 - apply restrictions in API.
[lhc/web/wiklou.git] / includes / api / ApiUnblock.php
1 <?php
2
3 /**
4 * Created on Sep 7, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * API module that facilitates the unblocking of users. Requires API write mode
32 * to be enabled.
33 *
34 * @ingroup API
35 */
36 class ApiUnblock extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 /**
43 * Unblocks the specified user or provides the reason the unblock failed.
44 */
45 public function execute() {
46 global $wgUser;
47 $params = $this->extractRequestParams();
48
49 if ( $params['gettoken'] ) {
50 $res['unblocktoken'] = $wgUser->editToken();
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
52 return;
53 }
54
55 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) {
56 $this->dieUsageMsg( array( 'unblock-notarget' ) );
57 }
58 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) {
59 $this->dieUsageMsg( array( 'unblock-idanduser' ) );
60 }
61
62 if ( !$wgUser->isAllowed( 'block' ) ) {
63 $this->dieUsageMsg( array( 'cantunblock' ) );
64 }
65 # bug 15810: blocked admins should have limited access here
66 if( $wgUser->isBlocked() ){
67 $user = User::newFromName( $params['user'] );
68 if( $user instanceof User
69 && $user->getId() == $wgUser->getId() )
70 {
71 # User is trying to unblock themselves
72 if( !$wgUser->isAllowed( 'unblockself' ) ){
73 $this->dieUsageMsg( array( 'ipbnounblockself' ) );
74 }
75 } else {
76 # User is trying to block/unblock someone else
77 $this->dieUsageMsg( array( 'ipbblocked' ) );
78 }
79 }
80
81 $id = $params['id'];
82 $user = $params['user'];
83 $reason = ( is_null( $params['reason'] ) ? '' : $params['reason'] );
84 $retval = IPUnblockForm::doUnblock( $id, $user, $reason, $range );
85 if ( $retval ) {
86 $this->dieUsageMsg( $retval );
87 }
88
89 $res['id'] = intval( $id );
90 $res['user'] = $user;
91 $res['reason'] = $reason;
92 $this->getResult()->addValue( null, $this->getModuleName(), $res );
93 }
94
95 public function mustBePosted() {
96 return true;
97 }
98
99 public function isWriteMode() {
100 return true;
101 }
102
103 public function getAllowedParams() {
104 return array(
105 'id' => null,
106 'user' => null,
107 'token' => null,
108 'gettoken' => false,
109 'reason' => null,
110 );
111 }
112
113 public function getParamDescription() {
114 return array(
115 'id' => 'ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with user',
116 'user' => 'Username, IP address or IP range you want to unblock. Cannot be used together with id',
117 'token' => 'An unblock token previously obtained through the gettoken parameter or prop=info',
118 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
119 'reason' => 'Reason for unblock (optional)',
120 );
121 }
122
123 public function getDescription() {
124 return array(
125 'Unblock a user.'
126 );
127 }
128
129 public function getPossibleErrors() {
130 return array_merge( parent::getPossibleErrors(), array(
131 array( 'unblock-notarget' ),
132 array( 'unblock-idanduser' ),
133 array( 'cantunblock' ),
134 array( 'ipbblocked' ),
135 array( 'ipbnounblockself' ),
136 ) );
137 }
138
139 public function getTokenSalt() {
140 return '';
141 }
142
143 protected function getExamples() {
144 return array(
145 'api.php?action=unblock&id=105',
146 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
147 );
148 }
149
150 public function getVersion() {
151 return __CLASS__ . ': $Id$';
152 }
153 }