<?php

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

defined('NIUX_INCLUDED') or exit('Access Denied.');
$ggclTitle = inSql($_POST['ggclTitle']);
preg_match('/^.{1,255}$/', $ggclTitle) or die("广告策略名称：不能为空/不超过80个汉字！");
$ggclExplain = inSql($_POST['ggclExplain']);
preg_match('/^.{0,255}$/', $ggclExplain) or die("广告策略说明：不能超过80个字符！");
$ggclLei = (int) substr(inSql($_POST['ggclLei']), 0, 10) or die('类组不能为空！请先新建类组！');
$ggclZu = (int) substr(inSql($_POST['ggclZu']), 0, 10) or die('类组不能为空！请先新建类组！');
$wllx = (int) substr(inSql($_POST['wllx']), 0, 1);
$ggclWeight = (int) substr(inSql($_POST['ggclWeight']), 0, 2);
$xzggs = substr(inSql($_POST['xzggs']), 0, 65535);
$rotate_cl = (int) substr(inSql($_POST['rotate_cl']), 0, 1);
$slide_interval = (int) substr(inSql($_POST['slide_interval']), 0, 3);
$time_cl = (int) substr(inSql($_POST['time_cl']), 0, 1);
$time_content = substr(inSql($_POST['time_content']), 0, 65535);
$limit_gg = (int) substr(inSql($_POST['limit_gg']), 0, 1);
$limit_gg_num = (int) substr(inSql($_POST['limit_gg_num']), 0, 10);
$limit_visitor = (int) substr(inSql($_POST['limit_visitor']), 0, 1);
$visitor_num = (int) substr(inSql($_POST['visitor_num']), 0, 3);
$os_cl = (int) substr(inSql($_POST['os_cl']), 0, 1);
$os_content = substr(inSql($_POST['os_content']), 0, 255);
$screen_cl = (int) substr(inSql($_POST['screen_cl']), 0, 1);
$screen_content = substr(inSql($_POST['screen_content']), 0, 255);
$browser_cl = (int) substr(inSql($_POST['browser_cl']), 0, 1);
$browser_content = substr(inSql($_POST['browser_content']), 0, 255);
$lang_cl = (int) substr(inSql($_POST['lang_cl']), 0, 1);
$lang_content = substr(inSql($_POST['lang_content']), 0, 255);
$referer_cl = (int) substr(inSql($_POST['referer_cl']), 0, 1);
$referer_content = substr(inSql($_POST['referer_content']), 0, 65535);
$url_cl = (int) substr(inSql($_POST['url_cl']), 0, 1);
$url_content = substr(inSql($_POST['url_content']), 0, 65535);
$local_cl = (int) substr(inSql($_POST['local_cl']), 0, 1);
$local_content = substr(inSql($_POST['local_content']), 0, 65535);
$ua_cl = (int) substr(inSql($_POST['ua_cl']), 0, 1);
$ua_content = substr(inSql($_POST['ua_content']), 0, 65535);
if ($id && !$copy) {
    $query = "UPDATE `${cfg_dbprefix}ggcl` SET title='$ggclTitle',exp='$ggclExplain',type='$wllx',weight='$ggclWeight',gg='$xzggs',rotate_cl='$rotate_cl',slide_interval='$slide_interval',time_cl='$time_cl',time_content='$time_content',limit_gg='$limit_gg',limit_gg_num='$limit_gg_num',limit_visitor='$limit_visitor',visitor_num='$visitor_num',"
            . "os_cl='$os_cl',os_content='$os_content',screen_cl='$screen_cl',screen_content='$screen_content',browser_cl='$browser_cl',browser_content='$browser_content',lang_cl='$lang_cl',lang_content='$lang_content',referer_cl='$referer_cl',referer_content='$referer_content',url_cl='$url_cl',url_content='$url_content',local_cl='$local_cl',local_content='$local_content',ua_cl='$ua_cl',ua_content='$ua_content',lei_id=$ggclLei,zu_id=$ggclZu WHERE id='${id}';";
    $nxsql->query($query);
    $nxsql->errno && die('更新数据库发生错误： (' . $nxsql->errno . ') ' . $nxsql->error);
    $path = ROOT_DIR . $cfg_output . '/' . $cfg_book . '-' . $id . '.js' . ($recycle ? 'recycle' : '');
    $ggclDm = getGGCLdm($id, $wllx, $ggclWeight, $xzggs, $rotate_cl, $slide_interval, $time_cl, $time_content, $limit_gg, $limit_gg_num, $limit_visitor, $visitor_num, $os_cl, $os_content, $screen_cl, $screen_content, $browser_cl, $browser_content, $lang_cl, $lang_content, $referer_cl, $referer_content, $url_cl, $url_content, $local_cl, $local_content, $ua_cl, $ua_content);
    file_put_contents($path, $ggclDm) or die('出错啦！无法修改广告策略文件！请检查输出目录名称、权限设置是否正确。');
    $recycle || updateGgw($id);
    $nxsql->inLog('修改广告策略' . $id);
    exit('2');
} else {
    $gid = date("YmdHis");
    $query = "INSERT INTO `${cfg_dbprefix}ggcl` (title,exp,gid,type,weight,gg,rotate_cl,slide_interval,time_cl,time_content,limit_gg,limit_gg_num,limit_visitor,visitor_num,os_cl,os_content,screen_cl,screen_content,browser_cl,browser_content,lang_cl,lang_content,referer_cl,referer_content,url_cl,url_content,local_cl,local_content,ua_cl,ua_content,lei_id,zu_id,admin_id) VALUES "
            . "('$ggclTitle','$ggclExplain','$gid','$wllx','$ggclWeight','$xzggs','$rotate_cl','$slide_interval','$time_cl','$time_content','$limit_gg','$limit_gg_num','$limit_visitor','$visitor_num','$os_cl','$os_content','$screen_cl','$screen_content','$browser_cl','$browser_content','$lang_cl','$lang_content','$referer_cl','$referer_content','$url_cl','$url_content','$local_cl','$local_content','$ua_cl','$ua_content','$ggclLei','$ggclZu','$adminID');";
    $nxsql->query($query);
    $nxsql->errno && die('插入数据库发生错误： (' . $nxsql->errno . ') ' . $nxsql->error);
    $id = $nxsql->insert_id;
    $path = ROOT_DIR . $cfg_output . '/' . $cfg_book . '-' . $id . '.js';
    $ggclDm = getGGCLdm($id, $wllx, $ggclWeight, $xzggs, $rotate_cl, $slide_interval, $time_cl, $time_content, $limit_gg, $limit_gg_num, $limit_visitor, $visitor_num, $os_cl, $os_content, $screen_cl, $screen_content, $browser_cl, $browser_content, $lang_cl, $lang_content, $referer_cl, $referer_content, $url_cl, $url_content, $local_cl, $local_content, $ua_cl, $ua_content);
    file_put_contents($path, $ggclDm) or die('出错啦！无法创建广告策略文件！请检查输出目录名称、权限设置是否正确。');
    $nxsql->inLog('新建策略' . $id);
    exit('1');
}


