karadav/www/_inc.php

80 lines
1.7 KiB
PHP
Raw Normal View History

2022-08-30 05:01:39 +00:00
<?php
namespace KaraDAV;
use KD2\ErrorManager;
2022-08-31 06:06:27 +00:00
spl_autoload_register(function ($class) {
$class = str_replace('\\', '/', $class);
require_once __DIR__ . '/../lib/' . $class . '.php';
});
2022-08-30 05:01:39 +00:00
ErrorManager::enable(ErrorManager::DEVELOPMENT);
2022-08-31 06:06:27 +00:00
ErrorManager::setLogFile(__DIR__ . '/../error.log');
2022-08-30 05:01:39 +00:00
if (!file_exists(__DIR__ . '/../config.local.php')) {
die('This server is not configured yet. Please copy config.dist.php to config.local.php and edit it.');
}
require __DIR__ . '/../config.local.php';
2022-08-31 06:06:27 +00:00
// Init database
if (!file_exists(DB_FILE)) {
$db = DB::getInstance();
$db->exec('BEGIN;');
$db->exec(file_get_contents(__DIR__ . '/../schema.sql'));
@session_start();
$users = new Users;
$p = Users::generatePassword();
$users->create('demo', $p);
$users->edit('demo', ['quota' => 10]);
$_SESSION['install_password'] = $p;
$users->login('demo', $p);
$db->exec('END;');
}
2022-08-31 07:57:49 +00:00
function html_head(string $title): void
2022-08-31 06:06:27 +00:00
{
$title = htmlspecialchars($title);
echo <<<EOF
2022-08-31 07:57:49 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{$title}</title>
<link rel="stylesheet" type="text/css" href="/admin.css" />
</head>
<body>
<h1>{$title}</h1>
2022-08-31 06:06:27 +00:00
EOF;
2022-08-31 07:57:49 +00:00
}
2022-08-31 06:06:27 +00:00
2022-08-31 07:57:49 +00:00
function html_foot(): void
{
echo '
<footer>
2022-09-30 13:17:47 +00:00
Powered by <a href="https://github.com/kd2org/karadav/">KaraDAV</a>
2022-08-31 07:57:49 +00:00
</footer>
</body>
</html>';
2022-08-31 06:06:27 +00:00
}
2022-10-10 13:20:41 +00:00
function format_bytes(int $bytes, string $unit = 'B'): string
{
if ($bytes >= 1024*1024*1024) {
return round($bytes / (1024*1024*1024), 1) . ' G' . $unit;
}
elseif ($bytes >= 1024*1024) {
return round($bytes / (1024*1024), 1) . ' M' . $unit;
}
elseif ($bytes >= 1024) {
return round($bytes / 1024, 1) . ' K' . $unit;
}
else {
return $bytes . ' ' . $unit;
}
}