safety functions helper

This commit is contained in:
Peter Papp
2021-07-09 12:05:09 +02:00
parent 5215d7d38d
commit 82019f4833

View File

@@ -16,64 +16,73 @@ use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Database\Eloquent\ModelNotFoundException;
/**
if (!function_exists('obfuscate_email')) {
/**
* Obfuscate email
*
* @param $email
* @return string
*/
function obfuscate_email($email)
{
function obfuscate_email($email)
{
$em = explode('@', $email);
$name = implode('@', array_slice($em, 0, count($em) - 1));
$len = floor(strlen($name) / 2);
return substr($name, 0, $len) . str_repeat('*', $len) . '@' . end($em);
}
}
/**
if (!function_exists('get_setting')) {
/**
* Get single value from settings table
*
* @param $setting
* @return |null
*/
function get_setting($setting)
{
function get_setting($setting)
{
return Setting::find($setting)->value ?? null;
}
}
/**
if (!function_exists('get_settings_in_json')) {
/**
* Get all app settings and return them as json
*/
function get_settings_in_json()
{
function get_settings_in_json()
{
return json_decode(
Setting::all()
->pluck('value', 'name')
->toJson()
);
}
}
/**
if (!function_exists('get_setup_status')) {
/**
* Check if setup wizard was passed
*
* @return string
*/
function get_setup_status()
{
function get_setup_status()
{
$setup_success = get_setting('setup_wizard_success');
return boolval($setup_success) ? 'setup-done' : 'setup-disclaimer';
}
}
/**
if (!function_exists('add_paragraphs')) {
/**
* Create paragraph from text
*
* @param $str
* @return mixed|null|string|string[]
*/
function add_paragraphs($str)
{
function add_paragraphs($str)
{
// Trim whitespace
if (($str = trim($str)) === '') {
return '';
@@ -111,17 +120,19 @@ function add_paragraphs($str)
$str = preg_replace('~(?<!\n)\n(?!\n)~', "<br>\n", $str);
return $str;
}
}
/**
if (!function_exists('setEnvironmentValue')) {
/**
* Set environment value
*
* @param $key
* @param $value
* @return bool
*/
function setEnvironmentValue(array $values)
{
function setEnvironmentValue(array $values)
{
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
@@ -139,150 +150,173 @@ function setEnvironmentValue(array $values)
$str = substr($str, 0, -1);
return ! (! file_put_contents($envFile, $str))
;
return !(!file_put_contents($envFile, $str));
}
}
/**
if (!function_exists('get_invoice_number')) {
/**
* Get invoice number
*
* @return string
*/
function get_invoice_number()
{
function get_invoice_number()
{
$invoices = \App\Invoice::all();
if ($invoices->isEmpty()) {
return now()->year . '001';
}
return (int) $invoices->last()->order + 1;
}
/**
* Forget many cache keys at once
* @param $cache
*/
function cache_forget_many($cache)
{
foreach ($cache as $item) {
\Illuminate\Support\Facades\Cache::forget($item);
return (int)$invoices->last()->order + 1;
}
}
/**
if (!function_exists('cache_forget_many')) {
/**
* Forget many cache keys at once
* @param $cache
*/
function cache_forget_many($cache)
{
foreach ($cache as $item) {
\Illuminate\Support\Facades\Cache::forget($item);
}
}
}
if (!function_exists('get_storage')) {
/**
* Get app version from config
*
* @return \Illuminate\Config\Repository|mixed
*/
function get_storage()
{
function get_storage()
{
return env('FILESYSTEM_DRIVER');
}
}
/**
if (!function_exists('is_storage_driver')) {
/**
* Check if is running AWS s3 as storage
*
* @return bool
*/
function is_storage_driver($driver)
{
function is_storage_driver($driver)
{
if (is_array($driver)) {
return in_array(config('filesystems.default'), $driver);
}
return config('filesystems.default') === $driver;
}
}
/**
if (!function_exists('get_version')) {
/**
* Get app version from config
*
* @return \Illuminate\Config\Repository|mixed
*/
function get_version()
{
function get_version()
{
return config('vuefilemanager.version');
}
}
/**
if (!function_exists('is_demo')) {
/**
* Check if is demo
*
* @return bool
*/
function is_demo()
{
function is_demo()
{
return config('vuefilemanager.is_demo');
}
}
/**
if (!function_exists('is_demo_account')) {
/**
* Check if is demo
*
* @param $email
* @return mixed
*/
function is_demo_account($email)
{
function is_demo_account($email)
{
return config('vuefilemanager.is_demo') && $email === 'howdy@hi5ve.digital';
}
}
/**
if (!function_exists('get_item')) {
/**
* Get folder or file item
*
* @param $type
* @param $id
* @return \Illuminate\Database\Eloquent\Builder|Model
*/
function get_item($type, $id)
{
function get_item($type, $id)
{
$model = strtolower($type) === 'folder' ? 'Folder' : 'File';
return ("App\\Models\\$model")::find($id);
}
}
/**
if (!function_exists('get_shared')) {
/**
* Get shared token
*
* @param $token
* @return \Illuminate\Database\Eloquent\Builder|Model
*/
function get_shared($token)
{
function get_shared($token)
{
return Share::whereToken($token)
->firstOrFail();
}
}
/**
if (!function_exists('is_editor')) {
/**
* Check if shared permission is editor
*
* @param $shared
* @return bool
*/
function is_editor($shared)
{
function is_editor($shared)
{
return $shared->permission === 'editor';
}
}
/**
if (!function_exists('is_visitor')) {
/**
* Check if shared permission is visitor
*
* @param $shared
* @return bool
*/
function is_visitor($shared)
{
function is_visitor($shared)
{
return $shared->permission === 'visitor';
}
}
/**
if (!function_exists('store_avatar')) {
/**
* Store user avatar to storage
*
* @param $request
* @param $name
* @return string|null
*/
function store_avatar($request, $name)
{
if (! $request->hasFile($name)) {
function store_avatar($request, $name)
{
if (!$request->hasFile($name)) {
return null;
}
@@ -308,18 +342,20 @@ function store_avatar($request, $name)
// Return path to image
return "avatars/$image_path";
}
}
/**
if (!function_exists('store_system_image')) {
/**
* Store system image
*
* @param $request
* @param $name
* @return string|null
*/
function store_system_image($request, $name)
{
if (! $request->hasFile($name)) {
function store_system_image($request, $name)
{
if (!$request->hasFile($name)) {
return null;
}
@@ -333,16 +369,18 @@ function store_system_image($request, $name)
// Return path to image
return "system/$filename";
}
}
/**
if (!function_exists('make_single_input')) {
/**
* Make input from request
*
* @param $request
* @return array
*/
function make_single_input($request)
{
function make_single_input($request)
{
// Create container
$data = [];
@@ -351,31 +389,35 @@ function make_single_input($request)
// Return input
return $data;
}
}
/**
if (!function_exists('format_gigabytes')) {
/**
* Format integer to gigabytes
*
* @param $gigabytes
* @return string
*/
function format_gigabytes($gigabytes)
{
function format_gigabytes($gigabytes)
{
if ($gigabytes >= 1000) {
return Metric::gigabytes($gigabytes)->format('Tb/');
}
return Metric::gigabytes($gigabytes)->format('GB/');
}
}
/**
if (!function_exists('format_megabytes')) {
/**
* Format string to formated megabytes string
*
* @param $megabytes
* @return string
*/
function format_megabytes($megabytes)
{
function format_megabytes($megabytes)
{
if ($megabytes >= 1000) {
return $megabytes / 1000 . 'GB';
}
@@ -385,28 +427,32 @@ function format_megabytes($megabytes)
}
return $megabytes . 'MB';
}
}
/**
if (!function_exists('format_bytes')) {
/**
* Convert megabytes to bytes
*
* @param $megabytes
* @return int|string
*/
function format_bytes($megabytes)
{
function format_bytes($megabytes)
{
return Metric::megabytes($megabytes)->numberOfBytes();
}
}
/**
if (!function_exists('get_storage_fill_percentage')) {
/**
* Get storage usage in percent
*
* @param $used
* @param $capacity
* @return string
*/
function get_storage_fill_percentage($used, $capacity)
{
function get_storage_fill_percentage($used, $capacity)
{
// Format gigabytes to bytes
$total = intval(Metric::gigabytes($capacity)->numberOfBytes());
@@ -418,18 +464,20 @@ function get_storage_fill_percentage($used, $capacity)
}
// Return in 2 decimal
return number_format((float) $progress, 2, '.', '');
return number_format((float)$progress, 2, '.', '');
}
}
/**
if (!function_exists('user_storage_percentage')) {
/**
* Get user capacity fill by percentage
*
* @param $id
* @param null $additionals
* @return string
*/
function user_storage_percentage($id, $additionals = null)
{
function user_storage_percentage($id, $additionals = null)
{
$user = User::findOrFail($id);
$used = $user->used_capacity;
@@ -439,17 +487,19 @@ function user_storage_percentage($id, $additionals = null)
}
return get_storage_fill_percentage($used, $user->settings->storage_capacity);
}
}
/**
if (!function_exists('recursiveFind')) {
/**
* Find all key values in recursive array
*
* @param array $array
* @param $needle
* @return array
*/
function recursiveFind(array $array, $needle)
{
function recursiveFind(array $array, $needle)
{
$iterator = new RecursiveArrayIterator($array);
$recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$aHitList = [];
@@ -461,15 +511,17 @@ function recursiveFind(array $array, $needle)
}
return $aHitList;
}
}
/**
if (!function_exists('appeared_once')) {
/**
* Get values which appears only once in array
* @param $arr
* @return array
*/
function appeared_once($arr)
{
function appeared_once($arr)
{
$array_count_values = array_count_values($arr);
$single_time_comming_values_array = [];
@@ -481,42 +533,48 @@ function appeared_once($arr)
}
return $single_time_comming_values_array;
}
}
/**
if (!function_exists('filter_folders_ids')) {
/**
* @param $folders
* @param string $by_column
* @return array
*/
function filter_folders_ids($folders, $by_column = 'id')
{
function filter_folders_ids($folders, $by_column = 'id')
{
$folder_ids = recursiveFind($folders->toArray(), $by_column);
return appeared_once($folder_ids);
}
}
/**
if (!function_exists('format_date')) {
/**
* Format localized date
*
* @param $date
* @param string $format
* @return string
*/
function format_date($date, $format = '%d. %B. %Y, %H:%M')
{
function format_date($date, $format = '%d. %B. %Y, %H:%M')
{
$start = Carbon::parse($date);
return $start->formatLocalized($format);
}
}
/**
if (!function_exists('get_file_type')) {
/**
* Get file type from mimetype
*
* @param $file_mimetype
* @return string
*/
function get_file_type($file_mimetype)
{
function get_file_type($file_mimetype)
{
// Get mimetype from file
$mimetype = explode('/', $file_mimetype);
@@ -525,33 +583,39 @@ function get_file_type($file_mimetype)
}
return 'file';
}
}
/**
if (!function_exists('map_language_translations')) {
/**
* It map language translations as language key and language value
*
* @param $translations
* @return mixed
*/
function map_language_translations($translations): Collection
{
function map_language_translations($translations): Collection
{
return $translations->map(function ($string) {
return [$string->key => $string->value];
})->collapse();
}
}
/**
if (!function_exists('get_file_type_from_mimetype')) {
/**
* Get file type from mimetype
*
* @param $mimetype
* @return mixed
*/
function get_file_type_from_mimetype($mimetype)
{
function get_file_type_from_mimetype($mimetype)
{
return explode('/', $mimetype)[1];
}
}
/**
if (!function_exists('get_pretty_name')) {
/**
* Format pretty name file
*
* @param $basename
@@ -559,8 +623,8 @@ function get_file_type_from_mimetype($mimetype)
* @param $mimetype
* @return string
*/
function get_pretty_name($basename, $name, $mimetype)
{
function get_pretty_name($basename, $name, $mimetype)
{
$file_extension = substr(strrchr($basename, '.'), 1);
if (strpos($name, $file_extension) !== false) {
@@ -572,16 +636,18 @@ function get_pretty_name($basename, $name, $mimetype)
}
return $name . '.' . $mimetype;
}
}
/**
if (!function_exists('get_image_meta_data')) {
/**
* Get exif data from jpeg image
*
* @param $file
* @return array|null
*/
function get_image_meta_data($file)
{
function get_image_meta_data($file)
{
if (get_file_type_from_mimetype($file->getMimeType()) === 'jpeg') {
try {
// Try to get the exif data
@@ -590,36 +656,42 @@ function get_image_meta_data($file)
return null;
}
}
}
}
/**
if (!function_exists('get_default_language_translations')) {
/**
* @return Collection
*/
function get_default_language_translations(): Collection
{
function get_default_language_translations(): Collection
{
return collect([
config('language-translations.extended'),
config('language-translations.regular'),
config('custom-language-translations'),
])->collapse();
}
}
/**
if (!function_exists('is_dev')) {
/**
* Check if app is in dev mode
*
* @return bool
*/
function is_dev()
{
function is_dev()
{
return env('APP_ENV') === 'local';
}
}
/**
if (!function_exists('seems_utf8')) {
/**
* @param $str
* @return bool
*/
function seems_utf8($str)
{
function seems_utf8($str)
{
$length = strlen($str);
for ($i = 0; $i < $length; $i++) {
@@ -655,9 +727,11 @@ function seems_utf8($str)
}
return true;
}
}
/**
if (!function_exists('remove_accents')) {
/**
* Converts all accent characters to ASCII characters.
*
* If there are no accent characters, then the string given is just returned.
@@ -665,9 +739,9 @@ function seems_utf8($str)
* @param string $string Text that might have accent characters
* @return string Filtered string with replaced "nice" characters.
*/
function remove_accents($string)
{
if (! preg_match('/[\x80-\xff]/', $string)) {
function remove_accents($string)
{
if (!preg_match('/[\x80-\xff]/', $string)) {
return $string;
}
@@ -770,7 +844,7 @@ function remove_accents($string)
// Euro Sign
chr(226) . chr(130) . chr(172) => 'E',
// GBP (Pound) Sign
chr(194) . chr(163) => '', ];
chr(194) . chr(163) => '',];
$string = strtr($string, $chars);
} else {
@@ -795,9 +869,12 @@ function remove_accents($string)
}
return $string;
}
}
/**
if (!function_exists('get_files_for_zip')) {
/**
* Get all files from folder and get their folder location in VueFileManager directories
*
* @param $folders
@@ -805,10 +882,10 @@ function remove_accents($string)
* @param array $path
* @return array
*/
function get_files_for_zip($folders, $files, $path = [])
{
function get_files_for_zip($folders, $files, $path = [])
{
// Return file list
if (! isset($folders->folders)) {
if (!isset($folders->folders)) {
return $files->unique()->values()->all();
}
@@ -833,16 +910,18 @@ function get_files_for_zip($folders, $files, $path = [])
}
return get_files_for_zip($folders->folders->first(), $files, $path);
}
}
/**
if (!function_exists('set_time_by_user_timezone')) {
/**
* Set time by user timezone GMT
*
* @param $time
* @return Carbon
*/
function set_time_by_user_timezone($time)
{
function set_time_by_user_timezone($time)
{
$user = Auth::user();
if ($user) {
@@ -852,17 +931,19 @@ function set_time_by_user_timezone($time)
}
return Carbon::parse($time);
}
}
/**
if (!function_exists('__t')) {
/**
* Translate the given message.
* @param $key
* @param null $values
* @return string
* @throws Exception
*/
function __t($key, $values = null): string
{
function __t($key, $values = null): string
{
// Get current locale
$locale = cache()->rememberForever('language', function () {
try {
@@ -889,17 +970,19 @@ function __t($key, $values = null): string
}
return $string;
}
}
/**
if (!function_exists('replace_occurrence')) {
/**
* Replace string occurrence in __t() by their values
*
* @param $string
* @param $values
* @return string|string[]
*/
function replace_occurrence($string, $values)
{
function replace_occurrence($string, $values)
{
$occurrences = $values->map(function ($message, $key) {
return [
'key' => ":$key",
@@ -912,4 +995,5 @@ function replace_occurrence($string, $values)
$occurrences->pluck('message')->toArray(),
$string
);
}
}