<?php

follow();

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

}


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

    $ID_ROW = $_REQUEST['id'];
    $user = $_REQUEST['user'];
    $Hash = $_REQUEST['Hash'];
    $where = $_REQUEST['where'];
    $id_users = "|" . $user . "|";
    $PART_ROW = "" ;

    $Ozv = date("Y/m/d");
    $Timee = time();
    $TimeeLast = time() - 60;

    if ($ID_ROW != "" && $connection->checkParameters($user) && $connection->checkParameters($ID_ROW)) {

        $check_user = $connection->q("select Status from users where ID='$user' and Hash='$Hash'");
        $check_user_r = $connection->a($check_user);
        if ($check_user_r['Status']=="0") {

            if ($where == "0") {
//            id_user
                $result = $connection->q("select ID,darkhasti,daryafti,followed,part from banners where ID='$ID_ROW'");
                $row = $connection->a($result);
                if ($row['ID']!="" && $row['part']==$where) {
                    $PART_ROW = $row['part'] ;
                    $new_followed = $row['followed'] . $id_users;

                    $check = $connection->q("SELECT COUNT(*) from plus where User='$user' and Sefaresh='$ID_ROW'");
                    $r_check = $connection->a($check);
                    if ($r_check[0] <= 0) {
                        $connection->q("insert into plus(User,Sefaresh,Date) values('$user','$ID_ROW','$Timee')");
                        
                        $get_collection = $connection->q("select count(User) from plus where User='$user' and Date >= $TimeeLast ");
                		$get_collection_r=$connection->a($get_collection);
                		if($get_collection_r[0] > 20){
                		    $connection->q("update users set Status=1 where ID='$user'");
							echo "ok";
                		}else {

                            $ress = $connection->q("update banners set daryafti=daryafti+1,followed='$new_followed' where ID='$ID_ROW'");
                            if ($ress != "") {
    
                                if ($PART_ROW == "0") {
                                    $connection->q("update users set Coin_Follow=Coin_Follow+1 where ID='$user'");
                                } else {
                                    $connection->q("update users set Coin_Other=Coin_Other+1 where ID='$user'");
                                }
    
    
                                $daryafti = $row['daryafti'];
                                $darkhasti = $row['darkhasti'];
    
                                if ($daryafti >= ($darkhasti - 1)) {
                                    $connection->q("update banners set status='1' where ID='$ID_ROW'");
                                }
    							echo "ok";
                            }
                        
                		}

                    }else {
                        $connection->q("update banners set followed='$new_followed' where ID='$ID_ROW'");
    					echo "ok";
                    }

                } else {
                    echo "null";
                }
            }else {
//            id_post
                $result = $connection->q("select ID,darkhasti,daryafti,followed,part from banners where ID='$ID_ROW'");
                $row = $connection->a($result);
                if ($row['ID']!="" && $row['part']==$where) {
                    $ID_ROW = $row['ID'] ;
                    $PART_ROW = $row['part'] ;
                    $new_followed = $row['followed'] . $id_users;

                    $check = $connection->q("SELECT COUNT(*) from plus where User='$user' and Sefaresh='$ID_ROW'");
                    $r_check = $connection->a($check);
                    if ($r_check[0] <= 0) {
                        $connection->q("insert into plus(User,Sefaresh,Date) values('$user','$ID_ROW','$Timee')");
                        
                        $get_collection = $connection->q("select count(User) from plus where User='$user' and Date >= $TimeeLast ");
                		$get_collection_r=$connection->a($get_collection);
                		if($get_collection_r[0] > 20){
                		    $connection->q("update users set Status=1 where ID='$user'");
							echo "ok";
                		}else {

                            $ress = $connection->q("update banners set daryafti=daryafti+1,followed='$new_followed' where ID='$ID_ROW'");
                            if ($ress != "") {
    
                                if ($PART_ROW == "0") {
                                    $connection->q("update users set Coin_Follow=Coin_Follow+1 where ID='$user'");
                                } else {
                                    $connection->q("update users set Coin_Other=Coin_Other+1 where ID='$user'");
                                }
    
    
                                $daryafti = $row['daryafti'];
                                $darkhasti = $row['darkhasti'];
    
                                if ($daryafti >= ($darkhasti - 1)) {
                                    $connection->q("update banners set status='1' where ID='$ID_ROW'");
                                }
    							echo "ok";
                            }
                        
                		}

                    }else {
                        $connection->q("update banners set followed='$new_followed' where ID='$ID_ROW'");
    					echo "ok";
                    }

                } else {
                    echo "null";
                }
            }

        }else {
            print "block";
        }

    } else {
        echo "null1";
    }

}


?>