-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUID.php
More file actions
executable file
·141 lines (124 loc) · 3.84 KB
/
UUID.php
File metadata and controls
executable file
·141 lines (124 loc) · 3.84 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
<?php
require_once "Init.php";
require_once "Log.php";
/**
* Convert a MD5 Hash to a UUID-like String.
*/
function HashUUID(string $Path_Or_Data, string $Separator = "-"): string {
if (is_file($Path_Or_Data)) {
$Md5 = md5_file($Path_Or_Data);
} else {
$Md5 = md5($Path_Or_Data);
}
return implode($Separator, [
substr($Md5, 0, 8),
substr($Md5, 8, 4),
substr($Md5, 12, 4),
substr($Md5, 16, 4),
substr($Md5, 20)
]);
}
/**
* Convert a Timestamp to a UUID-like String.
*/
function TimeUUID(int $Time = null, array $Seed = null, string $Separator = "-"): string {
$Time = str_pad($Time ?? time(), 10, "0", STR_PAD_LEFT);
$Seed = $Seed ?? [rand(0, 15), rand(0, 15)];
$Seed = [$Seed[0] % 16, $Seed[1] % 16];
$Hexa = "";
foreach (str_split($Time) as $Char) {
$Hexa .= sprintf("%03x", ord($Char) * ($Seed[0] % 8 + 1) * ($Seed[1] % 8 + 1));
}
$Hexa .= sprintf("%1x", $Seed[0]) . sprintf("%1x", $Seed[1]);
return implode($Separator, [
substr($Hexa, 0, 8),
substr($Hexa, 8, 4),
substr($Hexa, 12, 4),
substr($Hexa, 16, 4),
substr($Hexa, 20)
]);
}
/**
* Sort Files in a Directory by TimeUUID.
*/
function TimeUUID_Sort(string $Path, string $Separator = null): array {
$Response = [
"Ec" => 0,
"Em" => "",
"Files" => []
];
try {
$Files = scandir($Path);
} catch (Exception $Error) {
$Response["Ec"] = 50001; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$Metas = [];
foreach ($Files as $File) {
$Decoded = TimeUUID_Decode($File, $Separator);
if ($Decoded["Ec"] == 0) {
$Metas[] = [
"Time" => $Decoded["Time"],
"Name" => $File,
"Path" => $Path . DIRECTORY_SEPARATOR . $File
];
}
}
} catch (Exception $Error) {
$Response["Ec"] = 50002; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
usort($Metas, function($a, $b) {
return $a["Time"] <=> $b["Time"];
});
$Response["Files"] = $Metas;
} catch (Exception $Error) {
$Response["Ec"] = 50003; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
return $Response;
}
/**
* Decode a UUID-like String to a Timestamp.
*/
function TimeUUID_Decode(string $UUID, string $Separator = null): array {
$Response = [
"Ec" => 0,
"Em" => "",
"Time" => -1
];
try {
if ($Separator === null) {
$Separator_Len = (strlen($UUID) - 32) / 4;
if ($Separator_Len > 0) {
$Sep_Candidate = substr($UUID, 8, $Separator_Len);
if (substr_count($UUID, $Sep_Candidate) >= 4) {
$Separator = $Sep_Candidate;
} else {
$Separator = "";
}
} else {
$Separator = "";
}
}
} catch (Exception $Error) {
$Response["Ec"] = 50001; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$UUID = str_replace($Separator, "", $UUID);
$Seed = [hexdec($UUID[-2]), hexdec($UUID[-1])];
} catch (Exception $Error) {
$Response["Ec"] = 50002; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$Time = "";
for ($i = 0; $i < strlen($UUID) - 2; $i += 3) {
$Hexa = hexdec(substr($UUID, $i, 3));
$Time .= chr($Hexa / (($Seed[0] % 8 + 1) * ($Seed[1] % 8 + 1)));
}
$Response["Time"] = (int)$Time;
} catch (Exception $Error) {
$Response["Ec"] = 50003; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
return $Response;
}
?>