1 | <?php |
||
2 | |||
3 | namespace MVC; |
||
4 | |||
5 | use JSON\json; |
||
6 | |||
7 | class uid |
||
8 | { |
||
9 | public static function verifyUID() |
||
10 | { |
||
11 | $last_body = self::removeLastCache(); |
||
12 | //ksort($_SERVER); |
||
13 | //var_dump(isset($_SERVER['HTTP_UNIQUE_ID']), $_SERVER); |
||
14 | if (isset($_SERVER['HTTP_UNIQUE_ID'])) { |
||
15 | if (!isset($_SESSION['uid'])) { |
||
16 | json::json([ |
||
17 | 'error' => true, |
||
18 | 'message' => 'Undefined session request', |
||
19 | 'title' => 'UID Sess', |
||
20 | 'last_body' => $last_body, |
||
21 | ]); |
||
22 | exit; |
||
0 ignored issues
–
show
|
|||
23 | } |
||
24 | if ($_SESSION['uid'] != $_SERVER['HTTP_UNIQUE_ID']) { |
||
25 | json::json([ |
||
26 | 'error' => true, |
||
27 | 'message' => 'Undefined request', |
||
28 | 'title' => 'Network', |
||
29 | 'last_body' => $last_body, |
||
30 | ]); |
||
31 | exit; |
||
0 ignored issues
–
show
|
|||
32 | } |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public static function removeLastCache() |
||
37 | { |
||
38 | $last_body = ''; |
||
39 | if (headers_sent()) { |
||
40 | if (ob_get_level()) { |
||
41 | $last_body = ob_end_clean(); |
||
42 | ob_start(); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | return $last_body; |
||
47 | } |
||
48 | |||
49 | public static function checkRequestUID($header_name, $regen_session_timeout = 60) |
||
50 | { |
||
51 | //var_dump(\MVC\helper::cors(true)); |
||
52 | if (\MVC\helper::cors()) { |
||
53 | self::receiveUID($header_name, $regen_session_timeout); |
||
54 | } |
||
55 | //var_dump($_SERVER); |
||
56 | } |
||
57 | |||
58 | public static function receiveUID($header_name, $regen_session_timeout = 60) |
||
59 | { |
||
60 | if (isset($_REQUEST[$header_name]) || isset($_SERVER['HTTP_UID_SIGN'])) { |
||
61 | header('Content-Type: application/javascript'); |
||
62 | if (!isset($_SESSION['timer_start'])) { |
||
63 | $_SESSION['timer_start'] = time(); |
||
64 | } |
||
65 | $res['ago'] = time() - $_SESSION['timer_start']; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
66 | $res['dif'] = 30 * 60; // 1800 secs / 30 mins |
||
67 | $res['left'] = $res['ago'] < $res['dif']; |
||
68 | if ($res['ago'] > $regen_session_timeout * 60) { |
||
69 | // if session timer more than $regen_session_timeout minutes, regenerate session |
||
70 | //session_regenerate_id(session_id()); |
||
71 | } |
||
72 | if (isset($_REQUEST['check_file_session'])) { |
||
73 | $session_path = session_save_path() . '/sess_' . session_id(); |
||
74 | $session_time = fileatime($session_path); |
||
75 | $res['secs'] = (time() - $session_time); |
||
76 | $res['path'] = realpath($session_path); |
||
77 | } |
||
78 | $uid = isset($_REQUEST['uid']) ? $_REQUEST['uid'] : null; |
||
79 | $array = $_REQUEST; |
||
80 | if ($uid) { |
||
81 | $_SESSION['uid'] = $uid; |
||
82 | $array = [ |
||
83 | 'uid' => $uid, |
||
84 | ]; |
||
85 | } |
||
86 | if (isset($_REQUEST['callback'])) { |
||
87 | //header('Content-Type: application/json'); |
||
88 | |||
89 | echo $_REQUEST['callback'] . '(' . json_encode($array) . ')'; |
||
90 | exit; |
||
0 ignored issues
–
show
|
|||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | public static function include_uid_js() |
||
96 | { |
||
97 | if (!isset($_SESSION['uid'])) { |
||
98 | echo 'var UIDForce = true;'; |
||
99 | } |
||
100 | helper::include_asset(__DIR__ . '/themes/assets/js/uid.min.js', __DIR__ . '/themes/assets/js/uid.js'); |
||
101 | } |
||
102 | } |
||
103 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.