<?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.');
$ggwTitle = inSql($_POST['ggwTitle']);
preg_match('/^.{1,255}$/', $ggwTitle) or die("广告位名称：不能为空/不超过80个汉字！");
$ggwType = (int) substr(inSql($_POST['ggwType']), 0, 1);
$ggwWidth = (int) substr(inSql($_POST['ggwWidth']), 0, 5);
$ggwHeight = (int) substr(inSql($_POST['ggwHeight']), 0, 5);
$ggwLei = (int) substr(inSql($_POST['ggwLei']), 0, 10) or die('类组不能为空！请先新建类组！');
$ggwZu = (int) substr(inSql($_POST['ggwZu']), 0, 10) or die('类组不能为空！请先新建类组！');
$xzggcls = substr(inSql($_POST['xzggcls']), 0, 65535);
$bjgg = substr(inSql($_POST['bjgg']), 0, 30);
if ($id && !$copy) {
    $query = "UPDATE `${cfg_dbprefix}ggw` SET title='$ggwTitle',gid='$gid',type=$ggwType,ggcl='$xzggcls',bjgg='$bjgg',"
            . "lei_id=$ggwLei,zu_id=$ggwZu,width=$ggwWidth,height=$ggwHeight WHERE id='${id}';";
    $nxsql->query($query);
    $nxsql->errno && die('更新数据库发生错误： (' . $nxsql->errno . ') ' . $nxsql->error);
    $path = ROOT_DIR . $cfg_output . '/' . $cfg_forum . '-' . $id . '.js' . ($recycle ? 'recycle' : '');
    $ggwDm = getGGWdm($id, $xzggcls, $bjgg, $ggwType, $ggwWidth, $ggwHeight);
    file_put_contents($path, $ggwDm) or die('出错啦！无法修改广告位文件！请检查输出目录名称、权限设置是否正确。');
    $recycle || updateGgws($id);
    $nxsql->inLog('修改广告位' . $id);
    exit('2');
} else {
    $gid = date("YmdHis");
    $query = "INSERT INTO `${cfg_dbprefix}ggw` (title,gid,type,ggcl,bjgg,lei_id,zu_id,width,height,admin_id) VALUES "
            . "('$ggwTitle','$gid','$ggwType','$xzggcls','$bjgg','$ggwLei','$ggwZu','$ggwWidth','$ggwHeight','$adminID');";
    $nxsql->query($query);
    $nxsql->errno && die('插入数据库发生错误： (' . $nxsql->errno . ') ' . $nxsql->error);
    $id = $nxsql->insert_id;
    $path = ROOT_DIR . $cfg_output . '/' . $cfg_forum . '-' . $id . '.js';
    $ggwDm = getGGWdm($id, $xzggcls, $bjgg, $ggwType, $ggwWidth, $ggwHeight);
    file_put_contents($path, $ggwDm) or die('出错啦！无法创建广告位文件！请检查输出目录名称、权限设置是否正确。');
    $nxsql->inLog('新建广告位' . $id);
    exit('1');
}


