Enable conference registration

Change-Id: I5e973513d78c63a48e62ff1aa3a5d9a1621f4a4d
diff --git a/validators.php b/validators.php
index e40222f..966f1d1 100644
--- a/validators.php
+++ b/validators.php
@@ -3,6 +3,9 @@
 include_once 'redis.php';
 include_once 'config.php';
 include_once 'utils.php';
+include_once 'db_backend.php';
+
+use \DB as DB;
 
 $TEMPLATE = template_path();
 
@@ -63,26 +66,35 @@
 
 function validate_email(string $email)
 {
-        global $TEMPLATE;
-        include "config.php";
+        global $CONFERENCE_REGISTRATION, $TEMPLATE, $log, $MAIL_HOST_BLACKLIST, $EMAIL_VALIDATION_ERROR, $BASE_URL;
         include $TEMPLATE . 'strings.php';
         $error = "";
 
-        if (ldap_mail_count($email)) {
+        if (($CONFERENCE_REGISTRATION && (new DB(null))->mail_count($email)) || (!$CONFERENCE_REGISTRATION && ldap_mail_count($email))) {
+                $log->info("Email already registered");
                 $error = $error . format($EMAIL_VALIDATION_ERROR->registered, ["link" => $BASE_URL . "?type=recover"]);
         }
-        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
-                $error = $error . $EMAIL_VALIDATION_ERROR->invalid;
-        } elseif (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) {
+        if (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) {
                 $error = $error . $EMAIL_VALIDATION_ERROR->blacklisted;
         }
+        $log->debug("Checking if email is pending");
         $pending = redis_get("pending");
         if ($pending) {
+                $log->debug("Email might be pending");
                 $maillist = $pending->mails;
-                if (in_array($email, $maillist)) {
-                        $error = $error . $EMAIL_VALIDATION_ERROR->pending;
+                if (is_array($maillist) && in_array($email, $maillist)) {
+                        if ($CONFERENCE_REGISTRATION) {
+                                unset($maillist[array_search($user->email, $maillist)]);
+                                redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
+                                $log->debug("Email was pending, but let participant change details.");
+                                echo '<div class="alert alert-warning" role="alert">A confirmation request has already been sent to this email, but no problem.</div>';
+                        } else {
+                                $error = $error . $EMAIL_VALIDATION_ERROR->pending;
+                                $log->debug("Email is pending");
+                        }
                 }
         }
+        $log->debug("email validated, result: $error");
         return $error;
 }