2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2016-09-30 20:46:15 +08:00
|
|
|
use Log;
|
2017-01-02 16:23:31 +08:00
|
|
|
use Illuminate\Support\Str;
|
2017-01-01 16:37:29 +08:00
|
|
|
use Storage as LaravelStorage;
|
2016-11-21 21:50:24 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
class Utils
|
|
|
|
{
|
2016-12-31 13:56:53 +08:00
|
|
|
/**
|
|
|
|
* Returns the client IP address.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-01-02 16:23:31 +08:00
|
|
|
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'], '=')) {
|
|
|
|
// v3.2-pr < v3.2
|
|
|
|
if ($versions[0]['sub'] != "" || $versions[1]['sub'] != "") {
|
|
|
|
return version_compare($versions[0]['sub'], $versions[1]['sub'], $operator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:16:46 +08:00
|
|
|
/**
|
|
|
|
* Rename uploaded file
|
|
|
|
*
|
|
|
|
* @param array $file files uploaded via HTTP POST
|
|
|
|
* @return string $hash sha256 hash of file
|
|
|
|
*/
|
|
|
|
public static function upload($file)
|
|
|
|
{
|
2016-08-28 20:33:35 +08:00
|
|
|
$path = 'tmp'.time();
|
2016-11-21 21:50:24 +08:00
|
|
|
$absolute_path = storage_path("textures/$path");
|
2016-08-17 13:16:46 +08:00
|
|
|
|
2016-09-30 20:46:15 +08:00
|
|
|
try {
|
|
|
|
if (false === move_uploaded_file($file['tmp_name'], $absolute_path)) {
|
2016-11-21 21:50:24 +08:00
|
|
|
throw new \Exception('Failed to remove uploaded files, please check the permission', 1);
|
2016-08-28 20:33:35 +08:00
|
|
|
}
|
2016-09-30 20:46:15 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::warning("Failed to move uploaded file, $e");
|
|
|
|
} finally {
|
|
|
|
if (file_exists($absolute_path)) {
|
|
|
|
$hash = hash_file('sha256', $absolute_path);
|
|
|
|
|
2017-01-01 16:37:29 +08:00
|
|
|
if (!LaravelStorage::disk('textures')->has($hash)) {
|
|
|
|
LaravelStorage::disk('textures')->move($path, $hash);
|
2016-12-14 22:11:40 +08:00
|
|
|
} else {
|
|
|
|
// delete the temp file
|
|
|
|
unlink($absolute_path);
|
2016-09-30 20:46:15 +08:00
|
|
|
}
|
2016-08-28 20:33:35 +08:00
|
|
|
|
2016-09-30 20:46:15 +08:00
|
|
|
return $hash;
|
|
|
|
} else {
|
|
|
|
Log::warning("Failed to upload file $path");
|
|
|
|
}
|
2016-08-17 13:16:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:54:20 +08:00
|
|
|
public static function download($url, $path)
|
|
|
|
{
|
2016-12-16 22:54:12 +08:00
|
|
|
@set_time_limit(0);
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
Log::info("[File Downloader] Download started, source: $url");
|
|
|
|
Log::info("[File Downloader] ======================================");
|
|
|
|
|
|
|
|
if ($fp = fopen($url, "rb")) {
|
|
|
|
|
|
|
|
if (!$download_fp = fopen($path, "wb")) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
/**
|
|
|
|
* Generate random string
|
|
|
|
*
|
2016-08-18 21:51:39 +08:00
|
|
|
* @param int $length
|
|
|
|
* @param bool $special_chars
|
2016-07-21 22:01:57 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-18 21:51:39 +08:00
|
|
|
public static function generateRndString($length, $special_chars = true)
|
2016-08-17 13:16:46 +08:00
|
|
|
{
|
2016-08-18 21:51:39 +08:00
|
|
|
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
if ($special_chars) $chars .= "!@#$%^&*()-_ []{}<>~`+=,.;:/?|";
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
$rnd_string = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$rnd_string .= $chars[mt_rand(0, strlen($chars) - 1)];
|
|
|
|
}
|
|
|
|
return $rnd_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTimeFormatted($timestamp = 0)
|
|
|
|
{
|
|
|
|
return ($timestamp == 0) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $timestamp);
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:47:02 +08:00
|
|
|
/**
|
|
|
|
* Replace content of string according to given rules
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-12-25 23:21:13 +08:00
|
|
|
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
|
|
|
}
|