<?php
/*
// ------------------------------------------------------------------------
-+ Date: 24-Jan-2004
-+ Version: 1.3
-+ ========================================
-+ 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
+---------------------------------------------------------------------------
|
|   > Admin Forum functions
|   > Module written by Matt Mecham
|   > Date started: 17th March 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/

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


$idx = new ad_groups();


class ad_groups {

	var $base_url;

	function ad_groups() {
		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 'doadd':
				$this->save_group('add');
				break;
				
			case 'add':
				$this->group_form('add');
				break;
				
			case 'edit':
				$this->group_form('edit');
				break;
			
			case 'doedit':
				$this->save_group('edit');
				break;
			
			case 'delete':
				$this->delete_form();
				break;
			
			case 'dodelete':
				$this->do_delete();
				break;
				
			//-------------------------	
				
			case 'fedit':
				$this->forum_perms();
				break;
				
			case 'pdelete':
				$this->delete_mask();
				break;
				
			case 'dofedit':
				$this->do_forum_perms();
				break;
						
			case 'permsplash':
				$this->permsplash();
				break;
				
			case 'view_perm_users':
				$this->view_perm_users();
				break;
					
			case 'remove_mask':
				$this->remove_mask();
				break;
				
			case 'preview_forums':
				$this->preview_forums();
				break;
				
			case 'dopermadd':
				$this->add_new_perm();
				break;
				
			case 'donameedit':
				$this->edit_name_perm();
				break;
			//-------------------------			
					
					
						
			default:
				$this->main_screen();
				break;
		}
		
	}
	
	//+---------------------------------------------------------------------------------
	//
	// Member group /forum mask permission form thingy doodle do yes. Viewing Perm users
	//
	//+---------------------------------------------------------------------------------
	
	function delete_mask()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------------------
		// Check for a valid ID
		//+-------------------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析權限遮蔽 ID，請重試");
		}
		
		$DB->query("DELETE FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		$old_id = intval($IN['id']);
		
		//+-------------------------------------------
		// Remove from forums...
		//+-------------------------------------------
		
		$get = $DB->query("SELECT id, read_perms, reply_perms, start_perms, upload_perms FROM ibf_forums");
				
		while( $f = $DB->fetch_row($get) )
		{
			$d_str = "";
			$d_arr = array();
			
			foreach( array( 'read_perms', 'reply_perms', 'start_perms', 'upload_perms' ) as $perm_bit )
			{
				if ($f[ $perm_bit ] != '*')
				{
					if ( preg_match( "/(^|,)".$old_id."(,|$)/", $f[ $perm_bit ]) )
					{
						$f[ $perm_bit ] = preg_replace( "/(^|,)".$old_id."(,|$)/", "\\1\\2", $f[ $perm_bit ] );
						
						$d_arr[ $perm_bit ] = $this->clean_perms( $f[ $perm_bit ] );
					}
				}
			}
			
			// Do we have anything to save?
				
			if ( count($d_arr) > 0 )
			{
				$d_str = $DB->compile_db_update_string( $d_arr );
				
				// Sure?..
				
				if ( strlen($d_str) > 5)
				{
					$save = $DB->query("UPDATE ibf_forums SET $d_str WHERE id={$f['id']}");
				}
			}
		}
		
		$this->permsplash();
	}
	
	//+---------------------------------------------------------------------------------
	
	
	function add_new_perm()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$IN['new_perm_name'] = trim($IN['new_perm_name']);
		
		if ($IN['new_perm_name'] == "")
		{
			$ADMIN->error("You must enter a name");
		}
		
		$copy_id = $IN['new_perm_copy'];
		
		//+-------------------------------------------
		// UPDATE DB
		//+-------------------------------------------
		
		$DB->query("INSERT INTO ibf_forum_perms SET perm_name='".$IN['new_perm_name']."'");
		
		$new_id = $DB->get_insert_id();
		
		if ( $copy_id != 'none' )
		{
			//+-------------------------------------------
			// Add new mask to forum accesses
			//+-------------------------------------------
		
			$old_id = intval($copy_id);
			
			if ( ($new_id > 0) and ($old_id > 0) )
			{
				$get = $DB->query("SELECT id, read_perms, reply_perms, start_perms, upload_perms FROM ibf_forums");
				
				while( $f = $DB->fetch_row($get) )
				{
					$d_str = "";
					$d_arr = array();
					
					foreach( array( 'read_perms', 'reply_perms', 'start_perms', 'upload_perms' ) as $perm_bit )
					{
						if ($f[ $perm_bit ] != '*')
						{
							if ( preg_match( "/(^|,)".$old_id."(,|$)/", $f[ $perm_bit ]) )
							{
								$d_arr[ $perm_bit ] = $this->clean_perms( $f[ $perm_bit ] ) . ",".$new_id;
							}
						}
					}
					
					// Do we have anything to save?
						
					if ( count($d_arr) > 0 )
					{
						$d_str = $DB->compile_db_update_string( $d_arr );
						
						// Sure?..
						
						if ( strlen($d_str) > 5)
						{
							$save = $DB->query("UPDATE ibf_forums SET $d_str WHERE id={$f['id']}");
						}
					}
				}
			}
			
		
		}
		
		$this->permsplash();
			
		
	}
	
	//-_-_-_-_-_-_-_-_-
	//_-_-_-_-_-_-_-_-_
	//Now that's pretty
	
	function preview_forums()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------------------
		// Check for a valid ID
		//+-------------------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析權限遮蔽 ID，請重試");
		}
		
		
		$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		if ( ! $perms = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析權限遮蔽 ID，請重試");
		}
		
		//+-------------------------------------------
		// What we doin'?
		//+-------------------------------------------
		
		switch( $IN['t'] )
		{
			case 'start':
				$human_type = '建立主題';
				$code_word  = 'start_perms';
				break;
				
			case 'reply':
				$human_type = '回覆主題';
				$code_word  = 'reply_perms';
				break;
				
			default:
				$human_type = '檢視討論區';
				$code_word  = 'read_perms';
				break;
		}
		
		//+-------------------------------------------
		// Get all members using that ID then!
		//+-------------------------------------------
		
		$SKIN->td_header[] = array( "$human_type" , "100%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "預覽使用： " . $perms['perm_name'] );
		
		$last_cat_id = -1;
		
		$DB->query("SELECT f.id as forum_id, f.parent_id, f.subwrap, f.sub_can_post, f.name as forum_name, f.position, f.read_perms, f.start_perms, f.reply_perms, c.id as cat_id, c.name
				    FROM ibf_forums f
				     LEFT JOIN ibf_categories c ON (c.id=f.category)
				    ORDER BY c.position, f.position");
		
		
		$forum_keys = array();
		$cat_keys   = array();
		$children   = array();
		$subs       = array();
		$the_html   = "";
		
		$perm_id    = intval($IN['id']);
		
		while ( $i = $DB->fetch_row() )
		{
			
			if ($i['subwrap'] == 1 and $i['sub_can_post'] != 1)
			{
				$forum_keys[ $i['cat_id'] ][$i['forum_id']] = "&nbsp;&nbsp;- {$i['forum_name']}\n";
			}
			else
			{
				if ($i[ $code_word ] == '*')
				{
					if ($i['parent_id'] > 0)
					{
						$children[ $i['parent_id'] ][] = "&nbsp;&nbsp;---- {$i['forum_name']}\n";
					}
					else
					{
						$forum_keys[ $i['cat_id'] ][$i['forum_id']] = "&nbsp;&nbsp;- {$i['forum_name']}\n";
					}
				}
				else if (preg_match( "/(^|,)".$perm_id."(,|$)/", $i[ $code_word ]) )
				{
					if ($i['parent_id'] > 0)
					{
						$children[ $i['parent_id'] ][] = "&nbsp;&nbsp;---- {$i['forum_name']}\n";
					}
					else
					{
						$forum_keys[ $i['cat_id'] ][$i['forum_id']] = "&nbsp;&nbsp;- {$i['forum_name']}\n";
					}
				}
				else
				{
					//-------------------------------------
					// CAN'T ACCESS
					//-------------------------------------
					
					if ($i['parent_id'] > 0)
					{
						$children[ $i['parent_id'] ][] = "<span style='color:gray'>&nbsp;&nbsp;---- {$i['forum_name']}</span>\n";
					}
					else
					{
						$forum_keys[ $i['cat_id'] ][$i['forum_id']] = "<span style='color:gray'>&nbsp;&nbsp;- {$i['forum_name']}</span>\n";
					}
				}
			}
			
			if ($last_cat_id != $i['cat_id'])
			{
				
				// Make sure cats with hidden forums are not shown in forum jump
				
				$cat_keys[ $i['cat_id'] ] = "<b>{$i['name']}</b>\n";
							              
				$last_cat_id = $i['cat_id'];
				
			}
		}
		
		foreach($cat_keys as $cat_id => $cat_text)
		{
			if ( is_array( $forum_keys[$cat_id] ) && count( $forum_keys[$cat_id] ) > 0 )
			{
				$the_html .= $cat_text;
				
				foreach($forum_keys[$cat_id] as $idx => $forum_text)
				{
					$the_html .= $forum_text;
					
					if (count($children[$idx]) > 0)
					{
						foreach($children[$idx] as $ii => $tt)
						{
							$the_html .= $tt;
						}
					}
				}
			}
		}
		
		$the_html = str_replace( "\n", "<br />\n", $the_html );
		
		$ADMIN->html .= $SKIN->add_td_row( array( $the_html )      );
		
		$ADMIN->html .= $SKIN->end_table();
		
		//----------------------------------------------------------------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'preview_forums' ),
												  2 => array( 'act'   , 'group'   ),
												  3 => array( 'id'    , $IN['id']      ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;" , "60%" );
		$SKIN->td_header[] = array( "&nbsp;" , "40%" );
		
		$ADMIN->html .= $SKIN->start_table( "Legend & Info" );
		
		$ADMIN->html .= $SKIN->add_td_row( array(
													"可以在此討論區 $human_type",
													"<input type='text' readonly='readonly' style='border:1px solid black;background-color:black;size=30px' name='blah'>"
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array(
													"不能在此討論區 $human_type",
													"<input type='text' readonly='readonly' style='border:1px solid gray;background-color:gray;size=30px' name='blah'>"
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array(
													"套用於...",
													$SKIN->form_dropdown( 't',
																		array( 0 => array( 'start', '建立主題'    ),
																			   1 => array( 'reply', '回覆主題' ),
																			   2 => array( 'read' , '讀取討論區'      ),
																			  ), $IN['t'] )
										 )      );
										 
		$ADMIN->html .= $SKIN->end_form( "更新" );
		
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
							   
	}
	
	//===========================================================================
	
	function remove_mask()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------------------
		// Check for a valid ID
		//+-------------------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該會員 ID，請重試");
		}
		
		//+-------------------------------------------
		// Get, check and reset
		//+-------------------------------------------
		
		$DB->query("SELECT uid, uname, org_perm_id FROM ibf_members WHERE uid=".intval($IN['id']));
		
		if ( ! $mem = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析該會員 ID，請重試");
		}
		
		if ( $IN['pid'] == 'all' )
		{
			$DB->query("UPDATE ibf_members SET org_perm_id=0 WHERE uid=".intval($IN['id']));
		}
		else
		{
			$IN['pid'] = intval($IN['pid']);
			
			$pid_array = explode( ",", $mem['org_perm_id'] );
			
			if ( count($pid_array) < 2 )
			{
				$DB->query("UPDATE ibf_members SET org_perm_id=0 WHERE uid=".intval($IN['id']));
			}
			else
			{
				$new_arr = array();
				
				foreach( $pid_array as $sid )
				{
					if ( $sid != $IN['pid'] )
					{
						$new_arr[] = $sid;
					}
				}
				
				$DB->query("UPDATE ibf_members SET org_perm_id='".implode(",",$new_arr)."' WHERE uid=".intval($IN['id']));
			}	
			
		}
			
		//+-------------------------------------------
		// Get all members using that ID then!
		//+-------------------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;" , "100%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "結果" );
		
		
		
		$ADMIN->html .= $SKIN->add_td_row( array( "從 <b>{$mem['uname']}</b> 移除自訂權限遮蔽",
										 )      );
	
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
							   
	}
	
	//===========================================================================
	
	
	function view_perm_users()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------------------
		// Check for a valid ID
		//+-------------------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析權限遮蔽 ID，請重試");
		}
		
		
		$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		if ( ! $perms = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析權限遮蔽 ID，請重試");
		}
		
		//+-------------------------------------------
		// Get all members using that ID then!
		//+-------------------------------------------
		
		$SKIN->td_header[] = array( "使用者資料" , "50%" );
		$SKIN->td_header[] = array( "動作"       , "50%" );
		
		//+-------------------------------
		
		$ADMIN->html .= "<script language='javascript' type='text/javascript'>
						 <!--
						  function pop_close_and_stop( id )
						  {
						  	opener.location = \"{$SKIN->base_url}&act=mem&code=doform&MEMBER_ID=\" + id;
						  	self.close();
						  }
						  //-->
						  </script>";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "會員使用： " . $perms['perm_name'] );
		
		$outer = $DB->query("SELECT uid, uname, email, posts, org_perm_id FROM ibf_members WHERE (org_perm_id IS NOT NULL AND org_perm_id <> 0) ORDER BY uname");
		
		while( $r = $DB->fetch_row($outer) )
		{
			$exp_pid = explode( ",", $r['org_perm_id'] );
			
			foreach( explode( ",", $r['org_perm_id'] ) as $pid )
			{
				if ( $pid == $IN['id'] )
				{
					if ( count($exp_pid) > 1 )
					{
						$extra = "<li>Also using: <em style='color:red'>";
						
						$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id IN ({$r['org_perm_id']}) AND perm_id <> {$IN['id']}");
						
						while ( $mr = $DB->fetch_row() )
						{
							$extra .= $mr['perm_name'].",";
						}
						
						$extra = preg_replace( "/,$/", "", $extra );
						
						$extra .= "</em>";
					}
					else
					{
						$extra = "";
					}
					
					$ADMIN->html .= $SKIN->add_td_row( array( "<div style='font-weight:bold;font-size:11px;padding-bottom:6px;margin-bottom:3px;border-bottom:1px solid #000'>{$r['uname']}</div>
															   <li>文章： {$r['posts']}
															   <li>電子郵件： {$r['email']}
															   $extra" ,
															  "&#149;&nbsp;<a href='{$SKIN->base_url}&amp;act=group&amp;code=remove_mask&amp;id={$r['uid']}&amp;pid=$pid' title='從該使用者移除此遮蔽 (如果他們擁有多重遮蔽不會全部移除'>移除此遮蔽</a>
															   <br />&#149;&nbsp;<a href='{$SKIN->base_url}&amp;act=group&amp;code=remove_mask&amp;id={$r['uid']}&amp;pid=all' title='移除全部的使用者遮蔽'>移除全部遮蔽</a>
															   <br /><br />&#149;&nbsp;<a href='javascript:pop_close_and_stop(\"{$r['uid']}\");'>編輯會員</a>",
													 )      );
				}
			}
		}
		
						     
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
							   
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// Member group /forum mask permission form thingy doodle do yes.
	//
	//+---------------------------------------------------------------------------------
	
	
	function permsplash()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "討論區權限遮蔽 [ 首頁 ]";
		
		$ADMIN->page_detail = "您可以在這裡管理討論區權限遮蔽";
		
		$ADMIN->page_detail .= "<br /><b>使用群組</b> 與會員群組使用的權限遮蔽有關
								<br /><b>使用會員</b> 設定以此權限遮蔽凌駕群組所使用的權限遮蔽會員數
							    <br /><b>預覽</b> 以一種快速、便利的格式來預覽此遮蔽可存取的討論區
							   ";
								
		
		//+-------------------------------------------
		// Get the names for the perm masks w/id
		//+-------------------------------------------
		
		$perms = array();
		
		$DB->query("SELECT * FROM ibf_forum_perms");
		
		while( $r = $DB->fetch_row() )
		{
			$perms[ $r['perm_id'] ] = $r['perm_name'];
		}
		
		//+-------------------------------------------
		// Get the number of members using this mask
		// as an over ride
		//+-------------------------------------------
		
		$mems = array();
		
		$DB->query("SELECT COUNT(uid) as count, org_perm_id FROM ibf_members WHERE (org_perm_id IS NOT NULL AND org_perm_id <> 0) GROUP by org_perm_id");
		
		while( $r = $DB->fetch_row() )
		{
			if ( strstr($r['org_perm_id'] , "," ) )
			{
				foreach( explode( ",", $r['org_perm_id'] ) as $pid )
				{
					$mems[ $pid ] += $r['count'];
				}
			}
			else
			{
				$mems[ $r['org_perm_id'] ] += $r['count'];
			}
		}
		
		//+-------------------------------------------
		// Get the member group names and the mask
		// they use
		//+-------------------------------------------
		
		$groups = array();
		
		$DB->query("SELECT g_id, g_title, g_perm_id FROM ibf_groups");
		
		while( $r = $DB->fetch_row() )
		{
			if ( strstr($r['g_perm_id'] , "," ) )
			{
				foreach( explode( ",", $r['g_perm_id'] ) as $pid )
				{
					$groups[ $pid ][] = $r['g_title'];
				}
			}
			else
			{
				$groups[ $r['g_perm_id'] ][] = $r['g_title'];
			}
		}
		
		//+-------------------------------------------
		// Print the splash screen
		//+-------------------------------------------
		
		$SKIN->td_header[] = array( "遮蔽名稱"          , "20%" );
		$SKIN->td_header[] = array( "使用群組"   , "20%" );
		$SKIN->td_header[] = array( "使用會員"     , "20%" );
		$SKIN->td_header[] = array( "預覽"            , "10%" );
		$SKIN->td_header[] = array( "編輯"               , "15%" );
		$SKIN->td_header[] = array( "刪除"             , "15%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->js_pop_win();
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "討論區權限遮蔽" );
		
		foreach( $perms as $id => $name )
		{
			$groups_used = "";
			
			$is_active = 0;
			
			if ( is_array( $groups[ $id ] ) )
			{
				foreach( $groups[ $id ] as $bleh => $g_title )
				{
					$groups_used .= $g_title . "<br />";
				}
				
				$is_active = 1;
				
			}
			else
			{
				$groups_used = "<center><i>無</i></center>";
			}
			
			$mems_used = 0;
			
			if ( $mems[ $id ] > 0 )
			{
				$is_active = 1;
				$mems_used = $mems[ $id ] . " (<a href='javascript:pop_win(\"&amp;act=group&amp;code=view_perm_users&amp;id=$id\", \"User\", \"500\",\"350\");' title='在新視窗檢視使用此遮蔽的會員名稱'>檢視</a>)";
			}
			
			if ( $is_active > 0 )
			{
				$delete = "<i>不行，使用中</i>";
			}
			else
			{
				$delete = "<a href='{$SKIN->base_url}&amp;act=group&amp;code=pdelete&amp;id=$id'>刪除</a>";
			}
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>$name</b>" ,
													  "$groups_used",
													  "<center>$mems_used</center>",
													  "<center><a href='javascript:pop_win(\"&amp;act=group&amp;code=preview_forums&amp;id=$id&amp;t=read\", \"400\",\"350\");' title='看看這個群組所能看到的..'>預覽</a></center>",
													  "<center><a href='{$SKIN->base_url}&amp;act=group&amp;code=fedit&amp;id=$id'>編輯</a></center>",
													  "<center>$delete</center>",
											 )      );
		
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$dlist = array();
		
		$dlist[] = array( 'none', '不要繼承' );
		
		foreach( $perms as $id => $name )
		{
			$dlist[] = array( $id, $name );
		}
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dopermadd' ),
												  2 => array( 'act'   , 'group'   ),
									     )      );
									     
		
		$SKIN->td_header[] = array( "{none}" , "60%" );
		$SKIN->td_header[] = array( "{none}" , "40%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "建立新的權限遮蔽" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>權限遮蔽名稱</b>" ,
												  $SKIN->form_input( 'new_perm_name' ),
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>繼承討論區權限遮蔽從...</b>" ,
												 $SKIN->form_dropdown( 'new_perm_copy', $dlist ),
										 )      );
		
		$ADMIN->html .= $SKIN->end_form("建立");
						     
		$ADMIN->html .= $SKIN->end_table();
		
		
		
		$ADMIN->output();
			
			
	}
	
	
	
	function forum_perms()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析群組 ID，請重試");
		}
		
		//+----------------------------------
		
		$ADMIN->page_title = "討論區權限遮蔽 [ 編輯 ]";
		
		$ADMIN->page_detail = "您可以在這裡管理您的討論區權限遮蔽";
		
		$ADMIN->page_detail .= "<br />核取方塊以允許該動作的權限，或剔選方塊禁止該動作的權限。
							   <br /><b>整體</b> 表示所有現存及未來的權限遮蔽可進行該動作，並且無法被改變。";
		
		//+----------------------------------
		
		$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		$group = $DB->fetch_row();
		
		$gid   = $group['perm_id'];
		$gname = $group['perm_name'];
		
		//+-------------------------------
		
		$cats     = array();
		$forums   = array();
		$children = array();
		
		$DB->query("SELECT * from ibf_categories WHERE id > 0 ORDER BY position ASC");
		
		while ($r = $DB->fetch_row())
		{
			$cats[$r['id']] = $r;
		}
		
		$DB->query("SELECT * from ibf_forums ORDER BY position ASC");
		
		while ($r = $DB->fetch_row())
		{
			
			if ($r['parent_id'] > 0)
			{
				$children[ $r['parent_id'] ][] = $r;
			}
			else
			{
				$forums[] = $r;
			}
			
		}
		
		//+----------------------------------
		//| EDIT NAME
		//+----------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'donameedit' ),
												  2 => array( 'act'   , 'group'   ),
												  3 => array( 'id'    , $gid      ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;"   , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "60%" );
		
		$ADMIN->html .= $SKIN->start_table( "重新命名群組： ".$group['perm_name'] );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>遮蔽名稱</b>" ,
												  $SKIN->form_input("perm_name", $gname )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("編輯名稱");
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		//+----------------------------------
		//| MAIN FORM
		//+----------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dofedit' ),
												  2 => array( 'act'   , 'group'   ),
												  3 => array( 'id'    , $gid      ),
									     )      );
		
		$SKIN->td_header[] = array( "討論區名稱"   , "40%" );
		$SKIN->td_header[] = array( "讀取"         , "15%" );
		$SKIN->td_header[] = array( "回覆"        , "15%" );
		$SKIN->td_header[] = array( "建立"        , "15%" );
		$SKIN->td_header[] = array( "上傳"       , "15%" );
		
		$ADMIN->html .= $SKIN->start_table( $group['perm_name']." 的討論區存取權限" );
		
		$last_cat_id = -1;
		
		foreach ($cats as $c)
		{
			
			$ADMIN->html .= $SKIN->add_td_basic( $c['name'], 'left', 'catrow' );
													   
			$last_cat_id = $c['id'];
			
			
			foreach($forums as $r)
			{	
			
				if ($r['category'] == $last_cat_id)
				{
				
					$read   = "";
					$start  = "";
					$reply  = "";
					$upload = "";
					$global = '<center id="mgblue"><i>整體</i></center>';
					
					if ($r['read_perms'] == '*')
					{
						$read = $global;
					}
					else if ( preg_match( "/(^|,)".$gid."(,|$)/", $r['read_perms'] ) )
					{
						$read = "<center id='mgblue'><input type='checkbox' name='read_".$r['id']."' value='1' checked></center>";
					}
					else
					{
						$read = "<center id='mgblue'><input type='checkbox' name='read_".$r['id']."' value='1'></center>";
					}
					
					//---------------------------
					
					$global = '<center id="mgred"><i>整體</i></center>';
					
					if ($r['start_perms'] == '*')
					{
						$start = $global;
					}
					else if ( preg_match( "/(^|,)".$gid."(,|$)/", $r['start_perms'] ) )
					{
						$start = "<center id='mgred'><input type='checkbox' name='start_".$r['id']."' value='1' checked></center>";
					}
					else
					{
						$start = "<center id='mgred'><input type='checkbox' name='start_".$r['id']."' value='1'></center>";
					}
					
					//---------------------------
					
					$global = '<center id="mggreen"><i>整體</i></center>';
					
					if ($r['reply_perms'] == '*')
					{
						$reply = $global;
					}
					else if ( preg_match( "/(^|,)".$gid."(,|$)/", $r['reply_perms'] ) )
					{
						$reply = "<center id='mggreen'><input type='checkbox' name='reply_".$r['id']."' value='1' checked></center>";
					}
					else
					{
						$reply = "<center id='mggreen'><input type='checkbox' name='reply_".$r['id']."' value='1'></center>";
					}
					
					//---------------------------
					
					$global = '<center id="memgroup"><i>整體</i></center>';
					
					if ($r['upload_perms'] == '*')
					{
						$upload = $global;
					}
					else if ( preg_match( "/(^|,)".$gid."(,|$)/", $r['upload_perms'] ) )
					{
						$upload = "<center id='memgroup'><input type='checkbox' name='upload_".$r['id']."' value='1' checked></center>";
					}
					else
					{
						$upload = "<center id='memgroup'><input type='checkbox' name='upload_".$r['id']."' value='1'></center>";
					}
					
					//---------------------------
					
					if ($r['subwrap'] == 1 and $r['sub_can_post'] != 1)
					{
						$ADMIN->html .= $SKIN->add_td_basic( "&gt; ".$r['name'], 'left', 'catrow2' );
					}
					else
					{
						$css = $r['subwrap'] == 1 ? 'catrow2' : '';
						
						$ADMIN->html .= $SKIN->add_td_row( array(
															   "<b> - ".$r['name']."</b>",
															   $read,
															   $reply,
															   $start,
															   $upload
													 )   , $css   );
					}
													 
					if ( ( isset($children[ $r['id'] ]) ) and ( count ($children[ $r['id'] ]) > 0 ) )
					{
						foreach($children[ $r['id'] ] as $idx => $rd)
						{
							$read   = "";
							$start  = "";
							$reply  = "";
							$upload = "";
							$global = "<center id='mgblue'><i>整體</i></center>";
							
							if ($rd['read_perms'] == '*')
							{
								$read = $global;
							}
							else if ( preg_match( "/(^|,)".$gid."(,|$)/", $rd['read_perms'] ) )
							{
								$read = "<center id='mgblue'><input type='checkbox' name='read_".$rd['id']."' value='1' checked></center>";
							}
							else
							{
								$read = "<center id='mgblue'><input type='checkbox' name='read_".$rd['id']."' value='1'></center>";
							}
							
							//---------------------------
							
							$global = "<center id='mgred'><i>整體</i></center>";
							
							if ($rd['start_perms'] == '*')
							{
								$start = $global;
							}
							else if ( preg_match( "/(^|,)".$gid."(,|$)/", $rd['start_perms'] ) )
							{
								$start = "<center id='mgred'><input type='checkbox' name='start_".$rd['id']."' value='1' checked></center>";
							}
							else
							{
								$start = "<center id='mgred'><input type='checkbox' name='start_".$rd['id']."' value='1'></center>";
							}
							
							//---------------------------
							
							$global = "<center id='mggreen'><i>整體</i></center>";
							
							if ($rd['reply_perms'] == '*')
							{
								$reply = $global;
							}
							else if ( preg_match( "/(^|,)".$gid."(,|$)/", $rd['reply_perms'] ) )
							{
								$reply = "<center id='mggreen'><input type='checkbox' name='reply_".$rd['id']."' value='1' checked></center>";
							}
							else
							{
								$reply = "<center id='mggreen'><input type='checkbox' name='reply_".$rd['id']."' value='1'></center>";
							}
							
							//---------------------------
							
							$global = "<center id='memgroup'><i>整體</i></center>";
							
							if ($rd['upload_perms'] == '*')
							{
								$upload = $global;
							}
							else if ( preg_match( "/(^|,)".$gid."(,|$)/", $rd['upload_perms'] ) )
							{
								$upload = "<center id='memgroup'><input type='checkbox' name='upload_".$rd['id']."' value='1' checked></center>";
							}
							else
							{
								$upload = "<center id='memgroup'><input type='checkbox' name='upload_".$rd['id']."' value='1'></center>";
							}
							
							//---------------------------
					
							$ADMIN->html .= $SKIN->add_td_row( array(
															   "<b> --- ".$rd['name']."</b>",
															   $read,
															   $reply,
															   $start,
															   $upload
													 ) , 'subforum'     );
						}
					}					 
				}
			}
		}
		
		$ADMIN->html .= $SKIN->end_form("更新討論區權限");
		
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
	}
	
	
	function edit_name_perm()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//---------------------------
		// Check for legal ID
		//---------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該群組 ID");
		}
		
		if ( $IN['perm_name'] == "" )
		{
			$ADMIN->error("您必須輸入一個名稱");
		}
		
		$gid = $IN['id'];
		
		//---------------------------
		
		$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		if ( ! $gr = $DB->fetch_row() )
		{
			$ADMIN->error("不是有效的群組 ID");
		}
		
		$DB->query("UPDATE ibf_forum_perms SET perm_name='{$IN['perm_name']}' WHERE perm_id='".$IN['id']."'");
		
		$ADMIN->save_log("已編輯討論區存取權限名稱的遮蔽： '{$gr['perm_name']}'");
		
		$ADMIN->done_screen("已更新討論區存取權限", "權限遮蔽控制", "act=group&code=permsplash" );
		
		
	}
	
	
	
	function do_forum_perms()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//---------------------------
		// Check for legal ID
		//---------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該群組 ID");
		}
		
		$gid = $IN['id'];
		
		//---------------------------
		
		$DB->query("SELECT * FROM ibf_forum_perms WHERE perm_id='".$IN['id']."'");
		
		if ( ! $gr = $DB->fetch_row() )
		{
			$ADMIN->error("不是有效的群組 ID");
		}
		
		//---------------------------
		// Pull the forum data..
		//---------------------------
		
		$forum_q = $DB->query("SELECT id, read_perms, start_perms, reply_perms, upload_perms FROM ibf_forums ORDER BY position ASC");
		
		while ( $row = $DB->fetch_row( $forum_q ) )
		{
		
			$read  = "";
			$reply = "";
			$start = "";
			$upload = "";
			//---------------------------
			// Is this global?
			//---------------------------
			
			if ($row['read_perms'] == '*')
			{
				$read = '*';
				
			}
			else
			{
				//---------------------------
				// Split the set IDs
				//---------------------------
				
				$read_ids = explode( ",", $row['read_perms'] );
				
				if ( is_array($read_ids) )
				{
				   foreach ($read_ids as $i)
				   {
					   //---------------------------
					   // If it's the current ID, skip
					   //---------------------------
					   
					   if ($gid == $i)
					   {
						   continue;
					   }
					   else
					   {
						   $read .= $i.",";
					   }
				   }
				}
				//---------------------------
				// Was the box checked?
				//---------------------------
				
				if ($IN[ 'read_'.$row['id'] ] == 1)
				{
					// Add our group ID...
					
					$read .= $gid.",";
				}
				
				// Tidy..
				
				$read = preg_replace( "/,$/", "", $read );
				$read = preg_replace( "/^,/", "", $read );
				
			}
			
			//---------------------------
			// Reply topics..
			//---------------------------
				
			if ($row['reply_perms'] == '*')
			{
				$reply = '*';
			}
			else
			{
				$reply_ids = explode( ",", $row['reply_perms'] );
				
				if ( is_array($reply_ids) )
				{
				
					foreach ($reply_ids as $i)
					{
						if ($gid == $i)
						{
							continue;
						}
						else
						{
							$reply .= $i.",";
						}
					}
				
				}
				
				if ($IN[ 'reply_'.$row['id'] ] == 1)
				{
					$reply .= $gid.",";
				}
				
				$reply = preg_replace( "/,$/", "", $reply );
				$reply = preg_replace( "/^,/", "", $reply );
			}
			
			//---------------------------
			// Start topics..
			//---------------------------
				
			if ($row['start_perms'] == '*')
			{
				$start = '*';
			}
			else
			{
				$start_ids = explode( ",", $row['start_perms'] );
				
				if ( is_array($start_ids) )
				{
				
					foreach ($start_ids as $i)
					{
						if ($gid == $i)
						{
							continue;
						}
						else
						{
							$start .= $i.",";
						}
					}
				
				}
				
				if ($IN[ 'start_'.$row['id'] ] == 1)
				{
					$start .= $gid.",";
				}
				
				$start = preg_replace( "/,$/", "", $start );
				$start = preg_replace( "/^,/", "", $start );
			}
			
			//---------------------------
			// Upload topics..
			//---------------------------
				
			if ($row['upload_perms'] == '*')
			{
				$upload = '*';
			}
			else
			{
				$upload_ids = explode( ",", $row['upload_perms'] );
				
				if ( is_array($upload_ids) )
				{
				
					foreach ($upload_ids as $i)
					{
						if ($gid == $i)
						{
							continue;
						}
						else
						{
							$upload .= $i.",";
						}
					}
				
				}
				
				if ($IN[ 'upload_'.$row['id'] ] == 1)
				{
					$upload .= $gid.",";
				}
				
				$upload = preg_replace( "/,$/", "", $upload );
				$upload = preg_replace( "/^,/", "", $upload );
			}
			
			//---------------------------
			// Update the DB...
			//---------------------------
			
			if (! $new_q = $DB->query("UPDATE ibf_forums SET read_perms='$read', reply_perms='$reply', start_perms='$start', upload_perms='$upload' WHERE id='".$row['id']."'") )
			{
				die ("更新查詢失敗於討論區 ID ".$row['id']);
			}
			
		}
		
		$ADMIN->save_log("已編輯遮蔽 '{$gr['perm_name']}' 的討論區存取權限");
		
		$ADMIN->done_screen("已更新討論區權限遮蔽", "權限遮蔽控制", "act=group&code=permsplash" );
		
	}
	
	//+---------------------------------------------------------------------------------
	//
	// Delete a group
	//
	//+---------------------------------------------------------------------------------
	
	function delete_form()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析此群組 ID，請重試");
		}
		
		if ($IN['id'] < 5)
		{
			$ADMIN->error("您不能移除預設的群組，只能改名及編輯權限機能");
		}
		
		$ADMIN->page_title = "刪除一個使用者群組";
		
		$ADMIN->page_detail = "請檢查確定您正準備移除正常的群組";
		
		
		//+-------------------------------
		
		$DB->query("SELECT COUNT(uid) as users FROM ibf_members WHERE mgroup='".$IN['id']."'");
		$black_adder = $DB->fetch_row();
		
		if ($black_adder['users'] < 1)
		{
			$black_adder['users'] = 0;
		}
		
		$DB->query("SELECT g_title FROM ibf_groups WHERE g_id='".$IN['id']."'");
		$group = $DB->fetch_row();
		
		//+-------------------------------
		
		$DB->query("SELECT g_id, g_title FROM ibf_groups WHERE g_id <> '".$IN['id']."'");
		
		$mem_groups = array();
		
		while ( $r = $DB->fetch_row() )
		{
			$mem_groups[] = array( $r['g_id'], $r['g_title'] );
		}
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dodelete'  ),
												  2 => array( 'act'   , 'group'     ),
												  3 => array( 'id'    , $IN['id']   ),
												  4 => array( 'name'  , $group['g_title'] ),
									     )      );
									     
		
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "移除確認： ".$group['g_title'] );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>在這個群組的使用者數量</b>" ,
												  "<b>".$black_adder['users']."</b>",
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搬移這個群組的使用者到...</b>" ,
												  $SKIN->form_dropdown("to_id", $mem_groups )
									     )      );
		
		$ADMIN->html .= $SKIN->end_form("刪除這個群組");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
			
	}
	
	function do_delete()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該群組 ID, 請重試");
		}
		
		if ($IN['to_id'] == "")
		{
			$ADMIN->error("沒有指定要移至的群組 ID /我哭了，嗚嗚。");
		}
		
		// Check to make sure that the relevant groups exist.
		
		$DB->query("SELECT g_id FROM ibf_groups WHERE g_id IN(".$IN['id'].",".$IN['to_id'].")");
		
		if ( $DB->get_num_rows() != 2 )
		{
			$ADMIN->error("無法解析 ID，群組刪除跳過");
		}
		
		$DB->query("UPDATE ibf_members SET mgroup='".$IN['to_id']."' WHERE mgroup='".$IN['id']."'");
		
		$DB->query("DELETE FROM ibf_groups WHERE g_id='".$IN['id']."'");
		
		// Look for promotions in case we have members to be promoted to this group...
		
		$prq = $DB->query("SELECT g_id FROM ibf_groups WHERE g_promotion LIKE '{$IN['id']}&%'");
		
		while ( $row = $DB->fetch_row($prq) )
		{
			$nq = $DB->query("UPDATE ibf_groups SET g_promotion='-1&-1' WHERE g_id='".$row['g_id']."'");
		}
		
		// Remove from moderators table
		
		$DB->query("DELETE FROM ibf_moderators WHERE is_group=1 AND group_id=".$IN['id']);
		
		$ADMIN->save_log("移除會員群組 '{$IN['name']}'");
		
		$ADMIN->done_screen("已移除群組", "群組控制", "act=group" );
		
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// Save changes to DB
	//
	//+---------------------------------------------------------------------------------
	
	function save_group($type='edit')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['g_title'] == "")
		{
			$ADMIN->error("您必須輸入一個群組名稱");
		}
		
		if ($type == 'edit')
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("無法解析此群組 id");
			}
			
			if ($IN['id'] == $INFO['admin_group'] and $IN['g_access_cp'] != 1)
			{
				$ADMIN->error("您不能移除這個群組進入管理控制台的權限");
			}
		}
		
		//------------------------------------
		// Sort out the perm mask id things
		//------------------------------------
		
		if ( is_array( $HTTP_POST_VARS['permid'] ) )
		{
			$perm_id = implode( ",", $HTTP_POST_VARS['permid'] );
		}
		else
		{
			$ADMIN->error("未選擇權限遮蔽");
		}
		
		// Build up the hashy washy for the database ..er.. wase.
		
		$prefix = preg_replace( "/&#39;/", "'" , $std->txt_safeslashes($HTTP_POST_VARS['prefix']) );
		$prefix = preg_replace( "/&lt;/" , "<" , $prefix          );
		$suffix = preg_replace( "/&#39;/", "'" , $std->txt_safeslashes($HTTP_POST_VARS['suffix']) );
		$suffix = preg_replace( "/&lt;/" , "<" , $suffix          );
		
		$promotion_a = '-1'; //id
		$promotion_b = '-1'; // posts
		
		if ($IN['g_promotion_id'] > 0)
		{
			$promotion_a = $IN['g_promotion_id'];
			$promotion_b = $IN['g_promotion_posts'];
		}
		
		//if ($IN['g_max_messages'] == 0)
		//{
			//$IN['g_max_messages'] = -1;
		//}
		
		//list($p_max, $p_width, $p_height) = explode( ":", $group['g_photo_max_vars'] );
		
		$IN['p_max']    = str_replace( ":", "", $IN['p_max'] );
		$IN['p_width']  = str_replace( ":", "", $IN['p_width'] );
		$IN['p_height'] = str_replace( ":", "", $IN['p_height'] );
		
		$db_string = array(
							 'g_view_board'         => $IN['g_view_board'],
							 'g_mem_info'           => $IN['g_mem_info'],
							 'g_other_topics'       => $IN['g_other_topics'],
							 'g_use_search'         => $IN['g_use_search'],
							 'g_email_friend'       => $IN['g_email_friend'],
							 'g_invite_friend'      => $IN['g_invite_friend'],
							 'g_edit_profile'       => $IN['g_edit_profile'],
							 'g_post_new_topics'    => $IN['g_post_new_topics'],
							 'g_reply_own_topics'   => $IN['g_reply_own_topics'],
							 'g_reply_other_topics' => $IN['g_reply_other_topics'],
							 'g_edit_posts'         => $IN['g_edit_posts'],
							 'g_edit_cutoff'        => $IN['g_edit_cutoff'],
							 'g_delete_own_posts'   => $IN['g_delete_own_posts'],
							 'g_open_close_posts'   => $IN['g_open_close_posts'],
							 'g_delete_own_topics'  => $IN['g_delete_own_topics'],
							 'g_post_polls'         => $IN['g_post_polls'],
							 'g_vote_polls'         => $IN['g_vote_polls'],
							 'g_use_pm'             => $IN['g_use_pm'],
							 'g_is_supmod'          => $IN['g_is_supmod'],
							 'g_access_cp'          => $IN['g_access_cp'],
							 'g_title'              => trim($IN['g_title']),
							 'g_can_remove'         => $IN['g_can_remove'],
							 'g_append_edit'        => $IN['g_append_edit'],
							 'g_access_offline'     => $IN['g_access_offline'],
							 'g_avoid_q'            => $IN['g_avoid_q'],
							 'g_avoid_flood'        => $IN['g_avoid_flood'],
							 'g_icon'               => trim($IN['g_icon']),
							 'g_attach_max'         => $IN['g_attach_max'],
							 'g_avatar_upload'      => $IN['g_avatar_upload'],
							 'g_calendar_post'      => $IN['g_calendar_post'],
							 'g_max_messages'       => $IN['g_max_messages'],
							 'g_max_mass_pm'        => $IN['g_max_mass_pm'],
							 'g_search_flood'       => $IN['g_search_flood'],
							 'prefix'               => $prefix,
							 'suffix'               => $suffix,
							 'g_promotion'          => $promotion_a.'&'.$promotion_b,
							 'g_hide_from_list'     => $IN['g_hide_from_list'],
							 'g_post_closed'        => $IN['g_post_closed'],
							 'g_perm_id'			=> $perm_id,
							 'g_photo_max_vars'	    => $IN['p_max'].':'.$IN['p_width'].':'.$IN['p_height'],
							 'g_dohtml'			    => $IN['g_dohtml'],
							 'g_edit_topic'			=> $IN['g_edit_topic'],
							 'g_email_limit'		=> intval($IN['join_limit']).':'.intval($IN['join_flood']),
							 
						  );
						  
		if ($type == 'edit')
		{
			$rstring = $DB->compile_db_update_string( $db_string );
			
			$DB->query("UPDATE ibf_groups SET $rstring WHERE g_id='".$IN['id']."'");
			
			// Update the title of the group held in the mod table incase it changed.
			
			$DB->query("UPDATE ibf_moderators SET group_name='".trim($IN['g_title'])."' WHERE group_id='".$IN['id']."'");
			
			$ADMIN->save_log("編輯群組 '{$IN['g_title']}'");
			
			$ADMIN->done_screen("群組已編輯", "群組控制", "act=group" );
			
		}
		else
		{
			$rstring = $DB->compile_db_insert_string( $db_string );
			
			$DB->query("INSERT INTO ibf_groups (" .$rstring['FIELD_NAMES']. ") VALUES (". $rstring['FIELD_VALUES'] .")");
			
			$ADMIN->save_log("已增加群組 '{$IN['g_title']}'");
			
			$ADMIN->done_screen("已增加群組", "群組控制", "act=group" );
		}
	}
	
	function clean_perms($str)
	{
		$str = preg_replace( "/,$/", "", $str );
		$str = str_replace(  ",,", ",", $str );
		
		return $str;
	}
	
	//+---------------------------------------------------------------------------------
	//
	// Add / edit group
	//
	//+---------------------------------------------------------------------------------
	
	function group_form($type='edit')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$all_groups = array( 0 => array ('none', '不要晉升') );
		
		if ($type == 'edit')
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("資料庫中沒有可選擇的群組 id，請重試.");
			}
			
			if ( $INFO['admin_group'] == $IN['id'] )
			{
				if ( $MEMBER['mgroup'] != $INFO['admin_group'] )
				{
					$ADMIN->error("抱歉，您不能編輯最高管理群組。");
				}
			}
			
			$form_code = 'doedit';
			$button    = '完成編輯';
				
		}
		else
		{
			$form_code = 'doadd';
			$button    = '增加群組';
		}
		
		if ($IN['id'] != "")
		{
			$DB->query("SELECT * FROM ibf_groups WHERE g_id='".$IN['id']."'");
			$group = $DB->fetch_row();
			
			$query = "SELECT g_id, g_title FROM ibf_groups WHERE g_id <> {$IN['id']} ORDER BY g_title";
		}
		else
		{
			$group = array();
			
			$query = "SELECT g_id, g_title FROM ibf_groups ORDER BY g_title";
		}
		
		//-------------------------------------------
		// sort out the promotion stuff
		//-------------------------------------------
		
		list($group['g_promotion_id'], $group['g_promotion_posts']) = explode( '&', $group['g_promotion'] );
		
		if ($group['g_promotion_posts'] < 1)
		{
			$group['g_promotion_posts'] = '';
		}
		
		$DB->query($query);
		
		while ( $r = $DB->fetch_row() )
		{
			if ( $r['g_id'] == $INFO['admin_group'] )
			{
				continue;
			}
			
			$all_groups[] = array( $r['g_id'], $r['g_title'] );
		}
		
		//-------------------------------------------
		
		$perm_masks = array();
		
		$DB->query("SELECT * FROM ibf_forum_perms");
		
		while ( $r = $DB->fetch_row() )
		{
			$perm_masks[] = array( $r['perm_id'], $r['perm_name'] );
		}
		
		//-------------------------------------------
		
		if ($type == 'edit')
		{
			$ADMIN->page_title = "編輯使用者群組 ".$group['g_title'];
		}
		else
		{
			$ADMIN->page_title = '新增一個使用者群組';
			$group['g_title'] = '新群組';
		}
		
		$guest_legend = "";
		
		if ($group['g_id'] == $INFO['guest_group'])
		{
			$guest_legend = "</b><br><i>(不適用於訪客)</i>";
		}
		
		$ADMIN->page_detail = "請在送出表單之前仔細檢查資料";
		
		
		//+-------------------------------
		
		$ADMIN->html .= "<script language='javascript'>
						 <!--
						  function checkform() {
						  
						  	isAdmin = document.forms[0].g_access_cp;
						  	isMod   = document.forms[0].g_is_supmod;
						  	
						  	msg = '';
						  	
						  	if (isAdmin[0].checked == true)
						  	{
						  		msg += '在這個群組的會員可以進入管理控制台\\n\\n';
						  	}
						  	
						  	if (isMod[0].checked == true)
						  	{
						  		msg += '在這個群組的會員是超級板主\\n\\n';
						  	}
						  	
						  	if (msg != '')
						  	{
						  		msg = '安全檢查\\n--------------\\n會員群組名稱： ' + document.forms[0].g_title.value + '\\n--------------\\n\\n' + msg + '正確嗎？';
						  		
						  		formCheck = confirm(msg);
						  		
						  		if (formCheck == true)
						  		{
						  			return true;
						  		}
						  		else
						  		{
						  			return false;
						  		}
						  	}
						  }
						 //-->
						 </script>\n";
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $form_code  ),
												  2 => array( 'act'   , 'group'     ),
												  3 => array( 'id'    , $IN['id']   ),
									     ) , 'adform', "onSubmit='return checkform()'" );
									     
		
		list($p_max, $p_width, $p_height) = explode( ":", $group['g_photo_max_vars'] );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$prefix = preg_replace( "/'/", "&#39;", $group['prefix'] );
		$prefix = preg_replace( "/</", "&lt;" , $prefix          );
		$suffix = preg_replace( "/'/", "&#39;", $group['suffix'] );
		$suffix = preg_replace( "/</", "&lt;" , $suffix          );
		
		$ADMIN->html .= $SKIN->start_table( "整體設定", "基本群組設定" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>群組名稱</b>" ,
												  $SKIN->form_input("g_title", $group['g_title'] )
									     )      );
									     
		//+-------------------------------
		// Sort out default array
		//+-------------------------------
		
		$ADMIN->html .=
		"<script type='text/javascript'>
			
			var show   = '';
		";
		
		foreach ($perm_masks as $id => $d)
		{
			$ADMIN->html .= " 		perms_$d[0] = '$d[1]';\n";
		}
		
		$ADMIN->html .=
		"	
			var show = '';
			
		 	function saveit(f)
		 	{
		 		show = '';
		 		
		 		for (var i = 0 ; i < f.options.length; i++)
				{
					if (f.options[i].selected)
					{
						tid  = f.options[i].value;
						show += '\\n' + eval('perms_'+tid);
					}
				}
			}
			
			function show_me()
			{
				if (show == '')
				{
					show = '未偵測到改變\\n點按多重選擇方塊啟動';
				}
				
				alert('已選擇的權限遮蔽\\n---------------------------------\\n' + show);
			}
			
		</script>";
		
		$arr = explode( ",", $group['g_perm_id'] );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>使用討論區權限存取...</b><br>您可以選擇一個以上" ,
												  $SKIN->form_multiselect("permid[]", $perm_masks, $arr, 5, 'onfocus="saveit(this)"; onchange="saveit(this)";' )."<a href='javascript:show_me();'>顯示所選的遮蔽</a>"
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>群組圖示</b><br>(可以省略)" ,
												  $SKIN->form_input("g_icon", $group['g_icon'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>發表：上傳檔案大小上限 (KB)</b>".$SKIN->js_help_link('mg_upload')."<br>(要禁止上傳請留空)" ,
												  $SKIN->form_input("g_attach_max", $group['g_attach_max'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>個人照片：上傳檔案大小上限 (KB)</b><br>(要禁止上傳請留空)" ,
												  $SKIN->form_input("p_max", $p_max )."<br />"
												  ."最大寬度 (px): <input type='text' size='3' name='p_width' value='{$p_width}'> "
												  ."最大高度 (px): <input type='text' size='3' name='p_height' value='{$p_height}'>"
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>線上列表格式 [字首]</b><br>(可以留空)<br>(範例：&lt;span style='color:red'&gt;)" ,
												  $SKIN->form_input("prefix", $prefix )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>線上列表格式 [字尾]</b><br>(可以留空)<br>(範例：&lt;/span&gt;)" ,
												  $SKIN->form_input("suffix", $suffix )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>從會員列表中隱藏這個群組？</b>" ,
												  $SKIN->form_yes_no("g_hide_from_list", $group['g_hide_from_list'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$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_yes_no("g_view_board", $group['g_view_board'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以瀏覽關閉的論壇？</b>" ,
												  $SKIN->form_yes_no("g_access_offline", $group['g_access_offline'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以檢視會員個人資料及會員列表？</b>" ,
												  $SKIN->form_yes_no("g_mem_info", $group['g_mem_info'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以觀看其他會員的主題？</b>" ,
												  $SKIN->form_yes_no("g_other_topics", $group['g_other_topics'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以使用搜尋？</b>" ,
												  $SKIN->form_yes_no("g_use_search", $group['g_use_search'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜尋灌水控制秒數</b><br>停止濫用搜尋，輸入 0 或留空不做灌水控制" ,
												  $SKIN->form_input("g_search_flood", $group['g_search_flood'] )
									     )      );							     
									     
		list( $limit, $flood ) = explode( ":", $group['g_email_limit'] );					     
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可從論壇寄信給其他會員？</b><br />將欄位留空會取消限制 $guest_legend</b>" ,
												  $SKIN->form_yes_no("g_email_friend", $group['g_email_friend'] )
												 ."<br />僅允許 ". $SKIN->form_simple_input("join_limit", $limit, 2 )." 封電子郵件於 24 小時之內"
												 ."<br />...且每封電子郵件須間隔 ".$SKIN->form_simple_input("join_flood", $flood, 2 )." 分鐘"
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以編輯自己的個人資料？$guest_legend" ,
												  $SKIN->form_yes_no("g_edit_profile", $group['g_edit_profile'] )
									     )      );							     
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以使用 PM 系統？$guest_legend" ,
												  $SKIN->form_yes_no("g_use_pm", $group['g_use_pm'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>允許大量 PM 的人數上限?$guest_legend<br>(輸入 0 或留空禁止大量 PM)" ,
												  $SKIN->form_input("g_max_mass_pm", $group['g_max_mass_pm'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>最多可儲存的訊息數？$guest_legend" ,
												  $SKIN->form_input("g_max_messages", $group['g_max_messages'] )
									     )      );
									     						     							     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以上傳個人圖片？$guest_legend" ,
												  $SKIN->form_yes_no("g_avatar_upload", $group['g_avatar_upload'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$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_yes_no("g_post_new_topics", $group['g_post_new_topics'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以回覆自己的主題？</b>" ,
												  $SKIN->form_yes_no("g_reply_own_topics", $group['g_reply_own_topics'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以回覆其他會員的主題 (被允許的地方)？</b>" ,
												  $SKIN->form_yes_no("g_reply_other_topics", $group['g_reply_other_topics'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以編輯自己的文章？$guest_legend" ,
												  $SKIN->form_yes_no("g_edit_posts", $group['g_edit_posts'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>編輯時間限制 (分鐘)？$guest_legend<br>設定的時效過後拒絕使用者編輯，留空或輸入 0 不做限制。" ,
												  $SKIN->form_input("g_edit_cutoff", $group['g_edit_cutoff'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>允許使用者移除 '編輯於' 註腳？$guest_legend</b>" ,
												  $SKIN->form_yes_no("g_append_edit", $group['g_append_edit'] )
									     )      );							     
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以刪除自己的文章？$guest_legend" ,
												  $SKIN->form_yes_no("g_delete_own_posts", $group['g_delete_own_posts'] )
									     )      );
									     						     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以開放/關閉自己的主題？$guest_legend" ,
												  $SKIN->form_yes_no("g_open_close_posts", $group['g_open_close_posts'] )
									     )      );							     
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以編輯自己主題的標題 & 描述？$guest_legend" ,
												  $SKIN->form_yes_no("g_edit_topic", $group['g_edit_topic'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以刪除自己的主題？$guest_legend" ,
												  $SKIN->form_yes_no("g_delete_own_topics", $group['g_delete_own_topics'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以發起新投票 (被允許的地方)？$guest_legend</b>" ,
												  $SKIN->form_yes_no("g_post_polls", $group['g_post_polls'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以參與投票 (被允許的地方)？$guest_legend" ,
												  $SKIN->form_yes_no("g_vote_polls", $group['g_vote_polls'] )
									     )      );							     
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以免除灌水控制？</b>" ,
												  $SKIN->form_yes_no("g_avoid_flood", $group['g_avoid_flood'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以免除管制佇列？</b>" ,
												  $SKIN->form_yes_no("g_avoid_q", $group['g_avoid_q'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以加入事件到日曆？$guest_legend</b>" ,
												  $SKIN->form_yes_no("g_calendar_post", $group['g_calendar_post'] )
									     )      );
									     					     							     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以使用 [doHTML] 標籤？$guest_legend</b><br />".$SKIN->js_help_link('mg_dohtml') ,
												  $SKIN->form_yes_no("g_dohtml", $group['g_dohtml'] )
									     )      );
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$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>是否為超級板主 (可以管理任何地方)？$guest_legend" ,
												  $SKIN->form_yes_no("g_is_supmod", $group['g_is_supmod'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>可以進入管理控制台？$guest_legend" ,
												  $SKIN->form_yes_no("g_access_cp", $group['g_access_cp'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>允許群組在 '關閉' 的主題中發表？" ,
												  $SKIN->form_yes_no("g_post_closed", $group['g_post_closed'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "群組晉級" );
		
		if ($group['g_id'] == $INFO['admin_group'])
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>選擇 '不要晉升' 以取消晉級</b><br>".$SKIN->js_help_link('mg_promote') ,
													  "對於最高管理群組此功能無效，畢竟 - 如果您已經在層峰還能晉升到哪裡去？"
											 )      );
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>選擇 '不要晉升' 以取消晉級</b>$guest_legend<br>".$SKIN->js_help_link('mg_promote') ,
													  '晉升這個群組的會員到： '.$SKIN->form_dropdown("g_promotion_id", $all_groups, $group['g_promotion_id'] )
													 .'<br>當他們達到 '.$SKIN->form_simple_input('g_promotion_posts', $group['g_promotion_posts'] ).' 篇文章'
											 )      );
		}
		
		
		$ADMIN->html .= $SKIN->end_form($button);
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
			
			
	}

	//+---------------------------------------------------------------------------------
	//
	// Show "Management Screen
	//
	//+---------------------------------------------------------------------------------
	
	function main_screen()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "使用者群組";
		
		$ADMIN->page_detail = "使用者編組是一種快速、強大的組織會員方式，有 4 個預設的群組您是不能移除的 (待認證、訪客、會員及管理員)，但是您可以任意的編輯它們。舉個使用者編組的好例子是設置一個叫做 '板主' 的群組，允許他們存取某些其他群組無法進入的討論區。<br>討論區權限可讓您快速的變更群組的討論區讀取、寫入及回覆設定，您可以在討論區控制裡面對每一個討論區進行設定。";
		
		$g_array = array();
		
		$SKIN->td_header[] = array( "群組名稱"    , "30%" );
		$SKIN->td_header[] = array( "存取 ACP？"    , "15%" );
		$SKIN->td_header[] = array( "超級板主？"     , "15%" );
		$SKIN->td_header[] = array( "會員"        , "10%" );
		$SKIN->td_header[] = array( "編輯群組"     , "20%" );
		$SKIN->td_header[] = array( "刪除"         , "10%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "使用者群組管理" );
		
		$DB->query("SELECT ibf_groups.g_id, ibf_groups.g_access_cp, ibf_groups.g_is_supmod, ibf_groups.g_title,ibf_groups.prefix, ibf_groups.suffix, COUNT(ibf_members.uid) as count FROM ibf_groups "
		          ."LEFT JOIN ibf_members ON (ibf_members.mgroup = ibf_groups.g_id) "
		          ."GROUP BY ibf_groups.g_id ORDER BY ibf_groups.g_title");
		
		while ( $r = $DB->fetch_row() )
		{
		
			$del  = "";
			$mod  = '&nbsp;';
			$adm  = '&nbsp;';
			
			if ($r['g_id'] > 4)
			{
				$del = "<center><a href='{$ADMIN->base_url}&act=group&code=delete&id=".$r['g_id']."'>刪除</a></center>";
			}
			//-----------------------------------
			if ($r['g_access_cp'] == 1)
			{
				$adm = '<center><span style="color:red">是</span></center>';
			}
			//-----------------------------------
			if ($r['g_is_supmod'] == 1)
			{
				$mod = '<center><span style="color:red">是</span></center>';
			}
			
			if ($r['g_id'] != 1 and $r['g_id'] != 2)
			{
				$total_linkage = "<a href='{$INFO['board_url']}/index.{$INFO['php_ext']}?act=Members&max_results=30&filter={$r['g_id']}&sort_order=asc&sort_key=name&st=0' target='_blank' title='List Users'>".$r['prefix'].$r['g_title'].$r['suffix']."</a>";
			}
			else
			{
				$total_linkage = $r['prefix'].$r['g_title'].$r['suffix'];
			}
			
			if ( $INFO['admin_group'] == $r['g_id'] )
			{
				$is_root = " ( ROOT )";
			}
			else
			{
				$is_root = "";
			}
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>$total_linkage</b> $is_root" ,
												      $adm,
												      $mod,
												      "<center>".$r['count']."</center>",
												      "<center><a href='{$ADMIN->base_url}&act=group&code=edit&id=".$r['g_id']."'>編輯群組</a></center>",
												      $del
												      
									     )      );
									     
			$g_array[] = array( $r['g_id'], $r['g_title'] );
		}
		
		$ADMIN->html .= $SKIN->add_td_basic("&nbsp;", "center", "title");

		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'add' ),
												  2 => array( 'act'   , 'group'     ),
									     )      );
									     
		//+-------------------------------
		
		$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", $g_array, 3 )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("設定新群組");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
		
	}
	
		
}


?>