<?php

//载入全局配置和函数包
require '../../../include/global.php';
require '../../../include/cacher.php';

//载入模块配置和函数包
Module :: loader( 'test' );
Module :: loader( 'analytic' );

//当前 APPID
$appid = Module :: get_appid();

/////////////////////////////////////////////
	
//获取参数
$mode = getgpc("mode");
$action = getgpc('action');
$do = getgpc('do');
$fid = getnum('id',0);

//加载缓存
if( !Cached :: loader($appid,"form.".$fid) ){
	exit('Cache Error!');
}

//回收站中
if( $_CACHE[$appid]['form']['state'] == -1 ){
	exit('Forbidden');	
}

$skin = $_CACHE[$appid]['form']['skin'];

//连接数据库
System :: connect();
		
////////// 载入通行证 ////////////

if( $_G['licence']['type'] != 'free' ){
	
	//载入函数包
	Module :: loader( 'passport' );
	
	//重定义模板
	if( $_G['setting'][$appid]['templet'] ) $_G['setting']['passport']['theme'] = $_G['setting'][$appid]['templet'];
	$_G['setting']['passport']['hidenaver'] = $_CACHE[$appid]['form']['config']['HIDE_NAVER'];
	$_G['setting']['passport']['hideright'] = $_CACHE[$appid]['form']['config']['HIDE_RIGHT'];
	
	//主动初始用户信息
	Passport :: check( false, false );
}

////////// 验证模板 ////////////

if ( !file_exists( 'theme/'.$skin.'/index.htm' ) ) {
	$skin = 'default';
}

/////////////////////////////////////////////
	
//加载 Smarty 类
require VI_ROOT.'smarty/Smarty.class.php';

$smarty = new Smarty;

//模板路径
$smarty->template_dir = 'theme/'.$skin.'/';

//编译路径
$smarty->compile_dir = create_dir( VI_ROOT.'static/'.$appid );

//编译编号（防止冲突）
$smarty->compile_id = $skin;

$smarty->compile_check = true;

$smarty->debugging = false;

/////////// 全局变量 //////////

//系统变量
$smarty->assign("_G",$_G);


//当前页面绝对地址
$smarty->assign("SELF",GetAbsUrl());

//前台目录绝对地址
$smarty->assign("Index", Module :: get_index($appid) );

//随机颜色函数
$smarty->register_function('color',"rand_color");

//公共导航
$smarty->assign("Naver",Module :: hooks_slice('passport/naver'));

/////////// 表单变量 //////////

//处理表单配置
$config = $_CACHE[$appid]['form']["config"];

//随机取定量的选项组
if( $config["FORM_RAND_LIMIT"] ){
	$_CACHE[$appid]['group'] = array_limit( $_CACHE[$appid]['group'], $config["FORM_RAND_LIMIT"] );
}

//重组选项组配置
foreach( $_CACHE[$appid]['group'] as $gid => $group ){
	//$_CACHE[$appid]['group'][$gid]["config"] = fix_json( $group["config"] );
	$_CACHE[$appid]['group'][$gid]["description"] = stripslashes( str_replace(chr(13),'<br />',ubb_basic($group["description"])) );
}

//处理描述内容
$_CACHE[$appid]['form']["description"] = Module :: parse_form($_CACHE[$appid]['form']);

/////////////////////////////////

//表单模式
$smarty->assign("mode",$mode);

//表单模式
$smarty->assign("appid",$appid);

//动作名称
$smarty->assign("action",$action);

//表单验证
$smarty->assign("verify",rand_string(10));

//表单结构
$smarty->assign("form", $_CACHE[$appid] );

//表单名称
$smarty->assign("Member", $_SESSION['Member']['id'] && $_G['licence']['type'] != 'free' ? Passport :: member( $_SESSION['Member']['id'] ) : array() );

/////////// 个性化 //////////
	
//背景图片
if( !empty($config["PAGE_BG"]) || !empty($config["PAGE_BG_COLOR"]) ){
	$bg = "background:".$config["PAGE_BG_COLOR"].( $config["PAGE_BG"] ? " url(".$config["PAGE_BG"].") " : "" ).$config["PAGE_BG_REPEAT"]." ".$config["PAGE_BG_ATTACHMENT"]." ".$config["PAGE_BG_X"]." ".$config["PAGE_BG_Y"].";";
}

//主体透明
if( !empty($config["PAGE_BG_OPACITY"]) ){
	$op = "filter:alpha(opacity=".$config["PAGE_BG_OPACITY"].");opacity:".($config["PAGE_BG_OPACITY"]/100).";";
}

//页面宽度
if( !empty($config["PAGE_WIDTH"]) ){
	$op .= "width:".$config["PAGE_WIDTH"].$config["PAGE_UNIT"].";";
}

//背景音乐
if( !empty($config["PAGE_SOUND"]) && !$action ){
	$bs = '<bgsound src="'.$config["PAGE_SOUND"].'" autostart="true" loop="'.$config["PAGE_SOUND-LOOP"].'" />';
}

//背景图片
$smarty->assign("background", $bg );

//背景音乐
$smarty->assign("bgsound", $bs );

//页面主体
$smarty->assign("main", $op );

/////////// 加载资源 //////////

//脚本资源
$smarty->assign("script", array(VI_HOST."js/mo.js",VI_HOST."js/mo.ajax.js",VI_HOST."js/mo.ui.js",VI_HOST."js/mo.effect.js",VI_HOST."js/mo.drag.js",VI_HOST."js/mo.form.js") );

//处理动作
switch( $action ){

	//查看表单
	case "":
		//统计系统
		Analytic :: ping( $appid, $fid, 'views' );
	break;

	//提交表单
	case "post":
		require 'include.post.php';
	break;

	//用户结果
	case "state":
		require 'include.state.php';
	break;

}

////////////////////////////

//用户登录验证
if( $_G['licence']['type'] != 'free' && $config['USER_MODE'] == "REG" && !in_array( $action , array('result','status') ) ){

	//载入会员配置
	require VI_ROOT.'module/member/config.php';

	$arr = array();
	
	//记录需要更新的字段
	foreach ($_G['module']['member']["fieds"] as $fied => $name){				
		if( $config["REG_".strtoupper($fied)] ){
			$arr[] = $fied;
		}					
	}
	
	//更新特定字段
	if( count($arr) ){				
		//显示登录窗口，并需要更新资料
		$LOGIN = Passport :: check( true , "['". implode("','",$arr) ."']" );
	}else{
		//显示登录窗口
		$LOGIN = Passport :: check( true , false );				
	}
				
}

//分组类型
$smarty->assign("LOGIN", $LOGIN );

$smarty->display('index.htm');

////////////////////////////

//关闭数据库
System :: connect();
	
?>
