2016-09-02 22:00:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
class MiscHelper
|
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
public static function capitaliseWord($word)
|
|
|
|
{
|
|
|
|
$word = strtolower($word);
|
|
|
|
|
|
|
|
$wordLetters = [];
|
|
|
|
preg_match_all('/\b[a-z]/i', $word, $wordLetters, PREG_OFFSET_CAPTURE);
|
|
|
|
|
|
|
|
foreach ($wordLetters[0] as $wordLetter)
|
|
|
|
{
|
|
|
|
$word = substr_replace($word, strtoupper($wordLetter[0]), $wordLetter[1], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $word;
|
|
|
|
}
|
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
public static function convertToBytes($val)
|
|
|
|
{
|
|
|
|
if(empty($val))return 0;
|
|
|
|
|
|
|
|
$val = trim($val);
|
|
|
|
|
|
|
|
preg_match('#([0-9]+)[\s]*([a-z]+)#i', $val, $matches);
|
|
|
|
|
|
|
|
$last = '';
|
|
|
|
if(isset($matches[2])){
|
|
|
|
$last = $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($matches[1])){
|
|
|
|
$val = (int) $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (strtolower($last))
|
|
|
|
{
|
|
|
|
case 'g':
|
|
|
|
case 'gb':
|
|
|
|
$val *= 1024;
|
|
|
|
case 'm':
|
|
|
|
case 'mb':
|
|
|
|
$val *= 1024;
|
|
|
|
case 'k':
|
|
|
|
case 'kb':
|
|
|
|
$val *= 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $val;
|
|
|
|
}
|
|
|
|
|
2017-09-17 09:20:35 +01:00
|
|
|
/**
|
|
|
|
* Convert a decimal (e.g. 3.5) to a fraction (e.g. 7/2).
|
|
|
|
* Adapted from: http://jonisalonen.com/2012/converting-decimal-numbers-to-ratios/
|
|
|
|
*
|
|
|
|
* @param float $decimal the decimal number.
|
|
|
|
*
|
|
|
|
* @return array|bool a 1/2 would be [1, 2] array (this can be imploded with '/' to form a string)
|
|
|
|
*/
|
|
|
|
public static function decimalToFraction($decimal)
|
|
|
|
{
|
|
|
|
if ($decimal < 0 || !is_numeric($decimal)) {
|
|
|
|
// Negative digits need to be passed in as positive numbers
|
|
|
|
// and prefixed as negative once the response is imploded.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($decimal == 0) {
|
|
|
|
return [0, 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tolerance = 1.e-4;
|
|
|
|
|
|
|
|
$numerator = 1;
|
|
|
|
$h2 = 0;
|
|
|
|
$denominator = 0;
|
|
|
|
$k2 = 1;
|
|
|
|
$b = 1 / $decimal;
|
|
|
|
do {
|
|
|
|
$b = 1 / $b;
|
|
|
|
$a = floor($b);
|
|
|
|
$aux = $numerator;
|
|
|
|
$numerator = $a * $numerator + $h2;
|
|
|
|
$h2 = $aux;
|
|
|
|
$aux = $denominator;
|
|
|
|
$denominator = $a * $denominator + $k2;
|
|
|
|
$k2 = $aux;
|
|
|
|
$b = $b - $a;
|
|
|
|
} while (abs($decimal - $numerator / $denominator) > $decimal * $tolerance);
|
|
|
|
|
|
|
|
return [
|
|
|
|
$numerator,
|
|
|
|
$denominator
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-10-06 15:07:30 +01:00
|
|
|
public static function getEnvironmentFilePath()
|
|
|
|
{
|
|
|
|
return sprintf('%s/.env', dirname(dirname(__DIR__)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEnvironmentSetting($settingName)
|
|
|
|
{
|
|
|
|
$envFile = MiscHelper::getEnvironmentFilePath();
|
2017-08-30 20:42:35 +01:00
|
|
|
|
|
|
|
if (!file_exists($envFile))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-06 15:07:30 +01:00
|
|
|
$matches = null;
|
|
|
|
if (preg_match(sprintf('/^\s*%s\s*=\s*(.+)$/im', preg_quote($settingName)), file_get_contents($envFile), $matches))
|
|
|
|
{
|
|
|
|
return trim($matches[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-30 20:42:35 +01:00
|
|
|
public static function isAppInstalled()
|
|
|
|
{
|
|
|
|
return MiscHelper::getEnvironmentSetting('APP_INSTALLED');
|
|
|
|
}
|
|
|
|
|
2017-09-29 20:15:24 +01:00
|
|
|
public static function isExecEnabled()
|
|
|
|
{
|
|
|
|
$disabled = explode(',', ini_get('disable_functions'));
|
|
|
|
return !in_array('exec', $disabled);
|
|
|
|
}
|
|
|
|
|
2016-10-05 11:49:39 +01:00
|
|
|
/**
|
|
|
|
* Tests whether the provided URL belongs to the current application (i.e. both scheme and hostname match.)
|
|
|
|
* @param $url
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isSafeUrl($url)
|
|
|
|
{
|
|
|
|
$parts = parse_url($url);
|
|
|
|
$validParts = parse_url(url('/'));
|
|
|
|
|
|
|
|
return ($parts['scheme'] == $validParts['scheme'] && $parts['host'] == $validParts['host']);
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
public static function randomString($length = 10)
|
|
|
|
{
|
|
|
|
$seed = 'abcdefghijklmnopqrstuvwxyz01234567890';
|
|
|
|
$string = '';
|
|
|
|
|
|
|
|
while (strlen($string) < $length)
|
|
|
|
{
|
|
|
|
$string .= substr($seed, rand(0, strlen($seed) - 1), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2016-10-06 15:07:30 +01:00
|
|
|
|
|
|
|
public static function setEnvironmentSetting($settingName, $value)
|
|
|
|
{
|
|
|
|
if (is_null(MiscHelper::getEnvironmentSetting($settingName)))
|
|
|
|
{
|
|
|
|
return file_put_contents(MiscHelper::getEnvironmentFilePath(), sprintf('%s=%s', $settingName, $value) . PHP_EOL, FILE_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|