public function actionAuthentication() { if (!empty($_POST['username']) && !empty($_POST['password'])) { try { $condition = []; $condition['username'] = $_POST['username']; $condition['password'] = $_POST['password']; $condition['status'] = 1; $user = User::findByCondition($condition)[0]; Application::setCurrentByKey(['username' => $user->getUsername(), 'role' => $user->getUserRole()]); $logger = new Logger(); $logger->info('SUCCESSFUL LOGIN', ['code' => 100, 'info' => $condition]); setcookie('lastuser', $user->getUserName(), time() + 86400, '/'); setcookie('lastdate', time(), time() + 86400, '/'); header('Location: /'); } catch (E404Exception $e) { Application::catchException($e); } } else { $logger = new Logger(); $logger->notice('empty field exists!', ['code' => 101, 'info' => $_POST]); $_SESSION['notice'] = 'empty field exists!'; header('Location: /auth/login'); } }
/****************** parse url ***************************************/ $urlPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $urlParts = explode('/', trim($urlPath, '/')); $control = array_shift($urlParts); $action = array_shift($urlParts); /********* check authentication and set/unset $_SESSION['id'] ***********/ if (!Application::getCurrentByKey('username') && $action != 'authentication') { $control = 'auth'; $action = 'login'; } else { switch (true) { case !empty($urlParts): Application::setCurrentByKey(['pageid' => array_shift($urlParts)]); break; case !empty($_POST['id']): Application::setCurrentByKey(['pageid' => $_POST['id']]); break; case empty($control): Application::unsetCurrentByKey('pageid'); break; default: break; } } /************** call Controller's Method ******************************/ $ctrl = $control ?: 'show'; $ctrlClassName = 'App\\Controllers\\' . ucfirst($ctrl); $act = $action ?: 'all'; $method = 'action' . ucfirst($act); try { $controller = new $ctrlClassName();