<?php

/*
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
*/

$file = fopen("../API_logs/API_send_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!";
} 

if($_REQUEST['section'] != 1) {
	if(!isset($_REQUEST['password']) || $_REQUEST['password'] == '') {
		$validationErr = "Please enter password!";
	} 
}

if($validationErr != '') {

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

} 

date_default_timezone_set('Asia/Kolkata');

//require_once('database.php');
require_once('API_main_class.php');

class member_sign_up 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'] = "Service temporarily Unavailable!";
			print $return = json_encode($result);
			exit;
		}
    }
 
	public function getID() {
		
		$rowSelect 		= array();		
		$refer_id 		= $_REQUEST['refer_id'];
		//$password		= $_REQUEST['password'];
		$email			= $_REQUEST['emailID'];

		if($_REQUEST['section'] != 1) {
			$sqlSelect 		= "SELECT id, country_code, mobile_number FROM my_registration WHERE deleted=0 AND password='$password' AND refer_id='$refer_id'";
		} else {
			$sqlSelect 		= "SELECT id, country_code, mobile_number FROM my_registration WHERE deleted=0 AND refer_id='$refer_id' AND email_address='$email'";
		}

		$stmt 			= $this->pdo->prepare($sqlSelect);
		$stmt->execute();
		$rowSelect 		= $stmt->fetch (PDO::FETCH_ASSOC);
		
		return $rowSelect;
    }

	public function save_otp_request($otp, $mobile_number) {

		$refer_id 		= $_REQUEST['refer_id'];
		$section 		= $_REQUEST['section'];

		$otp1 	= rand(0, 9);
		$otp2 	= rand(0, 9);
		$otp3 	= rand(0, 9);
		$otp4 	= rand(0, 9);
		$randNo = $otp1.$otp2.$otp3.$otp4;
		$cdate  = date("Y-m-d H:i:s");
		$id 	= md5(date("Y-m-d H:i:s"));
		$idv 	= $id.$randNo;


		/*$sqlUpdate = "Update my_otp set verified_status='2' where section='$section' && refer_id='$refer_id' && verified_status='0'";
		$stmtUpdate->execute();*/


		$sqlInsert = "INSERT INTO my_otp(id, name, refer_id, otp, section, send_status, date_entered, date_modified) VALUES('$idv', '$mobile_number', '$refer_id', '$otp', '$section', 1, '$cdate', '$cdate')";
		$stmt 	   = $this->pdo->prepare($sqlInsert);
		$status    = $stmt->execute();
		return $status;
	}
}
 
$conn 		= new member_sign_up();

$result 	= array();

$regData = $conn->getID();




if( $regData['id'] != '' ) {
	if( strlen($regData['mobile_number']) == 10 ) {
		$otp 		= rand(1, 9).rand(0, 9).rand(1, 9).rand(0, 9);
		//$mobileNo 	= '+'.trim($regData['country_code']).trim($regData['mobile_number']);
		$mobileNo 	= trim($regData['country_code']).trim($regData['mobile_number']);
		$message= '';

	
		if($_REQUEST['section'] ==1)
			$message  	= "OTP to reset your password for MyBooster Member ID: " . $_REQUEST['refer_id'] ."  is ".$otp;
		
		
		$resultData = $conn->save_otp_request($otp, $mobileNo);
		if($resultData == 1) {

			/*if(trim($regData['country_code'])==91){
				$messageStatus = $conn->sendOTP_special(trim($regData['mobile_number']), $message);
			}else{
				$messageStatus = $conn->sendOTP_special_Twilio($mobileNo, $message);
			}*/


			//$messageStatus = $conn->sendOTP_special_Twilio($mobileNo, $message);
			$conn->send_SMS_ID($mobileNo, $message);
            $messageStatus = 1;

	
			if($messageStatus == 1) {
				$conn->pdo->commit();
				$result['status'] 			= '1';
				$result['message'] 			= 'Success!';
			} else {
				$result['status'] = '0';
				$result['message'] = $messageStatus ;
			}
		} else {
			$result['status'] = '0';
			$result['message'] = 'We are unable to send the OTP now. Please try after some time!';
		}
	} else {
		$result['status'] = '0';
		$result['message'] = 'Invalid Mobile Number. Please update mobile number!';
	}
} else {

	$result['status'] = '0';
	$result['message'] = 'Invalid Login Credential. Please contact adminstrator to reset your password!';

}

print $result = json_encode($result);
?> 

