Merge "Fix escaping for MSSQL LIKE queries."
[lhc/web/wiklou.git] / includes / specials / SpecialBotPasswords.php
1 <?php
2 /**
3 * Implements Special:BotPasswords
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Let users manage bot passwords
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialBotPasswords extends FormSpecialPage {
30
31 /** @var int Central user ID */
32 private $userId = 0;
33
34 /** @var BotPassword|null Bot password being edited, if any */
35 private $botPassword = null;
36
37 /** @var string Operation being performed: create, update, delete */
38 private $operation = null;
39
40 /** @var string New password set, for communication between onSubmit() and onSuccess() */
41 private $password = null;
42
43 public function __construct() {
44 parent::__construct( 'BotPasswords', 'editmyprivateinfo' );
45 }
46
47 /**
48 * @return bool
49 */
50 public function isListed() {
51 return $this->getConfig()->get( 'EnableBotPasswords' );
52 }
53
54 /**
55 * Main execution point
56 * @param string|null $par
57 */
58 function execute( $par ) {
59 $this->getOutput()->disallowUserJs();
60 $this->requireLogin();
61
62 $par = trim( $par );
63 if ( strlen( $par ) === 0 ) {
64 $par = null;
65 } elseif ( strlen( $par ) > BotPassword::APPID_MAXLENGTH ) {
66 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
67 [ htmlspecialchars( $par ) ] );
68 }
69
70 parent::execute( $par );
71 }
72
73 protected function checkExecutePermissions( User $user ) {
74 parent::checkExecutePermissions( $user );
75
76 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
77 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
78 }
79
80 $this->userId = CentralIdLookup::factory()->centralIdFromLocalUser( $this->getUser() );
81 if ( !$this->userId ) {
82 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
83 }
84 }
85
86 protected function getFormFields() {
87 $fields = [];
88
89 if ( $this->par !== null ) {
90 $this->botPassword = BotPassword::newFromCentralId( $this->userId, $this->par );
91 if ( !$this->botPassword ) {
92 $this->botPassword = BotPassword::newUnsaved( [
93 'centralId' => $this->userId,
94 'appId' => $this->par,
95 ] );
96 }
97
98 $sep = BotPassword::getSeparator();
99 $fields[] = [
100 'type' => 'info',
101 'label-message' => 'username',
102 'default' => $this->getUser()->getName() . $sep . $this->par
103 ];
104
105 if ( $this->botPassword->isSaved() ) {
106 $fields['resetPassword'] = [
107 'type' => 'check',
108 'label-message' => 'botpasswords-label-resetpassword',
109 ];
110 }
111
112 $lang = $this->getLanguage();
113 $showGrants = MWGrants::getValidGrants();
114 $fields['grants'] = [
115 'type' => 'checkmatrix',
116 'label-message' => 'botpasswords-label-grants',
117 'help-message' => 'botpasswords-help-grants',
118 'columns' => [
119 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
120 ],
121 'rows' => array_combine(
122 array_map( 'MWGrants::getGrantsLink', $showGrants ),
123 $showGrants
124 ),
125 'default' => array_map(
126 function( $g ) {
127 return "grant-$g";
128 },
129 $this->botPassword->getGrants()
130 ),
131 'tooltips' => array_combine(
132 array_map( 'MWGrants::getGrantsLink', $showGrants ),
133 array_map(
134 function( $rights ) use ( $lang ) {
135 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
136 },
137 array_intersect_key( MWGrants::getRightsByGrant(), array_flip( $showGrants ) )
138 )
139 ),
140 'force-options-on' => array_map(
141 function( $g ) {
142 return "grant-$g";
143 },
144 MWGrants::getHiddenGrants()
145 ),
146 ];
147
148 $fields['restrictions'] = [
149 'type' => 'textarea',
150 'label-message' => 'botpasswords-label-restrictions',
151 'required' => true,
152 'default' => $this->botPassword->getRestrictions()->toJson( true ),
153 'rows' => 5,
154 'validation-callback' => function ( $v ) {
155 try {
156 MWRestrictions::newFromJson( $v );
157 return true;
158 } catch ( InvalidArgumentException $ex ) {
159 return $ex->getMessage();
160 }
161 },
162 ];
163
164 } else {
165 $dbr = BotPassword::getDB( DB_SLAVE );
166 $res = $dbr->select(
167 'bot_passwords',
168 [ 'bp_app_id' ],
169 [ 'bp_user' => $this->userId ],
170 __METHOD__
171 );
172 foreach ( $res as $row ) {
173 $fields[] = [
174 'section' => 'existing',
175 'type' => 'info',
176 'raw' => true,
177 'default' => Linker::link(
178 $this->getPageTitle( $row->bp_app_id ),
179 htmlspecialchars( $row->bp_app_id ),
180 [],
181 [],
182 [ 'known' ]
183 ),
184 ];
185 }
186
187 $fields['appId'] = [
188 'section' => 'createnew',
189 'type' => 'textwithbutton',
190 'label-message' => 'botpasswords-label-appid',
191 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
192 'buttonflags' => [ 'progressive', 'primary' ],
193 'required' => true,
194 'size' => BotPassword::APPID_MAXLENGTH,
195 'maxlength' => BotPassword::APPID_MAXLENGTH,
196 'validation-callback' => function ( $v ) {
197 $v = trim( $v );
198 return $v !== '' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH;
199 },
200 ];
201
202 $fields[] = [
203 'type' => 'hidden',
204 'default' => 'new',
205 'name' => 'op',
206 ];
207 }
208
209 return $fields;
210 }
211
212 protected function alterForm( HTMLForm $form ) {
213 $form->setId( 'mw-botpasswords-form' );
214 $form->setTableId( 'mw-botpasswords-table' );
215 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
216 $form->suppressDefaultSubmit();
217
218 if ( $this->par !== null ) {
219 if ( $this->botPassword->isSaved() ) {
220 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
221 $form->addButton( [
222 'name' => 'op',
223 'value' => 'update',
224 'label-message' => 'botpasswords-label-update',
225 'flags' => [ 'primary', 'progressive' ],
226 ] );
227 $form->addButton( [
228 'name' => 'op',
229 'value' => 'delete',
230 'label-message' => 'botpasswords-label-delete',
231 'flags' => [ 'destructive' ],
232 ] );
233 } else {
234 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
235 $form->addButton( [
236 'name' => 'op',
237 'value' => 'create',
238 'label-message' => 'botpasswords-label-create',
239 'flags' => [ 'primary', 'constructive' ],
240 ] );
241 }
242
243 $form->addButton( [
244 'name' => 'op',
245 'value' => 'cancel',
246 'label-message' => 'botpasswords-label-cancel'
247 ] );
248 }
249 }
250
251 public function onSubmit( array $data ) {
252 $op = $this->getRequest()->getVal( 'op', '' );
253
254 switch ( $op ) {
255 case 'new':
256 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
257 return false;
258
259 case 'create':
260 $this->operation = 'insert';
261 return $this->save( $data );
262
263 case 'update':
264 $this->operation = 'update';
265 return $this->save( $data );
266
267 case 'delete':
268 $this->operation = 'delete';
269 $bp = BotPassword::newFromCentralId( $this->userId, $this->par );
270 if ( $bp ) {
271 $bp->delete();
272 }
273 return Status::newGood();
274
275 case 'cancel':
276 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
277 return false;
278 }
279
280 return false;
281 }
282
283 private function save( array $data ) {
284 $bp = BotPassword::newUnsaved( [
285 'centralId' => $this->userId,
286 'appId' => $this->par,
287 'restrictions' => MWRestrictions::newFromJson( $data['restrictions'] ),
288 'grants' => array_merge(
289 MWGrants::getHiddenGrants(),
290 preg_replace( '/^grant-/', '', $data['grants'] )
291 )
292 ] );
293
294 if ( $this->operation === 'insert' || !empty( $data['resetPassword'] ) ) {
295 $this->password = PasswordFactory::generateRandomPasswordString(
296 max( 32, $this->getConfig()->get( 'MinimalPasswordLength' ) )
297 );
298 $passwordFactory = new PasswordFactory();
299 $passwordFactory->init( RequestContext::getMain()->getConfig() );
300 $password = $passwordFactory->newFromPlaintext( $this->password );
301 } else {
302 $password = null;
303 }
304
305 if ( $bp->save( $this->operation, $password ) ) {
306 return Status::newGood();
307 } else {
308 // Messages: botpasswords-insert-failed, botpasswords-update-failed
309 return Status::newFatal( "botpasswords-{$this->operation}-failed", $this->par );
310 }
311 }
312
313 public function onSuccess() {
314 $out = $this->getOutput();
315
316 $username = $this->getUser()->getName();
317 switch ( $this->operation ) {
318 case 'insert':
319 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
320 $out->addWikiMsg( 'botpasswords-created-body', $this->par, $username );
321 break;
322
323 case 'update':
324 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
325 $out->addWikiMsg( 'botpasswords-updated-body', $this->par, $username );
326 break;
327
328 case 'delete':
329 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
330 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par, $username );
331 $this->password = null;
332 break;
333 }
334
335 if ( $this->password !== null ) {
336 $sep = BotPassword::getSeparator();
337 $out->addWikiMsg(
338 'botpasswords-newpassword',
339 htmlspecialchars( $username . $sep . $this->par ),
340 htmlspecialchars( $this->password )
341 );
342 $this->password = null;
343 }
344
345 $out->addReturnTo( $this->getPageTitle() );
346 }
347
348 protected function getGroupName() {
349 return 'users';
350 }
351
352 protected function getDisplayFormat() {
353 return 'ooui';
354 }
355 }