2016-02-13 10:56:15 +01:00
|
|
|
<!--
|
2016-05-19 21:44:24 +02:00
|
|
|
This is a demo page for AuthMe website integration.
|
|
|
|
See AuthMeController.php and the extending classes for the PHP code you need.
|
2016-02-13 10:56:15 +01:00
|
|
|
-->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<title>AuthMe Integration Sample</title>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<?php
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
2016-05-19 21:44:24 +02:00
|
|
|
require 'AuthMeController.php';
|
|
|
|
|
|
|
|
// Change this to the file of the hash encryption you need, e.g. Bcrypt.php or Sha256.php
|
|
|
|
require 'Sha256.php';
|
|
|
|
// The class name must correspond to the file you have in require above! e.g. require 'Sha256.php'; and new Sha256();
|
|
|
|
$authme_controller = new Sha256();
|
|
|
|
|
2016-02-19 19:13:49 +01:00
|
|
|
$action = get_from_post_or_empty('action');
|
2016-02-13 10:56:15 +01:00
|
|
|
$user = get_from_post_or_empty('username');
|
|
|
|
$pass = get_from_post_or_empty('password');
|
2017-01-22 11:17:31 +01:00
|
|
|
$email = get_from_post_or_empty('email');
|
2016-02-13 10:56:15 +01:00
|
|
|
|
|
|
|
$was_successful = false;
|
2016-02-19 19:13:49 +01:00
|
|
|
if ($action && $user && $pass) {
|
|
|
|
if ($action === 'Log in') {
|
2016-05-19 21:44:24 +02:00
|
|
|
$was_successful = process_login($user, $pass, $authme_controller);
|
2016-02-19 19:13:49 +01:00
|
|
|
} else if ($action === 'Register') {
|
2017-01-22 11:17:31 +01:00
|
|
|
$was_successful = process_register($user, $pass, $email, $authme_controller);
|
2016-02-13 11:00:56 +01:00
|
|
|
}
|
2016-02-13 10:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$was_successful) {
|
2016-02-13 11:00:56 +01:00
|
|
|
echo '<h1>Login sample</h1>
|
2016-02-13 10:56:15 +01:00
|
|
|
This is a demo form for AuthMe website integration. Enter your AuthMe login details
|
|
|
|
into the following form to test it.
|
|
|
|
<form method="post">
|
|
|
|
<table>
|
|
|
|
<tr><td>Name</td><td><input type="text" value="' . htmlspecialchars($user) . '" name="username" /></td></tr>
|
2017-01-22 11:17:31 +01:00
|
|
|
<tr><td>Email</td><td><input type="text" value="' . htmlspecialchars($email) . '" name="email" /></td></tr>
|
2016-02-13 10:56:15 +01:00
|
|
|
<tr><td>Pass</td><td><input type="password" value="' . htmlspecialchars($pass) . '" name="password" /></td></tr>
|
2016-02-19 19:13:49 +01:00
|
|
|
<tr>
|
|
|
|
<td><input type="submit" name="action" value="Log in" /></td>
|
|
|
|
<td><input type="submit" name="action" value="Register" /></td>
|
|
|
|
</tr>
|
2016-02-13 10:56:15 +01:00
|
|
|
</table>
|
|
|
|
</form>';
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_from_post_or_empty($index_name) {
|
2016-02-13 11:00:56 +01:00
|
|
|
return trim(
|
|
|
|
filter_input(INPUT_POST, $index_name, FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR | FILTER_FLAG_STRIP_LOW)
|
|
|
|
?: '');
|
2016-02-13 10:56:15 +01:00
|
|
|
}
|
2016-02-19 19:13:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Login logic
|
2016-05-19 21:44:24 +02:00
|
|
|
function process_login($user, $pass, AuthMeController $controller) {
|
|
|
|
if ($controller->checkPassword($user, $pass)) {
|
2016-02-19 19:13:49 +01:00
|
|
|
printf('<h1>Hello, %s!</h1>', htmlspecialchars($user));
|
|
|
|
echo 'Successful login. Nice to have you back!'
|
2016-05-19 21:44:24 +02:00
|
|
|
. '<br /><a href="index.php">Back to form</a>';
|
2016-02-19 19:13:49 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
echo '<h1>Error</h1> Invalid username or password.';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register logic
|
2017-01-22 11:17:31 +01:00
|
|
|
function process_register($user, $pass, $email, AuthMeController $controller) {
|
2016-05-19 21:44:24 +02:00
|
|
|
if ($controller->isUserRegistered($user)) {
|
2016-02-19 19:13:49 +01:00
|
|
|
echo '<h1>Error</h1> This user already exists.';
|
2017-01-22 11:17:31 +01:00
|
|
|
} else if (!is_email_valid($email)) {
|
|
|
|
echo '<h1>Error</h1> The supplied email is invalid.';
|
2016-02-19 19:13:49 +01:00
|
|
|
} else {
|
|
|
|
// Note that we don't validate the password or username at all in this demo...
|
2017-01-22 11:17:31 +01:00
|
|
|
$register_success = $controller->register($user, $pass, $email);
|
2016-02-19 19:13:49 +01:00
|
|
|
if ($register_success) {
|
|
|
|
printf('<h1>Welcome, %s!</h1>Thanks for registering', htmlspecialchars($user));
|
2016-05-19 21:44:24 +02:00
|
|
|
echo '<br /><a href="index.php">Back to form</a>';
|
2016-02-19 19:13:49 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
echo '<h1>Error</h1>Unfortunately, there was an error during the registration.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-22 11:17:31 +01:00
|
|
|
function is_email_valid($email) {
|
|
|
|
return trim($email) === ''
|
|
|
|
? true // accept no email
|
|
|
|
: filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
}
|
|
|
|
|
2016-02-13 10:56:15 +01:00
|
|
|
?>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|