<?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['userID']) || $_REQUEST['userID'] == ''){
		$validationErr = "Please login to system again!";
	} else if(!isset($_REQUEST['userName']) || $_REQUEST['userName'] == '') {
		$validationErr = "Please login to system again!";
	} else if(!isset($_REQUEST['currentPassword']) || $_REQUEST['currentPassword'] == '') {
		$validationErr = "Please Enter Current Password!";
	} else if(!isset($_REQUEST['currentPassword']) || $_REQUEST['currentPassword'] == '') {
		$validationErr = "Please Enter Current Password!";
	} else 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!";
	} else if($_REQUEST['currentPassword'] == $_REQUEST['newPassword']) {
		$validationErr = "Current and New Password cannot 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', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$userID          = $_REQUEST['userID'];
$userName        = $_REQUEST['userName'];
$currentPassword = md5($_REQUEST['currentPassword']);
$newPassword     = md5($_REQUEST['newPassword']);
$result = array();


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

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


	else
	{
		$result['success'] = false;
		$result['message'] = "Please enter a valid current password!";
	}

	
	function matchPassword($userID, $userName, $currentpassword, $db)
	{
		try
		{
			$stmt = $db->prepare("select firstname from my_admin_users where id=? AND username=? AND password=?");
			$stmt->execute(array($userID, $userName, $currentPassword));
			//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 updatePassword($userID, $userName, $newPassword, $currentPassword, $db)
	{
		try
		{
			$stmt = $db->prepare("update my_admin_users set password=? where id=? AND username=? AND password=?");
				$stmt->execute(array($newPassword, $userID,  $userName, $currentPassword));
				//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.";
		}

	}


echo json_encode($result);



?> 
