| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- /**
- * @author Open Mind
- * @date 09/04/2018
- * @time 16:15
- */
- namespace OpenMind\Web;
- class BaseController
- {
- protected $dbHandle;
- protected $userId;
- private $sanitizedPostRequest;
- /**
- * @return mixed
- */
- public function getSanitizedPostRequest()
- {
- return $this->sanitizedPostRequest;
- }
- public function __construct($f3)
- {
- $this->sanitizedPostRequest = $f3->clean($f3->get('POST'));
- $this->dbHandle = $f3->get('DB');
- $this->userId = $this->checkLogin($f3->get('COOKIE.username'), $f3->get('COOKIE.password'));
- $f3->set('USERID', $this->userId);
- if (!is_null($this->userId))
- return;
- $f3->clear('COOKIE.username');
- $f3->clear('COOKIE.password');
- $f3->clear('COOKIE.id');
- if (!in_array($f3->get('PATTERN'), ['/login', '/']))
- $f3->reroute('/');
- return;
- }
- /**
- * Check if login is valid
- * @param $username
- * @param $password
- * @return int|null
- */
- private function checkLogin($username, $password): ?int
- {
- $accounts = \F3::get('users.PASSWORD');
- foreach($accounts as $index => $account) {
- $exist = password_verify("{$username}|{$password}", $account);
- if ($exist)
- return $index;
- }
- return null;
- }
- public function Login($f3)
- {
- $accountData = $this->getSanitizedPostRequest();
- $userId = $this->checkLogin($accountData['username'], $accountData['password']);
- if (is_null($userId)) {
- http_response_code(500);
- echo "Dati di accesso non corretti";
- return;
- }
- $f3->set('COOKIE.username', $accountData['username']);
- $f3->set('COOKIE.password', $accountData['password']);
- $f3->set('COOKIE.id', $userId);
- //http_response_code(200);
- $f3->reroute('/homemenu');
- }
- public function Logout($f3)
- {
- $f3->clear('COOKIE.username');
- $f3->clear('COOKIE.password');
- $f3->clear('COOKIE.id');
- $f3->reroute('/');
- }
- }
|