mercredi 22 juin 2016

How do I dismiss a traffic exchange in php

I have a problem with traffic exchange script and I've tried repeatedly and always failed.

This script runs indefinitely and continue to spin endlessly, I want to restrict users only 1 visit to one site and dismiss the exchange if the total sites they already visit.

I hope to receive help because I always failed.

<?php

    class Exchange extends BaseModel
{
    private static $attack = 0;
    public static $info = array();

    public static function random($user_id, $limit=1)
    {
        $geo_location = self::geo_iso();
        $rand = rand(1, 555);
        if($rand >= 500)
        {
            $query = "SELECT * FROM websites WHERE status = :status and user_points = :userpoints and activated = :activated and enabled = :enabled and user_id != :uid and geolocation LIKE :geolike ORDER BY RAND() LIMIT :limit";
            Db::bind("geolike", "%".$geo_location."%");
        }
        else if($rand < 500)
        {
            Db::bind("status", "1");
            Db::bind("activated", "1");
            Db::bind("enabled", "1");
            Db::bind("userpoints", "working");
            Db::bind("uid", strip_tags($user_id));
            Db::bind("limit", $limit);
            Db::bind("geo", "");
            $count = Db::query("SELECT COUNT(id) FROM  websites WHERE status = :status and user_points = :userpoints and activated = :activated and enabled = :enabled and user_id != :uid and geolocation = :geo");
            if($count[0]["COUNT(id)"] > 0)
            {
                $newrand = rand(1, 10);
                if($newrand < 5)
                {
                    $query = "SELECT * FROM websites WHERE status = :status and user_points = :userpoints and activated = :activated and enabled = :enabled and user_id != :uid and geolocation LIKE :geolike ORDER BY RAND() LIMIT :limit";
                    Db::bind("geolike", "");
                }
                else
                {
                    $query = "SELECT * FROM websites WHERE status = :status and user_points = :userpoints and activated = :activated and enabled = :enabled and user_id != :uid and geolocation LIKE :geolike ORDER BY RAND() LIMIT :limit";
                    Db::bind("geolike", "%[ALL]%");
                }
            }
            else
            {
                $query = "SELECT * FROM websites WHERE status = :status and user_points = :userpoints and activated = :activated and enabled = :enabled and user_id != :uid and geolocation LIKE :geolike ORDER BY RAND() LIMIT :limit";
                Db::bind("geolike", "%[ALL]%");
            }
        }
        Db::bind("status", "1");
        Db::bind("activated", "1");
        Db::bind("enabled", "1");
        Db::bind("userpoints", "working");
        Db::bind("uid", strip_tags($user_id));
        Db::bind("limit", $limit);
        $rand = Db::query($query);
        return $rand[0];
    }

    public static function geo_iso()
    {
        $ipAddress = Sys::ip();
        $databaseFile = 'uploads/GeoIP/GeoLite2-Country.mmdb';
        $reader = new MaxMindDbReader($databaseFile);
        $GeoData = $reader->get($ipAddress);
        $iso_code = $GeoData["country"]["iso_code"];
        $reader->close();
        if(!empty($iso_code))
        {
            return "[".$iso_code."]";
        }
        else
        {
            return "[ALL]";
        }
    }

    public static function get_website($user_id)
    {
        $count_websites = Getdata::howmany("websites");
        if($count_websites == 0)
        {
            return array(
                "id" => "noexchange",
                "url" => s("stoped.html"),
                "source" => "",
                "max_hits" => 0,
                "max_hour_hits" => 0,
                "duration" => 15,
                "user_id" => 0
                );
        }
        $status = 1;
        while($status)
        {
            $website    = self::random($user_id, 1);
            $hour_hits  = self::hist_in_last_hour($website["id"]);
            $total_hits = $website["hits"];
            $user       = Getdata::one_active_user($website["user_id"]);
            $return_website = array();
            if(!empty($website) && is_array($website) && $user["points"] > 0 && $hour_hits < $website["max_hour_hits"] && $total_hits < $website["max_hits"])
            {
                $return_website = $website;
                $status = 0;
                break;
            }
            else if(!empty($website) && is_array($website) && $user["points"] > 0 && $hour_hits < $website["max_hour_hits"] && $website["max_hits"]=='0' || empty($website["max_hits"]) && !empty($website) && is_array($website) && $user["points"] > 0 && $hour_hits < $website["max_hour_hits"])
            {
                $return_website = $website;
                $status = 0;
                break;
            }
            else
            {
                if(self::$attack > 500 && empty($return_website))
                {
                    $return_website = array(
                    "id" => "noexchange",
                    "url" => s("defaults/website"),
                    "source" => "",
                    "max_hits" => 0,
                    "max_hour_hits" => 0,
                    "duration" => 15,
                    "user_id" => 0
                    );
                    $status = 0;
                    break;
                }
                else
                {
                    $status = 1;
                    self::$attack = self::$attack+1;
                    continue;
                }
            }
        }
        if(!empty($return_website))
        {
            return $return_website;
        }
        else
        {
            return array();
        }
    }

