<?php

add();

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

}


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

    $id_user_req = $_REQUEST['id_user_req'];
    $id_user = $_REQUEST['id_user'];
    $username_user = $_REQUEST['username_user'];
    $comments = $_REQUEST['comments'];
    $name_user = $_REQUEST['name_user'];
    $pic_user = $_REQUEST['pic_user'];
    $id_post = $_REQUEST['id_post'];
    $link_post = $_REQUEST['link_post'];
    $text_post = $_REQUEST['text_post'];
    $darkhasti = $_REQUEST['darkhasti'];
    $part = $_REQUEST['part'];
    $AndroidID = $_REQUEST['AndroidID'];
    
    $seke_niaz = $darkhasti * 2 ;

    $Timee = date("Y/m/d G:i");
    
    if($AndroidID!="" && $id_user_req!="" && $connection->checkParameters($id_user_req) && $connection->checkParameters($id_user) && $connection->checkParameters($username_user)){
        
        if($darkhasti >= 50){
    
            $check_coin = $connection->q("select Coin_Follow,Coin_Other,Status,AndroidID from users where ID='$id_user_req'");
            $row_coin = $connection->a($check_coin);
    		if($row_coin['Status']=="0"){
    		    
    		    if($row_coin["AndroidID"] == $AndroidID){
    			
    			    if($part=="0" && $row_coin['Coin_Follow']>=$seke_niaz){
        				$chech = $connection->q("select status from banners where id_user='$id_user' and part='$part' ");
        				$row = $connection->a($chech);
        				if($row[0]==""){
        					$result = $connection->q("insert into banners (id_user_req,id_user,username_user,name_user,pic_user,id_post,link_post,text_post,comments,darkhasti,daryafti,status,part,Date,followed) values('$id_user_req','$id_user','$username_user','$name_user','$pic_user','$id_post','$link_post','$text_post','$comments','$darkhasti','0','0','$part','$Timee','') ");
        					if($result!=""){
        						$connection->q("update users set Coin_Follow=Coin_Follow-$seke_niaz where ID='$id_user_req'");
        						print "ok";
        					}else {
        						print "no";
        					}
        				}else {
        
        					if($row['status']=="1"){
        						$result = $connection->q("update banners set darkhasti=darkhasti+$darkhasti,Date='$Timee',status='0',id_user_req='$id_user_req',comments='$comments',pic_user='$pic_user',name_user='$name_user',link_post='$link_post' where id_user='$id_user' and part='$part' ");
        						if($result!=""){
        							$connection->q("update users set Coin_Follow=Coin_Follow-$seke_niaz where ID='$id_user_req'");
        							print "ok";
        						}else {
        							print "no";
        						}
        					}else {
        						print "exist";
        					}
        
        				}
        			}else if($part!="0" && $row_coin['Coin_Other']>=$seke_niaz){
        				$chech = $connection->q("select status from banners where id_post='$id_post' and part='$part' ");
        				$row = $connection->a($chech);
        				if($row[0]==""){
        					$result = $connection->q("insert into banners (id_user_req,id_user,username_user,name_user,pic_user,id_post,link_post,text_post,comments,darkhasti,daryafti,status,part,Date,followed) values('$id_user_req','$id_user','$username_user','$name_user','$pic_user','$id_post','$link_post','$text_post','$comments','$darkhasti','0','0','$part','$Timee','') ");
        					if($result!=""){
        						$connection->q("update users set Coin_Other=Coin_Other-$seke_niaz where ID='$id_user_req'");
        						print "ok";
        					}else {
        						print "no";
        					}
        				}else {
        
        					if($row['status']=="1"){
        						$result = $connection->q("update banners set darkhasti=darkhasti+$darkhasti,Date='$Timee',status='0',id_user_req='$id_user_req',comments='$comments',pic_user='$pic_user',link_post='$link_post' where id_post='$id_post' and part='$part' ");
        						if($result!=""){
        							$connection->q("update users set Coin_Other=Coin_Other-$seke_niaz where ID='$id_user_req'");
        							print "ok";
        						}else {
        							print "no";
        						}
        					}else {
        						print "exist";
        					}
        				}
        			}else {
        				print "no_coin";
        			}
    			
    		    }else {
    		        print "wrong_androidid";
    		    }
    			
    		}else {
    			print "block";
    		}
		
        }else {
            echo "min_count";
        }
        
	}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 ;
    }
}


?>