blessing-skin-server/app/helpers.php

506 lines
14 KiB
PHP
Raw Normal View History

<?php
2018-08-17 21:41:11 +08:00
use Carbon\Carbon;
2017-01-08 14:15:55 +08:00
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
2016-09-14 22:44:30 +08:00
if (! function_exists('get_base_url')) {
function get_base_url()
{
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$base_url .= $_SERVER['SERVER_NAME'];
$base_url .= ($_SERVER['SERVER_PORT'] == '80') ? '' : (':'.$_SERVER['SERVER_PORT']);
return $base_url;
}
}
if (! function_exists('get_current_url')) {
function get_current_url()
{
return get_base_url().$_SERVER['REQUEST_URI'];
}
}
2016-09-05 22:18:34 +08:00
if (! function_exists('avatar')) {
2017-01-08 14:15:55 +08:00
function avatar(User $user, $size)
2016-09-05 22:18:34 +08:00
{
$fname = base64_encode($user->email).'.png?tid='.$user->getAvatarId();
2016-09-05 22:18:34 +08:00
return url("avatar/$size/$fname");
}
}
if (! function_exists('assets')) {
2017-01-08 16:05:54 +08:00
function assets($relativeUri)
2016-09-05 22:18:34 +08:00
{
2018-02-16 17:31:04 +08:00
// Add query string to fresh cache
2017-07-03 20:54:19 +08:00
if (Str::startsWith($relativeUri, 'css') || Str::startsWith($relativeUri, 'js')) {
return url("resources/assets/dist/$relativeUri").'?v='.config('app.version');
2017-01-08 16:05:54 +08:00
} elseif (Str::startsWith($relativeUri, 'lang')) {
return url("resources/$relativeUri");
2016-09-25 00:03:07 +08:00
} else {
2017-01-08 16:05:54 +08:00
return url("resources/assets/$relativeUri");
2016-09-14 22:44:30 +08:00
}
2016-09-05 22:18:34 +08:00
}
}
2016-09-10 21:39:45 +08:00
2018-08-08 18:17:11 +08:00
if (! function_exists('webpack_assets')) {
function webpack_assets($relativeUri)
{
if (app()->environment('development')) {
return "http://127.0.0.1:8080/public/$relativeUri";
} else {
2019-03-16 19:56:50 +08:00
$cdn = option('cdn_address');
return $cdn ? "$cdn/app/$relativeUri" : url("app/$relativeUri");
2018-08-08 18:17:11 +08:00
}
}
}
2017-01-08 16:05:54 +08:00
if (! function_exists('plugin')) {
2017-11-16 10:09:58 +08:00
/**
* @param string $id
* @return \App\Services\Plugin
*/
2017-01-08 16:05:54 +08:00
function plugin($id)
{
return app('plugins')->getPlugin($id);
}
}
2016-12-31 16:07:12 +08:00
if (! function_exists('plugin_assets')) {
2017-01-08 16:05:54 +08:00
function plugin_assets($id, $relativeUri)
2016-12-31 16:07:12 +08:00
{
2017-01-08 16:05:54 +08:00
if ($plugin = plugin($id)) {
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();
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
return Response::json(array_merge([
2016-09-10 21:39:45 +08:00
'errno' => $args[1],
'msg' => $args[0],
], $args[2]));
} else {
return Response::json([
'errno' => Arr::get($args, 1, 1),
'msg' => $args[0],
2016-09-10 21:39:45 +08:00
]);
}
}
}
if (! function_exists('bs_hash_file')) {
function bs_hash_file(Illuminate\Http\UploadedFile $file)
{
// Try to get hash from event listener
$responses = event(new App\Events\HashingFile($file));
if (isset($responses[0]) && is_string($responses[0])) {
return $responses[0];
}
// Default to sha256 hash
return hash_file('sha256', $file);
}
}
2017-07-03 20:54:19 +08:00
if (! function_exists('bs_footer_extra')) {
function bs_footer_extra()
{
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')) {
function bs_header_extra()
{
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);
}
}
2016-12-31 13:36:46 +08:00
if (! function_exists('bs_favicon')) {
function bs_favicon()
{
2018-02-16 17:31:04 +08:00
// Fallback to default favicon
2018-08-19 19:02:22 +08:00
$url = Str::startsWith($url = (option('favicon_url') ?: config('options.favicon_url')), 'http') ? $url : url($url);
2017-01-20 18:17:56 +08:00
return <<< ICONS
<link rel="shortcut icon" href="$url">
<link rel="icon" type="image/png" href="$url" sizes="192x192">
<link rel="apple-touch-icon" href="$url" sizes="180x180">
ICONS;
2016-12-31 13:36:46 +08:00
}
}
if (! function_exists('bs_menu')) {
2016-09-30 17:00:41 +08:00
function bs_menu($type)
{
2016-11-21 21:50:24 +08:00
$menu = config('menu');
Event::dispatch($type == 'user' ? new App\Events\ConfigureUserMenu($menu)
2016-11-17 17:32:12 +08:00
: new App\Events\ConfigureAdminMenu($menu));
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') {
$availablePluginConfigs = [];
foreach (app('plugins')->getEnabledPlugins() as $plugin) {
if ($plugin->hasConfigView()) {
$availablePluginConfigs[] = [
'title' => trans($plugin->title),
'link' => 'admin/plugins/config/'.$plugin->name,
'icon' => 'fa-circle',
2018-02-23 18:12:12 +08:00
];
}
}
// Don't display this menu item when no plugin config is available
if (count($availablePluginConfigs) > 0) {
$item['children'] = array_merge($item['children'], $availablePluginConfigs);
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
}
function bs_menu_render($data)
{
$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(
2018-09-06 23:14:54 +08:00
'<a href="%s"><i class="%s %s"></i> &nbsp;<span>%s</span></a>',
2018-02-23 18:12:12 +08:00
url((string) $value['link']),
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;
}
}
if (! function_exists('bs_copyright')) {
2016-12-27 23:05:09 +08:00
function bs_copyright($prefer = null)
{
$prefer = is_null($prefer) ? option_localized('copyright_prefer', 0) : $prefer;
2016-12-27 23:05:09 +08:00
$base64CopyrightText = [
'UG93ZXJlZCB3aXRoIOKdpCBieSA8YSBocmVmPSJodHRwczovL2dpdGh1Yi5jb20vcHJpbnRlbXB3L2JsZXNzaW5nLXNraW4tc2VydmVyIj5CbGVzc2luZyBTa2luIFNlcnZlcjwvYT4u',
'UG93ZXJlZCBieSA8YSBocmVmPSJodHRwczovL2dpdGh1Yi5jb20vcHJpbnRlbXB3L2JsZXNzaW5nLXNraW4tc2VydmVyIj5CbGVzc2luZyBTa2luIFNlcnZlcjwvYT4u',
2017-01-02 00:14:53 +08:00
'UHJvdWRseSBwb3dlcmVkIGJ5IDxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS9wcmludGVtcHcvYmxlc3Npbmctc2tpbi1zZXJ2ZXIiPkJsZXNzaW5nIFNraW4gU2VydmVyPC9hPi4=',
2016-12-27 23:05:09 +08:00
'55SxIDxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS9wcmludGVtcHcvYmxlc3Npbmctc2tpbi1zZXJ2ZXIiPkJsZXNzaW5nIFNraW4gU2VydmVyPC9hPiDlvLrlipvpqbHliqgu',
'6Ieq6LGq5Zyw6YeH55SoIDxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS9wcmludGVtcHcvYmxlc3Npbmctc2tpbi1zZXJ2ZXIiPkJsZXNzaW5nIFNraW4gU2VydmVyPC9hPi4=',
2016-12-27 23:05:09 +08:00
];
return base64_decode(Arr::get($base64CopyrightText, $prefer, $base64CopyrightText[0]));
}
}
if (! function_exists('bs_custom_copyright')) {
function bs_custom_copyright()
{
2018-08-17 22:54:26 +08:00
return get_string_replaced(option_localized('copyright_text'), [
2018-07-06 14:46:25 +08:00
'{site_name}' => option_localized('site_name'),
'{site_url}' => option('site_url'),
2018-07-06 14:46:25 +08:00
]);
}
}
if (! function_exists('bs_nickname')) {
2017-01-08 14:15:55 +08:00
function bs_nickname(User $user = null)
{
2018-09-14 15:46:44 +08:00
$user = $user ?: auth()->user();
return ($user->getNickName() == '') ? $user->email : $user->getNickName();
}
}
2017-01-08 14:15:55 +08:00
if (! function_exists('bs_role')) {
function bs_role(User $user = null)
{
2018-09-14 15:46:44 +08:00
$user = $user ?: auth()->user();
2017-01-08 14:15:55 +08:00
$roles = [
User::NORMAL => 'normal',
User::BANNED => 'banned',
User::ADMIN => 'admin',
User::SUPER_ADMIN => 'super-admin',
2017-01-08 14:15:55 +08:00
];
$role = Arr::get($roles, $user->getPermission());
return trans("admin.users.status.$role");
}
}
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)) {
foreach ($key as $innerKey => $innerValue) {
$options->set($innerKey, $innerValue);
}
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));
}
}
2016-10-02 20:30:27 +08:00
if (! function_exists('validate')) {
function validate($value, $type)
{
switch ($type) {
case 'email':
return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
break;
default:
// code...
2016-10-02 20:30:27 +08:00
break;
}
}
}
2016-10-23 11:41:52 +08:00
2018-07-21 16:55:36 +08:00
if (! function_exists('humanize_db_type')) {
function humanize_db_type($type = null)
2018-02-22 21:26:23 +08:00
{
$map = [
'mysql' => 'MySQL',
'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
if (! function_exists('format_http_date')) {
/**
* Format a UNIX timestamp to string for HTTP headers.
*
* e.g. Wed, 21 Oct 2015 07:28:00 GMT
*
* @param int $timestamp
* @return string
*/
function format_http_date($timestamp)
{
2018-08-17 21:41:11 +08:00
return Carbon::createFromTimestampUTC($timestamp)->format('D, d M Y H:i:s \G\M\T');
}
}
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
*/
function get_datetime_string($timestamp = 0)
{
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
*/
function get_client_ip()
{
if (option('ip_get_method') == '0') {
2018-08-17 22:54:26 +08:00
// Use `HTTP_X_FORWARDED_FOR` if available first
2019-02-27 23:44:50 +08:00
$ip = Arr::get(
2018-08-17 22:54:26 +08:00
$_SERVER,
'HTTP_X_FORWARDED_FOR',
// Fallback to `HTTP_CLIENT_IP`
2019-02-27 23:44:50 +08:00
Arr::get(
2018-08-17 22:54:26 +08:00
$_SERVER,
'HTTP_CLIENT_IP',
// Fallback to `REMOTE_ADDR`
2019-02-27 23:44:50 +08:00
Arr::get($_SERVER, 'REMOTE_ADDR')
2018-08-17 22:54:26 +08:00
)
);
} else {
2019-02-27 23:44:50 +08:00
$ip = Arr::get($_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
*/
function get_string_replaced($str, $rules)
{
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
*/
function is_request_secure()
{
if (Arr::get($_SERVER, 'HTTPS') == 'on') {
2018-08-17 22:54:26 +08:00
return true;
}
2018-08-17 22:54:26 +08:00
if (Arr::get($_SERVER, 'HTTP_X_FORWARDED_PROTO') == 'https') {
2018-08-17 22:54:26 +08:00
return true;
}
2018-08-17 22:54:26 +08:00
if (Arr::get($_SERVER, 'HTTP_X_FORWARDED_SSL') == 'on') {
2018-08-17 22:54:26 +08:00
return true;
}
2018-08-17 22:54:26 +08:00
return false;
}
}
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
*/
function nl2p($text)
{
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);
return $image;
}
}