2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
|
|
|
use Misuzu\Template;
|
2023-07-15 02:05:49 +00:00
|
|
|
use Misuzu\Changelog\Changelog;
|
2023-07-15 23:58:17 +00:00
|
|
|
use Misuzu\Comments\Comments;
|
2023-01-01 20:33:32 +00:00
|
|
|
use Misuzu\Config\IConfig;
|
2023-07-12 21:52:55 +00:00
|
|
|
use Misuzu\Emoticons\Emotes;
|
2023-07-15 17:02:46 +00:00
|
|
|
use Misuzu\News\News;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Misuzu\SharpChat\SharpChatRoutes;
|
|
|
|
use Misuzu\Users\Users;
|
2023-01-01 19:06:01 +00:00
|
|
|
use Index\Data\IDbConnection;
|
2023-01-07 04:15:19 +00:00
|
|
|
use Index\Data\Migration\IDbMigrationRepo;
|
|
|
|
use Index\Data\Migration\DbMigrationManager;
|
|
|
|
use Index\Data\Migration\FsDbMigrationRepo;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Index\Http\HttpFx;
|
|
|
|
use Index\Http\HttpRequest;
|
|
|
|
use Index\Routing\Router;
|
|
|
|
|
|
|
|
// this class should function as the root for everything going forward
|
|
|
|
// no more magical static classes that are just kind of assumed to exist
|
|
|
|
// it currently looks Pretty Messy, but most everything else will be holding instances of other classes
|
2023-01-06 20:35:03 +00:00
|
|
|
class MisuzuContext {
|
2023-01-01 19:06:01 +00:00
|
|
|
private IDbConnection $dbConn;
|
2023-01-01 20:23:53 +00:00
|
|
|
private IConfig $config;
|
2023-01-01 19:06:01 +00:00
|
|
|
private Users $users;
|
2022-09-13 13:14:49 +00:00
|
|
|
private HttpFx $router;
|
2023-07-12 21:52:55 +00:00
|
|
|
private Emotes $emotes;
|
2023-07-15 02:05:49 +00:00
|
|
|
private Changelog $changelog;
|
2023-07-15 17:02:46 +00:00
|
|
|
private News $news;
|
2023-07-15 23:58:17 +00:00
|
|
|
private Comments $comments;
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-01-01 20:23:53 +00:00
|
|
|
public function __construct(IDbConnection $dbConn, IConfig $config) {
|
2023-01-01 19:06:01 +00:00
|
|
|
$this->dbConn = $dbConn;
|
2023-01-01 20:23:53 +00:00
|
|
|
$this->config = $config;
|
2023-01-01 19:06:01 +00:00
|
|
|
$this->users = new Users($this->dbConn);
|
2023-07-12 21:52:55 +00:00
|
|
|
$this->emotes = new Emotes($this->dbConn);
|
2023-07-15 02:05:49 +00:00
|
|
|
$this->changelog = new Changelog($this->dbConn);
|
2023-07-15 17:02:46 +00:00
|
|
|
$this->news = new News($this->dbConn);
|
2023-07-15 23:58:17 +00:00
|
|
|
$this->comments = new Comments($this->dbConn);
|
2023-01-01 19:06:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:52:30 +00:00
|
|
|
public function getDbConn(): IDbConnection {
|
|
|
|
return $this->dbConn;
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:06:01 +00:00
|
|
|
public function getDbQueryCount(): int {
|
|
|
|
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
|
2023-07-15 17:15:40 +00:00
|
|
|
return $result->next() ? $result->getInteger(1) : 0;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 04:15:19 +00:00
|
|
|
public function createMigrationManager(): DbMigrationManager {
|
|
|
|
return new DbMigrationManager($this->dbConn, 'msz_' . DbMigrationManager::DEFAULT_TABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createMigrationRepo(): IDbMigrationRepo {
|
|
|
|
return new FsDbMigrationRepo(MSZ_MIGRATIONS);
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:23:53 +00:00
|
|
|
public function getConfig(): IConfig {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
public function getRouter(): Router {
|
|
|
|
return $this->router->getRouter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*public function getUsers(): Users {
|
|
|
|
return $this->users;
|
|
|
|
}*/
|
|
|
|
|
2023-07-12 21:52:55 +00:00
|
|
|
public function getEmotes(): Emotes {
|
|
|
|
return $this->emotes;
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
public function getChangelog(): Changelog {
|
|
|
|
return $this->changelog;
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
public function getNews(): News {
|
|
|
|
return $this->news;
|
|
|
|
}
|
|
|
|
|
2023-07-15 23:58:17 +00:00
|
|
|
public function getComments(): Comments {
|
|
|
|
return $this->comments;
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
public function setUpHttp(bool $legacy = false): void {
|
|
|
|
$this->router = new HttpFx;
|
|
|
|
$this->router->use('/', function($response) {
|
|
|
|
$response->setPoweredBy('Misuzu');
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->registerErrorPages();
|
|
|
|
|
|
|
|
if($legacy)
|
|
|
|
$this->registerLegacyRedirects();
|
|
|
|
else
|
|
|
|
$this->registerHttpRoutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dispatchHttp(?HttpRequest $request = null): void {
|
|
|
|
$this->router->dispatch($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function registerErrorPages(): void {
|
|
|
|
$this->router->addErrorHandler(400, function($response) {
|
|
|
|
$response->setContent(Template::renderRaw('errors.400'));
|
|
|
|
});
|
|
|
|
$this->router->addErrorHandler(403, function($response) {
|
|
|
|
$response->setContent(Template::renderRaw('errors.403'));
|
|
|
|
});
|
|
|
|
$this->router->addErrorHandler(404, function($response) {
|
|
|
|
$response->setContent(Template::renderRaw('errors.404'));
|
|
|
|
});
|
|
|
|
$this->router->addErrorHandler(500, function($response) {
|
|
|
|
$response->setContent(file_get_contents(MSZ_TEMPLATES . '/500.html'));
|
|
|
|
});
|
|
|
|
$this->router->addErrorHandler(503, function($response) {
|
|
|
|
$response->setContent(file_get_contents(MSZ_TEMPLATES . '/503.html'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function registerHttpRoutes(): void {
|
2023-07-15 02:05:49 +00:00
|
|
|
$mszCompatHandler = fn($className, $method) => fn(...$args) => (new ("\\Misuzu\\Http\\Handlers\\{$className}Handler")($this))->{$method}(...$args);
|
|
|
|
|
|
|
|
$this->router->get('/', $mszCompatHandler('Home', 'index'));
|
|
|
|
|
|
|
|
$this->router->get('/assets/avatar/:filename', $mszCompatHandler('Assets', 'serveAvatar'));
|
|
|
|
$this->router->get('/assets/profile-background/:filename', $mszCompatHandler('Assets', 'serveProfileBackground'));
|
|
|
|
|
|
|
|
$this->router->get('/info', $mszCompatHandler('Info', 'index'));
|
|
|
|
$this->router->get('/info/:name', $mszCompatHandler('Info', 'page'));
|
|
|
|
$this->router->get('/info/:project/:name', $mszCompatHandler('Info', 'page'));
|
|
|
|
|
|
|
|
$this->router->get('/changelog', $mszCompatHandler('Changelog', 'index'));
|
|
|
|
$this->router->get('/changelog.rss', $mszCompatHandler('Changelog', 'feedRss'));
|
|
|
|
$this->router->get('/changelog.atom', $mszCompatHandler('Changelog', 'feedAtom'));
|
|
|
|
$this->router->get('/changelog/change/:id', $mszCompatHandler('Changelog', 'change'));
|
|
|
|
|
|
|
|
$this->router->get('/news', $mszCompatHandler('News', 'index'));
|
|
|
|
$this->router->get('/news.rss', $mszCompatHandler('News', 'feedIndexRss'));
|
|
|
|
$this->router->get('/news.atom', $mszCompatHandler('News', 'feedIndexAtom'));
|
|
|
|
$this->router->get('/news/:category', $mszCompatHandler('News', 'viewCategory'));
|
|
|
|
$this->router->get('/news/post/:id', $mszCompatHandler('News', 'viewPost'));
|
|
|
|
|
|
|
|
$this->router->get('/forum/mark-as-read', $mszCompatHandler('Forum', 'markAsReadGET'));
|
|
|
|
$this->router->post('/forum/mark-as-read', $mszCompatHandler('Forum', 'markAsReadPOST'));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-12 21:52:55 +00:00
|
|
|
new SharpChatRoutes($this->router, $this->config->scopeTo('sockChat'), $this->emotes);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function registerLegacyRedirects(): void {
|
|
|
|
$this->router->get('/index.php', function($response) {
|
|
|
|
$response->redirect(url('index'), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/info.php', function($response) {
|
|
|
|
$response->redirect(url('info'), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/settings.php', function($response) {
|
|
|
|
$response->redirect(url('settings-index'), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/changelog.php', function($response, $request) {
|
|
|
|
$changeId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
if($changeId) {
|
|
|
|
$response->redirect(url('changelog-change', ['change' => $changeId]), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->redirect(url('changelog-index', [
|
|
|
|
'date' => $request->getParam('d'),
|
|
|
|
'user' => $request->getParam('u', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
]), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$infoRedirect = function($response, $request, string ...$parts) {
|
|
|
|
$response->redirect(url('info', ['title' => implode('/', $parts)]), true);
|
|
|
|
};
|
|
|
|
$this->router->get('/info.php/:name', $infoRedirect);
|
|
|
|
$this->router->get('/info.php/:project/:name', $infoRedirect);
|
|
|
|
|
|
|
|
$this->router->get('/auth.php', function($response, $request) {
|
|
|
|
$response->redirect(url([
|
|
|
|
'logout' => 'auth-logout',
|
|
|
|
'reset' => 'auth-reset',
|
|
|
|
'forgot' => 'auth-forgot',
|
|
|
|
'register' => 'auth-register',
|
|
|
|
][$request->getParam('m')] ?? 'auth-login'), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news.php', function($response, $request) {
|
|
|
|
$postId = $request->getParam('n', FILTER_SANITIZE_NUMBER_INT) ?? $request->getParam('p', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
|
|
|
|
if($postId > 0)
|
|
|
|
$location = url('news-post', ['post' => $postId]);
|
|
|
|
else {
|
|
|
|
$catId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$pageId = $request->getParam('page', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$location = url($catId > 0 ? 'news-category' : 'news-index', ['category' => $catId, 'page' => $pageId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->redirect($location, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news.php/rss', function($response, $request) {
|
|
|
|
$catId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$location = url($catId > 0 ? 'news-category-feed-rss' : 'news-feed-rss', ['category' => $catId]);
|
|
|
|
$response->redirect($location, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news.php/atom', function($response, $request) {
|
|
|
|
$catId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$location = url($catId > 0 ? 'news-category-feed-atom' : 'news-feed-atom', ['category' => $catId]);
|
|
|
|
$response->redirect($location, true);
|
|
|
|
});
|
|
|
|
|
2023-07-15 17:45:39 +00:00
|
|
|
$this->router->get('/news/index.php', function($response, $request) {
|
2022-09-13 13:14:49 +00:00
|
|
|
$response->redirect(url('news-index', [
|
|
|
|
'page' => $request->getParam('page', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
]), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news/category.php', function($response, $request) {
|
|
|
|
$response->redirect(url('news-category', [
|
|
|
|
'category' => $request->getParam('c', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
'page' => $request->getParam('p', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
]), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news/post.php', function($response, $request) {
|
|
|
|
$response->redirect(url('news-post', [
|
|
|
|
'post' => $request->getParam('p', FILTER_SANITIZE_NUMBER_INT),
|
|
|
|
]), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news/feed.php', function() {
|
|
|
|
return 400;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news/feed.php/rss', function($response, $request) {
|
2023-07-15 17:45:39 +00:00
|
|
|
$catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
2022-09-13 13:14:49 +00:00
|
|
|
$response->redirect(url(
|
|
|
|
$catId > 0 ? 'news-category-feed-rss' : 'news-feed-rss',
|
2023-07-15 17:45:39 +00:00
|
|
|
['category' => $catId]
|
2022-09-13 13:14:49 +00:00
|
|
|
), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/news/feed.php/atom', function($response, $request) {
|
2023-07-15 17:45:39 +00:00
|
|
|
$catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
|
2022-09-13 13:14:49 +00:00
|
|
|
$response->redirect(url(
|
|
|
|
$catId > 0 ? 'news-category-feed-atom' : 'news-feed-atom',
|
2023-07-15 17:45:39 +00:00
|
|
|
['category' => $catId]
|
2022-09-13 13:14:49 +00:00
|
|
|
), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->get('/user-assets.php', function($response, $request) {
|
2023-07-15 17:45:39 +00:00
|
|
|
return (new \Misuzu\Http\Handlers\AssetsHandler($this))->serveLegacy($response, $request);
|
2022-09-13 13:14:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|