2017-11-25 23:30:49 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file contains functions commonly used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copied from http://rogerstringer.com/2013/11/15/generate-uuids-php/
|
|
|
|
*/
|
|
|
|
function generate_uuid() {
|
|
|
|
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
|
|
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
|
|
|
|
mt_rand( 0, 0xffff ),
|
|
|
|
mt_rand( 0, 0x0fff ) | 0x4000,
|
|
|
|
mt_rand( 0, 0x3fff ) | 0x8000,
|
|
|
|
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-26 14:38:05 +01:00
|
|
|
function getDirectoryContent($path) {
|
|
|
|
if (dir_exists($path)) {
|
|
|
|
return array_diff(scandir($path), array('..', '.'));
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-11-26 11:44:14 +01:00
|
|
|
function dir_exists($path) {
|
|
|
|
return file_exists($path) && is_dir($path);
|
|
|
|
}
|
|
|
|
|
2017-11-25 23:30:49 +01:00
|
|
|
function format_size($size, $precision = 2) {
|
|
|
|
$sizes = ['bytes', 'Kb', 'Mb', 'Gb', 'Tb'];
|
|
|
|
$i = 0;
|
|
|
|
while (1023 < $size && $i < count($sizes) - 1) {
|
|
|
|
$size /= 1023;
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return number_format($size, $precision).' '.$sizes[$i];
|
|
|
|
}
|
|
|
|
|
|
|
|
function startsWith($haystack, $needle) {
|
|
|
|
$length = strlen($needle);
|
|
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function endsWith($haystack, $needle) {
|
|
|
|
$length = strlen($needle);
|
|
|
|
|
|
|
|
return $length === 0 || (substr($haystack, -$length) === $needle);
|
|
|
|
}
|
2017-12-10 20:20:21 +01:00
|
|
|
|
|
|
|
function generatePath($parts, $basePath = __DIR__) {
|
|
|
|
$path = $basePath;
|
|
|
|
if (!is_array($parts)) {
|
|
|
|
$parts = [$parts];
|
|
|
|
}
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$path .= DIRECTORY_SEPARATOR.generatePathName($part);
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generatePathName($name) {
|
|
|
|
return urlencode($name);
|
|
|
|
}
|
2017-11-25 23:30:49 +01:00
|
|
|
?>
|