<?php

$file = fopen("../API_logs/API_verify_otp.txt","a");
$today = date("Y-m-d H:i:s");
$parameter = "-------------------".$today."----------------------------"."\n";
//$parameter = $parameter."File Name=>".$_FILES['videofile']['name']."\n";
foreach($_REQUEST as $key=>$value) {
	$parameter = $parameter."$key=>$value"."\n";
}
$parameter = $parameter."-----------------------------------------------"."\n";
//print "<br>".$parameter;
fwrite($file, $parameter);
fclose($file);

$validationErr = '';

if(!isset($_REQUEST['refer_id']) || $_REQUEST['refer_id'] == '') {
	$validationErr = "Please enter refer id!";
} else if(!isset($_REQUEST['section']) || $_REQUEST['section'] == '') {
	$validationErr = "Please enter section!";
} else if(!isset($_REQUEST['otp']) || $_REQUEST['otp'] == '') {
	$validationErr = "Please enter OTP!";
} 

if($validationErr != '') {

	$result = array();
	$result['status'] = '0';
	$result['message'] = $validationErr;
	print $return = json_encode($result);
	exit;

} 

date_default_timezone_set('Asia/Kolkata');

require_once('API_main_class.php');

class blockbooster_verify_otp extends main_class {
   
	public $conn;
	public $pdo;
	
	function __construct() {
	    
		$host_name= "localhost";
		$db_name = "mybooster";
		$user_name = "root";
		$password = "root";

		$pdoconn = new PDO("mysql:host=$host_name;dbname=$db_name", $user_name, $password);
		$pdoconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

		$pdoconn->beginTransaction();

		$this->pdo = $pdoconn;

		if(!$this->pdo) {
			$result = array();
			$result['status'] = '0';
			$result['message'] = "APP temporarily Unavailable!";
			print $return = json_encode($result);
			exit;
		}
    }
 
	public function getID() {
		
		$rowSelect 	= array();		
		$refer_id 	= $_REQUEST['refer_id'];
		$section 	= $_REQUEST['section'];
		$otp 		= $_REQUEST['otp'];

		$sqlSelect 		= "SELECT id, verified_status FROM my_otp WHERE deleted=0 AND refer_id='$refer_id' AND otp='$otp' AND section='$section'";
		$stmt 			= $this->pdo->prepare($sqlSelect);
		$stmt->execute();
		$rowSelect 		= $stmt->fetch (PDO::FETCH_ASSOC);
		
		return $rowSelect;
    }

	public function update_otp_request( $otpID ) {
		$cdate  = date("Y-m-d H:i:s");
		$sqlUpdate = "UPDATE my_otp SET date_modified='$cdate', verified_status='1' WHERE id='$otpID'";
		$stmt 	   = $this->pdo->prepare($sqlUpdate);
		$status    = $stmt->execute();
		return $status;
	}
}
 
$conn 		= new blockbooster_verify_otp();

$result 	= array();

$regData = $conn->getID();

if( $regData['id'] != "" ) {
	if( $regData['verified_status'] == 0 ) {
		$messageStatus 	= $conn->update_otp_request( $regData['id'] );
		if($messageStatus == 1) {
			$resultOTP['status'] = '1';
			$resultOTP['message'] = 'OTP Verify Success!';
			$conn->pdo->commit();
		} else {
			$resultOTP['status'] = '0';
			$resultOTP['message'] = 'Now we are unable to verify the OTP. Please try after some time!';
		}
	} else {
		$resultOTP['status'] = '0';
		$resultOTP['message'] = 'The OTP is already verified. Please send request for another OTP!';
	}
} else {
	$resultOTP['status'] = '0';
	$resultOTP['message'] = 'Invalid OTP!';
}

print $resultOTP1 = json_encode($resultOTP);


?> 


