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