misuzu/src/MisuzuContext.php

286 lines
8.6 KiB
PHP
Raw Permalink 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;
2024-01-30 23:47:02 +00:00
use Index\Data\Migration\{IDbMigrationRepo,DbMigrationManager,FsDbMigrationRepo};
2023-08-31 21:33:34 +00:00
use Sasae\SasaeEnvironment;
use Syokuhou\IConfig;
2022-09-13 13:14:49 +00:00
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;
2023-09-08 20:40:48 +00:00
use Misuzu\URLs\URLRegistry;
2024-01-30 23:47:02 +00:00
use Misuzu\Users\{UsersContext,UserInfo};
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;
private SasaeEnvironment $templating;
private AuditLog $auditLog;
private Counters $counters;
2023-09-10 00:04:53 +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;
2023-09-08 00:43:00 +00:00
private AuthContext $authCtx;
private ForumContext $forumCtx;
2024-01-30 23:47:02 +00:00
private MessagesContext $messagesCtx;
private UsersContext $usersCtx;
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-09-10 00:04:53 +00:00
// this probably shouldn't be available
private URLRegistry $urls;
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 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 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;
}
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'],
2024-01-30 23:47:02 +00:00
['eeprom.appmsgs:s', '', 'eeprom_app_messages'],
2023-08-31 21:33:34 +00:00
]);
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;
$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-09-10 00:04:53 +00:00
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext();
2022-09-13 13:14:49 +00:00
2023-09-10 00:04:53 +00:00
$this->urls = $routingCtx->getURLs();
2022-09-13 13:14:49 +00: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 13:14:49 +00: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 13:14:49 +00:00
2024-01-30 23:47:02 +00:00
$routingCtx->register(new \Misuzu\Info\InfoRoutes);
2022-09-13 13:14:49 +00: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 13:14:49 +00: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\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 13:14:49 +00: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,
2023-09-08 00:43:00 +00:00
$this->authInfo
));
2022-09-13 13:14:49 +00: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 13:14:49 +00:00
2023-09-10 00:04:53 +00:00
$routingCtx->register(new LegacyRoutes($this->urls));
2022-09-13 13:14:49 +00:00
2023-09-10 00:04:53 +00:00
return $routingCtx;
2022-09-13 13:14:49 +00:00
}
}