Introduce logging

Change-Id: I5ac91cc195a46d666714904b56e9f89f56f9c2c0
diff --git a/User.php b/User.php
new file mode 100644
index 0000000..fb2bff6
--- /dev/null
+++ b/User.php
@@ -0,0 +1,35 @@
+<?php
+require_once 'vendor/autoload.php';
+
+class User
+{
+        public $user_name;
+        public $name;
+        public $first_name;
+        public $last_name;
+        public $email;
+        public $organization;
+        public $user_hash;
+        public $password;
+
+        function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password, string $organization)
+        {
+                include 'config.php';
+                $this->user_name = $user_name;
+                $this->name = $first_name;
+                $this->first_name = $first_name;
+                $this->last_name = $last_name;
+                $this->email = $email;
+                $this->organization = $organization;
+                if ($ENCRYPT_PASSWORDS) {
+                        $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$');
+                        # $this->user_hash = "{SHA}" .  base64_encode(sha1($password, true));
+                } else {
+                        $this->user_hash = "{CLEAR}" . $password;
+                }
+                $this->password = $this->user_hash;
+
+        }
+}
+
+?>
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 98cb9e4..cbe00da 100644
--- a/composer.json
+++ b/composer.json
@@ -1,5 +1,6 @@
 {
     "require": {
-        "gregwar/captcha": "1.*"
+        "gregwar/captcha": "1.*",
+        "monolog/monolog": "3.*"
     }
 }
diff --git a/index.php b/index.php
index 0ffa33c..ba273e4 100755
--- a/index.php
+++ b/index.php
@@ -6,6 +6,15 @@
 include_once 'mail.php';
 include_once 'ldap.php';
 include_once 'validators.php';
+include_once 'User.php';
+require __DIR__ . '/vendor/autoload.php';
+use Monolog\Level;
+use Monolog\Logger as Logger;
+use Monolog\Handler\StreamHandler;
+use Monolog\Handler\RotatingFileHandler;
+use \User as User;
+$log = new Logger('signup');
+$log->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/signup.log', 0, Logger::DEBUG));
 
 if (!$DEBUG)    error_reporting(0);
 else error_reporting(1);
@@ -95,7 +104,7 @@
 }
 
 
-function verify_request($user)
+function verify_request(User $user)
 {
     $TEMPLATE = template_path();
     unset($_SESSION['captcha_token']);
@@ -127,9 +136,11 @@
     $_SESSION['organization'] = $user->organization;
 }
 
-function approve_request($user)
+function approve_request(User $user)
 {
     include 'config.php';
+    global $log;
+
     $token = generateRandomString();
     redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY);
     $pending = redis_get("pending");
@@ -151,6 +162,7 @@
     backup_user_in_session($user);
     $TEMPLATE = template_path();
     include $TEMPLATE . "confirm_your_email.htm";
+    $log->info("Registration process started for " . user_to_string($user));
 }
 
 function recover_form($error = null)
@@ -229,6 +241,7 @@
                         $TEMPLATE = template_path();
                         send_recovery_email($email, $smtp, $url);
                         include $TEMPLATE . "confirm_your_email.htm";
+                        $log->info("Password recovery email sent to " . $email);
                     }
                 }
                 break;
@@ -249,6 +262,7 @@
                         else
                             $smtp = $SMTP;
                         send_mail($email, $smtp, $PASSWORD_CHANGED_EMAIL_TEMPLATE);
+                        $log->info("Password changed for " . $email);
                     } else {
                         include $TEMPLATE . "strings.php";
                         echo $STRINGS->change_password_ldap_error;
@@ -266,7 +280,7 @@
                 echo $RUNTIME_ERROR->user_trying_invalid_get;
             } else {
                 $token = $_GET["token"];
-                $user = redis_get($token);
+                $user = redis_get_user($token);
                 if ($user && gettype($user) == "object") {
                     if (ldap_add_user($user)) {
                         if ($REDIRECT_TO)
@@ -281,10 +295,12 @@
                             }
                         }
                         redis_inc_ipdata(getClientIP(), "register");
+                        $log->info("User registered: " . user_to_string($user));
                         echo $STRINGS->email_confirmation;
                         if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user);
                         include $TEMPLATE . "mail_confirmed.htm";
                     } else {
+                        $log->error("User registration failed for: " . user_to_string($user));
                         echo $STRINGS->email_confirmation;
                         include $TEMPLATE . "registration_error.htm";
                     }
diff --git a/ldap.php b/ldap.php
index d73349f..637c54f 100755
--- a/ldap.php
+++ b/ldap.php
@@ -1,5 +1,8 @@
 <?php
 
+include_once "User.php";
+use \User as User;
+
 function debug($msg)
 {
         include 'config.php';
@@ -18,26 +21,6 @@
         return $string;
 }
 
-class User
-{
-        function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password, string $organization)
-        {
-                $this->user_name = $user_name;
-                $this->name = $first_name;
-                $this->first_name = $first_name;
-                $this->last_name = $last_name;
-                $this->email = $email;
-                $this->organization = $organization;
-                if ($ENCRYPT_PASSWORDS) {
-                        $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$');
-                        # $this->user_hash = "{SHA}" .  base64_encode(sha1($password, true));
-                } else {
-                        $this->user_hash = "{CLEAR}" . $password;
-                }
-                $this->password = $this->user_hash;
-
-        }
-}
 
 function ldap_search_query($query, $filter = "cn")
 {
@@ -65,7 +48,7 @@
         return $data;
 }
 
-function ldap_add_user($user)
+function ldap_add_user(User $user)
 {
         include 'config.php';
         $ldap_host = $HOST;
@@ -116,7 +99,7 @@
 
         return $add;
 }
-function ldap_user_count($user)
+function ldap_user_count(string $user)
 {
         return ldap_search_query($user)["count"];
 }
diff --git a/redis.php b/redis.php
index f1cb0c0..edd0f6f 100755
--- a/redis.php
+++ b/redis.php
@@ -1,4 +1,7 @@
 <?php
+   include "User.php";
+   use \User as User;
+
    //Connecting to Redis server on localhost 
    function connect(){
       include 'config.php';
@@ -11,6 +14,15 @@
       $redis = connect();
       return json_decode($redis->get($key));
    }
+
+   function redis_get_user($key) {
+      $redis = connect();
+      $data = json_decode($redis->get($key));
+      $user = new User($data->user_name, $data->first_name, $data->last_name, $data->email, $data->password, $data->organization);
+
+      return $user;
+   }
+
    function redis_set($key, $data, $timeout=null){
       $redis = connect();
       $redis->set($key, json_encode($data), $timeout);
diff --git a/utils.php b/utils.php
index d50cc24..44160c0 100644
--- a/utils.php
+++ b/utils.php
@@ -1,4 +1,8 @@
 <?php
+
+include_once 'User.php';
+use \User as User;
+
 function generateRandomString($length = 96)
 {
     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
@@ -65,4 +69,9 @@
     $string = str_replace("{{organization}}", $user->organization, $string);
     $string = str_replace("{{username}}", $user->user_name, $string);
     return $string;
+}
+
+function user_to_string(User $user)
+{
+    return $user->first_name . " " . $user->last_name . " <" . $user->email . "> " . $user->organization . " " . $user->user_name;
 }
\ No newline at end of file