Skip to content

Commit

Permalink
template improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Razshal committed May 14, 2018
1 parent da32696 commit cc8c9f8
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 143 deletions.
5 changes: 5 additions & 0 deletions config/docker/reset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USE camagru;
DROP TABLE `like`;
DROP TABLE comment;
DROP TABLE post;
DROP TABLE user;
17 changes: 4 additions & 13 deletions config/setup.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php
if ($database !== NULL)
$success = $database->initiate();
if ($userManager !== NULL && $success = $userManager->initiate())
$info = $info . "<p class='success'>Website is ok</p><br>";
else
$success = false;
ob_start();
?>
<div>
<h2>Setup tried</h2>
<?php
if ($success === true)
$info = $info . "<p class='success'>Website is ok</p>";
?>
</div>
<?php $content = ob_get_clean(); ?>
$info = $info . "<p class='error'>Error during database init</p><br>";
$content = "";
15 changes: 8 additions & 7 deletions controller/password_reset.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php
if (isset($_POST) && isset($_POST["mail"]) && isset($_POST["submit"])
&& $_POST["submit"] === "Reset"
&& !empty($user = $database->get_mail($_POST["mail"])))
&& !empty($user = $userManager->get_mail($_POST["mail"])))
{
if ($database->initiatePasswordReset($_POST["mail"]))
$done = "<h2 class='success'>Reset account mail sent</h2>";
if ($userManager->initiatePasswordReset($_POST["mail"]))
$info = $info . "<h2 class='success'>Reset account mail sent</h2>";
else
$done = "<h2 class='error'>Unable to send reset mail,
$info = $info . "<h2 class='error'>Unable to send reset mail,
check if your mail is valid</h2>";
require ($_SERVER["DOCUMENT_ROOT"] . "/views/ask_password_reset.php");
}
else if (isset($_GET["token"]) && isset($_GET["mail"]))
{
if (!empty($user = $database->get_mail($_GET["mail"])[0])
&& $user["check_token"] === $_GET["token"])
echo $_GET["token"] . "<br>";
if (!empty($user = $userManager->get_mail($_GET["mail"]))
&& $user[0]["check_token"] === $_GET["token"])
{
var_dump($user["reset_date"]);
require ($_SERVER["DOCUMENT_ROOT"] . "/views/chose_new_password.php");
}
var_dump($user[0]["reset_date"]);
}
else
require ($_SERVER["DOCUMENT_ROOT"] . "/views/ask_password_reset.php");
10 changes: 5 additions & 5 deletions controller/signin.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
if ($database !== NULL && isset($_POST)
if ($userManager !== NULL && isset($_POST)
&& isset($_POST["submit"])
&& $_POST["submit"] === "Sign-in")
{
$validMail = $database->validNewMail($_POST["mail"]);
$validPass = $database->validNewPassword($_POST["password"]);
$validLogin = $database->validNewLogin($_POST["login"]);
$validMail = $userManager->validNewMail($_POST["mail"]);
$validPass = $userManager->validNewPassword($_POST["password"]);
$validLogin = $userManager->validNewLogin($_POST["login"]);
if ($validMail && $validPass && $validLogin)
{
$querySuccess = $database->newUser($_POST["login"],
$querySuccess = $userManager->newUser($_POST["login"],
$_POST["mail"], $_POST["password"]);
}
}
Expand Down
23 changes: 11 additions & 12 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
session_start();
require_once ("config/database.php");
require_once ("config/site.php");
require_once ("model/class_database.php");
require_once ("model/UserManager.php");

$title = "Camagru";
$content = "<h2>Welcome To Camagru</h2>";
$info = "";

try {
$database = new Database($DB_DSN, $DB_USER, $DB_PASSWORD, $SITE_ADDRESS);
$userManager = new UserManager($DB_DSN, $DB_USER, $DB_PASSWORD, $SITE_ADDRESS);
if (isset($_SESSION) && isset($_SESSION["user"])
&& $_SESSION["user"] != ""
&& $database != NULL && empty($database->get_user($_SESSION["user"])))
&& $userManager != NULL && empty($userManager->get_user($_SESSION["user"])))
$_SESSION["user"] = "";
} catch (Exception $e) {
$database = NULL;
$userManager = NULL;
$info = "<h1 class='error'>Fatal database error</h1><br>";
}

if ($database === NULL)
$info = $DB_ERROR;
}

