<?php

add();

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

}


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

    $id = $_REQUEST['id'];
    $Hash = $_REQUEST['Hash'];
    $key = $_REQUEST['key'];
    
    $Number = 5 ;
    $Coin = 2 ;
    
    if($id!="" && $connection->checkParameters($id) && $connection->checkParameters($key)){
        
        $check = $connection->q("select DateGetAds,NumberGetAds,Status from users where ID='$id' and Hash='$Hash'");
        $check_r = $connection->a($check);
		if($check_r['Status']=="0"){
		    
		    $get_values = $connection->q("select * from valuesBase where Name='NumberGetAds' or Name='CoinGetAds' ");
            while($get_values_r = $connection->a($get_values)){
                if($get_values_r['Name']=="NumberGetAds"){
                    $Number = $get_values_r['Value'] ;
                }else if($get_values_r['Name']=="CoinGetAds"){
                    $Coin = $get_values_r['Value'] ;
                }
            }
		    
			if($check_r['NumberGetAds']<$Number){
				add2($key,$id,$Coin);
			}else {
				$d1=strtotime($check_r['DateGetAds']);
				$d2=ceil((time()-$d1)/60);
				if($d2>=1440){
					$connection->q("update users set NumberGetAds=0 where ID='$id'");
					add2($key,$id,$Coin);
				}else {
					print "full" ;
				}
			}
		}else {
            print "block";
        }
    	
    }else {
    	echo "null";
    }
    
}

function add2($key,$id,$Coin){
    $connection = connectToDatabase();
    
    $Timee = date("Y/m/d G:i");
    
    if($key=="ads_follow"){
		$result = $connection->q("update users set Coin_Follow=Coin_Follow+$Coin,DateGetAds='$Timee',NumberGetAds=NumberGetAds+1 where ID='$id' ");
		if($result!=""){
			print "ok" ;
		}else {
			print "no" ;
		}
	}else if($key=="ads_other"){
		$result = $connection->q("update users set Coin_Other=Coin_Other+$Coin,DateGetAds='$Timee',NumberGetAds=NumberGetAds+1 where ID='$id'");
		if($result!=""){
			print "ok" ;
		}else {
			print "no" ;
		}
	}else {
		echo "null";
	}
    
}


?>