Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require $_SERVER['DOCUMENT_ROOT'] . "/LibrarySystem/API.php";
- require $_SERVER['DOCUMENT_ROOT'] . "/LibrarySystem/PHPMailer/src/PHPMailer.php";
- require $_SERVER['DOCUMENT_ROOT'] . "/LibrarySystem/PHPMailer/src/Exception.php";
- require $_SERVER['DOCUMENT_ROOT'] . "/LibrarySystem/PHPMailer/src/SMTP.php";
- class User {
- // Class variables
- private int $id;
- private string $mailAddress;
- private string $password;
- private string $studentId;
- private int $type;
- private string $profilePicture;
- protected $API;
- protected $Mail;
- // Constructor method
- public function __construct() {
- $this->API = new API();
- $this->Mail = new PHPMailer\PHPMailer\PHPMailer(true);
- }
- // ID accessor method
- public function getId() {
- return $this->id;
- }
- // Mail Address accessor method
- public function getMailAddress() {
- return $this->mailAddress;
- }
- // Password accessor method
- public function getPassword() {
- return $this->password;
- }
- // Student Id accessor method
- public function getStudentId() {
- return $this->studentId;
- }
- // Type accessor method
- public function getType() {
- return $this->type;
- }
- // Profile Picture accessor method
- public function getProfilePicture() {
- return $this->profilePicture;
- }
- // ID mutator method
- public function setId($id) {
- $this->id = $id;
- }
- // Mail Address mutator method
- public function setMailAddress($mailAddress) {
- $this->mailAddress = $mailAddress;
- }
- // Password mutator method
- public function setPassword($password) {
- $this->password = $password;
- }
- // Student ID mutator method
- public function setStudentID($studentId) {
- $this->studentId = $studentId;
- }
- // Type mutator method
- public function setType($type) {
- $this->type = $type;
- }
- // Profile Picture mutator method
- public function setProfilePicture($profilePicture) {
- $this->profilePicture = $profilePicture;
- }
- // Register method
- public function register() {
- // Setting the mail address from the register page as a parameter in the mutator.
- $this->setMailAddress($_POST['mailAddress']);
- // Setting the student ID from the register page as a parameter in the mutator.
- $this->setStudentId($_POST['studentId']);
- // Preparing the query to verify if the mail entered is already in the database.
- $this->API->query("SELECT * FROM LibrarySystem.User WHERE UserMailAddress = :UserMailAddress");
- // Binding the value returned by the User class for security purposes.
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- // Executing the query.
- $this->API->execute();
- // Verifying whether the result set is 0. If, it is 0, then, another if-statement will verify if the mail entered belongs to Université Des Mascareignes. In the condition that the mail belongs to UDM, another if-statement will verify if there is a student Id which has been entered. In the condition that a student ID has been entered, the account type will be set to 1 else, it will be set to 2 where afterwards it can be changed by the administrator.
- if (count($this->API->resultSet()) == 0) {
- if (strpos($this->getMailAddress(), "@student.udm.ac.mu") == true or strpos($this->getMailAddress(), "@udm.ac.mu") == true) {
- if (!empty($this->getStudentId())) {
- $this->setType(1);
- $this->setPassword($this->generatePassword());
- $this->API->query("INSERT INTO LibrarySystem.User (UserMailAddress, UserPassword, UserStudentId, UserType) VALUES (:UserMailAddress, :UserPassword, :UserStudentId, :UserType)");
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- $this->API->bind(":UserPassword", $this->getPassword());
- $this->API->bind(":UserStudentId", $this->getStudentId());
- $this->API->bind(":UserType", $this->getType());
- $this->API->execute();
- $this->Mail->IsSMTP();
- $this->Mail->CharSet = "UTF-8";
- $this->Mail->Host = "ssl://smtp.gmail.com";
- $this->Mail->SMTPDebug = 0;
- $this->Mail->Port = 465;
- $this->Mail->SMTPSecure = 'ssl';
- $this->Mail->SMTPAuth = true;
- $this->Mail->IsHTML(true);$this->Mail->Username = "";
- $this->Mail->Password = "";
- $this->Mail->setFrom($this->Mail->Username);
- $this->Mail->addAddress($this->getMailAddress());
- $this->Mail->subject = "Library System: Notification";
- $this->Mail->Body = "Your password is " . $this->getPassword() . ". Please consider to change your password after logging in!";
- $this->Mail->send();
- echo("<h1 id='success'>You have been registered into the system, you will be redirected to the login page.</h1>");
- header('refresh: 4; url = http://stormysystem.ddns.net/LibrarySystem/Login');
- } else {
- $this->setType(2);
- $this->setPassword($this->generatePassword());
- $this->API->query("INSERT INTO LibrarySystem.User (UserMailAddress, UserPassword, UserType) VALUES (:UserMailAddress, :UserPassword, :UserType)");
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- $this->API->bind(":UserPassword", $this->getPassword());
- $this->API->bind(":UserType", $this->getType());
- $this->API->execute();
- $this->Mail->IsSMTP();$this->Mail->CharSet = "UTF-8";
- $this->Mail->Host = "ssl://smtp.gmail.com";
- $this->Mail->SMTPDebug = 0;
- $this->Mail->Port = 465;
- $this->Mail->SMTPSecure = 'ssl';
- $this->Mail->SMTPAuth = true;
- $this->Mail->IsHTML(true);$this->Mail->Username = "";
- $this->Mail->Password = "";
- $this->Mail->setFrom($this->Mail->Username);
- $this->Mail->addAddress($this->getMailAddress());
- $this->Mail->subject = "Library System: Notification";
- $this->Mail->Body = "Your password is " . $this->getPassword() . ". Please consider to change your password after logging in!";
- $this->Mail->send();
- echo("<h1 id='success'>You have been registered into the system, you will be redirected to the login page.</h1>");
- header('refresh: 4; url = http://stormysystem.ddns.net/LibrarySystem/Login');
- }
- } else {
- echo("<h1 id='failure'>You cannot have access to this service as you are not a member of this organization!</h1>");
- }
- } else {
- echo "<h1 id='failure'>You already have an account on the system! You will be redirected to the login page!</h1>";
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Login');
- }
- }
- // Login method
- public function login() {
- // Setting the mail address from the login page as a parameter in the mutator.
- $this->setMailAddress($_POST['mailAddress']);
- // Setting the password from the login page as a parameter in the mutator.
- $this->setPassword($_POST['password']);
- // Preparing the query to verify if the mail and password entered are already in the database.
- $this->API->query("SELECT * FROM LibrarySystem.User WHERE UserMailAddress = :UserMailAddress AND UserPassword = :UserPassword");
- // Binding the values returned by the User class for security purposes.
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- $this->API->bind(":UserPassword", $this->getPassword());
- // Executing the query.
- $this->API->execute();
- // Verifying if, the results from the database is 0 and in the case that it is 0, the user will be redirected to the homepage, else, another if-statement will be called where it will verify whether there is a profile picture or a student ID given that in the end, Check Session method will be called.
- if (empty($this->API->resultSet())) {
- echo "<h1 id='failure'>Incorrect Credentials!</h1>";
- header('refresh:1.2; url=http://stormysystem.ddns.net/LibrarySystem/Login');
- } else {
- if ($this->API->resultSet()[0]['UserProfilePicture'] != null) {
- if ($this->API->resultSet()[0]['UserStudentId'] != null) {
- $this->setId($this->API->resultSet()[0]['UserId']);
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setStudentId($this->API->resultSet()[0]['UserStudentId']);
- $this->setProfilePicture($this->API->resultSet()[0]['UserProfilePicture']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- session_start();
- $_SESSION["id"] = $this->getId();
- $this->checkSession();
- } else {
- $this->setId($this->API->resultSet()[0]['UserId']);
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setProfilePicture($this->API->resultSet()[0]['UserProfilePicture']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- session_start();
- $_SESSION["id"] = $this->getId();
- $this->checkSession();
- }
- } else {
- if ($this->API->resultSet()[0]['UserStudentId'] != null) {
- $this->setId($this->API->resultSet()[0]['UserId']);
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setStudentId($this->API->resultSet()[0]['UserStudentId']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- session_start();
- $_SESSION["id"] = $this->getId();
- $this->checkSession();
- } else {
- $this->setId($this->API->resultSet()[0]['UserId']);
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- session_start();
- $_SESSION["id"] = $this->getId();
- $this->checkSession();
- }
- }
- }
- }
- // Check Session method
- public function checkSession() {
- // Verifying if, the session ID is the same as the ID of the user and if, it is the case, another switch-statement will verify will check the account type so that the the system will redirect the user to its designated page.
- if ($_SESSION["id"] = $this->getId()) {
- switch ($this->getType()) {
- case '0':
- echo("<h1 id='failure'>You cannot have access to this service as you are currently banned from this service! A mail will be sent to you!</h1>");
- $this->Mail->IsSMTP();$this->Mail->CharSet = "UTF-8";
- $this->Mail->Host = "ssl://smtp.gmail.com";
- $this->Mail->SMTPDebug = 0;
- $this->Mail->Port = 465;
- $this->Mail->SMTPSecure = 'ssl';
- $this->Mail->SMTPAuth = true;
- $this->Mail->IsHTML(true);$this->Mail->Username = "";
- $this->Mail->Password = "";
- $this->Mail->setFrom($this->Mail->Username);
- $this->Mail->addAddress($this->getMailAddress());
- $this->Mail->subject = "Library System: Notification";
- $this->Mail->Body = "You are currently banned from the system! Before, you can actually get accessed to the system once again, you will have to get it unban by contacting an administrator.";
- $this->Mail->send();
- header('refresh:4.4; url=http://stormysystem.ddns.net/LibrarySystem');
- break;
- case '1':
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Member');
- break;
- case '2':
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Member');
- break;
- case '3':
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Member');
- break;
- case '4':
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Admin');
- break;
- default:
- echo("<h1 id='failure'>You cannot have access to this service as you are not a member of this organization!</h1>");
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem');
- break;
- }
- } else {
- echo("<h1 id='failure'>You cannot have access to this service as you are not a member of this organization!</h1>");
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem');
- }
- }
- // Generate Password method
- public function generatePassword() {
- return uniqid();
- }
- // Forgot Password method
- public function forgotPassword() {
- // Setting the mail address entered from Reset_Password page as the parameter for the mutator.
- $this->setMailAddress($_POST['mailAddress']);
- // Preparing the query which will fetch data from the database.
- $this->API->query("SELECT * FROM LibrarySystem.User WHERE UserMailAddress = :UserMailAddress");
- // Binding the mail address which is returned from the accessor to prevent any SQL injection in the database.
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- // Executing the query.
- $this->API->execute();
- // If, the query does not return any value, the user will be redirected to the homepage, else, a mail will be sent to him with a new password and he will be redirected to the login page afterwards.
- if (empty($this->API->resultSet())) {
- echo"<h1 id='failure'>This mail is not registered in the system!</h1>";
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem');
- } else {
- $this->setPassword($this->generatePassword());
- $this->API->query("UPDATE LibrarySystem.User SET UserPassword = :UserPassword WHERE UserMailAddress = :UserMailAddress");
- $this->API->bind(":UserMailAddress", $this->getMailAddress());
- $this->API->bind(":UserPassword", $this->getPassword());
- $this->API->execute();
- $this->Mail->IsSMTP();
- $this->Mail->CharSet = "UTF-8";
- $this->Mail->Host = "ssl://smtp.gmail.com";
- $this->Mail->SMTPDebug = 0;
- $this->Mail->Port = 465;
- $this->Mail->SMTPSecure = 'ssl';
- $this->Mail->SMTPAuth = true;
- $this->Mail->IsHTML(true);
- $this->Mail->Username = "";
- $this->Mail->Password = "";
- $this->Mail->setFrom($this->Mail->Username);
- $this->Mail->addAddress($this->getMailAddress());
- $this->Mail->subject = "Library System: Notification";
- $this->Mail->Body = "Your password has been resetted. Your new password is " . $this->getPassword() . ".";
- $this->Mail->send();
- echo "<h1 id='success'>Your password have been resetted, you will be redirected to the login page.</h1>";
- header('refresh:4; url=http://stormysystem.ddns.net/LibrarySystem/Login');
- }
- }
- // Change Password method
- public function changePassword() {
- $this->API->query("SELECT * FROM LibrarySystem.User WHERE UserId = :UserId");
- $this->API->bind(":UserId", $this->getId());
- $this->API->execute();
- if ($_POST['oldPassword'] == $this->API->resultSet()[0]['UserPassword']) {
- if ($_POST['newPassword'] == $_POST['confirmNewPassword']) {
- $this->setPassword($_POST['newPassword']);
- $this->API->query("UPDATE User SET UserPassword = :UserPassword WHERE UserId = :UserId");
- $this->API->bind(":UserId", $this->getId());
- $this->API->bind(":UserPassword", $this->getPassword());
- $this->API->execute();$this->Mail->IsSMTP();
- $this->Mail->CharSet = "UTF-8";
- $this->Mail->Host = "ssl://smtp.gmail.com";
- $this->Mail->SMTPDebug = 0;
- $this->Mail->Port = 465;
- $this->Mail->SMTPSecure = 'ssl';
- $this->Mail->SMTPAuth = true;
- $this->Mail->IsHTML(true);
- $this->Mail->Username = "";
- $this->Mail->Password = "";
- $this->Mail->setFrom($this->Mail->Username);
- $this->Mail->addAddress($this->getMailAddress());
- $this->Mail->subject = "Library System: Notification";
- $this->Mail->Body = "Your password has been changed. Your new password is " . $this->getPassword() . ". If, you are not the one, please consider to reset your password on this link http://stormysystem.ddns.net/LibrarySystem/Reset_Password";
- $this->Mail->send();
- echo "<h1 id='success'>Your password has been successfully been changed. You will be logged out of the system and your new password will be sent to you by mail.</h1>";
- header('refresh:4.4; url=http://stormysystem.ddns.net/LibrarySystem/Member/Logout');
- } else {
- echo "<h1 id='failure'>The passwords entered, are not identical!</h1>";
- }
- } else {
- echo "<h1 id='failure'>This is not your password! You will be logged out of this account!</h1>";
- header('refresh:0.2; url=http://stormysystem.ddns.net/LibrarySystem/Member/Logout');
- }
- }
- // Change Profile Picture method
- public function changeProfilePicture() {
- $imageDirectory = "/LibrarySystem/Images/";
- $imageFile = $imageDirectory . basename($_FILES['image']['name']);
- $uploadedPath = $_SERVER["DOCUMENT_ROOT"] . $imageFile;
- if (move_uploaded_file($_FILES["image"]["tmp_name"], $uploadedPath)) {
- $this->setProfilePicture($imageFile);
- $this->API->query("UPDATE User SET UserProfilePicture = :UserProfilePicture WHERE UserId = :UserId");
- $this->API->bind(":UserProfilePicture", $this->getProfilePicture());
- $this->API->bind(":UserId", $this->getId());
- $this->API->execute();
- echo "<h1 id='success'>Your profile picture has been changed!</h1>";
- }
- }
- // Freeze Membership method
- public function freezeMembership() {
- $this->setType(0);
- $this->API->query("UPDATE User SET UserType = :UserType WHERE UserMail = :UserMail");
- $this->API->bind(":UserType", $this->getType());
- $this->API->bind(":UserMail", $this->getMailAddress());
- $this->API->execute();
- echo($this->getMail() . " has been banned!");
- }
- // Unfreeze Membership method
- public function unfreezeMembership() {
- if (strpos($this->getMailAddress(), "@student.udm.ac.mu") or strpos($this->getMail(), "@udm.ac.mu")) {
- if (!empty($this->getStudentId())) {
- $this->setType(1);
- $this->API->query("UPDATE User SET UserType = :UserType WHERE UserMail = :UserMail");
- $this->API->bind(":UserType", $this->getType());
- $this->API->bind(":UserMail", $this->getMailAddress());
- $this->API->execute();
- echo($this->getMail() . " has been unbanned!");
- } else {
- $this->setType(2);
- $this->API->query("UPDATE User SET UserType = :UserType WHERE UserMail = :UserMail");
- $this->API->bind(":UserType", $this->getType());
- $this->API->bind(":UserMail", $this->getMailAddress());
- $this->API->execute();
- echo($this->getMail() . " has been unbanned!");
- }
- } else {
- echo("This is not a member of this organization!");
- }
- }
- // Profile Checker method
- public function profileChecker() {
- // Setting the Session ID as a parameter in the User ID which will be used to verify if it exists in the database.
- $this->setId($_SESSION['id']);
- // Preparing the query.
- $this->API->query("SELECT * FROM LibrarySystem.User WHERE UserId = :UserId");
- // Binding the values which will be used in the query to prevent a sql injection.
- $this->API->bind(":UserId", $this->getId());
- // Executing the query
- $this->API->execute();
- // It will verify if there is a profile picture which is related to the User searched, when another if-statement will verify if the User has a student ID.
- if ($this->API->resultSet()[0]['UserProfilePicture'] != null) {
- if ($this->API->resultSet()[0]['UserStudentId'] != null) {
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setStudentId($this->API->resultSet()[0]['UserStudentId']);
- $this->setProfilePicture($this->API->resultSet()[0]['UserProfilePicture']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- } else {
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setProfilePicture($this->API->resultSet()[0]['UserProfilePicture']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- }
- } else {
- if ($this->API->resultSet()[0]['UserStudentId'] != null) {
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setStudentId($this->API->resultSet()[0]['UserStudentId']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- } else {
- $this->setMailAddress($this->API->resultSet()[0]['UserMailAddress']);
- $this->setPassword($this->API->resultSet()[0]['UserPassword']);
- $this->setType($this->API->resultSet()[0]['UserType']);
- }
- }
- }
- // Profile Icon method
- public function profileIcon() {
- // Calling Profile Checker method
- $this->profileChecker();
- // The statement will verify there is a profile picture which is related to the user and if, it is the case, it will fetch the url of that picture, else, it will fetch an .svg file from a script.
- if ($this->API->resultSet()[0]['UserProfilePicture'] != null) {
- $pp = "http://stormysystem.ddns.net" . $this->getProfilePicture();
- echo "<img src='{$pp}' />";
- } else {
- echo "<i class='fa fa-user faProfileCustom faProfileCustom1'></i>";
- }
- }
- // Profile Mail method
- public function profileMail() {
- // Calling Profile Checker method
- $this->profileChecker();
- // Creating a h1 tag which will be rendered given that User.getMailAddress() is called.
- $h1WelcomeText = "<h1>Hello, {$this->getMailAddress()}</h1>";
- echo $h1WelcomeText;
- }
- // Profile Type Checker method
- public function profileTypeChecker() {
- // This statement will check for the value to print the type into a string given that it is saved in the system as an integer.
- switch ($this->getType()) {
- case 1:
- echo "Student";
- break;
- case 2:
- echo "Non-Academical Staff";
- break;
- case 3:
- echo "Academical Staff";
- break;
- case 4:
- echo "Administrator";
- break;
- default:
- echo "<h1 id='detail'>Banned</h1>";
- break;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement