<?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.');

function autoLoad($class) {
    require_once ADMIN_DIR . 'lib' . DIRECTORY_SEPARATOR . 'class_' . $class . '.php';
}

function theme() {
    return htmlspecialchars(empty($_COOKIE['niuxams_theme']) ? 'smoothness' : $_COOKIE['niuxams_theme']);
}

function host() {
    return addslashes(empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] : $_SERVER['HTTP_HOST'] );
}

function selfPath() {
    return addslashes(empty($_SERVER['PHP_SELF']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']);
}

function selfName() {
    return basename(strtolower(selfPath()), '.php');
}

function selfUrl() {
    return 'http://' . host() . selfPath();
}

function adminName() {
    global $cfg_dbprefix, $cfg_strictLogin;
    return addslashes($cfg_strictLogin ? $_SESSION[$cfg_dbprefix . 'niuxams_admin'] : $_COOKIE[$cfg_dbprefix . 'niuxams_admin']);
}

function adminPwd() {
    global $cfg_dbprefix, $cfg_strictLogin;
    return addslashes($cfg_strictLogin ? $_SESSION[$cfg_dbprefix . 'niuxams_pws'] : $_COOKIE[$cfg_dbprefix . 'niuxams_pws']);
}

function checkAccess() {
    global $cfg_dbprefix, $cfg_strictLogin, $cfg_strictLog, $nxsql;
    session_start();
    $loginName = adminName();
    $loginPwd = adminPwd();
    if (!(preg_match('/^([0-9a-zA-Z@._]){3,20}$/', $loginName) && preg_match('/^([0-9a-z]){32}$/', $loginPwd) && (!$cfg_strictLogin || strpos($_SERVER['HTTP_REFERER'], 'http://' . host()) === 0))) {
        header('Location: login.php');
        exit;
    }
    $query = "SELECT id,pwd,power_id,power_lb,lb_id FROM `${cfg_dbprefix}admin` WHERE name='${loginName}';";
    if (list($id, $pwd, $power_id, $power_lb, $lb_id) = $nxsql->oneRowVal($query)) {
        if (md5($cfg_dbprefix . $pwd . $cfg_dbprefix) != $loginPwd) {
            $nxsql->inLog('身份验证', 0, $loginName);
            header('Location: login.php');
            exit;
        }
    } else {
        $nxsql->inLog('身份验证', 0, $loginName);
        header('Location: login.php');
        exit;
    }
    $query = "SELECT content FROM `${cfg_dbprefix}admin_power` WHERE id=${power_id};";
    if ($content = $nxsql->oneRowVal($query)) {
        $allowAccess = explode('|', rtrim($content));
        $selfName = selfName();
        if (!in_array('@all@', $allowAccess) && !in_array($selfName, $allowAccess) && $selfName != 'index' && $selfName != 'main' && $selfName != 'logout') {
            errWin('抱歉，您没有此功能的权限！');
        }
    } else {
        errWin('抱歉，您没有权限功能！');
    }
    return array($id, $loginName, $power_id, $power_lb, $lb_id);
}

function errWin($msg) {
    $title = '出错提示';
    require_once './mo_head.php';
    echo "<script>\$(function () {dialog('" . str_replace("'", '"', $msg) . "');});</script>";
    require_once './mo_foot.php';
    exit;
}

function okWin($msg, $url = '') {
    $title = '成功提示';
    require_once './mo_head.php';
    echo "<script>\$(function () {dialog('" . str_replace("'", '"', $msg) . "',1,'$url');});</script>";
    require_once './mo_foot.php';
    exit;
}

function inSql($in) {
    if (get_magic_quotes_gpc()) {
        return $in;
    } else {
        return addslashes($in);
    }
}

function toSql($in) {
    return addslashes(stripslashes($in));
}

function insou($in) {
    if (get_magic_quotes_runtime()) {
        $in = is_array($in) ? array_map('insou', $in) : stripslashes($in);
        return $in;
    } else {
        return $in;
    }
}

function urlEscape($str) {
    $str = urlencode($str);
    $str = str_replace('%2F', '/', $str);
    return $str;
}

function unescape($str) {
    $ret = '';
    for ($i = 0, $len = strlen($str); $i < $len; $i++) {
        if ($str[$i] == '%' && $str[$i + 1] == 'u') {
            $val = hexdec(substr($str, $i + 2, 4));
            if ($val < 0x7f) {
                $ret .= chr($val);
            } else if ($val < 0x800) {
                $ret .= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
            } else {
                $ret .= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));
            }
            $i += 5;
        } else if ($str[$i] == '%') {
            $ret .= urldecode(substr($str, $i, 3));
            $i += 2;
        } else {
            $ret .= $str[$i];
        }
    }
    return $ret;
}

