]> git.mxchange.org Git - friendica.git/blob - src/Model/User.php
Add User::authenticate()
[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::isResult($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                 $r = dba::select('user', array(), array('uid' => $uid), array("limit" => 1));
82
83                 call_hooks('remove_user', $r);
84
85                 // save username (actually the nickname as it is guaranteed
86                 // unique), so it cannot be re-registered in the future.
87
88                 dba::insert('userd', array('username' => $r['nickname']));
89
90                 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
91                 q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
92                 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
93
94                 // Send an update to the directory
95                 Worker::add(PRIORITY_LOW, "Directory", $r['url']);
96
97                 if ($uid == local_user()) {
98                         unset($_SESSION['authenticated']);
99                         unset($_SESSION['uid']);
100                         goaway(System::baseUrl());
101                 }
102         }
103 }