<?php
include 'pagename.php';
include $session;
Session::checkSession_log();
include '../config.php';
$evp_options_sql = "SELECT option_name, option_value FROM evp_options";
$evp_options_read = $db->select($evp_options_sql);
if ($evp_options_read) {
    $evp_options_check = mysqli_num_rows($evp_options_read);
    if ($evp_options_check > 0) {
		 while($row = $evp_options_read->fetch_assoc()) {
			$option_name[$row['option_name']] = $row['option_value'];
    }
	$evp_options_read->free();
        $s_url =  $option_name['site_url'];		
        $a_url =  $option_name['app_url'];		
		$s_path =  $option_name['script_path'];
		$site_title =  $option_name['site_title'];
		$system_email =  $option_name['system_email'];
        $smtp_debug  =  $option_name['smtp_debug'];
		$resettoken =  $option_name['verify_email_token'];
		$get_fname =  $option_name['fname'];
		$get_lname =  $option_name['lname'];
		$get_email =  $option_name['email'];
		$get_password =  $option_name['password'];
		$get_user_id =  $option_name['user_id'];
		$get_locale =  $option_name['locale'];
		$get_image =  $option_name['image'];
		$category =  $option_name['category'];
	}
}

function RandomString($length = 20) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function test_input($data) { //filter value function
    $db = new database();
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysqli_real_escape_string($db->link, $data);
    return $data;
}
$std_ex = false;
$error = false;
$message = '';
$action_std = false;
$action_cat = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (isset($_POST['login_action']))  {
          $action_std = true;
          $action_cat = 'exicute';
          $submessage = '';
          $email = test_input($_POST['email']);
          $email = strtolower($email);
          $password = test_input($_POST['password']);
          $email = mysqli_real_escape_string($db->link, $email);
          $password = mysqli_real_escape_string($db->link, $password);
          $password = md5($password);
           if($evp_options_read) {
			if ((($email == $get_email) && ($password == $get_password)) != false) {
              $std = true;
              if($category == 'admin'){
                Session::set("auth_log", true);
              }
              if($std){
                $std_ex = true;
                Session::set("login", true);
                Session::set("fname", !empty($get_fname) ? $get_fname : '');
                Session::set("lname", !empty($get_lname) ? $get_lname : '');
                Session::set("image", $get_image); 
                Session::set("email", $get_email); 
                Session::set("locale", $get_locale); 
                Session::set("id",  $get_user_id);
              }
            }else{
              $error = true;
              $message = "Wrong email or password!";
            }
          }else{
            $error = true;
            $message = "Database connection error!";
          }
        }elseif (isset($_POST['resend_verify_mail']))  {
          $action_std = true;
          $action_cat = 'token';
          $submessage = '';
          $email = test_input($_POST['email']);
          if(!empty($email) && !empty($resettoken)){
                $req_email = $email;
                $req_token = $resettoken;
                mailsend($req_email,$req_token,'verify_email');
                $error = false;
                $message = "Email Sent";
                $submessage = "Confirmation email has been sent again. Please check your mail inbox/spam folder";
          }else{
            $error = true;
            $message = "No data found";
          }
        }else{
          header("Location: $a_url/".$error_404_page);
        }
        if($action_std && !empty($action_cat)){
          if ($std_ex) {
            header("location:$a_url/".$index_page);
          }else{
            Session::set("action_cat", $action_cat);
            Session::set("action", $error);
            Session::set("action_message", $message);
            Session::set("action_submessage", $submessage);
            header("Location: $a_url/".$login_page);
          }
        }else{
          header("Location: $a_url/".$error_404_page);
        }
      }else{
        header("Location: $a_url/".$error_404_page);
      }
?>