You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.2 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Helper;
use Hyperf\Utils\Str as BaseStr;
class Str extends BaseStr
{
const PASSPHRASE = 'O0IP563o9WjoogRNFGN4';
const CIPHER_ALGO = 'aes-256-cbc';
public static function getRandomString($length, $special = true)
{
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
'3', '4', '5', '6', '7', '8', '9'
);
if($special){
$chars = array_merge($chars, array(
'!', '@', '#', '$', '?', '|', '{', '/', ':', ';',
'%', '^', '&', '*', '(', ')', '-', '_', '[', ']',
'}', '<', '>', '~', '+', '=', ',', '.'
));
}
$charsLen = count($chars) - 1;
shuffle($chars);
$password = '';
for($i=0; $i<$length; $i++){
$password .= $chars[mt_rand(0, $charsLen)];
}
return $password;
}
public static function encrypt(string $data)
{
$iv = '62dTsnuD1Ow68pR8';
return openssl_encrypt($data, self::CIPHER_ALGO, self::PASSPHRASE, 0, $iv);
}
public static function decrypt(string $password)
{
$iv = '62dTsnuD1Ow68pR8';
return openssl_decrypt($password, self::CIPHER_ALGO, self::PASSPHRASE, 0, $iv);
}
public static function parseUrl(string $url)
{
if (empty($url)) {
return $url;
}
$item = explode(':', $url);
if (!isset($item[0]) || in_array($item[0], ['http', 'https'])) {
return $url;
}
return self::getFileUrlViaStorage($item[1], $item[0]);
}
public static function getFileUrlViaStorage(string $url, string $storage): string
{
$domain = config('file.storage.' . $storage . '.domain');
$prefixPath = config('file.storage.' . $storage . '.prefix_path', '');
return $domain . $prefixPath . $url;
}
}