<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../../PHPMailer/src/Exception.php';
require '../../PHPMailer/src/PHPMailer.php';
require '../../PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

require_once 'auth.php';
$user = new Auth();

if(isset($_POST['action']) && $_POST['action'] == 'register'){
    //  print_r($_POST);
    $email = $user->test_input($_POST['email']);
    $username = $user->test_input($_POST['username']);
    $pass = $user->test_input($_POST['password']);
    $ref = $user->test_input($_POST['refer']);
    
    $ref = $user->ref_exist($ref);

    if(!empty($ref)){
        foreach($ref as $row){
            $referid = $row['id'];
        }
    }else{
        $referid = 1;
    }

    $n=7;
            function getName($n) {
                $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $randomString = '';
                for ($i = 0; $i < $n; $i++) {
                    $index = rand(0, strlen($characters) - 1);
                    $randomString .= $characters[$index];
                }
                return $randomString;
            }
            
            $user_ref=getName($n);
            $created_at = date('Y-m-d');


  
    $hpass = password_hash($pass,PASSWORD_DEFAULT);
    
    if($user->user_exist($email)){
      echo $user->showMessage('warning','Email already exist!');
      }elseif($user->username_exist($username)){
          echo $user->showMessage('warning','Username already exist!');
      }else{
          if($user->register($username,$email,$hpass,$referid,$user_ref,$created_at)){
              echo 'register';
            $_SESSION['user'] = $email; 
  
          
              //Server settings
              $mail->SMTPOptions = array(
                  'ssl' => array(
                  'verify_peer' => false,
                  'verify_peer_name' => false,
                  'allow_self_signed' => true
                  )
                  );
             
          
  
             // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
             $mail->isSMTP();                                            // Send using SMTP
              $mail->Host       = 'xmombo.com';                    // Set the SMTP server to send through
              $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
              $mail->Username   =  Database::USERNAME;                        // SMTP username
              $mail->Password   =  Database::PASSWORD;                              // SMTP password
              $mail->SMTPSecure =  PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
              $mail->Port       =   587;    
              
                          //Recipients
              $mail->setFrom(Database::USERNAME, 'Xmombo');
              $mail->addAddress($email);     // Add a recipient
  
              $mail->isHTML(true);                                  // Set email format to HTML
              $mail->Subject = 'Welcome to XMombo';
              $mail->Body    = '<table class="body-wrap" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">
              <tbody>
                  <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                      <td style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
                      <td class="container" width="600" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"
                          valign="top">
                          <div class="content" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
                              <table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;"
                                  bgcolor="#fff">
                                  <tbody>
                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                          <td class="" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #38414a; margin: 0; padding: 20px;"
                                              align="center" bgcolor="#71b6f9" valign="top">
                                              <a href="#" style="font-size:32px;color:#fff;"> Xmombo</a> <br>
                                              <span style="margin-top: 10px;display: block;">Auto BUY low & SELL high</span>
                                          </td>
                                      </tr>
                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                          <td class="content-wrap" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;" valign="top">
                                              <table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                  <tbody>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                             Welcome to <strong style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">XMombo
                                          </strong> ,
                                                          </td>
                                                      </tr>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                         We are super excited to have you on XMombo.  <br>
                                                         Your account has been created.
                                                          </td>
                                                      </tr>
                                                      
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                          To begin, please verify your account from your dashboard.
                                                          </td>
                                                      </tr>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                          Regards,<br>XMombo Team.
                                                          </td>
                                                      </tr>
                                                  </tbody>
                                              </table>
                                          </td>
                                      </tr>
                                  </tbody>
                              </table>
                              <div class="footer" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
                                  <table width="100%" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                      <tbody>
                                          <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                              <td class="aligncenter content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top"><a href="#" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">www.xmombo.com</a> 
                                              </td>
                                          </tr>
                                      </tbody>
                                  </table>
                              </div>
                          </div>
                      </td>
                      <td style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
                  </tr>
              </tbody>
          </table>';
  
              $mail->send();
      
          }else{
              echo $user->showMessage('danger','Something went wrong, try again!');
          }
      }
  }

