Merge remote-tracking branch 'origin/master' into oasis

# Conflicts:
#	app/Http/helpers.php
This commit is contained in:
Peter Papp
2021-07-09 12:06:58 +02:00
+187 -112
View File
@@ -16,64 +16,73 @@ use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image; use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
/** if (!function_exists('obfuscate_email')) {
/**
* Obfuscate email * Obfuscate email
* *
* @param $email * @param $email
* @return string * @return string
*/ */
function obfuscate_email($email) function obfuscate_email($email)
{ {
$em = explode('@', $email); $em = explode('@', $email);
$name = implode('@', array_slice($em, 0, count($em) - 1)); $name = implode('@', array_slice($em, 0, count($em) - 1));
$len = floor(strlen($name) / 2); $len = floor(strlen($name) / 2);
return substr($name, 0, $len) . str_repeat('*', $len) . '@' . end($em); return substr($name, 0, $len) . str_repeat('*', $len) . '@' . end($em);
}
} }
/** if (!function_exists('get_setting')) {
/**
* Get single value from settings table * Get single value from settings table
* *
* @param $setting * @param $setting
* @return |null * @return |null
*/ */
function get_setting($setting) function get_setting($setting)
{ {
return Setting::find($setting)->value ?? null; return Setting::find($setting)->value ?? null;
}
} }
/** if (!function_exists('get_settings_in_json')) {
/**
* Get all app settings and return them as json * Get all app settings and return them as json
*/ */
function get_settings_in_json() function get_settings_in_json()
{ {
return json_decode( return json_decode(
Setting::all() Setting::all()
->pluck('value', 'name') ->pluck('value', 'name')
->toJson() ->toJson()
); );
}
} }
/** if (!function_exists('get_setup_status')) {
/**
* Check if setup wizard was passed * Check if setup wizard was passed
* *
* @return string * @return string
*/ */
function get_setup_status() function get_setup_status()
{ {
$setup_success = get_setting('setup_wizard_success'); $setup_success = get_setting('setup_wizard_success');
return boolval($setup_success) ? 'setup-done' : 'setup-disclaimer'; return boolval($setup_success) ? 'setup-done' : 'setup-disclaimer';
}
} }
/** if (!function_exists('add_paragraphs')) {
/**
* Create paragraph from text * Create paragraph from text
* *
* @param $str * @param $str
* @return mixed|null|string|string[] * @return mixed|null|string|string[]
*/ */
function add_paragraphs($str) function add_paragraphs($str)
{ {
// Trim whitespace // Trim whitespace
if (($str = trim($str)) === '') { if (($str = trim($str)) === '') {
return ''; return '';
@@ -111,17 +120,19 @@ function add_paragraphs($str)
$str = preg_replace('~(?<!\n)\n(?!\n)~', "<br>\n", $str); $str = preg_replace('~(?<!\n)\n(?!\n)~', "<br>\n", $str);
return $str; return $str;
}
} }
/** if (!function_exists('setEnvironmentValue')) {
/**
* Set environment value * Set environment value
* *
* @param $key * @param $key
* @param $value * @param $value
* @return bool * @return bool
*/ */
function setEnvironmentValue(array $values) function setEnvironmentValue(array $values)
{ {
$envFile = app()->environmentFilePath(); $envFile = app()->environmentFilePath();
$str = file_get_contents($envFile); $str = file_get_contents($envFile);
@@ -140,6 +151,7 @@ function setEnvironmentValue(array $values)
$str = substr($str, 0, -1); $str = substr($str, 0, -1);
return !(!file_put_contents($envFile, $str)); return !(!file_put_contents($envFile, $str));
}
} }
if (!function_exists('get_invoice_number')) { if (!function_exists('get_invoice_number')) {
@@ -150,7 +162,7 @@ if (!function_exists('get_invoice_number')) {
*/ */
function get_invoice_number() function get_invoice_number()
{ {
$invoices = Invoice::all(); $invoices = \App\Invoice::all();
if ($invoices->isEmpty()) { if ($invoices->isEmpty()) {
return now()->year . '001'; return now()->year . '001';
@@ -160,129 +172,150 @@ if (!function_exists('get_invoice_number')) {
} }
} }
/** if (!function_exists('cache_forget_many')) {
/**
* Forget many cache keys at once * Forget many cache keys at once
* @param $cache * @param $cache
*/ */
function cache_forget_many($cache) function cache_forget_many($cache)
{ {
foreach ($cache as $item) { foreach ($cache as $item) {
\Illuminate\Support\Facades\Cache::forget($item); \Illuminate\Support\Facades\Cache::forget($item);
} }
}
} }
/** if (!function_exists('get_storage')) {
/**
* Get app version from config * Get app version from config
* *
* @return \Illuminate\Config\Repository|mixed * @return \Illuminate\Config\Repository|mixed
*/ */
function get_storage() function get_storage()
{ {
return env('FILESYSTEM_DRIVER'); return env('FILESYSTEM_DRIVER');
}
} }
/** if (!function_exists('is_storage_driver')) {
/**
* Check if is running AWS s3 as storage * Check if is running AWS s3 as storage
* *
* @return bool * @return bool
*/ */
function is_storage_driver($driver) function is_storage_driver($driver)
{ {
if (is_array($driver)) { if (is_array($driver)) {
return in_array(config('filesystems.default'), $driver); return in_array(config('filesystems.default'), $driver);
} }
return config('filesystems.default') === $driver; return config('filesystems.default') === $driver;
}
} }
/** if (!function_exists('get_version')) {
/**
* Get app version from config * Get app version from config
* *
* @return \Illuminate\Config\Repository|mixed * @return \Illuminate\Config\Repository|mixed
*/ */
function get_version() function get_version()
{ {
return config('vuefilemanager.version'); return config('vuefilemanager.version');
}
} }
/** if (!function_exists('is_demo')) {
/**
* Check if is demo * Check if is demo
* *
* @return bool * @return bool
*/ */
function is_demo() function is_demo()
{ {
return config('vuefilemanager.is_demo'); return config('vuefilemanager.is_demo');
}
} }
/** if (!function_exists('is_demo_account')) {
/**
* Check if is demo * Check if is demo
* *
* @param $email * @param $email
* @return mixed * @return mixed
*/ */
function is_demo_account($email) function is_demo_account($email)
{ {
return config('vuefilemanager.is_demo') && $email === 'howdy@hi5ve.digital'; return config('vuefilemanager.is_demo') && $email === 'howdy@hi5ve.digital';
}
} }
/** if (!function_exists('get_item')) {
/**
* Get folder or file item * Get folder or file item
* *
* @param $type * @param $type
* @param $id * @param $id
* @return \Illuminate\Database\Eloquent\Builder|Model * @return \Illuminate\Database\Eloquent\Builder|Model
*/ */
function get_item($type, $id) function get_item($type, $id)
{ {
$model = strtolower($type) === 'folder' ? 'Folder' : 'File'; $model = strtolower($type) === 'folder' ? 'Folder' : 'File';
return ("App\\Models\\$model")::find($id); return ("App\\Models\\$model")::find($id);
}
} }
/** if (!function_exists('get_shared')) {
/**
* Get shared token * Get shared token
* *
* @param $token * @param $token
* @return \Illuminate\Database\Eloquent\Builder|Model * @return \Illuminate\Database\Eloquent\Builder|Model
*/ */
function get_shared($token) function get_shared($token)
{ {
return Share::whereToken($token) return Share::whereToken($token)
->firstOrFail(); ->firstOrFail();
}
} }
/** if (!function_exists('is_editor')) {
/**
* Check if shared permission is editor * Check if shared permission is editor
* *
* @param $shared * @param $shared
* @return bool * @return bool
*/ */
function is_editor($shared) function is_editor($shared)
{ {
return $shared->permission === 'editor'; return $shared->permission === 'editor';
}
} }
/** if (!function_exists('is_visitor')) {
/**
* Check if shared permission is visitor * Check if shared permission is visitor
* *
* @param $shared * @param $shared
* @return bool * @return bool
*/ */
function is_visitor($shared) function is_visitor($shared)
{ {
return $shared->permission === 'visitor'; return $shared->permission === 'visitor';
}
} }
/** if (!function_exists('store_avatar')) {
/**
* Store user avatar to storage * Store user avatar to storage
* *
* @param $request * @param $request
* @param $name * @param $name
* @return string|null * @return string|null
*/ */
function store_avatar($request, $name) function store_avatar($request, $name)
{ {
if (!$request->hasFile($name)) { if (!$request->hasFile($name)) {
return null; return null;
} }
@@ -309,17 +342,19 @@ function store_avatar($request, $name)
// Return path to image // Return path to image
return "avatars/$image_path"; return "avatars/$image_path";
}
} }
/** if (!function_exists('store_system_image')) {
/**
* Store system image * Store system image
* *
* @param $request * @param $request
* @param $name * @param $name
* @return string|null * @return string|null
*/ */
function store_system_image($request, $name) function store_system_image($request, $name)
{ {
if (!$request->hasFile($name)) { if (!$request->hasFile($name)) {
return null; return null;
} }
@@ -334,16 +369,18 @@ function store_system_image($request, $name)
// Return path to image // Return path to image
return "system/$filename"; return "system/$filename";
}
} }
/** if (!function_exists('make_single_input')) {
/**
* Make input from request * Make input from request
* *
* @param $request * @param $request
* @return array * @return array
*/ */
function make_single_input($request) function make_single_input($request)
{ {
// Create container // Create container
$data = []; $data = [];
@@ -352,31 +389,35 @@ function make_single_input($request)
// Return input // Return input
return $data; return $data;
}
} }
/** if (!function_exists('format_gigabytes')) {
/**
* Format integer to gigabytes * Format integer to gigabytes
* *
* @param $gigabytes * @param $gigabytes
* @return string * @return string
*/ */
function format_gigabytes($gigabytes) function format_gigabytes($gigabytes)
{ {
if ($gigabytes >= 1000) { if ($gigabytes >= 1000) {
return Metric::gigabytes($gigabytes)->format('Tb/'); return Metric::gigabytes($gigabytes)->format('Tb/');
} }
return Metric::gigabytes($gigabytes)->format('GB/'); return Metric::gigabytes($gigabytes)->format('GB/');
}
} }
/** if (!function_exists('format_megabytes')) {
/**
* Format string to formated megabytes string * Format string to formated megabytes string
* *
* @param $megabytes * @param $megabytes
* @return string * @return string
*/ */
function format_megabytes($megabytes) function format_megabytes($megabytes)
{ {
if ($megabytes >= 1000) { if ($megabytes >= 1000) {
return $megabytes / 1000 . 'GB'; return $megabytes / 1000 . 'GB';
} }
@@ -386,28 +427,32 @@ function format_megabytes($megabytes)
} }
return $megabytes . 'MB'; return $megabytes . 'MB';
}
} }
/** if (!function_exists('format_bytes')) {
/**
* Convert megabytes to bytes * Convert megabytes to bytes
* *
* @param $megabytes * @param $megabytes
* @return int|string * @return int|string
*/ */
function format_bytes($megabytes) function format_bytes($megabytes)
{ {
return Metric::megabytes($megabytes)->numberOfBytes(); return Metric::megabytes($megabytes)->numberOfBytes();
}
} }
/** if (!function_exists('get_storage_fill_percentage')) {
/**
* Get storage usage in percent * Get storage usage in percent
* *
* @param $used * @param $used
* @param $capacity * @param $capacity
* @return string * @return string
*/ */
function get_storage_fill_percentage($used, $capacity) function get_storage_fill_percentage($used, $capacity)
{ {
// Format gigabytes to bytes // Format gigabytes to bytes
$total = intval(Metric::gigabytes($capacity)->numberOfBytes()); $total = intval(Metric::gigabytes($capacity)->numberOfBytes());
@@ -420,17 +465,19 @@ function get_storage_fill_percentage($used, $capacity)
// Return in 2 decimal // 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 * Get user capacity fill by percentage
* *
* @param $id * @param $id
* @param null $additionals * @param null $additionals
* @return string * @return string
*/ */
function user_storage_percentage($id, $additionals = null) function user_storage_percentage($id, $additionals = null)
{ {
$user = User::findOrFail($id); $user = User::findOrFail($id);
$used = $user->used_capacity; $used = $user->used_capacity;
@@ -440,17 +487,19 @@ function user_storage_percentage($id, $additionals = null)
} }
return get_storage_fill_percentage($used, $user->settings->storage_capacity); return get_storage_fill_percentage($used, $user->settings->storage_capacity);
}
} }
/** if (!function_exists('recursiveFind')) {
/**
* Find all key values in recursive array * Find all key values in recursive array
* *
* @param array $array * @param array $array
* @param $needle * @param $needle
* @return array * @return array
*/ */
function recursiveFind(array $array, $needle) function recursiveFind(array $array, $needle)
{ {
$iterator = new RecursiveArrayIterator($array); $iterator = new RecursiveArrayIterator($array);
$recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST); $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$aHitList = []; $aHitList = [];
@@ -462,15 +511,17 @@ function recursiveFind(array $array, $needle)
} }
return $aHitList; return $aHitList;
}
} }
/** if (!function_exists('appeared_once')) {
/**
* Get values which appears only once in array * Get values which appears only once in array
* @param $arr * @param $arr
* @return array * @return array
*/ */
function appeared_once($arr) function appeared_once($arr)
{ {
$array_count_values = array_count_values($arr); $array_count_values = array_count_values($arr);
$single_time_comming_values_array = []; $single_time_comming_values_array = [];
@@ -482,42 +533,48 @@ function appeared_once($arr)
} }
return $single_time_comming_values_array; return $single_time_comming_values_array;
}
} }
/** if (!function_exists('filter_folders_ids')) {
/**
* @param $folders * @param $folders
* @param string $by_column * @param string $by_column
* @return array * @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); $folder_ids = recursiveFind($folders->toArray(), $by_column);
return appeared_once($folder_ids); return appeared_once($folder_ids);
}
} }
/** if (!function_exists('format_date')) {
/**
* Format localized date * Format localized date
* *
* @param $date * @param $date
* @param string $format * @param string $format
* @return string * @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); $start = Carbon::parse($date);
return $start->formatLocalized($format); return $start->formatLocalized($format);
}
} }
/** if (!function_exists('get_file_type')) {
/**
* Get file type from mimetype * Get file type from mimetype
* *
* @param $file_mimetype * @param $file_mimetype
* @return string * @return string
*/ */
function get_file_type($file_mimetype) function get_file_type($file_mimetype)
{ {
// Get mimetype from file // Get mimetype from file
$mimetype = explode('/', $file_mimetype); $mimetype = explode('/', $file_mimetype);
@@ -526,6 +583,7 @@ function get_file_type($file_mimetype)
} }
return 'file'; return 'file';
}
} }
if (!function_exists('map_language_translations')) { if (!function_exists('map_language_translations')) {
@@ -543,18 +601,21 @@ if (!function_exists('map_language_translations')) {
} }
} }
/** if (!function_exists('get_file_type_from_mimetype')) {
/**
* Get file type from mimetype * Get file type from mimetype
* *
* @param $mimetype * @param $mimetype
* @return mixed * @return mixed
*/ */
function get_file_type_from_mimetype($mimetype) function get_file_type_from_mimetype($mimetype)
{ {
return explode('/', $mimetype)[1]; return explode('/', $mimetype)[1];
}
} }
/** if (!function_exists('get_pretty_name')) {
/**
* Format pretty name file * Format pretty name file
* *
* @param $basename * @param $basename
@@ -562,8 +623,8 @@ function get_file_type_from_mimetype($mimetype)
* @param $mimetype * @param $mimetype
* @return string * @return string
*/ */
function get_pretty_name($basename, $name, $mimetype) function get_pretty_name($basename, $name, $mimetype)
{ {
$file_extension = substr(strrchr($basename, '.'), 1); $file_extension = substr(strrchr($basename, '.'), 1);
if (strpos($name, $file_extension) !== false) { if (strpos($name, $file_extension) !== false) {
@@ -575,16 +636,18 @@ function get_pretty_name($basename, $name, $mimetype)
} }
return $name . '.' . $mimetype; return $name . '.' . $mimetype;
}
} }
/** if (!function_exists('get_image_meta_data')) {
/**
* Get exif data from jpeg image * Get exif data from jpeg image
* *
* @param $file * @param $file
* @return array|null * @return array|null
*/ */
function get_image_meta_data($file) function get_image_meta_data($file)
{ {
if (get_file_type_from_mimetype($file->getMimeType()) === 'jpeg') { if (get_file_type_from_mimetype($file->getMimeType()) === 'jpeg') {
try { try {
// Try to get the exif data // Try to get the exif data
@@ -593,6 +656,7 @@ function get_image_meta_data($file)
return null; return null;
} }
} }
}
} }
if (!function_exists('get_default_language_translations')) { if (!function_exists('get_default_language_translations')) {
@@ -609,22 +673,25 @@ if (!function_exists('get_default_language_translations')) {
} }
} }
/** if (!function_exists('is_dev')) {
/**
* Check if app is in dev mode * Check if app is in dev mode
* *
* @return bool * @return bool
*/ */
function is_dev() function is_dev()
{ {
return env('APP_ENV') === 'local'; return env('APP_ENV') === 'local';
}
} }
/** if (!function_exists('seems_utf8')) {
/**
* @param $str * @param $str
* @return bool * @return bool
*/ */
function seems_utf8($str) function seems_utf8($str)
{ {
$length = strlen($str); $length = strlen($str);
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
@@ -660,9 +727,11 @@ function seems_utf8($str)
} }
return true; return true;
}
} }
/** if (!function_exists('remove_accents')) {
/**
* Converts all accent characters to ASCII characters. * Converts all accent characters to ASCII characters.
* *
* If there are no accent characters, then the string given is just returned. * If there are no accent characters, then the string given is just returned.
@@ -670,8 +739,8 @@ function seems_utf8($str)
* @param string $string Text that might have accent characters * @param string $string Text that might have accent characters
* @return string Filtered string with replaced "nice" characters. * @return string Filtered string with replaced "nice" characters.
*/ */
function remove_accents($string) function remove_accents($string)
{ {
if (!preg_match('/[\x80-\xff]/', $string)) { if (!preg_match('/[\x80-\xff]/', $string)) {
return $string; return $string;
} }
@@ -800,9 +869,12 @@ function remove_accents($string)
} }
return $string; return $string;
}
} }
/** if (!function_exists('get_files_for_zip')) {
/**
* Get all files from folder and get their folder location in VueFileManager directories * Get all files from folder and get their folder location in VueFileManager directories
* *
* @param $folders * @param $folders
@@ -810,8 +882,8 @@ function remove_accents($string)
* @param array $path * @param array $path
* @return array * @return array
*/ */
function get_files_for_zip($folders, $files, $path = []) function get_files_for_zip($folders, $files, $path = [])
{ {
// Return file list // Return file list
if (!isset($folders->folders)) { if (!isset($folders->folders)) {
return $files->unique()->values()->all(); return $files->unique()->values()->all();
@@ -838,16 +910,18 @@ function get_files_for_zip($folders, $files, $path = [])
} }
return get_files_for_zip($folders->folders->first(), $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 * Set time by user timezone GMT
* *
* @param $time * @param $time
* @return Carbon * @return Carbon
*/ */
function set_time_by_user_timezone($time) function set_time_by_user_timezone($time)
{ {
$user = Auth::user(); $user = Auth::user();
if ($user) { if ($user) {
@@ -857,6 +931,7 @@ function set_time_by_user_timezone($time)
} }
return Carbon::parse($time); return Carbon::parse($time);
}
} }
if (!function_exists('__t')) { if (!function_exists('__t')) {