From fc9de5b15c224618c50059ae1715f2c2b129bad2 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 21 Oct 2023 22:12:56 +0200 Subject: [PATCH] added unique but sortable time --- web/inc/classes/Page.class.php | 3 +- web/inc/helpers.php | 55 ++++++++++++++++++++++++---------- web/pages/login/controller.php | 9 ++++++ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/web/inc/classes/Page.class.php b/web/inc/classes/Page.class.php index 24e56e1..67bf33f 100644 --- a/web/inc/classes/Page.class.php +++ b/web/inc/classes/Page.class.php @@ -104,7 +104,8 @@ class Page //render template by running include ob_start(); - extract($this->variables); + if(is_array($this->variables)) + extract($this->variables); if($templatefile!= '') include($templatefile); $pagecontent = ob_get_contents(); diff --git a/web/inc/helpers.php b/web/inc/helpers.php index 58c49f9..3555a60 100644 --- a/web/inc/helpers.php +++ b/web/inc/helpers.php @@ -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) diff --git a/web/pages/login/controller.php b/web/pages/login/controller.php index dda5f65..a6ab210 100644 --- a/web/pages/login/controller.php +++ b/web/pages/login/controller.php @@ -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'];