2020-07-30 00:55:37 +00:00
|
|
|
<?php
|
2022-02-04 20:30:52 +00:00
|
|
|
namespace Makai;
|
|
|
|
|
2023-05-28 18:22:55 +00:00
|
|
|
use Index\XString;
|
2022-09-26 23:06:12 +00:00
|
|
|
use Index\Http\HttpFx;
|
2022-02-04 20:30:52 +00:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../makai.php';
|
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
define('MKI_REDIRS', [
|
|
|
|
'/about' => '/',
|
|
|
|
'/about.html' => '/',
|
|
|
|
'/about.php' => '/',
|
|
|
|
'/index.php' => '/',
|
|
|
|
'/index.html' => '/',
|
|
|
|
'/related.php' => '/',
|
|
|
|
'/related.html' => '/',
|
|
|
|
'/friends' => '/',
|
|
|
|
'/friends.php' => '/',
|
|
|
|
'/friends.html' => '/',
|
|
|
|
'/related' => '/',
|
|
|
|
'/etc.php' => '/',
|
|
|
|
'/etc.html' => '/',
|
|
|
|
'/etcetera' => '/',
|
|
|
|
'/etcetera.html' => '/',
|
|
|
|
'/etcetera.php' => '/',
|
|
|
|
'/misc' => '/',
|
|
|
|
'/misc.html' => '/',
|
|
|
|
'/misc.php' => '/',
|
|
|
|
'/etc' => '/',
|
|
|
|
'/365' => '/',
|
|
|
|
'/donate' => '/',
|
|
|
|
'/blog.php' => '/',
|
|
|
|
'/blog-post.php' => '/',
|
|
|
|
'/blog/:id' => '/',
|
|
|
|
'/old-blog' => '/',
|
|
|
|
'/old-blog/:id' => '/',
|
|
|
|
'/projects.php' => '/projects',
|
|
|
|
'/projects.html' => '/projects',
|
|
|
|
'/utilities' => '/projects',
|
|
|
|
'/utilities.php' => '/projects',
|
|
|
|
'/utilities.html' => '/projects',
|
|
|
|
'/contact.php' => '/contact',
|
|
|
|
'/contact.html' => '/contact',
|
|
|
|
'/nintendo' => '/contact',
|
|
|
|
'/nintendo.php' => '/contact',
|
2022-09-26 23:34:02 +00:00
|
|
|
'/ascii.php' => '/ascii',
|
2023-05-28 18:22:55 +00:00
|
|
|
'/key.php' => '/rngstr',
|
2020-07-30 00:55:37 +00:00
|
|
|
]);
|
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router = new HttpFx;
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($ctx) {
|
|
|
|
$path = 'errors/' . $code;
|
|
|
|
$tpl = $ctx->getTemplating();
|
2023-10-12 18:45:11 +00:00
|
|
|
$name = is_file(sprintf('%s/errors/%s.twig', MKI_DIR_TEMPLATES, $code)) ? sprintf('errors/%s.twig', $code) : 'errors/master';
|
|
|
|
|
|
|
|
$response->setContent($tpl->render($name, [
|
2022-09-26 23:06:12 +00:00
|
|
|
'http_error_code' => $code,
|
|
|
|
'http_error_title' => $text,
|
|
|
|
]));
|
|
|
|
});
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2023-10-12 18:45:11 +00:00
|
|
|
$router->get('/error/:code', function($response, $request, string $code) {
|
|
|
|
return (int)$code;
|
2022-09-26 23:06:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
foreach(MKI_REDIRS as $source => $target)
|
|
|
|
$router->get($source, function($response) use ($target) {
|
|
|
|
$response->redirect($target);
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->use('/', function($response) {
|
2023-10-12 18:45:11 +00:00
|
|
|
$response->setPoweredBy('Makai');
|
2022-09-26 23:06:12 +00:00
|
|
|
});
|
|
|
|
|
2023-05-28 18:34:43 +00:00
|
|
|
$router->get('/header-bgs.json', function() use ($ctx) {
|
|
|
|
return $ctx->getDefaultHeaders();
|
2022-09-26 23:06:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/now-listening', function() use ($ctx) {
|
|
|
|
return $ctx->getTemplating()->render('np', [
|
|
|
|
'header_offset' => (int)filter_input(INPUT_GET, 'offset', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/now-listening.json', function() {
|
|
|
|
$lfmInfo = json_decode(file_get_contents('https://now.flash.moe/get.php?u=flashwave_'));
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
if(empty($lfmInfo[0]->name))
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$lfmInfo = $lfmInfo[0];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'name' => strval($lfmInfo->name),
|
|
|
|
'now_playing' => !empty($lfmInfo->nowplaying),
|
|
|
|
'url' => strval($lfmInfo->url),
|
|
|
|
'cover' => !empty($lfmInfo->images->large) ? strval($lfmInfo->images->large) : '',
|
|
|
|
'artist' => [
|
|
|
|
'name' => !empty($lfmInfo->artist->name) ? strval($lfmInfo->artist->name) : '',
|
|
|
|
'url' => explode('/_/', strval($lfmInfo->url))[0],
|
|
|
|
],
|
2020-07-30 00:55:37 +00:00
|
|
|
];
|
2022-09-26 23:06:12 +00:00
|
|
|
});
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->get('/home', function() use ($ctx) {
|
|
|
|
return $ctx->getTemplating()->render('home/index');
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/', function() use ($ctx) {
|
|
|
|
$legacyPage = (string)filter_input(INPUT_GET, 'p');
|
|
|
|
if(!empty($legacyPage)) {
|
|
|
|
$legacyPages = [
|
|
|
|
'projects' => '/projects',
|
|
|
|
'contact' => '/contact',
|
|
|
|
'about' => '/',
|
|
|
|
'etc' => '/etc',
|
|
|
|
'hosted' => '/etc',
|
|
|
|
'friends' => '/related',
|
|
|
|
];
|
|
|
|
|
|
|
|
if(isset($legacyPages[$legacyPage])) {
|
|
|
|
header('Location: ' . $legacyPages[$legacyPage]);
|
|
|
|
return 302;
|
|
|
|
}
|
2020-07-30 00:55:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 18:45:11 +00:00
|
|
|
$dbConn = $ctx->getDatabase();
|
|
|
|
|
2023-10-12 20:00:08 +00:00
|
|
|
$projects = new Projects($dbConn);
|
|
|
|
$projectInfos = $projects->getProjects(
|
|
|
|
featuredOnly: true,
|
|
|
|
deleted: false,
|
|
|
|
take: 3,
|
|
|
|
random: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
$projectItems = [];
|
2023-10-12 18:45:11 +00:00
|
|
|
foreach($projectInfos as $projectInfo)
|
2023-10-12 20:00:08 +00:00
|
|
|
$projectItems[] = [
|
2023-10-12 18:45:11 +00:00
|
|
|
'info' => $projectInfo,
|
2023-10-12 20:00:08 +00:00
|
|
|
'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $projects->getProjectColour($projectInfo),
|
2023-10-12 18:45:11 +00:00
|
|
|
];
|
|
|
|
|
2023-10-12 20:00:08 +00:00
|
|
|
$contacts = (new Contacts($dbConn))->getContacts(
|
|
|
|
homePageOnly: true,
|
|
|
|
take: 3,
|
|
|
|
);
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
return $ctx->getTemplating()->render('index', [
|
2023-10-12 20:00:08 +00:00
|
|
|
'projects' => $projectItems,
|
2023-10-12 18:45:11 +00:00
|
|
|
'contacts' => $contacts,
|
2022-09-26 23:06:12 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->get('/contact', function() use ($ctx) {
|
|
|
|
return $ctx->getTemplating()->render('contact', [
|
2023-10-12 20:00:08 +00:00
|
|
|
'contacts' => (new Contacts($ctx->getDatabase()))->getContacts(),
|
2022-09-26 23:06:12 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-08-31 15:43:18 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->get('/projects', function() use ($ctx) {
|
2023-10-12 18:45:11 +00:00
|
|
|
$dbConn = $ctx->getDatabase();
|
2023-10-12 20:00:08 +00:00
|
|
|
$projects = new Projects($dbConn);
|
|
|
|
$projectInfos = $projects->getProjects(deleted: false);
|
2023-10-12 18:45:11 +00:00
|
|
|
|
2023-10-12 20:00:08 +00:00
|
|
|
$items = [];
|
2023-10-12 18:45:11 +00:00
|
|
|
foreach($projectInfos as $projectInfo)
|
2023-10-12 20:00:08 +00:00
|
|
|
$items[] = [
|
2023-10-12 18:45:11 +00:00
|
|
|
'info' => $projectInfo,
|
2023-10-12 20:00:08 +00:00
|
|
|
'langs' => $projects->getLanguages(projectInfo: $projectInfo),
|
|
|
|
'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $projects->getProjectColour($projectInfo),
|
2023-10-12 18:45:11 +00:00
|
|
|
];
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
$sections = [
|
|
|
|
'projects' => [
|
2023-05-28 18:46:56 +00:00
|
|
|
'title' => 'Projects',
|
|
|
|
'desc' => '',
|
2023-10-12 20:00:08 +00:00
|
|
|
'items' => $items,
|
2022-09-26 23:06:12 +00:00
|
|
|
],
|
|
|
|
];
|
2020-07-30 00:55:37 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
return $ctx->getTemplating()->render('projects', [
|
|
|
|
'sections' => $sections,
|
|
|
|
]);
|
|
|
|
});
|
2020-08-19 14:33:55 +00:00
|
|
|
|
2022-09-26 23:34:02 +00:00
|
|
|
$router->get('/ascii', function() use ($ctx) {
|
2023-10-12 18:45:11 +00:00
|
|
|
$templating = $ctx->getTemplating();
|
|
|
|
$templating->addFilter('chr', 'chr');
|
|
|
|
$templating->addFilter('decoct', 'decoct');
|
|
|
|
$templating->addFilter('dechex', 'dechex');
|
|
|
|
|
|
|
|
return $templating->render('ascii');
|
2022-09-26 23:34:02 +00:00
|
|
|
});
|
|
|
|
|
2023-05-28 18:22:55 +00:00
|
|
|
$router->get('/rngstr', function($response, $request) {
|
|
|
|
$response->setTypePlain();
|
|
|
|
|
|
|
|
$length = (int)$request->getParam('length', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
if($length > 0 && $length <= 1024)
|
|
|
|
return XString::random($length);
|
|
|
|
|
|
|
|
$out = '8: ' . XString::random(8) . PHP_EOL;
|
|
|
|
$out .= '16: ' . XString::random(16) . PHP_EOL;
|
|
|
|
$out .= '32: ' . XString::random(32) . PHP_EOL;
|
|
|
|
$out .= '64: ' . XString::random(64) . PHP_EOL;
|
|
|
|
$out .= '128: ' . XString::random(128) . PHP_EOL;
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
});
|
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->get('/ssh.php', function() {
|
|
|
|
$query = '';
|
2022-02-05 03:35:42 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$minLevel = (int)filter_input(INPUT_GET, 'l', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
if($minLevel > 0)
|
|
|
|
$query .= sprintf('l=%d&', $minLevel);
|
2022-02-05 03:35:42 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
if(!empty($_GET['c']))
|
|
|
|
$query .= 'c=1&';
|
2022-02-05 03:35:42 +00:00
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
if(!empty($_GET['j']))
|
|
|
|
$query .= 'j=1&';
|
|
|
|
|
|
|
|
if($query !== '')
|
|
|
|
$query = '?' . substr($query, 0, -1);
|
|
|
|
|
|
|
|
header('Location: /ssh_keys' . $query);
|
|
|
|
return 302;
|
2022-02-05 03:35:42 +00:00
|
|
|
});
|
|
|
|
|
2022-09-26 23:06:12 +00:00
|
|
|
$router->get('/ssh_keys', function() use ($db) {
|
|
|
|
$minLevel = (int)filter_input(INPUT_GET, 'l', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$includeComment = !empty($_GET['c']);
|
|
|
|
$json = !empty($_GET['j']);
|
|
|
|
|
2023-10-12 20:00:08 +00:00
|
|
|
$keys = (new SSHKeys($db))->getKeys(minLevel: $minLevel, deprecated: false);
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
if($json) {
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach($keys as $key) {
|
|
|
|
$items[] = $item = new \stdClass;
|
|
|
|
$item->algo = $key->getAlgorithm();
|
|
|
|
$item->key = $key->getBody();
|
|
|
|
if($includeComment) {
|
|
|
|
$item->comment = (string)$key->getComment();
|
|
|
|
$item->created = $key->getCreatedAt()->format(\DateTime::ATOM);
|
|
|
|
$item->level = $key->getLevel();
|
|
|
|
}
|
2022-02-05 03:35:42 +00:00
|
|
|
}
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
return $items;
|
2022-02-05 03:35:42 +00:00
|
|
|
}
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
header('Content-Type: text/plain; charset=us-ascii');
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
|
|
|
|
foreach($keys as $key)
|
|
|
|
$body .= $key->toString($includeComment) . "\n";
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/authorized_keys', function() use ($db) {
|
2023-10-12 20:00:08 +00:00
|
|
|
$keys = (new SSHKeys($db))->getKeys(minLevel: 500, deprecated: false);
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
header('Content-Type: text/plain; charset=us-ascii');
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
foreach($keys as $key)
|
|
|
|
$body .= $key->toString(true) . "\n";
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/git_keys_ro', function() use ($db) {
|
2023-10-12 20:00:08 +00:00
|
|
|
$keys = (new SSHKeys($db))->getKeys(minLevel: 100, deprecated: false);
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
header('Content-Type: text/plain; charset=us-ascii');
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
foreach($keys as $key)
|
|
|
|
$body .= $key->toString(false) . "\n";
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/git_keys_rw', function() use ($db) {
|
2023-10-12 20:00:08 +00:00
|
|
|
$keys = (new SSHKeys($db))->getKeys(minLevel: 200, deprecated: false);
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
header('Content-Type: text/plain; charset=us-ascii');
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
foreach($keys as $key)
|
|
|
|
$body .= $key->toString(false) . "\n";
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
});
|
|
|
|
|
2022-09-27 01:58:32 +00:00
|
|
|
$router->get('/whois', function() use ($ctx) {
|
|
|
|
return $ctx->getTemplating()->render('whois');
|
|
|
|
});
|
|
|
|
|
|
|
|
$router->get('/whois/lookup', function($response, $request) use ($ctx) {
|
|
|
|
$target = trim((string)$request->getParam('target'));
|
|
|
|
if(empty($target))
|
|
|
|
return [
|
|
|
|
'error' => true,
|
|
|
|
'text' => 'Missing input data.',
|
|
|
|
];
|
|
|
|
|
|
|
|
$hash = hash('sha256', $target);
|
|
|
|
$prefix = 'fm:whois2:target:';
|
|
|
|
$source = '';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$redis = new \Redis;
|
|
|
|
$redis->connect('/var/run/redis/redis-server.sock');
|
|
|
|
|
|
|
|
$result = $redis->get($prefix . $hash);
|
|
|
|
|
|
|
|
if(empty($result)) {
|
|
|
|
$client = new \Makai\Whois\WhoisClient;
|
|
|
|
$result = $client->lookup($target);
|
|
|
|
$redis->setEx($prefix . $hash, 1800, serialize($result));
|
|
|
|
$source = 'fresh';
|
|
|
|
} else {
|
|
|
|
$result = unserialize($result);
|
|
|
|
$source = 'cache';
|
|
|
|
}
|
|
|
|
} catch (\RuntimeException $ex) {
|
|
|
|
return [
|
|
|
|
'error' => true,
|
|
|
|
'text' => $ex->getMessage(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'error' => false,
|
|
|
|
'source' => $source,
|
|
|
|
'result' => $result,
|
|
|
|
];
|
|
|
|
});
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
$router->dispatch();
|