<?php

/*
+--------------------------------------------------------------------------
|   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 -> Image Macro functions
|   > Module written by Matt Mecham
|   > Date started: 4th 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 'wrapper':
				$this->list_current();
				break;
				
			case 'add':
				$this->add_macro_set();
				break;
				
			case 'edit':
				$this->show_macros();
				break;
				
			case 'doedit':
				$this->edit_set_name();
				break;
				
			case 'macroedit':
				$this->macro_form('edit');
				break;
				
			case 'macroadd':
				$this->macro_form('add');
				break;
				
			case 'macroremove':
				$this->macro_remove();
				break;
				
			case 'doeditmacro':
				$this->macro_edit();
				break;
				
			case 'doaddmacro':
				$this->macro_add();
				break;
				
			case 'remove':
				$this->remove();
				break;
				
			case 'export':
				$this->export();
				break;
			
			case 'import':
				$this->import();
				break;
			
			//-------------------------
			default:
				$this->list_current();
				break;
		}
		
	}
	
	function import()
	{
		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
			
			$ADMIN->error("沒有選擇要上傳的檔案，請返回重試");
		}
		
		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 macro file..
		//-------------------------------------------------
		
		if ( ! preg_match( "/~=~/", $data ) )
		{
			$ADMIN->error("這個檔案好像不是有效的 Invision Power Board 圖樣檔案");
		}
		
		//-------------------------------------
		// Get the new set_id
		//-------------------------------------
		
		$DB->query("SELECT MAX(set_id) as max FROM ibf_macro_name");
		
		$max = $DB->fetch_row();
		
		$new_id = $max['max'] + 1;
		
		//-------------------------------------
		// Process data
		//-------------------------------------
		
		$init_array = array();
		$final_keys = array();
		
		$init_array = explode("\n", $data);
		
		foreach( $init_array as $l )
		{
			if (preg_match( "~=~", $l ) )
			{
				// is valid line
				
				list($k, $v) = explode( "~=~", $l );
				
				$k = trim($k);
				$v = trim($v);
				
				$final_keys[$k] = $v;
			}
		}
		
		//-------------------------------------
		// Insert the data in the DB
		//-------------------------------------
		
		foreach( $final_keys as $k => $v)
		{
			if ($v == '*UNASSIGNED*')
			{
				$v = "";
			}
			
			$row['macro_replace'] = str_replace( '\n', "\n", $v);
			
			$str = $DB->compile_db_insert_string( array (
														'macro_value'   => $std->txt_stripslashes($k),
														'macro_replace' => $v,
														'macro_set'     => $new_id,
														'can_remove'    => 1,
											)       );
		
			$DB->query("INSERT INTO ibf_macro ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
		}
		
		//----------------------------------
		// Insert macro name into DB
		//----------------------------------
		
		$set_name = "New Macro Set (Upload ID: ".substr( time(), -6 ).")";
		
		$str = $DB->compile_db_insert_string( array ( 'set_name'     => $set_name,
													  'set_id'       => $new_id,
											)       );
											
		$DB->query("INSERT INTO ibf_macro_name ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
		
		$ADMIN->done_screen("圖樣組件匯入完成", "管理圖樣組件", "act=image" );
		
	}
	
	//-------------------------------------------------------------
	// Edit set name
	//-------------------------------------------------------------
	
	function edit_set_name()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		$DB->query("SELECT * FROM ibf_macro WHERE macro_set='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		$DB->query("UPDATE ibf_macro_name SET set_name='{$IN['setname']}' WHERE set_id='".$IN['id']."'");
		
		$ADMIN->done_screen("圖樣組件已更新", "管理圖樣組件 {$row['macro_name']}", "act=image&code=edit&id={$row['macro_set']}" );
		
	}
	
	//-------------------------------------------------------------
	// Apply the edit to the DB
	//-------------------------------------------------------------
	
	function macro_remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		$DB->query("SELECT * FROM ibf_macro WHERE macro_id='".$IN['mid']."'");
		
		$row = $DB->fetch_row();
		
		$DB->query("DELETE FROM ibf_macro WHERE macro_id='".$IN['mid']."'");
		
		$ADMIN->done_screen("圖樣已移除", "管理圖樣組件 {$row['macro_name']}", "act=image&code=edit&id={$row['macro_set']}" );
		
	}
	
	//-------------------------------------------------------------
	// Apply the edit to the DB
	//-------------------------------------------------------------
	
	function macro_edit()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		$DB->query("SELECT * FROM ibf_macro WHERE macro_id='".$IN['mid']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從 DB 中取得該記錄");
		}
		
		$key = str_replace( "'", "\\'", $std->txt_safeslashes($HTTP_POST_VARS['macro_value']) );
		$val = str_replace( "'", "\\'", $std->txt_UNhtmlspecialchars($std->txt_safeslashes($HTTP_POST_VARS['macro_replace']) ) );
		
		$DB->query("UPDATE ibf_macro SET macro_value='$key', macro_replace='$val' WHERE macro_id='".$IN['mid']."'");
		
		$ADMIN->done_screen("圖樣已更新", "管理圖樣組件 {$row['macro_name']}", "act=image&code=edit&id={$row['macro_set']}" );
		
	}
	
	//------------------------------------------------
	
	
	function macro_add()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		$DB->query("SELECT * FROM ibf_macro WHERE macro_set='".$IN['id']."' LIMIT 0,1");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		$str = $DB->compile_db_insert_string( array (
														'macro_value'   => $std->txt_safeslashes($HTTP_POST_VARS['macro_value']),
														'macro_replace' => $std->txt_UNhtmlspecialchars($std->txt_safeslashes($HTTP_POST_VARS['macro_replace'])),
														'can_remove'    => 1,
														'macro_set'     => $row['macro_set']
											)       );
		
		$DB->query("INSERT INTO ibf_macro ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
		
		$ADMIN->done_screen("已增加圖樣", "管理圖樣組件 {$row['macro_name']}", "act=image&code=edit&id={$row['macro_set']}" );
		
	}
	
	//-------------------------------------------------------------
	// Print the edit/add formy wormy
	//-------------------------------------------------------------
	
	function macro_form($type='edit')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		
		if ($type == 'edit')
		{
		
			if ($IN['mid'] == "")
			{
				$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
			}
			
			//+-------------------------------
			
			$DB->query("SELECT m.*, mn.set_name from ibf_macro m, ibf_macro_name mn WHERE macro_id='".$IN['mid']."' AND mn.set_id=m.macro_set");
			
			if ( ! $row = $DB->fetch_row() )
			{
				$ADMIN->error("無法從資料庫中查詢該資訊");
			}
			
			$button = "編輯此圖樣";
			$title  = "編輯 <a href='{$SKIN->base_url}&act=image&code=edit&id={$row['macro_set']}'>{$row['set_name']} 組件中的圖樣</a>";
			$code   = 'doeditmacro';
		
		}
		else
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
			}
			
			//+-------------------------------
			
			$DB->query("SELECT set_name from ibf_macro_name WHERE set_id='".$IN['id']."'");
			
			if ( ! $row = $DB->fetch_row() )
			{
				$ADMIN->error("無法從資料庫中查詢該資訊");
			}
			
			$code = 'doaddmacro';
			$title = "增加圖樣到組件 <a href='{$SKIN->base_url}&act=image&code=edit&id={$IN['id']}'>{$row['set_name']}</a>";
			$button = '增加此圖樣';
		}
		
		//+-------------------------------
	
		$ADMIN->page_detail = "此圖樣 '鍵值' 將可被任何包裝或模板檔案使用。<br>
							 <b>範例：</b> 如果您增加一個 'green_font' 鍵值及一個 '&lt;font color='green'>' 以作為替代，則每一個 <span style='color:red'><b>&lt;{green_font}&gt;</b></span> 引例會被轉換為 &lt;font color='green'>
							 <br><b>&lt;#IMG_DIR#></b> 可用於任何圖樣，當使用此圖樣組件於一個面板之中時，這會被置換為您所選擇的圖片目錄名稱。";
		$ADMIN->page_title  = "管理圖樣組件： {$row['set_name']}";
		
		//+-------------------------------
		
		
		$ADMIN->html .= $SKIN->js_no_specialchars();
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $code    ),
												  2 => array( 'act'   , 'image'     ),
												  3 => array( 'id'    , $IN['id']   ),
												  4 => array( 'mid'   , $IN['mid']   ),
									     )       );
									     
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"   , "20%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "80%" );

		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( $title );
		
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"圖樣鍵值",
													"&lt;{".$SKIN->form_input('macro_value', $row['macro_value'])."}&gt;",
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"圖樣替換",
													$SKIN->form_textarea('macro_replace', $std->txt_htmlspecialchars($row['macro_replace']) ),
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form($button);
									     
		$ADMIN->html .= $SKIN->end_table();
									     
		$ADMIN->output();
	}
	
	
	
	//+-------------------------------
	
	
	function export()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個存在的圖片組件 ID，請返回重試。");
		}
		
		$DB->query("SELECT set_name FROM ibf_macro_name WHERE set_id='".$IN['id']."'");
		
		$name = $DB->fetch_row();
		
		//+-------------------------------
		// Pass file as an attachment, yeah!
		//+-------------------------------
		
		$l_name = preg_replace( "/\s{1,}/", "_", $name['set_name'] );
		
		$file_name = "macro-".substr($l_name, 0, 8).".txt";
		
		$contents = "";
		
		$DB->query("SELECT macro_replace, macro_value FROM ibf_macro WHERE macro_set='".$IN['id']."'");
		
		while( $row = $DB->fetch_row() )
		{
			if ($row['macro_replace'] == "")
			{
				$row['macro_replace'] = "*UNASSIGNED*";
			}
			
			// Fix newlines..
			
			$row['macro_replace'] = str_replace( "\n", '\n', str_replace("\r", "\n", $row['macro_replace'] ) );
			
			$contents .= $row['macro_value']."~=~".$row['macro_replace']."\n";
		}
		
		@header("Content-type: unknown/unknown");
		@header("Content-Disposition: attachment; filename=$file_name");
		
		print $contents."\n";
		
		exit();
		
	}
	
	//-------------------------------------------------------------
	// Add images..
	//-------------------------------------------------------------
	
	
	function add_macro_set()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		
		$DB->query("SELECT * FROM ibf_macro_name WHERE set_id='".$IN['id']."'");
		
		//-------------------------------------
		
		if ( ! $mac = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		//-------------------------------------
		
		$mac['set_name'] = $mac['set_name']." (Copy)";
		
		//-------------------------------------
		// Get the new set_id
		//-------------------------------------
		
		$DB->query("SELECT MAX(set_id) as max FROM ibf_macro_name");
		
		$max = $DB->fetch_row();
		
		$new_id = $max['max'] + 1;
		
		$q1 = $DB->query("SELECT macro_value, macro_replace FROM ibf_macro WHERE macro_set='{$IN['id']}'");
		
		while( $row = $DB->fetch_row($q1) )
		{
			$str = $DB->compile_db_insert_string( array (
														'macro_value'   => $std->txt_stripslashes($row['macro_value']),
														'macro_replace' => $std->txt_stripslashes($row['macro_replace']),
														'macro_set'     => $new_id,
														'can_remove'    => 1,
											)       );
		
			$q2 = $DB->query("INSERT INTO ibf_macro ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
			//$q2 = $DB->query("INSERT INTO ibf_macro SET macro_value='{$row['macro_value']}',macro_replace='{$row['macro_replace']}', macro_set='$new_id'");
		}
		
		$DB->query("INSERT INTO ibf_macro_name SET set_id='$new_id', set_name='{$mac['set_name']}'");
		
		$ADMIN->done_screen("已增加圖樣組件", "管理圖樣組件", "act=image" );
		
	}
	
	//-------------------------------------------------------------
	// REMOVE WRAPPERS
	//-------------------------------------------------------------
	
	function remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		//+-------------------------------
		
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		$DB->query("DELETE FROM ibf_macro WHERE macro_set='".$IN['id']."'");
		
		$DB->query("DELETE FROM ibf_macro_name WHERE set_id='".$IN['id']."'");
				
		$std->boink_it($SKIN->base_url."&act=image");
	}
	
	
	
	
	//-------------------------------------------------------------
	// ADD / EDIT MACRO SETS
	//-------------------------------------------------------------
	
	function show_macros()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個現有的圖樣組件 ID，請返回重試");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT m.*, ms.* from ibf_macro m, ibf_macro_name ms WHERE m.macro_set='".$IN['id']."' AND ms.set_id=m.macro_set LIMIT 0,1");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫中查詢該資訊");
		}
		
		//+-------------------------------
	
		$ADMIN->page_detail = "要編輯一個圖樣，只要點按對應圖樣的 '編輯' 連結即可。
							 <br>此圖樣 '鍵值' 將可被任何包裝或模板檔案使用。<br>
							 <b>範例：</b> 如果您增加一個 'green_font' 鍵值及一個 '&lt;font color='green'>' 以作為替代，則每一個 <span style='color:red'><b>&lt;{green_font}&gt;</b></span> 引例會被轉換為 &lt;font color='green'>
							 <br><b>&lt;#IMG_DIR#></b> 可用於任何圖樣，當使用此圖樣組件於一個面板之中時，這會被置換為您所選擇的圖片目錄名稱。";
		$ADMIN->page_title  = "管理圖樣組件： {$row['set_name']}";
		
		//+-------------------------------
		
		
		$ADMIN->html .= $SKIN->js_no_specialchars();
		$ADMIN->html .= $SKIN->js_checkdelete();
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doedit'    ),
												  2 => array( 'act'   , 'image'     ),
												  3 => array( 'id'    , $IN['id']   ),
									     ), "theAdminForm", "onSubmit=\"return no_specialchars('images')\""       );
									     
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"   , "20%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "80%" );

		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "編輯圖樣組件名稱" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"圖樣組件名稱",
													$SKIN->form_input('setname', $row['set_name']),
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("編輯圖樣名稱");
									     
		$ADMIN->html .= $SKIN->end_table();
									     
		//+-------------------------------
		
		$SKIN->td_header[] = array( "圖樣鍵值"           , "20%" );
		$SKIN->td_header[] = array( "圖樣替換"   , "45%" );
		$SKIN->td_header[] = array( "預覽"             , "15%" );
		$SKIN->td_header[] = array( "編輯"                , "10%" );
		$SKIN->td_header[] = array( "移除"              , "10%" );

		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "目前的圖樣 ["."<a href='".$SKIN->base_url."&act=image&code=macroadd&id={$row['macro_set']}'>增加一個圖樣</a>"." ]" );
		
		// Get img_dir this set is using...
		
		$DB->query("SELECT img_dir from ibf_skins WHERE macro_id='".$IN['id']."'");
		
		$skin = $DB->fetch_row();
		
		$DB->query("SELECT * from ibf_macro WHERE macro_set='".$IN['id']."'");
		
		while( $row = $DB->fetch_row() )
		{
			
			$real = str_replace( "<", "&lt;", $row['macro_replace'] );
			$real = str_replace( ">", "&gt;", $real );
			
			if ( strlen($real) > 55 )
			{
				$real = substr( $real, 0, 52 ) . '...';
			}
			
			if ( $INFO['preview'] == 0 )
			{
				$preview = '<em>已從 ACP 偏好禁止</em>';
			}
			else
			{
			$preview = str_replace( "<#IMG_DIR#>", $skin['img_dir'], $row['macro_replace'] );
			}
			
			$remove = "無法移除";
			
			if ($row['can_remove'] == 1)
			{
				$remove = "<center><a href='javascript:checkdelete(\"act=image&code=macroremove&mid={$row['macro_id']}\")'>移除</a></center>";
				
			}
			
			
			$ADMIN->html .= $SKIN->add_td_row( array( 
												"<b>{$row['macro_value']}</b>",
												$real,
												$preview,
												"<center><a href='".$SKIN->base_url."&act=image&code=macroedit&mid={$row['macro_id']}'>編輯</a></center>",
												$remove,
									 )      );
		}
									     
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
		
		
	}
	
	//-------------------------------------------------------------
	// SHOW WRAPPERS
	//-------------------------------------------------------------
	
	function list_current()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$form_array = array();
	
		$ADMIN->page_detail = "您可以在這裡管理您的圖樣組件，這賦予您有能力使用文字連結來取代按鈕或以 Flash 影片替代特定圖片。";
		$ADMIN->page_title  = "管理圖樣組件";
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "名稱"        , "40%" );
		$SKIN->td_header[] = array( "指派"   , "30%" );
		$SKIN->td_header[] = array( "匯出"       , "10%" );
		$SKIN->td_header[] = array( "編輯"         , "10%" );
		$SKIN->td_header[] = array( "移除"       , "10%" );
		
		//+-------------------------------
		
		$DB->query("SELECT DISTINCT(s.macro_id), i.macro_set, ms.set_name, s.sname from ibf_macro i, ibf_macro_name ms, ibf_skins s WHERE s.macro_id=i.macro_set  AND ms.set_id=i.macro_set ORDER BY ms.set_name 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['macro_set'] ] .= stripslashes($r['sname'])."<br>";
			
				if ( in_array( $r['macro_set'], $used_ids ) )
				{
					continue;
				}
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$std->txt_stripslashes($r['set_name'])."</b>",
														  "<#X-{$r['macro_set']}#>",
														  "<center><a href='".$SKIN->base_url."&act=image&code=export&id={$r['macro_set']}'>匯出</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=image&code=edit&id={$r['macro_set']}'>編輯</a></center>",
														  "<i>移除前請先撤銷指派</i>",
												 )      );
												   
				$used_ids[] = $r['macro_set'];
				
				$form_array[] = array( $r['macro_set'], $r['set_name'] );
				
			}
			
			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 set_id, set_name FROM ibf_macro_name WHERE set_id NOT IN(".implode(",",$used_ids).")");
		
			if ( $DB->get_num_rows() )
			{
			
				$SKIN->td_header[] = array( "名稱"  , "70%" );
				$SKIN->td_header[] = array( "匯出" , "10%" );
				$SKIN->td_header[] = array( "編輯"   , "10%" );
				$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>".$std->txt_stripslashes($r['set_name'])."</b>",
															  "<center><a href='".$SKIN->base_url."&act=image&code=export&id={$r['set_id']}'>匯出</a></center>",
															  "<center><a href='".$SKIN->base_url."&act=image&code=edit&id={$r['set_id']}'>編輯</a></center>",
															  "<center><a href='javascript:checkdelete(\"act=image&code=remove&id={$r['set_id']}\")'>移除</a></center>",
													 )      ); 
													 
					$form_array[] = array( $r['set_id'], $r['set_name'] );
													   
				}
				
				$ADMIN->html .= $SKIN->end_table();
			}
		}
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'add'     ),
												  2 => array( 'act'   , 'image'    ),
									     )      );
		
		$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->end_form("建立一個新的圖樣組件");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'import'     ),
												  2 => array( 'act'   , 'image'    ),
												  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><u>或者</u> 從您的電腦選擇一個要匯入的檔案</b><br>注意：這必須是一個有效的圖樣組件",
												  $SKIN->form_upload(),
										 )      );
		
		$ADMIN->html .= $SKIN->end_form("匯入圖樣組件");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
	
	}
	
	
}


?>