<?php
 
 header("Access-Control-Allow-Origin: http://localhost/rest-api-authentication-example/");
//header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
 
// files needed to connect to database

 $pathRoot = $_SERVER['DOCUMENT_ROOT'];
include_once  $pathRoot ."/saia/api/v1/config/database.php";
include_once $pathRoot ."/saia/api/v1/objects/appUser.php";

//========== Files needed for email =====================================
include_once $pathRoot ."/saia/api/v1/sendmail/customMailSender.php";

 
// get database connection
$database = new Database();
$db = $database->getConnection();
//==================Generate Verification Code =================================
 //$verificationCode = mt_rand(100000,999999);


//==============================================================
$data = json_decode(file_get_contents("php://input"));


// set product property values


$emailAddress =  $data->email;
//$resend =  $data->resend;


// instantiate AppUser object
$user = new AppUser($db);
$user->email = $emailAddress;
//$user->verificationCode = $verificationCode;

$emptyData = "";
//=================== New Code ===========================
    //$userExists = $user->getConfirmationCode($emailAddress);
    
    $userExists = $user->emailExists();
    if(!$userExists){
         http_response_code(401);
        echo json_encode(array("responseCode"=>401,"result"=>"Invalid user","confirmationCode"=>""));
        return;
           
    }

     if(strcmp($user->verificationCode,$emptyData)==0){
            $verificationCode = mt_rand(100000,999999);
             $user->verificationCode = strval($verificationCode);
              $user->updateVerificationCode();
       }
    
    
    
     $succesfulSend = sendEmail("",$user->verificationCode,$emailAddress,"PocketStylist Account Verification Code",$user->name);

     if( strcmp($succesfulSend,"Email send")==0 ){
          http_response_code(200);
          echo json_encode(array("responseCode"=>200,"result"=>"Email send","confirmationCode"=>$user->verificationCode));
         return;
       }
     http_response_code(200);
     echo json_encode(array("responseCode"=>200,"result"=>"Email not send","confirmationCode"=>""));

//========================================================



//==============================================================================

 function sendEmail($additionalEmail,$code,$emailAddress,$subject,$name){
     $msg = "Confirmation Code: " . $code;
      $msg = wordwrap($msg,70);
    $headers = "From: authenticator@pocketstylists.com";
    // send email
  //  $mailResults = mail($emailAddress,"PocketStylist Registration Confirmation //Code",$msg,$headers);
    //$subject = "PocketStylist Registration Confirmation Code";
    $mailResults = sendCustomMail($additionalEmail,"helpdesk@saimage-apps.co.za","Helpdesk", $emailAddress ,$name,$subject, $msg,"");
    if($mailResults){
        return "Email send";
    }else{
        return $mailResults;
    }
 }
 


?> 