diff --git a/src/Auth.php b/src/Auth.php index 0a46048..953ffe5 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -72,7 +72,7 @@ public function login(string $username, string $password): bool { /** * Deactives a user session and destroys the session object. */ - public function logout() { + public function logout(): void { $this->loggedIn = false; if (Session::valueForKey('ATHOS_SESSION_ID')) { @@ -164,7 +164,7 @@ private function attemptSessionLogin(): bool { * @see attemptSessionLogin() * @return bool true if a valid session is found. */ - private function attemptCookieLogin() { + private function attemptCookieLogin(): bool { if (isset($_COOKIE['athos']) && is_string($_COOKIE['athos'])) { $s = json_decode($_COOKIE['athos'], true); @@ -210,7 +210,7 @@ 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 exm_sessions WHERE id=? AND expires_at > NOW() AND is_active=true', ...array($sessionId)); + $this->db->query('SELECT * FROM exm_sessions WHERE id=? AND expires_at > NOW() AND is_active=true', ...[$sessionId]); if ($this->db->hasRows()) { $this->db->query('UPDATE exm_sessions SET last_updated_at=NOW() WHERE id=?', $sessionId); @@ -228,11 +228,11 @@ private function validateSession(string $sessionId): bool { * * @param string $sessionId User session ID */ - private function storeSessionData(string $sessionId) { + private function storeSessionData(string $sessionId): void { Session::setValueForKey('ATHOS_SESSION_ID', $sessionId); if ($this->shouldUseCookies()) { - $s = json_encode(array('ATHOS_SESSION_ID' => $sessionId)); + $s = json_encode(['ATHOS_SESSION_ID' => $sessionId]); setcookie('athos', $s, time()+$this->ttl); } } diff --git a/src/Config.php b/src/Config.php index 141e1f6..3226315 100644 --- a/src/Config.php +++ b/src/Config.php @@ -36,15 +36,21 @@ public function __construct($configFile) { } $stage = $this->currentStage(); - $this->stage = $stage; - $this->setupEnvironment($this->config->environments->$stage); + + if(isset($stage)) { + $this->stage = $stage; + $this->setupEnvironment($this->config->environments->$stage); + } else { + echo '[ATHOS] Environment not found'; + exit(); + } } - public function addModuleDir(string $directory) { + public function addModuleDir(string $directory): void { array_push($this->config->module_dirs, $directory); } - public function getEnvironmentVariable(string $key) { + public function getEnvironmentVariable(string $key): ?string { $stage = $this->stage; if(isset($this->config->environments->$stage->keys->$key)) { @@ -79,7 +85,7 @@ public function get(string $key) { // Private methods // - private function setupEnvironment($environment) { + private function setupEnvironment($environment): void { ini_set('display_errors', isset($environment->error_reporting) && $environment->error_reporting == true ? 1 : 0); ini_set('error_reporting', isset($environment->error_reporting) && $environment->error_reporting == true ? E_ALL : E_ERROR | E_PARSE); @@ -99,14 +105,14 @@ private function setupEnvironment($environment) { * * @return string environment name, or false if not found. */ - public function currentStage() { + public function currentStage(): ?string { foreach(array_keys(get_object_vars($this->config->environments)) as $environment) { if (in_array($_SERVER['HTTP_HOST'], $this->config->environments->$environment->domains)) { return $environment; } } - return false; + return null; } } ?> diff --git a/src/Controller.php b/src/Controller.php index 6a0ee78..3c83bc0 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -57,6 +57,6 @@ public function requiresCredentials(): bool { * @return array Array of accepted credentials */ public function acceptedCredentials(): array { - return array('admin'); + return ['admin']; } } diff --git a/src/Database.php b/src/Database.php index 23f5083..8f11c37 100644 --- a/src/Database.php +++ b/src/Database.php @@ -80,7 +80,7 @@ public function hasRows(): bool { * @return array all results */ public function getRows(): array { - if (!$this->hasRows($this->result)) return array(); + if (!$this->hasRows($this->result)) return []; return $this->result; } @@ -132,7 +132,7 @@ private function connect(): bool { /** * Closes the database connection. */ - private function disconnect() { + private function disconnect(): void { $this->db->close(); } } diff --git a/src/Files.php b/src/Files.php index b5a8974..264aea8 100644 --- a/src/Files.php +++ b/src/Files.php @@ -21,7 +21,7 @@ class Files { * @return array Array of filenames */ public static function getFilesInDirectory(string $directory, string $extension = ''): array { - $files = array(); + $files = []; if (is_dir($directory) && $handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { diff --git a/src/Logger.php b/src/Logger.php index 430ea47..63259a6 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -1,7 +1,8 @@ 'Failed to encode JSON'], JSON_THROW_ON_ERROR); + return; + } + + echo $responseJson; + } + + try { + $db->query("INSERT INTO exm_logs(status_code, method, ipaddress, path, headers, request, response) VALUES(?, ?, ?, ?, ?, ?, ?)", $statusCode, $_SERVER['REQUEST_METHOD'], $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], json_encode(getallheaders()), file_get_contents('php://input'), json_encode($response)); + } catch (Exception $e) { + } - $db->query("INSERT INTO exm_logs(status_code, method, ipaddress, path, headers, request, response) VALUES(?, ?, ?, ?, ?, ?, ?)", $statusCode, $_SERVER['REQUEST_METHOD'], $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], json_encode(getallheaders()), file_get_contents('php://input'), json_encode($response)); } -} -?> + } + +?> \ No newline at end of file diff --git a/src/Module.php b/src/Module.php index 13e2d90..03fe0a8 100644 --- a/src/Module.php +++ b/src/Module.php @@ -41,7 +41,7 @@ public function __construct() { * @param string $moduleName Name of the module * @param string $moduleAction Optional action to load within the module */ - public function loadModule(string $moduleName, string $moduleAction = null) { + public function loadModule(string $moduleName, string $moduleAction = null): void { $this->moduleName = $moduleName; $this->moduleAction = $moduleAction; @@ -109,7 +109,7 @@ private function moduleExists(): bool { * @param string $moduleAction Optional action to load within the module * @param bool $checkCredentials Allows for an override of credential checks */ - private function loadController(string $moduleName, string $moduleAction = null, bool $checkCredentials = true) { + private function loadController(string $moduleName, string $moduleAction = null, bool $checkCredentials = true): void { if(class_exists(ucfirst(isset($moduleAction) ? $moduleAction : $moduleName) . 'Controller')) { $controller = ucfirst(isset($moduleAction) ? $moduleAction : $moduleName) . 'Controller'; $controller = new $controller(); @@ -142,7 +142,7 @@ private function loadController(string $moduleName, string $moduleAction = null, /** * Attempts to find the default controller and loads it if found. */ - private function loadDefaultController() { + private function loadDefaultController(): void { if(file_exists(SITE_PATH . '/modules/DefaultController.php')) { require_once SITE_PATH.'/modules/DefaultController.php'; $this->loadController('Default', null, false); diff --git a/src/Session.php b/src/Session.php index 1f55462..0fb65af 100644 --- a/src/Session.php +++ b/src/Session.php @@ -19,7 +19,7 @@ class Session { * @param string $key * @param $value */ - public static function setValueForKey(string $key, $value) { + public static function setValueForKey(string $key, $value): void { $_SESSION[$key] = $value; } @@ -43,7 +43,7 @@ public static function hasValueForKey(string $key): bool { * @param string $key * @return Value for provided key, or null if not found. */ - public static function valueForKey(string $key) { + public static function valueForKey(string $key): ?string { if (self::hasValueForKey($key)) { return $_SESSION[$key]; } @@ -56,14 +56,14 @@ public static function valueForKey(string $key) { * * @param string $key */ - public static function removeValueForKey(string $key) { + public static function removeValueForKey(string $key): void { unset($_SESSION[$key]); } /** * Completely destroys the user session */ - public static function destroySession() { + public static function destroySession(): void { if (session_status() === PHP_SESSION_ACTIVE) { session_destroy(); } diff --git a/src/Template.php b/src/Template.php index 90a73fb..30d9202 100644 --- a/src/Template.php +++ b/src/Template.php @@ -32,7 +32,7 @@ public function __construct() { * @param string $moduleName Name of the module -- Will be used as view name * @param string $moduleAction Optional action name. View will override main view */ - public function loadTemplate(string $viewDir, string $moduleName, string $moduleAction = null) { + public function loadTemplate(string $viewDir, string $moduleName, string $moduleAction = null): void { if(file_exists($viewDir . $moduleAction . '.html')) { $this->render($viewDir . $moduleAction . '.html'); } else if(file_exists($viewDir . $moduleName . '.html')){ @@ -45,7 +45,7 @@ public function loadTemplate(string $viewDir, string $moduleName, string $module * * @param string $file Full filename of the template to render */ - private function render(string $file) { + private function render(string $file): void { if(!file_exists($file)) { return; }