]> git.mxchange.org Git - friendica.git/blob - src/Model/Register.php
Admin page improvements
[friendica.git] / src / Model / Register.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 namespace Friendica\Model;
23
24 use Friendica\Database\DBA;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Util\Strings;
27
28 /**
29  * Class interacting with the register database table
30  */
31 class Register
32 {
33         /**
34          * Return the list of pending registrations
35          *
36          * @return array
37          * @throws \Exception
38          */
39         public static function getPending()
40         {
41                 $stmt = DBA::p(
42                         "SELECT `register`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`email`
43                         FROM `register`
44                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid`
45                         INNER JOIN `user` ON `register`.`uid` = `user`.`uid`"
46                 );
47
48                 return DBA::toArray($stmt);
49         }
50
51         /**
52          * Returns the pending user based on a given user id
53          *
54          * @param int $uid The user id
55          *
56          * @return array The pending user information
57          *
58          * @throws \Exception
59          */
60         public static function getPendingForUser(int $uid)
61         {
62                 return DBA::fetchFirst(
63                         "SELECT `register`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`email`
64                         FROM `register`
65                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid`
66                         INNER JOIN `user` ON `register`.`uid` = `user`.`uid`
67                         WHERE `register`.uid = ?",
68                         $uid
69                 );
70         }
71
72         /**
73          * Returns the pending registration count
74          *
75          * @return int
76          * @throws \Exception
77          */
78         public static function getPendingCount()
79         {
80                 $register = DBA::fetchFirst(
81                         "SELECT COUNT(*) AS `count`
82                         FROM `register`
83                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid` AND `contact`.`self`"
84                 );
85
86                 return $register['count'];
87         }
88
89         /**
90          * Returns the register record associated with the provided hash
91          *
92          * @param  string $hash
93          * @return array
94          * @throws \Exception
95          */
96         public static function getByHash($hash)
97         {
98                 return DBA::selectFirst('register', [], ['hash' => $hash]);
99         }
100
101         /**
102          * Returns true if a register record exists with the provided hash
103          *
104          * @param  string $hash
105          * @return boolean
106          * @throws \Exception
107          */
108         public static function existsByHash($hash)
109         {
110                 return DBA::exists('register', ['hash' => $hash]);
111         }
112
113         /**
114          * Creates a register record for an invitation and returns the auto-generated code for it
115          *
116          * @return string
117          * @throws \Exception
118          */
119         public static function createForInvitation()
120         {
121                 $code = Strings::getRandomName(8) . random_int(1000, 9999);
122
123                 $fields = [
124                         'hash' => $code,
125                         'created' => DateTimeFormat::utcNow()
126                 ];
127
128                 DBA::insert('register', $fields);
129
130                 return $code;
131         }
132
133         /**
134          * Creates a register record for approval and returns the success of the database insert
135          * Checks for the existence of the provided user id
136          *
137          * @param  integer $uid      The ID of the user needing approval
138          * @param  string  $language The registration language
139          * @param  string  $note     An additional message from the user
140          * @return boolean
141          * @throws \Exception
142          */
143         public static function createForApproval($uid, $language, $note = '')
144         {
145                 $hash = Strings::getRandomHex();
146
147                 if (!User::exists($uid)) {
148                         return false;
149                 }
150
151                 $fields = [
152                         'hash'     => $hash,
153                         'created'  => DateTimeFormat::utcNow(),
154                         'uid'      => $uid,
155                         'password' => '', // Obsolete, slated for deletion
156                         'language' => $language,
157                         'note'     => $note
158                 ];
159
160                 return DBA::insert('register', $fields);
161         }
162
163         /**
164          * Deletes a register record by the provided hash and returns the success of the database deletion
165          *
166          * @param  string $hash
167          * @return boolean
168          * @throws \Exception
169          */
170         public static function deleteByHash($hash)
171         {
172                 return DBA::delete('register', ['hash' => $hash]);
173         }
174 }