<?php

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

define('NIUXAMS_ACCESS', TRUE);
require_once './config.php';
require_once ADMIN_DIR . 'data' . DIRECTORY_SEPARATOR . 'config_tj.php';
if ($cfg_autoTj) {
    $lctime1 = date("H:i:s", mktime(0, 0, 0));
    $lctime2 = date("H:i:s", mktime(0, 5, 0));
    $nowtime = date("H:i:s");
    if ($nowtime < $lctime2 && $nowtime > $lctime1) {
        require_once ADMIN_DIR . 'lib' . DIRECTORY_SEPARATOR . 'func_tj.php';
        tj();
    }
}
header('Access-Control-Allow-Origin:*');
$ac = (int) $_REQUEST['ac'];
if ($ac == 1 || $ac == 2) {
    $sw = (int) substr($_REQUEST['sw'], 0, 5);
    $sh = (int) substr($_REQUEST['sh'], 0, 5);
    $vw = (int) substr($_REQUEST['vw'], 0, 5);
    $vh = (int) substr($_REQUEST['vh'], 0, 5);
    $dw = (int) substr($_REQUEST['dw'], 0, 5);
    $dh = (int) substr($_REQUEST['dh'], 0, 5);
    $dx = (int) substr($_REQUEST['dx'], 0, 5);
    $dy = (int) substr($_REQUEST['dy'], 0, 5);
    $mx = (int) substr($_REQUEST['mx'], 0, 5);
    $my = (int) substr($_REQUEST['my'], 0, 5);
    $rx = (int) substr($_REQUEST['rx'], 0, 5);
    $ry = (int) substr($_REQUEST['ry'], 0, 5);
    $sc = (int) substr($_REQUEST['sc'], 0, 5);
    $co = (int) substr($_REQUEST['co'], 0, 1);
    $args = $_REQUEST['args'];
    $fr = substr(inSql($_REQUEST['fr']), 0, 255);
    $ur = substr(inSql($_REQUEST['ur']), 0, 255);
    $ua = substr(inSql($_REQUEST['ua']), 0, 255);
    $c = new Client();
    $query = "INSERT INTO `${cfg_dbprefix}counter` (ac,ip,po,os,la,bn,bv,fr,ur,sw,sh,vw,vh,dw,dh,dx,dy,mx,my,rx,ry,sc,co,ggwid,ggwx,ggwy,ggww,ggwh,ggid,ggsn,ggsn1,ggsn2,ggcn,ggcn1,asn,asn1,acn,acn1,ua) VALUES ";
    if ($ac == 1) {
        $ggw_array = explode(',', $args);
        for ($i = 0, $size_i = count($ggw_array); $i < $size_i; $i++) {
            list($ggw, $gg) = explode('+', $ggw_array[$i]);
            $ggw_att = explode(' ', $ggw);
            $ggw_att[0] = (int) substr($ggw_att[0], 0, 10);
            $ggw_att[1] = (int) substr($ggw_att[1], 0, 5);
            $ggw_att[2] = (int) substr($ggw_att[2], 0, 5);
            $ggw_att[3] = (int) substr($ggw_att[3], 0, 5);
            $ggw_att[4] = (int) substr($ggw_att[4], 0, 5);
            $gg_array = explode(';', $gg);
            for ($j = 0, $size_j = count($gg_array); $j < $size_j; $j++) {
                $gg_att = explode(' ', $gg_array[$j]);
                $gg_att[0] = (int) substr($gg_att[0], 0, 10);
                $gg_att[1] = (int) substr($gg_att[1], 0, 5);
                $gg_att[2] = (int) substr($gg_att[2], 0, 5);
                $gg_att[3] = (int) substr($gg_att[3], 0, 5);
                $gg_att[4] = (int) substr($gg_att[4] ? $gg_att[4] : 0, 0, 5);
                $gg_att[5] = (int) substr($gg_att[5] ? $gg_att[5] : 0, 0, 5);
                $gg_att[6] = (int) substr($gg_att[6], 0, 5);
                $gg_att[7] = (int) substr($gg_att[7], 0, 5);
                $gg_att[8] = (int) substr($gg_att[8] ? $gg_att[8] : 0, 0, 5);
                $gg_att[9] = (int) substr($gg_att[9] ? $gg_att[9] : 0, 0, 5);
                ($i || $j) && $query .= ",";
                $query .= "('$ac','$c->ip','$c->po','$c->os','$c->la','$c->bn','$c->bv','$fr','$ur','$sw','$sh','$vw','$vh','$dw','$dh','$dx','$dy','$mx','$my','$rx','$ry','$sc','$co','$ggw_att[0]','$ggw_att[1]','$ggw_att[2]','$ggw_att[3]','$ggw_att[4]','$gg_att[0]','$gg_att[1]','$gg_att[2]','$gg_att[3]','$gg_att[4]','$gg_att[5]','$gg_att[6]','$gg_att[7]','$gg_att[8]','$gg_att[9]','$ua')";
            }
        }
    } else {
        list($ggw, $gg) = explode('+', $args);
        $ggw_att = explode(' ', $ggw);
        $ggw_att[0] = (int) substr($ggw_att[0], 0, 10);
        $ggw_att[1] = (int) substr($ggw_att[1], 0, 5);
        $ggw_att[2] = (int) substr($ggw_att[2], 0, 5);
        $ggw_att[3] = (int) substr($ggw_att[3], 0, 5);
        $ggw_att[4] = (int) substr($ggw_att[4], 0, 5);
        $gg_array = explode(';', $gg);
        for ($j = 0, $size_j = count($gg_array); $j < $size_j; $j++) {
            $gg_att = explode(' ', $gg_array[$j]);
            $gg_att[0] = (int) substr($gg_att[0], 0, 10);
            $gg_att[1] = (int) substr($gg_att[1], 0, 5);
            $gg_att[2] = (int) substr($gg_att[2], 0, 5);
            $gg_att[3] = (int) substr($gg_att[3], 0, 5);
            $gg_att[4] = (int) substr($gg_att[4] ? $gg_att[4] : 0, 0, 5);
            $gg_att[5] = (int) substr($gg_att[5] ? $gg_att[5] : 0, 0, 5);
            $gg_att[6] = (int) substr($gg_att[6], 0, 5);
            $gg_att[7] = (int) substr($gg_att[7], 0, 5);
            $gg_att[8] = (int) substr($gg_att[8] ? $gg_att[8] : 0, 0, 5);
            $gg_att[9] = (int) substr($gg_att[9] ? $gg_att[9] : 0, 0, 5);
            $j && $query .= ",";
            $query .= "('$ac','$c->ip','$c->po','$c->os','$c->la','$c->bn','$c->bv','$fr','$ur','$sw','$sh','$vw','$vh','$dw','$dh','$dx','$dy','$mx','$my','$rx','$ry','$sc','$co','$ggw_att[0]','$ggw_att[1]','$ggw_att[2]','$ggw_att[3]','$ggw_att[4]','$gg_att[0]','$gg_att[1]','$gg_att[2]','$gg_att[3]','$gg_att[4]','$gg_att[5]','$gg_att[6]','$gg_att[7]','$gg_att[8]','$gg_att[9]','$ua')";
        }
    }
    $query .= ';';
    $nxsql->query($query);
} else if ($ac == 3) {
    $ggId = (int) substr($_REQUEST['id'], 0, 10);
    $queryClass = (int) substr($_REQUEST['class'], 0, 1);
    $query = "SELECT COUNT(*) FROM `${cfg_dbprefix}counter` WHERE ac=1 AND ggid='$ggId'";
    switch ($queryClass) {
        case 2:
            $query .= " AND MONTH(time)=MONTH(CURDATE())";
            break;
        case 3:
            $query .= " AND WEEK(time)=WEEK(CURDATE())";
            break;
        case 4:
            $query .= " AND DATE(time)=CURDATE()";
            break;
    }
    $query .= ';';
    echo $nxsql->oneRowVal($query);
}