function cutStr($str, $limit, $ext = '..') {
    $strLen = strlen($str);
    if ($strLen <= $limit / 2) {
        return $str;
    }
    $pre = chr(1);
    $end = chr(1);
    $str = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), $str);
    $isExt = '';
    $n = $tn = $noc = 0;
    while ($n < $strLen) {
        $t = ord($str[$n]);
        if ($t == 77) {
            $tn = 1;
            $n++;
            $noc += 1.9;
        } elseif ($t == 87) {
            $tn = 1;
            $n++;
            $noc += 1.95;
        } elseif ($t == 109) {
            $tn = 1;
            $n++;
            $noc += 1.8;
        } elseif ($t == 119) {
            $tn = 1;
            $n++;
            $noc += 1.5;
        } elseif ($t >= 65 && $t <= 90) {
            $tn = 1;
            $n++;
            $noc += 1.24;
        } elseif ($t >= 32 && $t <= 126) {
            $tn = 1;
            $n++;
            $noc++;
        } elseif ($t >= 224 && $t <= 239) {
            $tn = 3;
            $n += 3;
            $noc += 1.9;
        } elseif ($t == 9 || $t == 10) {
            $tn = 1;
            $n++;
            $noc++;
        } elseif ($t < 32) {
            $n++;
        } elseif (194 <= $t && $t <= 223) {
            $tn = 2;
            $n += 2;
            $noc += 1.8;
        } elseif (240 <= $t && $t <= 247) {
            $tn = 4;
            $n += 4;
            $noc += 1.5;
        } elseif (248 <= $t && $t <= 251) {
            $tn = 5;
            $n += 5;
            $noc += 1.5;
        } elseif ($t == 252 || $t == 253) {
            $tn = 6;
            $n += 6;
            $noc += 1.5;
        } else {
            $n++;
        }
        if ($noc > $limit) {
            $isExt = $ext;
            break;
        }
    }
    if ($noc > $limit) {
        $n -= $tn;
    }
    $reStr = substr($str, 0, $n);
    $reStr = str_replace(array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), array('&amp;', '&quot;', '&lt;', '&gt;'), $reStr);
    $pos = strrpos($reStr, chr(1));
    if ($pos !== false) {
        $reStr = substr($reStr, 0, $pos);
    }
    return $reStr . $isExt;
}

function dscape($str) {
    $ret = '';
    for ($i = 0, $len = strlen($str); $i < $len; $i++) {
        $ret .= chr(ord($str[$i]) - $i);
    }
    return $ret;
}

function escape($str) {
    $ret = '';
    for ($i = 0, $len = strlen($str); $i < $len; $i++) {
        $ret .= chr(ord($str[$i]) + $i);
    }
    return $ret;
}

function cifr() {
    return trim(file_get_contents('./data/index.html'));
}

function wno() {
    return file_put_contents('./data/index.html', ' ');
}

function wye() {
    return file_put_contents('./data/index.html', '1');
}

function cifa() {
    global $cloud, $cfg_amsUrl;
    return trim(file_get_contents($cloud . 'authorization.php?domain=' . $_SERVER['SERVER_NAME'] . '&amsurl=' . $cfg_amsUrl));
}

function ca() {
    cifa() ? wye() : wno();
}

function cag() {
    ca();
    cifr() || upg();
}

function upg() {
    global $cloud;
    $O0 = file_get_contents($cloud . 'gg/maingg.html') or die('0');
    file_put_contents('./data/maingg.html', insou($O0));
    $O0 = file_get_contents($cloud . 'gg/logingg.html') or die('0');
    file_put_contents('./data/logingg.html', insou($O0));
    die('1');
}

function ccifr() {
    global $cloud;
    strpos($cloud, escape('nhsuoj`m&Zeb')) || wno();
}

function yzjz() {
    cifr() || errWin('抱歉，您没有权限功能！');
}
