]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/NewPassword.php
Merge pull request #4863 from MrPetovan/task/4860-add-hidden-input-in-console
[friendica.git] / src / Core / Console / NewPassword.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core\L10n;
6 use Friendica\Model\Contact;
7 use Friendica\Model\User;
8 use Friendica\Core\Config;
9 use Friendica\Database\DBM;
10 use dba;
11
12 /**
13  * @brief tool to set a new password for a user
14  *
15  * With this tool, you can set a new password for a user
16  *
17  * License: AGPLv3 or later, same as Friendica
18  *
19  * @author Michael Vogel <heluecht@pirati.ca>
20  */
21 class NewPassword extends \Asika\SimpleConsole\Console
22 {
23         protected $helpOptions = ['h', 'help', '?'];
24
25         protected function getHelp()
26         {
27                 $help = <<<HELP
28 console newpassword - Creates a new password for a given user
29 Usage
30         bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
31
32 Description
33         Creates a new password for a user without using the "forgot password" functionality.
34
35 Options
36     -h|--help|-? Show help information
37     -v           Show more debug information.
38 HELP;
39                 return $help;
40         }
41
42         protected function doExecute()
43         {
44                 $a = get_app();
45
46                 if ($this->getOption('v')) {
47                         $this->out('Class: ' . __CLASS__);
48                         $this->out('Arguments: ' . var_export($this->args, true));
49                         $this->out('Options: ' . var_export($this->options, true));
50                 }
51
52                 if (count($this->args) == 0) {
53                         $this->out($this->getHelp());
54                         return 0;
55                 }
56
57                 if (count($this->args) > 2) {
58                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
59                 }
60
61                 require_once '.htconfig.php';
62                 $result = \dba::connect($db_host, $db_user, $db_pass, $db_data);
63                 unset($db_host, $db_user, $db_pass, $db_data);
64
65                 if (!$result) {
66                         throw new \RuntimeException('Unable to connect to database');
67                 }
68
69                 $nick = $this->getArgument(0);
70
71                 $user = dba::selectFirst('user', ['uid'], ['nickname' => $nick]);
72                 if (!DBM::is_result($user)) {
73                         throw new \RuntimeException(L10n::t('User not found'));
74                 }
75
76                 $password = $this->getArgument(1);
77                 if (is_null($password)) {
78                         $this->out(L10n::t('Enter new password: '), false);
79                         $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
80                 }
81
82                 if (!$password) {
83                         throw new \RuntimeException(L10n::t('Password can\'t be empty'));
84                 }
85
86                 if (!Config::get('system', 'disable_password_exposed', false) && User::isPasswordExposed($password)) {
87                         throw new \RuntimeException(L10n::t('The new password has been exposed in a public data dump, please choose another.'));
88                 }
89
90                 if (!User::updatePassword($user['uid'], $password)) {
91                         throw new \RuntimeException(L10n::t('Password update failed. Please try again.'));
92                 }
93
94                 $this->out(L10n::t('Password changed.'));
95
96                 return 0;
97         }
98 }