Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************* PAGE.CLASS.PHP ************************************************************************/
- <?php
- abstract class Page {
- protected $dbCon = null;
- protected function __construct() {
- $this->dbCon = mysqli_connect("127.0.0.1", "root", "", "chat") or die("Error " . mysqli_error($this->dbCon));
- }
- protected function __destruct() {
- mysqli_close($this->dbCon);
- $this->dbCon = null;
- }
- protected function generateHeader($title = "Standard Title - localhost") {
- echo <<< HTML
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
- <link rel="stylesheet" href="css/style.css">
- <title>$title</title>
- </head>
- <body>
- <script src="js/functions.js"></script>
- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
- <script src="js/cookie.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
- HTML;
- if(!isset($_COOKIE['cookieConsent']) || $_COOKIE['cookieConsent'] == false) {
- echo <<< HTML
- <div id="cookieBanner">
- <div class="row">
- <div class="col-md-6 col-md-offset-3">
- <div class="alert alert-info alert-dismissible" role="alert">
- <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
- This website is using Cookies to improve the user experience. If you do not allow cookies in your browser settings you will not be able to use this website.
- If you do not accept the website's cookie policy and usage, you may leave this website now and there will not be cookies saved yet. For further information
- you can use the buttons below.<br />
- <div id="cookieBannerActions">
- <a class="noconsent" href="ref=cookies"><button class="btn btn-sm btn-info">Learn about this website's cookies</button></a>
- <a class="denyConsent noconsent" href="#"><button class="btn btn-sm btn-default">Disallow cookies</button></a>
- </div>
- </div>
- </div>
- </div>
- HTML;
- }
- }
- protected function generateNav() {
- require_once 'html/nav.html';
- echo <<< HTML
- <div class="container">
- HTML;
- }
- protected function generateFooter() {
- echo <<< HTML
- </div>
- </body>
- </html>
- HTML;
- }
- }
- /************* FETCHPAGE.CLASS.PHP ************************************************************************/
- <?php
- // Includes
- require_once 'Page.class.php';
- require_once 'getIP.function.php';
- class Auth extends Page {
- /* This class is intended for the sole checks of whether there is a login and whether the login is on different rights (Moderator, Admin, etc.) */
- private $guestip = null;
- function __construct() {
- parent::__construct();
- if(!isset($_COOKIE['usrID'])) {
- $this->guestip = get_ip_address();
- setcookie("guestip", "$this->guestip", time() + 86400, "/"); // guest ip on visit saved for 1 day
- }
- }
- // to do: __destruct()
- function isAdmin() {
- $usrID = $_COOKIE['usrID'];
- $sqlCheckAdmin = mysqli_query($this->dbCon, "SELECT adminStatus FROM chatUsr WHERE usrID = '$usrID'");
- if($dataSql = mysqli_fetch_assoc($sqlCheckAdmin)) {
- var_dump($dataSql);
- }
- }
- function isLogged() {
- if(isset($_COOKIE['usrID']) && $_COOKIE['usrID'] != null) {
- return true;
- } else {
- return false;
- }
- }
- }
- class FetchPage extends Page {
- protected $pageContents = null;
- protected $objChatSession;
- public $objAuth = null;
- function __construct() {
- parent::__construct();
- $this->objAuth = new Auth();
- parent::generateHeader();
- parent::generateNav();
- var_dump($_COOKIE);
- if($this->load($_GET['ref']) === true) {
- include_once $_GET['ref'].".php";
- } else {
- echo "Failed loading page contents of function load()..<br />";
- }
- parent::generateFooter();
- }
- function __destruct() {
- // to do
- }
- function load($pageName) {
- /* @get ref set page, calls parent Page functions to generate, @return none */
- $pagePath = $pageName.".php";
- $pageFile = fopen($pagePath, "r") or die("Error opening the page file!\n");
- $this->pageContents = fread($pageFile, filesize($pagePath)) or die("Error reading the page contents!\n");
- if($this->pageContents != null) {
- return true;
- } else {
- return false;
- }
- }
- public static function main() {
- $page = new FetchPage();
- }
- }
- FetchPage::main();
- /************* CHATCOOKIE.CLASS.PHP ************************************************************************/
- <?php
- // Includes
- require_once 'Page.class.php';
- class ChatCookie extends Page {
- /* This class saves the active session in a database table */
- protected $cookieArray = Array();
- function __construct() {
- var_dump($_COOKIE);
- parent::__construct();
- setcookie("online", false, time() + 86400, "/");
- // @function read() checks if there was a session before for this fUsrID or not
- if(isset($_COOKIE['PHPSESSID'])) {
- //$this->readCookie();
- }
- $this->tmpCookieString = "";
- }
- function __destruct() {
- //
- }
- function deleteCookie() {
- // to do: query to update the timestampEnd in SQL
- $saved = false;
- $deleted = false;
- /* Save cookie data for later use */
- if($this->saveCookie()) {
- $saved = true;
- }
- /* Delete the Cookie and all data */
- foreach($_COOKIE as $key => $value) {
- if(is_array($value)) {
- // no arrays created yet
- }
- setcookie("$key", null, time() - 3600);
- unset($_COOKIE[$key]);
- }
- if(!isset($_COOKIE['usrID'])) {
- $deleted = true;
- }
- if($deleted == true && $saved == true) {
- $this->objChatCookie = new ChatCookie();
- return true;
- } else {
- return false;
- }
- var_dump($_COOKIE);
- }
- function readCookie() {
- $tmpCookieArray = Array();
- $cookieID = $_COOKIE['PHPSESSID'];
- $sqlRead = mysqli_query($this->dbCon, "SELECT * FROM chatCookies WHERE cookieID = '$cookieID'") or die("Error performing query #".mysqli_errno($sqlRead));
- while($row = mysqli_fetch_array($sqlRead)) {
- if(isset($row['cookieData']) && $row['cookieData'] != null) {
- $tmpCookieArray[] = $row;
- }
- }
- if(isset($tmpCookieArray)) {
- $this->cookieArray[] = $tmpCookieArray;
- return true;
- } else {
- return false;
- }
- }
- function saveCookie() {
- $tmpCookieString = "";
- $time = time();
- if(!isset($_COOKIE['usrID']) && $_COOKIE['usrID'] <= 0) {
- return false;
- } else {
- $cookieID = $_COOKIE['PHPSESSID'];
- $fUsrID = $_COOKIE['usrID'];
- foreach($_COOKIE as $key => $value) {
- if($key == "PHPSESSID" || $key == "usrID") continue;
- $tmpCookieString = $tmpCookieString.$key.":".$value.";";
- }
- if($this->readCookie()) {
- /* Cookie PHPSESSID has already been used and has to get updated, because its a primary key and can't be set twice */
- if(mysqli_query($this->dbCon, "UPDATE chatCookies SET cookieData = '$tmpCookieString' WHERE cookieID = '$cookieID'") or die("Error performing query UPDATE COOKIE in file ChatCookie.class.php on line ".__LINE__)) {
- return true;
- }
- } else {
- if(mysqli_query($this->dbCon, "INSERT INTO chatCookies (cookieID, fUsrID, timestampStart cookieData) VALUES ('$cookieID', '$fUsrID', '$time', '$tmpCookieString')") or die("Error performing query INSERT INTO COOKIE in file ChatCookie.class.php on line ".__LINE__)) {
- return true;
- }
- }
- }
- }
- }
- /************* LOGIN.CLASS.PHP ************************************************************************/
- <?php
- require_once 'Page.class.php';
- require_once 'ChatCookie.class.php';
- class Login extends Page {
- public $objChatCookie = null;
- function __construct() {
- var_dump($_COOKIE);
- parent::__construct();
- $this->objChatCookie = new ChatCookie();
- }
- function __destruct() {
- parent::__destruct();
- }
- protected function checkPwdHash($password, $oldHash) {
- /* Sample code from http://php.net/manual/de/function.password-needs-rehash.php - 18/03/2015 */
- $new = [
- 'options' => ['cost' => 11],
- 'algo' => PASSWORD_DEFAULT,
- 'hash' => null
- ];
- if (true === password_verify($password, $oldHash)) {
- if (true === password_needs_rehash($oldHash, $new['algo'], $new['options'])) {
- //rehash/store plain-text password using new hash
- return password_hash($password, $new['algo'], $new['options']);
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- protected function updateHash($newHash, $usrID) {
- $sqlQueryNewHash = mysqli_query($this->dbCon, "UPDATE chatUsr SET passwordHash = '$newHash' WHERE usrID = '$usrID'");
- if($sqlQueryNewHash) {
- return true;
- } else {
- return false;
- }
- }
- function checkUsrData($usrName, $usrPwd) {
- $sqlCheckUsr = mysqli_query($this->dbCon, "SELECT * FROM chatUsr WHERE eMail = '$usrName' OR userName = '$usrName' AND passwordHash = '$usrPwd' LIMIT 1");
- if($fetchSql = mysqli_fetch_assoc($sqlCheckUsr)) {
- $chkPwdHashVar = $this->checkPwdHash($usrPwd, $fetchSql['passwordHash']);
- if($chkPwdHashVar == true) {
- $this->setCookieData($fetchSql['usrID']);
- return true;
- } else if($chkPwdHashVar == false) {
- return false;
- } else {
- if($this->updateHash($chkPwdHashVar, $fetchSql['usrID'])) {
- $this->setCookieData(intval($fetchSql['usrID']));
- return true;
- } else {
- echo "Fatal error: Could not update your profile. Please contact website administrator!\nError occured at Login.class.php at line: ".__LINE__;
- }
- }
- }
- }
- function checkMail($email) {
- $sqlCheckMail = mysqli_query($this->dbCon, "SELECT eMail FROM chatUsr WHERE eMail = '$email' LIMIT 1");
- if($sqlCheckMail <= 0) {
- return true;
- } else {
- return false;
- }
- }
- protected function hashPwd($pwd) {
- return password_hash($pwd, PASSWORD_DEFAULT);
- }
- function addUsr($usrName, $plainPwd) {
- $pwdHash = $this->hashPwd($plainPwd);
- $sqlAddUsr = mysqli_query($this->dbCon, "INSERT INTO chatUsr (eMail, passwordHash) VALUES ('$usrName', '$pwdHash')");
- if($sqlAddUsr) {
- return true;
- } else {
- return false;
- }
- }
- protected function setCookieData($usrID) {
- /* Saving userdata into session variables to access them outside the class without initiating a class Object */
- $sqlGetUsrData = mysqli_query($this->dbCon, "SELECT * FROM chatUsr WHERE usrID = '$usrID' LIMIT 1");
- if($usrData = mysqli_fetch_assoc($sqlGetUsrData)) {
- setcookie("usrID", "$usrID", time() + (86400 * 30), "/"); // 86400s = 1 day -> 3 days
- }
- }
- function logoutUsr() {
- if($this->objChatCookie->deleteCookie()) {
- $this->objChatCookie = null;
- return true;
- } else {
- return false;
- }
- }
- }
- /************* LOGIN.PHP ************************************************************************/
- <?php
- if($this->objAuth->isLogged() == false) {
- require_once 'classes/Login.class.php';
- $login = new Login();
- if(count($_GET) === 1 && isset($_GET['ref'])) {
- // EOF
- include_once 'html/login.html';
- } else if(count($_GET) === 2 && isset($_GET['ref']) && isset($_GET['check']) && $_GET['check'] == true) {
- // Check the data login
- var_dump($_POST);
- if($login->checkUsrData($_POST['usr'], $_POST['pwd'])) {
- header("location: index.php?ref=chat");
- }
- } else if(count($_GET) === 2 && isset($_GET['ref']) && isset($_GET['proceed']) && $_GET['proceed'] == true) {
- }
- } else {
- include_once 'html/noview.html';
- }
- /************* LOGOUT.PHP ************************************************************************/
- <?php
- if($this->objAuth->isLogged() == true) {
- require_once 'classes/Login.class.php';
- $login = new Login();
- if($login->logoutUsr()) {
- echo '<div class="alert alert-success"><strong>Success!</strong> You logged out properly from the chat. All the data has been saved and is available to you when you log in next time!</div>';
- var_dump($_COOKIE);
- } else {
- echo '<div class="alert alert-danger"><strong>Oh snap!</strong> Something went completly wrong here. We have a <strong><em>false</em></strong> return.. that is harsh. Contact the website administrator please.</div>';
- }
- } else {
- echo '<div class="alert alert-info"><strong>Wait a moment..</strong> How about you first create an account, little fella?</div>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement