<?php
/*
// ------------------------------------------------------------------------
-+ Date: 12-Mar-2004
-+ Version: 1.4
-+ ========================================
-+ Be Modified by Koudanshi
-+ E-mail: koudanshi@gmx.net
-+ Homepage: koudanshi.net or bbpixel.com
-+ ========================================
-+ Any Problems please email me,
-+ Please! don't bother IPS INC.
-+ ========================================
\\ ------------------------------------------------------------------------
*/
/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.3 Final
|   ========================================
|   by Matthew Mecham
|   (c) 2001 - 2003 Invision Power Services
|   http://www.invisionpower.com
|   ========================================
|   Web: http://www.invisionboard.com
|   Time: Wed, 21 Jan 2004 09:54:34 GMT
|   Release: 2c4ce01a2d8aa60f718f2246a5cd4a18
|   Email: matt@invisionpower.com
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > Skin -> Templates functions
|   > Module written by Matt Mecham
|   > Date started: 15th April 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/

if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>您沒有權限瀏覽此頁</h1>您不能夠直接瀏覽這個網頁內容。如果你最近進行升級，確定您已升級 'admin.php'。";
	exit();
}


$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 'add':
				$this->add_splash();
				break;
				
			case 'find_pop':
				$this->find_popular();
				break;
				
			case 'edit':
				$this->show_cats();
				break;
				
			case 'dedit':
				$this->do_form();
				break;
				
			case 'doedit':
				$this->do_edit();
				break;
				
			case 'remove':
				$this->remove();
				break;
				
			case 'tools':
				$this->tools();
				break;
				
			case 'editinfo':
				$this->edit_info();
				break;
				
			case 'export':
				$this->export();
				break;
			
			case 'edit_bit':
				$this->edit_bit();
				break;
				
			case 'download':
				$this->download_group();
				break;
				
			case 'upload':
				$this->upload_form();
				break;
				
			case 'do_upload':
				$this->upload_single();
				break;
				
			
			//-------------------------
			default:
				$this->list_current();
				break;
		}
		
	}
	
	//------------------------------------------------------
	
	function add_splash()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_FILES;
		
		$FILE_NAME = $HTTP_POST_FILES['FILE_UPLOAD']['name'];
		$FILE_SIZE = $HTTP_POST_FILES['FILE_UPLOAD']['size'];
		$FILE_TYPE = $HTTP_POST_FILES['FILE_UPLOAD']['type'];
		
		// Naughty Opera adds the filename on the end of the
		// mime type - we don't want this.
		
		$FILE_TYPE = preg_replace( "/^(.+?);.*$/", "\\1", $FILE_TYPE );
		
		// Naughty Mozilla likes to use "none" to indicate an empty upload field.
		// I love universal languages that aren't universal.
		
		if ($HTTP_POST_FILES['FILE_UPLOAD']['name'] == "" or !$HTTP_POST_FILES['FILE_UPLOAD']['name'] or ($HTTP_POST_FILES['FILE_UPLOAD']['name'] == "none") )
		{
			// We're adding new templates based on another set
			
			$this->add_templates();
			exit();
		}
		
		if (! is_dir($INFO['upload_dir']) )
		{
			$ADMIN->error("無法找到上傳目錄 - 請確定 'uploads' 路徑是否設置正確");
		}
		
		//-------------------------------------------------
		// Copy the upload to the uploads directory
		//-------------------------------------------------
		
		if (! @move_uploaded_file( $HTTP_POST_FILES['FILE_UPLOAD']['tmp_name'], $INFO['upload_dir']."/".$FILE_NAME) )
		{
			$ADMIN->error("上傳失敗");
		}
		else
		{
			@chmod( $INFO['upload_dir']."/".$FILE_NAME, 0777 );
		}
		
		//-------------------------------------------------
		// Attempt to open the file..
		//-------------------------------------------------
		
		$filename = $INFO['upload_dir']."/".$FILE_NAME;
		
		if ( $FH = @fopen( $filename, 'r' ) )
		{
			$data = @fread( $FH, filesize($filename) );
			@fclose($FH);
			
			@unlink($filename);
		}
		else
		{
			$ADMIN->error("無法開啟上傳的檔案進行讀取！");
		}
		
		//-------------------------------------------------
		// If we're here, we'll assume that we've read the
		// file and the contents are in $data
		// So, lets make sure its the correct template file..
		//-------------------------------------------------
		
		if ( ! preg_match( "/<!--TEMPLATE_SET\|(.+?)-->/", $data, $matches ) )
		{
			$ADMIN->error("這個檔案似乎不是有效的 Invision Power Board 模板組件檔案");
		}
		
		list($pack_name,$author,$email,$url) = explode( ",", trim($matches[1]) );
		
		//-------------------------------------------------
		// Find the new set ID by inserting the data for the
		// template names, we can always remove it later if
		// we get an error
		//-------------------------------------------------
		
		$pack_name .= "(Upload ID: ".substr( time(), -6 ).")";
		$pack_name = str_replace( "'", "", $pack_name );
		$author    = str_replace( "'", "", $author );
		$email     = str_replace( "'", "", $email );
		$url       = str_replace( "'", "", $url );
		
		$DB->query("INSERT INTO ibf_tmpl_names (skname, author, email, url) VALUES('$pack_name', '$author', '$email', '$url')");
		$setid = $DB->get_insert_id();
		
		//-------------------------------------------------
		// Divide the file up into different sections
		//-------------------------------------------------
		
		preg_match_all( "/<!--IBF_GROUP_START:(\S+?)-->(.+?)<!--IBF_GROUP_END:\S+?-->/s", $data, $match );
		
		for ($i=0; $i < count($match[0]); $i++)
		{
			$match[1][$i] = trim($match[1][$i]);
			
			$match[2][$i] = trim($match[2][$i]);
			
			// Pass it on to our handler..
		
			$this->process_upload($match[2][$i], $setid, $match[1][$i], 1 );
			
			
		}
		
		// Insert this new data into the template names thingy
	
		$ADMIN->done_screen("模板組件匯入完成", "管理模板組件", "act=templ" );
		
	}
	
	//------------------------------------------------------
	
	function upload_single()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_FILES;
		
		$FILE_NAME = $HTTP_POST_FILES['FILE_UPLOAD']['name'];
		$FILE_SIZE = $HTTP_POST_FILES['FILE_UPLOAD']['size'];
		$FILE_TYPE = $HTTP_POST_FILES['FILE_UPLOAD']['type'];
		
		// Naughty Opera adds the filename on the end of the
		// mime type - we don't want this.
		
		$FILE_TYPE = preg_replace( "/^(.+?);.*$/", "\\1", $FILE_TYPE );
		
		if (! is_dir($INFO['upload_dir']) )
		{
			$ADMIN->error("無法找到上傳目錄 - 請確定 'uploads' 路徑是否設置正確");
		}
							
		// Naughty Mozilla likes to use "none" to indicate an empty upload field.
		// I love universal languages that aren't universal.
		
		if ($HTTP_POST_FILES['FILE_UPLOAD']['name'] == "" or !$HTTP_POST_FILES['FILE_UPLOAD']['name'] or ($HTTP_POST_FILES['FILE_UPLOAD']['name'] == "none") )
		{
			$ADMIN->error("沒有選擇要上傳的檔案！");
		}
		
		//-------------------------------------------------
		// Copy the upload to the uploads directory
		//-------------------------------------------------
		
		if (! @move_uploaded_file( $HTTP_POST_FILES['FILE_UPLOAD']['tmp_name'], $INFO['upload_dir']."/".$FILE_NAME) )
		{
			$ADMIN->error("上傳失敗");
		}
		else
		{
			@chmod( $INFO['upload_dir']."/".$FILE_NAME, 0777 );
		}
		
		//-------------------------------------------------
		// Attempt to open the file..
		//-------------------------------------------------
		
		$filename = $INFO['upload_dir']."/".$FILE_NAME;
		
		if ( $FH = @fopen( $filename, 'r' ) )
		{
			$data = @fread( $FH, filesize($filename) );
			@fclose($FH);
			
			@unlink($filename);
		}
		else
		{
			$ADMIN->error("無法開啟上傳的檔案進行讀取！");
		}
		
		//-------------------------------------------------
		// If we're here, we'll assume that we've read the
		// file and the contents are in $data
		// So, lets make sure its the correct template file..
		//-------------------------------------------------
		
		preg_match( "/<!--IBF_GROUP_START:(\S+?)-->/", $data, $matches );
		
		$found_group = trim($matches[1]);
		
		if ($found_group != $IN['group'])
		{
			$ADMIN->error("上傳的檔案似乎不是該模板群組正確的形態，尋找模板群組 '{$IN['group']}'，找到 '$found_group'");
		}
		
		//-------------------------------------------------
		// If we're here, then lets proceed, first lets
		// remove the END GROUP statement.
		//-------------------------------------------------
		
		$data = preg_replace( "/<!--IBF_GROUP_END:\S+-->/", "", $data );
		
		// Pass it on to our handler..
		
		$this->process_upload($data, $IN['setid'], $IN['group'] );
		
		$ADMIN->done_screen("模板組件更新完成", "管理模板組件", "act=templ" );
		
	}
	
	//------------------------------------------------------
	
	function upload_form()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		require './sources/Admin/skin_info.php';
		
		if ($IN['setid'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		if ($IN['group'] == "")
		{
			$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
		}
		
		//-----------------------------------
		// Get the info from the DB
		//-----------------------------------
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE set_id='".$IN['setid']."' AND group_name='".$IN['group']."'");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		$DB->query("SELECT skname FROM ibf_tmpl_names WHERE skid='".$IN['setid']."'");
		
		$row = $DB->fetch_row();
		
		//+-------------------------------
	
		$ADMIN->page_detail = "請在繼續之前仔細檢查所有資訊";
		$ADMIN->page_title  = "上傳一個模板檔案給模板組件： {$row['skname']}";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'do_upload' ),
												  2 => array( 'act'   , 'templ'     ),
												  3 => array( 'MAX_FILE_SIZE', '10000000000' ),
												  4 => array( 'setid' , $IN['setid']  ),
												  5 => array( 'group' , $IN['group']  ),
									     ) , "uploadform", " enctype='multipart/form-data'"     );
									     
		$SKIN->td_header[] = array( "&nbsp;"   , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "60%" );

		$ADMIN->html .= $SKIN->start_table("上傳模板檔案以取代： ".$skin_names[ $IN['group'] ][0]);
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>從您的電腦選擇一個要上傳的檔案</b><br>注意：所上傳的檔案將會取代目前所有的資料，無法被復原！",
												  $SKIN->form_upload(),
										 )      );
									     
		$ADMIN->html .= $SKIN->end_form('上傳檔案');
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->nav[] = array( 'act=templ' ,'模板控制首頁' );
		$ADMIN->nav[] = array( "act=templ&code=edit&id={$IN['setid']}" ,$row['skname'] );
		
		$ADMIN->output();						
		
	}
	
	//------------------------------------------------------
	
	function export()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		$DB->query("SELECT * FROM ibf_tmpl_names WHERE skid='".$IN['id']."'");
			
		if ( ! $set = $DB->fetch_row() )
		{
			$ADMIN->error("無法在資料庫中找到該 ID 的模板組件，請重試");
		}
			
		//-----------------------------------
		// Get the info from the DB
		//-----------------------------------
		
		$groups = $DB->query("SELECT DISTINCT(group_name) FROM ibf_skin_templates WHERE set_id='".$IN['id']."'");
		
		if ( ! $DB->get_num_rows($groups) )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		// Loop and pass it to the download compiler
		
		$author = str_replace( ",", "-", $set['author'] );
		$email  = str_replace( ",", "-", $set['email'] );
		$url    = str_replace( ",", "-", $set['url'] );
		$skname = str_replace( ",", "-", $set['skname'] );
		
		$output .= "<!--TEMPLATE_SET|$skname,$author,$email,$url-->\n\n";
		
		while ( $row = $DB->fetch_row($groups) )
		{
			$output .= $this->download_group(1, $IN['id'], $row['group_name'] );
		}
		
		$name = str_replace( " ", "_", $set['skname'] );
		
		@header("Content-type: unknown/unknown");
		@header("Content-Disposition: attachment; filename={$name}.SET.html");
		
		print $output;
		
		exit();
		
	}
	
	//------------------------------------------------------
	
	//------------------------------------------------------
	
	function download_group($return=0, $setid="", $group="")
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($setid != "")
		{
			$IN['setid'] = $setid;
		}
		
		if ($group != "")
		{
			$IN['group'] = $group;
		}
		
		if ($IN['setid'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		if ($IN['group'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		//-----------------------------------
		// Get the info from the DB
		//-----------------------------------
		
		$aq = $DB->query("SELECT * FROM ibf_skin_templates WHERE set_id='".$IN['setid']."' AND group_name='".$IN['group']."'");
		
		if ( ! $DB->get_num_rows($aq) )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		$output = "<!-- PLEASE LEAVE ALL 'IBF' COMMENTS IN PLACE, DO NOT REMOVE THEM! -->\n<!--IBF_GROUP_START:{$IN['group']}-->\n\n";
		
		while ( $row = $DB->fetch_row($aq) )
		{
			$text = $this->convert_tags($row['section_content']);
			
			$output .= "<!--IBF_START_FUNC|{$row['func_name']}|{$row['func_data']}-->\n\n";
			$output .= $text."\n";
			$output .= "<!--IBF_END_FUNC|{$row['func_name']}-->\n\n";
		}
		
		$output .= "\n<!--IBF_GROUP_END:{$IN['group']}-->\n";
		
		if ($return == 0)
		{
			$DB->query("SELECT skname FROM ibf_tmpl_names WHERE skid='".$IN['setid']."'");
			
			$set = $DB->fetch_row();
			
			$name = str_replace( " ", "_", $set['skname'] );
			
			@header("Content-type: unknown/unknown");
			@header("Content-Disposition: attachment; filename={$name}.{$IN['group']}.html");
			
			print $output;
			
			exit();
		}
		else
		{
			return $output;
		}
		
	}
	
	//------------------------------------------------------
	
	function show_cats()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		// Get $skin_names stuff
		
		require './sources/Admin/skin_info.php';
		
		
		if ($row['author'] and $row['email'])
		{
			$author = "<br><br>這個模板組件 <b>'{$row['skname']}'</b> 是由 <a href='mailto:{$row['email']}' target='_blank'>{$row['author']}</a> 所建立";
		}
		else if ($row['author'])
		{
			$author = "<br><br>這個模板組件 <b>'{$row['skname']}'</b> 是由 {$row['author']} 所建立";
		}
		
		if ($row['url'])
		{
			$url = " (網站： <a href='{$row['url']}' target='_blank'>{$row['url']}</a>)";
		}
		
		//+-------------------------------
	
		$ADMIN->page_detail = "請在下面選擇您要編輯的部份。<br><br><b>下載</b> 此 HTML 部份 這個選項允許您下載該模板部份的所有 HTML 以便離線編輯。<br><b>上傳</b> 此部份的 HTML 這個選項允許您上傳一個已儲存的 HTML 檔案來取代此模板部份。$author $url";
		$ADMIN->page_title  = "編輯模板組件";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->js_checkdelete();
		
		$all_cats = $DB->query("select group_name, set_id, suid, count(group_name) as number_secs, group_name FROM ibf_skin_templates WHERE set_id='".$IN['id']."' group by group_name");
		
		$SKIN->td_header[] = array( "面板類別名稱"   , "40%" );
		$SKIN->td_header[] = array( "檢視選項"          , "20%" );
		$SKIN->td_header[] = array( "管理"                , "30%" );
		$SKIN->td_header[] = array( "# 區塊"                , "10%" );

		//+-------------------------------
		
		$ADMIN->html .= "<script language='javascript'>
						 function pop_win(theUrl) {
						 	
						 	window.open('{$SKIN->base_url}&'+theUrl,'Preview','width=400,height=450,resizable=yes,scrollbars=yes');
						 }
						 </script>";
		
		$qc = $SKIN->form_dropdown( 'entry', array(
													0 => array( 'boardheader', '編輯論壇頭部'             ),
													1 => array( 'catforum'   , '編輯類別 & 討論區表格'   ),
													2 => array( 'topiclist'  , '編輯主題列表條目 & 表格' ),
													3 => array( 'postlist'   , '編輯文章檢視 & 表格'        ),
								  )			     );
												  
						 
		$quick_click = "<form action='{$SKIN->base_url}&act=templ&code=find_pop&id={$IN['id']}' method='post'>
						<div align='right'>快速&nbsp;點按&nbsp;&nbsp;{$qc}&nbsp;<input type='submit' value='Go' /></div></form>
					   ";
		
		$ADMIN->html .= $SKIN->start_table("模板： ".$row['skname'], $quick_click);
			
		while ( $group = $DB->fetch_row($all_cats) )
		{
		
			$name = "<b>".$group['group_name']."</b>";
			$desc = "";
			
			$expand = '展開編輯';
			$eid    = $group['suid'];
			$exp_content = "";
			
			// If available, change group name to easy name
			
			if ( isset($skin_names[ $group['group_name'] ]) )
			{
				$name = "<b>".$skin_names[ $group['group_name'] ][0]."</b>";
				$desc = "<br><span id='description'>".$skin_names[ $group['group_name'] ][1]."</span>";
			}
			else
			{
				$name .= " (非預設群組)";
				$desc = "<br>此群組並非標準 Invision Power Board 安裝的一部份，而且沒有可用的描述";
			}
			
			// Is this section expanded?
			
			if ($IN['expand'] == $group['suid'])
			{
				$expand = 'Collapse';
				$eid    = '';
				
				$new_q = $DB->query("SELECT func_name, LENGTH(section_content) as sec_length, suid FROM ibf_skin_templates WHERE set_id='{$IN['id']}' AND group_name='{$group['group_name']}'");
				
				//----------------------------
				
				if ( $DB->get_num_rows($new_q) )
				{
					$exp_content .= $SKIN->add_td_basic( "<script type='text/javascript'>
														  function checkall(cb) {
															   var fmobj = document.mutliact;
															   for (var i=0;i<fmobj.elements.length;i++) {
																   var e = fmobj.elements[i];
																   if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled)) {
																	   e.checked = fmobj.allbox.checked;
																   }
															   }
														   }
														   function checkcheckall(cb) {	
															   var fmobj = document.mutliact;
															   var TotalBoxes = 0;
															   var TotalOn = 0;
															   for (var i=0;i<fmobj.elements.length;i++) {
																   var e = fmobj.elements[i];
																   if ((e.name != 'allbox') && (e.type=='checkbox')) {
																	   TotalBoxes++;
																	   if (e.checked) {
																		   TotalOn++;
																	   }
																   }
															   }
															   if (TotalBoxes==TotalOn) {fmobj.allbox.checked=true;}
															   else {fmobj.allbox.checked=false;}
														   }
														   </script>
														  <a name='anc{$group['suid']}'>
														  <form name='mutliact' action='{$SKIN->base_url}&act=templ&code=edit_bit&expand={$group['suid']}' method='post'>
														  <table cellspacing='1' cellpadding='5' width='100%' align='center' bgcolor='#333333'>
														  <tr>
														   <td align='left' colspan='2' class='catrow2'><a style='font-weight:bold;font-size:12px;color:#000033' href='{$SKIN->base_url}&act=templ&code=edit&id={$IN['id']}&expand=' title='Collapse' alt='Collapse'>$name</a></td>
														   <td colspan='3' bgcolor='#FFFFFF'>&nbsp;</td>
														  <tr>
														   <td width='5%' class='catrow2' align='center'><input type='checkbox' onclick='checkall()' name='allbox' value='1' /></td>
														   <td width='25%' class='catrow2'>編輯下面所有區段</td>
														   <td width='20%' class='catrow2' align='center'># 字元</td>
														   <td width='20%' class='catrow2' align='center'>編輯</td>
														   <td width='30%' class='catrow2' align='center'>預覽選項</td>
														  </tr>
														  <!--CONTENT-->
														  <tr>
														   <td colspan='5'  class='catrow2' align='center'><input type='submit' value='Edit Selected' /></td>
														  </tr>
														  </table></form>", "left", "tdrow2" );
				
				
					$temp = "";
					$sec_arry = array();
					
					// Stuff array to sort on name
					
					while ( $i = $DB->fetch_row($new_q) )
					{
						$sec_arry[ $i['suid'] ] = $i;
						$sec_arry[ $i['suid'] ]['easy_name'] = $i['func_name'];
						
						// If easy name is available, use it
						
						if ($bit_names[ $group['group_name'] ][ $i['func_name'] ] != "")
						{
							$sec_arry[ $i['suid'] ]['easy_name'] = $bit_names[ $group['group_name'] ][ $i['func_name'] ];
						}
						
					}
					
					// Sort by easy_name
					
					usort($sec_arry, array( 'ad_settings', 'perly_word_sort' ) );
					
					// Loop and print
					
					foreach( $sec_arry as $id => $sec )
					{
						$sec['easy_name'] = preg_replace( "/^(\d+)\:\s+?/", "", $sec['easy_name'] );
						
						$temp .= "
									<tr>
									 <td width='5%'  class='tdrow1' align='center'><input type='checkbox' onclick='checkcheckall()' name='cb_{$sec['suid']}' value='1' /></td>
									 <td width='40%' class='tdrow1'><b>{$sec['easy_name']}</b></td>
									 <td width='10%' class='tdrow1' align='center'>{$sec['sec_length']}</td>
									 <td width='10%' class='tdrow1' align='center'><a href='{$SKIN->base_url}&act=templ&code=edit_bit&suid={$sec['suid']}&expand={$group['suid']}&type=single'>編輯單項</a></td>
									 <td width='40%' class='tdrow1' align='center' nowrap>(<a href='javascript:pop_win(\"act=rtempl&code=preview&suid={$sec['suid']}&type=html\")'>HTML</a> | <a href='javascript:pop_win(\"act=rtempl&code=preview&suid={$sec['suid']}&type=text\")'>文字</a> | <a href='javascript:pop_win(\"act=rtempl&code=preview&suid={$sec['suid']}&type=css\")'>連同 CSS</a>)</td>
									</tr>
								";
					}
					
					
					$exp_content = str_replace( "<!--CONTENT-->", $temp, $exp_content );
					
					$desc = "";
				
				}
				
				//----------------------------
				
			}
			else
			{
			
				$ADMIN->html .= $SKIN->add_td_row( array( 
															"<span style='font-weight:bold;font-size:12px;color:#000033'><a href='{$SKIN->base_url}&act=templ&code=edit&id={$IN['id']}&expand=$eid&#anc$eid'>".$name."</a></span>".$desc,
															"<center><a href='{$SKIN->base_url}&act=templ&code=edit&id={$IN['id']}&expand=$eid'>$expand</a></center>",
															"<center><a href='{$SKIN->base_url}&act=templ&code=download&setid={$group['set_id']}&group={$group['group_name']}' title='下載此部份的 HTML 檔案以離線編輯'>下載</a> | <a href='{$SKIN->base_url}&act=templ&code=upload&setid={$group['set_id']}&group={$group['group_name']}' title='上傳一個儲存的 HTML 檔案以取代這部份'>上傳</a></center>",
															"<center>".$group['number_secs']."</center>",
												 )      );
			}
											 
			$ADMIN->html .= $exp_content;
											 
		}
		
		$ADMIN->html .= $SKIN->end_table();
									     
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->nav[] = array( 'act=templ' ,'Template Control Home' );
		$ADMIN->nav[] = array( '' ,'管理模板組件 "'.$row['skname'].'"' );
		
		$ADMIN->output();
		
		
	}
	
	// Sneaky sorting.
	// We use the format "1: name". without this hack
	// 1: name, 2: other name, 11: other name
	// will sort as 1: name, 11: other name, 2: other name
	// There is natsort and such in PHP, but it causes some
	// problems on older PHP installs, this is hackish but works
	// by simply adding '0' to a number less than 2 characters long.
	// of course, this won't work with three numerics in the hundreds
	// but we don't have to worry about more that 99 bits in a template
	// at this stage.
	
	function perly_word_sort($a, $b)
	{
		$nat_a = intval( $a['easy_name'] );
		$nat_b = intval( $b['easy_name'] );
		
		if (strlen($nat_a) < 2)
		{
			$nat_a = '0'.$nat_a;
		}
		if (strlen($nat_b) < 2)
		{
			$nat_b = '0'.$nat_b;
		}
		
		return strcmp($nat_a, $nat_b);
	}
	
	
	//+--------------------------------------------------------------------------------
	//+--------------------------------------------------------------------------------
	
	
	function edit_info()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的模板組件 ID，請返回重試。");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		$final['skname'] = stripslashes($HTTP_POST_VARS['skname']);
		
		if (isset($HTTP_POST_VARS['author']))
		{
			$final['author'] = str_replace( ",", "", stripslashes($HTTP_POST_VARS['author']) );
			$final['email']  = str_replace( ",", "", stripslashes($HTTP_POST_VARS['email']) );
			$final['url']    = str_replace( ",", "", stripslashes($HTTP_POST_VARS['url']) );
		}
		
		$db_string = $DB->compile_db_update_string( $final );
		
		$DB->query("UPDATE ibf_tmpl_names SET $db_string WHERE skid='".$IN['id']."'");
		
		$ADMIN->done_screen("模板資訊已更新", "管理模板組件", "act=templ" );
		
	}
	
	
	//------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------
	
	function tools()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須選擇一個有效的面板檔案以完成這項操作");
		}
		
		if ($IN['tool'] == 'tmpl')
		{
      //-- mod_safe_mode begin
      if ($IN['resync_skins'] == '1')
          $this->tools_build_tmpl_all();
      else
      //-- mod_safe_mode end		  
			$this->tools_build_tmpl();
		}
		else
		{
      //-- mod_safe_mode begin
      if ($IN['resync_skins'] == '1')
          $this->tools_rebuildphp_all();
      else
      //-- mod_safe_mode end		  
			$this->tools_rebuildphp();
		}
		
	}
	
	//------------------------------------------------------------------------------------
	
	function tools_build_tmpl()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		$insert = 1;
		
		// Rebuilds the data editable files from the PHP source files
		
		$skin_dir     = ROOT_PATH."Skin/s".$IN['id'];
		
		/*$curr_groups = array();
		
		// Are we updating or inserting?
		
		$DB->query("SELECT group_name, func_name FROM ibf_skin_templates WHERE set_id='".$IN['id']."'");
		
		if ( $DB->get_num_rows() )
		{
			$insert = 0;
		
			while ( $gname = $DB->fetch_row() )
			{
				$curr_group[ $gname['group_name'] ][ $gname['func_name'] ] = 1;
			}
		}*/
		
		$errors = array();
		
		$flag = 0;
		
		//------------------------------------------------
		// Is this a safe mode only skinny poos?
		//------------------------------------------------
		
		if ( ! file_exists( $skin_dir ) )
		{
			$ADMIN->error("這個模板組件僅限於安全模式的面板並且不存在 PHP 面板檔案，沒有必要在這個模板組件上執行此工具。");
		}
		
		
		if ( ! is_readable($skin_dir) )
		{
			$ADMIN->error("無法寫入 '$skin_dir'，請檢查 CHMOD 值，必要的話，由 FTP CHMOD 為 0777，IBF 無法為您做這件事。");
		}
		
		if ( is_dir($skin_dir) )
		{
			if ( $handle = opendir($skin_dir) )
			{
			
				while (($filename = readdir($handle)) !== false)
				{
					if (($filename != ".") && ($filename != ".."))
					{
					
						if ( preg_match( "/\.php$/", $filename ) )
						{
							
							$name = preg_replace( "/^(\S+)\.(\S+)$/", "\\1", $filename );
							
							if ($FH = fopen($skin_dir."/".$filename, 'r') )
							{
								$fdata = fread( $FH, filesize($skin_dir."/".$filename) );
								fclose($FH);
							}
							else
							{
								$errors[] = "無法開啟 $filename 進行讀取，跳過檔案...";
								continue;
							}
							
							$fdata = str_replace( "\r", "\n", $fdata );
							$fdata = str_replace( "\n\n", "\n", $fdata );
							
							if ( ! preg_match( "/\n/", $fdata ) )
							{
								$errors[] = "無法在 $filename 中找到任何結束列，跳過檔案...";
								continue;
							}
							
							$farray = explode( "\n", $fdata );
							
							//----------------------------------------------------
							
							$functions = array();
							
							foreach($farray as $f)
							{
								
								// Skip javascript functions...
								
								if ( preg_match( "/<script/i", $f ) )
								{
									$script_token = 1;
								}
								
								if ( preg_match( "/<\/script>/i", $f ) )
								{
									$script_token = 0;
								}
								
								//-------------------------------
								
								if ($script_token == 0)
								{
									if ( preg_match( "/^function\s*([\w\_]+)\s*\((.*)\)/i", $f, $matches ) )
									{
										$functions[$matches[1]] = '';
										$config[$matches[1]]    = $matches[2];
										$flag = $matches[1];
										continue;
									}
								}
									
								if ($flag)
								{
									$functions[$flag] .= $f."\n";
									continue;
								}
								 
							}
							
              //-- mod_safe_mode begin
              if (SAFE_MODE_ON != 1 or $INFO['safe_mode_skins'] != 1) {
              //-- mod_safe_mode end

							//----------------------------------------------------
							// Remove current templates for this set...
							//----------------------------------------------------
							
							$DB->query("DELETE FROM ibf_skin_templates WHERE set_id='".$IN['id']."' AND group_name='$name'");
							
              //-- mod_safe_mode begin
              }
              //-- mod_safe_mode end
							$final = "";
							$flag  = 0;
							
							foreach($functions as $fname => $ftext)
							{
								preg_match( "/return <<<(EOF|HTML)(.+?)(EOF|HTML);/s", $ftext, $matches );
								
								$matches[2] = str_replace( '\\n' , '\\\\\\n', $matches[2] );
								
                //-- mod_safe_mode begin
                if (SAFE_MODE_ON == 1 or $INFO['safe_mode_skins'] == 1) {
                    $DB->query("DELETE FROM ibf_skin_templates WHERE set_id='".$IN['id']."' AND group_name='$name' AND func_name='$fname'");
                }
                //-- mod_safe_mode end
								
								$db_update = $DB->compile_db_update_string( array (
																				'set_id'          => $IN['id'],
																				'group_name'      => $name,
																				'section_content' => $matches[2],
																				'func_name'       => $fname,
																				'func_data'       => trim($config[$fname]),
																				'updated'         => time(),
																	  )       );
																	  
						
								$DB->query("INSERT INTO ibf_skin_templates SET $db_update");
							}
							
							$functions = array();
							
							//----------------------------------------------------
							
						} // if *.php
						
					} // if not dir
					
				} // while loop
				
				closedir($handle);
				
			}
			else
			{
				$ADMIN->error("無法開啟 $skin_dir 目錄進行讀取！");
			}
		}
		else
		{
			$ADMIN->error("$skin_dir 不是目錄，請檢查 admin.php 中的 \$root_path 變數");
		}
		
    //-- mod_safe_mode begin
    if ($IN['resync_skins'] != '1')
    //-- mod_safe_mode end

		$ADMIN->done_screen("已從原始的 PHP 面板檔案更新可編輯的模板", "管理模板組件", "act=templ" );
		
		if (count($errors > 0))
		{
			$this->html .= $SKIN->start_table("錯誤警告");
		
			$this->html .= $SKIN->add_td_basic( implode("<br>", $errors) );
											 
			$this->html .= $SKIN->end_table();
		}
		
		
	}
	
  //-- mod_safe_mode begin

  function save_config($item = "", $value) {
      if ($item == "") return;

      global $ADMIN, $HTTP_POST_VARS, $INFO;
      $master[$item] = $value;
      $INFO = $ADMIN->rebuild_config($master);
  }

  function tools_build_tmpl_all() {
      global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
      if (defined("ROOT_PATH")) { $root_path = ROOT_PATH; }

      $this->save_config("resync_skins_start", $IN['resync_skins_start']);

      $dirnames = array();
      
      $fh = opendir($root_path."Skin");
      while (($dirname = readdir($fh)) !== false) {
          if (($dirname != ".") && ($dirname != "..") && is_dir($root_path."Skin/".$dirname) )
              $dirnames[++$i] = $dirname;
      }

      if (!count($dirnames)) 
          $ADMIN->done_screen("沒有可用的面板", "管理模板組件", "act=templ" );
      
      $this->save_config("resync_skins", count($dirnames));

      for ($i = 1; $i <= count($dirnames); $i++ ) {
          if ($i > $INFO['resync_skins_start']) {
              $IN['id'] = substr($dirnames[$i],1 );
              $this->tools_build_tmpl();
              $INFO['resync_skins_start']++;
              $this->save_config("resync_skins_start", $INFO['resync_skins_start']);
          }
          $this->html .= $SKIN->end_table();
      }
      $this->save_config("resync_skins", '0');
      $this->save_config("resync_skins_start", '0');
      $ADMIN->done_screen("已經由所有面板資料夾中更新樣板", "管理模板組件", "act=templ" );
      
  }

  function tools_rebuildphp_all() {
      global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
      if (defined("ROOT_PATH")) { $root_path = ROOT_PATH; }

      $this->save_config("resync_skins_start", $IN['resync_skins_start']);

      $dirnames = array();
      
      $fh = opendir($root_path."Skin");
      while (($dirname = readdir($fh)) !== false) {
          if (($dirname != ".") && ($dirname != "..") && is_dir($root_path."Skin/".$dirname) )
              $dirnames[++$i] = $dirname;
      }

      if (!count($dirnames)) 
          $ADMIN->done_screen("沒有可用的面板", "管理模板組件", "act=templ" );
      
      $this->save_config("resync_skins", count($dirnames));

      for ($i = 1; $i <= count($dirnames); $i++ ) {
          if ($i > $INFO['resync_skins_start']) {
              $IN['id'] = substr($dirnames[$i],1 );
              $this->tools_rebuildphp();
              $INFO['resync_skins_start']++;
              $this->save_config("resync_skins_start", $INFO['resync_skins_start']);
          }
      }
      $this->save_config("resync_skins", '0');
      $this->save_config("resync_skins_start", '0');
      $ADMIN->done_screen("已經由所有面板資料夾中更新樣板", "管理模板組件", "act=templ" );
      
  }


  function tools_rebuildphp() {
      global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
      if (defined("ROOT_PATH")) { $root_path = ROOT_PATH; }

      if ($IN['id'] == "") {
          $ADMIN->error("你必須指定一個現有的面板 ID, 請返回重試");
      }

      $skin_dir = $root_path."Skin/s".$IN['id'];

      if (!is_dir($skin_dir)) {
          if (!@mkdir($skin_dir, 0755)) {
              if (SAFE_MODE_ON == 1) {
                  $ADMIN->error("$skin_dir 並不存在. 但是我們無法在安全模式下新增 $skin_dir.".
                                         "<br>你必須自行利用 FTP 修改檔案權限為 0755 或 0777 讓資料寫入.".
                                         " IBF 無法幫你做這件事");
              } else {
                  $ADMIN->error("$skin_dir 並不存在. 但是我們無法在安全模式下新增 $skin_dir.".
                                         "<br>你必須自行利用利用 FTP 修改檔案權限為 0755 或 0777 讓資料寫入.".
                                         " IBF 無法幫你做這件事");
              }
          }
      }

      if (!is_writeable($skin_dir)) {
          $ADMIN->error("無法寫入 $skin_dir, 請利用 FTP 修改檔案權限為 0755 或 0777.");
      }

      $groups = $DB->query("SELECT DISTINCT(group_name) FROM ibf_skin_templates WHERE set_id='".$IN['id']."'");

      if ( ! $DB->get_num_rows($groups) ) {
          $ADMIN->error("Rebuild Failed at template set creation: 無法從資料庫中查詢該資訊");
      }

      while ( $trow = $DB->fetch_row($groups) ) {

          $output = "";
          $aq = $DB->query("SELECT * FROM ibf_skin_templates WHERE set_id='".$IN['id']."' AND group_name='".$trow['group_name']."'");

          if ( ! $DB->get_num_rows($aq) ) {
              $ADMIN->error("無法從資料庫中查詢該資訊");
          }

          $output .= "<?php\n\n";
          $output .= "class ".$trow['group_name']." {\n\n";

          while ( $next_row = $DB->fetch_row($aq) ) {
              $text = $next_row['section_content'];
              $text = str_replace( "\r\n", "\n", $text);
              $text = str_replace( "\n\n", "\n", $text);

              $output .= "function ".$next_row['func_name']."(".$next_row['func_data'].") {\n";
              $output .= "global \$ibforums;\n\n";
              $output .= "return <<<EOF\n";
              $output .= $text."\n";
              $output .= "EOF;\n";
              $output .= "}\n\n\n";
          }
          $output .= "}\n\n?>";
          $FH = fopen($skin_dir."/".$trow['group_name'].".php", 'w');
          fwrite($FH, $output, strlen($output));
          fclose($FH);
          @chmod($skin_dir."/".$trow['group_name'].".php", 0777);
      }
      if ($IN['resync_skins'] != '1')
          $ADMIN->done_screen("已由資料庫中成功重建模版檔案", "管理模板組件", "act=templ" );
  }

  //-- mod_safe_mode end
	
	//-------------------------------------------------------------
	// Add templates
	//-------------------------------------------------------------
	
	
	function add_templates()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
		}
		
		//-------------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
		
			if ( ! is_writeable(ROOT_PATH.'Skin') )
			{
				$ADMIN->error("程式無法寫入 'Skin' 目錄，請檢查該目錄的權限，如果不能肯定，CHMOD 為 0777 後重試。");
			}
			
			//-------------------------------------
			
			if ( ! is_dir(ROOT_PATH.'Skin/s'.$IN['id']) )
			{
				$ADMIN->error("無法找到要複製的原始模板組件，請檢查重試");
			}
		
		}
		
		//-------------------------------------
		
		$DB->query("SELECT * FROM ibf_tmpl_names WHERE skid='".$IN['id']."'");
		
		//-------------------------------------
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從 DB 查詢該模板組件，so there");
		}
		
		//-------------------------------------
		
		$row['skname'] = $row['skname'].".NEW";
		
		// Insert a new row into the DB...
		
		$final = array();
		
		foreach($row as $k => $v)
		{
			if ($k == 'skid')
			{
				continue;
			}
			else
			{
				$final[ $k ] = $v;
			}
		}
		
		$db_string = $DB->compile_db_insert_string( $final );
		
		$DB->query("INSERT INTO ibf_tmpl_names (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");
		
		$new_id = $DB->get_insert_id();
		
		//-------------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
		
			//-------------------------------------
			
			if ( ! $ADMIN->copy_dir( $INFO['base_dir'].'Skin/s'.$IN['id'] , $INFO['base_dir'].'Skin/s'.$new_id ) )
			{
				$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='$new_id'");
				
				$ADMIN->error( $ADMIN->errors );
			}
		
		}
		
		// Copy over the templates stored inthe database...
		
		$get = $DB->query("SELECT * FROM ibf_skin_templates WHERE set_id='".$IN['id']."'");
		
		while( $r = $DB->fetch_row($get) )
		{
			$r['section_content'] = str_replace( '\\n' , '\\\\\\n', $r['section_content'] );
			
			$row = $DB->compile_db_insert_string( array (
														'set_id'          =>  $new_id,
														'group_name'      =>  $r['group_name'],
														'section_content' =>  $r['section_content'],
														'func_name'       =>  $r['func_name'],
														'func_data'       =>  $r['func_data'],
														'updated'         => time(),
														'can_remove'      => $r['can_remove'],
											 )       );
											 
			$put = $DB->query("INSERT INTO ibf_skin_templates ({$row['FIELD_NAMES']}) VALUES({$row['FIELD_VALUES']})");
		}
		
		//-------------------------------------
		// All done, yay!
		//-------------------------------------
		
		$ADMIN->done_screen("新模板組件", "管理模板組件", "act=templ" );
	
	}
	
	//-------------------------------------------------------------
	// REMOVE WRAPPERS
	//-------------------------------------------------------------
	
	function remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		//+-------------------------------
		
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
		}
		
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
			
			if ( ! $ADMIN->rm_dir( $INFO['base_dir']."Skin/s".$IN['id'] ) )
			{
				$ADMIN->error("無法移除該模板檔案，請檢查 CHMOD 權限以確定程式擁有正確的權限來進行");
			}
			
		}
		
		$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='".$IN['id']."'");
		$DB->query("DELETE FROM ibf_skin_templates WHERE set_id='".$IN['id']."'");
		
		$std->boink_it($SKIN->base_url."&act=templ");
		exit();
		
		
	}
	
	//-------------------------------------------------------------
	// Find popular bits :D
	//-------------------------------------------------------------
	
	function find_popular()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_COOKIE_VARS;
		
		//-----------------------------------
		// Check for valid input...
		//-----------------------------------
		
		$id = intval($IN['id']);
		
		if ( $id < 1 )
		{
			$ADMIN->error("Baah! Something is whack, man");
		}
		
		switch ( $IN['entry'] )
		{
			case 'boardheader':
				$sql = 'group_name="skin_global" AND func_name IN ( "Member_bar", "Guest_bar", "Member_no_usepm_bar", "BoardHeader" )';
				break;
			case 'catforum':
				$sql = 'group_name="skin_boards" AND func_name IN ( "CatHeader_Expanded", "PageTop",  "end_this_cat", "end_all_cats", "ForumRow" )';
				break;
			case 'topiclist':
				$sql = 'group_name="skin_forum" AND func_name IN ( "PageTop", "TableEnd", "RenderRow", "render_pinned_start", "render_pinned_end", "render_pinned_row" )';
				break;
			default:
				$sql = 'group_name="skin_topic" AND func_name IN ( "PageTop", "TableFooter", "RenderRow" )';
				break;
		}
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE $sql AND set_id=$id");
		
		while ( $r = $DB->fetch_row() )
		{
			$IN['cb_'.$r['suid']] = 1;
		}
		
		$this->edit_bit();
	}
	
	//-------------------------------------------------------------
	// EDIT TEMPLATES, STEP TWO
	//-------------------------------------------------------------
	
	function edit_bit()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_COOKIE_VARS;
		
		//-----------------------------------
		// Check for valid input...
		//-----------------------------------
		
		// Get $skin_names stuff
		
		require './sources/Admin/skin_info.php';
		
		$ADMIN->page_detail = "您可以編輯此模板的 HTML";
		$ADMIN->page_title  = "編輯模板";
		
		$ids = array();
		
		if ( $IN['type'] == 'single' )
		{
			if ($IN['suid'] == "")
			{
				$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
			}
			$ids[] = $IN['suid'];
		}
		else
		{
			foreach ($IN as $key => $value)
			{
				if ( preg_match( "/^cb_(\d+)$/", $key, $match ) )
				{
					if ($IN[$match[0]])
					{
						$ids[] = $match[1];
					}
				}
			}
 		}
 
 		
 		if ( count($ids) < 1 )
 		{
 			$ADMIN->error("未選取 ids，請返回並在送出表單之前選取所需");
 		}
		
		
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->js_template_tools();
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doedit'    ),
												  2 => array( 'act'   , 'templ'     ),
												  3 => array( 'suid'  , $IN['suid'] ),
												  4 => array( 'type'  , $IN['type'] ),
									     )  , "theform"    );
									     
		
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE suid IN (".implode(",",$ids).")");
		
		while ( $i = $DB->fetch_row() )
		{
			$sec_arry[ $i['suid'] ] = $i;
			$sec_arry[ $i['suid'] ]['easy_name'] = $i['func_name'];
			
			// If easy name is available, use it
			
			if ($bit_names[ $i['group_name'] ][ $i['func_name'] ] != "")
			{
				$sec_arry[ $i['suid'] ]['easy_name'] = $bit_names[ $i['group_name'] ][ $i['func_name'] ];
			}
			
		}
		
		// Sort by easy_name
		
		usort($sec_arry, array( 'ad_settings', 'perly_word_sort' ) );
		
		// Loop and print
		
		foreach( $sec_arry as $id => $template )
		{
			$template['easy_name'] = preg_replace( "/^(\d+)\:\s+?/", "", $template['easy_name'] );


			//+-------------------------------
			// Swop < and > into ascii entities
			// to prevent textarea breaking html
			//+-------------------------------
			
			$setid     = $template['set_id'];
			$groupname = $template['group_name'];
			
			$templ = $this->convert_tags($template['section_content']);
			
			$templ = preg_replace("/&/", "&#38;", $templ );
			$templ = preg_replace("/</", "&#60;", $templ );
			$templ = preg_replace("/>/", "&#62;", $templ );
			
			//+-------------------------------
			
			$SKIN->td_header[] = array( "{none}"   , "100%" );
			
			$ADMIN->html .= $SKIN->start_table( "模板： ". $template['easy_name'] );
		
			$ADMIN->html .= $SKIN->add_td_basic( "<input type='button' value='查看圖樣' id='editbutton' title='檢視圖樣定義' onClick='pop_win(\"code=macro_one&suid={$template['suid']}\", \"MacroWindow\", 400, 200)'>".
												 "&nbsp;<input type='button' value='比對' id='editbutton' title='將編輯過的版本與原始檔比對' onClick='pop_win(\"act=rtempl&code=compare&suid={$template['suid']}\", \"CompareWindow\", 500,400)'>".
												 "&nbsp;<input type='button' value='復原' id='editbutton' title='回復到原始未編輯的模板' onClick='restore(\"{$template['suid']}\",\"{$IN['expand']}\")'>".
												 "&nbsp;<input type='button' value='檢視原始模板' id='editbutton' title='檢視未被編輯的模板 HTML' onClick='pop_win(\"act=rtempl&code=preview&suid={$template['suid']}&type=html\", \"OriginalPreview\", 400,400)'>".
												 "&nbsp;<input type='button' value='搜尋' id='editbutton' title='搜尋模板中的字串' onClick='pop_win(\"act=rtempl&code=search&suid={$template['suid']}&type=html\", \"Search\", 500,400)'>".
												 "&nbsp;<input type='button' value='編輯框大小' id='editbutton' title='變更下面的編輯框大小' onClick=\"pop_win('&act=prefs', 'prefs', '300', '100')\">",
												 "center", "catrow");
											 
			$ADMIN->html .= $SKIN->add_td_basic( "<b>顯示 HTML 代碼：&nbsp;".
												 "<select name='htmlcode' onChange=\"document.theform.res.value='&'+document.theform.htmlcode.options[document.theform.htmlcode.selectedIndex].value+';'\" id='multitext'><option value='copy'>&copy;</option>
												 <option value='raquo'>&raquo;</option>
												 <option value='laquo'>&laquo;</option>
												 <option value='#149'>&#149;</option>
												 <option value='reg'>&reg;</option>
												 </select>&nbsp;&nbsp;<input type='text' name='res' size=20 id='multitext'>&nbsp;&nbsp;<input type='button' value='選取' id='editbutton' onClick='document.theform.res.focus();document.theform.res.select();'>"
			
												, "center", "tdrow1");
		
			$ADMIN->html .= $SKIN->add_td_row( array( 
														"<center>".$SKIN->form_textarea("txt_{$template['suid']}", $templ, $INFO['tx'], $INFO['ty'], $wrap)."</center>",
											 )      );
									     
			$ADMIN->html .= $SKIN->end_table();
		}
		
		$ADMIN->html .= "<div class='tableborder'><div class='catrow2' align='center'><input type='submit' value='更新模板' /></div></div></form>";
									     
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$setid."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢該資訊");
		}
		
		$ADMIN->nav[] = array( 'act=templ' ,'模板控制首頁' );
		$ADMIN->nav[] = array( "act=templ&code=edit&id={$setid}" ,$row['skname'] );
		$ADMIN->nav[] = array( "act=templ&code=edit&id={$setid}&expand={$IN['expand']}", $groupname );
		//$ADMIN->nav[] = array( "", $template['func_name'] );
		
		$ADMIN->output();
		
		
	}
	
	//-------------------------------------------------------------
	// COMPLETE EDIT
	//-------------------------------------------------------------
	
	function do_edit()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		//+-------------------------------
		// Check incoming
		//+-------------------------------
		
		foreach ($IN as $key => $value)
		{
			if ( preg_match( "/^txt_(\d+)$/", $key, $match ) )
			{
				if ($IN[$match[0]])
				{
					$ids[] = $match[1];
				}
			}
		}
 		
 		if ( count($ids) < 1 )
 		{
 			$ADMIN->error("未選取 ids，請返回並在送出表單之前選取所需");
 		}
 		
 		//+-------------------------------
		// Get the group name, etc
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_skin_templates WHERE suid IN(".implode( ",", $ids ).")");
		
		if ( ! $template = $DB->fetch_row() )
		{
			$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
		}
		
		$real_name = $template['group_name'];
		
		//+-------------------------------
		// Get the template set info
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$template['set_id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢該資訊");
		}
		
		//+-------------------------------
		
		$phpskin  = ROOT_PATH."Skin/s".$template['set_id']."/".$real_name.".php";
		
		//+-------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，若要進行，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
		
		   if ( ! is_writeable($phpskin) )
		   {
			   $ADMIN->error("無法寫入 '$phpskin'，請檢查 CHMOD 值，必要的話，由 FTP CHMOD 為 0777，IBF 無法為您做這件事。");
		   }
		
		}
		
		//+-------------------------------
		// Process my bits :o
		//+-------------------------------
		
		foreach( $ids as $id )
		{
		
			$text = stripslashes($HTTP_POST_VARS['txt_'.$id]);
		
			//+-------------------------------
			// Sw(o|a)p back < & >
			//+-------------------------------
			
			$text = preg_replace("/&#60;/", "<", $text);
			$text = preg_replace("/&#62;/", ">", $text);
			$text = preg_replace("/&#38;/", "&", $text);
			
			$text = str_replace( '\\n' , '\\\\\\n', $text );
			
			//+-------------------------------
			// Convert \r to nowt
			//+-------------------------------
			
			$text = preg_replace("/\r/", "", $text);
			
			$text = $this->unconvert_tags($text);
			
			//+-------------------------------
			// Update the DB
			//+-------------------------------
			
			$string = $DB->compile_db_update_string( array (
															 'section_content' => $text,
															 'updated'         => time(),
												   )       );
			
			$DB->query("UPDATE ibf_skin_templates SET $string WHERE suid=$id");
		
		}
		
		
		//+-------------------------------
		// Rebuild the PHP file
		//+-------------------------------
		
		$final = "<"."?php\n\n".
				 "class $real_name {\n\n";
				 
		//+------------------------------------------
		// Get all the data from the DB that matches
		// the group name (filename) and set_id
		//+------------------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'。");
			}
		
			$DB->query("SELECT * FROM ibf_skin_templates WHERE group_name='$real_name' AND set_id='{$template['set_id']}'"); 
			
			while ( $data = $DB->fetch_row() )
			{
			
				$final .= "\n\nfunction {$data['func_name']}({$data['func_data']}) {\n".
					      "global \$ibforums;\n".
					      "return <<<EOF\n";
					   
				$final .= $data['section_content'];
					   
				$final .= "\nEOF;\n}\n";
			
			}
			
			$final .= "\n\n}\n?".">";
			
			if ($fh = fopen( "$phpskin", 'w' ) )
			{
				fwrite($fh, $final, strlen($final) );
				fclose($fh);
			}
			else
			{
				$ADMIN->error("無法儲存資訊到 $phpskin，請確定 CHMOD 權限是否正確。");
			}
		
		}
		
		$ADMIN->done_screen("已更新模板檔案", "管理模板於模板組件： {$row['skname']}", "act=templ&code=edit&id={$template['set_id']}" );
		
	}
	
	//-------------------------------------------------------------
	// EDIT TEMPLATES, STEP ONE
	//-------------------------------------------------------------
	
	function do_form()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個存在的模板組件 ID，請返回重試。");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		
		$form_array = array();
	
		//+-------------------------------
	
		$ADMIN->page_detail = "請在下面選擇您要編輯的部份";
		$ADMIN->page_title  = "編輯模板組件資料";
		
		//+-------------------------------
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->js_no_specialchars();
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'editinfo'    ),
												  2 => array( 'act'   , 'templ'       ),
												  3 => array( 'id'    , $IN['id']     ),
									     ), "theAdminForm", "onSubmit=\"return no_specialchars('templates')\""      );
									     
		//+-------------------------------
		
		$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>",
													$SKIN->form_input('skname', $row['skname']),
									     )      );
									     
		
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>模板組件作者名稱：</b>",
													$SKIN->form_input('author', $row['author']),
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>模板組件作者電子郵件：</b>",
													$SKIN->form_input('email', $row['email']),
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>模板組件作者網頁：</b>",
													$SKIN->form_input('url', $row['url']),
										 )      );
									     
		
									     
		$ADMIN->html .= $SKIN->end_form("編輯模板組件細節");
									     
		$ADMIN->html .= $SKIN->end_table();
									     
		
		$ADMIN->nav[] = array( 'act=templ' ,'模板控制首頁' );
		
		$ADMIN->output();
		
		
	}
	
	//-------------------------------------------------------------
	// SHOW CURRENT TEMPLATE PACKS
	//-------------------------------------------------------------
	
	function list_current()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$form_array = array();
	
		$ADMIN->page_detail = "此面板模板包含所有被論壇使用的 HTML。
							   <br>
							   這部份可以讓您建立新的模板組件或編輯在您目前模板組件中的 HTML";
		$ADMIN->page_title  = "管理模板組件";
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "名稱"        , "30%" );
		$SKIN->td_header[] = array( "指派"   , "20%" );
		$SKIN->td_header[] = array( "編輯內容"    , "20%" );
		$SKIN->td_header[] = array( "管理 HTML"  , "20%" );
		$SKIN->td_header[] = array( "移除"       , "10%" );
		
		//+-------------------------------
		
		$DB->query("SELECT DISTINCT(s.set_id), s.sname, t.skid, t.skname from ibf_tmpl_names t, ibf_skins s WHERE s.set_id=t.skid ORDER BY t.skname ASC");
		
		$used_ids = array();
		$show_array = array();
		
		if ( $DB->get_num_rows() )
		{
		
			$ADMIN->html .= $SKIN->start_table( "目前使用中的模板組件" );
			
			while ( $r = $DB->fetch_row() )
			{
			
				$show_array[ $r['skid'] ] .= stripslashes($r['sname'])."<br>";
			
				if ( in_array( $r['skid'], $used_ids ) )
				{
					continue;
				}
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".stripslashes($r['skname'])."</b><br>[ <a href='{$SKIN->base_url}&act=templ&code=export&id={$r['skid']}' title='下載此完整的模板組件'>匯出</a> ]",
														  "<#X-{$r['skid']}#>",
														  //"<center><a href='".$SKIN->base_url."&act=templ&code=export&id={$r['skid']}'>Download</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=templ&code=dedit&id={$r['skid']}' title='編輯模板組件名稱'>編輯內容</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=templ&code=edit&id={$r['skid']}' title='編輯、上傳及下載 '>管理 HTML</a></center>",
														  "<i>移除前請先撤銷指派</i>",
												 )      );
												   
				$used_ids[] = $r['skid'];
				
				$form_array[] = array( $r['skid'], $r['skname'] );
				
			}
			
			foreach( $show_array as $idx => $string )
			{
				$string = preg_replace( "/<br>$/", "", $string );
				
				$ADMIN->html = preg_replace( "/<#X-$idx#>/", "$string", $ADMIN->html );
			}
			
			$ADMIN->html .= $SKIN->end_table();
		}
		
		if ( count($used_ids) > 0 )
		{
		
			$DB->query("SELECT skid, skname FROM ibf_tmpl_names WHERE skid NOT IN(".implode(",",$used_ids).")");
		
			if ( $DB->get_num_rows() )
			{
			
				$SKIN->td_header[] = array( "名稱"          , "50%" );
				$SKIN->td_header[] = array( "編輯內容"    , "20%" );
				$SKIN->td_header[] = array( "管理 HTML"    , "20%" );
				$SKIN->td_header[] = array( "移除"         , "10%" );
			
				$ADMIN->html .= $SKIN->start_table( "目前未指派的模板組件" );
				
				$ADMIN->html .= $SKIN->js_checkdelete();
				
				while ( $r = $DB->fetch_row() )
				{
					
					$ADMIN->html .= $SKIN->add_td_row( array( "<b>".stripslashes($r['skname'])."</b>",
															  //"<center><a href='".$SKIN->base_url."&act=templ&code=export&id={$r['skid']}'>Download</a></center>",
															  "<center><a href='".$SKIN->base_url."&act=templ&code=dedit&id={$r['skid']}'>編輯內容</a></center>",
															  "<center><a href='".$SKIN->base_url."&act=templ&code=edit&id={$r['skid']}' title='編輯、上傳及下載'>管理 HTML</a></center>",
															  "<center><a href='javascript:checkdelete(\"act=templ&code=remove&id={$r['skid']}\")'>移除</a></center>",
													 )      );
													 
					$form_array[] = array( $r['skid'], $r['skname'] );
													   
				}
				
				$ADMIN->html .= $SKIN->end_table();
			}
		}
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'add'     ),
												  2 => array( 'act'   , 'templ'    ),
												  3 => array( 'MAX_FILE_SIZE', '10000000000' ),
									     ) , "uploadform", " enctype='multipart/form-data'"     );
												  
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		$ADMIN->html .= $SKIN->start_table( "建立新模板組件" );
			
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>新模板組件基於...</b>" ,
										  		  $SKIN->form_dropdown( "id", $form_array)
								 )      );
								 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b><u>或者</u> 從您的電腦選擇一個要匯入的檔案</b><br>注意：這必須是一個模板群組組件。",
												  $SKIN->form_upload(),
										 )      );
		
		$ADMIN->html .= $SKIN->end_form("建立新的模板組件");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'tools'     ),
												  2 => array( 'act'   , 'templ'    ),
									     )      );
		
		$SKIN->td_header[] = array( "工具"  , "50%" );
		$SKIN->td_header[] = array( "執行於模板組件"  , "50%" );
		
		$extra = "";
		
		if ( SAFE_MODE_ON == 1)
		{
			$extra = "<br><span id='detail'>警告：偵測到安全模式限制，某些工具將沒有作用。</span>";
		}
		
		$ADMIN->html .= $SKIN->start_table( "模板工具".$extra );
			
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->add_td_row( array( $SKIN->form_dropdown( "tool",
																		array(
																				1 => array( 'tmpl', '從 PHP 面板檔案同步化資料庫模板'   ),
                                                                                2 => array( 'php',  '由資料庫模板重建 PHP 面板檔案' ),
																			 )
												                      ) ,
										  		  $SKIN->form_dropdown( "id", $form_array)
								 )      );
		
    $ADMIN->html .= $SKIN->add_td_row( array( "<b>管理所有面板?</b>" ,
                                      $SKIN->form_yes_no( "resync_skins", '0' )
                             )      );

    $ADMIN->html .= $SKIN->add_td_row( array( "<b>由編號多少的面板開始?</b>" ,
                                     $SKIN->form_simple_input( 'resync_skins_start' , ($INFO['resync_skins_start']?$INFO['resync_skins_start']:'0'), 4 )
                             )      );
    if (FALSE)
    //-- mod_safe_mode end

		$ADMIN->html .= $SKIN->add_td_row( array( $SKIN->form_dropdown( "tool",
																		array(
																				1 => array( 'tmpl', '從 PHP 面板檔案同步化資料庫模板'   ),
																			 )
												                      ) ,
										  		  $SKIN->form_dropdown( "id", $form_array)
								 )      );

		$ADMIN->html .= $SKIN->end_form("執行工具");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
	
	}
	
	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 );
		
		// Make some tags safe..
		
		$t = preg_replace( "/\{ibf\.vars\.(sql_driver|sql_host|sql_database|sql_pass|sql_user|sql_port|sql_tbl_prefix|smtp_host|smtp_port|smtp_user|smtp_pass|html_dir|base_dir|upload_dir)\}/", "" , $t );
				
		return $t;
		
	}
	
	function unconvert_tags($t="")
	{
		if ($t == "")
		{
			return "";
		}
		
		// Make some tags safe..
		
		$t = preg_replace( "/\{ibf\.vars\.(sql_driver|sql_host|sql_database|sql_pass|sql_user|sql_port|sql_tbl_prefix|smtp_host|smtp_port|smtp_user|smtp_pass|html_dir|base_dir|upload_dir)\}/", "" , $t );
		
		$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;
		
	}
	
	/*
	<!--IBF_START_FUNC|calendar_events|$events = ""-->
        <tr>
           <td id='category' colspan='2'>{ibf.lang.calender_f_title}</td>
    	</tr>
    	<tr>
          <td id='forum1' width='5%' valign='middle'>{ibf.skin.F_ACTIVE}</td>
          <td id='forum2' width='95%'>$events</td>
        </tr>
    <!--IBF_END_FUNC|calendar_events-->
*/
	
	
	
	function process_upload($raw, $setid, $group, $isnew=0)
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$skin_dir     = ROOT_PATH."Skin/s".$setid;
		
		//-------------------------------------------
		// If we are not using safe mode skins, lets
		// test to make sure we can write to that dir
		//-------------------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				if ($isnew == 1)
				{
					$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='$setid'");
				}
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
			
			// Are we creating a new template set? 
			// if so, lets create the directory
			
			if ($isnew == 1)
			{
				if ( ! is_writeable(ROOT_PATH.'Skin') )
				{	
					$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='$setid'");
					$ADMIN->error("程式無法寫入 'Skin' 目錄，請檢查該目錄的權限，如果不能肯定，CHMOD 為 0777 後重試。");
				}
				
				if ( ! file_exists($skin_dir) )
				{
					// Directory does not exist, lets create it
					
					if ( ! @mkdir($skin_dir, 0777) )
					{
						$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='$setid'");
						$ADMIN->error("無法建立 '$skin_dir' 目錄，請檢查 CHMOD 權限後重試");
					}
					else
					{
						@chmod($skin_dir, 0777);
					}
				}
			}
			else
			{
				if ( ! is_writeable($skin_dir) )
				{
					$ADMIN->error("無法寫入 '$skin_dir'，請檢查 CHMOD 值，必要的話，由 FTP CHMOD 為 0777，IBF 無法為您做這件事。");
				}
			}
		
		}
		
		//--------------------------------
		// Remove everything up until the
		// first <!--START tag...
		//--------------------------------
		
		$raw = preg_replace( "/^.*?(<!--IBF_START_FUNC)/s", "\\1", trim($raw));
		
		$raw = str_replace( "\r\n", "\n", $raw);
		
		//+-------------------------------
		// Convert the tags back to php native
		//+-------------------------------
		
		$raw = $this->unconvert_tags($raw);
		
		//+-------------------------------
		// Grab our vars and stuff
		//+-------------------------------
		
		$DB->query("SELECT func_name, group_name FROM ibf_skin_templates WHERE set_id='$setid'");
		
		if ( $DB->get_num_rows() )
		{
			while ( $gname = $DB->fetch_row() )
			{
				$curr_group[ $gname['group_name'] ][ $gname['func_name'] ] = 1;
			}
		}
		
		$master = array();
		$flag   = 0;
		
		$eachline = explode( "\n", $raw );
		
		foreach ($eachline as $line)
		{
			if ($flag == 0)
			{
				// We're not gathering HTML, lets see if we have a new
				// function start..
				
				if ( preg_match( "/\s*<!--IBF_START_FUNC\|(\S+?)\|(.*?)-->\s*/", $line, $matches) )
				{
					$func = trim($matches[1]);
					$data = trim($matches[2]);
					
					if ($func != "")
					{
					
						$flag = $func;
						
						$master[$func] = array( 'func_name'  => $func,
												'func_data'  => $data,
												'content'    => ""
											  );
					}
					continue;
					
				}
				
			}
			
			if ( preg_match("/\s*?<!--IBF_END_FUNC\|$flag-->\s*?/", $line) )
			{
				 // We have found the end of the subbie..
				 // Reset the flag and feed the next line.
				 
				 $flag = 0;
				 continue;
			}
			else
			{
				// Carry on feeding the HTML...
				
				if ( isset($master[$flag]['content']) )
				{
					$master[$flag]['content'] .= $line."\n";
					continue;
				}
			}
			
		}
		
		//+-------------------------------
		// Start parsing the php skin file
		//+-------------------------------
		
		if ($INFO['safe_mode_skins'] != 1)
		{
		
			if (SAFE_MODE_ON == 1)
			{
				$ADMIN->error("偵測到安全模式，您必須改變論壇配置將 '安全模式面板' 開啟，要這樣做，點按 '論壇設定' 選單出現子選單時選擇 '基本組態'");
			}
		
			$final = "<"."?php\n\n".
					 "class $group {\n\n";
			
			foreach( $master as $func_name => $data )
			{
			
				$final .= "\n\nfunction ".trim($data['func_name'])."(".trim($data['func_data']).") {\n".
					   	  "global \$ibforums;\n".
					   	  "return <<<EOF\n";
					   	  
			    $final .= trim($data['content']);
					   
				$final .= "\nEOF;\n}\n";
				
			}
			
			$final .= "\n\n}\n?".">";
			
			if ($fh = fopen( $skin_dir."/".$group.".php", 'w' ) )
			{
				fwrite($fh, $final, strlen($final) );
				fclose($fh);
			}
			else
			{
				if ($isnew == 1)
				{
					$DB->query("DELETE FROM ibf_tmpl_names WHERE skid='$setid'");
				}
				$errors[] = "無法儲存資訊到 $phpskin，請確定 CHMOD 權限是否正確。";
			}
		
		}
		
		//+-------------------------------
		// Update the DB
		//+-------------------------------
		
		
		foreach( $master as $func_name => $data )
		{
		
			if ($isnew == 0)
			{
				if ($curr_group[$group][$func_name] != 1)
				{
					// Not a current group/ func..
					
					$isnew = 1;
				}
			}
		
			if ($isnew == 0)
			{
				$data['content'] = str_replace( '\\n' , '\\\\\\n', $data['content'] );
				
				$str = $DB->compile_db_update_string( array(
															  'section_content' => trim($data['content']),
															  'func_data'       => trim($data['func_data'])
													)      );
													
				$DB->query("UPDATE ibf_skin_templates SET $str WHERE set_id='$setid' AND group_name='$group' AND func_name='".trim($data['func_name'])."'");
			}
			else
			{
				$data['content'] = str_replace( '\\n' , '\\\\\\n', $data['content'] );
				
				$str = $DB->compile_db_insert_string( array(
															  'section_content' => trim($data['content']),
															  'func_data'       => trim($data['func_data']),
															  'set_id'          => $setid,
															  'group_name'      => $group,
															  'func_name'       => trim($data['func_name']),
															  'can_remove'      => 0,
													)      );
													
				$DB->query("INSERT INTO ibf_skin_templates ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
			}
		}
		
		
		return TRUE;
		
		
	}
	
	/*foreach($functions as $fname => $ftext)
	  {
		  preg_match( "/return <<<(EOF|HTML)(.+?)(EOF|HTML);/s", $ftext, $matches );
		  
		  // Are we updating a current set, but have a new group to add?
		  // Who knows, but it's bloody exciting
		  
		  if ($insert == 0)
		  {
			  if ($curr_group[$name][$fname] != 1)
			  {
				  // Not a current group..
				  
				  $insert = 1;
			  }
		  }
		  
		  // Swap fake newlines
		  
		  $matches[2] = str_replace( '\n', '\\\n', $matches[2] );
		  
		  // Swap real newlines
		  //$matches[2] = str_replace( "\r", '\\r', $matches[2] );
		  //$matches[2] = str_replace( "\n", '\\n', $matches[2] );
		  
		  if ($insert == 0)
		  {
		  
			  $db_update = $DB->compile_db_update_string( array (
															  'section_content' => str_replace( '\n', '\\\n', $matches[2] ),
															  'func_data'       => trim($config[$fname]),
															  'updated'         => time(),
													)       );
																						
			  $DB->query("UPDATE ibf_skin_templates SET $db_update WHERE func_name='$fname' AND set_id='".$IN['id']."' AND group_name='$name'");
		  }
		  else
		  {
		  
			  $db_update = $DB->compile_db_update_string( array (
															  'set_id'          => $IN['id'],
															  'group_name'      => $name,
															  'section_content' => str_replace( '\n', '\\\n', $matches[2] ),
															  'func_name'       => $fname,
															  'func_data'       => trim($config[$fname]),
															  'updated'         => time(),
													)       );
													
			  //----------------------------------------------------
			  // Remove current templates with this name
			  //----------------------------------------------------
	  
			  //$DB->query("DELETE FROM ibf_skin_templates WHERE func_name='$fname' AND set_id='".$IN['id']."' AND group_name='$name'");
	  
			  $DB->query("INSERT INTO ibf_skin_templates SET $db_update");
		  //}
		  
		  
	  }*/
	
	
}


?>