    private static function hist_in_last_hour($wid)
    {
        Db::bind("time", time()-3600);// last hour
        Db::bind("wid", strip_tags($wid));
        $get = Db::query("SELECT COUNT(id) FROM hits WHERE website_id = :wid and created_at > :time");
        if($get)
        {
            return $get[0]["COUNT(id)"];
        }
        else
        {
            return 0;
        }
    }

    public static function info($info)
    {
        self::$info = $info;
    }

    public static function delete($id)
    {
        $sessionid = strip_tags($id);
        if(is_numeric($sessionid))
        {
            $u = self::$info;
            Db::bind("id", strip_tags($sessionid));
            Db::bind("uid", $u["id"]);
            $get = Db::query("SELECT * FROM exchange WHERE id = :id and user_id = :uid");
            if(!empty($get))
            {
                Db::bind("sid", $sessionid);
                Db::query("DELETE FROM exchange WHERE id = :sid");
            }
        }
    }

    public static function clear($id)
    {
        $sessionid = strip_tags($id);
        if(is_numeric($sessionid))
        {
            $u = self::$info;
            Db::bind("id", strip_tags($sessionid));
            Db::bind("uid", $u["id"]);
            $get = Db::query("SELECT * FROM exchange WHERE id = :id and user_id = :uid");
            if(!empty($get))
            {
                Db::bind("sid", $sessionid);
                Db::bind("check_run", floor(time()-s("exchange/maxduration")-200));
                Db::query("UPDATE exchange SET `last_run` = :check_run WHERE id = :sid");
            }
        }
    }

    public static function add()
    {
        if(!empty(self::$info))
        {
            $u = self::$info;
            $sessions = Getdata::howmany("exchange WHERE user_id = :userid", array(
                "userid" => $u["id"]
            ));
            if($u["session_slots"] > $sessions)
            {
                Db::bind("uid", $u["id"]);
                Db::bind("cat", time());
                Db::bind("uat", time());
                Db::bind("lastrun", floor(time()-s("exchange/maxduration")-200));
                $query = "INSERT INTO exchange(`user_id`, `lasturl_id`, `accepted_time`, `last_run`, `closed`, `status`, `created_at`, `updated_at`) VALUES(:uid, '', '', :lastrun, '0', '1', :cat, :uat)";
                if(Db::query($query))
                {
                    return true;
                }
                else 
                {
                    return false;  
                }
            }
        }
        else
        {
            return false;
        }
    }

