XBackBone/app/helpers.php

278 lines
6.4 KiB
PHP
Raw Normal View History

<?php
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
require __DIR__ . '/../vendor/autoload.php';
if (!function_exists('humanFileSize')) {
/**
* Generate a human readable file size
* @param $size
* @param int $precision
* @return string
*/
function humanFileSize($size, $precision = 2): string
{
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
}
return round($size, $precision) . ' ' . ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
}
}
if (!function_exists('stringToBytes')) {
/**
* @param $str
* @return int|string
*/
function stringToBytes(string $str): int
{
$val = trim($str);
if (is_numeric($val)) {
return (int)$val;
}
$last = strtolower($val[strlen($val) - 1]);
$val = substr($val, 0, -1);
$val = (int)$val;
switch ($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
}
if (!function_exists('removeDirectory')) {
/**
* Remove a directory and it's content
* @param $path
*/
function removeDirectory($path)
{
$files = glob($path . '/*');
foreach ($files as $file) {
is_dir($file) ? removeDirectory($file) : unlink($file);
}
rmdir($path);
return;
}
}
if (!function_exists('cleanDirectory')) {
/**
* Removes all directory contents
* @param $path
*/
function cleanDirectory($path)
{
$directoryIterator = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iteratorIterator as $file) {
if ($file->getFilename() !== '.gitkeep') {
$file->isDir() ? rmdir($file) : unlink($file);
}
}
}
2018-11-14 18:45:47 +00:00
}
if (!function_exists('redirect')) {
/**
* Set the redirect response
* @param \Slim\Http\Response $response
* @param string $path
2018-11-19 18:04:43 +00:00
* @param array $args
2018-11-14 18:45:47 +00:00
* @param null $status
* @return \Slim\Http\Response
*/
2018-11-19 18:04:43 +00:00
function redirect(\Slim\Http\Response $response, string $path, $args = [], $status = null)
2018-11-14 18:45:47 +00:00
{
if (substr($path, 0, 1) === '/' || substr($path, 0, 3) === '../' || substr($path, 0, 2) === './') {
2018-11-19 18:04:43 +00:00
$url = urlFor($path);
} else {
$url = route($path, $args);
}
return $response->withRedirect($url, $status);
2018-11-14 18:45:47 +00:00
}
}
if (!function_exists('asset')) {
/**
* Get the asset link with timestamp
* @param string $path
* @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')) {
/**
* Generate the app url given a path
* @param string $path
2019-01-22 16:39:57 +00:00
* @param string $append
2018-11-14 18:45:47 +00:00
* @return string
*/
2019-01-22 16:39:57 +00:00
function urlFor(string $path, string $append = ''): string
2018-11-14 18:45:47 +00:00
{
global $app;
$baseUrl = $app->getContainer()->get('settings')['base_url'];
2019-01-22 16:39:57 +00:00
return $baseUrl . $path . $append;
2018-11-14 18:45:47 +00:00
}
}
2018-11-19 18:04:43 +00:00
if (!function_exists('route')) {
/**
* Generate the app url given a path
* @param string $path
* @param array $args
2019-01-22 16:39:57 +00:00
* @param string $append
2018-11-19 18:04:43 +00:00
* @return string
*/
2019-01-22 16:39:57 +00:00
function route(string $path, array $args = [], string $append = ''): string
2018-11-19 18:04:43 +00:00
{
global $app;
2019-09-14 12:50:14 +00:00
$uri = $app->getContainer()->get('router')->relativePathFor($path, $args);
2019-01-22 16:39:57 +00:00
return urlFor($uri, $append);
2018-11-19 18:04:43 +00:00
}
}
if (!function_exists('lang')) {
/**
* @param string $key
* @param array $args
* @return string
*/
function lang(string $key, $args = []): string
2018-11-19 18:04:43 +00:00
{
2019-01-10 22:22:19 +00:00
global $app;
return $app->getContainer()->get('lang')->get($key, $args);
2018-11-19 18:04:43 +00:00
}
}
if (!function_exists('isBot')) {
/**
* @param string $userAgent
* @return boolean
*/
function isBot(string $userAgent)
{
$bots = [
'TelegramBot',
'facebookexternalhit/',
'Discordbot/',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0', // The discord service bot?
'Facebot',
2018-12-09 13:58:25 +00:00
'curl/',
'wget/',
];
foreach ($bots as $bot) {
if (stripos($userAgent, $bot) !== false) {
return true;
}
}
return false;
}
}
if (!function_exists('mime2font')) {
2019-01-13 20:27:10 +00:00
/**
* Convert get the icon from the file mimetype
* @param $mime
* @return mixed|string
*/
function mime2font($mime)
{
$classes = [
'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',
'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word',
'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',
2019-01-13 20:27:10 +00:00
'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',
];
foreach ($classes as $fullMime => $class) {
if (strpos($mime, $fullMime) === 0) {
return $class;
}
}
return 'fa-file';
}
2019-01-13 20:27:10 +00:00
}
if (!function_exists('dd')) {
/**
* Dumps all the given vars and halt the execution.
2019-01-13 20:27:10 +00:00
*/
function dd()
{
array_map(function ($x) {
2019-01-24 20:48:22 +00:00
echo '<pre>';
2019-01-13 20:27:10 +00:00
print_r($x);
2019-01-24 20:48:22 +00:00
echo '</pre>';
2019-01-13 20:27:10 +00:00
}, func_get_args());
die();
}
2019-01-22 16:39:57 +00:00
}
if (!function_exists('queryParams')) {
/**
* Get the query parameters of the current request.
* @param array $replace
* @return string
* @throws \Interop\Container\Exception\ContainerException
*/
function queryParams(array $replace = [])
{
global $container;
/** @var \Slim\Http\Request $request */
$request = $container->get('request');
$params = array_replace_recursive($request->getQueryParams(), $replace);
return !empty($params) ? '?' . http_build_query($params) : '';
}
}
if (!function_exists('glob_recursive')) {
/**
* Does not support flag GLOB_BRACE
* @param $pattern
* @param int $flags
* @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));
}
return $files;
}
2018-11-19 18:04:43 +00:00
}