Improve confirmation mails

Change-Id: Ida840dfc4c4477fd8b86a7dcb640360c96c99be1
diff --git a/index.php b/index.php
index ec70b9f..0ffa33c 100755
--- a/index.php
+++ b/index.php
@@ -27,7 +27,7 @@
 $TEMPLATE = template_path();
 
 
-function send_confirmation_email(string $mail, object $smtp, string $url)
+function send_confirmation_email(string $mail, object $smtp, string $url, User $user)
 {
     include 'config.php';
     $TEMPLATE = template_path();
@@ -35,8 +35,8 @@
 
     send_mail($mail, $smtp, (object) [
         "subject" => $MAIL_TEMPLATE->subject,
-        "text"    => str_replace("{{url}}", $url, $MAIL_TEMPLATE->text),
-        "html"    => str_replace("{{url}}", $url, $MAIL_TEMPLATE->html)
+        "text"    => replace_all_user_variables($MAIL_TEMPLATE->text, $user, $url),
+        "html"    => replace_all_user_variables($MAIL_TEMPLATE->html, $user, $url)
     ]);
 }
 
@@ -118,6 +118,15 @@
     return $error;
 }
 
+function backup_user_in_session($user)
+{
+    $_SESSION['username'] = $user->user_name;
+    $_SESSION['first_name'] = $user->first_name;
+    $_SESSION['last_name'] = $user->last_name;
+    $_SESSION['email'] = $user->email;
+    $_SESSION['organization'] = $user->organization;
+}
+
 function approve_request($user)
 {
     include 'config.php';
@@ -136,10 +145,10 @@
         $smtp = $FALLBACK_SMTP;
     else
         $smtp = $SMTP;
-    send_confirmation_email($user->email, $smtp, $url);
+    send_confirmation_email($user->email, $smtp, $url, $user);
     $_SESSION['resend'] = generateRandomString(12);
     $_SESSION['token'] = $token;
-    $_SESSION['email'] = $user->email;
+    backup_user_in_session($user);
     $TEMPLATE = template_path();
     include $TEMPLATE . "confirm_your_email.htm";
 }
@@ -288,19 +297,22 @@
         case "resend":
             if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) {
                 include $TEMPLATE . "resend_mail.htm";
+                $user = new User($_SESSION["username"], $_SESSION["first_name"], $_SESSION["last_name"], $_SESSION["email"], "", $_SESSION["organization"]);
                 $token = $_SESSION['token'];
                 $url = $BASE_URL . "?type=confirmation&token=" . $token;
                 $smtp = $FALLBACK_SMTP;
                 $address = $_SESSION["email"];
                 if (isset($_SESSION['recover'])) {
                     $url = $BASE_URL . "?type=password_change&token=" . $token;
-                    send_recovery_email($address, $smtp, $url);
+                    send_recovery_email($address, $smtp, $url, $user);
                     unset($_SESSION['recover']);
                 } else
-                    send_confirmation_email($address, $smtp, $url);
+                    send_confirmation_email($address, $smtp, $url, $user);
                 unset($_SESSION['resend']);
                 unset($_SESSION['token']);
-                unset($_SESSION['email']);
+                # unset($_SESSION['email']);
+            } else {
+                echo "<h2>A second email has already been sent to " . $_SESSION['email'] .".</h2>";
             }
             break;
 
diff --git a/templates/email.php b/templates/email.php
index 3033d65..7d61eef 100644
--- a/templates/email.php
+++ b/templates/email.php
@@ -1,9 +1,55 @@
 <?php
+$REGISTRATION_INFORMATION = "
+<h3>Registration information</h3>
+<p>
+<table style='border: 1px solid #e1e4e8; border-collapse: collapse; width: 100%;'>
+<tr>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+<strong>Username</strong>
+</td>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+{{username}}
+</td>
+</tr>
+<tr>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+<strong>Name</strong>
+</td>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+{{full_name}}
+</td>
+</tr>
+<tr>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+<strong>Organization</strong>
+</td>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+{{organization}}
+</td>
+</tr>
+<tr>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+<strong>Email</strong>
+</td>
+<td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>
+{{email}}
+</td>
+</tr>
+</table>
+</p>
+";
+
 $MAIL_TEMPLATE = (object)[
         "subject" => "[" . $SERVICE_ACRONYM . "] Confirm your email address",
         "text"    => "To complete your registration please paste this to your browser: {{url}}",
         "html" => "<html><body>
-                <p>Almost there! Click on the confirm button below to confirm your email address and to complete your " . $SERVICE_ACRONYM . " registration</p>
+                <p>Dear {{full_name}},</p>
+                <p>Please check the information below and click on the confirm button to complete your " . $SERVICE_ACRONYM . " registration:</p>"
+                . $REGISTRATION_INFORMATION . "
+                <p>
+                If you did not request this registration, please ignore this email.
+                If you find errors in the information above, please ignore this email and repeat the registration process.
+                </p>
                 <a style='display: inline-block; outline: 0; cursor: pointer;
                 padding: 5px 16px;
                 font-size: 14px;
@@ -25,6 +71,8 @@
                     transition-duration: 0.1s;
                 }
             ' href='{{url}}'>Confirm " . $SERVICE_ACRONYM . " registration</a>
+            <p>Best regards,</p>
+                <p>" . $SERVICE_ACRONYM . " team</p>
         </body></html>"
 ];
 
diff --git a/templates/register.htm b/templates/register.htm
index 60b7bd5..62466fc 100644
--- a/templates/register.htm
+++ b/templates/register.htm
@@ -175,7 +175,7 @@
             </div>
         </div>
         <input pattern="[^\s]{<?php echo $CAPTCHA_LENGTH; ?>,}" required
-            title="Please fill the captcha. It has 5 characters" name="captcha" type="text" id="form3Example6"
+            title="Please fill the captcha. It has <?php echo $CAPTCHA_LENGTH; ?> characters" name="captcha" type="text" id="form3Example6"
             class="form-control" placeholder="Type what you see on the image above" />
             <div class="invalid-feedback">
                 Type what you see on the image above.
diff --git a/templates/resend_mail.htm b/templates/resend_mail.htm
index be5a7c6..7f8557d 100644
--- a/templates/resend_mail.htm
+++ b/templates/resend_mail.htm
@@ -1 +1 @@
-<h2>Another email was sent!</h2>
+<h2>Another email has been sent to <?php echo $_SESSION['email']; ?>!</h2>
diff --git a/utils.php b/utils.php
index 5c2f399..d50cc24 100644
--- a/utils.php
+++ b/utils.php
@@ -53,3 +53,16 @@
 
     return $TEMPLATE;
 }
+
+function replace_all_user_variables(string $string, User $user, string $url)
+{
+    $string = str_replace("{{url}}", $url, $string);
+    $string = str_replace("{{full_name}}", $user->first_name . " " . $user->last_name, $string);
+    $string = str_replace("{{fullname}}", $user->first_name . " " . $user->last_name, $string);
+    $string = str_replace("{{first_name}}", $user->first_name, $string);
+    $string = str_replace("{{last_name}}", $user->last_name, $string);
+    $string = str_replace("{{email}}", $user->email, $string);
+    $string = str_replace("{{organization}}", $user->organization, $string);
+    $string = str_replace("{{username}}", $user->user_name, $string);
+    return $string;
+}
\ No newline at end of file