diff --git a/app/controllers/Users.php b/app/controllers/Users.php index c9042d29..560679f0 100644 --- a/app/controllers/Users.php +++ b/app/controllers/Users.php @@ -14,8 +14,10 @@ public function register(){ 'name' => trim($_POST['name']), 'email' => trim($_POST['email']), 'password' => trim($_POST['password']), + 'username' => trim($_POST['username']), 'confirm_password' => trim($_POST['confirm_password']), 'user_type' => trim($_POST['user_type']), + 'contact_no' => trim($_POST['contact_no']), 'err' => '' ]; @@ -35,6 +37,16 @@ public function register(){ } } + // Validate username + if (empty($data['username'])){ + $data['err'] = 'Please enter username'; + } else { + // Check email + if ($this->userModel->findUserByUsername($data['username'])){ + $data['err'] = 'Username is already taken'; + } + } + // Validate password if (empty($data['password'])){ $data['err'] = 'Please enter password'; @@ -56,6 +68,11 @@ public function register(){ $data['err'] = 'Please select user type'; } + // Validate contact number + if (empty($data['contact_no'])){ + $data['err'] = 'Please enter contact number'; + } + // Validation is completed and no error found if (empty($data['err'])){ // Hash password @@ -77,9 +94,11 @@ public function register(){ $data = [ 'name' => '', 'email' => '', + 'username' => '', 'password' => '', 'confirm_password' => '', 'user_type' => '', + 'contact_no' => '', 'err' => '', ]; @@ -97,6 +116,7 @@ public function login(){ // Input data $data = [ 'email' => trim($_POST['email']), + 'username' => trim($_POST['email']), 'password' => trim($_POST['password']), 'remember_me' => isset($_POST['remember_me']), 'err' => '' @@ -108,7 +128,7 @@ public function login(){ $data['err'] = 'Please enter email'; } else{ - if ($this->userModel->findUserByEmail($data['email'])){ + if ($this->userModel->findUserByEmail($data['email']) or $this->userModel->findUserByUsername($data['username'])){ // User found } else{ @@ -125,7 +145,7 @@ public function login(){ // Check if error is empty if (empty($data['err'])){ // log the user - $loggedInUser = $this->userModel->login($data['email'], $data['password']); + $loggedInUser = $this->userModel->login($data['email'], $data['password'], $data['username']); if ($loggedInUser){ // Create session $this->createUserSession($loggedInUser); @@ -151,6 +171,7 @@ public function login(){ // Initial form load $data = [ 'email' => '', + 'username' => '', 'password' => '', 'err' => '' ]; @@ -176,6 +197,7 @@ private function setRememberMeCookie($userId) { public function createUserSession($user){ $_SESSION['user_id'] = $user->id; $_SESSION['user_email'] = $user->email; + $_SESSION['username'] = $user->username; $_SESSION['user_name'] = $user->name; $_SESSION['user_type'] = $user->userType; @@ -186,6 +208,7 @@ public function createUserSession($user){ public function logout(){ unset($_SESSION['user_id']); unset($_SESSION['user_email']); + unset($_SESSION['username']); unset($_SESSION['user_name']); unset($_SESSION['user_type']); diff --git a/app/models/DriverModel.php b/app/models/DriverModel.php index e383ab1b..ec8ce9a2 100644 --- a/app/models/DriverModel.php +++ b/app/models/DriverModel.php @@ -6,9 +6,73 @@ public function __construct(){ $this->db = new Database(); } - public function getUser(){ - $this->db->query('SELECT * FROM users'); + // Register user + public function register($data): bool + { + // Prepare statement + $this->db->query('INSERT INTO user (name, username, email, password, userType, contactNo) VALUES (:name, :username, :email, :password, :userType, :contactNo)'); - return $this->db->resultSet(); + // Bind values + $this->db->bind(':name', $data['name']); + $this->db->bind(':email', $data['email']); + $this->db->bind(':username', $data['username']); + $this->db->bind(':password', $data['password']); + $this->db->bind(':userType', $data['user_type']); + $this->db->bind(':contactNo', $data['contact_no']); + + // Execute + if ($this->db->execute()){ + return true; + } + else { + return false; + } + } + + // Find user + public function findUserByEmail($email): bool + { + $this->db->query('SELECT * FROM user WHERE email = :email'); + $this->db->bind(':email', $email); + + $row = $this->db->single(); + + // Check row + if ($this->db->rowCount() > 0){ + return true; + } else { + return false; + } + } + + public function findUserByUsername($username): bool + { + $this->db->query('SELECT * FROM user WHERE username = :username'); + $this->db->bind(':username', $username); + + $row = $this->db->single(); + + // Check row + if ($this->db->rowCount() > 0){ + return true; + } else { + return false; + } + } + + // Login user + public function login($email, $password, $username){ + $this->db->query('SELECT * FROM user WHERE email = :email OR username = :username'); + $this->db->bind(':email', $email); + $this->db->bind(':username', $username); + + $row = $this->db->single(); + + $hashed_password = $row->password; + if (password_verify($password, $hashed_password)){ + return $row; + } else { + return false; + } } } \ No newline at end of file diff --git a/app/models/UserModel.php b/app/models/UserModel.php index bc5df821..8fa1f8a2 100644 --- a/app/models/UserModel.php +++ b/app/models/UserModel.php @@ -7,15 +7,18 @@ public function __construct(){ } // Register user - public function register($data){ + public function register($data): bool + { // Prepare statement - $this->db->query('INSERT INTO users (name, email, password, userType) VALUES (:name, :email, :password, :userType)'); + $this->db->query('INSERT INTO user (name, username, email, password, userType, contactNo) VALUES (:name, :username, :email, :password, :userType, :contactNo)'); // Bind values $this->db->bind(':name', $data['name']); $this->db->bind(':email', $data['email']); + $this->db->bind(':username', $data['username']); $this->db->bind(':password', $data['password']); $this->db->bind(':userType', $data['user_type']); + $this->db->bind(':contactNo', $data['contact_no']); // Execute if ($this->db->execute()){ @@ -27,8 +30,9 @@ public function register($data){ } // Find user - public function findUserByEmail($email){ - $this->db->query('SELECT * FROM users WHERE email = :email'); + public function findUserByEmail($email): bool + { + $this->db->query('SELECT * FROM user WHERE email = :email'); $this->db->bind(':email', $email); $row = $this->db->single(); @@ -41,10 +45,26 @@ public function findUserByEmail($email){ } } + public function findUserByUsername($username): bool + { + $this->db->query('SELECT * FROM user WHERE username = :username'); + $this->db->bind(':username', $username); + + $row = $this->db->single(); + + // Check row + if ($this->db->rowCount() > 0){ + return true; + } else { + return false; + } + } + // Login user - public function login($email, $password){ - $this->db->query('SELECT * FROM users WHERE email = :email'); + public function login($email, $password, $username){ + $this->db->query('SELECT * FROM user WHERE email = :email OR username = :username'); $this->db->bind(':email', $email); + $this->db->bind(':username', $username); $row = $this->db->single(); diff --git a/app/views/driver/vehicles.php b/app/views/driver/vehicles.php new file mode 100644 index 00000000..b6fdc306 --- /dev/null +++ b/app/views/driver/vehicles.php @@ -0,0 +1,77 @@ +isSMTP(); + +// Define smtp host +$mail->Host = 'smtp.gmail.com'; + +// Enable smtp authentication +$mail->SMTPAuth = true; + +// Set smtp encryption type (ssl/tls) +$mail->SMTPSecure = 'tls'; + +// Port to connect smtp +$mail->Port = '587'; + +// Set gmail username +$mail->Username = 'dasunthathsara974@gmail.com'; + +// Set gmail password +$mail->Password = 'Dasun@974'; + +// Email subject +$mail->Subject = 'Test email from Localhost by Dasun'; + +// Set sender email +try { + $mail->setFrom('dasun.thathsara.sri@gmail.com'); +} catch (Exception $e) { + print_r(e); +} + +// Email body +$mail->Body = 'Hello Dasun'; + +// Add recipient +try { + $mail->addAddress('dasun.thathsara.sri@gmail.com'); +} catch (Exception $e) { + print_r(e); +} + +// Send email +try { + $mail->Send(); +} catch (Exception $e) { + print_r(e); +} + +// Close smtp connection +$mail->smtpClose(); +?> + + + + + + + + + +