added unique but sortable time
All checks were successful
Build and push / Pulling repo on server (push) Successful in 19s

This commit is contained in:
Chris 2023-10-21 22:12:56 +02:00
parent b095073447
commit fc9de5b15c
3 changed files with 50 additions and 17 deletions

View File

@ -104,6 +104,7 @@ class Page
//render template by running include
ob_start();
if(is_array($this->variables))
extract($this->variables);
if($templatefile!= '')
include($templatefile);

View File

@ -147,26 +147,49 @@ function getHighestSQLVersion()
return end($files);
}
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
function gen_ulid($milliseconds = null,$lowercase = false) {
if($milliseconds===null)
$milliseconds = (int) (microtime(true) * 1000);
$encodingChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
$encodingLength = 32;
$randomLength = 16;
$lastRandChars = [];
$timeLength = 10;
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
$timeChars = '';
$randChars = '';
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
for ($i = $timeLength - 1; $i >= 0; $i--) {
$mod = $milliseconds % $encodingLength;
$timeChars = $encodingChars[$mod].$timeChars;
$milliseconds = ($milliseconds - $mod) / $encodingLength;
}
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
for ($i = 0; $i < $randomLength; $i++) {
$lastRandChars[$i] = random_int(0, 31);
}
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
for ($i = 0; $i < $randomLength; $i++) {
$randChars .= $encodingChars[$lastRandChars[$i]];
}
return ($value = $timeChars . $randChars) && $lowercase ? strtolower($value) : strtoupper($value);
}
function ulid_to_timestamp($ulid)
{
$encodingChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
$encodingLength = 32;
$timeLength = 10;
$timeChars = substr($ulid,0,$timeLength);
$time = 0;
for ($i = 0; $i < $timeLength; $i++) {
$time = $time * $encodingLength + strpos($encodingChars, $timeChars[$i]);
}
return $time;
}
function escape($str)

View File

@ -9,6 +9,15 @@ class Login extends Page {
//return print_r($_REQUEST, true);
}
function test()
{
return nl2br(print_r([
'uuid' => gen_ulid(),
'timestamp' => ulid_to_timestamp("01HD9XN98F8SGT01X527KBNHRN"),
'freshtime' => ulid_to_timestamp(gen_ulid())
],true));
}
function validate()
{
$email = $_REQUEST['email'];