2016-09-02 22:00:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
class MiscHelper
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
public static function gravatarUrl($emailAddress, $size = 48, $default = 'identicon')
|
|
|
|
{
|
|
|
|
$hash = md5(strtolower(trim($emailAddress)));
|
|
|
|
return sprintf('https://www.gravatar.com/avatar/%s?s=%d&d=%s', $hash, $size, $default);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|