<?php
function removePTAG($string){
 return str_replace("</p>","",str_replace("<p>", "", $string));
}


function addCont(){
	global $DB;
	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
	$DB->table = $DB->pre."contact";		
	$DB->data  = $_REQUEST;
	$str = 'ERR';
	if($DB->dbInsert()){
		$str = 'OK';
	}
	return $str;
}
function frntUserLogin(){
	@session_start();
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND userName="'.mysql_real_escape_string($_REQUEST['uname']).'" AND password="'.base64_encode(mysql_real_escape_string($_REQUEST['pwd'])).'" AND isActivate="1"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if(count($D) > 0){
		$str = array('str'=>'OK');
		$_SESSION['userCode'] = $D['userCode'];
		$_SESSION['regID'] = $D['regID'];
		$_SESSION['name'] = $D['name'];
	}
	return json_encode($str);
}
function chkUser(){
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND userName="'.$_REQUEST['userName'].'"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if(count($D) > 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function chkRef(){
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND userCode="'.$_REQUEST['referenceNo'].'" AND isActivate="1"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if(count($D) <= 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function chkSide(){
	global $DB;
	if($_REQUEST['side'] == 'R'){
		$sql = 'SELECT * FROM '.$DB->pre.'side WHERE  refNo="'.$_REQUEST['referenceNo'].'" AND rightSide="'.$_REQUEST['side'].'"';
	}else{
		$sql = 'SELECT * FROM '.$DB->pre.'side WHERE refNo="'.$_REQUEST['referenceNo'].'" AND leftSide="'.$_REQUEST['side'].'"';
	}
	
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if(count($D) > 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function saveReg(){
	global $DB;
	$refNo = json_decode(chkRef());
	//$side = json_decode(chkSide());
	$epin = json_decode(chkEpin());
	$str = array('str'=>'U');
	//
	if($refNo->str == 'OK' && $epin->str == 'OK' ){
	
		$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
		$_REQUEST["password"] = base64_encode('123456');
		$_REQUEST["confirmPassword"] = base64_encode('123456');
		$_REQUEST['isActivate'] = '1';
		$DB->table = $DB->pre."user_reg";		
		$DB->data  = $_REQUEST;
		$str = array('str'=>'ERR');
		if($DB->dbInsert()){
			$regID = $DB->insertID;
			addUserCodeFront($regID);
			//addSide($_REQUEST['referenceNo'],$_REQUEST['side']);
			updateEpin();
			
			$str = array('str'=>'OK');
		}
	}
	return json_encode($str);
}

function addUserCodeFront($regID){
	global $DB;
	$DB->table = $DB->pre."user_reg";	
	$userCode = $regID;
	$DB->data  = array('userCode'=>'DSM'.$userCode);
	$DB->dbUpdate("regID='$regID'");
	addRefSide($_REQUEST['referenceNo'],$_REQUEST['side'],'DSM'.$userCode);
	
}

function addRefSide($refNo,$side,$userCode){
	global $DB;
	//$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1"';
	$where = '';
	//echo $side;
	if($side == 'R'){
			$where = ' AND rightSide="R"';
	}
	if($side == 'L'){
		$where = ' AND leftSide="L"';
	}
	 $sql = 'SELECT sideID FROM '.$DB->pre.'side WHERE upPlace="'.$refNo.'"'.$where;
	$D = $DB->dbRow($sql);
	//die;
	if($D['sideID'] == ''){
		$sqlChk = 'SELECT sideID FROM '.$DB->pre.'side WHERE userCode="'.$userCode.'"';
			$chk = $DB->dbRow($sqlChk);
			if($chk['sideID'] == ''){
				if($side == 'R'){
						$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
						$DB->table = $DB->pre."side";
						$DB->data  = array('rightSide'=>$side,'userCode'=>$userCode,'refNo'=>$refNo,'upPlace'=>$refNo,'dateAdded'=>date('Y-m-d H:i:s'));
						$DB->dbInsert();
				}
				if($side == 'L'){
						$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
						$DB->table = $DB->pre."side";
						$DB->data  = array('leftSide'=>$side,'userCode'=>$userCode,'refNo'=>$refNo,'upPlace'=>$refNo,'dateAdded'=>date('Y-m-d H:i:s'));
						$DB->dbInsert();
				}
			}
	}else{
		$sql = 'SELECT * FROM '.$DB->pre.'side WHERE upPlace="'.$refNo.'"'.$where;
		
		$D = $DB->dbRow($sql);
		display_childrenPlace($D['userCode'],$refNo,$userCode,$side);
	}
	
	
}

function display_childrenPlace($upPlace,$refNo,$userCode,$side){
	global $DB;
	$where = '';
	if($side == 'R'){
			$where = ' AND rightSide="R"';
	}
	if($side == 'L'){
		$where = ' AND leftSide="L"';
	}
	 $sql = 'SELECT sideID FROM '.$DB->pre.'side WHERE upPlace="'.$upPlace.'"'.$where;
	
	$D = $DB->dbRow($sql);
	if($D['sideID'] == ''){
		$sqlChk = 'SELECT sideID FROM '.$DB->pre.'side WHERE userCode="'.$userCode.'"';
			$chk = $DB->dbRow($sqlChk);
			if($chk['sideID'] == ''){
				if($side == 'R'){
						$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
						$DB->table = $DB->pre."side";
						$DB->data  = array('rightSide'=>$side,'userCode'=>$userCode,'refNo'=>$refNo,'upPlace'=>$upPlace,'dateAdded'=>date('Y-m-d H:i:s'));
						$DB->dbInsert();
				}
				if($side == 'L'){
						$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
						$DB->table = $DB->pre."side";
						$DB->data  = array('leftSide'=>$side,'userCode'=>$userCode,'refNo'=>$refNo,'upPlace'=>$upPlace,'dateAdded'=>date('Y-m-d H:i:s'));
						$DB->dbInsert();
				}
			}
	}else{
		$sql = 'SELECT * FROM '.$DB->pre.'side WHERE upPlace="'.$upPlace.'"'.$where;
		$D = $DB->dbRow($sql);
		display_childrenPlace($D['userCode'],$refNo,$userCode,$side);
	}
}
function countBinary(){
		global $DB;
		$sql = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1"';
		$U = $DB->dbRows($sql);
		foreach($U as $U){
			echo $U['userCode'].'---'.checkBinary($U['userCode'],'0','0').'</br>';
		}

}
function checkBinary1111($userCode,$level,$cnt){ 
	global $DB;
	$levelTwo = '';
	$levelThird = 0;
	$levelFourth = 0;
	$query = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$userCode.'" AND isActivate="1"';
	$D = $DB->dbRows($query);
	$levelTwo = count($D);
		foreach($D as $D){ //level third
		if($D['userCode']){
			 $queryT = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$D['userCode'].'" AND isActivate="1"';
			$T = $DB->dbRows($queryT);
			$levelThird += count($T);				
			foreach($T as $T){ //level four
				if($T['userCode']){
					$queryF = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$T['userCode'].'" AND isActivate="1"';
					$F = $DB->dbRows($queryF);
					$levelFourth += count($F);
				}
			}
		}

	}
	
   
    return $levelTwo.'---'.$levelThird.'-----'.$levelFourth; 
}
function checkBinary123456($userCode,$level){
	global $DB;
	$count = 0;
	$query = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$userCode.'" AND isActivate="1"';
	$D = $DB->dbRows($query);
	foreach($D as $D){
		$count++;
		checkBinary($D['userCode'],$level++);
	}
	
   
    return $count.'---'.$level; // it will return all user_id count under parent_id
}
function display_children($parent,$level) {
		
	  $sql = 'SELECT userCode FROM mx_side'.' WHERE refNo="'.$parent.'" AND (rightSide="R" OR leftSide ="L")';
     $result = mysql_query($sql);
	 // mysql_num_rows($result).'--------'.$level.'</br>';
	  $rows =  mysql_num_rows($result);
	 
	 if($rows == 2){
			echo $rows.'---'.$level.'<br>';
			while ($row = mysql_fetch_array($result))
			{
					$level++;
				   //$var = str_repeat(' ',$level).$row['userCode']."\n";
		
						   //echo $var  after remove comment check tree
		
						   // i call function in while loop until count all user_id 
					
				   //$count += 1 +display_children($row['userCode'],$level );
				   display_children($row['userCode'],$level );
		
			}
	 }
	  
   // return $count.'------'.$level; // it will return all user_id count under parent_id
}
function chkActMail(){
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND email="'.$_REQUEST['email'].'" AND isActivate="1"';
	$DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($DB->numRows > 0){
		//$D = $DB->dbRow($sql);
		//sendPassword($D,$_REQUEST['email']);
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function sendPassword($email){
	
	
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND email="'.$_REQUEST['email'].'" AND isActivate="1"';
	$DB->dbRow($sql);
	$str = array('str'=>'ERR');
	//echo $DB->numRows;
	if($DB->numRows > 0){
		$D = $DB->dbRow($sql);
		$str = array('str'=>'OK');
	require("class.phpmailer.inc.php");	
		
		$mail = new PHPMailer();
		$msg = '';
		$mail->From = trim('admin@shrisamarthinfo.com');
		$mail->FromName = "Shri Samarth Info";
		$mail->AddAddress($email);
		$mail->Subject = "Password";
		$msg .= '<table><tr><th>Name</th><td>'.$D['name'].'</td></tr>';
		$msg .= '<tr><th>User Code</th><td>'.$D['userCode'].'</td></tr>';
		$msg .= '<tr><th>Password</th><td>'.base64_decode($D['password']).'</td></tr>';
		$msg .= '</table>';
		 $mail->Body = $msg;
		
		$mail->ContentType = "text/html";
		$mail->Send() or die('err');
		$str = 'OK';
	
	}
	return json_encode($str);
}
function checkDirect(){
	global $DB;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND userCode="'.$_REQUEST['userName'].'" AND isActivate="1"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if(count($D) <= 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function chkEpin(){
	global $DB;
	 $sql = 'SELECT * FROM '.$DB->pre.'epin WHERE status="1" AND ePin="'.$_REQUEST['ePin'].'" AND isUsed="0"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if(count($D) > 0){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function updateEpin(){
	global $DB;
	$sql = 'UPDATE '.$DB->pre.'epin SET isUsed="1" WHERE ePin="'.$_REQUEST['ePin'].'"';
	$DB->dbQuery($sql);
}
if($_REQUEST["xAction"]) {
	@session_start();
	switch($_REQUEST["xAction"]) {
		
		
		case 'cont':
			include("../connectdb.inc.php");
			echo addCont();
		break;
		case 'eventReg':
			include("../connectdb.inc.php");
			echo eventRegister();
		break;
		
		case 'chkUser':
			include("../connectdb.inc.php");
			echo chkUser();
		break;
		case 'chkRef':
			include("../connectdb.inc.php");
			echo chkRef();
		break;
		case 'chkSide':
			include("../connectdb.inc.php");
			echo chkSide();
		break;
		case 'saveReg':
			include("../connectdb.inc.php");
			echo saveReg();
		break;
		case 'login':
			include("../connectdb.inc.php");
			echo frntUserLogin();
		break;
		case 'chkActMail':
			include("../connectdb.inc.php");
			echo chkActMail();
		break;
		case 'sendPass':
			include("../connectdb.inc.php");
			echo sendPassword($_REQUEST['email']);
		break;
		case 'chkDirect':
			include("../connectdb.inc.php");
			echo checkDirect();
		break;
		case 'chkChild':
			include("../connectdb.inc.php");
			echo display_children('SS124391','1');
		break;
		case 'chkEpin':
			include("../connectdb.inc.php");
			echo chkEpin();
		break;
		
		
	}
}

?>