Skip to content

Commit

Permalink
verification fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Razshal committed May 11, 2018
1 parent 5a8ee8b commit 811794f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 121 deletions.
2 changes: 1 addition & 1 deletion config/setup.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<? ob_start(); ?>
<?php ob_start(); ?>
<div>
<h2>Setup tried, Site status :</h2>
<?php
Expand Down
17 changes: 0 additions & 17 deletions controller/C_signin.php

This file was deleted.

14 changes: 0 additions & 14 deletions controller/C_verify.php

This file was deleted.

64 changes: 48 additions & 16 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,62 @@

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

if (isset($_GET["action"]) && $_GET["action"] === "login") {
if (isset($_GET) && isset($_GET["action"])) {
if ($_GET["action"] === "login")
{
if (isset($_POST)
&& isset($_POST["submit"]) && $_POST["submit"] === "Login"
&& isset($_POST["login"]) && isset($_POST["password"])
&& ($auth = $database->authenticate($_POST["login"], $_POST["password"])))
$_SESSION["user"] = $_POST["login"];
require ("views/login.php");
}
require ("views/login.php");
}

else if (isset($_GET["action"]) && $_GET["action"] === "logout") {
if ($database !== NULL) {
if (isset($_SESSION)
&& isset($_SESSION["user"]) && $_SESSION["user"] != "")
$_SESSION["user"] = "";
else if ($_GET["action"] === "logout")
{
if ($database !== NULL) {
if (isset($_SESSION)
&& isset($_SESSION["user"]) && $_SESSION["user"] != "")
$_SESSION["user"] = "";
}
require ("views/structure/template.php");
}
}

else if (isset($_GET["action"]) && $_GET["action"] === "setup") {
if ($database !== NULL)
$success = $database->initiate();
else
$success= false;
require ("config/setup.php");
}
else if ($_GET["action"] === "setup")
{
if ($database !== NULL)
$success = $database->initiate();
else
$success = false;
require ("config/setup.php");
}

else if ($_GET["action"] === "signin") {
if ($database !== NULL && isset($_POST)
&& isset($_POST["submit"]) && $_POST["submit"] === "Sign-in")
{
$validMail = validNewMail($database, $_POST["mail"]);
$validPass = validNewPassword($_POST["password"]);
$validLogin = validNewLogin($database, $_POST["login"]);
if ($validMail && $validPass && $validLogin)
{
$querySuccess = $database->newUser($_POST["login"],
$_POST["mail"], $_POST["password"]);
}
}
else if (isset($_POST["submit"]))
$querySuccess = false;
require ("views/signin.php");
}

else if ($_GET["action"] === "verify")
{
$done = 0;
if (isset($_GET) && isset($_GET["user"]) && isset($_GET["token"]))
$done = $database->verify_user($_GET["user"], $_GET["token"]);
else
$done = false;
require ("views/verify.php");
}
}
require ("views/structure/template.php");
34 changes: 21 additions & 13 deletions model/class_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function initiate() {
");
return true;
} catch (Exception $e) {
return $e;
return false;
}
}

Expand All @@ -85,7 +85,7 @@ private function hash_pw($pw) {
private function sendUserCheckMail($login, $mail, $token)
{
$token =
"http://{$this->SITE_ADDRESS}/views/verify.php" .
"http://{$this->SITE_ADDRESS}/index.php" .
"?action=verify&user={$login}&token={$token}";
$subject = 'Activate your Camagru account';
$message =
Expand All @@ -101,8 +101,8 @@ private function sendUserCheckMail($login, $mail, $token)
"<a style='color: whitesmoke' href=\"{$token}\">Validate account</a><br>" .
"Or access this page on a web browser<br>{$token}</div>";
$headers =
"From: noreply@{$this->SITE_ADDRESS}.com" . "\r\n" .
"Reply-To: noreply@{$this->SITE_ADDRESS}.com" . "\r\n" .
"From: noreply@{$this->SITE_ADDRESS}" . "\r\n" .
"Reply-To: noreply@{$this->SITE_ADDRESS}" . "\r\n" .
'X-Mailer: PHP/' . phpversion() .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Expand Down Expand Up @@ -138,16 +138,24 @@ public function newUser($login, $mail, $password)
}
}

