blessing-skin-server/app/helpers.php

365 lines
10 KiB
PHP
Raw Normal View History

<?php
2019-07-02 22:22:05 +08:00
declare(strict_types=1);
2018-08-17 21:41:11 +08:00
use Carbon\Carbon;
use Illuminate\Support\Arr;
2019-06-28 23:47:30 +08:00
use Illuminate\Support\Str;
2016-09-14 22:44:30 +08:00
2018-08-08 18:17:11 +08:00
if (! function_exists('webpack_assets')) {
2019-07-02 22:22:05 +08:00
function webpack_assets(string $relativeUri): string
2018-08-08 18:17:11 +08:00
{
2019-07-23 17:34:04 +08:00
if (env('WEBPACK_ENV', 'production') == 'development') {
2019-07-02 11:36:43 +08:00
// @codeCoverageIgnoreStart
2019-07-02 11:35:29 +08:00
$host = parse_url(url('/'), PHP_URL_HOST);
2019-07-02 11:36:43 +08:00
return "http://$host:8080/$relativeUri";
// @codeCoverageIgnoreEnd
2018-08-08 18:17:11 +08:00
} else {
2019-09-07 10:18:24 +08:00
$path = resolve(\App\Services\Webpack::class)->$relativeUri;
2019-03-16 19:56:50 +08:00
$cdn = option('cdn_address');
2019-04-19 19:36:36 +08:00
2019-03-31 14:38:17 +08:00
return $cdn ? "$cdn/app/$path" : url("/app/$path");
2018-08-08 18:17:11 +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 {
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],
]);
} 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
]);
}
}
}
2017-07-03 20:54:19 +08:00
if (! function_exists('bs_footer_extra')) {
2019-07-02 22:22:05 +08:00
function bs_footer_extra(): string
{
2017-01-20 18:17:56 +08:00
$extraContents = [];
2019-02-27 23:44:50 +08:00
Event::dispatch(new App\Events\RenderingFooter($extraContents));
2017-07-03 20:54:19 +08:00
return implode("\n", $extraContents);
}
}
2017-07-03 20:54:19 +08:00
if (! function_exists('bs_header_extra')) {
2019-07-02 22:22:05 +08:00
function bs_header_extra(): string
{
2017-01-20 18:17:56 +08:00
$extraContents = [];
2019-02-27 23:44:50 +08:00
Event::dispatch(new App\Events\RenderingHeader($extraContents));
2017-07-03 20:54:19 +08:00
return implode("\n", $extraContents);
}
}
if (! function_exists('bs_menu')) {
2019-07-02 22:22:05 +08:00
function bs_menu(string $type): string
{
2016-11-21 21:50:24 +08:00
$menu = config('menu');
2019-04-25 23:24:24 +08:00
switch ($type) {
case 'user':
event(new App\Events\ConfigureUserMenu($menu));
break;
2019-06-28 23:47:30 +08:00
case 'explore':
event(new App\Events\ConfigureExploreMenu($menu));
break;
2019-04-25 23:24:24 +08:00
case 'admin':
event(new App\Events\ConfigureAdminMenu($menu));
break;
}
2016-10-25 22:47:50 +08:00
2018-02-16 17:31:04 +08:00
if (! isset($menu[$type])) {
throw new InvalidArgumentException;
}
2018-02-23 18:12:12 +08:00
$menu[$type] = array_map(function ($item) {
if (Arr::get($item, 'id') === 'plugin-configs') {
$pluginConfigs = app('plugins')->getEnabledPlugins()
2019-08-13 23:06:28 +08:00
->filter(function ($plugin) {
return $plugin->hasConfigView();
})
->map(function ($plugin) {
return [
2018-02-23 18:12:12 +08:00
'title' => trans($plugin->title),
'link' => 'admin/plugins/config/'.$plugin->name,
'icon' => 'fa-circle',
2018-02-23 18:12:12 +08:00
];
2019-08-13 23:06:28 +08:00
});
2018-02-23 18:12:12 +08:00
// Don't display this menu item when no plugin config is available
2019-08-13 23:06:28 +08:00
if ($pluginConfigs->isNotEmpty()) {
$item['children'] = array_merge($item['children'], $pluginConfigs->values()->all());
2018-02-23 18:12:12 +08:00
return $item;
}
} else {
return $item;
}
}, $menu[$type]);
2017-01-20 18:17:56 +08:00
return bs_menu_render($menu[$type]);
2016-12-10 21:30:24 +08:00
}
2019-07-02 22:22:05 +08:00
function bs_menu_render(array $data): string
2016-12-10 21:30:24 +08:00
{
$content = '';
2016-12-10 21:30:24 +08:00
foreach ($data as $key => $value) {
2016-12-17 19:02:56 +08:00
$active = app('request')->is(@$value['link']);
// also set parent as active if any child is active
foreach ((array) @$value['children'] as $childKey => $childValue) {
if (app('request')->is(@$childValue['link'])) {
$active = true;
}
}
$classes = [];
2018-02-23 16:51:50 +08:00
$active ? ($classes[] = 'active menu-open') : null;
isset($value['children']) ? ($classes[] = 'treeview') : null;
2018-02-23 16:51:50 +08:00
$attr = count($classes) ? sprintf(' class="%s"', implode(' ', $classes)) : '';
$content .= "<li{$attr}>";
2016-12-10 21:30:24 +08:00
if (isset($value['children'])) {
2018-08-08 12:30:50 +08:00
$content .= sprintf('<a href="#"><i class="fas %s"></i> &nbsp;<span>%s</span><span class="pull-right-container"><i class="fas fa-angle-left pull-right"></i></span></a>', $value['icon'], trans($value['title']));
2016-12-10 21:30:24 +08:00
// recurse
2018-02-23 16:51:50 +08:00
$content .= '<ul class="treeview-menu">'.bs_menu_render($value['children']).'</ul>';
2016-12-10 21:30:24 +08:00
} else {
2018-02-23 18:12:12 +08:00
if ($value) {
$content .= sprintf(
'<a href="%s" %s><i class="%s %s"></i> &nbsp;<span>%s</span></a>',
2018-02-23 18:12:12 +08:00
url((string) $value['link']),
Arr::get($value, 'new-tab') ? 'target="_blank"' : '',
2018-09-06 23:14:54 +08:00
$value['icon'] == 'fa-circle' ? 'far' : 'fas',
2018-02-23 18:12:12 +08:00
(string) $value['icon'],
trans((string) $value['title'])
);
}
2016-12-10 21:30:24 +08:00
}
2016-12-10 21:30:24 +08:00
$content .= '</li>';
}
2016-12-10 21:30:24 +08:00
return $content;
}
}
2019-05-01 10:14:14 +08:00
if (! function_exists('bs_copyright')) {
2019-07-02 22:22:05 +08:00
function bs_copyright(): string
2019-05-01 10:14:14 +08:00
{
return Arr::get(
[
'Powered with ❤ by Blessing Skin Server.',
'Powered by Blessing Skin Server.',
'Proudly powered by Blessing Skin Server.',
'由 Blessing Skin Server 强力驱动。',
2019-05-19 13:49:44 +08:00
'自豪地采用 Blessing Skin Server。',
2019-05-01 10:14:14 +08:00
],
option_localized('copyright_prefer', 0)
);
}
}
if (! function_exists('option')) {
/**
* 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
* @param raw $raw return raw value without convertion
* @return mixed
*/
function option($key = null, $default = null, $raw = false)
{
$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
return;
}
return $options->get($key, $default, $raw);
}
}
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',
'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.
*
* @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
{
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-09-04 21:28:21 +08:00
$request = request();
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);
}
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
if (! function_exists('nl2p')) {
/**
* Wrap blocks of text (delimited by \n) in p tags (similar to nl2br).
*
* @param string $text
* @return string
*/
2019-07-02 22:22:05 +08:00
function nl2p(string $text): string
{
2018-08-18 09:48:39 +08:00
$parts = explode("\n", $text);
$result = '<p>'.implode('</p><p>', $parts).'</p>';
// Remove empty paragraphs
return str_replace('<p></p>', '', $result);
}
}
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
return $image;
}
}