| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace app\common\services;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Support\Facades\Cookie;
- use Illuminate\Support\Facades\DB;
- use Log;
- use Carbon\Carbon;
- use Illuminate\Support\Str;
- use Storage as LaravelStorage;
- class Utils
- {
- /**
- * Returns the client IP address.
- *
- * This method is defined because Symfony's Request::getClientIp() needs "setTrustedProxies()"
- * which sucks when load balancer is enabled.
- *
- * @return string
- */
- public static function getClientIp()
- {
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } else {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- return $ip;
- }
- /**
- * Checks whether the request is secure or not.
- * True is always returned when "X-Forwarded-Proto" header is set.
- *
- * This method is defined because Symfony's Request::isSecure() needs "setTrustedProxies()"
- * which sucks when load balancer is enabled.
- *
- * @return bool
- */
- public static function isRequestSecure()
- {
- if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
- return true;
- if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
- return true;
- if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
- return true;
- return false;
- }
- /**
- * Compares two "PHP-standardized" version number strings.
- * Unlike version_compare(), this method will determine that versions with suffix are lower.
- *
- * e.g. 3.2-beta > 3.2-alpha
- * 3.2 > 3.2-beta
- * 3.2 > 3.2-pre-release
- * 3.2 > 3.2-pr8
- *
- * @param string $version1
- * @param string $version2
- * @param string $operator
- * @return mixed
- */
- public static function versionCompare($version1, $version2, $operator = null)
- {
- $versions = [$version1, $version2];
- // pre-processing for version contains hyphen
- foreach ([0, 1] as $offset) {
- if (false !== ($result = self::parseVersionWithHyphen($versions[$offset]))) {
- $versions[$offset] = $result;
- } else {
- $versions[$offset] = ['main' => $versions[$offset], 'sub' => ''];
- }
- }
- if (version_compare($versions[0]['main'], $versions[1]['main'], '=')) {
- $sub1 = $versions[0]['sub'];
- $sub2 = $versions[1]['sub'];
- // v3.2-pr < v3.2
- if ($sub1 != "" || $sub2 != "") {
- // if both of sub-versions are not empty
- if ($sub1 != "" && $sub2 != "") {
- return version_compare($sub1, $sub2, $operator);
- } else {
- $result = version_compare($sub1, $sub2, $operator);
- // reverse the result since version_compare() will determine that "beta" > ""
- return ($operator == "=") ? $result : !$result;
- }
- }
- }
- return version_compare($versions[0]['main'], $versions[1]['main'], $operator);
- }
- public static function parseVersionWithHyphen($version)
- {
- preg_match('/([^-]*)-(.*)/', $version, $matches);
- if (isset($matches[2])) {
- return [
- 'main' => $matches[1],
- 'sub' => $matches[2]
- ];
- }
- return false;
- }
- /**
- * Rename uploaded file
- *
- * @param array $file files uploaded via HTTP POST
- * @return string $hash sha256 hash of file
- */
- public static function upload($file)
- {
- $path = 'tmp'.time();
- $absolute_path = storage_path("textures/$path");
- try {
- if (false === move_uploaded_file($file['tmp_name'], $absolute_path)) {
- throw new \Exception('Failed to remove uploaded files, please check the permission', 1);
- }
- } catch (\Exception $e) {
- Log::warning("Failed to move uploaded file, $e");
- } finally {
- if (file_exists($absolute_path)) {
- $hash = hash_file('sha256', $absolute_path);
- if (!LaravelStorage::disk('textures')->has($hash)) {
- LaravelStorage::disk('textures')->move($path, $hash);
- } else {
- // delete the temp file
- unlink($absolute_path);
- }
- return $hash;
- } else {
- Log::warning("Failed to upload file $path");
- }
- }
- }
- public static function download($url, $path)
- {
- @set_time_limit(0);
- touch($path);
- Log::info("[File Downloader] Download started, source: $url");
- Log::info("[File Downloader] ======================================");
- $ctx = stream_context_create([
- "ssl"=>[
- "verify_peer"=>false,
- "verify_peer_name"=>false,
- ]
- ]);
- if ($fp = fopen($url, "rb", false, $ctx)) {
- if (!$download_fp = fopen($path, "wb", false, $ctx)) {
- return false;
- }
- while (!feof($fp)) {
- if (!file_exists($path)) {
- // cancel downloading if destination is no longer available
- fclose($download_fp);
- return false;
- }
- Log::info("[Download] 1024 bytes wrote");
- fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
- }
- fclose($download_fp);
- fclose($fp);
- Log::info("[File Downloader] Finished downloading, data stored to: $path");
- Log::info("[File Downloader] ===========================================");
- return true;
- } else {
- return false;
- }
- }
- public static function getRemoteFileSize($url)
- {
- $regex = '/^Content-Length: *+\K\d++$/im';
- if (!$fp = @fopen($url, 'rb')) {
- return false;
- }
- if (
- isset($http_response_header) &&
- preg_match($regex, implode("\n", $http_response_header), $matches)
- ) {
- return (int)$matches[0];
- }
- return strlen(stream_get_contents($fp));
- }
- public static function getTimeFormatted($timestamp = 0)
- {
- return ($timestamp == 0) ? Carbon::now()->toDateTimeString() : Carbon::createFromTimestamp($timestamp)->toDateTimeString();
- }
- /**
- * Replace content of string according to given rules.
- *
- * @param string $str
- * @param array $rules
- * @return string
- */
- public static function getStringReplaced($str, $rules)
- {
- foreach ($rules as $search => $replace) {
- $str = str_replace($search, $replace, $str);
- }
- return $str;
- }
- /**
- * Convert error number of uploading files to human-readable text.
- *
- * @param int $errno
- * @return string
- */
- public static function convertUploadFileError($errno = 0)
- {
- $phpFileUploadErrors = [
- 0 => 'There is no error, the file uploaded with success',
- 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
- 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
- 3 => 'The uploaded file was only partially uploaded',
- 4 => 'No file was uploaded',
- 6 => 'Missing a temporary folder',
- 7 => 'Failed to write file to disk.',
- 8 => 'A PHP extension stopped the file upload.',
- ];
- return $phpFileUploadErrors[$errno];
- }
- public static function fieldexists($table, $field)
- {
- $prefix = DB::getTablePrefix();
- $tb = $prefix . $table;
- $res = DB::select("Describe {$tb} {$field}");
- if (!empty($res)) {
- return true;
- } else {
- return false;
- }
- }
- public static function dataEncrypt(&$data)
- {
- foreach ($data as $key => &$val) {
- if (!empty($val)) {
- switch ($key) {
- case 'alipay_app_id':
- case 'rsa_private_key':
- case 'rsa_public_key':
- case 'alipay_number':
- case 'alipay_name':
- $val = encrypt($val);
- break;
- }
- }
- }
- }
- public static function dataDecrypt(&$data)
- {
- foreach ($data as $key => &$val) {
- if (!empty($val)) {
- switch ($key) {
- case 'alipay_app_id':
- case 'rsa_private_key':
- case 'rsa_public_key':
- case 'alipay_number':
- case 'alipay_name':
- $val = decrypt($val);
- break;
- }
- }
- }
- }
- public static function mkdirs($path)
- {
- $filesystem = app(Filesystem::class);
- if (!$filesystem->isDirectory($path)) {
- $filesystem->makeDirectory($path, 0755, true);
- }
- return $filesystem->isDirectory($path);
- }
- /**
- * 清除uniacid
- *
- */
- public static function removeUniacid()
- {
- Cookie::queue('uniacid',null,time() - 2160000, '/');
- Cookie::queue('uniacid',null,time() - 2160000, '/admin');
- Cookie::queue('uniacid',null,time() - 2160000,'/admin/shop');
- }
- /**
- * 保存uniacid
- */
- public static function addUniacid($uniacid = null)
- {
- if (is_null($uniacid)) {
- $uniacid = request()->uniacid;
- }
- Cookie::queue('uniacid',$uniacid,time() + 2160000, '/admin');
- Cookie::queue('uniacid',$uniacid,time() + 2160000,'/admin/shop');
- }
- }
|