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