Skip to content

Commit

Permalink
Tweaks for new Athos release
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisnikoy committed Nov 10, 2023
1 parent 582589f commit 99af884
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
18 changes: 9 additions & 9 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function __construct() {
* @return bool true if registration was succesful
*/
public function register(string $username, string $password): bool {
$this->db->query('SELECT * FROM apl_users WHERE username=? AND password=?', $username, hash('sha256', $password));
$this->db->query('SELECT * FROM exm_users WHERE username=? AND password=?', $username, hash('sha256', $password));

if ($this->db->hasRows()) {
return false;
}

$this->db->query('INSERT INTO apl_users(username, password) VALUES(?, ?)', $username, hash('sha256', $password));
$this->db->query('INSERT INTO exm_users(username, password) VALUES(?, ?)', $username, hash('sha256', $password));

return $this->attemptLogin($username, $password);
}
Expand All @@ -76,7 +76,7 @@ public function logout() {
$this->loggedIn = false;

if (Session::valueForKey('ATHOS_SESSION_ID')) {
$this->db->query('UPDATE apl_sessions SET isActive=0 WHERE id=?', Session::valueForKey('ATHOS_SESSION_ID'));
$this->db->query('UPDATE exm_sessions SET is_active=false WHERE id=?', Session::valueForKey('ATHOS_SESSION_ID'));
}

Session::destroySession();
Expand Down Expand Up @@ -115,7 +115,7 @@ public function getUsername(): string {
if ($this->loggedIn) {
$sessionId = Session::valueForKey('ATHOS_SESSION_ID');

$this->db->query('SELECT username FROM apl_users WHERE id=(SELECT userId FROM apl_sessions WHERE id=?)', $sessionId);
$this->db->query('SELECT username FROM exm_users WHERE id=(SELECT user_id FROM exm_sessions WHERE id=?)', $sessionId);
return ucfirst($this->db->getRow()->username);
}

Expand All @@ -133,7 +133,7 @@ public function getUserCredentials(): string {
if ($this->loggedIn) {
$sessionId = Session::valueForKey('ATHOS_SESSION_ID');

$this->db->query('SELECT role FROM apl_users WHERE id=(SELECT userId FROM apl_sessions WHERE id=?)', $sessionId);
$this->db->query('SELECT role FROM exm_users WHERE id=(SELECT user_id FROM exm_sessions WHERE id=?)', $sessionId);
return $this->db->getRow()->role;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ private function attemptCookieLogin() {
* @param string $password
*/
private function attemptLogin(string $username, string $password): bool {
$this->db->query('SELECT * FROM apl_users WHERE username=? AND password=?', $username, hash('sha256', $password));
$this->db->query('SELECT * FROM exm_users WHERE username=? AND password=?', $username, hash('sha256', $password));

if (!$this->db->hasRows()) {
$this->loggedIn = false;
Expand All @@ -195,7 +195,7 @@ private function attemptLogin(string $username, string $password): bool {

$sessionId = md5($row->username . $row->password . time());

$this->db->query('INSERT INTO apl_sessions(id, userId, expiresAt) VALUES(?, ?, FROM_UNIXTIME(?))', $sessionId, $row->id, time()+$this->ttl);
$this->db->query('INSERT INTO exm_sessions(id, user_id, expires_at) VALUES(?, ?, to_timestamp(?))', $sessionId, $row->id, time()+$this->ttl);
$this->storeSessionData($sessionId);

$this->loggedIn = true;
Expand All @@ -210,10 +210,10 @@ private function attemptLogin(string $username, string $password): bool {
* @return bool True if the session was validated
*/
private function validateSession(string $sessionId): bool {
$this->db->query('SELECT * FROM apl_sessions WHERE id=? AND expiresAt > NOW() AND isActive=1', $sessionId);
$this->db->query('SELECT * FROM exm_sessions WHERE id=? AND expires_at > NOW() AND is_active=true', ...array($sessionId));

if ($this->db->hasRows()) {
$this->db->query('UPDATE apl_sessions SET lastUpdatedAt=NOW() WHERE id=?', $sessionId);
$this->db->query('UPDATE exm_sessions SET last_updated_at=NOW() WHERE id=?', $sessionId);
Session::setValueForKey('ATHOS_SESSION_ID', $sessionId);
$this->loggedIn = true;
} else {
Expand Down
22 changes: 17 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ private function development() {
ini_set('error_reporting', E_ALL);

$this->dbHost = $this->config->db->development->host;
$this->dbUser = $this->config->db->development->user;
if(substr($_SERVER['REQUEST_URI'], 0, 5) != '/rest') {
$this->dbUser = $this->config->db->development->adminUser;
} else {
$this->dbUser = $this->config->db->development->user;
}
$this->dbPass = $this->config->db->development->pass;
$this->dbName = $this->config->db->development->name;
}
Expand All @@ -94,11 +98,15 @@ private function development() {
* Errors and exceptions will be displayed.
*/
private function test() {
ini_set('display_errors', '1');
ini_set('display_errors', '0');
ini_set('error_reporting', E_ALL);

$this->dbHost = $this->config->db->test->host;
$this->dbUser = $this->config->db->test->user;
if(substr($_SERVER['REQUEST_URI'], 0, 5) != '/rest') {
$this->dbUser = $this->config->db->test->adminUser;
} else {
$this->dbUser = $this->config->db->test->user;
}
$this->dbPass = $this->config->db->test->pass;
$this->dbName = $this->config->db->test->name;
}
Expand All @@ -109,10 +117,14 @@ private function test() {
*/
private function production() {
ini_set('display_errors', '0');
ini_set('error_reporting', '0');
ini_set('error_reporting', 0);

$this->dbHost = $this->config->db->production->host;
$this->dbUser = $this->config->db->production->user;
if(substr($_SERVER['REQUEST_URI'], 0, 5) != '/rest') {
$this->dbUser = $this->config->db->production->adminUser;
} else {
$this->dbUser = $this->config->db->production->user;
}
$this->dbPass = $this->config->db->production->pass;
$this->dbName = $this->config->db->production->name;
}
Expand Down
19 changes: 7 additions & 12 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Database {
private $name;
private $result;
private $statement;
private $provider;
private $isConnected = false;

/**
* Initializes Database with the provided credentials.
Expand All @@ -35,6 +37,7 @@ function __construct() {
$this->user = $config->get('dbUser');
$this->pass = $config->get('dbPass');
$this->name = $config->get('dbName');
$this->provider = $config->get('db_provider');
}

/**
Expand All @@ -47,7 +50,7 @@ function __construct() {
* @return array Results of the query
*/
public function query(string $sql): array {
if (!$this->isConnected()) $this->connect();
if (!$this->isConnected) $this->connect();

$this->statement = $this->db->prepare($sql);
$params = func_get_args();
Expand Down Expand Up @@ -116,13 +119,14 @@ public function insertId(): int {
* @return bool true if connection is succesful.
*/
private function connect(): bool {
$this->db = new \PDO('mysql:host=' . $this->host . ';dbname=' . $this->name, $this->user, $this->pass);
$this->db = new \PDO($this->provider . ':host=' . $this->host . ';dbname=' . $this->name, $this->user, $this->pass);

if (!$this->db) {
throw new Error('An error occurred while connecting to the database: >>');
}

return $this->isConnected();
$this->isConnected = true;
return true;
}

/**
Expand All @@ -131,14 +135,5 @@ private function connect(): bool {
private function disconnect() {
$this->db->close();
}

/**
* Determines whether a database connection is active.
*
* @return true if a connection is present.
*/
private function isConnected(): bool {
return is_resource($this->db) && get_resource_type($this->db) == 'mysql link';
}
}
?>
30 changes: 20 additions & 10 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ public function __construct() {
*/
public function loadModule(string $moduleName, string $moduleAction = null) {
$this->moduleName = $moduleName;
$this->moduleAction = $moduleAction;

$this->loadDefaultController();

if ($this->moduleExists()) {
include $this->moduleFile;
$this->loadController($moduleName);
$this->loadController($moduleName, $moduleAction);

if (isset($this->viewDir)) {
$template = new Template();
Expand Down Expand Up @@ -77,8 +78,9 @@ private function moduleExists(): bool {
$this->viewDir = $directory . strtolower($this->moduleName) . '/views/';
}

$moduleControllerFile = $directory . strtolower($this->moduleName) . '/controllers/' . ucfirst($this->moduleName) . 'Controller.php';
$moduleControllerFile = $directory . strtolower($this->moduleName) . '/controllers/' . ucfirst(isset($this->moduleAction) ? $this->moduleAction : $this->moduleName) . 'Controller.php';
$standaloneControllerFile = $directory . ucfirst($this->moduleName) . 'Controller.php';
$mainControllerFile = $directory . strtolower($this->moduleName) . '/controllers/' . ucfirst($this->moduleName) . 'Controller.php';

if (file_exists($moduleControllerFile)) {
$this->moduleDir = $directory . strtolower($this->moduleName) . '/controllers/';
Expand All @@ -88,6 +90,10 @@ private function moduleExists(): bool {
$this->moduleDir = $directory;
$this->moduleFile = $standaloneControllerFile;
return true;
} else if(file_exists($mainControllerFile)) {
$this->moduleDir = $directory;
$this->moduleFile = $mainControllerFile;
return true;
}
}

Expand All @@ -104,14 +110,23 @@ private function moduleExists(): bool {
* @param bool $checkCredentials Allows for an override of credential checks
*/
private function loadController(string $moduleName, string $moduleAction = null, bool $checkCredentials = true) {
$controller = ucfirst($moduleName) . 'Controller';
$controller = new $controller();
if(class_exists(ucfirst(isset($moduleAction) ? $moduleAction : $moduleName) . 'Controller')) {
$controller = ucfirst(isset($moduleAction) ? $moduleAction : $moduleName) . 'Controller';
$controller = new $controller();
}else if(class_exists(ucfirst($moduleName) . 'Controller')) {
$controller = ucfirst($moduleName) . 'Controller';
$controller = new $controller();
} else {
$module = new Module();
$module->loadModule('error');
exit();
}

if ($checkCredentials) {
$requiresCredentials = $controller->requiresCredentials();
$acceptedCredentials = $controller->acceptedCredentials();

if ($requiresCredentials && isset($_GET) && $_GET['rt'] != 'login' && !$this->auth->loggedIn()) {
if ($requiresCredentials && $moduleName != 'login' && !$this->auth->loggedIn()) {
header('Location: ' . $this->config->get('site_root') . '/login');
return;
}
Expand All @@ -122,11 +137,6 @@ private function loadController(string $moduleName, string $moduleAction = null,
exit();
}
}

if ($moduleAction != null) {
$moduleAction = $moduleAction . 'Action';
$controller->$moduleAction();
}
}

/**
Expand Down

0 comments on commit 99af884

Please sign in to comment.