<?php
ob_start();
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
@set_magic_quotes_runtime(0);
@ini_set('memory_limit', '128M');
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}
$system_start = getSysTime();
$_SET = array();
define('R_P', dirname(dirname(__FILE__)));
require_once(R_P . '/config/config.php');
require_once(R_P . '/config/options.php');
define('A_P', R_P . '/' . $_SET['admin_path']);
define('C_P', R_P . '/cache');
define('I_P', A_P . '/include');
define('M_P', A_P . '/module');
define('T_P', A_P . '/templates/');
ini_get('session.auto_start') || @session_start();
define('AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
define('JSONP', (isset($_GET['jsonp']) && intval($_GET['jsonp'])) ? true : false);
define('DEBUG', intval($_SET['debug']) ? true : false);
define('CACHE_VERSION', intval($_SET['version']) ? true : false);
define('W_P', $_SET['weburl'].$_SET['webpath']);
define('REWRITE', $_SET['rewrite'] ? true : false);
define('TABLEPRE', $_SET['tablepre']);
define('QMECMS_CONNECT', 'mysql');
if(substr(trim($_SET['net_static_host']), 0, 4) == 'http'){
	define('AW_P', $_SET['net_static_host'] . '/' . $_SET['admin_path'] . '/');
	define('WW_P', $_SET['net_static_host'] . '/' );
}else{
	define('AW_P', '');
	define('WW_P', '../');
}
header("Content-Type: text/html; charset=".$_SET['def_charset']);
$magic_quote = get_magic_quotes_gpc();
$path = "";
require_once(R_P . '/include/class.db.php');
require_once(R_P . '/include/inc.functions.php');
require_once(R_P . '/include/inc.adminfunctions.php');
require_once(R_P . '/include/inc.admin.common.php');
require_once(R_P . '/include/class.pager.php');
require_once(R_P . '/include/class.cache_'. $_SET['cache'] .'.php');
require_once(R_P . '/include/class.html.php');
require_once(R_P . '/config/admin_options.php');
require_once(R_P . '/include/class.qmecms.php');
$_GET = qbase::saddslashes($_GET);
$_POST = qbase::saddslashes($_POST);
$_COOKIE = qbase::saddslashes($_COOKIE);
$channel = $usergroup = $_MODULE = $manage_channel = $manage_acc_channel = $manage_category = $manage_other = array();
if(!is_file(R_P . '/config/install.lock')){
	qbase::showmsg("Need to install", "default", "../install.php");
}
if(!@include(R_P . '/cache/config/common.php')){
	commonCache();
	@include(R_P . '/cache/config/common.php');
}
require_once(R_P . '/include/global/global.php');
if(strpos($_SERVER['HTTP_ACCEPT'], "text/") === false && strpos($_SERVER['HTTP_ACCEPT'], "*/*") === false) die("access die");
if(AJAX){
	$xml = array("<?xml version=\"1.0\" encoding=\"{$_SET['def_charset']}\"?><root><![CDATA[", "]]></root>");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Content-Type: text/xml; charset={$_SET['def_charset']}");
}else if(JSONP){
	foreach($_GET as $key=>$val){
		if(substr((string)$key, 0, 6) == '_post_'){
			$_POST[substr((string)$key, 6)] = urldecode($val);
		}
	}
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Content-Type: application/x-javascript; charset={$_SET['def_charset']}");
}
adminGetCookie();
$admin_group = array(
	1=>'超级管理员',
	2=>'普通管理员',
	3=>'频道管理员',
	4=>'栏目管理员',
);
if(!defined('LOGIN')){
	checkAdminPath();
	adminCheck();
	if( $admin_info['groupid'] > 2){
		$purviewCacheFile = C_P."/config/admin/".$admin_info['uid'].".php";
		if( !is_file($purviewCacheFile)) cachePurview();
		if( is_file($purviewCacheFile)) @include_once(C_P."/config/admin/".$admin_info['uid'].".php");
	}
	if ( isset($_GET['channelid'])){
		$channelid = intval($_GET['channelid']);
		if ($channelid > 0){
			if( array_key_exists( $channelid, $channel)){
				purviewCheck( $channelid, 'acc_channel');
				$channel[$channelid] = qdb::queryfirst("select * from ".TABLEPRE."channel where channelid='$channelid' limit 1");
				unset($channel[$channelid]['id']);
				@extract( $channel[$channelid]);
				if ( $channel[$channelid]['domain']){
					$url  = $channel[$channelid]['domain'];
				}else{
					$url  = $_SET['weburl'].$_SET['webpath'].$channeldir;
				}
				$mod = $_MODULE[$channel[$channelid]['channelselect']]['table_name'];
				if(empty($mod)) qbase::purviewCheckDie("error request mod");

				$menuTree = $strall = '';
				$menus = array();
				if(false === ($menus = cache::get('sys_category_' . $channelid))){
					$menus = qbase::get_object('category', $channelid)->cache();
				}

				require_once(R_P . '/include/class.extend.php');
				if( $admin_info['groupid'] < 3 || in_array( $channelid, $manage_channel)){
					foreach( $menus as $val){
						$menuTree.= "<option value=\"{$val['cid']}-{$val['child']}\" rel=\"{$val['pid']}\">". str_repeat('|--', $val['vlayer']) ."{$val['title']}</option>";
					}
				}else{
					foreach( $menus as $val){
						if ( in_array($val['cid'], $manage_category[$channelid])){
							$menuTree.= "<option value=\"{$val['cid']}-{$val['child']}\" rel=\"{$val['pid']}\">". str_repeat('|--', $val['vlayer']) ."{$val['title']}</option>";
						}else{
							$menuTree.= "<OPTGROUP rel=\"{$val['pid']}\" label=\"". str_repeat('|--', $val['vlayer']) ."{$val['title']}\"></OPTGROUP>";
						}
					}
				}
				$strall = str_replace("value=\"{$ppcid}-", " selected value=\"{$ppcid}-", $menuTree);
			}else{
				$n = qdb::queryAlone("select count(*) from ".TABLEPRE."channel where channelid='$channelid' limit 1");	
				if( $n == 0){
					qbase::purviewCheckDie("error request");
				}
			}
		}
	}
	$pageno = intval($_GET['pageno']) < 1 ? 1 : intval($_GET['pageno']);
	if(($pagesize = intval($_GET['pagesize'])) < 1){
		($pagesize = min(max(intval($_COOKIE['_ps']), 0),200)) || $pagesize = 15;
	}
	$systemMaxUpload = min(formatSystemSize(@ini_get("upload_max_filesize")), formatSystemSize(@ini_get("post_max_size"))) ;
	$filterData = getFilterData();
	$filterDataJson = qbase::jsonencode($filterData);
}

function formatSystemSize($sys){
	$n = intval($sys);
	$sys = strtoupper($sys);
	if(strpos($sys, "G") !== false){
		return $n * 1024*1024;
	}else if(strpos($sys, "M") !== false){
		return $n * 1024;
	}else{
		return $n;
	}
}

function getSysTime(){
	$time = explode(' ', microtime());
	return $time[1] + $time[0];
}

function getFilterData(){
	$temp = qdb::queryAlone("select v from ".TABLEPRE."config where g = 'author' and k = 'filter' ");
	return explode("\n", $temp);
}

function checkAdminPath(){
	global $_SET;
	$main_location = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	$main_path = parse_url($main_location);
	$temp = explode("/", $main_path['path']);
	if($temp[count($temp)-2] != $_SET['admin_path']){
		adminOut();
		qbase::showmsg('出错了，您需要重新登录', 'default', W_P . $_SET['admin_path'] . '/index.php');
	}
}
?>