2021-03-29 00:22:00 +02:00
|
|
|
<?php
|
2021-04-24 05:54:56 +02:00
|
|
|
use ReallySimpleJWT\Token;
|
|
|
|
use ReallySimpleJWT\Exception\BuildException;
|
2021-03-29 00:22:00 +02:00
|
|
|
//include "brain/data/Auth.inc.php";
|
|
|
|
|
|
|
|
class StringTools
|
|
|
|
{
|
2021-04-14 05:49:12 +02:00
|
|
|
public static function createUUID()
|
|
|
|
{
|
|
|
|
if (function_exists("com_create_guid") === true) {
|
|
|
|
return trim(com_create_guid(), "{}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
"%04X%04X-%04X-%04X-%04X-%04X%04X%04X",
|
|
|
|
mt_rand(0, 65535),
|
|
|
|
mt_rand(0, 65535),
|
|
|
|
mt_rand(0, 65535),
|
|
|
|
mt_rand(16384, 20479),
|
|
|
|
mt_rand(32768, 49151),
|
|
|
|
mt_rand(0, 65535),
|
|
|
|
mt_rand(0, 65535),
|
|
|
|
mt_rand(0, 65535)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:50:30 +02:00
|
|
|
public static function safeString($string)
|
|
|
|
{
|
|
|
|
return strtolower(
|
|
|
|
trim(
|
|
|
|
preg_replace(
|
|
|
|
"~[^0-9a-z]+~i",
|
|
|
|
"_",
|
|
|
|
html_entity_decode(
|
|
|
|
preg_replace(
|
|
|
|
"~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i",
|
|
|
|
'$1',
|
|
|
|
htmlentities($string, ENT_QUOTES, "UTF-8")
|
|
|
|
),
|
|
|
|
ENT_QUOTES,
|
|
|
|
"UTF-8"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"-"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:22:00 +02:00
|
|
|
public static function randomString(int $length)
|
|
|
|
{
|
|
|
|
$alphanum =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
$special = '*&!@%^#$';
|
|
|
|
$alphabet = $alphanum . $special;
|
|
|
|
$random = openssl_random_pseudo_bytes($length);
|
|
|
|
$alphabet_length = strlen($alphabet);
|
|
|
|
$string = "";
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
$string .= $alphabet[ord($random[$i]) % $alphabet_length];
|
|
|
|
}
|
|
|
|
|
2021-04-24 05:54:56 +02:00
|
|
|
//secret needs to be a valid token
|
|
|
|
if ($length == 12) {
|
|
|
|
try {
|
|
|
|
$secret = Token::create(12, $string, time() + 3600, "localhost");
|
|
|
|
return $string;
|
|
|
|
} catch (BuildException $e) {
|
|
|
|
//bad secret, so try agiain
|
|
|
|
//echo "BAD STRING";
|
|
|
|
return self::randomString(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::validate($key, $string)) {
|
|
|
|
return $string;
|
|
|
|
} else {
|
|
|
|
return self::randomString(12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function checkSpecial($string)
|
|
|
|
{
|
|
|
|
$specials = ["*", "&", "!", "@", "%", "^", "#", "$"];
|
|
|
|
$valid = false;
|
|
|
|
foreach ($specials as $item) {
|
|
|
|
if (strpos($string, $item)) {
|
|
|
|
return $valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $valid;
|
2021-03-29 00:22:00 +02:00
|
|
|
}
|
|
|
|
}
|