<?php

include "bank.php";

$tipe = sanitize($_GET['type']);

	if($tipe == "mobileCheck"){
		
		$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

		if(isset($_GET['mobile'])){
				$mobile = sanitize($_GET['mobile']);
				$sql = "SELECT * FROM `users` WHERE  mobile = '$mobile' ";
				$result = $conn->query($sql);
				$pushid =  sanitize($_GET['pushid']);
				$R = rand(1111,9999);
				$token = md5(time() + $R);
				$upto = time() + 259200;
				$randi = "1111"; 
				$sdata = "کد تائید شما ".$randi." می باشد";
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();
					$conn->query("UPDATE users SET token='".$token."', pushid='".$pushid."', passcode=$randi, uptodate='".$upto."' WHERE id='".$row['id']."' ");
					$data = array("HaveData" => "1" ,"msg" => "پیامکی حامل کد تائید برای شما ارسال شد" , "helpBar"=>$actual_link);
					echo json_encode($data, JSON_UNESCAPED_UNICODE);
					
					sendSms($mobile,$sdata);  
				} else {
					sendSms($mobile,$sdata);  
					$sql = "INSERT INTO users (mobile,  mood, uptodate, passcode, pushid) VALUES ('".$mobile."','0','".$upto."','".$randi."','".$pushid."')";
					$conn->query($sql);
					
					
					$data = array("HaveData" => "0" ,"msg" => "پیامکی حامل کد تائید برای شما ارسال شد" , "helpBar"=>$actual_link );
						echo json_encode($data );
				}
				echo(mysqli_error($conn));
			 }
		
	}elseif($tipe == "verfy"){
					$mobile = sanitize($_GET['mobile']);
					$code = sanitize($_GET['code']);
				$sql = "SELECT * FROM `users` WHERE  mobile =".$mobile."  AND passcode = ".$code;
				$result = $conn->query($sql);
				$row = $result->fetch_assoc();
				if ($result->num_rows > 0) {
					$R = rand(1111,9999);
					$token = md5(time() + $R);
					$upto = time() + 259200;
					$conn->query("UPDATE users SET token='".$token."', uptodate='".$upto."' WHERE id='".$row['id']."' ");
					
					if($row['mood'] == 0){
							echo json_encode(array('newUser' => "1" ,'stats' => "you are logined!" , 'token' => $token));
					}else{						
							echo json_encode(array('newUser' => "0" ,'stats' => "you are logined!" , 'token' => $token));						
					}
					
					
					
					
				}else{
					$data = array('stats' => "کد وارد شده صحیح نیست");
						echo json_encode($data );
				}
		
		
		
	}elseif($tipe == "profile" ){
		$token =  sanitize($_GET['token']);
		$name =  sanitize($_GET['name']);
		$finder =  sanitize($_GET['finderid']-1000);
		
		$sql = "SELECT * FROM `users` WHERE  token='".$token."' ";
				$result = $conn->query($sql);
				$row = $result->fetch_assoc();
				if ($result->num_rows > 0){
					
				$st = time();
				$upto = time() + 259200;
    			$exp = $st + 259200;
				$randi = "1111";
    			$numbers = '["985000145","985000949","98100009","98sim","985000415"]';
    			$data = array('stats' => 1 ,'msg'=>'کاربر با موفقیت بروز شد');
					
					$sql = "UPDATE users SET name='".$name."', st='".$st."', mood=1, exp='".$exp."', numbers='".$numbers."', credit='1000', finderid='".$finder."', uptodate='".$upto."' WHERE token='".$token."' ";
				
						if ($conn->query($sql) === TRUE) {
							echo json_encode($data );
						//	sendSms($mobile,$data);
							
							if($fp > 0){
								pushsend("تبریک ","کاربر با شماره  ".hid($row['mobile'])." به زیر مجموعه مشتریان شما افزوده شد و از خریدهای این کاربر شما نیز سود خواهید کرد.",array($fp));
							}
						} else {
							$data = array('stats' => 0 , 'msg' => 'مشکل در دیتا بیس' );
							
						}
						
					 echo(mysqli_error($conn));
				}else{				
					
				}
		
		
		
		
	}elseif($tipe == "delMe" ){
		$sql = "DELETE FROM users WHERE mobile=09032926441";

		if ($conn->query($sql) === TRUE) {
			echo "Record deleted successfully";
		} else {
			echo "Error deleting record: " . $conn->error;
		}
		
	}elseif($tipe == "sim" ){
		$send = sanitize($_GET['send']);
		$sql = "UPDATE users SET sendsim='".$send."' WHERE mobile=09032926441";

		if ($conn->query($sql) === TRUE) {
			echo "this is ready";
		} else {
			echo "Error deleting record: " . $conn->error;
		}
		
	}
 //sanitize($_POST['password']
	else{
		$data = array('stats' => 0 , 'msg' => 'فرمت اطلاعات نا معتبر' );
		
	}
	
	
	