<?php
defined('IN_HS') or exit('Access Denied');

if(!isset($filename) && $file == 'tag' && $action != 'clear')
{
	require_once HS_ROOT.'/include/tag.func.php';
	require_once HS_ROOT.'/include/formselect.func.php';

	$action = $action ? $action : 'manage';
	$actions = array('add','edit','copy','delete','manage','save', 'preview', 'checkname', 'list','category_select', 'quickoperate','listtag','specialid_select','tagcache');
	if(!in_array($action, $actions)) showmessage($LANG['illegal_action']);

	$function = isset($function) ? $function : 'hs_cat';
	$functions = array('hs_cat'=>$LANG['category'], 'hs_type'=>$LANG['type'], 'hs_special_list'=>$LANG['specail_list'], 'hs_special_slide'=>$LANG['specail_slide']);
	if(!array_key_exists($function, $functions)) showmessage($LANG['not_exist']." $function ".$LANG['function_label'], "goback");

	$referer = isset($referer) ? $referer : "?mod=$mod&file=$file";

	$tag_funcs = array(
		'hs_cat'=>'$templateid,$keyid,$catid,$child,$showtype,$open',
		'hs_type'=>'$templateid,$keyid',
		'hs_special_list'=>'$templateid,$keyid,$page,$specialnum,$specialnamelen,$descriptionlen,$iselite,$datenum,$showtype,$imgwidth,$imgheight,$cols',
		'hs_special_slide'=>'$templateid,$keyid,$specialnum,$specialnamelen,$iselite,$datenum,$imgwidth,$imgheight,$timeout,$effectid',
	);

	$submenu = array();
	foreach($functions as $func=>$name)
	{
		$submenu[] = array($name.$LANG['label'], "?mod=$mod&file=$file&action=manage&function=$func");
	}
	$menu = adminmenu($LANG['label_manage'], $submenu);

	require_once HS_ROOT."/include/tree.class.php";
	$tree = new tree();

	require_once HS_ROOT.'/admin/include/tag.class.php';
	$tag = new tag($mod);

	if(!$tag->writeable()) showmessage($LANG['template_dir_not_writeable']);
    if($action == 'add' || $action == 'edit' || $action == 'copy')
	{
		if(!isset($keyid)) $keyid = 1;
		require HS_ROOT.'/admin/'.$file.'_'.$action.'.inc.php';
		exit;
	}
}

