flash.moe/public/_v4/html.php

499 lines
16 KiB
PHP
Raw Normal View History

2020-08-20 00:02:37 +00:00
<?php
define('FWH_2016', 2016);
define('FWH_2019', 2019);
define('FWH_2020', 2020);
define('FWH_FMOE', 1);
define('FWH_JVDG', 2);
define('HTML_MODERN', 1);
define('HTML_NETSCAPE', 2);
define('HTML_OLDIE', 3);
define('HTML_PRESTO', 4);
define('HTML_INORI', 5);
define('HTML_GECKSCAPE', 5);
define('HTML_N3DS', 6);
function html_browser(): int {
static $browser = null;
if($browser)
return $browser;
$simulate = !empty($_GET['_sim']) && is_string($_GET['_sim']) ? (int)$_GET['_sim'] : null;
if(!empty($simulate))
return $browser = $simulate;
if(preg_match('#Nintendo 3DS#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_N3DS;
if(preg_match('#Presto#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_PRESTO;
if(preg_match('#Trident#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_INORI;
if(preg_match('#MSIE#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_OLDIE;
if(preg_match('#Netscape6#i', $_SERVER['HTTP_USER_AGENT'] ?? '') || preg_match('#Navigator#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_GECKSCAPE;
if(preg_match('#Mozilla/4#i', $_SERVER['HTTP_USER_AGENT'] ?? ''))
return $browser = HTML_NETSCAPE;
return $browser = HTML_MODERN;
}
function html_old_browser(): bool {
return in_array(html_browser(), [HTML_NETSCAPE, HTML_OLDIE], true);
}
function html_modern(): bool {
return html_browser() === HTML_MODERN;
}
function html_netscape(): bool {
return html_browser() === HTML_NETSCAPE;
}
function html_old_ie(): bool {
return html_browser() === HTML_OLDIE;
}
function html_presto(): bool {
return html_browser() === HTML_PRESTO || html_browser() === HTML_GECKSCAPE;
}
function html_inori(): bool {
return html_browser() === HTML_INORI;
}
function html_n3ds(): bool {
return html_browser() === HTML_N3DS;
}
function html_default_ver(): int {
return defined('FWH_STYLE') ? FWH_STYLE : 0;
}
function html_default_mode(): int {
return defined('FWH_MODE') ? FWH_MODE : FWH_FMOE;
}
function html_baseurl(): string {
if($_SERVER['HTTP_HOST'] !== 'flash.moe')
return '//flash.moe';
return '';
}
function html_local_url(string $url): string {
if($url[0] === '/' && substr($url, 0, 2) !== '//') {
$url = '/' . trim($url, '/');
$originalUrl = $url;
$cleanPath = '/' . trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
if(substr($cleanPath, 0, 3) === '/20'
&& (md5_file(__DIR__ . '/../index.php') !== '20c2c2b0b4a9fa31e10922b913a5ac07'
|| substr($cleanPath, 0, 14) !== '/2020/blog.php')) {
$url = substr($cleanPath, 0, 5) . $url;
if(in_array($originalUrl, ['/projects', '/404', '/blog', '/nintendo']))
$url .= '.php';
}
}
return $url;
}
function html_colour(?int $raw): string {
if (is_null($raw))
return 'inherit';
return '#' . str_pad(dechex($raw), 6, '0', STR_PAD_LEFT);
}
function html_doctype(?int $version = null): string {
return html_old_browser()
? "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"
: "<!doctype html>\r\n";
}
function html_charset(string $charset = 'utf-8', ?int $version = null): string {
return html_old_browser()
? "<meta http-equiv=\"content-type\" content=\"text/html; charset={$charset}\"/>\r\n"
: "<meta charset=\"{$charset}\"/>\r\n";
}
function html_stylesheet(string $name, array $params = [], ?int $version = null): string {
if(html_modern())
$cssFormat = '/css/%s';
else {
$cssFormat = '/css.php?path=%s';
if(empty($params['accent']))
$params['accent'] = html_default_mode() === FWH_JVDG ? '#2d9940' : '#4a3650';
if(!empty($params)) {
foreach($params as $key => $val)
$cssFormat .= '&' . rawurlencode($key) . '=' . str_replace('%', '%%', rawurlencode($val));
}
}
return "<link href=\"". html_baseurl() . sprintf($cssFormat, $name) ."\" type=\"text/css\" rel=\"stylesheet\"/>\r\n";
}
function html_script(string $path, string $charset = 'utf-8', string $language = 'javascript', ?int $version = null): string {
if($path[0] === '/' && $path[1] !== '/')
$path = html_baseurl() . $path;
$html = "<script src=\"{$path}\" type=\"text/{$language}\"";
if(html_old_browser())
$html .= "language=\"{$language}\" ";
return $html . " charset=\"{$charset}\"></script>\r\n";
}
function html_meta(?int $version = null): string {
$meta = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\"/>\r\n";
if(html_default_mode() === FWH_JVDG)
$meta .= " <style>:root { --accent-colour: #2d9940; }</style>\r\n";
return $meta;
}
function html_open(?int $version = null): string {
return html_doctype($version) . (($version ?? html_default_ver()) < FWH_2020 ? "<html>" : '');
}
function html_close(?int $version = null): string {
return ($version ?? html_default_ver()) < FWH_2020 ? "</html>" : '';
}
function html_head(string $title, array $vars = [], ?int $version = null): string {
$version = $version ?? html_default_ver();
$accentColour = $vars['accent_colour'] ?? '#555';
$html = html_charset();
$html .= "<title>{$title}</title>";
switch ($version) {
case FWH_2016:
return $html . html_stylesheet('2016.css', ['accent' => $accentColour], $version) . html_meta($version);
case FWH_2019:
$html .= html_stylesheet('2019.css', ['accent' => $accentColour], $version);
if (!empty($vars['transitional']))
$html .= html_stylesheet('2016-lite.css', ['accent' => $accentColour], $version);
$html .= html_meta($version);
if(html_modern()) {
$html .= <<<HTML
<style>
:root {
--accent-colour: {$accentColour};
}
</style>
HTML;
}
return $html;
}
return '';
}
function html_navigation(array $navigation, array $vars = [], ?int $version = null): string {
$home = html_local_url($navigation[0]['link'] ?? $vars['link'] ?? '/');
if(html_old_browser()) {
$title = !empty($vars['title1']) ? ($vars['title1'] . ($vars['title2'] ?? '')) : $vars['title'] ?? 'flashwave';
$html = '<table border="1" width="100%"><tr><td align="center" colspan="' . count($navigation) . '"><h1>' . $title . '</h1></td></tr><tr>';
foreach ($navigation as $item) {
$item['link'] = html_local_url($item['link']);
$html .= "<td align=\"center\"><a href=\"{$item['link']}\">{$item['title']}</a></td>";
}
return $html . '</tr></table>';
}
switch ($version ?? html_default_ver()) {
case FWH_2016:
$title = $vars['title'] ?? 'flash.moe';
$html = <<<HTML
<nav class="header">
<div class="header__inner">
<a href="{$home}" class="header__logo">
{$title}
</a>
<div class="header__menu">
HTML;
foreach ($navigation as $item) {
$item['link'] = html_local_url($item['link']);
$html .= sprintf('
<a class="nav-item" href="%s">
<span class="nav-item__icon mdi %s"></span>
<span class="nav-item__name">%s</span>
</a>
', $item['link'], $item['mdi-icon'] ?? 'mdi-link-variant', $item['title']);
}
$html .= <<<HTML
</div>
</div>
</nav>
HTML;
return $html;
case FWH_2019:
$title1 = $vars['title1'] ?? 'flash';
$title2 = $vars['title2'] ?? 'wave';
$html = <<<HTML
<nav class="header">
<div class="header__background"></div>
<div class="header__wrapper">
<a class="header__logo" href="{$home}">{$title1}<span class="header__logo__highlight">{$title2}</span></a>
<div class="header__items">
HTML;
foreach ($navigation as $item) {
$item['link'] = html_local_url($item['link']);
$html .= "<a href=\"{$item['link']}\" class=\"header__item\">{$item['title']}</a>";
}
$html .= '</div>';
$html .= <<<HTML
</div>
</nav>
HTML;
return $html;
}
return '';
}
function html_footer(?int $version = null): string {
$currentYear = date('Y');
if(html_old_browser()) {
return <<<HTML
<div align="center"><small>
&copy; <a href="http://flash.moe">Flashwave</a> 2010-{$currentYear}
</small></div>
HTML;
}
switch ($version ?? html_default_ver()) {
case FWH_2016:
return <<<HTML
<footer class="footer">
&copy; <a href="https://flash.moe" class="footer__link">Flashwave</a> 2010-{$currentYear}
</footer>
HTML;
case FWH_2019:
return <<<HTML
<footer class="footer">
<div class="footer__background"></div>
<div class="footer__wrapper">
&copy; <a href="https://flash.moe" class="footer__link">Flashwave</a> 2010-{$currentYear}
</div>
</footer>
HTML;
}
return '';
}
function html_sidebar(?int $version = null, ?array $links = null, ?array $accounts = null): string {
$version = $version ?? html_default_ver();
if($version < FWH_2020)
return '';
$isJvdg = html_default_mode() === FWH_JVDG;
$links = $links ?? [
[
'title' => 'Home',
'desc' => 'Index with latest blog posts',
'dest' => html_local_url('/'),
'hide' => $isJvdg,
],
[
'title' => 'Projects',
'desc' => 'List of things I\'ve made or am making',
'dest' => html_local_url('/projects'),
'hide' => $isJvdg,
],
[
'title' => 'Forum',
'desc' => 'Feature requests and bug reports for some projects',
'dest' => '//forum.flash.moe',
'hide' => $isJvdg,
],
[
'title' => 'Flashii',
'desc' => 'Community site I run and develop',
'dest' => '//flashii.net',
'hide' => $isJvdg,
],
[
'title' => 'Railgun',
'desc' => 'Chat server and chat protocols I work on',
'dest' => '//railgun.sh',
'hide' => $isJvdg,
],
];
$accounts = $accounts ?? [
[
'title' => 'E-mail',
'url' => 'mailto:me+contact@flash.moe',
'image' => html_baseurl() . '/assets/icons/email.png',
],
[
'title' => 'Flashii',
'url' => '//flashii.net/profile.php?u=1',
'image' => html_baseurl() . '/assets/icons/flashii.png',
'hide' => $isJvdg,
],
[
'title' => 'Github',
'url' => '//github.com/flashwave',
'image' => html_baseurl() . '/assets/icons/github.png',
],
[
'title' => 'YouTube',
'url' => '//youtube.com/c/flashwave',
'image' => html_baseurl() . '/assets/icons/youtube.png',
'hide' => $isJvdg,
],
[
'title' => 'Twitch',
'url' => '//twitch.tv/flashwave0',
'image' => html_baseurl() . '/assets/icons/twitch.png',
'hide' => $isJvdg,
],
[
'title' => 'Steam',
'url' => '//steamcommunity.com/id/flashwave_',
'image' => html_baseurl() . '/assets/icons/steam.png',
'hide' => $isJvdg,
],
[
'title' => 'Twitter',
'url' => 'javascript:confirm(\'Proceed with caution.\') ? location.assign(\'//twitter.com/smugwave\') : void(0);',
'image' => html_baseurl() . '/assets/icons/twitter.png',
'hide' => $isJvdg,
],
[
'title' => 'last.fm',
'url' => '//www.last.fm/user/flashwave_',
'image' => html_baseurl() . '/assets/icons/lastfm.png',
'hide' => $isJvdg,
],
[
'title' => 'Nintendo',
'url' => '/nintendo',
'image' => html_baseurl() . '/assets/icons/ninswitch.png',
'hide' => $isJvdg,
],
[
'title' => 'Donate',
'url' => '//paypal.me/flashwave',
'image' => html_baseurl() . '/assets/icons/paypal.png',
'hide' => $isJvdg,
],
];
$title1 = $isJvdg ? 'julian' : 'flash';
$title2 = $isJvdg ? 'vdg' : 'wave';
$avatar = $isJvdg ? '//julianvdg.nl/avatar.php' : '//flashii.net/user-assets.php?m=avatar&amp;u=1&amp;r=120';
$avatarHTML = $isJvdg ? '' : '<img src="'. $avatar .'" width="60" height="60" alt="" class="header-avatar"/>';
$html = <<<HTML
<div class="sidebar">
<div class="sidebar-inner">
<div class="header">
<div class="header-text">
<div class="header-title">{$title1}<span class="header-title-hilight">{$title2}</span></div>
<div class="header-slogan">if it ain't broke, i'll break it</div>
</div>
{$avatarHTML}
</div>\r\n
HTML;
$html .= " <div class=\"links\">\r\n";
foreach($links as $link) {
if(!empty($link['hide']))
continue;
$html .= <<<HTML
<a href="{$link['dest']}" class="link">
<div class="link-title">{$link['title']}</div>
<div class="link-desc">{$link['desc']}</div>
</a>\r\n
HTML;
}
$html .= " </div>\r\n";
$html .= " <div class=\"accounts\">\r\n";
foreach($accounts as $account) {
if(!empty($account['hide']))
continue;
$html .= <<<HTML
<a href="{$account['url']}" class="account" rel="noreferrer noopener">
<img src="{$account['image']}" alt="" width="25" height="25" class="account-icon"/>
<div class="account-name">{$account['title']}</div>
</a>\r\n
HTML;
}
$html .= " </div>\r\n";
$html .= <<<HTML
<div class="music hidden" id="music">
<div class="music-header" id="music-header">
Listening to
</div>
<div class="music-wrap">
<div class="music-meta">
<div class="music-title">
<a href="#" class="music-title-link" id="music-title">Title</a>
</div>
<div class="music-artist">
<a href="#" class="music-artist-link" id="music-artist">Artist</a>
</div>
</div>
<img src="//now.flash.moe/resources/no-cover.png" width="50" height="50" alt="" class="music-cover" id="music-cover"/>
</div>
</div>\r\n
HTML;
$name = $isJvdg ? 'Julian van de Groep' : 'Flashwave';
$year = date('Y');
if(!$isJvdg)
$html .= <<<HTML
</div>
<div class="copyright">
&copy; <a href="//{$_SERVER['HTTP_HOST']}">{$name}</a> 2010-{$year}
</div>
</div>\r\n
HTML;
else $html .= '</div></div>';
return $html;
}