-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
162 lines (120 loc) · 4.72 KB
/
Session.php
File metadata and controls
162 lines (120 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace MerapiPanel\Module\Auth;
use MerapiPanel\Box;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Box\Module\Entity\Module;
use MerapiPanel\Database\DB;
use MerapiPanel\Utility\AES;
use MerapiPanel\Utility\Util;
use PDO;
class Session extends __Fragment
{
protected $module;
function onCreate(Module $module)
{
$this->module = $module;
}
function getToken()
{
$cookie_name = $this->module->getConfig()->get("cookie_name");
if (!isset($_COOKIE[$cookie_name]) || !$token = AES::getInstance()->decrypt($_COOKIE[$cookie_name] ?? "")) {
return null;
}
return $token;
}
function getUser($colums = ["id", "name", "email", "role", "status", "post_date", "update_date"])
{
$token = $this->getToken();
if (!$token) {
return null;
}
$SQL = "SELECT user_id FROM `session` WHERE token = :token";
$stmt = DB::instance()->prepare($SQL);
$stmt->execute(['token' => $token]);
$session = $stmt->fetch(PDO::FETCH_ASSOC);
if ($session) {
return Box::module("User")->fetch($colums, ["id" => $session['user_id'], "status" => 2]);
}
return null;
}
public function getCurrent()
{
$token = $this->getToken();
if (!$token) {
return null;
}
return DB::table("session")->select("*")->where("token")->equals($token)->execute()->fetch(PDO::FETCH_ASSOC);
}
function getUserSessions($user_id = null)
{
if($user_id) {
$SQL = "SELECT * FROM `session` WHERE user_id = :user_id ORDER BY post_date DESC LIMIT 100";
$stmt = DB::instance()->prepare($SQL);
$stmt->execute(['user_id' => $user_id]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $key => $value) {
$result[$key]['data'] = json_decode($value['data'], true);
$result[$key]['browser'] = $this->getBrowserName($value['user_agent']);
$result[$key]['device'] = $this->getDeviceName($value['user_agent']);
$result[$key]['time_ago'] = Util::timeAgo($value['post_date']);
}
return $result;
}
$token = $this->getToken();
$user = $this->getUser();
if (!$user) {
return [];
}
$SQL = "SELECT * FROM `session` WHERE user_id = :user_id ORDER BY post_date DESC LIMIT 100";
$stmt = DB::instance()->prepare($SQL);
$stmt->execute(['user_id' => $user['id']]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $key => $value) {
$result[$key]['data'] = json_decode($value['data'], true);
$result[$key]['browser'] = $this->getBrowserName($value['user_agent']);
$result[$key]['device'] = $this->getDeviceName($value['user_agent']);
$result[$key]['time_ago'] = Util::timeAgo($value['post_date']);
$result[$key]['is_current'] = ($value['token'] == $token);
}
return $result;
}
function getBrowserName($userAgent)
{
$browser = "Unknown";
// Check if user agent contains specific browser keywords
if (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident') !== false) {
$browser = 'Internet Explorer';
} elseif (strpos($userAgent, 'Firefox') !== false) {
$browser = 'Mozilla Firefox';
} elseif (strpos($userAgent, 'Chrome') !== false) {
$browser = 'Google Chrome';
} elseif (strpos($userAgent, 'Safari') !== false) {
$browser = 'Safari';
} elseif (strpos($userAgent, 'Opera') !== false || strpos($userAgent, 'OPR') !== false) {
$browser = 'Opera';
} elseif (strpos($userAgent, 'Edge') !== false) {
$browser = 'Microsoft Edge';
}
return $browser;
}
// Function to get device name from user agent
function getDeviceName($userAgent)
{
$device = "Unknown";
// Check if user agent contains specific device keywords
if (strpos($userAgent, 'Windows') !== false) {
$device = 'Windows';
} elseif (strpos($userAgent, 'iPad') !== false) {
$device = 'iPad';
} elseif (strpos($userAgent, 'iPhone') !== false) {
$device = 'iPhone';
} elseif (strpos($userAgent, 'Android') !== false) {
$device = 'Android Device';
} elseif (strpos($userAgent, 'Linux') !== false) {
$device = 'Linux';
} elseif (strpos($userAgent, 'Macintosh') !== false || strpos($userAgent, 'Mac OS X') !== false) {
$device = 'Macintosh';
}
return $device;
}
}