<?php

include '../config.php';


$firstname = $_POST['fullname'];

$lastname = "";

$user_type = $_POST['user_type'];

$country = $_POST['country'];

$contactno = $_POST['contactno'];

$email = $_POST['email'];

$password = $_POST['password'];

$language = $_POST['language'];

$langvalue = $language.'_label';

if ($firstname && $user_type && $country && $contactno && $email && $password) {

if($user_type == '2')
{
	$text = "Corporate Service Provider";
}
if($user_type == '3')
{
	$text = "Corporate Client";
}
if($user_type == '4')
{
	$text = "Individual Service Provider";
}
if($user_type == '5')
{
	$text = "Individual Client";
}
$label = mysql_query("select * from lts_label_managment where english_identifier='".$text."'");
$labelname = mysql_fetch_array($label);
	
	if($user_type=='2' || $user_type=='3') {
		$result = mysql_query("select * from lts_user_master where email = '" . $email . "' and user_type = '" . $user_type . "'");
	} else {
		$result = mysql_query("select * from lts_user_master where (email = '" . $email . "' or contactno = '".$contactno."') and user_type = '" . $user_type . "'");
	}

	if (mysql_num_rows($result) > 0)
	{
		$user_data_delete=mysql_fetch_assoc($result);
		if($user_data_delete['is_deleted']=='1')
		{
			$userid=$user_data_delete['user_id'];
			// for message send
			$getmobno = mysql_fetch_array(mysql_query("select contactno from lts_user_master where user_id=".$userid.""));
	        $getcountry = mysql_query("select country_code from lts_country where id=".$country.""); 
	        $getlang = mysql_fetch_array(mysql_query("select identifier from lts_language where id = (select language from lts_device where user_id = ".$userid.")"));
	        $lang = $getlang['identifier'];

	        $getlangval = mysql_fetch_array(mysql_query("select ".$langvalue." from lts_label_managment where english_identifier = 'Your Verification code is {verification_code}. Please use this code to verify your LTS Account'"));
	        $valmsg  = $getlangval[$langvalue];

	        $getnamecountry = mysql_fetch_array($getcountry);

	        if($user_type == '4' || $user_type == '5')
	        {
	        	$confirmation_code = rand(1111,9999);
		        $fival = str_replace('{verification_code}',$confirmation_code,$valmsg);
		        $phn = $getnamecountry['country_code'].$contactno;
		        $data = array('to_phone' => $phn, 'code' => $fival);
				$ch = curl_init(); // initiate curl
				$url = SITE_URL."/sendsms.php"; // where you want to post data
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_POST, true); // tell curl you want to post something
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // define what you want to post
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
				$output = curl_exec($ch); // execute
				$status = '0';
				if($output)
				{
					$updatedata = mysql_query("UPDATE lts_user_master SET firstname='".$firstname."', lastname='".$lastname."', contactno='".$contactno."', password='".md5($password)."',  account_verification_code='".$confirmation_code."', is_deleted='0' WHERE user_id='".$userid."'");
				}
				// end for message send
				$newmsg = 'Registration successfully Completed. Please Enter Verification Code that Sent to your registered number';
	        }
	        else
	        {
	        	$key = '';
				$userid=$user_data_delete['user_id'];
				$keys = array_merge(range(0, 9), range('a', 'z'));
				for ($i = 0; $i < 6; $i++) {
					$key .= $keys[array_rand($keys)];
				}
				$code = $key;
				$q = "user_id=$userid&code=$code";
				$link = SITE_URL."/?q=".base64_encode($q);
				mysql_query("UPDATE lts_user_master SET firstname='".$firstname."', lastname='".$lastname."', password='".md5($password)."', account_verification_code='" . $code . "', is_deleted='0' , active_status = 0 ,account_verify_status = 0, notification_status = 0  WHERE user_id='" . $userid . "'");
				$tag_array = array("{SiteTitle}", "{UserId}", "{FirstName}", "{LastName}", "{Usertype}", "{Email}", "{Password}", "{VerificationLink}", "{IpAddress}", "{SiteURL}");
				$str_array = array(SITE_TITLE, $userid, $firstname, "", $labelname[$language.'_label'], $email, $password, $link, $_SERVER['REMOTE_ADDR'], SITE_EMAIL);
				$mail_template = mysql_fetch_assoc(mysql_query("select * from  lts_email_template where template_id='1'"));
				if ($mail_template['active_status'] == 1) 
				{
					$mail_subject = str_replace($tag_array, $str_array, $mail_template[$language.'_'.'subject']);
					$mail_message = str_replace($tag_array, $str_array, $mail_template[$language.'_'.'message']);
					$to = $email;
					$from = $sitedata['smtp_mailfrom'];
					$body = $mail_message;
					$subject = $mail_subject;
					$headers  = "From: LTS < $from > \n";
					$headers .= "Content-type: text/html;charset=UTF-8\r\n";
		                        $headers .= "X-Priority: 1\r\n";
		                        $headers .= "X-MSMail-Priority: High\r\n";
		                        $headers .= "X-Mailer: Just My Server\r\n";
					$sentmail = @mail($to, $subject, $body, $headers);
				}
				$newmsg = 'Registration successfully. Please check your mail for active your account.';
	        }
		        
			//mysql_query("update lts_post_inquiry set is_deleted='0' where user_id='".$userid."'");
			//mysql_query("update lts_bid_details set active_status='1' where u_id='".$userid."'");
			
			$response['response'] = '1';
			$response['message'] = $newmsg;
			$response['Registration_id'] = $userid;
		}
		else
		{

			$response['response'] = '2';

			if($user_type=='2' || $user_type=='3'){
				$response['message'] = 'Email id is already registered.';
			} else {
				$response['message'] = 'Email id or Contact Number is already registered.';
			}

			$response['Registration_id'] = "0";
			
		}

	} else {

		mysql_query("insert into lts_user_master set

            firstname = '" . $firstname . "',

            lastname='" . $lastname . "',

            country='" . $country . "',

            contactno = '" . $contactno . "',

            email = '" . $email . "',

            password = '" . md5($password) . "',

            register_date = '" . date("Y-m-d H:i:s") . "',

            expire_date = '" . date('Y-m-d',strtotime('+1 year')) . "',

            updated_datetime = '" . date("Y-m-d H:i:s") . "',

            register_ipaddress = '" . $_SERVER['REMOTE_ADDR'] . "',

            user_type = '" . $user_type . "',

            active_status='0',

            account_verify_status='0'

            ");

		$userid = mysql_insert_id();



		mysql_query("insert into lts_device set

					user_id = '".$userid."' ,

					push_type = '0' ,

					push_status = '1',

					status = '1'

					");



		if ($user_type == 5) {
		//if(1==1) {
			// for message send
				$getmobno = mysql_fetch_array(mysql_query("select contactno from lts_user_master where user_id = ".$userid.""));
		        $getcountry = mysql_query("select country_code from lts_country where id = (select country from lts_user_master where user_id = ".$userid.") "); 
		        $getlang = mysql_fetch_array(mysql_query("select identifier from lts_language where id = (select language from lts_device where user_id = ".$userid.")"));
		        $lang = $getlang['identifier'];
		        $getlangval = mysql_fetch_array(mysql_query("select ".$langvalue." from lts_label_managment where english_identifier = 'Your Verification code is {verification_code}. Please use this code to verify your LTS Account'"));
		        $valmsg  = $getlangval[$langvalue];

		        $getnamecountry = mysql_fetch_array($getcountry);
		        $confirmation_code = rand(1111,9999);
		        $fival = str_replace('{verification_code}',$confirmation_code,$valmsg);
		        $phn = $getnamecountry['country_code'].$getmobno['contactno'];
		        $data = array('to_phone' => $phn, 'code' => $fival);
		        
		      $ch = curl_init(); // initiate curl
		      $url = "http://asitji.com/lts/sendsms.php"; // where you want to post data
		      curl_setopt($ch, CURLOPT_URL, $url);
		      curl_setopt($ch, CURLOPT_POST, true); // tell curl you want to post something
		      curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // define what you want to post
		      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
		      $output = curl_exec($ch); // execute
		      $status = '0';
		      if($output)
		      {
		        $updatedata = mysql_query("UPDATE lts_user_master SET account_verification_code='".$confirmation_code."' WHERE user_id='".$userid."'");
		      }
			// end for message send

		       $newmsg = 'Registration successfully Completed. Please Enter Verification Code that Sent to your registered number';

		}

		if($user_type == 4 || $user_type == 5){

			mysql_query("UPDATE lts_user_master set
                                    person_name = '" . $_POST['fullname'] . "',
                                    person_country = '" . $country . "',
                                    person_contact = '" . $contactno . "',
                                    person_email = '" . $email . "'
                                    WHERE user_id = '" . $userid . "'
                        ");

			if($user_type == 5) {

				$response['response'] = '1';

				$response['message'] = 'Registration successfully Completed. Please Enter Verification Code that Sent to your registered number';

				$response['Registration_id'] = $userid;

			} else {


				// for message send
				$getmobno = mysql_fetch_array(mysql_query("select contactno from lts_user_master where user_id = ".$userid.""));
		        $getcountry = mysql_query("select country_code from lts_country where id = (select country from lts_user_master where user_id = ".$userid.") "); 
		        $getlang = mysql_fetch_array(mysql_query("select identifier from lts_language where id = (select language from lts_device where user_id = ".$userid.")"));
		        $lang = $getlang['identifier'];
		        $getlangval = mysql_fetch_array(mysql_query("select ".$langvalue." from lts_label_managment where english_identifier = 'Your Verification code is {verification_code}. Please use this code to verify your LTS Account'"));
		        $valmsg  = $getlangval[$langvalue];

		        $getnamecountry = mysql_fetch_array($getcountry);
		        $confirmation_code = rand(1111,9999);
		        $fival = str_replace('{verification_code}',$confirmation_code,$valmsg);
		        $phn = $getnamecountry['country_code'].$getmobno['contactno'];
		        $data = array('to_phone' => $phn, 'code' => $fival);
		      $ch = curl_init(); // initiate curl
		      $url = "http://asitji.com/lts/sendsms.php"; // where you want to post data
		      curl_setopt($ch, CURLOPT_URL, $url);
		      curl_setopt($ch, CURLOPT_POST, true); // tell curl you want to post something
		      curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // define what you want to post
		      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
		      $output = curl_exec($ch); // execute
		      $status = '0';
		      if($output)
		      {
		        $updatedata = mysql_query("UPDATE lts_user_master SET account_verification_code='".$confirmation_code."' WHERE user_id='".$userid."'");
		      }
			// end for message send

		       $newmsg = 'Registration successfully Completed. Please Enter Verification Code that Sent to your registered number';

				$response['response'] = '1';

				$response['message'] = $newmsg;

				$response['Registration_id'] = $userid;

			}

		} else {

			mysql_query("UPDATE lts_user_master set

                                frim_name = '" . $_POST['fullname'] . "',

                                frim_country = '" . $country . "',

                                frim_contact = '" . $contactno . "',

                                frim_email = '" . $email . "'

                                WHERE user_id = '" . $userid . "'

                                ");

				$key = '';
				$keys = array_merge(range(0, 9), range('a', 'z'));
				for ($i = 0; $i < 6; $i++) {
					$key .= $keys[array_rand($keys)];
				}
				$code = $key;
				$q = "user_id=$userid&code=$code";
				$link = SITE_URL."/?q=".base64_encode($q);
				mysql_query("UPDATE lts_user_master SET account_verification_code='" . $code . "' WHERE user_id='" . $userid . "'");
				$tag_array = array("{SiteTitle}", "{UserId}", "{FirstName}", "{LastName}", "{Usertype}", "{Email}", "{Password}", "{VerificationLink}", "{IpAddress}", "{SiteURL}");
				$str_array = array(SITE_TITLE, $userid, $_POST['fullname'], "", $labelname[$language.'_label'], $email, $password, $link, $_SERVER['REMOTE_ADDR'], SITE_EMAIL);
				$mail_template = mysql_fetch_assoc(mysql_query("select * from  lts_email_template where template_id='1'"));
				if ($mail_template['active_status'] == 1) {
					$mail_subject = str_replace($tag_array, $str_array, $mail_template[$language.'_'.'subject']);
					$mail_message = str_replace($tag_array, $str_array, $mail_template[$language.'_'.'message']);
					$to = $email;
					$from = $sitedata['smtp_mailfrom'];
					$body = $mail_message;
					$subject = $mail_subject;
					$headers  = "from: LTS < $from > \n";
					$headers .= "Content-type: text/html;charset=UTF-8\r\n";
			        $headers .= "X-Priority: 1\r\n";
			        $headers .= "X-MSMail-Priority: High\r\n";
			        $headers .= "X-Mailer: Just My Server\r\n";
					$sentmail = @mail($to, $subject, $body, $headers);
				}
				
				$response['response'] = '1';

				$response['message'] = 'Registration successfully. Please check your mail for active your account.';

				$response['Registration_id'] = $userid;

		}

	}

} else {

	$response['response'] = '0';

	$response['message'] = ' Parameters missing';

	$response['Registration_id'] = "0";

}

echo json_encode($response);

?>