hinamizawa/public/php/functions.php

67 lines
1.9 KiB
PHP

<?php
/* process functions */
function getByteString($bytes) {
$symbol = array('B', 'KB', 'MB', 'GB', 'TB');
$exp = ($bytes ? floor(log($bytes)/log(1024)) : 0);
return sprintf(
'%.2f '.$symbol[$exp],
($bytes ? $bytes/pow(1024, floor($exp)) : 0)
);
}
function getPercentageUsed($total, $used) {
return ($total && $used ?
sprintf('%.4f', (100 * $used / $total))
: false);
}
function getDifference($minuend, $subtrahend) {
return ($minuend && $subtrahend ?
$minuend - $subtrahend
: false);
}
function getDriveValues($drives) {
foreach ($drives as $drive) {
$drive->spaceFree = disk_free_space($drive->location);
$drive->spaceTotal = disk_total_space($drive->location);
$drive->spaceUsed = getDifference(
$drive->spaceFree, $drive->spaceTotal);
$drive->spacePercentageUsed = getPercentageUsed(
$drive->spaceTotal, $drive->spaceUsed);
}
return $drives;
}
/* display functions */
function printDrive($drive) {
echo '
<div class="drive">
<h3>' . $drive->name . '</h3>
<h4>' . $drive->description . '</h4>
<svg class="chart">
<circle class="pie" />
<circle
id="' . $drive->id . '"
class="piece"
stroke-dasharray="' . ($drive->spacePercentageUsed ?
$drive->spacePercentageUsed : 0) * 31.4 / 100 . ' 31.4"
/>
</svg>
<h5>Free Space: <em>' . getByteString($drive->spaceFree) . '</em></h5>
<h5>Used Space: <em>' . getByteString($drive->spaceUsed) . '</em></h5>
</div>';
}
function printService($service) {
echo '
<a href="' . $service->location . '">
<div class="service" id="' . $service->id . '">
<h3>' . $service->name . '</h3>
<img
class="logo"
src="img/' . $service->id . '.svg"
alt="' . $service->name . ' logo"
/>
<h4>' . $service->description . '</h4>
</div>
</a>';
}
?>