/** * Checks if valid registration details have been submitted. * @throws Exception If an error occurs with registration. */ public function checkValidRegistration() { if (empty($_POST['username']) && empty($_POST['password']) && empty($_POST['password2'])) { /* hasn't submitted any information */ return; } $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING); $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING); $password2 = filter_var($_POST['password2'], FILTER_SANITIZE_STRING); $this->model->setUsername($username); if ($username === false) { $this->model->setError('Invalid username.'); } else { if (strlen($username) < 3) { $this->model->setError('Username requires 3 characters.'); } else { if (strlen($username) > 50) { $this->model->setError('Username exceeds 50 characters.'); } else { if ($password === false) { $this->model->setError('Invalid password.'); } else { if (strlen($password) < 3) { $this->model->setError('Password requires 3 characters.'); } else { if (strlen($password) > 50) { $this->model->setError('Password exceeds 50 characters.'); } else { if ($password !== $password2) { $this->model->setError('Passwords do not match.'); } else { $database = new DatabaseWrapper(); $database->connect(DB_NAME); try { /* if the queryUser method executes without any errors then it means an account does exist */ $database->queryUser($username); throw new AlreadyRegisteredException(); } catch (AlreadyRegisteredException $e) { throw $e; // throw the error upwards for it to be handled correctly } catch (Exception $e) { /* error thrown as the statement failed to find an account */ } $database->addAccount(new UserAccount($username, password_hash($password, PASSWORD_BCRYPT), 'USER')); $this->model->registered(); } } } } } } } }
/** * Fetches the statuses of the circuit boards from the database. * @throws Exception If a query fails to execute. */ public function fetchStatuses() { $database = new DatabaseWrapper(); $database->connect(DB_NAME); $boards = $database->queryAllBoardInformation(); foreach ($boards as $board) { $msisdn = $board->getMSISDN(); $status = null; try { $status = $database->queryBoardStatus($msisdn); } catch (Exception $e) { /* failed to find its status */ } $this->model->addBoard(new CircuitBoard($board, $status)); } }
/** * Fetches the statuses of the circuit boards from the database. * @throws Exception If a query fails to execute. */ public function fetchUpdates() { $soap = new SoapClientWrapper(); $messages = $soap->getNewMessages(); $database = new DatabaseWrapper(); $database->connect(DB_NAME); foreach ($messages as $message) { $xmlParser = new XMLParser($message); $xmlParser->parse(); $parsedMessage = $xmlParser->getParsedData(); $validator = new SMSValidator($parsedMessage); try { $msisdn = $validator->validateMSISDN(); $status = $validator->validateStatus(); $information = $database->queryBoardInformation($msisdn); $update = new CircuitBoard($information, $status); $database->updateBoardStatus($msisdn, $status); $this->model->addUpdate($update); } catch (Exception $e) { continue; } } }
/** * Checks for valid login details. * @throws Exception If there was an error with the login procedure. */ public function checkValidLogin() { if (empty($_POST['username']) && empty($_POST['password'])) { /* hasn't submitted any information */ return; } $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING); $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING); $database = new DatabaseWrapper(); $database->connect(DB_NAME); $account = null; try { $account = $database->queryUser($username); } catch (Exception $e) { throw new LoginDetailsException(); } $passwordHash = $account->getPasswordHash(); if (!password_verify($password, $passwordHash)) { throw new LoginDetailsException(); } $_SESSION['username'] = $account->getUsername(); $_SESSION['rank'] = $account->getRank(); $this->model->loggedIn(); }
function setUp() { $this->db = new DatabaseWrapper(); $this->db->connect(TEST_DB_NAME); }