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