<?php

//载入全局配置和函数包
require("../../../include/global.php");

//载入模板引擎
require(VI_ROOT."/class/template.php");

//载入模块配置和函数包
require(VI_ROOT."/module/function.php");
require(VI_ROOT."/module/form/config.php");

//载入分析和通行证模块
require(VI_ROOT."/module/analytic/function.php");
$Licence['type'] != 'free' ? require(VI_ROOT."/module/passport/function.php") : '';

//获取参数
$mode = GetQuery("mode");
$action = GetQuery("action");
$do = GetQuery("do");
$fid = GetNumber("id",0);

//加载缓存
if( !loader_cache($appid,"forms.".$fid) ){
	exit('Cache Error!');
}

//回收站中
if( $VICACHE["mod.forms"]["status"] == -1 ){
	exit('Forbidden');	
}

$config = fix_json($VICACHE["mod.forms"]["config"]);

$skin = $VICACHE["mod.forms"]["skin"];

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

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 = 'compile/';

$smarty->compile_check = true;

$smarty->debugging = false;

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

//全局配置
$smarty->assign("Global",$Global);

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

//模块配置
$smarty->assign("Setting",$Setting);

//VeryIDE 相对地址
$smarty->assign("BASE",VI_BASE);

//VeryIDE 绝对地址
$smarty->assign("HOST",VI_HOST);

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

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

/////////// 用户信息 //////////

//用户会话
$smarty->assign("Member",$_SESSION["Member"]);

/////////// 勾子相关 //////////

//加载钩子函数
require_once(VI_ROOT."/function/hocker.php");
	
//缓存所有Hock
hocker_push();	

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

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

foreach( $VICACHE["mod.forms"]["groups"] as $gid => $group ){
	
	$config = fix_json( $group["config"] );
	
	$VICACHE["mod.forms"]["groups"][$gid]["config"] = $config;
}

//表单ID
$smarty->assign("fid",$fid);

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

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

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

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

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

//表单结构
$smarty->assign("form", $VICACHE["mod.forms"] );

//表单名称
$smarty->assign("name",$VICACHE["mod.forms"]["name"]);

//表单名称
$smarty->assign("desc",parse_forms($VICACHE["mod.forms"]));

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

//分组类型
$smarty->assign("type", $Modules[$appid]["groups"] );

/////////// 个性化 //////////
	
//背景图片
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_BASE."js/mo.js",VI_BASE."js/mo.ajax.js",VI_BASE."js/mo.ui.js",VI_BASE."js/mo.effect.js",VI_BASE."js/mo.drag.js",VI_BASE."js/mo.form.js") );

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

	//提交表单
	case "post":
		include("include.post.php");
	break;

	//用户结果
	case "status":
		include("include.status.php");
	break;

}

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

//用户登录验证
if( $Licence['type'] != 'free' && $config['USER_MODE'] == "REG" && !in_array( $action , array('result','status') ) ){
	
	//载入会员配置
	require(VI_ROOT."/module/member/config.php");

	$arr = array();
	
	//记录需要更新的字段
	foreach ($Modules["member"]["fieds"] as $fied => $name){				
		if( $config["REG-".$fied] ){
			$arr[] = $fied;
		}					
	}
	
	//更新特定字段
	if( count($arr) ){				
		//显示登录窗口，并需要更新资料
		$check = passport_check( true , "['". implode("','",$arr) ."']" );
	}else{
		//显示登录窗口
		$check = passport_check( true , false );				
	}
	
}else{				
	//统计系统
	$check = analytic_ping($appid,$fid,'view');				
}

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

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


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

/////////////////////////////////////////////
	
?>
