<?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 drawTime(){
	global $DB;
	date_default_timezone_set('Asia/Kolkata');

$date = time();

$lotMin = date("i", $date);
$lotHour = date("h", $date);
$lotSec = date("s", $date);
$ampm = date("A", $date);



if ($lotMin >= 0 && $lotMin < 15) {
    $lotMin = 15;
} else if ($lotMin >= 15 && $lotMin < 30) {
    $lotMin = 30;
} else if ($lotMin >= 30 && $lotMin < 45) {
    $lotMin = 45;
} else if ($lotMin >= 45 && $lotMin < 60) {
    $lotMin = 0;
    $lotHour++;
}

$lotHour = $lotHour % 12;
if ($lotHour == 0) {
    $lotHour = 12;
}

if ($lotHour < 10) {
    $lotHour = "0" . $lotHour;
}
if ($lotMin < 10) {
    $lotMin = "0" . $lotMin;
}
// if ($lotSec < 10) {
//     $lotSec = "0" . $lotSec;
// }

$time_lot =  "$lotHour:$lotMin";
return $time_lot;
}
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'];
		$_SESSION['dateAdded'] = $D['dateAdded'];
		$_SESSION['expDate'] = $D['expDate'];
 	}
	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'].'" ';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK','name'=>$D['name']);
	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(){
//	sendSMS('8');die;
	global $DB;
	$refNo = json_decode(chkRef());
	//$side = json_decode(chkSide());
	//$epin = json_decode(chkEpin());
	$str = array('str'=>'U');
	$date = date('Y-m-d');
	$expDate = '';
	if($refNo->str == 'OK'){
	
		$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
		$pass = base64_encode('1234');
		$_REQUEST["password"] = $pass;
		$_REQUEST["confirmPassword"] = $pass;
		$_REQUEST['referenceNo'] = strtoupper($_REQUEST['referenceNo']);
		
		
		$_REQUEST['tranPassword'] = base64_encode(generateKey(5));
		$_REQUEST['isActivate'] = '0';
		$DB->table = $DB->pre."user_reg";		
		$DB->data  = $_REQUEST;
		$str = array('str'=>'ERR');
		if($DB->dbInsert()){
			$regID = $DB->insertID;
			addUserCodeFront($regID,$pass);
			//sendSMS($regID);
			$html = 'User Name :- '.$_SESSION['showUserCode'];
			$html .= '</br>Password :-'.base64_decode($pass);
			$str = array('str'=>'OK','html'=>$html);
		}
	}
	return json_encode($str);
}

function addOrder($regID){
	global $DB;
	@session_start();
	$productTile = '';
	if($_REQUEST['typeID'] == '2'){
		$productTile = $_REQUEST['product'];
	}else{
		$productTile = 'Sanitary Pad';
	}
	$DB->table = $DB->pre."order";		
	$DB->data  = array('userCode'=>$_SESSION['showUserCode'],'regID'=>$regID,'product'=>$productTile,'dateAdded'=>date('Y-m-d H:i:s'));
	$DB->dbInsert();
}
function addUserCodeFront($regID,$pass){
	global $DB;
	@session_start();
	$DB->table = $DB->pre."user_reg";	
	$userCode = substr(rand(),0,2).$regID;
	$DB->data  = array('userCode'=>'EHM'.$userCode,'userName'=>'EHM'.$userCode,'password'=>$pass,'confirmPassword'=>$pass);
	$DB->dbUpdate("regID='$regID'");
	$_SESSION['showUserCode'] = 'EHM'.$userCode;
	addRefSide($_REQUEST['referenceNo'],$_REQUEST['side'],'EHM'.$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 sendSMS($regID){
      global $DB;
    $sender="IDLTRD"; // approved sender id
$domain="http://sms.ssinfomate.in/API/WebSMS/Http/v1.0a/index.php"; // connecting url 
$route="257";// 0-Normal,1-Priorityfas
$method="POST";


		$sql = 'SELECT mobile,userName,password,name,tranPassword FROM '.$DB->pre.'user_reg WHERE status="1" AND regID="'.$regID.'"';
        $M = $DB->dbRow($sql);
	
		
		$mobile=$M['mobile'];

	//$message = "Dear%20Customer%2C%0A%0AYou%20have%20been%20successfully%20registered.Your%20Userid%3A%23VAL%23%20and%20Password%3A%23VAL%23.Thanks";
	$message='Dear '.$M['name'].',You are successfully registered with us.Your ID: '.$M['userName'].' and Password: '.base64_decode($M['password']).' Update Profile visit: www.idealtrading.in';

	$uid=urlencode($uid);
	$pin=urlencode($pin);
	$sender=urlencode($sender);
	 $message=($message);
	//101106
	
	$route = "256";
//Prepare you post parameters
$postData = array(
	'username'=>'RVUMKT',
	'password'=>'123456',
    'to' => $mobile,
    'message' => $message,
    'sender' => $sender,
    'route' => "257",
	'reqid'=>'1',
	'sendondate'=>date('d-m-Y H:i:s'),
	'format' =>'{json|text}',

);

//API URL
$url="203.129.225.68/API/WebSMS/Http/v1.0a/index.php";

// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
    //,CURLOPT_FOLLOWLOCATION => true
));


//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);


//get response
$output = curl_exec($ch);

//print_r($output);
//Print error if any
if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}

