<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.1
|   ========================================
|   by Matthew Mecham
|   (c) 2001,2002 Invision Power Services
|   http://www.ibforums.com
|   ========================================
|   Web: http://www.ibforums.com
|   Email: phpboards@ibforums.com
|   Licence Info: phpib-licence@ibforums.com
+---------------------------------------------------------------------------
|
|   > Skin -> Templates pop up functions
|   > Module written by Matt Mecham
|   > Date started: 9th July 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/




$idx = new ad_settings();


class ad_settings {

	var $base_url;

	function ad_settings() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}

		switch($IN['code'])
		{
			case 'preview':
				$this->do_preview();
				break;
				
			case 'edit_bit':
				$this->edit_bit();
				break;
				
			case 'macro_one':
				$this->macro_one();
				break;
				
			case 'macro_two':
				$this->macro_two();
				break;
				
			case 'compare':
				$this->compare_frames();
				break;
				
			case 'dotop':
				$this->print_compare_top();
				break;
				
			case 'donew':
				$this->print_compare_new();
				break;
			
			//-----------------------------
			
			case 'search':
				$this->search_frames();
				break;
				
			case 'searchbox':
				$this->print_search_box();
				break;
				
			case 'searchlinks':
				$this->print_searchlinks();
				break;
			
			//-----------------------------
			
			case 'css_search':
				$this->css_search_frames();
				break;
				
			case 'csssearchlinks':
				$this->print_css_searchlinks();
				break;
				
			case 'previewstate':
				$this->do_message("预览窗口");
				break;
				
			//-------------------------
			
			case 'css_preview':
				$this->css_preview();
				break;
				
			default:
				exit();
				break;
		}
		
	}
	
	//+---------------------------------------
	
	function css_search_frames()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_GET_VARS;
		
		print "<html>
				 <head>
				 <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
				 <meta name='author' content='Powered By HuaCooL Network'>
				 <meta name='copyright' content='Copyright 2002-2003 www.ibfchina.com, HuaCooL Network'>
				 <title>搜索</title>
				 </head>
				   <frameset cols='200, *' frameborder='no' border='1' framespacing='0'>
					<frame name='links' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=csssearchlinks&id={$IN['id']}&element={$HTTP_GET_VARS['element']}'>
					<frame name='preview' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=previewstate'>
				   </frameset>
			   </html>";
			   
		exit();
	}
	
	//+---------------------------------------
	
	function css_preview()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_GET_VARS;
		
		//+---------------------------------------
		// GET THE TEMPLATES THAT THIS CSS USES
		//+---------------------------------------
		
		$DB->query("SELECT css_text, css_name FROM ibf_css WHERE cssid='".$IN['id']."'");
		
		if ( ! $set = $DB->fetch_row() )
		{
			$ADMIN->error("无法找到相关的数据记录");
		}
		
		$DB->query("SELECT img_dir FROM ibf_skins WHERE css_id='".$IN['id']."'");
		
		$skin = $DB->fetch_row();
		
		$element = trim(stripslashes($HTTP_GET_VARS['element']));
		
		
		list($type, $name) = explode( "|", $element );
		
		$like = "class='{$name}'";
		$first = '.';
		
		if ($type == 'id')
		{
			$like = "id='{$name}'";
			$first = '#';
		}
		
		preg_match( "/($first"."$name)\s{0,}\{(.+?)\}/s", $set['css_text'], $match );
		
		//$definition = nl2br( str_replace( "\n\n", "\n", str_replace( "\r\n", "\n", $match[2]) ) );
		
		//preg_match_all( "/(\S+?):(.+?);?/s", trim($match[2]), $defs );
		
		$defs = explode( ";", str_replace( "\n\n", "\n", str_replace( "\r\n", "\n", trim($match[2]) ) ) );
		
		$def_output = "";
		
		foreach($defs as $bit)
		{
			list($type, $value) = explode( ":", trim($bit) );
			
			$type = trim($type);
			
			$value = trim($value);
			
			if ($type != "" and $value != "")
			{
			    $extra = "";
			    
				if ($type == 'color' or $type == 'background-color')
				{
					$extra = "&nbsp;&nbsp;&nbsp;<input type='text' size='6' style='background-color:$value' readonly>";
				}
			
				$def_output .= "<tr><td width='40%'><b>$type</b></td><td width='60%'>$value $extra</td></tr>\n";
			}
		}
	
		$css = "\n<style>\n<!--\n".str_replace( "<#IMG_DIR#>", $skin['img_dir'], $set['css_text'] )."\n//-->\n</style>";
		
    	$html = "<html>
    	         <head>
				 <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
				 <meta name='author' content='Powered By HuaCooL Network'>
				 <meta name='copyright' content='Copyright 2002-2003 www.ibfchina.com, HuaCooL Network'>
    	              <title>CSS 预览</title>
    	              $css
    	           </head>
    	           <body topmargin='0' leftmargin='0' rightmargin='0' marginwidth='0' marginheight='0' alink='#000000' vlink='#000000'>
    	           <table border='1' width='95%' cellspacing='0' cellpadding='4' align='center'>
    	           <tr>
    	            <td bgcolor='#EEEEEE' style='font-size:14px'><b>预览CSS文件部分：'$name'<br>从CSS文件：'{$set['css_name']}'</b></td>
    	           </tr>
    	           </table>
    	           <br>
    	           <table border='1' width='95%' cellspacing='0' cellpadding='4' align='center'>
    	           <tr>
    	            <td><b>预览</b></td>
    	           </tr>
    	           <tr>
    	           	<td $like>欢迎使用华酷论坛：http://www.ibfchina.com</td>
    	           	</tr>
    	           	</table>
    	           	<br>
    	           	<table border='1' width='95%' cellspacing='0' cellpadding='4' align='center'>
    	            <tr>
    	             <td colspan='2'><b>CSS 定义格式</b></td>
    	            </tr>
    	              $def_output
    	           </table>
    	           </body>
    	         </html>
    	        ";
    	        
		print $html;
		
		exit();
    	        
    }
    	        
   	//+---------------------------------------
   	//+---------------------------------------
	
	function print_css_searchlinks()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_GET_VARS;
		
		//+---------------------------------------
		// GET THE TEMPLATES THAT THIS CSS USES
		//+---------------------------------------
		
		$DB->query("SELECT set_id FROM ibf_skins WHERE css_id='".$IN['id']."'");
		
		if ( ! $set = $DB->fetch_row() )
		{
			$ADMIN->error("无法找到相关的数据记录");
		}
		
		$element = trim(stripslashes($HTTP_GET_VARS['element']));
		
		
		list($type, $name) = explode( "|", $element );
		
		$like = "class=_{$name}";
		
		if ($type == 'id')
		{
			$like = "id=_{$name}";
		}
		
		require './sources/Admin/skin_info.php';
		
		//die("SELECT suid, set_id, group_name, func_name FROM ibf_skin_templates WHERE set_id='".$set['tmpl_id']."' AND section_content LIKE '%".$like."%' ORDER BY group_name");
		
		$DB->query("SELECT suid, set_id, group_name, func_name FROM ibf_skin_templates WHERE set_id='".$set['set_id']."' AND section_content LIKE '%".$like."%' ORDER BY group_name");
		
		if (! $DB->get_num_rows() )
		{
			$this->do_message("无法在论坛模板文件中找到相关数据：{$set['set_id']}");
		}
		
		$results = array();
		
		while ( $r = $DB->fetch_row() )
		{
			if ( ! isset($result[ $r['group_name'] ]) )
			{
				$result[ $r['group_name'] ] = array();
			}
			
			$result[ $r['group_name'] ][] = array( 'suid' => $r['suid'], 'func_name' => $r['func_name'] );
			
		}
		
		$ADMIN->html .= $SKIN->start_table();
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜索结果</b>" )  , 'title' );
		
		foreach( $result as $group_name => $sub_array )
		{
			if ( isset($skin_names[ $group_name ]) )
			{
				$group_name = $skin_names[ $group_name ][0];
			}
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>$group_name</b>" )  , 'catrow' );
			
			if (is_array($sub_array) and count($sub_array) > 0 )
			{
				foreach( $sub_array as $idx => $data )
				{
					$ADMIN->html .= $SKIN->add_td_row( array( "+ <a href='{$SKIN->base_url}&act=rtempl&code=preview&suid={$data['suid']}&type=text&hl=".urlencode($name)."' target='preview'>{$data['func_name']}</a>" )  );
				}
			}
		}
										 
		
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
		
	}
	
	//+---------------------------------------
	//+---------------------------------------
	
	function print_searchlinks()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['bypass'] == 1)
		{
			$this->do_message("没有找到搜索结果");
		}
		
		$search_text = trim($this->unconvert_tags(stripslashes($HTTP_POST_VARS['search'])));
		
		$search_text = str_replace( "\$", "\\$", $search_text);
		$search_text = str_replace( "'" , "\\'", $search_text);
		
		if ($search_text == "")
		{
			$this->do_message("输入需要进行搜索的字段");
		}
		
		require './sources/Admin/skin_info.php';
		
		$DB->query("SELECT set_id FROM ibf_skin_templates WHERE suid='".$IN['suid']."'");
		
		$set = $DB->fetch_row();
		
		$DB->query("SELECT suid, set_id, group_name, func_name FROM ibf_skin_templates WHERE set_id='".$set['set_id']."' AND section_content LIKE '%$search_text%' ORDER BY group_name");
		
		if (! $DB->get_num_rows() )
		{
			$this->do_message("无法找到相匹配的字段");
		}
		
		$results = array();
		
		while ( $r = $DB->fetch_row() )
		{
			if ( ! isset($result[ $r['group_name'] ]) )
			{
				$result[ $r['group_name'] ] = array();
			}
			
			$result[ $r['group_name'] ][] = array( 'suid' => $r['suid'], 'func_name' => $r['func_name'] );
			
		}
		
		$ADMIN->html .= $SKIN->start_table();
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜索结果</b>" )  , 'title' );
		
		foreach( $result as $group_name => $sub_array )
		{
			if ( isset($skin_names[ $group_name ]) )
			{
				$group_name = $skin_names[ $group_name ][0];
			}
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>$group_name</b>" )  , 'catrow' );
			
			if (is_array($sub_array) and count($sub_array) > 0 )
			{
				foreach( $sub_array as $idx => $data )
				{
					$ADMIN->html .= $SKIN->add_td_row( array( "+ <a href='{$SKIN->base_url}&act=rtempl&code=preview&suid={$data['suid']}&type=text&hl=".urlencode(stripslashes($HTTP_POST_VARS['search']))."' target='preview'>{$data['func_name']}</a>" )  );
				}
			}
		}
										 
		
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
		
	}
	
	//+---------------------------------------
	
	function do_message($message="")
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->html = "<tr><td id='tdrow1' height='100%' align='center' valign='middle'><br><br><b>$message</b><br><br>&nbsp;</td></tr>";
		
		$ADMIN->print_popup();
		
	}
	
	//+---------------------------------------
	
	function print_search_box()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'searchlinks'   ),
												  2 => array( 'act'   , 'rtempl'        ),
												  3 => array( 'suid'  , $IN['suid']   )
									     )  ,'theform', 'target="links"'    );
									     
		//+-------------------------------
		
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table(  );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>输入需要搜索的文本内容：</b><br>".
										          $SKIN->form_input( 'search' )
										 )      );
										 
		$ADMIN->html .= $SKIN->end_form("搜索");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
		
	}
	
	//+---------------------------------------
	
	function search_frames()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		print "<html>
				 <head>
				   <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
				   <meta name='author' content='Chinese Version Development By HuaCooL Network'>
				   <meta name='copyright' content='Copyright 2002 www.ibfchina.com, HuaCooL Network'>			<title>搜索</title>
				 </head>
				   <frameset cols='200, *' frameborder='no' border='1' framespacing='0'>
					 <frameset rows='*, 100' frameborder='no' border='1' framespacing='0'>
					   <frame name='links' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=searchlinks&suid={$IN['suid']}&bypass=1'>
					   <frame name='searchbox' scrolling='no' src='{$SKIN->base_url}&act=rtempl&code=searchbox&suid={$IN['suid']}'>
					 </frameset>
					<frame name='preview' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=previewstate'>
				   </frameset>
			   </html>";
			   
		exit();
	}
	
	//+---------------------------------------
	
	function compare_frames()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		print "<html>
				 <head>
				   <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
				   <meta name='author' content='Chinese Version Development By HuaCooL Network'>
				   <meta name='copyright' content='Copyright 2002 www.ibfchina.com, HuaCooL Network'>
				   <title>比较</title>
				 </head>
				   <frameset cols='50%, 50%' frameborder='yes' border='1' framespacing='0'>
				     <frameset rows='30, *' frameborder='yes' border='1' framespacing='0'>
				       <frame name='origtop' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=dotop&content=orig'>
				       <frame name='origbot' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=preview&suid={$IN['suid']}&type=css'>
				     </frameset>
					<frameset rows='30, *' frameborder='yes' border='1' framespacing='0'>
				       <frame name='newtop' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=dotop&content=new'>
				       <frame name='newbot' scrolling='auto' src='{$SKIN->base_url}&act=rtempl&code=donew&suid={$IN['suid']}'>
				     </frameset>
				   </frameset>
			   </html>";
			   
		exit();
	}
	
	//+---------------------------------------
	
	function print_compare_top()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$content = $IN['content'] == 'orig' ? '默认模板' : '当前模板';
		
		print "<html>
			   <body marginheight='0' marginwidth='0' leftmargin='0' topmargin='0' bgcolor='#000055'>
			   <center><font face='verdana' size='2' color='white'><b>$content</b></font></center>
			   </body></html>";
		
		exit();
	}
	
	//+---------------------------------------
	
	function print_compare_new()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE suid='".$IN['suid']."'");
		
		if ( ! $template = $DB->fetch_row() )
		{
			$ADMIN->error("你必须指定一个存在的模板ID，请返回后重试");
		}
		
		$DB->query("SELECT css_id, img_dir FROM ibf_skins WHERE set_id='".$template['set_id']."'");
		$r = $DB->fetch_row();
		
		$DB->query("SELECT css_text FROM ibf_css WHERE cssid='".$r['css_id']."'");
		
		$css = $DB->fetch_row();
		
		$css_text = "\n<style>\n<!--\n".str_replace( "<#IMG_DIR#>", "style_images/".$r['img_dir'], $css['css_text'] )."\n//-->\n</style>";
		
		print "<html><head>
				$css_text
				
				</head>
				<body>
				<script language='Javascript'>
				
					templatedata = window.parent.opener.document.theform.template.value;
					
					document.write( templatedata);
					document.close();
					
				</script>
				</body></html>
				";
				
		exit();
		
		
	}
	
	//+---------------------------------------
	
	function macro_one()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'macro_two'   ),
												  2 => array( 'act'   , 'rtempl'       ),
												  3 => array( 'suid'  , $IN['suid']   )
									     )      );
									     
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"   , "60%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "40%" );

		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "搜索论坛模板变量" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>输入需要搜索的论坛模板变量</b><br>(例如：{ibf.skin.tbl_width})",
										          $SKIN->form_input( 'lookup' )
										 )      );
										 
		$ADMIN->html .= $SKIN->end_form("搜索");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
		
	}
	
	//+-------------------------------
	
	function macro_two()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['lookup'] == "")
		{
			$ADMIN->error("你必须输入需要搜索的论坛模板变量", 1);
		}
		
		$is_valid = 0;
		$macro    = "";
		$extra    = "";
		
		if ( preg_match( "/&lt;\{(\S+)\}&gt;/", $IN['lookup'], $match ) )
		{
			$is_valid = 1;
			$macro    = $match[1];
		}
		
		else if ( preg_match( "/{ibf\.(skin|lang|vars|member)\.(\w+)}$/", $IN['lookup'], $match ) )
		{
			$is_valid = 1;
			$macro    = $match[2];
			$extra    = $match[1];
		}
		else
		{
			$is_valid = 0;
		}
		
		if ($is_valid != 1)
		{
			$ADMIN->error("输入的论坛模板变量格式错误，请检查后重试。", 1);
		}
		
		if ($extra != "")
		{
			if ($extra == 'member')
			{
				if (isset($MEMBER[$macro]))
				{
					if ($MEMBER[$macro] == "")
					{
						$answer = "";
					}
					else
					{
						$answer = $MEMBER[$macro];
					}
				}
				else if (isset($GROUP[$macro]))
				{
					if ($GROUP[$macro] == "")
					{
						$answer = "";
					}
					else
					{
						$answer = $GROUP[$macro]." (用户团队信息)";
					}
				}
					
				$result = "读取用户信息";
			}
			else if ($extra == 'vars')
			{
			
				// Filter out sensitive stuff
				
				$safe_INFO['board_name'] = $INFO['board_name'];
				$safe_INFO['board_url']  = $INFO['board_url'];
				
				$answer = $safe_INFO[$macro];
				
				$result = "配置变量信息 (可能已经被设置为保护)";
			}
			else if ($extra == 'lang')
			{
				$result = "语言文件内容";
				
				$DB->query("SELECT group_name FROM ibf_skin_templates WHERE suid='{$IN['suid']}'");
				
				if ( $r = $DB->fetch_row() )
				{
					$filename = preg_replace( "/^skin_/", "lang_", $r['group_name'] );
					
					if ( @file_exists( "./lang/en/$filename".".php" ) )
					{
						require "./lang/en/$filename".".php";
						
						$answer = $lang[$macro];
					}
				}
			}
		}
		else
		{
			// Is macro
			
			$DB->query("SELECT set_id FROM ibf_skin_templates WHERE suid='{$IN['suid']}'");
			
			$template = $DB->fetch_row();
			
			$DB->query("SELECT macro_id FROM ibf_skins WHERE set_id='".$template['set_id']."'");
			
			$macrod = $DB->fetch_row();
			
			$DB->query("SELECT macro_value, macro_replace FROM ibf_macro WHERE macro_set='".$macrod['macro_id']."' AND macro_value='$macro'");
			
			if ($val = $DB->fetch_row())
			{
				$answer = $val['macro_replace'];
				$result = "从论坛模板变量";
			}
			else
			{
				$answer = "没有找到相关论坛模板变量";
			}
		}
		
		$SKIN->td_header[] = array( "&nbsp;"   , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"    , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "论坛模板变量搜索结果：<a href='{$SKIN->base_url}&act=rtempl&code=macro_one&suid={$IN['suid']}'>继续搜索</a>" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>输入论坛模板变量</b>", $IN['lookup'] ) );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>论坛模板变量类型</b>", $result )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜索结果</b><br>如果没有找到，将不显示", $answer )      );
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
		
		
		
	}
	
	//+-------------------------------
	//+-------------------------------
	
	function do_preview()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_GET_VARS;
		
		
		if ($IN['suid'] == "")
		{
			$ADMIN->error("你必须指定一个存在的论坛模板，请返回后再试。");
		}
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE suid='".$IN['suid']."'");
		
		if ( ! $template = $DB->fetch_row() )
		{
			$ADMIN->error("你必须指定一个存在的论坛模板，请返回后再试。");
		}
		
		$DB->query("SELECT macro_id FROM ibf_skins WHERE set_id='".$template['set_id']."'");
		
		$macrod = $DB->fetch_row();
		
		$DB->query("SELECT macro_value, macro_replace FROM ibf_macro WHERE macro_set='".$macrod['macro_id']."'");
		
		while ( $mc = $DB->fetch_row() )
		{
			$macro_orig[] = "<{".$mc['macro_value']."}>";
			$macro_repl[] = $mc['macro_replace'];
		}
		
		$table = "<table width='100%' bgcolor='black' cellpadding='4' style='font-family:verdana, arial;font-size:12px;color:white'>
				  <tr>
				   <td align='center' style='font-family:verdana, arial;font-size:12px;color:white'>论坛模板名称 {$template['group_name']} : 论坛模板函数 {$template['func_name']}</td>
				  </tr>
				  <tr>
				   <td align='center' style='font-family:verdana, arial;font-size:11px;color:white'>查看方式 [ <a href='{$ADMIN->base_url}&act=rtempl&code=preview&suid={$IN['suid']}&type=text' style='font-family:verdana, arial;font-size:11px;color:white'>文本方式</a> | <a href='{$ADMIN->base_url}&act=rtempl&code=preview&suid={$IN['suid']}&type=html' style='font-family:verdana, arial;font-size:11px;color:white'>HTML 方式</a> | <a href='{$ADMIN->base_url}&act=rtempl&code=preview&suid={$IN['suid']}&type=css' style='font-family:verdana, arial;font-size:11px;color:white'>HTML + CSS 方式</a> ]</td>
				  </tr>
				  </table>
				  <br><br>
				  ";
		
		if ($IN['type'] == 'text')
		{
			@header("Content-type: text/html");
			print $table;
			$html = $this->convert_tags($template['section_content']);
			
			$html = str_replace( "<" , "&lt;"  , $html);
			$html = str_replace( ">" , "&gt;"  , $html);
			$html = str_replace( "\"", "&quot;", $html);
			
			if ($HTTP_GET_VARS['hl'] != "")
			{
				$hl = urldecode(stripslashes($HTTP_GET_VARS['hl']));
				
				$hl = str_replace( "<" , "&lt;"  , $hl);
				$hl = str_replace( ">" , "&gt;"  , $hl);
				$hl = str_replace( "\"", "&quot;", $hl);
				
				$html = str_replace( $hl, "<span style='color:red;font-weight:bold;background-color:yellow'>$hl</span>", $html );
			}
			
			$html = preg_replace( "!&lt;\!--(.+?)(//)?--&gt;!s"                    , "&#60;&#33;<span style='color:red'>--\\1--\\2</span>&#62;", $html );
			$html = preg_replace( "#&lt;([^&<>]+)&gt;#s"                           , "&lt;<span style='color:blue'>\\1</span>&gt;"        , $html );   //Matches <tag>
			$html = preg_replace( "#&lt;([^&<>]+)=#s"                              , "&lt;<span style='color:blue'>\\1</span>="           , $html );   //Matches <tag
			$html = preg_replace( "#&lt;/([^&]+)&gt;#s"                            , "&lt;/<span style='color:blue'>\\1</span>&gt;"       , $html );   //Matches </tag>
			$html = preg_replace( "!=(&quot;|')([^<>])(&quot;|')(\s|&gt;)!s"   , "=\\1<span style='color:purple'>\\2</span>\\3\\4"       , $html );   //Matches ='this'
			
			$html = str_replace( "\n", "<br>", str_replace("\r\n", "\n", $html ) );
			
			print "<pre>".$html."</pre>";
			exit();
			
		}
		else if ($IN['type'] == 'html')
		{
			@header("Content-type: text/html");
			print $table;
			print $this->convert_tags($template['section_content']);
			
			exit();
		}
		else if($IN['type'] == 'css')
		{
			$DB->query("SELECT css_id, img_dir FROM ibf_skins WHERE set_id='".$template['set_id']."'");
			$r = $DB->fetch_row();
			
			$DB->query("SELECT css_text FROM ibf_css WHERE cssid='".$r['css_id']."'");
			
			$css = $DB->fetch_row();
			
			$css_text = "\n<style>\n<!--\n".str_replace( "<#IMG_DIR#>", "style_images/".$r['img_dir'], $css['css_text'] )."\n//-->\n</style>";
			
			@header("Content-type: text/html");
			print "<html><head><title>预览</title>$css_text</head><body>$table \n";
			print str_replace( $macro_orig, $macro_repl, $this->convert_tags($template['section_content']) );
			
			exit();
		
		}
			
		
		
	}
	
	
	//------------------------------------------------------
	
	
	
	function convert_tags($t="")
	{
		if ($t == "")
		{
			return "";
		}
		
		$t = preg_replace( "/{?\\\$ibforums->base_url}?/"            , "{ibf.script_url}"   , $t );
		$t = preg_replace( "/{?\\\$ibforums->session_id}?/"          , "{ibf.session_id}"   , $t );
		$t = preg_replace( "/{?\\\$ibforums->skin\['?(\w+)'?\]}?/"   , "{ibf.skin.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->lang\['?(\w+)'?\]}?/"   , "{ibf.lang.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->vars\['?(\w+)'?\]}?/"   , "{ibf.vars.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->member\['?(\w+)'?\]}?/" , "{ibf.member.\\1}"    , $t );
		
		return $t;
		
	}
	
	function unconvert_tags($t="")
	{
		if ($t == "")
		{
			return "";
		}
		
		$t = preg_replace( "/{ibf\.script_url}/i"   , '{$ibforums->base_url}'         , $t);
		$t = preg_replace( "/{ibf\.session_id}/i"   , '{$ibforums->session_id}'       , $t);
		$t = preg_replace( "/{ibf\.skin\.(\w+)}/"   , '{$ibforums->skin[\''."\\1".'\']}'   , $t);
		$t = preg_replace( "/{ibf\.lang\.(\w+)}/"   , '{$ibforums->lang[\''."\\1".'\']}'   , $t);
		$t = preg_replace( "/{ibf\.vars\.(\w+)}/"   , '{$ibforums->vars[\''."\\1".'\']}'   , $t);
		$t = preg_replace( "/{ibf\.member\.(\w+)}/" , '{$ibforums->member[\''."\\1".'\']}' , $t);
		
		return $t;
		
	}
	
	
}


?>