<?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.');
$thread = htmlspecialchars(inSql($_POST['thread']));
preg_match('/^([0-9a-zA-Z]){3,}$/', $thread) or die("不能有特殊字符！");
$book = htmlspecialchars(inSql($_POST['book']));
preg_match('/^([0-9a-zA-Z]){3,}$/', $book) or die("不能有特殊字符！");
$forum = htmlspecialchars(inSql($_POST['forum']));
preg_match('/^([0-9a-zA-Z]){3,}$/', $forum) or die("不能有特殊字符！");
$bag = htmlspecialchars(inSql($_POST['bag']));
preg_match('/^([0-9a-zA-Z]){3,}$/', $bag) or die("不能有特殊字符！");
$nameSpace = htmlspecialchars(inSql($_POST['nameSpace']));
preg_match('/[a-zA-Z_][a-zA-Z0-9_]{3,}/', $nameSpace) or die("不能有特殊字符，数字不能开头！");
preg_match('/^([a-zA-Z_])*[A-Z]([a-zA-Z0-9_])*/', $nameSpace) or die("至少一个大写字母！");
$fl = (int) $_POST['fl'];
$na = (int) $_POST['na'];
if ($thread == $book || $thread == $forum || $thread == $bag || $book == $forum || $book == $bag || $forum == $bag)
    die("前缀不能相同啊！");
if ($thread != $cfg_thread || $book != $cfg_book || $forum != $cfg_forum || $bag != $cfg_bag || $nameSpace != $cfg_nameSpace) {
    if ($dh = opendir(ROOT_DIR . $cfg_output)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != "..") {
                if ($nameSpace != $cfg_nameSpace && (strpos($file, $cfg_forum . '-') === 0 || strpos($file, $cfg_bag . '-') === 0 || strtolower($file) == 'thread.html')) {
                    $dm = insou(file_get_contents(ROOT_DIR . $cfg_output . '/' . $file));
                    if (strpos($dm, $cfg_nameSpace) !== false) {
                        $dm = str_replace($cfg_nameSpace, $nameSpace, $dm);
                        file_put_contents(ROOT_DIR . $cfg_output . '/' . $file, $dm) or die('出错啦！无法修改js文件！请检查输出目录名称、权限设置是否正确。');
                    }
                }
                if ($thread != $cfg_thread && strpos($file, '.js') !== false && (strpos($file, $cfg_book . '-') === 0 || strpos($file, $cfg_forum . '-') === 0 || strpos($file, $cfg_bag . '-') === 0)) {
                    $dm = insou(file_get_contents(ROOT_DIR . $cfg_output . '/' . $file));
                    if (strpos($dm, '"' . $cfg_thread . '-') !== false) {
                        $dm = str_replace('"' . $cfg_thread . '-', '"' . $thread . '-', $dm);
                        file_put_contents(ROOT_DIR . $cfg_output . '/' . $file, $dm) or die('出错啦！无法修改js文件！请检查输出目录名称、权限设置是否正确。');
                    }
                }
                if ($thread != $cfg_thread && strpos($file, $cfg_thread . '-') === 0 && strpos($file, '.js') !== false) {
                    rename(ROOT_DIR . $cfg_output . '/' . $file, ROOT_DIR . $cfg_output . '/' . str_replace($cfg_thread, $thread, $file)) or die('修改文件名错误！');
                } elseif ($book != $cfg_book && strpos($file, $cfg_book . '-') === 0 && strpos($file, '.js') !== false) {
                    rename(ROOT_DIR . $cfg_output . '/' . $file, ROOT_DIR . $cfg_output . '/' . str_replace($cfg_book, $book, $file)) or die('修改文件名错误！');
                } elseif ($forum != $cfg_forum && strpos($file, $cfg_forum . '-') === 0 && strpos($file, '.js') !== false) {
                    rename(ROOT_DIR . $cfg_output . '/' . $file, ROOT_DIR . $cfg_output . '/' . str_replace($cfg_forum, $forum, $file)) or die('修改文件名错误！');
                } elseif ($bag != $cfg_bag && strpos($file, $cfg_bag . '-') === 0 && strpos($file, '.js') !== false) {
                    rename(ROOT_DIR . $cfg_output . '/' . $file, ROOT_DIR . $cfg_output . '/' . str_replace($cfg_bag, $bag, $file)) or die('修改文件名错误！');
                }
            }
        }
        closedir($dh);
    }
}
$config_data = "<?php
defined('NIUX_INCLUDED') or exit('Access Denied.');
\$cfg_thread = '$thread';
\$cfg_book = '$book';
\$cfg_forum = '$forum';
\$cfg_bag = '$bag';
\$cfg_nameSpace = '$nameSpace';
\$cfg_fl = $fl;
\$cfg_na = $na;
";
file_put_contents('./data/config_gg.php', $config_data) or die('更新配置文件出错！请将程序目录和所有文件的文件权限设置属性0755或0777。');
$nxsql->inLog('广告参数修改');
echo 1;
