]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - plugins/sql_auth/SQL/user.php
Create the tables automatically on plugin-load
[irc/unrealircd/unrealircd-webpanel.git] / plugins / sql_auth / SQL / user.php
CommitLineData
961b0aa7
VP
1<?php
2
3class SQLA_User
4{
5 public $id = NULL;
6 public $username = NULL;
a3151e7c 7 protected $passhash = NULL;
961b0aa7
VP
8 public $first_name = NULL;
9 public $last_name = NULL;
4d634d0a 10 public $user_meta = [];
961b0aa7 11
a3151e7c
VP
12 /**
13 * Find a user in the database by name or ID
14 * @param string $name
15 * @param mixed $id
16 */
4d634d0a 17 function __construct(string $name = NULL, $id = NULL)
961b0aa7
VP
18 {
19 $conn = sqlnew();
a3151e7c
VP
20
21 if ($id)
22 {
23 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE user_id = :id LIMIT 1");
24 $prep->execute(["id" => strtolower($id)]);
25 }
26 elseif ($name)
27 {
28 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE LOWER(user_name) = :name LIMIT 1");
29 $prep->execute(["name" => strtolower($name)]);
30 }
4d634d0a
VP
31 $data = NULL;
32 if ($prep)
33 $data = $prep->fetchAll();
961b0aa7
VP
34 if ($data = $data[0])
35 {
36 $this->id = $data['user_id'];
37 $this->username = $data['user_name'];
38 $this->passhash = $data['user_pass'];
4d634d0a
VP
39 $this->first_name = $data['user_fname'] ?? NULL;
40 $this->last_name = $data['user_lname'] ?? NULL;
41 $this->user_meta = (new SQLA_User_Meta($this->id))->list;
961b0aa7 42 }
961b0aa7
VP
43 }
44
a3151e7c
VP
45 function password_verify(string $input)
46 {
47 if (password_verify($input, $this->passhash))
48 return true;
49 return false;
50 }
4d634d0a 51}
a3151e7c 52
4d634d0a
VP
53class SQLA_User_Meta
54{
55 public $list = [];
56 function __construct($id)
57 {
58 $conn = sqlnew();
59 if ($id)
60 {
61 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id");
62 $prep->execute(["id" => $id]);
63 }
64 foreach ($prep->fetchAll() as $row)
65 {
66 $this->list[$row['meta_key']] = $row['meta_value'];
67 }
68 }
a3151e7c
VP
69}
70
4d634d0a
VP
71function create_new_user(array $user) : bool
72{
73 if (!isset($user['user_name']) || !isset($user['user_pass']))
74 throw new Exception("Attempted to add user without specifying user_name or user_pass");
75
76 $username = $user['user_name'];
77 $password = password_hash($user['user_pass'], PASSWORD_ARGON2ID);
78 $first_name = (isset($user['fname'])) ? $user['fname'] : NULL;
79 $last_name = (isset($user['lname'])) ? $user['lname'] : NULL;
a3151e7c 80
4d634d0a
VP
81 $conn = sqlnew();
82 $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname) VALUES (:name, :pass, :fname, :lname)");
83 $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name]);
84
85 return true;
86}
87
88/**
89 * Gets the user object for the current session
90 * @return SQLA_User|bool
91 */
92function unreal_get_current_user() : SQLA_User|bool
a3151e7c
VP
93{
94 session_start();
95 if (isset($_SESSION['id']))
96 {
4d634d0a
VP
97 $user = new SQLA_User(NULL, $_SESSION['id']);
98 if ($user->id)
99 return $user;
a3151e7c
VP
100 }
101 return false;
4d634d0a
VP
102}
103
104/**
105 * Checks if a user can do something
106 * @param string $permission
107 * @return bool
108 */
109function current_user_can() : bool
110{
111
112 return false;
113}
114