]> git.mxchange.org Git - friendica.git/blob - src/Model/Register.php
Merge pull request #10969 from MrPetovan/task/remove-private-contacts
[friendica.git] / src / Model / Register.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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          *
40          * @return array
41          * @throws \Exception
42          */
43         public static function getPending($start = 0, $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 The pending user information
54          *
55          * @throws \Exception
56          */
57         public static function getPendingForUser(int $uid)
58         {
59                 return DBA::selectFirst('pending-view', [], ['uid' => $uid, 'self' => true]);
60         }
61
62         /**
63          * Returns the pending registration count
64          *
65          * @return int
66          * @throws \Exception
67          */
68         public static function getPendingCount()
69         {
70                 return DBA::count('pending-view', ['self' => true]);
71         }
72
73         /**
74          * Returns the register record associated with the provided hash
75          *
76          * @param  string $hash
77          * @return array
78          * @throws \Exception
79          */
80         public static function getByHash($hash)
81         {
82                 return DBA::selectFirst('register', [], ['hash' => $hash]);
83         }
84
85         /**
86          * Returns true if a register record exists with the provided hash
87          *
88          * @param  string $hash
89          * @return boolean
90          * @throws \Exception
91          */
92         public static function existsByHash($hash)
93         {
94                 return DBA::exists('register', ['hash' => $hash]);
95         }
96
97         /**
98          * Creates a register record for an invitation and returns the auto-generated code for it
99          *
100          * @return string
101          * @throws \Exception
102          */
103         public static function createForInvitation()
104         {
105                 $code = Strings::getRandomName(8) . random_int(1000, 9999);
106
107                 $fields = [
108                         'hash' => $code,
109                         'created' => DateTimeFormat::utcNow()
110                 ];
111
112                 DBA::insert('register', $fields);
113
114                 return $code;
115         }
116
117         /**
118          * Creates a register record for approval and returns the success of the database insert
119          * Checks for the existence of the provided user id
120          *
121          * @param  integer $uid      The ID of the user needing approval
122          * @param  string  $language The registration language
123          * @param  string  $note     An additional message from the user
124          * @return boolean
125          * @throws \Exception
126          */
127         public static function createForApproval($uid, $language, $note = '')
128         {
129                 $hash = Strings::getRandomHex();
130
131                 if (!User::exists($uid)) {
132                         return false;
133                 }
134
135                 $fields = [
136                         'hash'     => $hash,
137                         'created'  => DateTimeFormat::utcNow(),
138                         'uid'      => $uid,
139                         'password' => '', // Obsolete, slated for deletion
140                         'language' => $language,
141                         'note'     => $note
142                 ];
143
144                 return DBA::insert('register', $fields);
145         }
146
147         /**
148          * Deletes a register record by the provided hash and returns the success of the database deletion
149          *
150          * @param  string $hash
151          * @return boolean
152          * @throws \Exception
153          */
154         public static function deleteByHash($hash)
155         {
156                 return DBA::delete('register', ['hash' => $hash]);
157         }
158 }