<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	模块勾子函数包
*
*	$Id: hocker.php,v2.0 12:25 2010-4-18 Leilei $
*/

//初始化Hock
$HOCKERS = array();

/*
	扫描模块，缓存所有Hock
	$modid 	模块ID
	$state	设置状态
			true 启用 false 禁用
*/
function hocker_push($debug=false){
	global $Project;
	global $Modules;
	global $VICACHE;
	global $HOCKERS;
	
	/*
	
	$HOCKERS = array(
					"x" = array("aaaaaaaaaaaa","bbbbbbbbbbbb"),
					"y" = array("cccccccccccc","dddddddddddd"),
					);
	
	*/
			
	//var_dump($VICACHE["sys.module"]);
	
	//遍历模块
	foreach($VICACHE["sys.module"] as $key){
			
		if( isset($key["config"]) && $key["config"] && $key["status"] ){
		
			//conf
			$conf = VI_ROOT.$key["config"];
			
			//func
			$func = str_replace("config.php","function.php",$conf);
			
			/*
			echo $key["appid"];
			echo '<br />';
			
			echo $conf;
			echo '<br />';
			
			echo $func;
			echo '<br />';
			*/
			
			//忽略不存在的模块
			if( !file_exists($conf) || !file_exists($func) ) continue;
			
			//载入 conf
			require_once($conf);
			
			//有配置hock选项
			if( isset($Modules[$key["appid"]]["hock"]) && is_array($Modules[$key["appid"]]["hock"]) ){
				
				//载入 func
				require_once($func);
				
				foreach( $Modules[$key["appid"]]["hock"] as $hck => $fun ){
					
					if( function_exists($fun) ){						
						if( is_array($HOCKERS[$hck]) ){						
							array_push($HOCKERS[$hck],$fun);
						}else{
							$HOCKERS[$hck] = array($fun);
						}					
					}
						
				}
			}
			
		}
	
	}
	
	if($debug){
		echo '<pre>';
		var_dump($HOCKERS);
		echo '</pre>';
	}
	
}

/*
	输出当前 Hock
	$key	Hock索引
*/
function hocker_slice($key){
	global $Project;
	global $Modules;
	global $VICACHE;
	global $HOCKERS;
	
	$res = '';
	
	//var_dump($HOCKERS[$key]);
	
	if( !isset($HOCKERS[$key]) || !is_array($HOCKERS[$key]) ) return "<!--hock $key null-->";
	
	//遍历模块
	foreach($HOCKERS[$key] as $val){
		
		if( function_exists($val) ){
			$res .= $val();
		}else{
			$res .= $val;	
		}		
		
	}
	return $res."<!--$key hocked-->";
	
}

/*
	系统导航栏快捷菜单
	$appid	模块ID
*/
function hocker_quick( $appid ){
	global $Project;
	global $Modules;
	
	//模块
	if( $appid ){
		
		//xml
		$xml = VI_ROOT.$Project["module"].$appid."/setting.xml";
		
		//php
		$php = str_replace("setting.xml","setting.php",$xml);
		
		//忽略不存在的模块
		if( !file_exists($xml) || !file_exists($php) ) return;			
	
		return '<li class="menu"><a href="'.VI_BASE.'/module/system/serve.control.php?appid='.$appid.'" onclick="parent.Mo.Dialog(\'dialog\',\'模块配置 - '.$Modules[$appid]["name"].'\' , \'<iframe src=\'+this.href+\' frameborder=0></iframe>\' ,\'800\',\'60%\',{ \'remove\' : true, \'draged\' : true, \'create\' : true }); return false;">⊙更改配置</a></li>';
		
	//系统
	}else{
		
		echo '<ul id="passage" class="select"><li><a href="veryide.start.php">欢迎使用</a></li><li><a href="veryide.avatar.php">用户中心</a></li><li><a href="veryide.setting.php">系统管理</a></li><li><a href="system.secure.php">安全中心</a></li></ul>';
	
		return '<li class="menu" onmouseover=\'Mo.Tips(null,this,"passage",null,document,"click",5,27);\'><a href="javascript:void(0);">快速通道&#9660;</a></li>';
		
	}
	
}

?>