misuzu/src/MisuzuContext.php

456 lines
22 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-09-08 00:54:19 +00:00
use Index\Environment;
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigrationRepo;
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
use Index\Http\HttpFx;
use Index\Http\HttpRequest;
2023-09-08 01:05:17 +00:00
use Index\Routing\IRouter;
2023-08-31 21:33:34 +00:00
use Sasae\SasaeEnvironment;
2022-09-13 13:14:49 +00:00
use Misuzu\Template;
2023-09-08 00:43:00 +00:00
use Misuzu\Auth\AuthContext;
use Misuzu\Auth\AuthInfo;
use Misuzu\AuditLog\AuditLog;
2023-07-15 02:05:49 +00:00
use Misuzu\Changelog\Changelog;
use Misuzu\Changelog\ChangelogRoutes;
2023-07-15 23:58:17 +00:00
use Misuzu\Comments\Comments;
use Misuzu\Config\IConfig;
use Misuzu\Counters\Counters;
use Misuzu\Emoticons\Emotes;
use Misuzu\Forum\ForumContext;
use Misuzu\Home\HomeRoutes;
use Misuzu\Info\InfoRoutes;
2023-07-15 17:02:46 +00:00
use Misuzu\News\News;
use Misuzu\News\NewsRoutes;
2023-08-30 22:37:21 +00:00
use Misuzu\Perms\Permissions;
use Misuzu\Profile\ProfileFields;
use Misuzu\Satori\SatoriRoutes;
2022-09-13 13:14:49 +00:00
use Misuzu\SharpChat\SharpChatRoutes;
2023-09-08 20:40:48 +00:00
use Misuzu\URLs\URLRegistry;
use Misuzu\Users\UsersContext;
use Misuzu\Users\UserInfo;
use Misuzu\Users\Assets\AssetsRoutes;
2022-09-13 13:14:49 +00:00
// 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
// instances of certain classes should only be made as needed,
// dunno if i want null checks some maybe some kind of init func should be called first like is the case
// with the http shit
class MisuzuContext {
private IDbConnection $dbConn;
2023-01-01 20:23:53 +00:00
private IConfig $config;
2022-09-13 13:14:49 +00:00
private HttpFx $router;
private SasaeEnvironment $templating;
2023-09-08 20:40:48 +00:00
private URLRegistry $urls;
private AuditLog $auditLog;
private Counters $counters;
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;
2023-09-08 00:43:00 +00:00
private AuthContext $authCtx;
private UsersContext $usersCtx;
private ForumContext $forumCtx;
private ProfileFields $profileFields;
2023-08-30 22:37:21 +00:00
private Permissions $perms;
private AuthInfo $authInfo;
2023-09-08 20:40:48 +00:00
private SiteInfo $siteInfo;
2022-09-13 13:14:49 +00:00
2023-01-01 20:23:53 +00:00
public function __construct(IDbConnection $dbConn, IConfig $config) {
$this->dbConn = $dbConn;
2023-01-01 20:23:53 +00:00
$this->config = $config;
2023-09-08 20:40:48 +00:00
$this->urls = new URLRegistry;
$this->registerURLs();
2023-09-08 00:43:00 +00:00
$this->perms = new Permissions($dbConn);
2023-08-30 22:37:21 +00:00
$this->authInfo = new AuthInfo($this->perms);
2023-09-08 20:40:48 +00:00
$this->siteInfo = new SiteInfo($config->scopeTo('site'));
2023-09-08 00:43:00 +00:00
$this->authCtx = new AuthContext($dbConn, $config->scopeTo('auth'));
$this->usersCtx = new UsersContext($dbConn);
$this->forumCtx = new ForumContext($dbConn);
2023-09-08 00:43:00 +00:00
$this->auditLog = new AuditLog($dbConn);
$this->changelog = new Changelog($dbConn);
$this->comments = new Comments($dbConn);
$this->counters = new Counters($dbConn);
$this->emotes = new Emotes($dbConn);
$this->news = new News($dbConn);
$this->profileFields = new ProfileFields($dbConn);
}
public function getDbConn(): IDbConnection {
return $this->dbConn;
}
public function getDbQueryCount(): int {
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
return $result->next() ? $result->getInteger(1) : 0;
2022-09-13 13:14:49 +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-09-08 20:40:48 +00:00
public function getURLs(): URLRegistry {
return $this->urls;
}
2023-01-01 20:23:53 +00:00
public function getConfig(): IConfig {
return $this->config;
}
2023-09-08 01:05:17 +00:00
public function getRouter(): IRouter {
return $this->router;
2022-09-13 13:14:49 +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;
}
public function getAuditLog(): AuditLog {
return $this->auditLog;
}
public function getCounters(): Counters {
return $this->counters;
}
public function getProfileFields(): ProfileFields {
return $this->profileFields;
}
2023-08-30 22:37:21 +00:00
public function getPerms(): Permissions {
return $this->perms;
}
2023-09-08 00:43:00 +00:00
public function getAuthContext(): AuthContext {
return $this->authCtx;
}
public function getUsersContext(): UsersContext {
return $this->usersCtx;
}
public function getForumContext(): ForumContext {
return $this->forumCtx;
}
public function getAuthInfo(): AuthInfo {
return $this->authInfo;
}
2023-09-08 20:40:48 +00:00
public function getSiteInfo(): SiteInfo {
return $this->siteInfo;
}
public function createAuditLog(string $action, array $params = [], UserInfo|string|null $userInfo = null): void {
if($userInfo === null && $this->authInfo->isLoggedIn())
$userInfo = $this->authInfo->getUserInfo();
$this->auditLog->createLog(
$userInfo,
$action,
$params,
$_SERVER['REMOTE_ADDR'] ?? '::1',
$_SERVER['COUNTRY_CODE'] ?? 'XX'
);
}
2023-08-31 21:33:34 +00:00
private ?bool $hasManageAccess = null;
public function hasManageAccess(): bool {
$this->hasManageAccess ??= $this->authInfo->isLoggedIn()
&& !$this->usersCtx->hasActiveBan($this->authInfo->getUserInfo())
&& $this->authInfo->getPerms('global')->check(Perm::G_IS_JANITOR);
2023-08-31 21:33:34 +00:00
return $this->hasManageAccess;
}
2023-08-31 21:33:34 +00:00
public function getWebAssetInfo(): ?object {
return json_decode(file_get_contents(MSZ_ASSETS . '/current.json'));
}
private ?string $chatUrl = null;
public function getChatURL(): string {
$this->chatUrl ??= $this->config->getString('sockChat.chatPath.normal');
return $this->chatUrl;
}
public function startTemplating(): void {
$globals = $this->config->getValues([
['eeprom.path:s', '', 'eeprom_path'],
['eeprom.app:s', '', 'eeprom_app'],
]);
2023-09-08 00:54:19 +00:00
$isDebug = Environment::isDebug();
2023-09-08 20:40:48 +00:00
$globals['site_info'] = $this->siteInfo;
$globals['auth_info'] = $this->authInfo;
2023-09-08 20:40:48 +00:00
$globals['assets'] = $this->getWebAssetInfo();
$globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->getUserInfo());
2023-09-08 00:54:19 +00:00
$globals['display_timings_info'] = $isDebug || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW);
2023-08-31 21:33:34 +00:00
2023-09-08 00:54:19 +00:00
$this->templating = new SasaeEnvironment(
2023-08-31 21:33:34 +00:00
MSZ_TEMPLATES,
2023-09-08 00:54:19 +00:00
cache: $isDebug ? null : ['Misuzu', GitInfo::hash(true)],
debug: $isDebug
2023-08-31 21:33:34 +00:00
);
2023-09-08 00:54:19 +00:00
$this->templating->addExtension(new MisuzuSasaeExtension($this));
$this->templating->addGlobal('globals', $globals);
2023-09-08 00:54:19 +00:00
Template::init($this->templating);
}
2023-08-31 21:33:34 +00:00
public function startRouter(): void {
2022-09-13 13:14:49 +00:00
$this->router = new HttpFx;
$this->router->use('/', function($response) {
$response->setPoweredBy('Misuzu');
});
$this->registerErrorPages();
$this->registerHttpRoutes();
2022-09-13 13:14:49 +00:00
}
2023-08-31 21:33:34 +00:00
public function dispatchRouter(?HttpRequest $request = null): void {
2022-09-13 13:14:49 +00:00
$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 {
$this->router->register(new HomeRoutes(
$this->config,
$this->dbConn,
2023-09-08 20:40:48 +00:00
$this->siteInfo,
$this->authInfo,
$this->changelog,
$this->comments,
$this->counters,
$this->news,
$this->usersCtx
));
2022-09-13 13:14:49 +00:00
$this->router->register(new AssetsRoutes(
$this->authInfo,
2023-09-08 20:40:48 +00:00
$this->urls,
$this->usersCtx
));
2022-09-13 13:14:49 +00:00
$this->router->register(new InfoRoutes);
2022-09-13 13:14:49 +00:00
$this->router->register(new NewsRoutes(
2023-09-08 20:40:48 +00:00
$this->siteInfo,
$this->authInfo,
2023-09-08 20:40:48 +00:00
$this->urls,
$this->news,
$this->usersCtx,
$this->comments
));
2022-09-13 13:14:49 +00:00
$this->router->register(new ChangelogRoutes(
2023-09-08 20:40:48 +00:00
$this->siteInfo,
$this->urls,
$this->changelog,
$this->usersCtx,
$this->authInfo,
$this->comments
));
2022-09-13 13:14:49 +00:00
$this->router->register(new SharpChatRoutes(
$this->config->scopeTo('sockChat'),
2023-09-08 20:40:48 +00:00
$this->urls,
$this->usersCtx,
2023-09-08 00:43:00 +00:00
$this->authCtx,
$this->emotes,
$this->perms,
2023-09-08 00:43:00 +00:00
$this->authInfo
));
2022-09-13 13:14:49 +00:00
$this->router->register(new SatoriRoutes(
$this->config->scopeTo('satori'),
$this->usersCtx,
$this->forumCtx,
$this->profileFields
));
2022-09-13 13:14:49 +00:00
2023-09-08 20:40:48 +00:00
$this->router->register(new LegacyRoutes($this->urls));
}
2022-09-13 13:14:49 +00:00
2023-09-08 20:40:48 +00:00
public function registerURLs(): void {
// eventually this should be handled by context classes
$this->urls->register('index', '/');
$this->urls->register('info', '/info/<title>');
$this->urls->register('search-index', '/search.php');
$this->urls->register('search-query', '/search.php', ['q' => '<query>'], '<section>');
$this->urls->register('auth-login', '/auth/login.php', ['username' => '<username>', 'redirect' => '<redirect>']);
$this->urls->register('auth-login-welcome', '/auth/login.php', ['welcome' => '1', 'username' => '<username>']);
$this->urls->register('auth-register', '/auth/register.php');
$this->urls->register('auth-forgot', '/auth/password.php');
$this->urls->register('auth-reset', '/auth/password.php', ['user' => '<user>']);
$this->urls->register('auth-logout', '/auth/logout.php', ['csrf' => '<csrf>']);
$this->urls->register('auth-resolve-user', '/auth/login.php', ['resolve' => '1', 'name' => '<username>']);
$this->urls->register('auth-two-factor', '/auth/twofactor.php', ['token' => '<token>']);
$this->urls->register('auth-revert', '/auth/revert.php', ['csrf' => '<csrf>']);
$this->urls->register('changelog-index', '/changelog', ['date' => '<date>', 'user' => '<user>', 'tags' => '<tags>', 'p' => '<page>']);
$this->urls->register('changelog-feed-rss', '/changelog.rss');
$this->urls->register('changelog-feed-atom', '/changelog.atom');
$this->urls->register('changelog-change', '/changelog/change/<change>');
$this->urls->register('changelog-change-comments', '/changelog/change/<change>', fragment: 'comments');
$this->urls->register('news-index', '/news', ['p' => '<page>']);
$this->urls->register('news-category', '/news/<category>', ['p' => '<page>']);
$this->urls->register('news-post', '/news/post/<post>');
$this->urls->register('news-post-comments', '/news/post/<post>', fragment: 'comments');
$this->urls->register('news-feed-rss', '/news.rss');
$this->urls->register('news-category-feed-rss', '/news/<category>.rss');
$this->urls->register('news-feed-atom', '/news.atom');
$this->urls->register('news-category-feed-atom', '/news/<category>.atom');
$this->urls->register('forum-index', '/forum');
$this->urls->register('forum-leaderboard', '/forum/leaderboard.php', ['id' => '<id>', 'mode' => '<mode>']);
$this->urls->register('forum-mark-global', '/forum/index.php', ['m' => 'mark']);
$this->urls->register('forum-mark-single', '/forum/index.php', ['m' => 'mark', 'f' => '<forum>']);
$this->urls->register('forum-topic-new', '/forum/posting.php', ['f' => '<forum>']);
$this->urls->register('forum-reply-new', '/forum/posting.php', ['t' => '<topic>']);
$this->urls->register('forum-category', '/forum/forum.php', ['f' => '<forum>', 'p' => '<page>']);
$this->urls->register('forum-category-root', '/forum/index.php', fragment: '<forum>');
$this->urls->register('forum-topic', '/forum/topic.php', ['t' => '<topic>', 'page' => '<page>']);
$this->urls->register('forum-topic-create', '/forum/posting.php', ['f' => '<forum>']);
$this->urls->register('forum-topic-bump', '/forum/topic.php', ['t' => '<topic>', 'm' => 'bump', 'csrf' => '<csrf>']);
$this->urls->register('forum-topic-lock', '/forum/topic.php', ['t' => '<topic>', 'm' => 'lock', 'csrf' => '<csrf>']);
$this->urls->register('forum-topic-unlock', '/forum/topic.php', ['t' => '<topic>', 'm' => 'unlock', 'csrf' => '<csrf>']);
$this->urls->register('forum-topic-delete', '/forum/topic.php', ['t' => '<topic>', 'm' => 'delete', 'csrf' => '<csrf>']);
$this->urls->register('forum-topic-restore', '/forum/topic.php', ['t' => '<topic>', 'm' => 'restore', 'csrf' => '<csrf>']);
$this->urls->register('forum-topic-nuke', '/forum/topic.php', ['t' => '<topic>', 'm' => 'nuke', 'csrf' => '<csrf>']);
$this->urls->register('forum-post', '/forum/topic.php', ['p' => '<post>'], 'p<post>');
$this->urls->register('forum-post-create', '/forum/posting.php', ['t' => '<topic>']);
$this->urls->register('forum-post-delete', '/forum/post.php', ['p' => '<post>', 'm' => 'delete']);
$this->urls->register('forum-post-restore', '/forum/post.php', ['p' => '<post>', 'm' => 'restore']);
$this->urls->register('forum-post-nuke', '/forum/post.php', ['p' => '<post>', 'm' => 'nuke']);
$this->urls->register('forum-post-quote', '/forum/posting.php', ['q' => '<post>']);
$this->urls->register('forum-post-edit', '/forum/posting.php', ['p' => '<post>', 'm' => 'edit']);
$this->urls->register('user-list', '/members.php', ['r' => '<role>', 'ss' => '<sort>', 'sd' => '<direction>', 'p' => '<page>']);
$this->urls->register('user-profile', '/profile.php', ['u' => '<user>']);
$this->urls->register('user-profile-forum-topics', '/profile.php', ['u' => '<user>', 'm' => 'forum-topics']);
$this->urls->register('user-profile-forum-posts', '/profile.php', ['u' => '<user>', 'm' => 'forum-posts']);
$this->urls->register('user-profile-edit', '/profile.php', ['u' => '<user>', 'edit' => '1']);
$this->urls->register('user-account-standing', '/profile.php', ['u' => '<user>'], 'account-standing');
$this->urls->register('user-avatar', '/assets/avatar/<user>', ['res' => '<res>']);
$this->urls->register('user-background', '/assets/profile-background/<user>');
$this->urls->register('settings-index', '/settings');
$this->urls->register('settings-account', '/settings/account.php');
$this->urls->register('settings-sessions', '/settings/sessions.php', ['p' => '<page>']);
$this->urls->register('settings-logs', '/settings/logs.php');
$this->urls->register('settings-logs-logins', '/settings/logs.php', ['ap' => '<account-page>', 'hp' => '<page>'], 'login-history');
$this->urls->register('settings-logs-account', '/settings/logs.php', ['hp' => '<logins-page>', 'ap' => '<page>'], 'account-log');
$this->urls->register('settings-data', '/settings/data.php');
$this->urls->register('comment-create', '/comments.php', ['m' => 'create', 'return' => '<return>']);
$this->urls->register('comment-vote', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'vote', 'v' => '<vote>', 'return' => '<return>']);
$this->urls->register('comment-delete', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'delete', 'return' => '<return>']);
$this->urls->register('comment-restore', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'restore', 'return' => '<return>']);
$this->urls->register('comment-pin', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'pin', 'return' => '<return>']);
$this->urls->register('comment-unpin', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'unpin', 'return' => '<return>']);
$this->urls->register('manage-index', '/manage');
$this->urls->register('manage-general-overview', '/manage/general');
$this->urls->register('manage-general-logs', '/manage/general/logs.php', ['p' => '<page>']);
$this->urls->register('manage-general-emoticons', '/manage/general/emoticons.php');
$this->urls->register('manage-general-emoticon', '/manage/general/emoticon.php', ['e' => '<emote>']);
$this->urls->register('manage-general-emoticon-order-up', '/manage/general/emoticons.php', ['emote' => '<emote>', 'order' => 'd', 'csrf' => '<csrf>']);
$this->urls->register('manage-general-emoticon-order-down', '/manage/general/emoticons.php', ['emote' => '<emote>', 'order' => 'i', 'csrf' => '<csrf>']);
$this->urls->register('manage-general-emoticon-delete', '/manage/general/emoticons.php', ['emote' => '<emote>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-general-emoticon-alias', '/manage/general/emoticons.php', ['emote' => '<emote>', 'alias' => '<string>', 'csrf' => '<csrf>']);
$this->urls->register('manage-general-settings', '/manage/general/settings.php');
$this->urls->register('manage-general-setting', '/manage/general/setting.php', ['name' => '<name>', 'type' => '<type>']);
$this->urls->register('manage-general-setting-delete', '/manage/general/setting-delete.php', ['name' => '<name>']);
$this->urls->register('manage-forum-categories', '/manage/forum/index.php');
$this->urls->register('manage-forum-category', '/manage/forum/category.php', ['f' => '<forum>']);
$this->urls->register('manage-forum-topic-redirs', '/manage/forum/redirs.php', ['p' => '<page>']);
$this->urls->register('manage-forum-topic-redirs-create', '/manage/forum/redirs.php');
$this->urls->register('manage-forum-topic-redirs-nuke', '/manage/forum/redirs.php', ['m' => 'explode', 't' => '<topic>', 'csrf' => '<csrf>']);
$this->urls->register('manage-changelog-changes', '/manage/changelog', ['p' => '<page>']);
$this->urls->register('manage-changelog-change', '/manage/changelog/change.php', ['c' => '<change>']);
$this->urls->register('manage-changelog-change-delete', '/manage/changelog/change.php', ['c' => '<change>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-changelog-tags', '/manage/changelog/tags.php');
$this->urls->register('manage-changelog-tag', '/manage/changelog/tag.php', ['t' => '<tag>']);
$this->urls->register('manage-changelog-tag-delete', '/manage/changelog/tag.php', ['t' => '<tag>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-news-categories', '/manage/news/categories.php', ['p' => '<page>']);
$this->urls->register('manage-news-category', '/manage/news/category.php', ['c' => '<category>']);
$this->urls->register('manage-news-category-delete', '/manage/news/category.php', ['c' => '<category>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-news-posts', '/manage/news/posts.php', ['p' => '<page>']);
$this->urls->register('manage-news-post', '/manage/news/post.php', ['p' => '<post>']);
$this->urls->register('manage-news-post-delete', '/manage/news/post.php', ['p' => '<post>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-users', '/manage/users', ['p' => '<page>']);
$this->urls->register('manage-user', '/manage/users/user.php', ['u' => '<user>']);
$this->urls->register('manage-users-warnings', '/manage/users/warnings.php', ['u' => '<user>', 'p' => '<page>']);
$this->urls->register('manage-users-warning', '/manage/users/warning.php', ['u' => '<user>']);
$this->urls->register('manage-users-warning-delete', '/manage/users/warning.php', ['w' => '<warning>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-users-notes', '/manage/users/notes.php', ['u' => '<user>', 'p' => '<page>']);
$this->urls->register('manage-users-note', '/manage/users/note.php', ['n' => '<note>', 'u' => '<user>']);
$this->urls->register('manage-users-note-delete', '/manage/users/note.php', ['n' => '<note>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-users-bans', '/manage/users/bans.php', ['u' => '<user>', 'p' => '<page>']);
$this->urls->register('manage-users-ban', '/manage/users/ban.php', ['u' => '<user>']);
$this->urls->register('manage-users-ban-delete', '/manage/users/ban.php', ['b' => '<ban>', 'delete' => '1', 'csrf' => '<csrf>']);
$this->urls->register('manage-roles', '/manage/users/roles.php', ['p' => '<page>']);
$this->urls->register('manage-role', '/manage/users/role.php', ['r' => '<role>']);
2022-09-13 13:14:49 +00:00
}
}