]> git.mxchange.org Git - friendica.git/blob - src/Model/Register.php
Merge remote-tracking branch 'upstream/develop' into json-ld
[friendica.git] / src / Model / Register.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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 namespace Friendica\Model;
23
24 use Friendica\Content\Pager;
25 use Friendica\Database\DBA;
26 use Friendica\Util\DateTimeFormat;
27 use Friendica\Util\Strings;
28
29 /**
30  * Class interacting with the register database table
31  */
32 class Register
33 {
34         /**
35          * Return the list of pending registrations
36          *
37          * @param int $start Start count (Default is 0)
38          * @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE)
39          * @return array|bool Array on succes, false on failure
40          * @throws \Exception
41          */
42         public static function getPending(int $start = 0, int $count = Pager::ITEMS_PER_PAGE)
43         {
44                 return DBA::selectToArray('pending-view', [], [], ['limit' => [$start, $count]]);
45         }
46
47         /**
48          * Returns the pending user based on a given user id
49          *
50          * @param int $uid The user id
51          *
52          * @return array|bool Array on succes, false on failure
53          * @throws \Exception
54          */
55         public static function getPendingForUser(int $uid)
56         {
57                 return DBA::selectFirst('pending-view', [], ['uid' => $uid, 'self' => true]);
58         }
59
60         /**
61          * Returns the pending registration count
62          *
63          * @return int
64          * @throws \Exception
65          */
66         public static function getPendingCount(): int
67         {
68                 return DBA::count('pending-view', ['self' => true]);
69         }
70
71         /**
72          * Returns the register record associated with the provided hash
73          *
74          * @param  string $hash
75          * @return array|bool Array on succes, false on failure
76          * @throws \Exception
77          */
78         public static function getByHash(string $hash)
79         {
80                 return DBA::selectFirst('register', [], ['hash' => $hash]);
81         }
82
83         /**
84          * Returns true if a register record exists with the provided hash
85          *
86          * @param  string $hash
87          * @return boolean
88          * @throws \Exception
89          */
90         public static function existsByHash(string $hash): bool
91         {
92                 return DBA::exists('register', ['hash' => $hash]);
93         }
94
95         /**
96          * Creates a register record for an invitation and returns the auto-generated code for it
97          *
98          * @return string
99          * @throws \Exception
100          */
101         public static function createForInvitation(): string
102         {
103                 $code = Strings::getRandomName(8) . random_int(1000, 9999);
104
105                 $fields = [
106                         'hash' => $code,
107                         'created' => DateTimeFormat::utcNow()
108                 ];
109
110                 DBA::insert('register', $fields);
111
112                 return $code;
113         }
114
115         /**
116          * Creates a register record for approval and returns the success of the database insert
117          * Checks for the existence of the provided user id
118          *
119          * @param  integer $uid      The ID of the user needing approval
120          * @param  string  $language The registration language
121          * @param  string  $note     An additional message from the user
122          * @return boolean
123          * @throws \Exception
124          */
125         public static function createForApproval(int $uid, string $language, string $note = ''): bool
126         {
127                 $hash = Strings::getRandomHex();
128
129                 if (!User::exists($uid)) {
130                         return false;
131                 }
132
133                 $fields = [
134                         'hash'     => $hash,
135                         'created'  => DateTimeFormat::utcNow(),
136                         'uid'      => $uid,
137                         'password' => '', // Obsolete, slated for deletion
138                         'language' => $language,
139                         'note'     => $note
140                 ];
141
142                 return DBA::insert('register', $fields);
143         }
144
145         /**
146          * Deletes a register record by the provided hash and returns the success of the database deletion
147          *
148          * @param  string $hash
149          * @return boolean
150          * @throws \Exception
151          */
152         public static function deleteByHash(string $hash): bool
153         {
154                 return DBA::delete('register', ['hash' => $hash]);
155         }
156 }