<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.1
|   ========================================
|   by Matthew Mecham
|   (c) 2001,2002 Invision Power Services
|   http://www.ibforums.com
|   ========================================
|   Web: http://www.ibforums.com
|   Email: phpboards@ibforums.com
|   Licence Info: phpib-licence@ibforums.com
+---------------------------------------------------------------------------
|
|   > 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, 请检查目录属性（CHMOD 777）。");
		}
		
		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("无法在数据库中找到相关主题界面信息");
		}
		
		//+-------------------------------
		
		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'      => stripslashes($HTTP_POST_VARS['sname']),
						 'set_id'     => $IN['template'],
						 'tmpl_id'    => $IN['wrapper'],
						 'img_dir'     => $IN['img_dir'],
						 'css_id'     => $IN['css'],
						 '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 );
 		
 		
		
		//+-------------------------------
	
		$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>使用CSS文件：</b>",
													$SKIN->form_dropdown('css', $css, $row['css_id']),
									     )      );
									     
		$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 = "你可以在下面 添加/编辑/删除 主题界面文件。";
		$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.id) 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>(使用论坛栏目：".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>";
				}
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".stripslashes($r['sname'])."</b>$extra",
														  "<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>(使用论坛栏目：".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>";
				}
				
				$ADMIN->html .= $SKIN->js_checkdelete();
				
			
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".stripslashes($r['sname'])."</b>$extra",
														  "<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();
	
	}
	
	
}


?>