Merge pull request #7251 from annando/fix-fatal
[friendica.git/.git] / src / Model / Register.php
1 <?php
2
3 /**
4  * @file src/Model/Register.php
5  */
6 namespace Friendica\Model;
7
8 use Friendica\Database\DBA;
9 use Friendica\Util\DateTimeFormat;
10 use Friendica\Util\Strings;
11
12 /**
13  * Class interacting with the register database table
14  *
15  * @author Hypolite Petovan <mrpetovan@gmail.com>
16  */
17 class Register
18 {
19         /**
20          * Return the list of pending registrations
21          *
22          * @return array
23          * @throws \Exception
24          */
25         public static function getPending()
26         {
27                 $stmt = DBA::p(
28                         "SELECT `register`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`email`
29                         FROM `register`
30                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid`
31                         INNER JOIN `user` ON `register`.`uid` = `user`.`uid`"
32                 );
33
34                 return DBA::toArray($stmt);
35         }
36
37         /**
38          * Returns the pending registration count
39          *
40          * @return int
41          * @throws \Exception
42          */
43         public static function getPendingCount()
44         {
45                 $register = DBA::fetchFirst(
46                         "SELECT COUNT(*) AS `count`
47                         FROM `register`
48                         INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid` AND `contact`.`self`"
49                 );
50
51                 return $register['count'];
52         }
53
54         /**
55          * Returns the register record associated with the provided hash
56          *
57          * @param  string $hash
58          * @return array
59          * @throws \Exception
60          */
61         public static function getByHash($hash)
62         {
63                 return DBA::selectFirst('register', [], ['hash' => $hash]);
64         }
65
66         /**
67          * Returns true if a register record exists with the provided hash
68          *
69          * @param  string $hash
70          * @return boolean
71          * @throws \Exception
72          */
73         public static function existsByHash($hash)
74         {
75                 return DBA::exists('register', ['hash' => $hash]);
76         }
77
78         /**
79          * Creates a register record for an invitation and returns the auto-generated code for it
80          *
81          * @return string
82          * @throws \Exception
83          */
84         public static function createForInvitation()
85         {
86                 $code = Strings::getRandomName(8) . srand(1000, 9999);
87
88                 $fields = [
89                         'hash' => $code,
90                         'created' => DateTimeFormat::utcNow()
91                 ];
92
93                 DBA::insert('register', $fields);
94
95                 return $code;
96         }
97
98         /**
99          * Creates a register record for approval and returns the success of the database insert
100          * Checks for the existence of the provided user id
101          *
102          * @param  integer $uid      The ID of the user needing approval
103          * @param  string  $language The registration language
104          * @param  string  $note     An additional message from the user
105          * @return boolean
106          * @throws \Exception
107          */
108         public static function createForApproval($uid, $language, $note = '')
109         {
110                 $hash = Strings::getRandomHex();
111
112                 if (!User::exists($uid)) {
113                         return false;
114                 }
115
116                 $fields = [
117                         'hash'     => $hash,
118                         'created'  => DateTimeFormat::utcNow(),
119                         'uid'      => $uid,
120                         'password' => '', // Obsolete, slated for deletion
121                         'language' => $language,
122                         'note'     => $note
123                 ];
124
125                 return DBA::insert('register', $fields);
126         }
127
128         /**
129          * Deletes a register record by the provided hash and returns the success of the database deletion
130          *
131          * @param  string $hash
132          * @return boolean
133          * @throws \Exception
134          */
135         public static function deleteByHash($hash)
136         {
137                 return DBA::delete('register', ['hash' => $hash]);
138         }
139 }