    public static function active_now($who="")
    {
        $u = self::$info;
        if(empty($who))
        {
            $id = $u["id"];
            if(empty($id)){ return 0; }
            Db::bind("uid", $id);
            Db::bind("check_run", floor(time()-s("exchange/maxduration")));
            $query = "SELECT COUNT(id) FROM exchange WHERE user_id = :uid and last_run > :check_run";
            $get = Db::query($query);
            if($get)
            {
                return $get[0]["COUNT(id)"];
            }
            else
            {
                return 0;
            }
        }
        else if(empty($who))
        {
            $id = $u["id"];
            if(empty($id)){ return 0; }
            Db::bind("uid", $id);
            Db::bind("check_run", floor(time()-s("exchange/maxduration")));
            $query = "SELECT COUNT(id) FROM exchange WHERE user_id = :uid and last_run > :check_run";
            $get = Db::query($query);
            if($get)
            {
                return $get[0]["COUNT(id)"];
            }
            else
            {
                return 0;
            }
        }
        else if(!empty($who) && $who=="all")
        {
            Db::bind("check_run", floor(time()-s("exchange/maxduration")));
            $query = "SELECT COUNT(id) FROM exchange WHERE last_run > :check_run";
            $get = Db::query($query);
            if($get)
            {
                return $get[0]["COUNT(id)"];
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    public static function is_active($session_id)
    {
        $session = strip_tags($session_id);
        if(is_numeric($session))
        {
            $sdata = Getdata::one_active_exchange($session);
            if(!empty($sdata) && $sdata["id"] == $session)
            {
                $time = floor(time()-s("stoped.html"));
                if($sdata["last_run"] > $time)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }   

    public static function run($id)
    {
        $id = strip_tags($id);
        if(!empty(self::$info) && !empty($id))
        {
            $posted_id = Encryption::decode(strip_tags(Request::post("browse")));
            if(is_numeric($posted_id) && !empty($posted_id))
            {
                $check_website = Getdata::one_website($posted_id);
            }
            else
            {
                $check_website = array();
            }
            if(self::is_active($id) && $check_website["id"] != $posted_id && $posted_id != "noexchange" || empty(self::$info))
            {
                return array(array(
                    "status" => false,
                    "open_status" => false,
                    "show_url" => "",
                    "url" => "",
                    "duration" => "0",
                    "browse" => "",
                    "points" => "0",
                    "message" => false
                ));
            }
            $user = self::$info;
            $user_id = $user["id"];
            $website = self::get_website($user_id);
            Db::bind("id", $id);
            Db::bind("uid", $user["id"]);
            $exchange = Db::query("SELECT * FROM exchange WHERE id = :id and user_id = :uid");
            $exchange = $exchange[0];
            if(!empty($exchange) && !empty($website))
            {
                $session_uid = Session::get("current_exchange".$exchange["id"]);
                if(!empty($posted_id) && $exchange["lasturl_id"]==$session_uid && $exchange["lasturl_id"]==$posted_id && $exchange["accepted_time"] <= time())
                {
                    if($exchange["lasturl_id"]=='noexchange')
                    {
                        Points::info($user);
                        Points::add("noexchange");
                    }
                    else
                    {
                        $oldwebsite = Getdata::one_active_website($exchange["lasturl_id"]);
                        if(!empty($oldwebsite))
                        {
                            Hits::info($user);
                            Hits::add($oldwebsite["id"]);
                            Points::info($user);
                            Points::add($oldwebsite["id"]);
                        }
                    }
                }
                $eid = Encryption::encode($website["id"]);
                $esid = Encryption::encode($exchange["id"]);
                $accepted_time = floor(time()+$website["duration"]);
                Db::bind("uid", $user["id"]);
                Db::bind("sid", $id);
                Db::bind("lastrun", time());
                Db::bind("urlid", $website["id"]);
                Db::bind("actime", $accepted_time);
                Db::bind("lastrun", time());
                Db::bind("uat", time());
                Db::query("UPDATE exchange SET `lasturl_id` = :urlid, `accepted_time` = :actime, `last_run` = :lastrun, `closed` = '0', `updated_at` = :uat WHERE `id` = :sid and user_id = :uid");
                return array(array(
                    "status" => true,
                    "open_status" => true,
                    "show_url" => urldecode($website["url"]),
                    "url" => router("noref", array("id" => $eid, "sid" => $esid)),
                    "duration" => floor($website["duration"]+1),
                    "browse" => $eid,
                    "points" => round(Points::get($user["id"]), 2),
                    "message" => false
                ));
            }
            else
            {
                return array(array(
                    "status" => false,
                    "open_status" => false,
                    "show_url" => "",
                    "url" => "",
                    "duration" => "0",
                    "browse" => "",
                    "points" => "0",
                    "message" => false
                ));
            }
        }
    }

    public static function check_session($uid, $sid)
    {
        $uid = strip_tags($uid);
        $sid = strip_tags($sid);
        if(is_numeric($uid) && is_numeric($sid))
        {
            Db::bind("id", $sid);
            $session = Db::query("SELECT * FROM exchange WHERE id = :id");
            $exchange = $session[0];
            if($exchange["user_id"] == $uid && $exchange["id"] == $sid)
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    /*public static function useragent($agent)
    {
        if($agent=="all")
        {
            $new_agents = array();
            foreach(self::$useragents as $ag)
            {
                $new_agents = array_merge($ag, $new_agents);
            }
            return $new_agents[array_rand($new_agents)];
        }
        $agent = str_replace(array(",,,", ",,", ","), ",", $agent);
        $agents = explode(",", $agent);
        if(!empty($agent) && !empty($agents) && is_array($agents))
        {
            $new_agents = array();
            foreach($agents as $useragent)
            {
                if(is_array(self::$useragents[$useragent]) && !empty(self::$useragents[$useragent]))
                {
                    $new_agents = array_merge(self::$useragents[$useragent], $new_agents);
                }
            }
            if(!empty($new_agents))
            {
                return $new_agents[array_rand($new_agents)];
            }
            else
            {
                return "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0";
            }
        }
        else
        {
            return "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0";
        }
    }*/

}
?>

Aucun commentaire:

Enregistrer un commentaire