<?php

/*
 * ----------------------------------------------------
 * 程序名称: 牛叉广告管理系统(NiuXams)
 * 程序功能: 实现专业的广告投放、管理和统计等管理系统！
 * 程序开发: 牛叉软件(NiuXSoft.com)
 * 版权所有: [NiuXams] (C)2013-2099 NiuXSoft.com
 * 官方网站: www.niuxsoft.com  Email: niuxsoft@163.com
 * ----------------------------------------------------
 * 使用条款:
 * 1.为构建创新型经济社会,请尊重保护知识产权！
 * 2.该软件个人非商业用途免费使用.
 * 3.免费使用禁止修改版权信息和官方推广链接.
 * 4.禁止任何衍生版本.禁止任何非法修改源代码贩卖.
 * ----------------------------------------------------
 */
class Client {

    public $ua = '';
    public $bn = '未知浏览器';
    public $bv = 0;
    public $os = '未知操作系统';
    public $ip = 0;
    public $po = 0;
    public $la = '未知';
    public $re = '直接访问';

    function __construct($ua = '') {
        $this->ua = substr(addslashes(strtolower($ua ? $ua : (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''))), 0, 255);
        $this->getBrowse($this->ua);
        $this->getOs($this->ua);
        $this->getIp();
        $this->getPort();
        $this->getLang();
        $this->getReferer();
    }

    function getBrowse($ua) {
        $matches = array();
        if (preg_match('/msie[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'IE';
        } else if (preg_match('/opera(?:.*version)?[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'Opera';
        } else if (preg_match('/maxthon[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'Maxthon';
        } else if (preg_match('/firefox[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'Firefox';
        } else if (preg_match('/chrome[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'Chrome';
        } else if (preg_match('/safari[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'Safari';
            preg_match('/version[ \/]([\w.]+)/', $ua, $matches);
        } else if (preg_match('/webkit[ \/]([\w.]+)/', $ua, $matches)) {
            $this->bn = 'WebKit';
        } else if (preg_match('/rv:([\w.]+)/', $ua, $matches)) {
            $this->bn = 'IE';
        }
        $this->bv = empty($matches[1]) ? 0 : (int) $matches[1];
    }

    function getOs($ua) {
        if (strpos($ua, 'win') && strpos($ua, 'nt 5.1')) {
            $this->os = 'Windows XP';
        } else if (strpos($ua, 'googlebot')) {
            $this->os = 'Google蜘蛛';
        } else if (strpos($ua, 'baiduspider')) {
            $this->os = 'Baidu蜘蛛';
        } else if (strpos($ua, 'baidu transcoder')) {
            $this->os = '百度转码';
        } else if (strpos($ua, 'bingbot') || strpos($ua, 'BingPreview')) {
            $this->os = 'Bing蜘蛛';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 10')) {
            $this->os = 'Windows 10';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 6.3')) {
            $this->os = 'Windows 8.1';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 6.2')) {
            $this->os = 'Windows 8';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 6.1')) {
            $this->os = 'Windows 7';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 6')) {
            $this->os = 'Windows Visita';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 5.2')) {
            $this->os = 'Windows Server 2003';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt 5')) {
            $this->os = 'Windows 2000';
        } else if (strpos($ua, 'windows phone') && strpos($ua, '8.1')) {
            $this->os = 'Windows Phone 8.1';
        } else if (strpos($ua, 'windows phone') && strpos($ua, '8')) {
            $this->os = 'Windows Phone 8';
        } else if (strpos($ua, 'windows phone') && strpos($ua, '7.5')) {
            $this->os = 'Windows Phone 7.5';
        } else if (strpos($ua, 'windows phone') && strpos($ua, '7')) {
            $this->os = 'Windows Phone 7';
        } else if (strpos($ua, 'android')) {
            $this->os = 'Android';
        } else if (strpos($ua, 'ipod')) {
            $this->os = 'iPod';
        } else if (strpos($ua, 'ipad')) {
            $this->os = 'iPad';
        } else if (strpos($ua, 'iphone')) {
            $this->os = 'iPhone';
        } else if (strpos($ua, 'mac') && strpos($ua, 'os')) {
            $this->os = 'Mac';
        } else if (strpos($ua, 'linux')) {
            $this->os = 'Linux';
        } else if (strpos($ua, 'unix')) {
            $this->os = 'Unix';
        } else if (strpos($ua, 'sun') && strpos($ua, 'os')) {
            $this->os = 'SunOS';
        } else if (strpos($ua, 'ibm') && strpos($ua, 'os')) {
            $this->os = 'IBM OS/2';
        } else if (strpos($ua, 'powerpc')) {
            $this->os = 'PowerPC';
        } else if (strpos($ua, 'freebsd')) {
            $this->os = 'FreeBSD';
        } else if (strpos($ua, 'win 9x') && strpos($ua, '4.90')) {
            $this->os = 'Windows ME';
        } else if (strpos($ua, 'win') && strpos($ua, '98')) {
            $this->os = 'Windows 98';
        } else if (strpos($ua, 'win') && strpos($ua, '95')) {
            $this->os = 'Windows 95';
        } else if (strpos($ua, 'win') && strpos($ua, '32')) {
            $this->os = 'Windows 32';
        } else if (strpos($ua, 'win') && strpos($ua, 'nt')) {
            $this->os = 'Windows NT';
        }
    }

    function getIp() {
        $this->ip = ip2long(empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
    }

    function getPort() {
        $this->po = empty($_SERVER['REMOTE_PORT']) ? 0 : (int) $_SERVER['REMOTE_PORT'];
    }

    function getLang() {
        $lang = addslashes(strtolower(empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? '未知' : substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4)));
        if (strpos($lang, 'zh-c') !== false || strpos($lang, 'zh-h') !== false) {
            $this->la = "简体";
        } else if (strpos($lang, 'zh') !== false) {
            $this->la = "繁體";
        } else if (strpos($lang, 'en') !== false) {
            $this->la = "English";
        } else if (strpos($lang, 'fr') !== false) {
            $this->la = "French";
        } else if (strpos($lang, 'de') !== false) {
            $this->la = "German";
        } else if (strpos($lang, 'jp') !== false) {
            $this->la = "Japanese";
        } else if (strpos($lang, 'ko') !== false) {
            $this->la = "Korean";
        } else if (strpos($lang, 'es') !== false) {
            $this->la = "Spanish";
        } else if (strpos($lang, 'sv') !== false) {
            $this->la = "Swedish";
        } else {
            $this->la = $lang;
        }
    }

    function getReferer() {
        $this->re = substr(addslashes(empty($_SERVER['HTTP_REFERER']) ? '直接访问' : $_SERVER['HTTP_REFERER']), 0, 255);
    }

}
