This repository has been archived on 2025-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
misuzu-interim/src/MisuzuContext.php

276 lines
9.2 KiB
PHP

<?php
namespace Misuzu;
use Misuzu\Template;
use Misuzu\Auth\{AuthContext,AuthInfo};
use Misuzu\AuditLog\AuditLog;
use Misuzu\Changelog\Changelog;
use Misuzu\Comments\Comments;
use Misuzu\Counters\Counters;
use Misuzu\Emoticons\Emotes;
use Misuzu\Forum\ForumContext;
use Misuzu\Messages\MessagesContext;
use Misuzu\News\News;
use Misuzu\Perms\Permissions;
use Misuzu\Profile\ProfileFields;
use Misuzu\Users\{UsersContext,UserInfo};
use RPCii\HmacVerificationProvider;
use RPCii\Server\HttpRpcServer;
use Index\Config\Config;
use Index\Db\DbConnection;
use Index\Db\Migration\{DbMigrationManager,DbMigrationRepo,FsDbMigrationRepo};
use Index\Templating\TplEnvironment;
use Index\Urls\UrlRegistry;
// 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 TplEnvironment $templating;
public private(set) AuditLog $auditLog;
public private(set) Counters $counters;
public private(set) Emotes $emotes;
public private(set) Changelog $changelog;
public private(set) News $news;
public private(set) Comments $comments;
public private(set) AuthContext $authCtx;
public private(set) ForumContext $forumCtx;
private MessagesContext $messagesCtx;
public private(set) UsersContext $usersCtx;
public private(set) ProfileFields $profileFields;
public private(set) Permissions $perms;
public private(set) AuthInfo $authInfo;
public private(set) SiteInfo $siteInfo;
// this probably shouldn't be available
public private(set) UrlRegistry $urls;
public function __construct(
public private(set) DbConnection $dbConn,
public private(set) Config $config
) {
$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->forumCtx = new ForumContext($dbConn);
$this->messagesCtx = new MessagesContext($dbConn);
$this->usersCtx = new UsersContext($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 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(): DbMigrationRepo {
return new FsDbMigrationRepo(MSZ_MIGRATIONS);
}
/** @param mixed[] $params */
public function createAuditLog(string $action, array $params = [], UserInfo|string|null $userInfo = null): void {
if($userInfo === null && $this->authInfo->isLoggedIn)
$userInfo = $this->authInfo->userInfo;
$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->userInfo)
&& $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'],
['eeprom.appmsgs:s', '', 'eeprom_app_messages'],
]);
$isDebug = MSZ_DEBUG;
$globals['site_info'] = $this->siteInfo;
$globals['auth_info'] = $this->authInfo;
$globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->userInfo);
$globals['display_timings_info'] = $isDebug || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW);
$this->templating = new TplEnvironment(
MSZ_TEMPLATES,
cache: $isDebug ? null : ['Misuzu', GitInfo::hash(true)],
debug: $isDebug
);
$this->templating->addExtension(new TemplatingExtension($this));
$this->templating->addGlobal('globals', $globals);
Template::init($this->templating);
}
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext;
$this->urls = $routingCtx->urls;
$routingCtx->register(new \Misuzu\Home\HomeRoutes(
$this->config,
$this->dbConn,
$this->siteInfo,
$this->authInfo,
$this->changelog,
$this->comments,
$this->counters,
$this->news,
$this->usersCtx
));
$routingCtx->register(new \Misuzu\Users\Assets\AssetsRoutes(
$this->authInfo,
$this->urls,
$this->usersCtx
));
$routingCtx->register(new \Misuzu\Info\InfoRoutes);
$routingCtx->register(new \Misuzu\News\NewsRoutes(
$this->siteInfo,
$this->authInfo,
$this->urls,
$this->news,
$this->usersCtx,
$this->comments
));
$routingCtx->register(new \Misuzu\Messages\MessagesRoutes(
$this->config->scopeTo('messages'),
$this->urls,
$this->authInfo,
$this->messagesCtx,
$this->usersCtx,
$this->perms
));
$routingCtx->register(new \Misuzu\Forum\ForumCategoriesRoutes(
$this->forumCtx,
$this->usersCtx,
$this->authInfo,
));
$routingCtx->register(new \Misuzu\Forum\ForumTopicsRoutes(
$this->urls,
$this->forumCtx,
$this->usersCtx,
$this->auditLog,
$this->authInfo,
));
$routingCtx->register(new \Misuzu\Forum\ForumPostsRoutes(
$this->urls,
$this->forumCtx,
$this->usersCtx,
$this->auditLog,
$this->authInfo,
));
$routingCtx->register(new \Misuzu\Changelog\ChangelogRoutes(
$this->siteInfo,
$this->urls,
$this->changelog,
$this->usersCtx,
$this->authInfo,
$this->comments
));
$routingCtx->register(new \Misuzu\SharpChat\SharpChatRoutes(
$this->config->scopeTo('sockChat'),
$this->config->scopeTo('impersonate'),
$this->urls,
$this->usersCtx,
$this->authCtx,
$this->emotes,
$this->perms,
$this->authInfo,
$this->counters
));
$routingCtx->register(new \Misuzu\Satori\SatoriRoutes(
$this->config->scopeTo('satori'),
$this->usersCtx,
$this->forumCtx,
$this->profileFields
));
$routingCtx->register(new LegacyRoutes($this->urls));
$rpcServer = new HttpRpcServer;
$routingCtx->router->register($rpcServer->createRouteHandler(
new HmacVerificationProvider(fn() => $this->config->getString('aleister.secret'))
));
$rpcServer->register(new Auth\AuthRpcHandler(
$this->config->scopeTo('impersonate'),
$this->usersCtx,
$this->authCtx
));
$rpcServer->register(new Emoticons\EmotesRpcHandler(
$this->emotes
));
$rpcServer->register(new Users\UsersRpcHandler(
$this->siteInfo,
$this->urls,
$this->usersCtx
));
// This RPC server will eventually despawn when Hanyuu fully owns auth
$hanyuuRpcServer = new HttpRpcServer;
$routingCtx->router->scopeTo('/_hanyuu')->register($hanyuuRpcServer->createRouteHandler(
new HmacVerificationProvider(fn() => $this->config->getString('hanyuu.secret'))
));
$hanyuuRpcServer->register(new Hanyuu\HanyuuRpcHandler(
fn() => $this->config->getString('hanyuu.endpoint'),
$this->config->scopeTo('impersonate'),
$this->urls,
$this->usersCtx,
$this->authCtx
));
return $routingCtx;
}
}