132 lines
4.1 KiB
PHP
132 lines
4.1 KiB
PHP
<?php
|
|
namespace Hanyuu;
|
|
|
|
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\IRouter;
|
|
use Hanyuu\Config\IConfig;
|
|
use Hanyuu\Templating\TemplateContext;
|
|
use Hanyuu\Users\IUsers;
|
|
use Hanyuu\Users\Db\DbUsers;
|
|
|
|
class HanyuuContext {
|
|
private IConfig $config;
|
|
private IDbConnection $dbConn;
|
|
private IUsers $users;
|
|
private ?TemplateContext $tpl = null;
|
|
|
|
public function __construct(IConfig $config, IDbConnection $dbConn) {
|
|
$this->config = $config;
|
|
$this->dbConn = $dbConn;
|
|
$this->users = new DbUsers($dbConn);
|
|
}
|
|
|
|
public function getSiteName(): string {
|
|
return $this->config->getValue('site:name', IConfig::T_STR, 'Hanyuu');
|
|
}
|
|
|
|
public function getUsers(): IUsers {
|
|
return $this->users;
|
|
}
|
|
|
|
public function getDatabase(): IDbConnection {
|
|
return $this->dbConn;
|
|
}
|
|
|
|
public function getDbQueryCount(): int {
|
|
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
|
|
return $result->next() ? $result->getInteger(0) : 0;
|
|
}
|
|
|
|
public function createMigrationManager(): DbMigrationManager {
|
|
return new DbMigrationManager($this->dbConn, 'hau_' . DbMigrationManager::DEFAULT_TABLE);
|
|
}
|
|
|
|
public function createMigrationRepo(): IDbMigrationRepo {
|
|
return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS);
|
|
}
|
|
|
|
public function getTemplating(): TemplateContext {
|
|
if($this->tpl === null) {
|
|
$this->tpl = new TemplateContext(HAU_DIR_TEMPLATES);
|
|
$this->tpl->setGlobal('hau', $this);
|
|
}
|
|
|
|
return $this->tpl;
|
|
}
|
|
|
|
public function renderTemplate(...$args): string {
|
|
return $this->getTemplating()->render(...$args);
|
|
}
|
|
|
|
public function getRouter(): IRouter {
|
|
return $this->router->getRouter();
|
|
}
|
|
|
|
public function setUpHttp(): void {
|
|
$this->router = new HttpFx;
|
|
$this->router->use('/', function($response) {
|
|
$response->setPoweredBy('Hanyuu');
|
|
});
|
|
|
|
$this->registerErrorPages();
|
|
$this->registerHttpRoutes();
|
|
}
|
|
|
|
public function dispatchHttp(?HttpRequest $request = null): void {
|
|
$this->router->dispatch($request);
|
|
}
|
|
|
|
private function registerErrorPages(): void {
|
|
$this->router->addErrorHandler(400, function($response) {
|
|
$response->setContent($this->renderTemplate('errors/400'));
|
|
});
|
|
$this->router->addErrorHandler(401, function($response) {
|
|
$response->setContent($this->renderTemplate('errors/401'));
|
|
});
|
|
$this->router->addErrorHandler(403, function($response) {
|
|
$response->setContent($this->renderTemplate('errors/403'));
|
|
});
|
|
$this->router->addErrorHandler(404, function($response) {
|
|
$response->setContent($this->renderTemplate('errors/404'));
|
|
});
|
|
$this->router->addErrorHandler(500, function($response) {
|
|
$response->setContent(file_get_contents(HAU_DIR_TEMPLATES . '/errors/500.html'));
|
|
});
|
|
$this->router->addErrorHandler(503, function($response) {
|
|
$response->setContent(file_get_contents(HAU_DIR_TEMPLATES . '/errors/503.html'));
|
|
});
|
|
}
|
|
|
|
private function registerHttpRoutes(): void {
|
|
$this->router->get('/', function($response, $request) {
|
|
return 503;
|
|
});
|
|
|
|
if(!HAU_DEBUG)
|
|
return;
|
|
|
|
$this->router->get('/test', function() {
|
|
$users = $this->getUsers();
|
|
$userInfo = $users->getUserInfoByName('flAsH');
|
|
|
|
$ffa = $users->getUserFirstFactorAuthInfos($userInfo);
|
|
$tfa = $users->getUserSecondFactorAuthInfos($userInfo);
|
|
|
|
$pwdInfo = $users->getUserPasswordInfo($userInfo);
|
|
|
|
$totpInfo = $users->getUserTOTPInfo($userInfo);
|
|
$totpGen = $totpInfo->createGenerator();
|
|
|
|
$emailInfos = $users->getUserEMailInfos($userInfo);
|
|
|
|
$backupInfos = $users->getUserBackupInfos($userInfo);
|
|
|
|
});
|
|
}
|
|
}
|