File: D:/HostingSpaces/SBogers10/slenders.komma.pro/app/Helpers/KommaHelpers.php
<?php
namespace App\Helpers;
use Carbon\Carbon;
use DateTime;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
/**
* Short description for the file.
*
* @copyright (c) 2012-2015, Komma
*/
class KommaHelpers
{
static function komma_ip()
{
if ($_SERVER['REMOTE_ADDR'] == '5.172.219.238') return true;
return false;
}
/**
* Make the place-holder replacements on a line.
* This is a duplicate from the translator class
* Illuminate\Translation\Translator
*
*
* @param string $line
* @param array $replace
* @return string
*/
static function makeReplacements($line, array $replace)
{
$replace = KommaHelpers::sortReplacements($replace);
foreach ($replace as $key => $value) {
$line = str_replace(':' . $key, $value, $line);
}
return $line;
}
/**
* Sort the replacements array.
* This is a duplicate from the translator class
* Illuminate\Translation\Translator
*
* @param array $replace
* @return array
*/
static function sortReplacements(array $replace)
{
return (new Collection($replace))->sortBy(function ($value, $key) {
return mb_strlen($key) * -1;
});
}
/**
* @param $string
* @param int $length
* @return null|string|string[]
*/
static function str_limit_full_word($string, $length = 75){
$length = abs((int)$length);
if(strlen($string) > $length) {
$string = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $string);
}
return($string);
}
/**
* Returns the maximum upload size in bytes
*
* @return int
*/
public static function fileUploadMaxSize() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = self::parseSize(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = self::parseSize(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
/**
* Returns the max post size in bytes
*/
public static function maxPostSize()
{
$iniPostSize = strtolower(ini_get('post_max_size'));
if(strpos($iniPostSize, 'm') !== FALSE) {
$maxPostSizeInBytes = (int)(str_replace('M', '', $iniPostSize)) * 1024 * 1024;
} else if(strpos($iniPostSize, 'g') !== FALSE) {
$maxPostSizeInBytes = (int)(str_replace('g', '', $iniPostSize)) * 1024 * 1024 * 1024;
} else {
$maxPostSizeInBytes = (int) $iniPostSize;
}
return $maxPostSizeInBytes;
}
/**
* Returns a php ini file size as bytes. taking into account that it may have unit characters in the size.
*
* @param $size
* @return float
*/
public static function parseSize($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
/**
* Gets a class name (with or without namespace) and returns the class name only without the namespace
*
* @param object|string $classInstanceOrReference
* @param bool $snakecase If true then the shortModelClassName will be formatted to snake_case.
* @param bool $removeModelString Wether or not to remove the string 'model' if it is in the class name
* @return string
*/
public static function getShortNameFromClass($classInstanceOrReference, bool $snakecase = false, bool $removeModelString = false): string
{
if(!is_object($classInstanceOrReference) && !is_string($classInstanceOrReference)) throw new \InvalidArgumentException("Argument 1 passed to this method must be an class instance. But was a: ".gettype($classInstanceOrReference));
$FQCNSplit = (is_object($classInstanceOrReference)) ? explode('\\', get_class($classInstanceOrReference)) : explode('\\', $classInstanceOrReference);
if($FQCNSplit == $classInstanceOrReference) return $classInstanceOrReference;
$shortModelClassName = $FQCNSplit[count($FQCNSplit)-1];
if($snakecase) return snake_case($shortModelClassName);
return $shortModelClassName;
}
/**
* Logs how long the code ran since the start of the request
* @param string $note An extra comment you can add to the log.
*/
public static function howLongSinceStart($note = null)
{
$backtrace = debug_backtrace();
$backtrace = $backtrace[2];
$functionInClassString = $backtrace['function'].'()';
if (isset($backtrace['object'])) {
$functionInClassString .= ' '.get_class($backtrace['object']).':';
}
$timestamp = constant('LARAVEL_START');
$start = Carbon::createFromTimestamp($timestamp);
$now = Carbon::now();
$seconds = $now->diffInSeconds($start);
\Log::debug('It took '.$seconds.' second(s) since start to reach '.$functionInClassString.'. '.($note ? 'Note: '.$note : ''));
}
/**
*Parses the output of debug_backtrace to a string for the console
* @param array $debug_backtrace
* @return string
*/
public static function parseBacktraceToConsoleString(array $debug_backtrace, $stackDepth = 10):string
{
$resultString = PHP_EOL;
foreach($debug_backtrace as $itemNumber => $debug_backtrace_line) {
$maxFilePathLength = 32;
if(isset($debug_backtrace_line['file'])) {
$filePath = strrev(substr(strrev($debug_backtrace_line['file']), 0, $maxFilePathLength));
if (strlen($debug_backtrace_line['file']) > $maxFilePathLength) {
$filePath = '...' . $filePath;
}
} else {
$filePath = 'n/a';
}
$line = isset($debug_backtrace_line['line']) ? $debug_backtrace_line['line'] : 'n/a';
$shortClassName = isset($debug_backtrace_line['class']) ? self::getShortNameFromClass($debug_backtrace_line['class']) : '';
$type = isset($debug_backtrace_line['type']) ? $debug_backtrace_line['type'] : '';
$arguments = [];
foreach ($debug_backtrace_line['args'] as $argument) {
if(is_scalar($argument)) {
$arguments[] = (string) $argument;
} elseif(is_array($argument)) {
$arguments[] = '[.....]';
} else if (is_object($argument)) {
$arguments[] = get_class($argument);
}
}
$arguments = implode(', ', $arguments);
// MyClass -> Myfunction ( arg1, arg2, ...) on line 312 of file
$resultString .= PHP_EOL.str_pad($filePath.':'.$line, $maxFilePathLength + 7).' '.$shortClassName.$type.$debug_backtrace_line['function'].'('.$arguments.')';
if($itemNumber >= $stackDepth - 1) break;
}
return $resultString;
}
// ----------- NOT USED ATM, CAN BE DELETED?? ------------- //
// /**
// * This function will localize the date
// *
// * @param $date
// * @return DateTime
// */
// static function localize_date($date, $format = 'd-m-Y H:i', $strftime = false)
// {
//
// date_default_timezone_set('UTC');
// $datetime = new DateTime($date);
// $la_time = new \DateTimeZone('Europe/Amsterdam');
// $datetime->setTimezone($la_time);
//
//
// if ($strftime) {
//
// /**
// * When it is an windows server %e and % do not work, so convert these
// *
// */
// if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
//
// $format = preg_replace('/%e/', '%#d', $format);
// $format = preg_replace('/%G/', '%Y', $format);
// }
// return strftime($format, $datetime->getTimestamp());
// }
//
// return $datetime->format($format);
// }
//
// static function price($value, $parameters = array())
// {
// return number_format($value / 100, 2, ',', '.');
// }
//
// /**
// * Create an empty file on the fly based on the filePath
// * Check if it already exist, if it exist return true
// * Check if the folder exist, create when needed
// * Create a file and fill with the given data.
// *
// * @param $filePath
// * @param $data | String
// * @return bool
// */
// static function createFile($filePath, $data = '')
// {
// //Check if file exist, if true return
// if (is_file($filePath)) return true;
//
// //split the path in parts based on / (folder)
// $parts = explode('/', $filePath);
//
// //Pop the last item from the array, this is the file name
// $fileName = array_pop($parts);
//
// //Glue the path back together, without filename
// $path = implode('/', $parts);
//
//
// //If the dir doesn't exist, create this
// if (!is_dir($path)) mkdir($path, 0777, true);
//
// //Create an empty file on the location
// file_put_contents($path . '/' . $fileName, $data);
//
// return true;
// }
// /**
// * Remove a file from the disc
// *
// *
// * @param $filePath
// * @return bool
// */
// static function removeFile($filePath)
// {
// //Check if file exist, if false, return
// if (!is_file($filePath)) return true;
// //Remove the file
// unlink($filePath);
// }
//
// /**
// * Replace the / to a -
// * for the given string.
// *
// * @param $string
// * @return string
// */
// static function slashToDash($string)
// {
// return str_replace('/', '-', $string);
// }
//
// /**
// * Replace the - to a /
// * for the given string.
// *
// * @param $string
// * @return string
// */
// static function dashToSlash($string)
// {
// return str_replace('-', '/', $string);
// }
//
//
// /**
// * Replace the / to a _
// * for the given string.
// *
// * @param $string
// * @return string
// */
// static function slashToUnderScore($string)
// {
// return str_replace('/', '_', $string);
// }
// /**
// * Replace the _ to a /
// * for the given string.
// *
// * @param $string
// * @return string
// */
// static function UnderScoreToSlash($string)
// {
// return str_replace('_', '/', $string);
// }
// /**
// * Get data from the input.
// * based on the given key
// * and the language id
// *
// * @param $key
// * @param $language
// * @return mixed
// */
// static function getTranslationInput($key, $language, $suffix = '')
// {
// $key = $key;
// $key .= '_' . $language->id;
//
// if ($suffix != '') $key .= '_' . $suffix;
// return \Input::get($key);
// }
// /**
// * This method checks if an Kms attribute is required
// *
// * @param $attribute
// * @return bool
// */
// static function isRequired($attribute)
// {
// if (!isset($attribute->options)) return false;
// if (!isset($attribute->options['validation'])) return false;
// if (!isset($attribute->options['validation']['rules'])) return false;
// if (!preg_match('/required/', $attribute->options['validation']['rules'])) return false;
// return true;
// }
// /**
// * @param string $code
// * @param string $message
// * @return mixed
// */
// static function ajaxAbort($code = '400', $message = 'Oops something went wrong')
// {
// return \Response::json([
// 'message' => $message,
// ], $code);
// }
}