|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Cadmium\System\Modules\Auth |
|
5
|
|
|
* @author Anton Romanov |
|
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
|
7
|
|
|
* @link http://cadmium-cms.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Modules { |
|
11
|
|
|
|
|
12
|
|
|
use DB, Session; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Auth { |
|
15
|
|
|
|
|
16
|
|
|
private static $admin = false, $auth = null, $user = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initialize the session |
|
20
|
|
|
* |
|
21
|
|
|
* @property string $section : an active section (SECTION_SITE or SECTION_ADMIN) |
|
22
|
|
|
* |
|
23
|
|
|
* @return bool : true on success or false on failure |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
public static function init(string $section) : bool { |
|
27
|
|
|
|
|
28
|
|
|
self::$admin = ($section === SECTION_ADMIN); |
|
29
|
|
|
|
|
30
|
|
|
self::$auth = null; self::$user = Entitizer::get(TABLE_USERS); |
|
31
|
|
|
|
|
32
|
|
|
# Check session code |
|
33
|
|
|
|
|
34
|
|
|
if (!is_string($code = Session::get('code'))) return false; |
|
35
|
|
|
|
|
36
|
|
|
# Authorize |
|
37
|
|
|
|
|
38
|
|
|
if (false === ($result = Auth\Utils\Connector\Session::authorize($code, self::$admin))) return false; |
|
39
|
|
|
|
|
40
|
|
|
# Update auth |
|
41
|
|
|
|
|
42
|
|
|
$result['auth']->edit(['time' => REQUEST_TIME]); |
|
43
|
|
|
|
|
44
|
|
|
# Update user |
|
45
|
|
|
|
|
46
|
|
|
$result['user']->edit(['time_logged' => REQUEST_TIME]); |
|
47
|
|
|
|
|
48
|
|
|
# Set entities |
|
49
|
|
|
|
|
50
|
|
|
self::$auth = $result['auth']; self::$user = $result['user']; |
|
51
|
|
|
|
|
52
|
|
|
# ------------------------ |
|
53
|
|
|
|
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Delete the session |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool : true on success or false on failure |
|
61
|
|
|
*/ |
|
62
|
|
|
|
|
63
|
|
|
public static function logout() : bool { |
|
64
|
|
|
|
|
65
|
|
|
if ((null === self::$user) || (0 === self::$user->id)) return false; |
|
66
|
|
|
|
|
67
|
|
|
# Remove auth entry from db |
|
68
|
|
|
|
|
69
|
|
|
self::$auth->remove(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
# Remove session variable |
|
72
|
|
|
|
|
73
|
|
|
Session::delete('code'); |
|
74
|
|
|
|
|
75
|
|
|
# Reset entities |
|
76
|
|
|
|
|
77
|
|
|
self::$auth = null; self::$user = Entitizer::get(TABLE_USERS); |
|
78
|
|
|
|
|
79
|
|
|
# ------------------------ |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check whether the initial registration is required (the users table is empty and auto_increment is set to 0) |
|
86
|
|
|
*/ |
|
87
|
|
|
|
|
88
|
|
|
public static function isInitial() : bool { |
|
89
|
|
|
|
|
90
|
|
|
DB::send("SHOW TABLE STATUS WHERE name LIKE '" . TABLE_USERS . "'"); |
|
91
|
|
|
|
|
92
|
|
|
if (!(DB::getLast() && (null !== ($data = DB::getLast()->getRow())))) return false; |
|
93
|
|
|
|
|
94
|
|
|
# ------------------------ |
|
95
|
|
|
|
|
96
|
|
|
return (($data['Rows'] == 0) && ($data['Auto_increment'] == 1)); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Check whether the authorization mode is set to admin |
|
101
|
|
|
*/ |
|
102
|
|
|
|
|
103
|
|
|
public static function isAdmin() : bool { |
|
104
|
|
|
|
|
105
|
|
|
return self::$admin; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Check whether the authorization was successful |
|
110
|
|
|
*/ |
|
111
|
|
|
|
|
112
|
|
|
public static function isLogged() : bool { |
|
113
|
|
|
|
|
114
|
|
|
return ((null !== self::$user) && (0 !== self::$user->id)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the user param |
|
119
|
|
|
* |
|
120
|
|
|
* @return mixed|null : the param value or null if the session was not initialized |
|
121
|
|
|
*/ |
|
122
|
|
|
|
|
123
|
|
|
public static function get(string $name) { |
|
124
|
|
|
|
|
125
|
|
|
return (self::$user->$name ?? null); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return the user entity object |
|
130
|
|
|
* |
|
131
|
|
|
* @return Modules\Entitizer\Entity\User|false : the user entity object or false if the session was not initialized |
|
132
|
|
|
*/ |
|
133
|
|
|
|
|
134
|
|
|
public static function getUser() { |
|
135
|
|
|
|
|
136
|
|
|
return (self::$user ?? false); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.