<?php
/*
// +--------------------------------------------------------------------------+
// | HTTP-User-Agents Class                                                    |
// | by dada                                                              |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
 */

class UserAgent {

    var $agent_id = 0;
    var $agent_name = "";

    function UserAgent( $agent_name ){
        if (strlen($agent_name) > 2) {
        	$agent_name = stripslashes($agent_name);
            preg_match("/^([^;]*).*$/", $agent_name, $m);
			$this->agent_name = $m[1];

            if ($agent = $this->getAgent()) {
                $this->updateHits();

                if ($agent["aktiv"] == 0) {
                    exit("Du benutzt einen gebannten Client. Bitte lies die FAQ!");
                }
            }
            else $this->insert();
        }
        else exit("Was soll das den? n00b :D");
    }

    function getAgent(){
        $ret = false;

        $sql = "SELECT * FROM agents WHERE agent_name = '".addslashes($this->agent_name)."'";
        $qry = mysql_query($sql);
        $a = mysql_fetch_assoc($qry);

        if ($a["agent_id"] > 0) {
            $this->agent_id = $a["agent_id"];
            $ret = $a;
        }

        return $ret;
    }

    function updateHits(){
        $sql = "UPDATE agents SET hits=hits+1 WHERE agent_id = '".$this->agent_id."'";
        mysql_query($sql);
    }

    function insert(){
        if (strlen($this->agent_name) > 2) {
            $sql = "INSERT INTO agents VALUES ('', '".$this->agent_name."', 1, ".time().", 1)";
            mysql_query($sql);

            if (isset($_GET["passkey"])) {
                preg_match("/^([^?]{16})/", $_GET["passkey"], $m);
                    $from = $m[1];
            }
            else $from = "ohne Passkey";

            $betreff = "Neuer Torrent-Client \"".$this->agent_name."\" !";
            $msg =  "Name  : " . $this->agent_name . "\n";
            if ($this->agent_name != $_SERVER["HTTP_USER_AGENT"]) {
                $msg .= "Header: " . $_SERVER["HTTP_USER_AGENT"] . "\n";
            }
            $msg .= "Passkey : " . $from . "\n";
            $msg .= "IP    : " . $_SERVER['REMOTE_ADDR'];

            $res = mysql_query("SELECT id FROM users WHERE class >= ".UC_SYSOP);
            while($arr = mysql_fetch_row($res)){
                sendPersonalMessage(0, $arr[0], $betreff, $msg, PM_FOLDERID_INBOX, 0);
            }
        }
    }

    function getAll($order="first"){
        $ret = false;

        switch($order){
            case "name":
                $order_by = "agent_name";
                break;

            case "hits":
                $order_by = "hits DESC";
                break;

            case "aktiv":
                $order_by = "aktiv";
                break;

            default:
                $order_by = "ins_date DESC";
        } // switch

        $sql = "SELECT * FROM agents ORDER BY $order_by";
        $qry = mysql_query($sql);
        while($a = mysql_fetch_assoc($qry)){
            $ret[] = $a;
        } // while

        return $ret;
    }

    function setAktiv($agent_id, $aktiv){
        $agent_id = intval($agent_id);
        $aktiv = intval($aktiv);

        if ($agent_id) {
            $sql = "UPDATE agents SET aktiv=$aktiv WHERE agent_id=$agent_id";
            mysql_query($sql);
        }
    }
}
?>