2021-03-27 19:37:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function jslog($obj)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
if (!($config['log_level'] == 'DEBUG' || $config['log_level'] === Logger::DEBUG)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
echo '<script>console.log('.json_encode($obj).');</script>';
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_page_header(
|
|
|
|
?string $page_title = null,
|
|
|
|
bool $include_app_name = true,
|
|
|
|
array $scripts = []
|
|
|
|
): string {
|
|
|
|
global $api;
|
|
|
|
$greeting = '';
|
|
|
|
$logout_link = '';
|
2021-04-04 07:44:33 +00:00
|
|
|
$newpoll_class = '';
|
2021-03-27 19:37:17 +00:00
|
|
|
if ($api->isAuthenticated(false, true)) {
|
|
|
|
$u = $api->getAuthorizedUser();
|
|
|
|
$greeting = 'Welcome, ' . ($u->name ?? '@'.$u->username);
|
|
|
|
$logout_link = '<a href="logout.php" class="logout">Logout</a>';
|
|
|
|
} else {
|
2021-04-04 07:44:33 +00:00
|
|
|
$newpoll_class = 'disabled';
|
2021-03-27 19:37:17 +00:00
|
|
|
$greeting = '<a href="' . $api->getAuthURL() . '">Login with pnut</a>';
|
|
|
|
}
|
|
|
|
$title = '';
|
|
|
|
if ($include_app_name) {
|
|
|
|
$title = $api->app_name;
|
|
|
|
}
|
|
|
|
if (!empty($page_title)) {
|
|
|
|
$title .= $include_app_name ? ' > ' : '';
|
|
|
|
$title .= $page_title;
|
|
|
|
}
|
|
|
|
$script_str = '';
|
|
|
|
foreach ($scripts as $script) {
|
2021-03-27 20:15:07 +00:00
|
|
|
$script_str .= '<script src="scripts/' . $script . '.js"></script>';
|
2021-03-27 19:37:17 +00:00
|
|
|
}
|
2021-04-04 07:44:33 +00:00
|
|
|
|
2021-04-17 15:05:13 +00:00
|
|
|
$home_link = get_icon_link(
|
|
|
|
'index.php',
|
|
|
|
'Home',
|
|
|
|
'home',
|
|
|
|
'homelink'
|
|
|
|
);
|
|
|
|
$new_poll_link = get_icon_link(
|
|
|
|
'new_poll.php',
|
|
|
|
'New Poll',
|
|
|
|
'plus',
|
|
|
|
'newpolllink'
|
|
|
|
);
|
|
|
|
|
2021-03-27 19:37:17 +00:00
|
|
|
return '<html><head><meta charset="utf-8"><title>'.$title.'</title><link rel="stylesheet" href="styles/style.css">'
|
|
|
|
. $script_str
|
|
|
|
. '</head><body><header>'
|
2021-04-17 15:05:13 +00:00
|
|
|
. $home_link
|
|
|
|
. $new_poll_link
|
2021-03-27 19:37:17 +00:00
|
|
|
. $greeting
|
|
|
|
. '<div class="spacer"></div>'
|
|
|
|
. $logout_link
|
2021-04-04 10:04:00 +00:00
|
|
|
. '</header><main>';
|
|
|
|
}
|
|
|
|
|
2021-04-17 15:05:13 +00:00
|
|
|
function get_icon_link(string $href, string $label, string $icon, string $class)
|
|
|
|
{
|
|
|
|
return '<a href="'.$href.'" title="'.$label.'" class="'.$class.'">'
|
|
|
|
. '<div class="linkcontents">'
|
|
|
|
. file_get_contents(__DIR__.'/icons/'.$icon.'.svg')
|
|
|
|
. '<span class="linklabel">'.$label.'</span></div></a>';
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:04:00 +00:00
|
|
|
function get_page_footer()
|
|
|
|
{
|
|
|
|
$version = json_decode(file_get_contents(__DIR__ . '/composer.json'), true)['version'];
|
2021-04-17 15:05:13 +00:00
|
|
|
$repo_link = get_icon_link(
|
|
|
|
'https://phlaym.net/git/phlaym/Dragonpolls',
|
|
|
|
'Source Code',
|
|
|
|
'src',
|
|
|
|
'sourcecode'
|
|
|
|
);
|
|
|
|
$issues_link = get_icon_link(
|
|
|
|
'https://github.com/hutattedonmyarm/Dragonpolls/issues',
|
|
|
|
'Issues',
|
|
|
|
'issues',
|
|
|
|
'issues'
|
|
|
|
);
|
2021-04-04 10:04:00 +00:00
|
|
|
return '</main><footer>'
|
2021-04-05 10:54:10 +00:00
|
|
|
. '<a href="https://phlaym.net/git/phlaym/Dragonpolls/releases/tag/'.$version.'">Version ' . $version . '</a>'
|
2021-04-17 15:05:13 +00:00
|
|
|
. $repo_link
|
|
|
|
. $issues_link
|
2021-04-04 10:04:00 +00:00
|
|
|
. '</footer></body></html>';
|
2021-03-27 19:37:17 +00:00
|
|
|
}
|
2021-03-28 19:19:04 +00:00
|
|
|
|
|
|
|
function redirect($to)
|
|
|
|
{
|
|
|
|
header('Location: '.$to);
|
|
|
|
die('<html><meta http-equiv="refresh" content="0;url='.$to.'">'
|
|
|
|
.'<script>window.location.replace("'.$to.'");</script></html>');
|
|
|
|
}
|
2021-04-04 07:44:33 +00:00
|
|
|
|
|
|
|
function get_source_set($user, int $base_size, int $max_scale = 3): string
|
|
|
|
{
|
|
|
|
$srcset_entries = [$user->getAvatarUrl($base_size)];
|
|
|
|
for ($s = 2; $s <= $max_scale; $s++) {
|
|
|
|
$srcset_entries[] = $user->getAvatarUrl($base_size * $s) . ' ' . $s . 'x';
|
|
|
|
}
|
|
|
|
return implode(', ', $srcset_entries);
|
|
|
|
}
|
2021-04-17 11:39:45 +00:00
|
|
|
|
2021-04-17 14:04:34 +00:00
|
|
|
function make_banner(string $type, string $content, string $custom_symbol = null): string
|
2021-04-17 11:39:45 +00:00
|
|
|
{
|
2021-04-17 14:04:34 +00:00
|
|
|
if (empty($custom_symbol)) {
|
|
|
|
$custom_symbol = $type === 'success' ? '✓' : '𐄂';
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:39:45 +00:00
|
|
|
return '<div class="banner-wrapper">'
|
|
|
|
. ' <div class="'
|
|
|
|
. $type
|
2021-04-17 14:04:34 +00:00
|
|
|
. ' banner"><span>'
|
|
|
|
. $custom_symbol
|
|
|
|
. '</span>'
|
2021-04-17 11:39:45 +00:00
|
|
|
. $content
|
|
|
|
. '</div></div>';
|
|
|
|
}
|
2021-04-17 14:04:34 +00:00
|
|
|
|
|
|
|
function quit(string $error, string $error_details = '')
|
|
|
|
{
|
|
|
|
die(make_banner('error', $error) . $error_details . get_page_footer());
|
|
|
|
}
|