blessing-skin-server/app/Services/Utils.php

243 lines
7.2 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Services;
2016-09-30 20:46:15 +08:00
use Log;
2017-08-05 15:03:58 +08:00
use Storage;
2017-01-18 12:56:37 +08:00
use Carbon\Carbon;
2017-01-02 16:23:31 +08:00
use Illuminate\Support\Str;
2016-11-21 21:50:24 +08:00
use App\Exceptions\PrettyPageException;
2016-07-21 22:01:57 +08:00
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 (option('ip_get_method') == "0") {
2018-02-16 17:31:04 +08:00
// Fallback to REMOTE_ADDR
$ip = array_get(
$_SERVER, 'HTTP_X_FORWARDED_FOR',
array_get($_SERVER, 'HTTP_CLIENT_IP', $_SERVER['REMOTE_ADDR'])
);
} else {
$ip = array_get($_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;
2018-02-16 17:31:04 +08:00
if (! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
return true;
2018-02-16 17:31:04 +08:00
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
2017-01-08 13:26:57 +08:00
* 3.2 > 3.2-pre-release
* 3.2 > 3.2-pr8
*
* @param string $version1
* @param string $version2
* @param string $operator
* @return mixed
*/
2017-01-02 16:23:31 +08:00
public static function versionCompare($version1, $version2, $operator = null)
{
$versions = [$version1, $version2];
2018-02-16 17:31:04 +08:00
// Pre-processing for version contains hyphen
2017-01-02 16:23:31 +08:00
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'], '=')) {
2017-01-08 13:26:57 +08:00
$sub1 = $versions[0]['sub'];
$sub2 = $versions[1]['sub'];
2017-01-02 16:23:31 +08:00
// v3.2-pr < v3.2
2017-01-08 13:26:57 +08:00
if ($sub1 != "" || $sub2 != "") {
2018-02-16 17:31:04 +08:00
// If both of sub-versions are not empty
2017-01-08 13:26:57 +08:00
if ($sub1 != "" && $sub2 != "") {
return version_compare($sub1, $sub2, $operator);
} else {
$result = version_compare($sub1, $sub2, $operator);
2018-02-16 17:31:04 +08:00
// Reverse the result since version_compare() will determine that "beta" > ""
2017-01-08 13:26:57 +08:00
return ($operator == "=") ? $result : !$result;
}
2017-01-02 16:23:31 +08:00
}
}
return version_compare($versions[0]['main'], $versions[1]['main'], $operator);
}
public static function parseVersionWithHyphen($version)
{
2017-01-08 13:26:57 +08:00
preg_match('/([^-]*)-(.*)/', $version, $matches);
2017-01-02 16:23:31 +08:00
if (isset($matches[2])) {
return [
'main' => $matches[1],
'sub' => $matches[2]
];
}
return false;
}
/**
* Rename uploaded file
*
2018-02-16 17:31:04 +08:00
* @param \Illuminate\Http\UploadedFile $file The Files uploaded via HTTP POST
* @return string $hash The sha256 hash of file
2017-11-24 18:54:30 +08:00
* @throws \Exception
*/
public static function upload($file)
{
2017-11-24 18:54:30 +08:00
$hash = hash_file('sha256', $file);
2016-09-30 20:46:15 +08:00
try {
2017-11-24 18:54:30 +08:00
$storage = Storage::disk('textures');
2018-02-16 17:31:04 +08:00
if (! $storage->exists($hash)) {
2017-11-24 18:54:30 +08:00
$storage->put($hash, file_get_contents($file));
2016-08-28 20:33:35 +08:00
}
2016-09-30 20:46:15 +08:00
} catch (\Exception $e) {
2017-11-24 18:54:30 +08:00
Log::warning("Failed to upload file {$file->getFilename()}");
throw new \Exception("Failed to upload file {$file->getFilename()}");
}
2017-11-24 18:54:30 +08:00
return $hash;
}
public static function download($url, $path)
{
@set_time_limit(0);
touch($path);
Log::info("[File Downloader] Download started, source: $url");
Log::info("[File Downloader] ======================================");
if ($fp = fopen($url, "rb")) {
2018-02-16 17:31:04 +08:00
if (! $download_fp = fopen($path, "wb")) {
return false;
}
while (!feof($fp)) {
2018-02-16 17:31:04 +08:00
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';
2018-02-16 17:31:04 +08:00
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));
}
2016-07-21 22:01:57 +08:00
public static function getTimeFormatted($timestamp = 0)
{
2017-01-18 12:56:37 +08:00
return ($timestamp == 0) ? Carbon::now()->toDateTimeString() : Carbon::createFromTimestamp($timestamp)->toDateTimeString();
2016-07-21 22:01:57 +08:00
}
2016-08-11 12:47:02 +08:00
/**
2017-01-08 13:44:03 +08:00
* Replace content of string according to given rules.
2016-08-11 12:47:02 +08:00
*
* @param string $str
* @param array $rules
* @return string
*/
2016-08-11 22:56:36 +08:00
public static function getStringReplaced($str, $rules)
2016-08-11 12:47:02 +08:00
{
foreach ($rules as $search => $replace) {
$str = str_replace($search, $replace, $str);
}
return $str;
}
2017-01-08 13:44:03 +08:00
/**
* 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];
}
2016-07-21 22:01:57 +08:00
}