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

277 lines
9.2 KiB
PHP
Raw Normal View History

2022-09-13 15:14:49 +02:00
<?php
namespace Misuzu;
use Misuzu\Template;
2024-01-30 23:47:02 +00:00
use Misuzu\Auth\{AuthContext,AuthInfo};
use Misuzu\AuditLog\AuditLog;
2023-07-15 02:05:49 +00:00
use Misuzu\Changelog\Changelog;
2023-07-15 23:58:17 +00:00
use Misuzu\Comments\Comments;
use Misuzu\Counters\Counters;
use Misuzu\Emoticons\Emotes;
use Misuzu\Forum\ForumContext;
2024-01-30 23:47:02 +00:00
use Misuzu\Messages\MessagesContext;
2023-07-15 17:02:46 +00:00
use Misuzu\News\News;
2023-08-30 22:37:21 +00:00
use Misuzu\Perms\Permissions;
use Misuzu\Profile\ProfileFields;
2024-01-30 23:47:02 +00:00
use Misuzu\Users\{UsersContext,UserInfo};
2024-11-13 23:30:34 +00:00
use RPCii\HmacVerificationProvider;
use RPCii\Server\HttpRpcServer;
2024-10-05 02:40:29 +00:00
use Index\Config\Config;
use Index\Db\DbConnection;
use Index\Db\Migration\{DbMigrationManager,DbMigrationRepo,FsDbMigrationRepo};
use Index\Templating\TplEnvironment;
use Index\Urls\UrlRegistry;
2022-09-13 15:14:49 +02: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 {
2024-10-05 02:40:29 +00:00
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;
2024-01-30 23:47:02 +00:00
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;
2022-09-13 15:14:49 +02:00
2023-09-10 00:04:53 +00:00
// this probably shouldn't be available
public private(set) UrlRegistry $urls;
public function __construct(
public private(set) DbConnection $dbConn,
public private(set) Config $config
) {
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->forumCtx = new ForumContext($dbConn);
2024-01-30 23:47:02 +00:00
$this->messagesCtx = new MessagesContext($dbConn);
$this->usersCtx = new UsersContext($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 getDbQueryCount(): int {
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
return $result->next() ? $result->getInteger(1) : 0;
2022-09-13 15:14:49 +02:00
}
public function createMigrationManager(): DbMigrationManager {
return new DbMigrationManager($this->dbConn, 'msz_' . DbMigrationManager::DEFAULT_TABLE);
}
2024-10-05 02:40:29 +00:00
public function createMigrationRepo(): DbMigrationRepo {
return new FsDbMigrationRepo(MSZ_MIGRATIONS);
}
2024-12-02 21:33:15 +00:00
/** @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'
);
}
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->userInfo)
&& $this->authInfo->getPerms('global')->check(Perm::G_IS_JANITOR);
2023-08-31 21:33:34 +00:00
return $this->hasManageAccess;
}
2024-12-02 02:28:08 +00:00
public function getWebAssetInfo(): object {
2023-08-31 21:33:34 +00:00
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'],
2024-01-30 23:47:02 +00:00
['eeprom.appmsgs:s', '', 'eeprom_app_messages'],
2023-08-31 21:33:34 +00:00
]);
$isDebug = MSZ_DEBUG;
2023-09-08 20:40:48 +00:00
$globals['site_info'] = $this->siteInfo;
$globals['auth_info'] = $this->authInfo;
$globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->userInfo);
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
2024-10-05 02:40:29 +00:00
$this->templating = new TplEnvironment(
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
);
2024-10-05 02:40:29 +00:00
$this->templating->addExtension(new TemplatingExtension($this));
2023-09-08 00:54:19 +00:00
$this->templating->addGlobal('globals', $globals);
2023-09-08 00:54:19 +00:00
Template::init($this->templating);
}
2023-09-10 00:04:53 +00:00
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext;
$this->urls = $routingCtx->urls;
2022-09-13 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Home\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 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Users\Assets\AssetsRoutes(
$this->authInfo,
2023-09-08 20:40:48 +00:00
$this->urls,
$this->usersCtx
));
2022-09-13 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Info\InfoRoutes);
2022-09-13 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\News\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 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Messages\MessagesRoutes(
$this->config->scopeTo('messages'),
$this->urls,
$this->authInfo,
$this->messagesCtx,
$this->usersCtx,
$this->perms
2024-01-30 23:47:02 +00:00
));
$routingCtx->register(new \Misuzu\Forum\ForumCategoriesRoutes(
$this->forumCtx,
2024-12-18 23:58:53 +00:00
$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,
));
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Changelog\ChangelogRoutes(
2023-09-08 20:40:48 +00:00
$this->siteInfo,
$this->urls,
$this->changelog,
$this->usersCtx,
$this->authInfo,
$this->comments
));
2022-09-13 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\SharpChat\SharpChatRoutes(
$this->config->scopeTo('sockChat'),
$this->config->scopeTo('impersonate'),
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,
$this->authInfo,
$this->counters
));
2022-09-13 15:14:49 +02:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Satori\SatoriRoutes(
$this->config->scopeTo('satori'),
$this->usersCtx,
$this->forumCtx,
$this->profileFields
));
2022-09-13 15:14:49 +02:00
$routingCtx->register(new LegacyRoutes($this->urls));
2024-11-13 23:30:34 +00:00
$rpcServer = new HttpRpcServer;
$routingCtx->router->register($rpcServer->createRouteHandler(
2024-08-25 23:03:46 +00:00
new HmacVerificationProvider(fn() => $this->config->getString('aleister.secret'))
));
2024-11-13 23:30:34 +00:00
$rpcServer->register(new Auth\AuthRpcHandler(
2024-08-25 23:03:46 +00:00
$this->config->scopeTo('impersonate'),
$this->usersCtx,
$this->authCtx
));
2024-11-14 02:44:02 +00:00
$rpcServer->register(new Emoticons\EmotesRpcHandler(
$this->emotes
));
2024-11-13 23:30:34 +00:00
$rpcServer->register(new Users\UsersRpcHandler(
$this->siteInfo,
$this->urls,
$this->usersCtx
));
2024-08-25 23:03:46 +00:00
// This RPC server will eventually despawn when Hanyuu fully owns auth
2024-11-13 23:30:34 +00:00
$hanyuuRpcServer = new HttpRpcServer;
$routingCtx->router->scopeTo('/_hanyuu')->register($hanyuuRpcServer->createRouteHandler(
new HmacVerificationProvider(fn() => $this->config->getString('hanyuu.secret'))
));
2024-11-13 23:30:34 +00:00
$hanyuuRpcServer->register(new Hanyuu\HanyuuRpcHandler(
fn() => $this->config->getString('hanyuu.endpoint'),
2024-07-20 19:35:50 +00:00
$this->config->scopeTo('impersonate'),
$this->urls,
$this->usersCtx,
$this->authCtx
2024-07-20 19:35:50 +00:00
));
2023-09-10 00:04:53 +00:00
return $routingCtx;
2022-09-13 15:14:49 +02:00
}
}