]> git.mxchange.org Git - friendica.git/blob - src/Model/Register.php
Expand system.no_auto_update to community, profile statuses and contact conversations...
[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 registration count
53          *
54          * @return int
55          * @throws \Exception
56          */
57         public static function getPendingCount()
58         {
59                 $register = DBA::fetchFirst(
60                         "SELECT COUNT(*) AS `count`
61                         FROM `register`
62                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid` AND `contact`.`self`"
63                 );
64
65                 return $register['count'];
66         }
67
68         /**
69          * Returns the register record associated with the provided hash
70          *
71          * @param  string $hash
72          * @return array
73          * @throws \Exception
74          */
75         public static function getByHash($hash)
76         {
77                 return DBA::selectFirst('register', [], ['hash' => $hash]);
78         }
79
80         /**
81          * Returns true if a register record exists with the provided hash
82          *
83          * @param  string $hash
84          * @return boolean
85          * @throws \Exception
86          */
87         public static function existsByHash($hash)
88         {
89                 return DBA::exists('register', ['hash' => $hash]);
90         }
91
92         /**
93          * Creates a register record for an invitation and returns the auto-generated code for it
94          *
95          * @return string
96          * @throws \Exception
97          */
98         public static function createForInvitation()
99         {
100                 $code = Strings::getRandomName(8) . random_int(1000, 9999);
101
102                 $fields = [
103                         'hash' => $code,
104                         'created' => DateTimeFormat::utcNow()
105                 ];
106
107                 DBA::insert('register', $fields);
108
109                 return $code;
110         }
111
112         /**
113          * Creates a register record for approval and returns the success of the database insert
114          * Checks for the existence of the provided user id
115          *
116          * @param  integer $uid      The ID of the user needing approval
117          * @param  string  $language The registration language
118          * @param  string  $note     An additional message from the user
119          * @return boolean
120          * @throws \Exception
121          */
122         public static function createForApproval($uid, $language, $note = '')
123         {
124                 $hash = Strings::getRandomHex();
125
126                 if (!User::exists($uid)) {
127                         return false;
128                 }
129
130                 $fields = [
131                         'hash'     => $hash,
132                         'created'  => DateTimeFormat::utcNow(),
133                         'uid'      => $uid,
134                         'password' => '', // Obsolete, slated for deletion
135                         'language' => $language,
136                         'note'     => $note
137                 ];
138
139                 return DBA::insert('register', $fields);
140         }
141
142         /**
143          * Deletes a register record by the provided hash and returns the success of the database deletion
144          *
145          * @param  string $hash
146          * @return boolean
147          * @throws \Exception
148          */
149         public static function deleteByHash($hash)
150         {
151                 return DBA::delete('register', ['hash' => $hash]);
152         }
153 }