/************* Router ************/

Expand All @@ -29,13 +28,13 @@
if (isset($_POST)
&& isset($_POST["submit"]) && $_POST["submit"] === "Login"
&& isset($_POST["login"]) && isset($_POST["password"])
&& ($auth = $database->authenticate(
&& ($auth = $userManager->authenticate(
$_POST["login"], $_POST["password"])))
$_SESSION["user"] = $_POST["login"];
require("controller/login.php");
}
else if ($_GET["action"] === "logout") {
if ($database !== NULL) {
if ($userManager !== NULL) {
if (isset($_SESSION)
&& isset($_SESSION["user"]) && $_SESSION["user"] != "")
$_SESSION["user"] = "";
Expand All @@ -52,10 +51,10 @@
else if ($_GET["action"] === "verify") {
$done = 0;
if (isset($_GET["user"]) && isset($_GET["token"])
&& $database->verify_user($_GET["user"], $_GET["token"]))
$info = $info . ("<h2 class='success'>Account activated</h2><br>");
&& $userManager->verify_user($_GET["user"], $_GET["token"]))
$info = $info . "<h2 class='success'>Account activated</h2><br>";
else
$info = $info . ("<h2 class='error'>Error wrong token/login</h2><br>");;
$info = $info . "<h2 class='error'>Error wrong token/login</h2><br>";
}
else if ($_GET["action"] === "reset") {
require("controller/password_reset.php");
Expand Down
85 changes: 85 additions & 0 deletions model/DatabaseManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

class DatabaseManager
{
protected $PDO = NULL;
protected $SITE_ADDRESS = NULL;

public function __construct($DB_DSN, $DB_USER, $DB_PASSWORD, $SITE_ADDRESS)
{
try
{
$this->PDO = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$this->SITE_ADDRESS = $SITE_ADDRESS;
}
catch (Exception $exception)
{
throw new Exception("Cannot connect to database");
}
}

public function initiate()
{
try
{
$this->PDO->exec("
CREATE TABLE IF NOT EXISTS user (
id INT NOT NULL AUTO_INCREMENT UNIQUE,
login VARCHAR(20) NOT NULL,
password VARCHAR(128),
mail VARCHAR(254),
check_token VARCHAR(128),
reset_token VARCHAR(128),
creation_date TIMESTAMP NOT NULL DEFAULT now(),
reset_date TIMESTAMP,
is_verified INT NOT NULL DEFAULT 0,
PRIMARY KEY (id))
ENGINE = InnoDB;");

$this->PDO->exec("
CREATE TABLE IF NOT EXISTS post (
id INT NOT NULL AUTO_INCREMENT UNIQUE,
user_id INT NOT NULL,
image VARCHAR(100),
description VARCHAR(256),
post_date TIMESTAMP NOT NULL DEFAULT now(),
PRIMARY KEY (id),
CONSTRAINT fk_user_id
FOREIGN KEY (user_id)
REFERENCES user (id))
ENGINE = InnoDB;");

$this->PDO->exec("
CREATE TABLE IF NOT EXISTS comment (
id INT NOT NULL AUTO_INCREMENT UNIQUE,
post_id INT NOT NULL,
`text` VARCHAR(256) NOT NULL,
comment_date TIMESTAMP NOT NULL DEFAULT now(),
PRIMARY KEY (id),
CONSTRAINT fk_post_id
FOREIGN KEY (post_id)
REFERENCES post (id))
ENGINE = InnoDB;");

$this->PDO->exec("
CREATE TABLE IF NOT EXISTS `like` (
post_id INT NOT NULL,
user_id INT NOT NULL,
CONSTRAINT fk_like_post_id FOREIGN KEY (post_id) REFERENCES post(id),
CONSTRAINT fk_like_user_id FOREIGN KEY (user_id) REFERENCES user(id))
ENGINE = InnoDB;
");
return true;
}
catch (Exception $e)
{
return false;
}
}

protected function generate_random_token()
{
return bin2hex(openssl_random_pseudo_bytes(16));
}
}
Loading

0 comments on commit cc8c9f8

Please sign in to comment.