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