public function verify_user ($login) {
if (validChars($login) && !empty($this->get_user($login))) {
$query = $this->PDO->prepare("
UPDATE user
SET is_verified = 1
WHERE login = :login");
$query->execute(array(':login' => $_GET["user"]));
return (!empty($query->fetchAll()));
public function verify_user ($login, $token) {
try {
if (validChars($login)
&& !empty($user = $this->get_user($login)[0])
&& !$user["is_verified"] == 1) {
$query = $this->PDO->prepare("
UPDATE user SET is_verified = 1
WHERE login = :login
AND check_token = :token");
$query->execute(array(
':login' => $login,
':token' => $token));
return ($query->rowCount() > 0);
} else if (isset($user) && $user["is_verified"] == 1)
return true;
} catch (Exception $e) {
return false;
}
return false;
}

public function get_user($login) {
Expand Down
6 changes: 3 additions & 3 deletions views/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
if (!isset($_SESSION) || !isset($_SESSION["user"])
|| $_SESSION["user"] === "") {
?>
<form class="loginForm" method="post" action="/index.php">
<form class="loginForm" method="post" action="/index.php?action=login">
<p>Login</p><br/>
<input type="text" placeholder="Login" title="login" name="login"><br/>
<input type="password" placeholder="Password" title="password" name="password"><br/>
<input class="submit" type="submit" title="send" name="submit" value="Login"><br/>
<a class="link" href="signin.php">Don't have an account ? Sign in</a>
<a class="link" href="password_reset.php">Forgot your password ? Reset password</a>
<a class="link" href="index.php?action=signin">Don't have an account ? Sign in</a>
<a class="link" href="index.php?action=reset">Forgot your password ? Reset password</a>
</form>
<?php
}
Expand Down
62 changes: 25 additions & 37 deletions views/signin.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
<?php
include_once ($_SERVER["DOCUMENT_ROOT"] . "/views/structure/head.php");
require_once ($_SERVER["DOCUMENT_ROOT"] . "/controller/C_signin.php"); ?>
<html lang="en">
<body>
<?php include("structure/header.php") ?>
<main>
<div id="errorPlace">
<?php
if ($DB_ERROR !== false)
echo $DB_ERROR;
if (isset($validMail) && !$validMail)
echo ("<h2 class='error'>Mail is already in use or not valid</h2>");
if (isset($validLogin) && !$validLogin)
echo ("<h2 class='error'>Login is already in use or not valid
(4 chars >= login <= 20 chars)</h2>");
if (isset($validPass) && !$validPass)
echo ("<h2 class='error'>Password should be at least 8 chars and
contains at least one letter and one digit</h2>");
if (isset($querySuccess) && $querySuccess === false)
echo ("<h2 class='error'>Error during user creation, please retry</h2>");
else if (isset($querySuccess) && $querySuccess === true)
echo ("<h2 class='success'>Account created</h2>");
?>
</div>
<form class="loginForm" method="post" action="signin.php">
<p>Sign-in</p><br/>
<input type="text" placeholder="Login" title="login" name="login"><br/>
<input type="email" placeholder="Mail" title="mail" name="mail"><br/>
<input type="password" placeholder="Password" title="password" name="password"><br/>
<input class="submit" type="submit" title="send" name="submit" value="Sign-in"><br/>
</form>
</main>
</body>
<?php include("structure/footer.php") ?>
</html>
<script src="style/style.js"></script>
<?php ob_start(); ?>
<div id="errorPlace"><?php
if ($database === NULL)
echo $DB_ERROR;
if (isset($validMail) && !$validMail)
echo ("<h2 class='error'>Mail is already in use or not valid</h2>");
if (isset($validLogin) && !$validLogin)
echo ("<h2 class='error'>Login is already in use or not valid
(4 chars >= login <= 20 chars)</h2>");
if (isset($validPass) && !$validPass)
echo ("<h2 class='error'>Password should be at least 8 chars and
contains at least one letter and one digit</h2>");
if (isset($querySuccess) && $querySuccess === false)
echo ("<h2 class='error'>Error during user creation, please retry</h2>");
else if (isset($querySuccess) && $querySuccess === true)
echo ("<h2 class='success'>Account created</h2>");?>
</div>
<form class="loginForm" method="post" action="index.php?action=signin">
<p>Sign-in</p><br/>
<input type="text" placeholder="Login" title="login" name="login"><br/>
<input type="email" placeholder="Mail" title="mail" name="mail"><br/>
<input type="password" placeholder="Password" title="password" name="password"><br/>
<input class="submit" type="submit" title="send" name="submit" value="Sign-in"><br/>
</form>
<?php $content = ob_get_clean(); ?>
30 changes: 10 additions & 20 deletions views/verify.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<?php
include_once ($_SERVER["DOCUMENT_ROOT"] . "/views/structure/head.php");
require_once ($_SERVER["DOCUMENT_ROOT"] . "/controller/C_verify.php");
?>
<html lang="en">
<body>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/views/structure/header.php") ?>
<main>
<div id="errorPlace">
<?php
if ($done === false)
echo ("<h2 class='error'>Error wrong token/login</h2>");
if ($done === true)
echo ("<h2 class='success'>Account activated</h2>");
?>
</div>
</main>
</body>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/views/structure/footer.php") ?>
</html>
<?php ob_start(); ?>
<div id="errorPlace">
<?php
if ($done === false)
echo ("<h2 class='error'>Error wrong token/login</h2>");
if ($done === true)
echo ("<h2 class='success'>Account activated</h2>");
?>
</div>
<?php $content = ob_get_clean() ?>

0 comments on commit 811794f

Please sign in to comment.