2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-09-08 00:54:19 +00:00
|
|
|
use Index\Environment;
|
2023-08-04 20:51:02 +00:00
|
|
|
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;
|
2023-10-20 22:29:28 +00:00
|
|
|
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};
|
2023-07-17 17:43:17 +00:00
|
|
|
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;
|
2023-07-28 23:17:33 +00:00
|
|
|
use Misuzu\Counters\Counters;
|
2023-07-12 21:52:55 +00:00
|
|
|
use Misuzu\Emoticons\Emotes;
|
2023-09-08 13:22:46 +00:00
|
|
|
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;
|
2023-07-29 22:18:20 +00:00
|
|
|
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
|
2023-07-22 16:37:57 +00:00
|
|
|
// 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
|
2023-01-06 20:35:03 +00:00
|
|
|
class MisuzuContext {
|
2023-01-01 19:06:01 +00:00
|
|
|
private IDbConnection $dbConn;
|
2023-01-01 20:23:53 +00:00
|
|
|
private IConfig $config;
|
2023-09-06 13:50:19 +00:00
|
|
|
private SasaeEnvironment $templating;
|
|
|
|
|
2023-07-17 17:43:17 +00:00
|
|
|
private AuditLog $auditLog;
|
2023-09-06 13:50:19 +00:00
|
|
|
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-06 13:50:19 +00:00
|
|
|
|
2023-09-08 00:43:00 +00:00
|
|
|
private AuthContext $authCtx;
|
2023-09-08 13:22:46 +00:00
|
|
|
private ForumContext $forumCtx;
|
2024-01-30 23:47:02 +00:00
|
|
|
private MessagesContext $messagesCtx;
|
|
|
|
private UsersContext $usersCtx;
|
2023-09-06 13:50:19 +00:00
|
|
|
|
2023-07-29 22:18:20 +00:00
|
|
|
private ProfileFields $profileFields;
|
2023-09-06 13:50:19 +00:00
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
private Permissions $perms;
|
2023-08-03 01:35:08 +00:00
|
|
|
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) {
|
2023-01-01 19:06:01 +00:00
|
|
|
$this->dbConn = $dbConn;
|
2023-01-01 20:23:53 +00:00
|
|
|
$this->config = $config;
|
2023-09-06 13:50:19 +00:00
|
|
|
|
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'));
|
2023-09-08 13:22:46 +00:00
|
|
|
$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);
|
2023-01-01 19:06:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:52:30 +00:00
|
|
|
public function getDbConn(): IDbConnection {
|
|
|
|
return $this->dbConn;
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:06:01 +00:00
|
|
|
public function getDbQueryCount(): int {
|
|
|
|
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
|
2023-07-15 17:15:40 +00:00
|
|
|
return $result->next() ? $result->getInteger(1) : 0;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 04:15:19 +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-07-12 21:52:55 +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;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:43:17 +00:00
|
|
|
public function getAuditLog(): AuditLog {
|
|
|
|
return $this->auditLog;
|
|
|
|
}
|
|
|
|
|
2023-07-28 23:17:33 +00:00
|
|
|
public function getCounters(): Counters {
|
|
|
|
return $this->counters;
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:18:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:50:19 +00:00
|
|
|
public function getUsersContext(): UsersContext {
|
|
|
|
return $this->usersCtx;
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
public function getForumContext(): ForumContext {
|
|
|
|
return $this->forumCtx;
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
public function getAuthInfo(): AuthInfo {
|
|
|
|
return $this->authInfo;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 20:40:48 +00:00
|
|
|
public function getSiteInfo(): SiteInfo {
|
|
|
|
return $this->siteInfo;
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:12:47 +00:00
|
|
|
public function createAuditLog(string $action, array $params = [], UserInfo|string|null $userInfo = null): void {
|
2023-09-06 20:06:07 +00:00
|
|
|
if($userInfo === null && $this->authInfo->isLoggedIn())
|
|
|
|
$userInfo = $this->authInfo->getUserInfo();
|
2023-07-17 17:43:17 +00:00
|
|
|
|
|
|
|
$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 {
|
2023-09-06 20:06:07 +00:00
|
|
|
$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-02 22:12:47 +00:00
|
|
|
|
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;
|
2023-09-06 20:06:07 +00:00
|
|
|
$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-08-02 22:12:47 +00:00
|
|
|
|
2023-09-08 00:54:19 +00:00
|
|
|
Template::init($this->templating);
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
$routingCtx->registerDefaultErrorPages();
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-01-30 23:47:02 +00:00
|
|
|
$routingCtx->register(new \Misuzu\Home\HomeRoutes(
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->config,
|
|
|
|
$this->dbConn,
|
2023-09-08 20:40:48 +00:00
|
|
|
$this->siteInfo,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->authInfo,
|
|
|
|
$this->changelog,
|
|
|
|
$this->comments,
|
|
|
|
$this->counters,
|
|
|
|
$this->news,
|
|
|
|
$this->usersCtx
|
2023-09-06 11:59:44 +00:00
|
|
|
));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-01-30 23:47:02 +00:00
|
|
|
$routingCtx->register(new \Misuzu\Users\Assets\AssetsRoutes(
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->authInfo,
|
2023-09-08 20:40:48 +00:00
|
|
|
$this->urls,
|
2023-09-06 13:50:19 +00:00
|
|
|
$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,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->authInfo,
|
2023-09-08 20:40:48 +00:00
|
|
|
$this->urls,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->news,
|
|
|
|
$this->usersCtx,
|
|
|
|
$this->comments
|
2023-09-06 11:59:44 +00:00
|
|
|
));
|
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
|
|
|
|
));
|
|
|
|
|
|
|
|
$routingCtx->register(new \Misuzu\Changelog\ChangelogRoutes(
|
2023-09-08 20:40:48 +00:00
|
|
|
$this->siteInfo,
|
|
|
|
$this->urls,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->changelog,
|
|
|
|
$this->usersCtx,
|
|
|
|
$this->authInfo,
|
|
|
|
$this->comments
|
2023-09-06 11:59:44 +00:00
|
|
|
));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-01-30 23:47:02 +00:00
|
|
|
$routingCtx->register(new \Misuzu\SharpChat\SharpChatRoutes(
|
2023-09-06 11:59:44 +00:00
|
|
|
$this->config->scopeTo('sockChat'),
|
2023-09-08 20:40:48 +00:00
|
|
|
$this->urls,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->usersCtx,
|
2023-09-08 00:43:00 +00:00
|
|
|
$this->authCtx,
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->emotes,
|
|
|
|
$this->perms,
|
2023-09-08 00:43:00 +00:00
|
|
|
$this->authInfo
|
2023-09-06 11:59:44 +00:00
|
|
|
));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-01-30 23:47:02 +00:00
|
|
|
$routingCtx->register(new \Misuzu\Satori\SatoriRoutes(
|
2023-09-06 13:50:19 +00:00
|
|
|
$this->config->scopeTo('satori'),
|
|
|
|
$this->usersCtx,
|
2023-09-08 13:22:46 +00:00
|
|
|
$this->forumCtx,
|
|
|
|
$this->profileFields
|
2023-09-06 11:59:44 +00:00
|
|
|
));
|
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
|
|
|
}
|
|
|
|
}
|