Fixed fatal error
[friendica.git/.git] / mod / regmod.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Worker;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Register;
27 use Friendica\Model\User;
28 use Friendica\Module\Security\Login;
29
30 function user_allow($hash)
31 {
32         $register = Register::getByHash($hash);
33         if (!DBA::isResult($register)) {
34                 return false;
35         }
36
37         $user = User::getById($register['uid']);
38         if (!DBA::isResult($user)) {
39                 exit();
40         }
41
42         Register::deleteByHash($hash);
43
44         DBA::update('user', ['blocked' => false, 'verified' => true], ['uid' => $register['uid']]);
45
46         $profile = DBA::selectFirst('profile', ['net-publish'], ['uid' => $register['uid']]);
47
48         if (DBA::isResult($profile) && $profile['net-publish'] && DI::config()->get('system', 'directory')) {
49                 $url = DI::baseUrl() . '/profile/' . $user['nickname'];
50                 Worker::add(PRIORITY_LOW, "Directory", $url);
51         }
52
53         $l10n = DI::l10n()->withLang($register['language']);
54
55         $res = User::sendRegisterOpenEmail(
56                 $l10n,
57                 $user,
58                 DI::config()->get('config', 'sitename'),
59                 DI::baseUrl()->get(),
60                 ($register['password'] ?? '') ?: 'Sent in a previous email'
61         );
62
63         if ($res) {
64                 info(DI::l10n()->t('Account approved.') . EOL);
65                 return true;
66         }
67 }
68
69 // This does not have to go through user_remove() and save the nickname
70 // permanently against re-registration, as the person was not yet
71 // allowed to have friends on this system
72 function user_deny($hash)
73 {
74         $register = Register::getByHash($hash);
75         if (!DBA::isResult($register)) {
76                 return false;
77         }
78
79         $user = User::getById($register['uid']);
80         if (!DBA::isResult($user)) {
81                 exit();
82         }
83
84         DBA::delete('user', ['uid' => $register['uid']]);
85
86         Register::deleteByHash($register['hash']);
87
88         notice(DI::l10n()->t('Registration revoked for %s', $user['username']) . EOL);
89         return true;
90 }
91
92 function regmod_content(App $a)
93 {
94         if (!local_user()) {
95                 info(DI::l10n()->t('Please login.') . EOL);
96                 return Login::form(DI::args()->getQueryString(), intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED ? 0 : 1);
97         }
98
99         if (!is_site_admin() || !empty($_SESSION['submanage'])) {
100                 notice(DI::l10n()->t('Permission denied.') . EOL);
101                 return '';
102         }
103
104         if ($a->argc != 3) {
105                 exit();
106         }
107
108         $cmd = $a->argv[1];
109         $hash = $a->argv[2];
110
111         if ($cmd === 'deny') {
112                 user_deny($hash);
113                 DI::baseUrl()->redirect('admin/users/');
114         }
115
116         if ($cmd === 'allow') {
117                 user_allow($hash);
118                 DI::baseUrl()->redirect('admin/users/');
119         }
120 }