2022-03-19 00:00:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\utility;
|
|
|
|
|
|
|
|
use ReallySimpleJWT\Token;
|
|
|
|
use ReallySimpleJWT\Exception\BuildException;
|
|
|
|
use Mni\FrontYAML\Parser;
|
|
|
|
|
|
|
|
class StringTools
|
|
|
|
{
|
|
|
|
public static function createUUID()
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
if (function_exists('com_create_guid') === true) {
|
|
|
|
return trim(com_create_guid(), '{}');
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
2022-05-17 02:41:15 +02:00
|
|
|
'%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
|
2022-03-19 00:00:51 +01:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public static function sanitizeContent($entry)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$parser = new Parser();
|
|
|
|
$rendered = $parser->parse($entry);
|
2022-03-19 00:00:51 +01:00
|
|
|
$sanitizer = HtmlSanitizer\Sanitizer::create([
|
2022-05-17 02:41:15 +02:00
|
|
|
'extensions' => ['basic', 'image', 'list', 'code'],
|
|
|
|
'tags' => [
|
|
|
|
'img' => [
|
|
|
|
'allowed_attributes' => ['src', 'alt', 'title', 'class'],
|
|
|
|
'allowed_hosts' => null,
|
|
|
|
],
|
|
|
|
],
|
2022-03-19 00:00:51 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$preclean = $sanitizer->sanitize($rendered->getContent());
|
|
|
|
|
|
|
|
$cleaned = strip_tags($rendered->getContent(), [
|
2022-05-17 02:41:15 +02:00
|
|
|
'a',
|
|
|
|
'br',
|
|
|
|
'p',
|
|
|
|
'strong',
|
|
|
|
'br',
|
|
|
|
'img',
|
|
|
|
'iframe',
|
|
|
|
'ul',
|
|
|
|
'li',
|
|
|
|
'i',
|
|
|
|
'h1',
|
|
|
|
'h2',
|
|
|
|
'h3',
|
|
|
|
'pre',
|
|
|
|
'code',
|
2022-03-19 00:00:51 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $cleaned;
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public static function safeString($string)
|
|
|
|
{
|
|
|
|
return strtolower(
|
|
|
|
trim(
|
|
|
|
preg_replace(
|
2022-05-17 02:41:15 +02:00
|
|
|
'~[^0-9a-z]+~i',
|
|
|
|
'_',
|
2022-03-19 00:00:51 +01:00
|
|
|
html_entity_decode(
|
|
|
|
preg_replace(
|
2022-05-17 02:41:15 +02:00
|
|
|
'~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i',
|
2022-03-19 00:00:51 +01:00
|
|
|
'$1',
|
2022-05-17 02:41:15 +02:00
|
|
|
htmlentities($string, ENT_QUOTES, 'UTF-8')
|
2022-03-19 00:00:51 +01:00
|
|
|
),
|
|
|
|
ENT_QUOTES,
|
2022-05-17 02:41:15 +02:00
|
|
|
'UTF-8'
|
2022-03-19 00:00:51 +01:00
|
|
|
)
|
|
|
|
),
|
2022-05-17 02:41:15 +02:00
|
|
|
'-'
|
2022-03-19 00:00:51 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public static function randomString(int $length)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
$special = '*&!@%^#$';
|
|
|
|
$alphabet = $alphanum . $special;
|
|
|
|
$random = openssl_random_pseudo_bytes($length);
|
2022-03-19 00:00:51 +01:00
|
|
|
$alphabet_length = strlen($alphabet);
|
2022-05-17 02:41:15 +02:00
|
|
|
$string = '';
|
2022-03-19 00:00:51 +01:00
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
$string .= $alphabet[ord($random[$i]) % $alphabet_length];
|
|
|
|
}
|
|
|
|
|
|
|
|
//secret needs to be a valid token
|
|
|
|
if ($length == 12) {
|
|
|
|
try {
|
2022-05-17 02:41:15 +02:00
|
|
|
$secret = Token::create(12, $string, time() + 3600, 'localhost');
|
2022-03-19 00:00:51 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
private static function checkSpecial($string)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$specials = ['*', '&', '!', '@', '%', '^', '#', '$'];
|
|
|
|
$valid = false;
|
2022-03-19 00:00:51 +01:00
|
|
|
foreach ($specials as $item) {
|
|
|
|
if (strpos($string, $item)) {
|
|
|
|
return $valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
}
|