From aa64f964664961a9075ffd7f3403d13488cc8e2c Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 22 Jan 2017 11:17:31 +0100 Subject: [PATCH] #1019 Add email field to web integration sample --- samples/website_integration/AuthMeController.php | 10 ++++++---- samples/website_integration/index.php | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/samples/website_integration/AuthMeController.php b/samples/website_integration/AuthMeController.php index f67cf0608..8838bf1dd 100644 --- a/samples/website_integration/AuthMeController.php +++ b/samples/website_integration/AuthMeController.php @@ -54,16 +54,18 @@ abstract class AuthMeController { * * @param string $username the username to register * @param string $password the password to associate to the user + * @param string $email the email (may be empty) * @return bool whether or not the registration was successful */ - function register($username, $password) { + function register($username, $password, $email) { + $email = $email ? $email : 'your@email.com'; $mysqli = $this->getAuthmeMySqli(); if ($mysqli !== null) { $hash = $this->hash($password); - $stmt = $mysqli->prepare('INSERT INTO ' . self::AUTHME_TABLE . ' (username, realname, password, ip) ' - . 'VALUES (?, ?, ?, ?)'); + $stmt = $mysqli->prepare('INSERT INTO ' . self::AUTHME_TABLE . ' (username, realname, password, email, ip) ' + . 'VALUES (?, ?, ?, ?, ?)'); $username_low = strtolower($username); - $stmt->bind_param('ssss', $username, $username_low, $hash, $_SERVER['REMOTE_ADDR']); + $stmt->bind_param('sssss', $username, $username_low, $hash, $email, $_SERVER['REMOTE_ADDR']); return $stmt->execute(); } return false; diff --git a/samples/website_integration/index.php b/samples/website_integration/index.php index 0c2a1a1cc..c20cbe41b 100644 --- a/samples/website_integration/index.php +++ b/samples/website_integration/index.php @@ -22,13 +22,14 @@ $authme_controller = new Sha256(); $action = get_from_post_or_empty('action'); $user = get_from_post_or_empty('username'); $pass = get_from_post_or_empty('password'); +$email = get_from_post_or_empty('email'); $was_successful = false; if ($action && $user && $pass) { if ($action === 'Log in') { $was_successful = process_login($user, $pass, $authme_controller); } else if ($action === 'Register') { - $was_successful = process_register($user, $pass, $authme_controller); + $was_successful = process_register($user, $pass, $email, $authme_controller); } } @@ -39,6 +40,7 @@ into the following form to test it.
+ @@ -69,12 +71,14 @@ function process_login($user, $pass, AuthMeController $controller) { } // Register logic -function process_register($user, $pass, AuthMeController $controller) { +function process_register($user, $pass, $email, AuthMeController $controller) { if ($controller->isUserRegistered($user)) { echo '

Error

This user already exists.'; + } else if (!is_email_valid($email)) { + echo '

Error

The supplied email is invalid.'; } else { // Note that we don't validate the password or username at all in this demo... - $register_success = $controller->register($user, $pass); + $register_success = $controller->register($user, $pass, $email); if ($register_success) { printf('

Welcome, %s!

Thanks for registering', htmlspecialchars($user)); echo '
Back to form'; @@ -86,6 +90,12 @@ function process_register($user, $pass, AuthMeController $controller) { return false; } +function is_email_valid($email) { + return trim($email) === '' + ? true // accept no email + : filter_var($email, FILTER_VALIDATE_EMAIL); +} + ?>
Name
Email
Pass