-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
52 lines (49 loc) · 1.61 KB
/
utils.php
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
<?php
if (!defined('CORRECT_ENTRY')) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$tzutc = new DateTimeZone('UTC');
function base64url_encode($data) {
return sodium_bin2base64($data, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING);
//return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return sodium_base642bin($data, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING);
// INVALID: return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
function generateRandomKeyIdShort() {
return base64url_encode(random_bytes(6));
}
function generateRandomKeyIdLong() {
return base64url_encode(random_bytes(12));
}
function is_base64urlsafe($str) {
return true;
}
function nowUtc() {
global $tzutc;
return new DateTime('now', $tzutc);
}
function fromTimestampUtc($timestamp) {
global $tzutc;
return new DateTime('@'.$timestamp, $tzutc);
}
function spxp_parse_datetime($str) {
global $tzutc;
return DateTime::createFromFormat('Y-m-d\TH:i:s.v', $str, $tzutc);
}
function spxp_format_datetime($datetime) {
return $datetime->format('Y-m-d\TH:i:s.v');
}
function datetime_to_microtime($datetime) {
return ($datetime->getTimestamp() * 1000) + intval($datetime->format('v'));
}
function microtime_to_datetime($microtime) {
global $tzutc;
$timestamp = floor($microtime / 1000);
$ms = $microtime % 1000;
$dt_without_ms = fromTimestampUtc($timestamp);
return DateTime::createFromFormat('Y-m-d\TH:i:s.v', $dt_without_ms->format('Y-m-d\TH:i:s').'.'.sprintf('%03d', $ms), $tzutc);
}