XBackBone/app/helpers.php

554 lines
13 KiB
PHP
Raw Normal View History

<?php
2019-11-12 23:13:23 +00:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\ServerRequestCreatorFactory;
2019-09-15 14:00:12 +00:00
if (!defined('HUMAN_RANDOM_CHARS')) {
2019-11-12 23:13:23 +00:00
define('HUMAN_RANDOM_CHARS', 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZaeiouAEIOU');
2019-09-15 14:00:12 +00:00
}
if (!function_exists('humanFileSize')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Generate a human readable file size.
*
2019-11-12 23:13:23 +00:00
* @param $size
2020-02-27 14:18:01 +00:00
* @param int $precision
2019-11-20 17:49:31 +00:00
*
2020-02-27 14:18:01 +00:00
* @param bool $iniMode
2019-11-12 23:13:23 +00:00
* @return string
*/
2020-02-27 14:18:01 +00:00
function humanFileSize($size, $precision = 2, $iniMode = false): string
2019-11-12 23:13:23 +00:00
{
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
}
2019-11-20 17:49:31 +00:00
2020-02-27 14:18:01 +00:00
if ($iniMode) {
return round($size, $precision).['B', 'K', 'M', 'G', 'T'][$i];
}
return round($size, $precision).' '.['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
2019-11-12 23:13:23 +00:00
}
}
2019-09-15 14:00:12 +00:00
if (!function_exists('humanRandomString')) {
2019-11-12 23:13:23 +00:00
/**
2020-02-27 14:18:01 +00:00
* @param int $length
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
function humanRandomString(int $length = 10): string
2019-11-12 23:13:23 +00:00
{
$result = '';
$numberOffset = round($length * 0.2);
for ($x = 0; $x < $length - $numberOffset; $x++) {
$result .= ($x % 2) ? HUMAN_RANDOM_CHARS[rand(42, 51)] : HUMAN_RANDOM_CHARS[rand(0, 41)];
}
for ($x = 0; $x < $numberOffset; $x++) {
$result .= rand(0, 9);
}
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $result;
}
2019-09-15 14:00:12 +00:00
}
if (!function_exists('isDisplayableImage')) {
2019-11-12 23:13:23 +00:00
/**
2020-02-27 14:18:01 +00:00
* @param string $mime
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return bool
*/
function isDisplayableImage(?string $mime): bool
2019-11-12 23:13:23 +00:00
{
2019-11-21 17:00:47 +00:00
return in_array($mime, [
2019-11-12 23:13:23 +00:00
'image/apng',
'image/bmp',
'image/gif',
'image/x-icon',
'image/jpeg',
'image/png',
'image/tiff',
'image/webp',
2019-11-21 17:00:47 +00:00
]);
2019-11-12 23:13:23 +00:00
}
2019-09-15 14:00:12 +00:00
}
if (!function_exists('stringToBytes')) {
2019-11-12 23:13:23 +00:00
/**
* @param $str
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return float
*/
function stringToBytes(string $str): float
{
$val = trim($str);
if (is_numeric($val)) {
2019-11-20 17:49:31 +00:00
return (float) $val;
2019-11-12 23:13:23 +00:00
}
2019-11-12 23:13:23 +00:00
$last = strtolower($val[strlen($val) - 1]);
$val = substr($val, 0, -1);
2019-11-20 17:49:31 +00:00
$val = (float) $val;
2019-11-12 23:13:23 +00:00
switch ($last) {
case 't':
$val *= 1024;
2021-07-31 10:55:56 +00:00
// no break
2019-11-12 23:13:23 +00:00
case 'g':
$val *= 1024;
2021-07-31 10:55:56 +00:00
// no break
2019-11-12 23:13:23 +00:00
case 'm':
$val *= 1024;
2021-07-31 10:55:56 +00:00
// no break
2019-11-12 23:13:23 +00:00
case 'k':
$val *= 1024;
}
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $val;
}
}
if (!function_exists('removeDirectory')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Remove a directory and it's content.
*
2019-11-12 23:13:23 +00:00
* @param $path
*/
function removeDirectory($path)
{
2020-04-08 11:19:57 +00:00
cleanDirectory($path, true);
2019-11-12 23:13:23 +00:00
rmdir($path);
}
}
if (!function_exists('cleanDirectory')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Removes all directory contents.
*
2019-11-12 23:13:23 +00:00
* @param $path
2020-04-08 11:19:57 +00:00
* @param bool $all
2019-11-12 23:13:23 +00:00
*/
2020-04-08 11:19:57 +00:00
function cleanDirectory($path, $all = false)
2019-11-12 23:13:23 +00:00
{
$directoryIterator = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iteratorIterator as $file) {
2020-04-08 11:19:57 +00:00
if ($all || $file->getFilename() !== '.gitkeep') {
2019-11-12 23:13:23 +00:00
$file->isDir() ? rmdir($file) : unlink($file);
}
}
}
}
if (!function_exists('resolve')) {
/**
2019-11-20 17:49:31 +00:00
* Resolve a service from de DI container.
*
2020-02-27 14:18:01 +00:00
* @param string $service
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return mixed
*/
function resolve(string $service)
{
global $app;
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $app->getContainer()->get($service);
}
}
if (!function_exists('make')) {
/**
* Resolve a service from de DI container.
*
* @param string $class
* @param array $params
* @return mixed
*/
function make(string $class, array $params = [])
{
global $app;
return $app->getContainer()->make($class, $params);
}
}
2019-11-12 23:13:23 +00:00
if (!function_exists('view')) {
/**
2019-11-20 17:49:31 +00:00
* Render a view to the response body.
*
2019-11-12 23:13:23 +00:00
* @return \App\Web\View
*/
function view()
{
return resolve('view');
}
2018-11-14 18:45:47 +00:00
}
2019-11-12 23:13:23 +00:00
if (!function_exists('redirect')) {
/**
2019-11-20 17:49:31 +00:00
* Set the redirect response.
*
2020-02-27 14:18:01 +00:00
* @param Response $response
* @param string $url
* @param int $status
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return Response
*/
function redirect(Response $response, string $url, $status = 302)
{
return $response
->withHeader('Location', $url)
->withStatus($status);
}
2018-11-14 18:45:47 +00:00
}
if (!function_exists('asset')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Get the asset link with timestamp.
*
2020-02-27 14:18:01 +00:00
* @param string $path
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
function asset(string $path): string
{
return urlFor($path, '?'.filemtime(realpath(BASE_DIR.$path)));
}
}
2018-11-14 18:45:47 +00:00
if (!function_exists('urlFor')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Generate the app url given a path.
*
2020-02-27 14:18:01 +00:00
* @param string $path
* @param string $append
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
function urlFor(string $path = '', string $append = ''): string
2019-11-12 23:13:23 +00:00
{
2019-11-18 10:42:42 +00:00
$baseUrl = resolve('config')['base_url'];
2019-11-20 17:49:31 +00:00
2019-11-18 10:42:42 +00:00
return $baseUrl.$path.$append;
2019-11-12 23:13:23 +00:00
}
2018-11-14 18:45:47 +00:00
}
2018-11-19 18:04:43 +00:00
if (!function_exists('route')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Generate the app url given a path.
*
2020-02-27 14:18:01 +00:00
* @param string $path
* @param array $args
* @param string $append
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
2019-11-21 17:00:47 +00:00
function route(string $path, array $args = [], string $append = ''): string
2019-11-12 23:13:23 +00:00
{
global $app;
$uri = $app->getRouteCollector()->getRouteParser()->relativeUrlFor($path, $args);
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return urlFor($uri, $append);
}
}
if (!function_exists('param')) {
/**
2019-11-20 17:49:31 +00:00
* Get a parameter from the request.
*
2020-02-27 14:18:01 +00:00
* @param Request $request
* @param string $name
* @param null $default
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
function param(Request $request, string $name, $default = null)
{
if ($request->getMethod() === 'GET') {
$params = $request->getQueryParams();
} else {
$params = $request->getParsedBody();
}
if (isset($params[$name])) {
return $params[$name];
}
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $default;
}
}
if (!function_exists('json')) {
/**
2019-11-20 17:49:31 +00:00
* Return a json response.
*
2020-02-27 14:18:01 +00:00
* @param Response $response
2019-11-12 23:13:23 +00:00
* @param $data
2020-02-27 14:18:01 +00:00
* @param int $status
* @param int $options
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return Response
*/
function json(Response $response, $data, int $status = 200, $options = 0): Response
{
$response->getBody()->write(json_encode($data, $options));
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $response
->withStatus($status)
->withHeader('Content-Type', 'application/json');
}
2018-11-19 18:04:43 +00:00
}
if (!function_exists('lang')) {
2019-11-12 23:13:23 +00:00
/**
2020-02-27 14:18:01 +00:00
* @param string $key
* @param array $args
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
2019-11-21 17:00:47 +00:00
function lang(string $key, $args = []): string
2019-11-12 23:13:23 +00:00
{
return resolve('lang')->get($key, $args);
}
}
if (!function_exists('isBot')) {
2019-11-12 23:13:23 +00:00
/**
2020-02-27 14:18:01 +00:00
* @param string $userAgent
2019-11-20 17:49:31 +00:00
*
* @return bool
2019-11-12 23:13:23 +00:00
*/
function isBot(string $userAgent)
{
2019-11-21 17:00:47 +00:00
$bots = [
2019-11-12 23:13:23 +00:00
'TelegramBot',
'facebookexternalhit/',
'Facebot',
'curl/',
'wget/',
2020-03-19 08:39:32 +00:00
'WhatsApp/',
'Slack',
'Twitterbot/',
2021-04-24 11:56:36 +00:00
'discord',
2021-04-25 11:07:50 +00:00
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0' // discord image bot
2019-11-21 17:00:47 +00:00
];
2019-11-12 23:13:23 +00:00
foreach ($bots as $bot) {
if (stripos($userAgent, $bot) !== false) {
return true;
}
}
2019-11-12 23:13:23 +00:00
return false;
}
}
if (!function_exists('isDiscord')) {
/**
* @param string $userAgent
*
* @return bool
*/
function isDiscord(string $userAgent): bool
{
$bots = [
'discord',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0' // discord image bot
];
foreach ($bots as $bot) {
if (stripos($userAgent, $bot) !== false) {
return true;
}
}
return false;
}
}
if (!function_exists('mime2font')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Convert get the icon from the file mimetype.
*
2019-11-12 23:13:23 +00:00
* @param $mime
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return mixed|string
*/
function mime2font($mime)
{
2019-11-21 17:00:47 +00:00
$classes = [
2020-02-27 14:18:01 +00:00
'image' => 'fa-file-image',
'audio' => 'fa-file-audio',
'video' => 'fa-file-video',
'application/pdf' => 'fa-file-pdf',
'application/msword' => 'fa-file-word',
'application/vnd.ms-word' => 'fa-file-word',
'application/vnd.oasis.opendocument.text' => 'fa-file-word',
2019-11-12 23:13:23 +00:00
'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word',
2020-02-27 14:18:01 +00:00
'application/vnd.ms-excel' => 'fa-file-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel',
'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel',
'application/vnd.ms-powerpoint' => 'fa-file-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml' => 'fa-file-powerpoint',
'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint',
'text/plain' => 'fa-file-alt',
'text/html' => 'fa-file-code',
'text/x-php' => 'fa-file-code',
'application/json' => 'fa-file-code',
'application/gzip' => 'fa-file-archive',
'application/zip' => 'fa-file-archive',
'application/octet-stream' => 'fa-file-alt',
2019-11-21 17:00:47 +00:00
];
2019-11-12 23:13:23 +00:00
foreach ($classes as $fullMime => $class) {
if (strpos($mime, $fullMime) === 0) {
return $class;
}
}
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return 'fa-file';
}
2019-01-13 20:27:10 +00:00
}
if (!function_exists('dd')) {
2019-11-12 23:13:23 +00:00
/**
* Dumps all the given vars and halt the execution.
*/
function dd()
{
array_map(function ($x) {
echo '<pre>';
print_r($x);
echo '</pre>';
}, func_get_args());
die();
}
2019-01-22 16:39:57 +00:00
}
if (!function_exists('queryParams')) {
2019-11-12 23:13:23 +00:00
/**
* Get the query parameters of the current request.
2019-11-20 17:49:31 +00:00
*
2020-02-27 14:18:01 +00:00
* @param array $replace
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return string
*/
2019-11-21 17:00:47 +00:00
function queryParams(array $replace = [])
2019-11-12 23:13:23 +00:00
{
2019-11-19 12:59:17 +00:00
$request = ServerRequestCreatorFactory::determineServerRequestCreator()->createServerRequestFromGlobals();
2019-01-22 16:39:57 +00:00
2019-11-12 23:13:23 +00:00
$params = array_replace_recursive($request->getQueryParams(), $replace);
2019-01-22 16:39:57 +00:00
2019-11-12 23:13:23 +00:00
return !empty($params) ? '?'.http_build_query($params) : '';
}
}
2019-11-19 12:59:17 +00:00
if (!function_exists('inPath')) {
/**
2019-11-20 17:49:31 +00:00
* Check if uri start with a path.
*
2020-02-27 14:18:01 +00:00
* @param string $uri
* @param string $path
2019-11-20 17:49:31 +00:00
*
2019-11-19 12:59:17 +00:00
* @return bool
*/
function inPath(string $uri, string $path): bool
{
$path = parse_url(urlFor($path), PHP_URL_PATH);
2019-11-20 17:49:31 +00:00
2020-02-27 14:18:01 +00:00
return substr($uri, 0, strlen($path)) === $path;
2019-11-19 12:59:17 +00:00
}
}
if (!function_exists('glob_recursive')) {
2019-11-12 23:13:23 +00:00
/**
2019-11-20 17:49:31 +00:00
* Does not support flag GLOB_BRACE.
*
2019-11-12 23:13:23 +00:00
* @param $pattern
2020-02-27 14:18:01 +00:00
* @param int $flags
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return array|false
*/
function glob_recursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
2019-11-20 17:49:31 +00:00
2019-11-12 23:13:23 +00:00
return $files;
}
2019-11-18 10:42:42 +00:00
}
if (!function_exists('dsnFromConfig')) {
/**
* Return the database DSN from config.
2019-11-20 17:49:31 +00:00
*
* @param array $config
2019-11-20 17:49:31 +00:00
*
2019-11-18 10:42:42 +00:00
* @return string
*/
2020-04-05 12:53:22 +00:00
function dsnFromConfig(array $config): string
2019-11-18 10:42:42 +00:00
{
2020-04-06 10:43:47 +00:00
$dsn = $config['db']['dsn'];
if ($config['db']['connection'] === 'sqlite') {
if (getcwd() !== BASE_DIR) { // if in installer, change the working dir to the app dir
chdir(BASE_DIR);
}
if (file_exists($config['db']['dsn'])) {
$dsn = realpath($config['db']['dsn']);
}
2020-04-06 10:43:47 +00:00
}
return $config['db']['connection'].':'.$dsn;
2019-11-18 10:42:42 +00:00
}
2019-11-20 17:49:31 +00:00
}
if (!function_exists('platform_mail')) {
/**
* Return the system no-reply mail.
*
* @param string $mailbox
* @return string
*/
function platform_mail($mailbox = 'no-reply'): string
{
return $mailbox.'@'.str_ireplace('www.', '', parse_url(resolve('config')['base_url'], PHP_URL_HOST));
}
}
if (!function_exists('must_be_escaped')) {
/**
* Return the system no-reply mail.
*
* @param $mime
* @return bool
*/
function must_be_escaped($mime): bool
{
$mimes = [
'text/htm',
2021-07-31 10:55:56 +00:00
'image/svg',
];
foreach ($mimes as $m) {
if (stripos($mime, $m) !== false) {
return true;
}
}
return false;
}
}
2021-07-31 10:55:56 +00:00
if (!function_exists('isSecure')) {
/**
* @return bool
*/
function isSecure(): bool
{
2021-07-31 11:00:21 +00:00
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (!empty($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] === 443);
2021-07-31 10:55:56 +00:00
}
}