<?php

header('Content-Type: application/json');
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(1);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");

date_default_timezone_set('Asia/Kolkata');
set_time_limit ( 300 );
ob_start();



//var_dump($data['refer_id']);


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

if($validationErr != '') {

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

} 

/*
$headers = apache_request_headers();

if($headers['enc_text'] != "") {
	$enc_text = $headers['enc_text'];
} 
*/


class member_sign_up{
    //global $sugar_config;

    public $sessionId;
	public $username;
	public $password;
	public $uri;
	public $soap_url;
	public $sugarClient;
	public $updateID;
	public $conn;
	public $db;
	public $google_api_key;
	public $google_api_path;
	public $ios_api_path;
	public $ios_port;
	public $ios_certificate;
	public $ios_passphrase;
	public $SMS_Message;
	
	function __construct() {
		global $sugar_config;

		$host_name= "localhost";
		$db_name = "mybooster";
		$user_name = "root";
		$password = "";

		$this->db = mysql_connect($host_name, $user_name, $password);
		mysql_select_db($db_name);		

		if(!$this->db) {
			$result = array();
			$result['status'] = '0';
			$result['message'] = "APP temporarily Unavailable!";
			print $return = json_encode($result);
			//exit;
		}

    }
 

	public function verifyReferID($registrationID) {
		
		$sqlSelect 	= "SELECT id FROM my_registration WHERE refer_id = '".$registrationID."'";
		$sqlResult 	= mysql_query($sqlSelect);
		$sqlRow		= mysql_fetch_assoc($sqlResult);
		return $sqlRow;
    }
	

	public function get_user_data( $registrationID ) {
		
		$sqlSelect 	= "SELECT * FROM my_registration WHERE refer_id = '".$registrationID."'";
		$sqlResult 	= mysql_query($sqlSelect);
		$sqlRow		= mysql_fetch_assoc($sqlResult);

		if($sqlRow['sponsor_by'] == null) {
			$sqlRow['sponsor_by'] = '';
		}
		return $sqlRow;
    }
	
	public function get_tree_data( $refer_id ) {
		$treeData = array();
		$sqlSelect 	= "SELECT id, name, last_name, mobile_number, sponsor_by, left_node, right_node FROM my_registration WHERE sponsor_id = '".$refer_id."'";
		$sqlResult 	= mysql_query($sqlSelect);
		while($sqlRow		= mysql_fetch_assoc($sqlResult)) {
			array_push($treeData, $sqlRow);
		}

		return $treeData;
	}

	public function member_package( $refer_id ) {
		
	     return 0;

    }
}
 
$conn 		= new member_sign_up();


$result 	= array();

$moduleName = "my_registration";

	if($conn->verifyReferID(($_REQUEST['refer_id'])))
	{	
		$userData = array();
		$userData = $conn->get_user_data($_REQUEST['refer_id']);
		$treeStrc = $conn->get_tree_data($_REQUEST['refer_id']);
		$packageCnt = $conn->member_package($_REQUEST['refer_id']);
		$userData['tree_data'] 			= $treeStrc;


		if($userData !=NULL)
		{
			$result['status'] 				= '1';
			$result['message'] 				= 'Success!';
			$result['user_info'] 			= $userData;
			$result['package_count'] 		= $packageCnt;
		}
		 else {

			$result['status'] = '0';
			$result['message'] = 'Invalid refer id!';

		}

	}


	else {

			$result['status'] = '0';
			$result['message'] = 'Invalid refer id!';

		}




print $result = json_encode($result);

?> 


