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