curl_close($ch);

	
	
	
		
		

}
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 sendForgotPassword(){
	global $DB;
	$sql = 'SELECT mobile,name,password,tranPassword,userName FROM '.$DB->pre.'user_reg WHERE status="1" AND userCode="'.$_REQUEST['passUserCode'].'"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($D['mobile'] !=''){
		$sender="DSMMNE"; // approved sender id
		$method="POST";


			
		
		$mobile=$M['mobile'];
	//$message = "Dear%20Customer%2C%0A%0AYou%20have%20been%20successfully%20registered.Your%20Userid%3A%23VAL%23%20and%20Password%3A%23VAL%23.Thanks";
	$message = '';
	if($_REQUEST['passType'] == 'U'){
		$message='Dear '.$D['name'].',Your ID:'.$D['userName'].' and Password:'.base64_decode($D['password']).' Update Profile visit:www.dsmmnetwork.com';
	}
	if($_REQUEST['passType'] == 'B'){
		$message='Dear '.$D['name'].',Your ID:'.$D['userName'].' and Password:'.base64_decode($D['password']).' AND Transaction Password:'.base64_decode($D['tranPassword']).' Update Profile visit:www.dsmmnetwork.com';
	}
	if($_REQUEST['passType'] == 'T'){
		$message='Dear '.$D['name'].',Your ID:'.$D['userName'].'Transaction Password:'.base64_decode($D['tranPassword']).' Update Profile visit:www.dsmmnetwork.com';
	}
  	 $url = 'http://dnd.saakshisoftware.com/api/mt/SendSMS?user=DSMM&password=123456&senderid='.urlencode($sender).'&channel=trans&DCS=0&flashsms=0&number='.$D['mobile'].'&text='.urlencode($message).'&route=15';
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);

	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 0);	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	$data = curl_exec($ch);
	$js = json_decode($data);
	curl_close($ch);
	if($js->ErrorMessage == 'Done'){
		$str = array('str'=>'OK');
	}
	}
	return json_encode($str);
}

function chkMob(){
	global $DB;
	$sql = 'SELECT count(regID) as c FROM '.$DB->pre.'user_reg WHERE status="1" AND mobile="'.$_REQUEST['mobile'].'"' ;
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if($D['c'] > 2){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function chkPan(){
	global $DB;
	$sql = 'SELECT count(regID) as c FROM '.$DB->pre.'user_reg WHERE status="1" AND pan="'.$_REQUEST['pan'].'"' ;
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if($D['c'] > 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}
function chkAdhar(){
	global $DB;
	$sql = 'SELECT count(regID) as c FROM '.$DB->pre.'user_reg WHERE status="1" AND adhar="'.$_REQUEST['adhar'].'"' ;
	$D = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if($D['c'] > 2){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
}

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 'forPass':
			include("../connectdb.inc.php");
			echo sendForgotPassword();
		break;
		case 'chkMob':
			include("../connectdb.inc.php");
			echo chkMob();
		break;
		
		case 'chkPan':
			include("../connectdb.inc.php");
			echo chkPan();
		break;
		case 'chkAdhar':
			include("../connectdb.inc.php");
			echo chkAdhar();
		break;
		
		
	}
}

?>