misuzu/src/MisuzuContext.php

322 lines
9.9 KiB
PHP
Raw 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;
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\Router;
2023-08-31 21:33:34 +00:00
use Sasae\SasaeEnvironment;
2022-09-13 13:14:49 +00:00
use Misuzu\Template;
2023-09-08 00:43:00 +00:00
use Misuzu\Auth\AuthContext;
use Misuzu\Auth\AuthInfo;
use Misuzu\Auth\AuthTokenPacker;
use Misuzu\AuditLog\AuditLog;
2023-07-15 02:05:49 +00:00
use Misuzu\Changelog\Changelog;
use Misuzu\Changelog\ChangelogRoutes;
2023-07-15 23:58:17 +00:00
use Misuzu\Comments\Comments;
use Misuzu\Config\IConfig;
use Misuzu\Counters\Counters;
use Misuzu\Emoticons\Emotes;
2023-08-28 01:17:34 +00:00
use Misuzu\Forum\Forum;
use Misuzu\Home\HomeRoutes;
use Misuzu\Info\InfoRoutes;
2023-07-15 17:02:46 +00:00
use Misuzu\News\News;
use Misuzu\News\NewsRoutes;
2023-08-30 22:37:21 +00:00
use Misuzu\Perms\Permissions;
use Misuzu\Profile\ProfileFields;
use Misuzu\Satori\SatoriRoutes;
2022-09-13 13:14:49 +00:00
use Misuzu\SharpChat\SharpChatRoutes;
use Misuzu\Users\UsersContext;
use Misuzu\Users\BanInfo;
use Misuzu\Users\UserInfo;
use Misuzu\Users\Assets\AssetsRoutes;
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;
2022-09-13 13:14:49 +00:00
private HttpFx $router;
private SasaeEnvironment $templating;
private AuditLog $auditLog;
private Counters $counters;
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 UsersContext $usersCtx;
private ProfileFields $profileFields;
2023-08-28 01:17:34 +00:00
private Forum $forum;
2023-08-30 22:37:21 +00:00
private Permissions $perms;
private AuthInfo $authInfo;
2022-09-13 13:14:49 +00:00
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 00:43:00 +00:00
$this->authCtx = new AuthContext($dbConn, $config->scopeTo('auth'));
$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->forum = new Forum($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-01-01 20:23:53 +00:00
public function getConfig(): IConfig {
return $this->config;
}
2022-09-13 13:14:49 +00:00
public function getRouter(): Router {
return $this->router->getRouter();
}
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-28 01:17:34 +00:00
public function getForum(): Forum {
return $this->forum;
}
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 createAuthTokenPacker(): AuthTokenPacker {
return new AuthTokenPacker($this->config->getString('auth.secret', 'meow'));
}
public function getAuthInfo(): AuthInfo {
return $this->authInfo;
}
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([
['site.name:s', 'Misuzu', 'site_name'],
['site.desc:s', '', 'site_description'],
['site.url:s', '', 'site_url'],
['eeprom.path:s', '', 'eeprom_path'],
['eeprom.app:s', '', 'eeprom_app'],
]);
2023-09-08 00:54:19 +00:00
$isDebug = Environment::isDebug();
2023-08-31 21:33:34 +00:00
$globals['assets'] = $this->getWebAssetInfo();
$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-08-31 21:33:34 +00:00
public function startRouter(): void {
2022-09-13 13:14:49 +00:00
$this->router = new HttpFx;
$this->router->use('/', function($response) {
$response->setPoweredBy('Misuzu');
});
$this->registerErrorPages();
$this->registerHttpRoutes();
2022-09-13 13:14:49 +00:00
}
2023-08-31 21:33:34 +00:00
public function dispatchRouter(?HttpRequest $request = null): void {
2022-09-13 13:14:49 +00:00
$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->authInfo,
$this->changelog,
$this->comments,
$this->counters,
$this->news,
$this->usersCtx
));
2022-09-13 13:14:49 +00:00
$this->router->register(new AssetsRoutes(
$this->authInfo,
$this->usersCtx
));
2022-09-13 13:14:49 +00:00
$this->router->register(new InfoRoutes);
2022-09-13 13:14:49 +00:00
$this->router->register(new NewsRoutes(
$this->config,
$this->authInfo,
$this->news,
$this->usersCtx,
$this->comments
));
2022-09-13 13:14:49 +00:00
$this->router->register(new ChangelogRoutes(
$this->config,
$this->changelog,
$this->usersCtx,
$this->authInfo,
$this->comments
));
2022-09-13 13:14:49 +00:00
$this->router->register(new SharpChatRoutes(
$this->config->scopeTo('sockChat'),
$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
$this->router->register(new SatoriRoutes(
$this->config->scopeTo('satori'),
$this->usersCtx,
$this->profileFields,
$this->forum
));
2022-09-13 13:14:49 +00:00
// below is still only otherwise available as stinky php files
2022-09-13 13:14:49 +00:00
$this->router->get('/auth.php', function($response, $request) {
$response->redirect(url([
'logout' => 'auth-logout',
'reset' => 'auth-reset',
'forgot' => 'auth-forgot',
'register' => 'auth-register',
][$request->getParam('m')] ?? 'auth-login'), true);
});
$this->router->get('/settings.php', function($response) {
$response->redirect(url('settings-index'), true);
2022-09-13 13:14:49 +00:00
});
}
}