<?php

//session_start();

header('Content-Type: application/json');
date_default_timezone_set('Asia/Kolkata');


//header( 'Content-Type: text/html; charset=utf-8' ); 

	ini_set('display_startup_errors',1);
	ini_set('display_errors',1);
	error_reporting(1);



$validationErr = '';
$section = 0;

    if(!isset($_REQUEST['newPassword']) || $_REQUEST['newPassword'] == '') {
		$validationErr = "Please Enter New Password!";
	} else if(!isset($_REQUEST['confirmPassword']) || $_REQUEST['confirmPassword'] == '') {
		$validationErr = "Please Enter Confirm Password!";
	} else if($_REQUEST['confirmPassword'] != $_REQUEST['newPassword']) {
		$validationErr = "New and Confirm Password should be same!";
	} 


if($validationErr != '') {

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

}

$db = new PDO('mysql:host=localhost;dbname=mybooster;', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$refer_id    = $_REQUEST['refer_id'];
$user_id     = $_REQUEST['user_id'];
$customer_id = $_REQUEST['customer_id'];
$oldpassword = md5($_REQUEST['oldPassword']);
$newpassword = md5($_REQUEST['newPassword']);
$result = array();


	if(matchPassword($user_id, $newpassword, $db))
	{
	 
		if(updatePassword($user_id, $newpassword, $db))
		{
			$result['success'] = true;
			$result['message'] = "Login Password updated successfully!";
		}

		else
		{
			$result['success'] = false;
			$result['message'] = "Invalid Password Credential!";
		}
	}


	else
	{
		$result['success'] = false;
		$result['message'] = "Login and Transaction password cannot be same!";
	}






	function updatePassword($user_id, $newpassword, $db)
	{

       $customer_id = $_REQUEST['customer_id'];
	   $callType    = 1;
	   $mybooster   = '';	

	   
	   if(!empty($customer_id) && ($customer_id!='null'))
	   {
		   $mybooster = updateCredentials();
		   if(isset($mybooster->customer))
		   	   $callType = 1;
		   else
			   $callType = 0;
	   }

		
	  		
		if($callType)
		{
			try
			{
				$stmt = $db->prepare("update my_registration set password=? where id=?");
				$stmt->execute(array($newpassword, $user_id));
				//echo $stmt->rowCount(); exit;
				if($stmt->rowCount())
					return true;
				else
					return false;
			}

			catch(PDOException $ex){
			   $result['success'] = false;
			   $result['message'] = "Change Password request failed! Please try after sometime.";

			}
		}

		else
		{
			$result['success'] = false;
			$result['message'] = "Change Password request failed! Please contact admin.";
		}


	}



	
	function matchPassword($user_id, $newpassword, $db)
	{

		try
		{
			$stmt = $db->prepare("select refer_id from my_registration where transaction_password=? AND id=?");
			$stmt->execute(array($newpassword, $user_id));
			//echo $stmt->rowCount(); exit;
			if($stmt->rowCount())
				return false;
			else
				return true;
		}

		catch(PDOException $ex){
		    $result['success'] = false;
		    $result['message'] = "Change Password request failed! Please try after sometime.";
		}
		
	}


	function updateCredentials()
	{

		$apiUrl = "https://5a3db12020244ee2e56274dc0addfecb:7dfd66436f07538a43c806e1364f2b39@marvel-mii.myshopify.com/admin/customers/".$_REQUEST['customer_id'].".json";


		$customerData= array();
		$customerData['customer']['id'] = $_REQUEST['customer_id'];
		$customerData['customer']['password'] = $_REQUEST['newPassword'];
		$customerData['customer']['password_confirmation'] = $_REQUEST['newPassword'];

		$session = curl_init($apiUrl);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($session, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
		curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($customerData));
		curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
		curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
		curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

		$result = curl_exec($session);
		$response = json_decode($result);
		return $response;

	}




	





echo json_encode($result);



?> 
