2016-08-29 23:31:43 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-02 22:22:05 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-17 21:41:11 +08:00
|
|
|
use Carbon\Carbon;
|
2016-09-30 16:31:45 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2019-06-28 23:47:30 +08:00
|
|
|
use Illuminate\Support\Str;
|
2016-09-14 22:44:30 +08:00
|
|
|
|
2017-01-08 16:05:54 +08:00
|
|
|
if (! function_exists('plugin')) {
|
2019-08-13 18:42:17 +08:00
|
|
|
function plugin(string $name)
|
2017-01-08 16:05:54 +08:00
|
|
|
{
|
2019-08-13 18:42:17 +08:00
|
|
|
return app('plugins')->get($name);
|
2017-01-08 16:05:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:07:12 +08:00
|
|
|
if (! function_exists('plugin_assets')) {
|
2019-08-15 17:08:25 +08:00
|
|
|
function plugin_assets(string $name, string $relativeUri): string
|
2016-12-31 16:07:12 +08:00
|
|
|
{
|
2019-08-15 17:08:25 +08:00
|
|
|
$plugin = plugin($name);
|
|
|
|
if ($plugin) {
|
2017-01-08 16:05:54 +08:00
|
|
|
return $plugin->assets($relativeUri);
|
2016-12-31 16:07:12 +08:00
|
|
|
} else {
|
2019-03-02 22:58:37 +08:00
|
|
|
throw new InvalidArgumentException('No such plugin.');
|
2016-12-31 16:07:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:39:45 +08:00
|
|
|
if (! function_exists('json')) {
|
|
|
|
function json()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
|
2019-09-10 19:52:17 +08:00
|
|
|
if (count($args) === 1 && is_array($args[0])) {
|
|
|
|
return response()->json($args[0]);
|
|
|
|
} elseif (count($args) === 3 && is_array($args[2])) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// The third argument is array of extra fields
|
2019-09-10 19:52:17 +08:00
|
|
|
return response()->json([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => $args[1],
|
|
|
|
'message' => $args[0],
|
2019-04-23 19:14:41 +08:00
|
|
|
'data' => $args[2],
|
|
|
|
]);
|
2016-09-30 16:31:45 +08:00
|
|
|
} else {
|
2019-09-10 19:52:17 +08:00
|
|
|
return response()->json([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => Arr::get($args, 1, 1),
|
|
|
|
'message' => $args[0],
|
2016-09-10 21:39:45 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 11:04:08 +08:00
|
|
|
|
2016-09-15 20:35:46 +08:00
|
|
|
if (! function_exists('option')) {
|
2016-12-29 23:14:09 +08:00
|
|
|
/**
|
|
|
|
* Get / set the specified option value.
|
|
|
|
*
|
|
|
|
* If an array is passed as the key, we will assume you want to set an array of values.
|
|
|
|
*
|
|
|
|
* @param array|string $key
|
|
|
|
* @param mixed $default
|
2017-01-02 10:39:50 +08:00
|
|
|
* @param raw $raw return raw value without convertion
|
2016-12-29 23:14:09 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-01-02 10:39:50 +08:00
|
|
|
function option($key = null, $default = null, $raw = false)
|
2016-09-15 20:35:46 +08:00
|
|
|
{
|
2016-11-18 16:46:58 +08:00
|
|
|
$options = app('options');
|
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($key)) {
|
2019-03-23 15:44:16 +08:00
|
|
|
$options->set($key);
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2016-12-31 23:36:44 +08:00
|
|
|
return;
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-02 10:39:50 +08:00
|
|
|
return $options->get($key, $default, $raw);
|
2016-09-15 20:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 22:35:04 +08:00
|
|
|
|
2018-07-06 14:46:25 +08:00
|
|
|
if (! function_exists('option_localized')) {
|
|
|
|
function option_localized($key = null, $default = null, $raw = false)
|
|
|
|
{
|
|
|
|
return option($key.'_'.config('app.locale'), option($key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
if (! function_exists('humanize_db_type')) {
|
2019-07-02 22:22:05 +08:00
|
|
|
function humanize_db_type($type = null): string
|
2018-02-22 21:26:23 +08:00
|
|
|
{
|
|
|
|
$map = [
|
2019-08-28 14:52:51 +08:00
|
|
|
'mysql' => 'MySQL/MariaDB',
|
2018-02-22 21:26:23 +08:00
|
|
|
'sqlite' => 'SQLite',
|
2019-03-02 22:58:37 +08:00
|
|
|
'pgsql' => 'PostgreSQL',
|
2018-02-22 21:26:23 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$type = $type ?: config('database.default');
|
|
|
|
|
|
|
|
return Arr::get($map, $type, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! function_exists('get_db_config')) {
|
|
|
|
function get_db_config($type = null)
|
|
|
|
{
|
|
|
|
$type = $type ?: config('database.default');
|
|
|
|
|
|
|
|
return config("database.connections.$type");
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 21:41:11 +08:00
|
|
|
|
2018-08-17 22:54:26 +08:00
|
|
|
if (! function_exists('get_datetime_string')) {
|
|
|
|
/**
|
|
|
|
* Get date time string in "Y-m-d H:i:s" format.
|
|
|
|
*
|
2019-03-02 22:58:37 +08:00
|
|
|
* @param int $timestamp
|
2018-08-17 22:54:26 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
function get_datetime_string($timestamp = 0): string
|
2019-03-02 22:58:37 +08:00
|
|
|
{
|
2018-08-17 22:54:26 +08:00
|
|
|
return $timestamp == 0 ? Carbon::now()->toDateTimeString() : Carbon::createFromTimestamp($timestamp)->toDateTimeString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! function_exists('get_client_ip')) {
|
|
|
|
/**
|
|
|
|
* Return the client IP address.
|
|
|
|
*
|
|
|
|
* We define this function because Symfony's "Request::getClientIp()" method
|
|
|
|
* needs "setTrustedProxies()", which sucks when load balancer is enabled.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
function get_client_ip(): string
|
2019-03-02 22:58:37 +08:00
|
|
|
{
|
2019-09-04 21:28:21 +08:00
|
|
|
$request = request();
|
2019-03-02 22:58:37 +08:00
|
|
|
if (option('ip_get_method') == '0') {
|
2019-09-04 21:28:21 +08:00
|
|
|
$ip = $request->server('HTTP_X_FORWARDED_FOR')
|
|
|
|
?? $request->server('HTTP_CLIENT_IP')
|
|
|
|
?? $request->server('REMOTE_ADDR');
|
2018-08-17 22:54:26 +08:00
|
|
|
} else {
|
2019-09-04 21:28:21 +08:00
|
|
|
$ip = $request->server('REMOTE_ADDR');
|
2018-08-17 22:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! function_exists('get_string_replaced')) {
|
|
|
|
/**
|
|
|
|
* Replace content of string according to given rules.
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @param array $rules
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
function get_string_replaced(string $str, array $rules): string
|
2018-08-17 22:54:26 +08:00
|
|
|
{
|
|
|
|
foreach ($rules as $search => $replace) {
|
|
|
|
$str = str_replace($search, $replace, $str);
|
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2018-08-17 22:54:26 +08:00
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! function_exists('is_request_secure')) {
|
|
|
|
/**
|
|
|
|
* Check whether the request is secure or not.
|
|
|
|
* True is always returned when "X-Forwarded-Proto" header is set.
|
|
|
|
*
|
|
|
|
* We define this function because Symfony's "Request::isSecure()" method
|
|
|
|
* needs "setTrustedProxies()" which sucks when load balancer is enabled.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
function is_request_secure(): bool
|
2018-08-17 22:54:26 +08:00
|
|
|
{
|
2019-09-04 21:28:21 +08:00
|
|
|
$request = request();
|
2019-09-08 09:44:00 +08:00
|
|
|
return $request->server('HTTPS') === 'on'
|
|
|
|
|| $request->server('HTTP_X_FORWARDED_PROTO') === 'https'
|
|
|
|
|| $request->server('HTTP_X_FORWARDED_SSL') === 'on';
|
2018-08-17 22:54:26 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-18 09:48:39 +08:00
|
|
|
|
2019-03-21 12:44:15 +08:00
|
|
|
if (! function_exists('png')) {
|
|
|
|
function png($resource)
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
imagepng($resource);
|
|
|
|
$image = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
imagedestroy($resource);
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-21 12:44:15 +08:00
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
}
|