switch($action)
{
    case 'list':
		$modules = array();
		foreach($MODULE as $key=>$m)
		{
			if(file_exists(HS_ROOT.'/'.moduledir($key).'/admin/tag.inc.php')) $modules[$key] = $m['name'];
		}
        require HS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/tags.php';
	    include admintpl('tag_list');
	    break;

	case 'quickoperate':
        require HS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/tags.php';
	    $tagname = preg_replace("/^[^{]*[{]?tag_([^}]+)[}]?.*/", "\\1", trim($tagname));
        if(!isset($tags[$tagname])) showmessage($LANG['label'].$LANG['not_exist'], $PHP_REFERER);
		$tag = $tags[$tagname];
		preg_match("/^(([a-z0-9]+)[_][a-z0-9_]*)[(]/", $tag, $m);
		$function = $m[1];
		$mod = $m[2];
		header('location:?mod='.$mod.'&file=tag&action='.$operate.'&function='.$function.'&job='.$job.'&tagname='.urlencode($tagname));
		break;

	case 'listtag':
		if($dosubmit)
	    {
			require HS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/tags.php';
			$content = stripslashes($content);
			preg_match_all("/[{]tag_([^}]+)[}]/", $content, $m);
			$listtags = $m[1];
			$listtags1 = $listtags2 = array();
			foreach($listtags as $tagname)
			{
				isset($tags[$tagname]) ? $listtags2[] = $tagname : $listtags1[] = $tagname;
			}
			$templateinfo = $templatename.($templatename ? '/' : '').$template;
		    include admintpl('tag_listtag','hs');
		}
		else
	    {
			$message = '<form name="myform" method="post" action="?mod=hs&file=tag&action=listtag&module='.$module.'&template='.$template.'&templatename='.$templatename.'&dosubmit=1"><input type="hidden" name="content"></form><script type="text/javascript">myform.content.value=window.opener.document.myform.content.value;myform.submit();</script>';
			showmessage($message);
		}
		break;

	case 'manage':
		$tags = $tag->get_tags_config($function);
		include admintpl('tag_manage','hs');
	    break;

	case 'preview':
		if($tag->exists($tagname) && !isset($tag_config))
		{
			$tag_config = $tag->get_tag_config($tagname);
			$eval = $tag_config['longtag'].';';
			$vars = get_var($eval);
			if($vars === 0 || is_defined($vars))
			{
		        include admintpl('tag_preview', 'hs');
			}
			else
			{
				parse_str($PHP_QUERYSTRING, $hiddens);
            	include admintpl('tag_preview_form', 'hs');
			}
		}
		else
		{
			$function or showmessage($LANG['function_name_not_null']);
			$tagname or showmessage($LANG['label_name_not_null']);
			@extract($tag_config);
			strpos($catid, '$catid') === FALSE or showmessage($LANG['contain_catid_cannot_preview'], 'goback');
			$eval = $function.'('.$tag_funcs[$function].');';
			include admintpl('tag_preview', 'hs');
		}
	    break;

	case 'delete':
		if(!$tag->exists($tagname)) showmessage($LANG['label']." $tagname ".$LANG['not_exist']);
		$tag->update($tagname , '');
		showmessage($LANG['operation_success'], "?mod=$mod&file=$file&channelid=$channelid&function=$function");
	    break;

	case 'checkname':
		if(empty($tagname))
		{
			$message = '<font color="red">'.$LANG['input_label_name'].'</font>';
		}
		else
		{
			if($tag->exists($tagname))
			{
				$message = '<font color="red">'.$LANG['label_name_exist_cannot_use_it'].'</font>';
			}
			else
			{
				$message = '<font color="blue">'.$LANG['label_name_not_exist_can_use'].'</font>';
			}
		}
		include admintpl('tag_checkname', 'hs');
	    break;

    case 'clear':

		dir_delete(HS_ROOT.'/data/tagscache/', 0);

		showmessage($LANG['label_cache_update_success']);
		break;
		
    case 'tagcache':
		$tag->tagcache();
		showmessage($LANG['label_cache_update_success']);
		break;

	case 'category_select':
		if($keyid && strpos($keyid, '$') === FALSE)
	    {
		    $catid = isset($catid) ? intval($catid) : 0;
			$CATEGORY = cache_read('categorys_'.$keyid.'.php');
			echo category_select('setcatid', '', $catid, 'onchange="myform.catid.value=this.value"');
		}
		else
	    {
			echo '<select name="setcatid"><option value="0"></option></select>';
		}
		break;

	case 'specialid_select':
	if($keyid && strpos($keyid, '$') === FALSE)
	    {	
			echo str_replace('<option value=\'0\'>1</option>','<option value="0">不限专题</option><option value=\'$specialid\'>$specialid</option>',special_select($keyid, 'specialid','1','','onchange="$(\'specialid\').value=this.value"'));
		}
		else
	{
				echo '<select name="setcatid" onchange="$(\'specialid\').value=this.value"><option value="0">不限专题</option><option value=\'$specialid\'>$specialid</option></select>';
	}
	break;
}

function get_var($string)
{
	return preg_match_all("/\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+/", $string, $m) ? $m[0] : 0;
}

function is_defined($vars)
{
	@extract($GLOBALS);
	if(is_array($vars))
	{
		foreach($vars as $var)
		{
			$isset = 0;
			eval("\$isset = isset($var);");
			if(!$isset || ($var = '$channelid' && (!isset($CHANNEL[$channelid]) || ($MODULE[$mod]['iscopy'] && $CHANNEL[$channelid]['module'] != $mod)))) return FALSE;
		}
	}
	else
	{
		$isset = 0;
		eval("\$isset = isset($vars);");
		if(!$isset) return FALSE;
	}
	return TRUE;
}
?>