//handle login ajax request
if(isset($_POST['action']) && $_POST['action'] == 'login'){
    //  print_r($_POST);
    $email = $user->test_input($_POST['email']);
    $pass = $user->test_input($_POST['password']);
  
    $loggedInUser = $user->login($email);
  
    if($loggedInUser != null){
        if(password_verify($pass, $loggedInUser['password'])){
            if(!empty($_POST['rem'])){
                setcookie("email", $email, time()+(30*24*60*60), '/');
                setcookie("password", $pass, time()+(30*24*60*60), '/');
            }else{
                setcookie("email","",1,'/');
                setcookie("password","",1,'/');
            }
            echo 'login';
            $_SESSION['user'] = $email;
            
  
        }
        else{
            echo $user->showMessage('danger', 'Incorrect Password!');
        }
    }
    else{
        echo $user->showMessage('danger', 'User not found! Contact Support');
    }
  }

  
  //handle forgot password ajax request
  if(isset($_POST['action']) && $_POST['action'] == 'forgot'){
    //  print_r($_POST);
      $email = $user->test_input($_POST['email']);

     
      
          try {
              //Server settings
              $mail->SMTPOptions = array(
                  'ssl' => array(
                  'verify_peer' => false,
                  'verify_peer_name' => false,
                  'allow_self_signed' => true
                  )
                  );
             
          

             // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
             $mail->isSMTP();                                            // Send using SMTP
              $mail->Host       = 'xmombo.com';                    // Set the SMTP server to send through
              $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
              $mail->Username   =  Database::USERNAME;                        // SMTP username
              $mail->Password   =  Database::PASSWORD;                              // SMTP password
              $mail->SMTPSecure =  PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
              $mail->Port       =   587;    
              
                          //Recipients
              $mail->setFrom(Database::USERNAME, 'XMombo');
              $mail->addAddress($email);     // Add a recipient

              $mail->isHTML(true);                                  // Set email format to HTML
              $mail->Subject = 'XMombo- Reset Password';
              $mail->Body    = '<table class="body-wrap" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">
              <tbody>
                  <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                      <td style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
                      <td class="container" width="600" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"
                          valign="top">
                          <div class="content" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
                              <table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;"
                                  bgcolor="#fff">
                                  <tbody>
                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                          <td class="" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #38414a; margin: 0; padding: 20px;"
                                              align="center" bgcolor="#71b6f9" valign="top">
                                              <a href="www.xmombo.com" style="font-size:32px;color:#fff;"> Xmombo</a> <br>
                                              <span style="margin-top: 10px;display: block;">Auto BUY low & SELL high</span>
                                          </td>
                                      </tr>
                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                          <td class="content-wrap" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;" valign="top">
                                              <table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                  <tbody>
                                                      
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                          Please click the link below to reset your password.
                                                          </td>
                                                      </tr>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                              <a href="www.xmombo.com/reset-password.php?email='.$email.'&token='.$token.'" class="btn-primary" style="font-family: ,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #f1556c; margin: 0; border-color: #f1556c; border-style: solid; border-width: 8px 16px;">
                                              Reset Password</a>
                                                          </td>
                                                      </tr>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                          www.xmombo.com/reset-password.php?email='.$email.'&token='.$token.'
                                                          </td>
                                                      </tr>
                                                      <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                          <td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                          Regards,<br>XMombo Team.
                                                          </td>
                                                      </tr>
                                                  </tbody>
                                              </table>
                                          </td>
                                      </tr>
                                  </tbody>
                              </table>
                              <div class="footer" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
                                  <table width="100%" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                      <tbody>
                                          <tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                              <td class="aligncenter content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top"><a href="www.xmombo.com" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">www.xmombo.com</a> 
                                              </td>
                                          </tr>
                                      </tbody>
                                  </table>
                              </div>
                          </div>
                      </td>
                      <td style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
                  </tr>
              </tbody>
          </table>';

              $mail->send();
              echo $user->showMessage('success', 'We have sent a reset link to your email, It can take up to five(5) minutes to arrive!');
          }
          catch(Exception $e){
              echo $user->showMessage('danger', 'Something went wrong, please try again!');
          }
          }
          
  }


       // Contact Us
    if(isset($_POST['action']) && $_POST['action'] == 'contact'){
        $fullname = $user->test_input($_POST['fullname']);
        $email = $user->test_input($_POST['email']);
        $subject = $user->test_input($_POST['subject']);
        $content = $user->test_input($_POST['content']);


        try {
            //Server settings
            $mail->SMTPOptions = array(
                'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
                )
                );
           
        

           // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
           $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = 'xmombo.com';                    // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   =  Database::USERNAME;                        // SMTP username
            $mail->Password   =  Database::PASSWORD;                              // SMTP password
            $mail->SMTPSecure =  PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       =   587;    
            
                        //Recipients
            $mail->setFrom($email, 'XMOMBO');
            $mail->addAddress(Database::USERNAME);     // Add a recipient

            $mail->isHTML(true);                                  // Set email format to HTML

            $bodyParagraphs = ["Name: {$fullname}", "Email: {$email}", "Message:", nl2br($content)];
            $body = join('<br />', $bodyParagraphs);
            $mail->Subject = $subject;
            $mail->Body    = $body;

            $mail->send();
            echo $user->showMessage('success', 'Your message has been submitted successfully. We will get back to you shortly.');
        }
        catch(Exception $e){
            echo $user->showMessage('danger', 'Something went wrong, please try again!');
        }
        

    }

    // Checking user is logged in or not
    if(isset($_POST['action']) && $_POST['action'] == 'checkUser'){
        if(!$user->currentUser($_SESSION['user'])){
            echo 'bye';
            unset($_SESSION['user']);
        }
    }





?>