]> git.mxchange.org Git - friendica.git/blob - src/Model/User.php
b2beb8e19b7a6fe49efcf290ab1395fa121cbe43
[friendica.git] / src / Model / User.php
1 <?php
2
3 /**
4  * @file src/Model/User.php
5  * @brief This file includes the User class with user related database functions
6  */
7
8 namespace Friendica\Model;
9
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use dba;
14
15 require_once 'boot.php';
16 require_once 'include/plugin.php';
17
18 /**
19  * @brief This class handles User related functions
20  */
21 class User
22 {
23         public static function authenticate($user_info, $password)
24         {
25                 if (is_object($user_info)) {
26                         $user = (array) $user_info;
27                 } elseif (is_int($user_info)) {
28                         $user = dba::select('user',
29                                 ['uid', 'password'],
30                                 [
31                                         'uid' => $user_info,
32                                         'blocked' => 0,
33                                         'account_expired' => 0,
34                                         'account_removed' => 0,
35                                         'verified' => 1
36                                 ],
37                                 ['limit' => 1]
38                         );
39                 } elseif (is_string($user_info)) {
40                         $user = dba::fetch_first('SELECT `uid`, `password`
41                                 FROM `user`
42                                 WHERE (`email` = ? OR `username` = ? OR `nickname` = ?)
43                                 AND `blocked` = 0
44                                 AND `account_expired` = 0
45                                 AND `account_removed` = 0
46                                 AND `verified` = 1
47                                 LIMIT 1',
48                                 $user_info,
49                                 $user_info,
50                                 $user_info
51                         );
52                 } else {
53                         $user = $user_info;
54                 }
55
56                 if (!DBM::is_result($user) || !isset($user['uid']) || !isset($user['password'])) {
57                         return false;
58                 }
59
60                 $password_hashed = hash('whirlpool', $password);
61
62                 if ($password_hashed !== $user['password']) {
63                         return false;
64                 }
65
66                 return $user['uid'];
67         }
68
69         /**
70          * @param object $uid user to remove
71          * @return void
72          */
73         public static function remove($uid)
74         {
75                 if (!$uid) {
76                         return;
77                 }
78
79                 logger('Removing user: ' . $uid);
80
81                 $user = dba::select('user', [], ['uid' => $uid], ['limit' => 1]);
82
83                 call_hooks('remove_user', $user);
84
85                 // save username (actually the nickname as it is guaranteed
86                 // unique), so it cannot be re-registered in the future.
87                 dba::insert('userd', ['username' => $user['nickname']]);
88
89                 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
90                 dba::update('user', ['account_removed' => true, 'account_expires_on' => datetime_convert()], ['uid' => $uid]);
91                 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
92
93                 // Send an update to the directory
94                 Worker::add(PRIORITY_LOW, "Directory", $user['url']);
95
96                 if ($uid == local_user()) {
97                         unset($_SESSION['authenticated']);
98                         unset($_SESSION['uid']);
99                         goaway(System::baseUrl());
100                 }
101         }
102 }