misuzu/src/MisuzuContext.php
2023-09-08 20:40:48 +00:00

456 lines
22 KiB
PHP

<?php
namespace Misuzu;
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;
use Index\Routing\IRouter;
use Sasae\SasaeEnvironment;
use Misuzu\Template;
use Misuzu\Auth\AuthContext;
use Misuzu\Auth\AuthInfo;
use Misuzu\AuditLog\AuditLog;
use Misuzu\Changelog\Changelog;
use Misuzu\Changelog\ChangelogRoutes;
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;
use Misuzu\News\News;
use Misuzu\News\NewsRoutes;
use Misuzu\Perms\Permissions;
use Misuzu\Profile\ProfileFields;
use Misuzu\Satori\SatoriRoutes;
use Misuzu\SharpChat\SharpChatRoutes;
use Misuzu\URLs\URLRegistry;
use Misuzu\Users\UsersContext;
use Misuzu\Users\UserInfo;
use Misuzu\Users\Assets\AssetsRoutes;
// 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;
private IConfig $config;
private HttpFx $router;
private SasaeEnvironment $templating;
private URLRegistry $urls;
private AuditLog $auditLog;
private Counters $counters;
private Emotes $emotes;
private Changelog $changelog;
private News $news;
private Comments $comments;
private AuthContext $authCtx;
private UsersContext $usersCtx;
private ForumContext $forumCtx;
private ProfileFields $profileFields;
private Permissions $perms;
private AuthInfo $authInfo;
private SiteInfo $siteInfo;
public function __construct(IDbConnection $dbConn, IConfig $config) {
$this->dbConn = $dbConn;
$this->config = $config;
$this->urls = new URLRegistry;
$this->registerURLs();
$this->perms = new Permissions($dbConn);
$this->authInfo = new AuthInfo($this->perms);
$this->siteInfo = new SiteInfo($config->scopeTo('site'));
$this->authCtx = new AuthContext($dbConn, $config->scopeTo('auth'));
$this->usersCtx = new UsersContext($dbConn);
$this->forumCtx = new ForumContext($dbConn);
$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;
}
public function createMigrationManager(): DbMigrationManager {
return new DbMigrationManager($this->dbConn, 'msz_' . DbMigrationManager::DEFAULT_TABLE);
}
public function createMigrationRepo(): IDbMigrationRepo {
return new FsDbMigrationRepo(MSZ_MIGRATIONS);
}
public function getURLs(): URLRegistry {
return $this->urls;
}
public function getConfig(): IConfig {
return $this->config;
}
public function getRouter(): IRouter {
return $this->router;
}
public function getEmotes(): Emotes {
return $this->emotes;
}
public function getChangelog(): Changelog {
return $this->changelog;
}
public function getNews(): News {
return $this->news;
}
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;
}
public function getPerms(): Permissions {
return $this->perms;
}
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;
}
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'
);
}
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);
return $this->hasManageAccess;
}
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'],
]);
$isDebug = Environment::isDebug();
$globals['site_info'] = $this->siteInfo;
$globals['auth_info'] = $this->authInfo;
$globals['assets'] = $this->getWebAssetInfo();
$globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->getUserInfo());
$globals['display_timings_info'] = $isDebug || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW);
$this->templating = new SasaeEnvironment(
MSZ_TEMPLATES,
cache: $isDebug ? null : ['Misuzu', GitInfo::hash(true)],
debug: $isDebug
);
$this->templating->addExtension(new MisuzuSasaeExtension($this));
$this->templating->addGlobal('globals', $globals);
Template::init($this->templating);
}
public function startRouter(): void {
$this->router = new HttpFx;
$this->router->use('/', function($response) {
$response->setPoweredBy('Misuzu');
});
$this->registerErrorPages();
$this->registerHttpRoutes();
}
public function dispatchRouter(?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 {
$this->router->register(new HomeRoutes(
$this->config,
$this->dbConn,
$this->siteInfo,
$this->authInfo,
$this->changelog,
$this->comments,
$this->counters,
$this->news,
$this->usersCtx
));
$this->router->register(new AssetsRoutes(
$this->authInfo,
$this->urls,
$this->usersCtx
));
$this->router->register(new InfoRoutes);
$this->router->register(new NewsRoutes(
$this->siteInfo,
$this->authInfo,
$this->urls,
$this->news,
$this->usersCtx,
$this->comments
));
$this->router->register(new ChangelogRoutes(
$this->siteInfo,
$this->urls,
$this->changelog,
$this->usersCtx,
$this->authInfo,
$this->comments
));
$this->router->register(new SharpChatRoutes(
$this->config->scopeTo('sockChat'),
$this->urls,
$this->usersCtx,
$this->authCtx,
$this->emotes,
$this->perms,
$this->authInfo
));
$this->router->register(new SatoriRoutes(
$this->config->scopeTo('satori'),
$this->usersCtx,
$this->forumCtx,
$this->profileFields
));
$this->router->register(new LegacyRoutes($this->urls));
}
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>']);
}
}