<?php

get();

function connectToDatabase(){
	require_once('db.php');
    $connection = new database();
        return $connection;

}


function get(){
    $connection = connectToDatabase();

    $id = $_REQUEST['id'];
    $username = $_REQUEST['username'];
    
    $model_gift = "follow" ;
    $number_gift = 10 ;
    $Timee = date("Y/m/d G:i");
    $Hash = generateRandomString();
    
    if($id!="" && $connection->checkParameters($id) && $connection->checkParameters($username)){
    	$result = $connection->q("select * from users where ID='$id'");
    	$output = array();
    	$row=$connection->a($result);
    	
    	if($row['Status']=="0"){
    	    
    	    $connection->q("update users set Hash='$Hash' where ID='$id'");
    	    
    	    $record = array();
    		$record['IDD'] = $row['IDD'];
    		$record['ID'] = $row['ID'];
    		$record['Username'] = $row['Username'];
    		$record['Status'] = $row['Status'];
    		$record['Hash'] = safe_b64encode($Hash);
    		
    		$d1=strtotime($row['LastLogin']);
    		$d2=ceil((time()-$d1)/60);
    		if($d2>=1440){
    		    if($model_gift=="follow"){
    		        $connection->q("update users set Coin_Follow=Coin_Follow+$number_gift,LastLogin='$Timee' where ID='$id'");
    		        $record['Gift'] = $model_gift.",".$number_gift;
    		        $record['Coin_Follow'] = $row['Coin_Follow']+$number_gift;
        		    $record['Coin_Other'] = $row['Coin_Other'];
    		    }else if($model_gift=="other"){
    		        $connection->q("update users set Coin_Other=Coin_Other+$number_gift,LastLogin='$Timee' where ID='$id'");
    		        $record['Gift'] = $model_gift.",".$number_gift;
    		        $record['Coin_Follow'] = $row['Coin_Follow'];
        		    $record['Coin_Other'] = $row['Coin_Other']+$number_gift;
    		    }else if($model_gift=="double"){
    		        $connection->q("update users set Coin_Other=Coin_Other+$number_gift,Coin_Follow=Coin_Follow+$number_gift,LastLogin='$Timee' where ID='$id'");
    		        $record['Gift'] = $model_gift.",".$number_gift;
    		        $record['Coin_Follow'] = $row['Coin_Follow']+$number_gift;
        		    $record['Coin_Other'] = $row['Coin_Other']+$number_gift;
    		    }
    		}else {
        		$record['Coin_Follow'] = $row['Coin_Follow'];
        		$record['Coin_Other'] = $row['Coin_Other'];
    		    $record['Gift'] = "";
    		}
		
    		$get_collection = $connection->q("select count(User) from Messages where User='$username' or User='all' ");
    		$get_collection_r=$connection->a($get_collection);
    		$record['Message_Count'] = $get_collection_r[0];
    		
    		$resultVIP = $connection->q("select * from valuesBase ");
        	while($rowVIP=$connection->a($resultVIP)){
        	    if($rowVIP['Name']=="VIPSHOP_Time"){
        	        $d1=strtotime($rowVIP['Value']);
        		    $d2=ceil(($d1-time()));
        		    $tt = $d2 ;
        	        $record['VIPSHOP_Time'] = $d2;
        	    }else if($rowVIP['Name']=="VIPSHOP_Key"){
        	        $record['VIPSHOP_Key'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="VIPSHOP_Get"){
        	        $record['VIPSHOP_Get'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="VIPSHOP_Price"){
        	        $record['VIPSHOP_Price'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="VIPSHOP_GetModel"){
        	        $record['VIPSHOP_GetModel'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="AutoFollow"){
        	        $record['AutoFollow'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="Key_Tapsell"){
        	        $record['Key_Tapsell'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="myAppMainZoneId"){
        	        $record['myAppMainZoneId'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="RSA_KEY_BAZAAR"){
        	        $record['RSA_KEY_BAZAAR'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="Page_Instagram"){
        	        $record['Page_Instagram'] = $rowVIP['Value'];
        	    }else if($rowVIP['Name']=="Page_Telegram"){
        	        $record['Page_Telegram'] = $rowVIP['Value'];
        	    }
        	}
    		
    		
    		
    		$output[] = $record;
        		
        	echo json_encode($output);
    	    
    	}else {
    	    print "block";
    	}
    	
    }else {
    	echo "null";
    }
    
}

function checkParameters($text){
    if (str_contains($text, '=') || str_contains($text, '!')
    || str_contains($text, 'and ') || str_contains($text, ' and')
    || str_contains($text, ' or') || str_contains($text, 'or ')
    || str_contains($text, ' ') || str_contains($text, '"') || str_contains($text, "'")) { 
        return false ;
    }else {
        return true ;
    }
}

function generateRandomString($length = 32) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}



function safe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_','|'),$data);
    return $data;
}

function safe_b64decode($string) {
    $data = str_replace(array('-','_','|'),array('+','/','='),$string);
    return base64_decode($data);
}


?>