]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/NewPassword.php
Avoid memory issue in exception
[friendica.git] / src / Core / Console / NewPassword.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core\L10n;
6 use Friendica\Database\DBA;
7 use Friendica\Model\User;
8 use RuntimeException;
9
10 /**
11  * @brief tool to set a new password for a user
12  *
13  * With this tool, you can set a new password for a user
14  *
15  * License: AGPLv3 or later, same as Friendica
16  *
17  * @author Michael Vogel <heluecht@pirati.ca>
18  */
19 class NewPassword extends \Asika\SimpleConsole\Console
20 {
21         protected $helpOptions = ['h', 'help', '?'];
22
23         protected function getHelp()
24         {
25                 $help = <<<HELP
26 console newpassword - Creates a new password for a given user
27 Usage
28         bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
29
30 Description
31         Creates a new password for a user without using the "forgot password" functionality.
32
33 Options
34     -h|--help|-? Show help information
35     -v           Show more debug information.
36 HELP;
37                 return $help;
38         }
39
40         protected function doExecute()
41         {
42                 $a = \get_app();
43
44                 if ($this->getOption('v')) {
45                         $this->out('Class: ' . __CLASS__);
46                         $this->out('Arguments: ' . var_export($this->args, true));
47                         $this->out('Options: ' . var_export($this->options, true));
48                 }
49
50                 if (count($this->args) == 0) {
51                         $this->out($this->getHelp());
52                         return 0;
53                 }
54
55                 if (count($this->args) > 2) {
56                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57                 }
58
59                 if ($a->getMode()->isInstall()) {
60                         throw new RuntimeException('Database isn\'t ready or populated yet');
61                 }
62
63                 $nick = $this->getArgument(0);
64
65                 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
66                 if (!DBA::isResult($user)) {
67                         throw new RuntimeException(L10n::t('User not found'));
68                 }
69
70                 $password = $this->getArgument(1);
71                 if (is_null($password)) {
72                         $this->out(L10n::t('Enter new password: '), false);
73                         $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
74                 }
75
76                 try {
77                         $result = User::updatePassword($user['uid'], $password);
78
79                         if (!DBA::isResult($result)) {
80                                 throw new \Exception(L10n::t('Password update failed. Please try again.'));
81                         }
82
83                         $this->out(L10n::t('Password changed.'));
84                 } catch (\Exception $e) {
85                         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
86                 }
87
88                 return 0;
89         }
90 }