2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
use App\Contracts\Model;
|
|
|
|
use Hashids\Hashids;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
2019-11-27 22:19:20 +08:00
|
|
|
use Nwidart\Modules\Facades\Module;
|
2021-03-05 06:08:51 +08:00
|
|
|
use Pdp\Rules;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Global utilities
|
|
|
|
*/
|
|
|
|
class Utils
|
|
|
|
{
|
2020-02-23 05:03:01 +08:00
|
|
|
/**
|
|
|
|
* Generate a new ID with a given length
|
|
|
|
*
|
|
|
|
* @param int [$length]
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generateNewId(int $length = null)
|
|
|
|
{
|
|
|
|
if (!$length) {
|
|
|
|
$length = Model::ID_MAX_LENGTH;
|
|
|
|
}
|
|
|
|
|
2020-03-10 01:16:10 +08:00
|
|
|
$hashids = new Hashids(uniqid(), $length);
|
2020-02-23 05:03:01 +08:00
|
|
|
$mt = str_replace('.', '', microtime(true));
|
|
|
|
return $hashids->encode($mt);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
/**
|
|
|
|
* Returns a 40 character API key that a user can use
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generateApiKey(): string
|
|
|
|
{
|
|
|
|
$key = substr(sha1(time().mt_rand()), 0, 20);
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple check on the first character if it's an object or not
|
|
|
|
*
|
|
|
|
* @param $obj
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isObject($obj): bool
|
|
|
|
{
|
|
|
|
if (!$obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($obj[0] === '{' || $obj[0] === '[') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
/**
|
|
|
|
* Enable the debug toolbar
|
|
|
|
*/
|
|
|
|
public static function enableDebugToolbar()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
app('debugbar')->enable();
|
|
|
|
} catch (BindingResolutionException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the debug toolbar
|
|
|
|
*/
|
|
|
|
public static function disableDebugToolbar()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
app('debugbar')->disable();
|
|
|
|
} catch (BindingResolutionException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* Is the installer enabled?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function installerEnabled()
|
|
|
|
{
|
|
|
|
/** @var \Nwidart\Modules\Module $installer */
|
|
|
|
$installer = Module::find('installer');
|
|
|
|
if (!$installer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $installer->isEnabled();
|
|
|
|
}
|
2020-01-29 01:42:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the domain from a URL
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getRootDomain(string $url): string
|
|
|
|
{
|
2021-03-05 06:08:51 +08:00
|
|
|
if (!str_starts_with($url, 'http')) {
|
|
|
|
$url = 'http://'.$url;
|
2020-01-29 01:42:46 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:08:51 +08:00
|
|
|
$parsed_url = parse_url($url, PHP_URL_HOST);
|
|
|
|
if (empty($parsed_url)) {
|
|
|
|
return '';
|
2020-08-16 01:50:58 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:08:51 +08:00
|
|
|
if (str_ends_with($parsed_url, 'localhost')) {
|
2021-02-14 04:59:19 +08:00
|
|
|
return 'localhost';
|
|
|
|
}
|
|
|
|
|
2021-03-05 06:08:51 +08:00
|
|
|
if (str_ends_with($parsed_url, '/')) {
|
|
|
|
$parsed_url = substr($parsed_url, 0, strlen($parsed_url) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rules = Rules::createFromPath(resource_path('tld/public_suffix_list.dat'));
|
|
|
|
$domain = $rules->resolve($parsed_url);
|
|
|
|
|
|
|
|
$val = $domain->getRegistrableDomain();
|
|
|
|
if (!empty($val)) {
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2020-08-16 01:50:58 +08:00
|
|
|
// Couldn't validate a domain, see if this is an IP address?
|
2021-03-05 06:08:51 +08:00
|
|
|
if (filter_var($parsed_url, FILTER_VALIDATE_IP)) {
|
|
|
|
return $parsed_url;
|
2020-08-16 01:50:58 +08:00
|
|
|
}
|
2021-03-05 06:08:51 +08:00
|
|
|
|
|
|
|
return '';
|
2020-01-29 01:42:46 +08:00
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|