<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.2
|   ========================================
|   by Matthew Mecham
|   (c) 2001 - 2003 Invision Power Services
|   http://www.invisionpower.com
|   ========================================
|   Web: http://www.invisionboard.com
|   Email: matt@invisionpower.com
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > Help Control functions
|   > Module written by Matt Mecham
|   > Date started: 2nd April 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/



$idx = new ad_settings();


class ad_settings {

	var $base_url;

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

		//---------------------------------------

		switch($IN['code'])
		{
			case 'wrapper':
				$this->list_wrappers();
				break;
				
			case 'add':
				$this->do_form('add');
				break;
				
			case 'edit':
				$this->do_form('edit');
				break;
				
			case 'doadd':
				$this->save_skin('add');
				break;
				
			case 'doedit':
				$this->save_skin('edit');
				break;
				
			case 'remove':
				$this->remove();
				break;
			
			//-------------------------
			case 'memskins':
				$this->mem_skins();
				break;
				
			case 'export':
				$this->export();
				break;
				
			default:
				$this->list_sets();
				break;
		}
		
	}
	
	//----------------------------------------------------
	
	function export()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的面板組件 ID，返回重試");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_skins WHERE uid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_macro_name WHERE set_id='".$row['macro_id']."'");
		
		if ( ! $macro_name = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_css WHERE cssid='".$row['css_id']."'");
		
		if ( ! $css = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$row['set_id']."'");
		
		if ( ! $tmpl = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_templates WHERE tmid='".$row['tmpl_id']."'");
		
		if ( ! $wrap = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		
		//+-------------------------------
		
		if ($INFO['base_dir'] == './')
		{
			$INFO['base_dir'] = str_replace( '\\', '/', getcwd() ) . '/';
		}
		
		$archive_dir   = $INFO['base_dir']."archive_out";
		$images_dir    = $INFO['base_dir']."style_images/".$row['img_dir'];
		
		require ROOT_PATH."sources/lib/tar.php";
		
		if (!is_dir($archive_dir))
		{
			$ADMIN->error("無法找到 $archive_dir，目錄存在嗎？");
		}
		
		if (!is_writeable($archive_dir))
		{
			$ADMIN->error("無法寫入 $archive_dir，由 FTP CHMOD 為 0755 或 0777 以使程式能夠進行寫入，IBF 無法為您做這件事");
		}
		
		if (!is_dir($images_dir))
		{
			$ADMIN->error("無法找到 $images_dir，目錄存在嗎？");
		}
		
		//+-------------------------------
		// Set up the dir structure
		//+-------------------------------
		
		$css_name  = "stylesheet.css";
		$wrap_name = "wrapper.html";
		$macro_name = "macro.txt";
		$template_name = "templates.html";
		
		$img_dir   = 'images';
		
		$pack_name = preg_replace( "/\s{1,}/", "_", $row['sname'] );
		
		$new_dir = "set-".$pack_name;
		
		//+-------------------------------
		
		if ( ! mkdir($archive_dir."/".$new_dir, 0777) )
		{
			$ADMIN->error("目錄建立失敗，無法匯出面板組件，請檢查 'archive_out' 的權限。");
		}
		
		//+-------------------------------
		
		if ( ! mkdir($archive_dir."/".$new_dir."/".$img_dir, 0777) )
		{
			$ADMIN->error("目錄建立失敗，無法匯出面板組件，請檢查 'archive_out' 的權限。");
		}
		
		//+-------------------------------
		// Make the wrapper file...
		//+-------------------------------
		
		$wrap['template'] = preg_replace("/\r/", "\n", $wrap['template'] );
		
		$FH = fopen($archive_dir."/".$new_dir."/".$wrap_name, 'w');
		fwrite($FH, $wrap['template'], strlen($wrap['template']));
		fclose($FH);
		
		@chmod($archive_dir."/".$new_dir."/".$wrap_name, 0777);
		
		//+-------------------------------
		// Make the css file...
		//+-------------------------------
		
		$FH = fopen($archive_dir."/".$new_dir."/".$css_name, 'w');
		fwrite($FH, $css['css_text'], strlen($css['css_text']));
		fclose($FH);
		
		@chmod($archive_dir."/".$new_dir."/".$css_name, 0777);
		
		//+-------------------------------
		// Copy over the images...
		//+-------------------------------
		
		if ( ! $ADMIN->copy_dir($images_dir, $archive_dir."/".$new_dir."/".$img_dir) )
		{
			$ADMIN->error( $ADMIN->errors );
		}
		
		//+-------------------------------
		// Make the macro file...
		//+-------------------------------
		
		$file_content = "";
		
		$DB->query("SELECT macro_replace, macro_value FROM ibf_macro WHERE macro_set='".$row['macro_id']."'");
		
		while( $mrow = $DB->fetch_row() )
		{
			if ($mrow['macro_replace'] == "")
			{
				$mrow['macro_replace'] = "*UNASSIGNED*";
			}
			$file_content .= $mrow['macro_value']."~=~".$mrow['macro_replace']."\n";
		}
		
		$FH = fopen($archive_dir."/".$new_dir."/".$macro_name, 'w');
		fwrite($FH, $file_content, strlen($file_content));
		fclose($FH);
		
		@chmod($archive_dir."/".$new_dir."/".$macro_name, 0777);
		
		//+----------------------------------------------------------------------------
		// Generate template HTML file
		//+----------------------------------------------------------------------------
		
		$output = "";
		
		$groups = $DB->query("SELECT DISTINCT(group_name) FROM ibf_skin_templates WHERE set_id='".$row['set_id']."'");
		
		if ( ! $DB->get_num_rows($groups) )
		{
			$ADMIN->rm_dir($archive_dir);
			$ADMIN->error("建立模板組件時匯出失敗： 無法從資料庫查詢資訊。");
		}
		
		$output .= "<!--TEMPLATE_SET|internal,internal,internal,internal-->\n\n";
		
		while ( $trow = $DB->fetch_row($groups) )
		{
		
			$aq = $DB->query("SELECT * FROM ibf_skin_templates WHERE set_id='".$row['set_id']."' AND group_name='".$trow['group_name']."'");
			
			if ( ! $DB->get_num_rows($aq) )
			{
				$ADMIN->rm_dir($archive_dir);
				$ADMIN->error("無法從資料庫查詢資訊");
			}
			
			$output .= "<!-- PLEASE LEAVE ALL 'IBF' COMMENTS IN PLACE, DO NOT REMOVE THEM! -->\n<!--IBF_GROUP_START:{$trow['group_name']}-->\n\n";
			
			while ( $next_row = $DB->fetch_row($aq) )
			{
				$text = $this->convert_tags($next_row['section_content']);
				$text = str_replace( "\r\n", "\n", $text);
				$text = str_replace( "\n\n", "\n", $text);
				
				$output .= "<!--IBF_START_FUNC|{$next_row['func_name']}|{$next_row['func_data']}-->\n\n";
				$output .= $text."\n";
				$output .= "<!--IBF_END_FUNC|{$next_row['func_name']}-->\n\n";
			}
			
			$output .= "\n<!--IBF_GROUP_END:{$trow['group_name']}-->\n";
		
		}
		
		$FH = fopen($archive_dir."/".$new_dir."/".$template_name, 'w');
		fwrite($FH, $output, strlen($output));
		fclose($FH);
		
		@chmod($archive_dir."/".$new_dir."/".$template_name, 0777);
		
		//+----------------------------------------------------------------------------
		// Generate the config file..
		//+----------------------------------------------------------------------------
		
		$file_content = "<?php\n\n".
					    "\$config=array('author' => \"".addslashes($tmpl['author'])."\", ".
					    "'email'=>\"".addslashes($tmpl['email'])."\", ".
					    "'url'=>\"".addslashes($tmpl['url'])."\")\n\n?".">";
		
		$FH = fopen($archive_dir."/".$new_dir."/"."templates_conf.inc", 'w');
		fwrite($FH, $file_content, strlen($file_content));
		fclose($FH);
		
		@chmod($archive_dir."/".$new_dir."/"."templates_conf.inc", 0777);
		
		//+-------------------------------
		// Add files and write tarball
		//+-------------------------------
		
		$tar = new tar();
		
		$tar->new_tar( $archive_dir, $new_dir.".tar" );
		$tar->add_directory( $archive_dir."/".$new_dir );
		$tar->write_tar();
		
		// Check for errors.
		
		if ($tar->error != "")
		{
			$ADMIN->rm_dir($archive_dir);
			$ADMIN->error($tar->error);
		}
		
		// remove original unarchived directory
		
		$ADMIN->rm_dir($archive_dir."/".$new_dir);
		
		$ADMIN->done_screen("匯出的面板套件已建立<br><br>您可以從 <a href='archive_out/{$new_dir}.tar' target='_blank'>這裡</a> 下載此 tar 檔案", "管理面板組件", "act=sets" );
		
		
	}
	
	//----------------------------------------------
	// I must get around to centralising this...
	//----------------------------------------------
	
	function convert_tags($t="")
	{
		if ($t == "")
		{
			return "";
		}
		
		$t = preg_replace( "/{?\\\$ibforums->base_url}?/"            , "{ibf.script_url}"   , $t );
		$t = preg_replace( "/{?\\\$ibforums->session_id}?/"          , "{ibf.session_id}"   , $t );
		$t = preg_replace( "/{?\\\$ibforums->skin\['?(\w+)'?\]}?/"   , "{ibf.skin.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->lang\['?(\w+)'?\]}?/"   , "{ibf.lang.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->vars\['?(\w+)'?\]}?/"   , "{ibf.vars.\\1}"      , $t );
		$t = preg_replace( "/{?\\\$ibforums->member\['?(\w+)'?\]}?/" , "{ibf.member.\\1}"    , $t );
		
		return $t;
		
	}
	
	//----------------------------------------------
	
	function mem_skins()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		$DB->query("SELECT sid FROM ibf_skins WHERE uid='".$IN['oid']."'");
		$old = $DB->fetch_row();
		
		if ($IN['nid'] == 'n')
		{
			$DB->query("UPDATE ibf_members SET skin=NULL WHERE skin='".$old['sid']."'");
		}
		else
		{
			$DB->query("SELECT sid FROM ibf_skins WHERE uid='".$IN['nid']."'");
			$new = $DB->fetch_row();
			$DB->query("UPDATE ibf_members SET skin='".$new['sid']."' WHERE skin='".$old['sid']."'");
		}
		
		$ADMIN->done_screen("已更新會員面板選擇", "管理面板組件", "act=sets" );
		
	}
	
	
	//-------------------------------------------------------------
	// REMOVE WRAPPERS
	//-------------------------------------------------------------
	
	function remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		//+-------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的面板組件 ID，返回重試");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_skins WHERE uid='".$IN['id']."'");
		
		if (! $skin = $DB->fetch_row() )
		{
			$ADMIN->error("無法從 DB 查詢該面板組件資訊");
		}
		
		//+-------------------------------
		
		if ($skin['default_set'] == 1)
		{
			$ADMIN->error("您不能將這個預設的面板組件移除，請先讓其他面板成為預設後重試。");
		}
		
		//+-------------------------------
		
		// Update the members skins..
		
		$DB->query("UPDATE ibf_members SET skin='' WHERE skin='".$skin['sid']."'");
		
		// Update the forums DB
		
		$DB->query("SELECT id FROM ibf_forums WHERE skin_id='".$skin['sid']."'");
		
		if ( $DB->get_num_rows() )
		{
			$arr = array();
			
			while ( $i = $DB->fetch_row() )
			{
				$arr['forum_skin_'.$i['id']] = '';
			}
		
			$DB->query("UPDATE ibf_forums SET skin_id='' WHERE skin_id='".$skin['sid']."'");
			
			// Remove it from the config file..
			
			$ADMIN->rebuild_config($arr);
			
		}
		
		// Remove skin from the DB
		
		$DB->query("DELETE FROM ibf_skins WHERE uid='".$IN['id']."'");
		
		$std->boink_it($SKIN->base_url."&act=sets");
			
		exit();
		
		
	}
	
	
	
	//-------------------------------------------------------------
	// ADD / EDIT SKIN SETS
	//-------------------------------------------------------------
	
	function save_skin( $type='add' )
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		//+-------------------------------
		
		if ($type == 'edit')
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("您必須指定一個已存在的面板組件 ID，返回重試");
			}
		}
		
		if ($IN['sname'] == "")
		{
			$ADMIN->error("您必須為這個面板套件 ID 指定一個名稱。");
		}
		
		
		$barney = array( 'sname'      => $std->txt_stripslashes($HTTP_POST_VARS['sname']),
						 'set_id'     => $IN['template'],
						 'tmpl_id'    => $IN['wrapper'],
						 'img_dir'     => $IN['img_dir'],
						 'css_id'     => $IN['css'],
						 'css_method' => $IN['css_method'],
						 'hidden'     => $IN['hidden'],
						 'default_set'=> $IN['default_set'],
						 'macro_id'   => $IN['macro_id'],
					   );
					   
		if ($type == 'add')
		{
		
			$DB->query("SELECT MAX(sid) as new_id FROM ibf_skins");
			
			$row = $DB->fetch_row();
			
			$barney['sid'] = $row['new_id'] + 1;
		
			$db_string = $DB->compile_db_insert_string( $barney );
			
			$DB->query("INSERT INTO ibf_skins (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");
			
			if ($IN['default_set'] == 1)
			{
				$DB->query("UPDATE ibf_skins SET default_set=0 WHERE sid <> '".$barney['sid']."'");
			}
			
			$std->boink_it($SKIN->base_url."&act=sets");
			
			exit();
			
		}
		else
		{
			$db_string = $DB->compile_db_update_string( $barney );
			
			$DB->query("UPDATE ibf_skins SET $db_string WHERE uid='".$IN['id']."'");
			
			if ($IN['default_set'] == 1)
			{
				$DB->query("UPDATE ibf_skins SET default_set=0 WHERE uid <> '".$IN['id']."'");
			}
			
			$ADMIN->done_screen("已更新面板組件", "管理面板組件", "act=sets" );
		}
		
		
	}
	
	//-------------------------------------------------------------
	// ADD / EDIT SETS
	//-------------------------------------------------------------
	
	function do_form( $type='add' )
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------
		
		$css       = array();
		$wrappers  = array();
		$templates = array();
		$macros    = array();
		
		//+-------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須指定一個已存在的包裝 ID，返回重試");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * from ibf_skins WHERE uid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法從資料庫查詢此資訊");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_macro_name");
		
		while( $img = $DB->fetch_row() )
		{
			$macros[] = array( $img['set_id'], $img['set_name'] );
		}
		
		//+-------------------------------
		
		$DB->query("SELECT cssid, css_name FROM ibf_css");
		
		while ( $c = $DB->fetch_row() )
		{
			$css[] = array( $c['cssid'], $c['css_name'] );
		}
		
		//+-------------------------------
		
		$DB->query("SELECT tmid, name FROM ibf_templates");
		
		while ( $t = $DB->fetch_row() )
		{
			$wrappers[] = array( $t['tmid'], $t['name'] );
		}
		
		//+-------------------------------
		
		$DB->query("SELECT skid, skname FROM ibf_tmpl_names");
		
		while ( $s = $DB->fetch_row() )
		{
			$templates[] = array( $s['skid'], $s['skname'] );
		}
		
		
		//+-------------------------------
		
		if ($type == 'add')
		{
			$code = 'doadd';
			$button = '建立面板組件';
			$row['sname'] = $row['sname'].".2";
			$row['default_set'] = 0;
		}
		else
		{
			$code = 'doedit';
			$button = '編輯面板組件';
		}
		
		$dirs = array();
		
		$dh = opendir( './style_images' );
 		while ( $file = readdir( $dh ) )
 		{
 			if (($file != ".") && ($file != ".."))
 			{
				if ( is_dir('./style_images/'.$file) )
				{
					$dirs[] = array( $file, $file );
				}
 			}
 		}
 		closedir( $dh );
 		
 		//+-------------------------------
 		
 		if ( file_exists( ROOT_PATH."cache" ) )
		{
			if ( is_writeable( ROOT_PATH."cache" ) )
			{
				$cssextra = $SKIN->form_dropdown('css_method', array( 0 => array( 'inline', 'Inline'), 1 => array( 'external', 'External' ) ), $row['css_method']);
			}
		}
		
		//+-------------------------------
	
		$ADMIN->page_detail = "您可以搭配組合您想要套用到此面板組件的面板資源。";
		$ADMIN->page_title  = "管理面板組件";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $code      ),
												  2 => array( 'act'   , 'sets'      ),
												  3 => array( 'id'    , $IN['id']   ),
									     ), "theAdminForm", "onSubmit=\"return no_specialchars('sets')\""    );
									     
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );

		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( $button );
		
		$ADMIN->html .= $SKIN->js_no_specialchars();
		
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>面板組件名稱</b>",
													$SKIN->form_input('sname', $row['sname']),
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>使用模板：</b>",
													$SKIN->form_dropdown('template', $templates, $row['set_id']),
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>使用圖樣組件：</b>",
													$SKIN->form_dropdown('macro_id', $macros, $row['macro_id']),
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>使用圖片目錄：</b>",
													$SKIN->form_dropdown('img_dir', $dirs, $row['img_dir']),
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>使用樣式表：</b>",
													$SKIN->form_dropdown('css', $css, $row['css_id']).'&nbsp;'.$cssextra,
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>使用包裝：</b>",
													$SKIN->form_dropdown('wrapper', $wrappers, $row['tmpl_id']),
									     )      );
									     

		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>對會員隱藏？</b><br>如果您想要讓討論區只有唯一的面板，這項很有用處",
													$SKIN->form_yes_no('hidden', $row['hidden']),
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( 
													"<b>做為預設面板組件？</b><br>用於未指派的討論區及會員面板",
													$SKIN->form_yes_no('default_set', $row['default_set']),
									     )      );
												 
		$ADMIN->html .= $SKIN->end_form($button);
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
		
		
	}
	
	//-------------------------------------------------------------
	// SHOW WRAPPERS
	//-------------------------------------------------------------
	
	function list_sets()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$form_array = array();
		
		$forums      = array();
		$forum_skins = array();
	
		$ADMIN->page_detail = "您可以增加/編輯與移除面板組件。<br><br>面板組件是風格資源的集合，您可以搭配組合論壇包裝、圖片及圖樣組件、樣式表及面板模板。如果您想要編輯任何的資源，請從左邊的選單欄選擇您要管理的資源。";
		$ADMIN->page_title  = "管理面板組件";
		
		//+-------------------------------
		
		$DB->query("SELECT id, name FROM ibf_forums");
		
		while ( $f = $DB->fetch_row() )
		{
			$forums[ $f['id'] ] = $f['name'];
			
			if ($INFO['forum_skin_'.$f['id']] != "")
			{
				$forum_skins[ $INFO['forum_skin_'.$f['id']] ][] = $f['name'];
			}
		}
		
		
		$DB->query("select ibf_skins.*, count(ibf_members.uid) as mcount from ibf_skins left join ibf_members on(ibf_members.skin=ibf_skins.sid) where (ibf_members.skin is not null or ibf_skins.default_set = 1) group by ibf_skins.sid order by ibf_skins.sname");
		
		$used_ids = array();
		
		if ( $DB->get_num_rows() )
		{
		
			$SKIN->td_header[] = array( "名稱"       , "40%" );
			$SKIN->td_header[] = array( "會員數" , "20%" );
			$SKIN->td_header[] = array( "匯出"      , "10%" );
			$SKIN->td_header[] = array( "編輯"        , "10%" );
			$SKIN->td_header[] = array( "移除"      , "10%" );
			$SKIN->td_header[] = array( "隱藏"      , "5%" );
			$SKIN->td_header[] = array( "預設"     , "5%" );
			
			//+-------------------------------
		
			$ADMIN->html .= $SKIN->start_table( "目前正被會員使用的面板" );
			
			while ( $r = $DB->fetch_row() )
			{
				$extra = "";
				
				if ( is_array($forum_skins[ $r['sid'] ]) )
				{
					if ( count($forum_skins[ $r['sid'] ]) > 0 )
					{
						$extra = "<br>(Used in forums: ".implode( ",", $forum_skins[ $r['sid'] ] )." )";
					}
				}
				
				$default = '&nbsp;';
				$hidden  = '&nbsp;';
				
				if ($r['hidden'] == 1)
				{
					$hidden = "<span style='color:red;font-weight:bold'>X</span>";
				}
				
				if ($r['default_set'] == 1)
				{
					$default = "<span style='color:red;font-weight:bold'>X</span>";
				}
				
				$editlist = "<br /><b>Edit:</b> <a href='{$SKIN->base_url}&act=wrap&code=edit&id={$r['tmpl_id']}'>包裝</a>
							&middot; <a href='{$SKIN->base_url}&act=templ&code=edit&id={$r['set_id']}'>HTML</a>
							&middot; <a href='{$SKIN->base_url}&act=style&code=edit&id={$r['css_id']}'>CSS</a>
							&middot; <a href='{$SKIN->base_url}&act=style&code=colouredit&id={$r['css_id']}'>顏色</a>
							&middot; <a href='{$SKIN->base_url}&act=image&code=edit&id={$r['macro_id']}'>圖樣</a>";
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$std->txt_stripslashes($r['sname'])."</b>$extra".$editlist,
														  "<center>".$r['mcount']."</center>",
														  "<center><a href='".$SKIN->base_url."&act=sets&code=export&id={$r['uid']}'>匯出</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=sets&code=edit&id={$r['uid']}'>編輯</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=sets&code=remove&id={$r['uid']}'>移除</a></center>",
														  "<center>$hidden</center>",
														  "<center>$default</center>",
												 )      );
												   
				$used_ids[] = $r['uid'];
				
				$form_array[] = array( $r['uid'], $r['sname'] );
				
			}
			
			$ADMIN->html .= $SKIN->end_table();
		}
		
		if ( count($used_ids) < 1 )
		{
			$DB->query("SELECT * FROM ibf_skins");
			
			$left_one = $DB->get_num_rows();
		}
		else if ( count($used_ids) > 0 )
		{
			$DB->query("SELECT * FROM ibf_skins WHERE uid NOT IN(".implode(",",$used_ids).")");
			
			$left_two = $DB->get_num_rows();
		}
		
		if ( $left_one > 0 or $left_two > 0 )
		{
			$SKIN->td_header[] = array( "名稱"  , "60%" );
			$SKIN->td_header[] = array( "匯出" , "10%" );
			$SKIN->td_header[] = array( "編輯"   , "10%" );
			$SKIN->td_header[] = array( "移除" , "10%" );
			$SKIN->td_header[] = array( "隱藏" , "5%" );
			$SKIN->td_header[] = array( "預設", "5%" );
		
			$ADMIN->html .= $SKIN->start_table( "未被會員使用的面板組件" );
			
			while ( $r = $DB->fetch_row() )
			{
				
				$extra = "";
			
				if ( is_array($forum_skins[ $r['sid'] ]) )
				{
					if ( count($forum_skins[ $r['sid'] ]) > 0 )
					{
						$extra = "<br>(Used in forums: ".implode( ",", $forum_skins[ $r['sid'] ] )." )";
					}
				}
				
				$default = '&nbsp;';
				$hidden  = '&nbsp;';
				
				if ($r['hidden'] == 1)
				{
					$hidden = "<span style='color:red;font-weight:bold'>X</span>";
				}
				
				if ($r['default_set'] == 1)
				{
					$default = "<span style='color:red;font-weight:bold'>X</span>";
				}
				
				$editlist = "<br /><b>Edit:</b> <a href='{$SKIN->base_url}&act=wrap&code=edit&id={$r['tmpl_id']}'>包裝</a>
							&middot; <a href='{$SKIN->base_url}&act=templ&code=edit&id={$r['set_id']}'>HTML</a>
							&middot; <a href='{$SKIN->base_url}&act=style&code=edit&id={$r['css_id']}'>CSS</a>
							&middot; <a href='{$SKIN->base_url}&act=style&code=colouredit&id={$r['css_id']}'>顏色</a>
							&middot; <a href='{$SKIN->base_url}&act=image&code=edit&id={$r['macro_id']}'>圖樣</a>";
				
				$ADMIN->html .= $SKIN->js_checkdelete();
				
			
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$std->txt_stripslashes($r['sname'])."</b>$extra".$editlist,
														  "<center><a href='".$SKIN->base_url."&act=sets&code=export&id={$r['uid']}'>匯出</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=sets&code=edit&id={$r['uid']}'>編輯</a></center>",
														  "<center><a href='javascript:checkdelete(\"act=sets&code=remove&id={$r['uid']}\")'>移除</a></center>",
														  "<center>$hidden</center>",
														  "<center>$default</center>",
												 )      );
												 
				$form_array[] = array( $r['uid'], $r['sname'] );
												   
			}
			
			$ADMIN->html .= $SKIN->end_table();
		}
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'add'     ),
												  2 => array( 'act'   , 'sets'    ),
									     )      );
		
		$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'  , 'memskins'     ),
												  2 => array( 'act'   , 'sets'         ),
									     )      );
		
		$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( "oid", $form_array)
								 )      );
								 
		$form_array[] = array( 'n', '無 (將會使用任何預設的)' );
								 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>更新為使用面板...</b>",
										  		  $SKIN->form_dropdown( "nid", $form_array)
								 )      );
		
		$ADMIN->html .= $SKIN->end_form("更新會員面板選擇");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
	
	}
	
	
}


?>
