<?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
+---------------------------------------------------------------------------
|
|   > Admin Forum functions
|   > Module written by Matt Mecham
|   > Date started: 1st march 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/


$idx = new ad_forums();

$root_path = "";

class ad_forums {

	var $base_url;
	var $modules = "";

	function ad_forums()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $ibforums;
		
		//--------------------------------------------
    	// Get the sync module
		//--------------------------------------------
		
		if ( USE_MODULES == 1 )
		{
			require ROOT_PATH."modules/ipb_member_sync.php";
			
			$this->modules = new ipb_member_sync();
		}
		
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}
		
		$ADMIN->nav[] = array( 'act=mem&code=edit', '編輯會員搜尋表單' );
		
		//---------------------------------------

		switch($IN['code'])
		{
			case 'stepone':
				$this->do_advanced_search(1);
				break;
			case 'doform':
				$this->do_edit_form();
				break;
			case 'doedit':
				$this->do_edit();
				break;
			case 'advancedsearch':
				$this->do_advanced_search();
				break;
			//---------------------
			case 'unsuspend':
				$this->unsuspend();
				break;
			//---------------------
			case 'add':
				$this->add_form();
				break;
			case 'doadd':
				$this->do_add();
				break;
			//---------------------
			case 'del':
				$this->delete_form();
				break;
			case 'delete2':
				$this->delete_lookup_form();
				break;
			case 'dodelete':
				$this->dodelete();
				break;
			case 'prune':
				$this->prune_confirm();
				break;
			case 'doprune':
				$this->doprune();
				break;
			//---------------------
			case 'title':
				$this->titles();
				break;
			case 'rank_edit':
				$this->rank_setup('edit');
				break;
			case 'rank_add':
				$this->rank_setup('add');
				break;
			case 'do_add_rank':
				$this->add_rank();
				break;
			case 'do_rank_edit':
				$this->edit_rank();
				break;
			case 'rank_delete':
				$this->delete_rank();
				break;
			//---------------------
			case 'ban':
				$this->ban_control();
				break;
			case 'doban':
				$this->update_ban();
				break;
			//---------------------
			case 'mod':
				$this->view_mod();
				break;
			case 'domod':
				$this->domod();
				break;
			//---------------------
			case 'changename':
				$this->change_name_start();
				break;
			case 'dochangename':
				$this->change_name_complete();
				break;
			//---------------------	
			case 'mail':
				$this->bulk_mail_form();
				break;
			case 'domail':
				$this->do_bulk_mail();
				break;
				
			case 'banmember':
				$this->temp_ban_start();
				break;
				
			case 'dobanmember':
				$this->temp_ban_complete();
				break;
			//---------------------
			default:
				$this->search_form();
				break;
		}
		
	}
	
	//+---------------------------------------------------------------------------------
	//
	// MASS EMAIL PEOPLE!
	//
	//+---------------------------------------------------------------------------------
	
	function do_bulk_mail() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		// Get the ID's of the groups we're emailing.
		
		$ids = array();
 		
 		foreach ($IN as $key => $value)
 		{
 			if ( preg_match( "/^sg_(\d+)$/", $key, $match ) )
 			{
 				if ($IN[$match[0]])
 				{
 					$ids[] = $match[1];
 				}
 			}
 		}
 		
 		if ( count($ids) < 1 )
 		{
 			$this->bulk_mail_form(1, '錯誤', '您至少必須選擇一個群組以寄發郵件');
 			exit();
 		}
 		
 		if ($IN['title'] == "")
 		{
 			$this->bulk_mail_form(1, '錯誤', '您必須為此郵件訂定一個標題');
 			exit();
 		}
 		
 		if ($IN['email_contents'] == "")
 		{
 			$this->bulk_mail_form(1, '錯誤', '電子郵件主體必須含有文字內容');
 			exit();
 		}
 		
 		$group_str = implode( ",", $ids);
 		
 		// Sort out the rest of the DB stuff
 		
 		$where = ""; // Where? who knows? who cares?
 		
 		if ($IN['posts'] > 0)
 		{
 			$where .= " AND posts < ".$IN['posts'];
 		}
 		
 		if ($IN['days'] > 0)
 		{
 			$time = time() - ($IN['days']*60*60*24);
 			$where .= " AND last_activity < '$time'";
 		}
 		
 		if ($IN["honour_user_setting"] == 1)
 		{
 			$where .= "AND allow_admin_mails=1";
 		}
 		
 		//+---------------------------------------
 		// Get a grip, er count
 		//+---------------------------------------
 		
 		$DB->query("SELECT COUNT(uid) as total FROM ibf_members WHERE mgroup IN($group_str)".$where);
 		
 		$rows = $DB->fetch_row();
 		
 		if ($rows['total'] < 1)
 		{
 			$this->bulk_mail_form(1, '錯誤', '請放寬您的標準，因為所提供的資訊無法找到要寄發電子郵件的會員。');
 			exit();
 		}
 		
 		//+---------------------------------------
 		// Regex up stuff
		//+---------------------------------------
		
		$DB->query("SELECT * FROM ibf_stats");
		$stats = $DB->fetch_row();
		
		$contents = $std->txt_raw2form($HTTP_POST_VARS['email_contents']); /* 中文修正 */
		
		$contents = str_replace( "{board_name}" , str_replace( "&#39;", "'", $INFO['board_name'] ) , $contents );
		$contents = str_replace( "{board_url}"  , $INFO['board_url']."/index.".$INFO['php_ext'] , $contents );
		$contents = str_replace( "{reg_total}"  , $stats['MEM_COUNT'] , $contents );
		$contents = str_replace( "{total_posts}", $stats['TOTAL_TOPICS'] + $stats['TOTAL_REPLIES'] , $contents );
		$contents = str_replace( "{busy_count}" , $stats['MOST_COUNT'] , $contents );
		$contents = str_replace( "{busy_time}"  , $std->get_date( $stats['MOST_DATE'], 'SHORT' ), $contents );

 		
 		//+---------------------------------------
 		// Are we previewing? Why am I asking you?
		//+---------------------------------------
		
		if ($IN['preview'] != "")
		{
			$this->bulk_mail_form(1, '預覽',
									 "<b>".$std->txt_raw2form($HTTP_POST_VARS['title'])."</b><br><br>"
									 .$contents."<br><br><b>要寄發的會員數：</b> ".$rows['total']
								 ); /* 中文修正 */
 			exit();
 		}
 		
 		//+---------------------------------------
 		// We're still here? GROOVY, send da mail
		//+---------------------------------------
		
		@set_time_limit(1200);
		
		require "./sources/lib/emailer.php";
		
		$this->email = new emailer();
		
		$this->email->bcc = array();
		
		$DB->query("SELECT email FROM ibf_members WHERE mgroup IN($group_str)".$where);
		
		while ( $r = $DB->fetch_row() )
		{
			if ($r['email'] != "")
			{
				$this->email->bcc[] = $r['email'];
			}
		}
		
		$this->email->message = str_replace( "\r\n", "\n", $contents);
		$this->email->subject = $std->txt_stripslashes($DB->oopsAddSlashes($HTTP_POST_VARS['title'])); /* 中文修正 */
		
		if ($IN['email_admin'] == 1)
		{
			$this->email->to = $INFO['email_in'];
		}
		else
		{
			$this->email->to = "";
		}
		
		$this->email->send_mail();
		
		$ADMIN->save_log("群發電子郵件會員 ($where)");
		
		$ADMIN->done_screen("群發電子郵件已寄出", "會員控制", "act=mem" );
		
	}
	
	
	
	
	function bulk_mail_form($preview=0, $title='Preview', $content="") {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		
		$ADMIN->page_title = "群發會員電子郵件";
		
		$ADMIN->page_detail = "您可以配置下面的表單群發電子郵件給您的會員。按 '快速說明' 連結取得更多資訊";
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'domail'  ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );
									     
		if ($HTTP_POST_VARS['email_contents'] == "")
		{
			$email_contents = "\n\n\n-------------------------------------\n{board_name} 統計：\n"
								   ."-------------------------------------\n註冊使用者： {reg_total}\n文章總數： {total_posts}\n"
								   ."最繁忙時間： 於 {busy_time} 有 {busy_count} 位使用者同時在線上\n\n"
								   ."-------------------------------------\n便利連結\n"
								   ."-------------------------------------\n論壇位址： {board_url}\n登入： {board_url}?act=Login&CODE=00\n"
								   ."遺失密碼回復： {board_url}?act=Reg&CODE=10\n\n"
								   ."-------------------------------------\n如何取消訂閱 \n"
								   ."-------------------------------------\n訪問您的電子郵件設定 ({board_url}?act=UserCP&CODE=02) 並且確定"
								   ."剔選 '接收由論壇管理員寄出的任何消息' 方塊，然後送出表單";
		}
		
		if ($preview == 1)
		{
			$SKIN->td_header[] = array( "&nbsp;"  , "100%" );
			
			$ADMIN->html .= $SKIN->start_table( $title );
			
			$ADMIN->html .= $SKIN->add_td_row( array( nl2br($content) ) );
			
			$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><br>強烈推薦您遵守！" ,
												  $SKIN->form_yes_no( "honour_user_setting", isset($IN["honour_user_setting"]) ? $IN["honour_user_setting"] : 1 )
									     	)      );
		
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件標題</b>" ,
												  $SKIN->form_input( "title", $std->txt_raw2form($HTTP_POST_VARS['title']) )
									     )      ); /* 中文修正 */
		if ($HTTP_POST_VARS['email_contents'] == "")
		{							
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件內容</b><br>".$SKIN->js_help_link('m_bulkemail') ,
												  $SKIN->form_textarea( "email_contents", $email_contents, 60, 15 )
									     )      ); /* 中文修正 */
		}
        else
        {
        $ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件內容</b><br>".$SKIN->js_help_link('m_bulkemail') ,
												  $SKIN->form_textarea( "email_contents", $std->txt_raw2form($HTTP_POST_VARS['email_contents']), 60, 15 )
									     )      ); /* 中文修正 */
        }							
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>將此電子郵件傳送到管理員收信位址？</b><br>如果您在送出時發生郵件錯誤請啟用這個選項 - 使用 SMTP 時必須這樣做。" ,
												  $SKIN->form_yes_no( "email_admin", isset($IN['email_admin']) ? $IN['email_admin'] : 1 )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "群發會員電子郵件：設定" );
		
		$DB->query("SELECT g_id, g_title FROM ibf_groups WHERE g_id <> ".$INFO['guest_group']." ORDER BY g_title");
		
		while ( $r = $DB->fetch_row() )
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>寄給群組 <span style='color:red'>{$r['g_title']}</span>?</b>" ,
												  $SKIN->form_yes_no( "sg_{$r['g_id']}", isset($IN['sg_'.$r['g_id']]) ? $IN['sg_'.$r['g_id']] : 1 )
									     	)      );
		}
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>使用者發表數少於 [x] 篇</b><br>留空時寄發電子郵件不考慮文章數" ,
												  $SKIN->form_input( "post", $IN['post'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>使用者超過 [x] 天未曾上線</b><br>留空時寄發電子郵件不考慮最後訪問時間" ,
												  $SKIN->form_input( "days", $IN['days'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_basic( '<input type="submit" name="preview" id="button" value="預覽">', 'center' );
		
		$ADMIN->html .= $SKIN->end_form("處理");
		
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
	}
	
	//+---------------------------------------------------------------------------------
	//
	// TEMP BANNING
	//
	//+---------------------------------------------------------------------------------
	
	function temp_ban_start()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "帳號停權";
		
		$ADMIN->page_detail = "要自動停權會員，只需選擇停權期間並送出底下的表單即可。";
		
		$contents = "{membername},\n您在 {$INFO['board_name']} 的會員帳號已被暫時停權。\n\n您的帳號在 {date_end} 之前將沒有作用 (端賴您的時區而定)。這是自動處理的，對於復權的處理您不必做任何動作。\n\n論壇位址： {$INFO['board_url']}/index.php";
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個有效的會員 id，請返回重試。");
		}
		
		$DB->query("SELECT * FROM ibf_members WHERE uid='".$IN['mid']."'");
		
		if ( ! $member = $DB->fetch_row() )
		{
			$ADMIN->error("會員資料庫中無符合的 ID");
		}
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dobanmember'  ),
												  2 => array( 'act'   , 'mem'       ),
												  3 => array( 'mid'   , $IN['mid']  ),
									     )      );
		
		$ban = $std->hdl_ban_line( $member['temp_ban'] );
		
		$units = array( 0 => array( 'h', '小時' ), 1 => array( 'd', '天' ) );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "會員帳號停權", "注意：如果此會員已經被停權，任何新的設定將會使此禁令重新開始。" );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<strong>停權 {$member['uname']} ...</strong>" ,
												  $SKIN->form_input('timespan', $ban['timespan'], "text", "", '5' ) . '&nbsp;' . $SKIN->form_dropdown('units', $units, $ban['units'] ),
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b>以電子郵件通知此會員？</b><br>(如果要，您可以編輯下面的電子郵件)" ,
												  $SKIN->form_yes_no( "send_email", 0 )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件內容</b><br>(標籤： {membername} = 會員名稱， {date_end} = 禁令終止)" ,
												  $SKIN->form_textarea( "email_contents", $contents )
									     ), "", 'top'       );
									     									     
		$ADMIN->html .= $SKIN->end_form("停權此帳號");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//---------------------------------------------------------------
	
	function unsuspend()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個有效的會員 id，請返回重試。");
		}
		
		if ($IN['mid'] == 'all')
		{
			$DB->query("UPDATE ibf_members SET temp_ban=''");
			
			$ADMIN->save_log("將全部會員帳號復權");
		
			$ADMIN->done_screen("所有帳號已復權", "會員控制", "act=mem" );
		}
		else
		{
			$mid = intval($IN['mid']);
			
			$DB->query("UPDATE ibf_members SET temp_ban='' WHERE uid=$mid");
			
			$DB->query("SELECT uname FROM ibf_members WHERE uid=$mid");
			
			$member = $DB->fetch_row();
			$member['uname'] = $DB->oopsAddSlashes($member['uname']); /* 中文修正 */
			$ADMIN->save_log("已復權 {$member['uname']}");
		
			$ADMIN->done_screen("{$member['uname']} 已復權", "會員控制", "act=mem" );
		}	
			
		
	}
	
	//---------------------------------------------------------------
	
	function temp_ban_complete()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		$ADMIN->page_title = "帳號停權";
		
		$ADMIN->page_detail = "自動停權會員，確認及資訊";
		
		$IN['mid'] = intval($IN['mid']);
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個有效的會員 id，請返回重試。");
		}
		
		$DB->query("SELECT * FROM ibf_members WHERE uid='".$IN['mid']."'");
		
		if ( ! $member = $DB->fetch_row() )
		{
			$ADMIN->error("無法在會員資料庫中找到相符的 ID");
		}
		
		//+-------------------------------
		// Work out end date
		//+-------------------------------
		
		$IN['timespan'] = intval($IN['timespan']);
		
		if ( $IN['timespan'] == "" )
		{
			$new_ban = "";
		}
		else
		{
			$new_ban = $std->hdl_ban_line( array( 'timespan' => intval($IN['timespan']), 'unit' => $IN['units']  ) );
		}
		
		$show_ban = $std->hdl_ban_line( $new_ban );
			
		//+-------------------------------
		// Update and show confirmation
		//+-------------------------------
									     
		$DB->query("UPDATE ibf_members SET temp_ban='$new_ban' WHERE uid={$IN['mid']}");
		
		// I say, did we choose to email 'dis member?
		
		if ($IN['send_email'] == 1)
		{
			// By golly, we did!
			
			require "./sources/lib/emailer.php";
		
			$this->email = new emailer();
			
			$msg = trim($std->txt_stripslashes($HTTP_POST_VARS['email_contents']));
			
			$msg = str_replace( "{membername}", $DB->oopsAddSlashes($member['uname'])       , $msg ); /* 中文修正 */
			$msg = str_replace( "{date_end}"  , $ADMIN->get_date( $show_ban['date_end'], 'LONG') , $msg );
			
			$this->email->message = $DB->oopsAddSlashes($this->email->clean_message($msg)); /* 中文修正 */
			$this->email->subject = "帳號停權通知";
			$this->email->to      = $member['email'];
			$this->email->send_mail();
			
			$skin_extra = $SKIN->add_td_row( array( "<strong>已寄出電子郵件</strong>",
												    "<strong>帳號停權通知</strong><br /><br />".str_replace( "\n", "<br />", $DB->oopsAddSlashes($msg))
									    ), "", 'top'      ); /* 中文修正 */
		}
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "會員停權：結果"  );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<strong>{$member['uname']} 被停權至...</strong>",
												  $ADMIN->get_date( $show_ban['date_end'], 'LONG'),
									    )      );
									    
		$ADMIN->html .= $skin_extra;
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->nav[] = array( "act=mem&code=stepone&USER_NAME={$member['uname']}", "對 {$member['uname']} 的管理選項" );
		$member['uname'] = $DB->oopsAddSlashes($member['uname']); /* 中文修正 */
		$ADMIN->save_log("停權 {$member['uname']} 至 ".$ADMIN->get_date( $show_ban['date_end'], 'SHORT'));
		
		$ADMIN->output();
	}
	
		
	//+---------------------------------------------------------------------------------
	//
	// CHANGE MEMBER NAME
	//
	//+---------------------------------------------------------------------------------
	
	function change_name_complete()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
//		$IN['new_name'] = str_replace( '|', '&#124;', $IN['new_name'] );
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個有效的會員 id，請返回重試。");
		}
		
		if ($IN['new_name'] == "")
		{
			$this->change_name_start("您必須輸入此會員的新名稱");
			exit();
		}
		
		$DB->query("SELECT uname, email FROM ibf_members WHERE uid='".$IN['mid']."'");
		
		if ( ! $member = $DB->fetch_row() )
		{
			$ADMIN->error("無法在會員資料庫中找到相符的 ID");
		}
		
		$mid = $IN['mid']; // Save me poor ol' carpels
		
		if ($IN['new_name'] == $member['uname'])
		{
			$this->change_name_start("新名稱與舊名稱相同，這是不合邏輯的指令。");
			exit();
		}
		
		// Check to ensure that his member name hasn't already been taken.
		
		$new_name = trim($IN['new_name']);
		
		$DB->query("SELECT uid FROM ibf_members WHERE LOWER(uname)='".strtolower($new_name)."'");
		
		if ( $DB->get_num_rows() )
		{
			$this->change_name_start("名稱 '$new_name' 已經存在，請另選其他。");
			exit();
		}
		
		// If one gets here, one can assume that the new name is correct for one, er...one.
		// So, lets do the converteroo
		
		$DB->query("UPDATE ibf_members SET uname='$new_name' WHERE uid='$mid'");
		$DB->query("UPDATE ibf_contacts SET contact_name='$new_name' WHERE contact_id='$mid'");
		$DB->query("UPDATE ibf_forums SET last_poster_name='$new_name' WHERE last_poster_id='$mid'");
		$DB->query("UPDATE ibf_moderator_logs SET member_name='$new_name' WHERE member_id='$mid'");
		$DB->query("UPDATE ibf_moderators SET member_name='$new_name' WHERE member_id='$mid'");
		$DB->query("UPDATE ibf_posts SET author_name='$new_name' WHERE author_id='$mid'");
		$DB->query("UPDATE ibf_sessions SET member_name='$new_name' WHERE member_id='$mid'");
		$DB->query("UPDATE ibf_topics SET starter_name='$new_name' WHERE starter_id='$mid'");
		$DB->query("UPDATE ibf_topics SET last_poster_name='$new_name' WHERE last_poster_id='$mid'");
		
		// I say, did we choose to email 'dis member?
		
		if ($IN['send_email'] == 1)
		{
			// By golly, we did!
			
			require "./sources/lib/emailer.php";
		
			$this->email = new emailer();
			
			$msg = trim($HTTP_POST_VARS['email_contents']);
			
			$msg = str_replace( "{old_name}", $DB->oopsAddSlashes($member['uname']), $msg ); /* 中文修正 */
			$msg = str_replace( "{new_name}", $new_name      , $msg );
			
			$this->email->message = $DB->oopsAddSlashes($this->email->clean_message($msg)); /* 中文修正 */
			$this->email->subject = "會員名稱變更通知";
			$this->email->to      = $member['email'];
			$this->email->send_mail();
		}
		
		$DB->query("SELECT uid, uname FROM ibf_members WHERE mgroup <> '".$INFO['auth_group']."' ORDER BY uid DESC LIMIT 0,1");
		$r = $DB->fetch_row();
		$stats['LAST_MEM_NAME'] = $r['uname'];
		$stats['LAST_MEM_ID']   = $r['uid'];
		
		$db_string = $DB->compile_db_update_string( $stats );
		$DB->query("UPDATE ibf_stats SET $db_string");
		$member['uname'] = $DB->oopsAddSlashes($member['uname']); /* 中文修正 */
		$ADMIN->save_log("已變更會員名稱 '{$member['uname']}' 為 '$new_name'");
		
		if ( USE_MODULES == 1 )
		{
			$this->modules->register_class(&$this);
			$this->modules->on_name_change($mid, $new_name );
		}
		
		$ADMIN->done_screen("已變更會員名稱", "會員控制", "act=mem" );
	}
	
	
	
	//===========================================================================
	
	function change_name_start($message="") {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "變更會員名稱";
		
		$ADMIN->page_detail = "您可以替這位會員輸入新名稱";
		
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您必須指定一個有效的會員 id，請返回重試。");
		}
		
		$DB->query("SELECT uname FROM ibf_members WHERE uid='".$IN['mid']."'");
		
		if ( ! $member = $DB->fetch_row() )
		{
			$ADMIN->error("無法在會員資料庫找到相符的 ID");
		}
		
		$contents = "{old_name},\n有位管理員已經將您在 {$INFO['board_name']} 的會員名稱改變。\n\n您的新名稱為： {new_name}\n\n請牢記，下次您必須使用這個新名稱登入。\n論壇位址： {$INFO['board_url']}/index.php";
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dochangename'  ),
												  2 => array( 'act'   , 'mem'       ),
												  3 => array( 'mid'   , $IN['mid']  ),
									     )      );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "變更會員名稱" );
		
		if ($message != "")
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>錯誤訊息：</b>" ,
												  	  "<b><span style='color:red'>$message</span></b>",
									     	 )      );
		}
		
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>目前的會員名稱</b>" ,
												  $member['uname'],
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>新的會員名稱</b>" ,
												  $SKIN->form_input( "new_name", $IN['new_name'] )
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件通知此會員？</b><br>(如果要的話，您可以在下面編輯電子郵件)" ,
												  $SKIN->form_yes_no( "send_email", 1 )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件內容</b><br>(標籤： {old_name} = 目前名稱， {new_name} = 新名稱)" ,
												  $SKIN->form_textarea( "email_contents", $contents )
									     )      );
									     									     
		$ADMIN->html .= $SKIN->end_form("變更此會員名稱");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//+---------------------------------------------------------------------------------
	
	
	
	//+---------------------------------------------------------------------------------
	//
	// Moderation control...
	//
	//+---------------------------------------------------------------------------------
	
	function domod() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ids = array();
		
		foreach ($IN as $k => $v)
		{
			if ( preg_match( "/^mid_(\d+)$/", $k, $match ) )
			{
				if ($IN[ $match[0] ])
				{
					$ids[] = $match[1];
				}
			}
		}
		
		//-------------------
		
		if ( count($ids) < 1 )
		{	
			$ADMIN->error("您沒有選擇任何要批准或刪除的會員");
		}
		
		//-------------------
		
		if ($IN['type'] == 'approve')
		{
		
			//-------------------------------------------
			
			require ROOT_PATH."sources/lib/emailer.php";
			
			$email = new emailer();
			
			$email->get_template("complete_reg");
			
			$email->build_message( "" );
			
			$email->subject = "帳號啟用於 ".$INFO['board_name'];
			
			//-------------------------------------------
			
			$main = $DB->query("SELECT m.uid, m.email, m.mgroup, v.* FROM ibf_validating v
								 LEFT JOIN ibf_members m ON (v.member_id=m.uid)
							    WHERE m.uid IN(".implode( ",",$ids ).")");
			
			while( $row = $DB->fetch_row( $main ) )
			{
				if ($row['mgroup'] != $INFO['auth_group'])
				{
					continue;
				}
				
				if ($row['real_group'] == "")
				{
					$row['real_group'] = $INFO['member_group'];
				}
				
				$update = $DB->query("UPDATE ibf_members SET mgroup=".$row['real_group']." WHERE uid=".$row['uid']);
				
				$email->to = $row['email'];
				
				$email->send_mail();
			}
			
			$DB->query("DELETE FROM ibf_validating WHERE member_id IN(".implode( ",",$ids ).")");
			
			$DB->query("SELECT uid, uname FROM ibf_members WHERE mgroup <> ".$INFO['auth_group']." ORDER BY uid DESC LIMIT 0,1");
			$r = $DB->fetch_row();

			$DB->query("UPDATE ibf_stats SET MEM_COUNT=MEM_COUNT+".count($ids).", LAST_MEM_NAME='{$r['uname']}', LAST_MEM_ID='{$r['uid']}'");
			
			$ADMIN->save_log("核准等待註冊");
			
			$ADMIN->done_screen( count($ids)." 會員已核准", "管理註冊", "act=mem&code=mod" );	
			
		}
		else
		{
			$DB->query("DELETE FROM ibf_members WHERE uid IN(".implode( ",",$ids ).")");
			
			$DB->query("DELETE FROM ibf_member_extra WHERE id IN(".implode( ",",$ids ).")");
			
			// Delete member messages...
		
			$DB->query("DELETE from ibf_messages WHERE member_id IN(".implode( ",",$ids ).")");
			$DB->query("DELETE from ibf_contacts WHERE member_id IN(".implode( ",",$ids ).") or contact_id IN(".implode( ",",$ids ).")");
			
			$DB->query("DELETE FROM ibf_validating WHERE member_id IN(".implode( ",",$ids ).")");
			
			$DB->query("DELETE from ibf_pfields_content WHERE member_id IN(".implode( ",",$ids ).")");
			
			$DB->query("DELETE from ibf_warn_logs WHERE wlog_mid IN(".implode( ",",$ids ).")");
			
			// Convert their posts and topics into guest postings..
		
			$DB->query("UPDATE ibf_posts SET author_id='0' WHERE author_id IN(".implode( ",",$ids ).")");
		
			$DB->query("UPDATE ibf_topics SET starter_id='0' WHERE starter_id IN(".implode( ",",$ids ).")");
			
			if ( USE_MODULES == 1 )
			{
				$this->modules->register_class(&$this);
				$this->modules->on_delete($ids);
			}
			
			$ADMIN->save_log("駁回等待註冊");
			
			$ADMIN->done_screen( count($ids)." 會員已移除", "管理註冊", "act=mem&code=mod" );
		}
		
	}
	
	
	//---------------------------------------------
	
	function view_mod() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title  = "管理使用者註冊/電子郵件變更佇列";
		
		$ADMIN->page_detail = "您要求管理員在充分授與會員資格之前先行預覽的新帳號，可以在這裡允許或駁回註冊，也可以讓您完成或駁回新電子郵件位址的變更。<br><br>這個表單也能夠讓您對那些沒有收到電子郵件的人完成註冊。";
		
		$DB->query("SELECT COUNT(vid) as mcount FROM ibf_validating WHERE lost_pass <> 1");
		
		$row = $DB->fetch_row();
		
		$cnt = $row['mcount'] < 1 ? 0 : $row['mcount'];
		
		$st = intval($IN['st']);
		
		$ord = $IN['ord'] == 'asc' ? 'asc' : 'desc';
		
		$new_ord  = $ord  == 'asc' ? 'desc' : 'asc';
		
		switch ($IN['sort'])
		{
			case 'mem':
				$col = 'm.name';
				break;
			case 'email':
				$col = 'm.email';
				break;
			case 'sent':
				$col = 'v.entry_date';
				break;
			case 'posts':
				$col = 'm.posts';
				break;
			case 'reg':
				$col = 'm.joined';
				break;
			default:
				$col = 'v.entry_date';
				break;
		}
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'domod'  ),
												  2 => array( 'act'   , 'mem'    ),
									     )      );
									     
		$SKIN->td_header[] = array( "<a href='{$SKIN->base_url}&act=mem&code=mod&st=$st&sort=mem&ord=$new_ord'>會員名稱</a>"       , "20%" );
		$SKIN->td_header[] = array( "位置？"            , "20%" );
		$SKIN->td_header[] = array( "<a href='{$SKIN->base_url}&act=mem&code=mod&st=$st&sort=email&ord=$new_ord'>電子郵件位址</a>"     , "15%" );
		$SKIN->td_header[] = array( "<a href='{$SKIN->base_url}&act=mem&code=mod&st=$st&sort=sent&ord=$new_ord'>已寄出電子郵件</a>"        , "10%" );
		$SKIN->td_header[] = array( "<a href='{$SKIN->base_url}&act=mem&code=mod&st=$st&sort=posts&ord=$new_ord'>文章數</a>"             , "10%" );
		$SKIN->td_header[] = array( "<a href='{$SKIN->base_url}&act=mem&code=mod&st=$st&sort=reg&ord=$new_ord'>註冊日期</a>"           , "10%" );
		$SKIN->td_header[] = array( "年齡" , "10%" );
		$SKIN->td_header[] = array( "&nbsp;"            , "5%" );
		
		$ADMIN->html .= $SKIN->start_table( "等待批准的使用者" );
		
		$links = $std->build_pagelinks( array( 'TOTAL_POSS'  => $cnt,
											   'PER_PAGE'    => 75,
											   'CUR_ST_VAL'  => $st,
											   'L_SINGLE'    => "單頁",
											   'L_MULTI'     => "多頁",
											   'BASE_URL'    => $SKIN->base_url."&act=mem&code=mod",
									  )      );
		
		$ADMIN->html .= $SKIN->add_td_basic( "<b>$cnt 位使用者需要註冊或電子郵件變更認證</b>", "center", "catrow2");
		
		if ($cnt > 0)
		{
			$DB->query("SELECT m.uname, m.uid, m.email, m.posts, m.user_regdate, v.*
					      FROM ibf_validating v
					    LEFT JOIN ibf_members m ON (v.member_id=m.uid)
					    WHERE v.lost_pass <> 1
					    ORDER BY $col $ord LIMIT $st,75");
			
			while ( $r = $DB->fetch_row() )
			{
			
				if ($r['coppa_user'] == 1)
				{
					$coppa = ' ( COPPA 請求 )';
				}
				else
				{
					$coppa = "";
				}
				
				$where = ( $r['lost_pass'] ? '遺失密碼' : ( $r['new_reg'] ? "註冊" : ( $r['email_chg'] ? "電子郵件變更" : 'N/A' ) ) );
				
				//$age = floor( ( time() - $r['entry_date'] ) / 86400 );
				
				$hours  = floor( ( time() - $r['entry_date'] ) / 3600 );
				
				$days   = intval( $hours / 24 );
				
				$rhours = intval( $hours - ($days * 24) );
				
				if ( $r['uname'] == "" )
				{
					$r['uname'] = "<em>刪除的會員</em>";
				}
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$r['uname']."</b>$coppa" ,
				 										  "<center>$where</center>",
												  		  $r['email'],
												  		  "<center>".$std->get_date( $r['entry_date'], 'JOINED' )."</center>",
												  		  "<center>{$r['posts']}</center>",
												  		  "<center>".$std->get_date( $r['user_regdate'], 'JOINED' )."</center>",
												  		  "<center><strong><span style='color:red'>$days d</span>, $rhours h</center>",
												  		  "<center><input type='checkbox' name='mid_{$r['member_id']}' value='1'></center>"
											 )      );
			}
			$ADMIN->html .= $SKIN->add_td_basic( "$links", "left", "catrow2");
	
			$ADMIN->html .= $SKIN->add_td_basic("<select name='type' id='dropdown'><option value='approve'>批准這些帳號</option><option value='delete'>刪除這些帳號</option></select>", "center", "catrow2" );
			
		}
		
		$ADMIN->html .= $SKIN->end_form("Go!");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
	}
	
	
	
	//+---------------------------------------------------------------------------------
	//
	// Ban control...
	//
	//+---------------------------------------------------------------------------------
	
	function ban_control() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "封鎖控制";
		
		$ADMIN->page_detail = "您可以在這裡修改、刪除或加入 IP 位址、電子郵件位址及預留的名稱到封鎖過濾中";
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doban'  ),
												  2 => array( 'act'   , 'mem'       ),
									     )      );
									     
		$ip_list    = "";
		$name_list  = "";
		$email_list = "";
		
		if ($INFO['ban_ip'] != "")
		{
			$ip_list = preg_replace( "/\|/", "\n", $INFO['ban_ip'] );
		}
		
		//+-------------------------------
		
		if ($INFO['ban_email'] != "")
		{
			$email_list = preg_replace( "/\|/", "\n", $INFO['ban_email'] );
		}
		
		//+-------------------------------
		
		if ($INFO['ban_names'] != "")
		{
			$name_list = preg_replace( "/\|/", "\n", $INFO['ban_names'] );
		}

		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"     , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"     , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "封鎖控制" );
		
		$ADMIN->html .= $SKIN->add_td_basic("封鎖的 IP 位址 (每行一個 - 使用 * 當作萬用字元)", "center", "pformstrip");
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>封鎖的 IP 位址</b><br>(範例： 212.45.45.23)<br>(範例： 212.45.45.*)" ,
												  $SKIN->form_textarea( 'ban_ip', $ip_list )
											 )      );
											 
		$ADMIN->html .= $SKIN->add_td_basic("封鎖的電子郵件位址 (每行一個 - 使用 * 當作萬用字元)", "center", "pformstrip");
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>封鎖的電子郵件位址</b><br>(範例： name@domain.com)<br>(範例： *@domain.com)" ,
												  $SKIN->form_textarea( 'ban_email', $email_list )
											 )      );
											 
		$ADMIN->html .= $SKIN->add_td_basic("封鎖/預留的名稱 (每行一個)", "center", "pformstrip");
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>封鎖的名稱</b>" ,
												  $SKIN->form_textarea( 'ban_names', $name_list )
											 )      );
											 
		$ADMIN->html .= $SKIN->end_form("更新封鎖過濾");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();

	}
	
	function update_ban()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		// Get the incoming..
		
		$new = array();
		
		$new['ban_ip']    = $this->_do_banline($HTTP_POST_VARS['ban_ip']);
		$new['ban_email'] = $this->_do_banline($HTTP_POST_VARS['ban_email']);
		$new['ban_names'] = $this->_do_banline($HTTP_POST_VARS['ban_names']);
		
		$ADMIN->rebuild_config( $new );
		
		$ADMIN->save_log("更新封鎖過濾");
		
		$ADMIN->done_screen("已更新封鎖過濾", "封鎖控制", "act=mem&code=ban" );		
		
	}
		
	function _do_banline($raw)
	{
		global $std;
		
		$ban = trim($std->txt_stripslashes($raw));
		
		$ban = str_replace('|', "&#124;", $ban);
		
		$ban = preg_replace( "/\n/", '|', str_replace( "\n\n", "\n", str_replace( "\r", "\n", $ban ) ) );
		
		$ban = preg_replace( "/\|{1,}\s{1,}?/s", "|", $ban );
		
		$ban = preg_replace( "/^\|/", "", $ban );
		
		$ban = preg_replace( "/\|$/", "", $ban );
		
		$ban = str_replace( "'", '&#39;', $ban );
		
		return $ban;
	}
		
		
	//+---------------------------------------------------------------------------------
	//
	// MEMBER RANKS...
	//
	//+---------------------------------------------------------------------------------
	
	function titles() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "會員等級設定";
		
		$ADMIN->page_detail = "您可以在這裡修改、刪除或增加額外的等級。<br>如果您要在會員名稱底下顯示格點，請輸入格點數；如果您要使用自訂的圖片，只要在格點欄輸入圖片名稱即可。注意，這些自訂圖片必須放在您安裝的 'html/team_icons' 目錄內。";
		
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "頭銜"      , "30%" );
		$SKIN->td_header[] = array( "文章數"  , "10%" );
		$SKIN->td_header[] = array( "格點"       , "20%" );
		$SKIN->td_header[] = array( "&nbsp;"     , "20%" );
		$SKIN->td_header[] = array( "&nbsp;"     , "20%" );
		
		//+-------------------------------
		
		$DB->query("SELECT macro_id, img_dir FROM ibf_skins WHERE default_set=1");
		
		$mid = $DB->fetch_row();
		
		$DB->query("SELECT macro_replace AS A_STAR FROM ibf_macro WHERE macro_set={$mid['macro_id']} AND macro_value='A_STAR'");
    	           
    	$row = $DB->fetch_row();
    	
    	$row['A_STAR'] = str_replace( "<#IMG_DIR#>", $mid['img_dir'], $row['A_STAR'] );
		
		$ADMIN->html .= $SKIN->start_table( "會員頭銜/等級" );
		
		$DB->query("SELECT * FROM ibf_titles ORDER BY posts");
		
		while ( $r = $DB->fetch_row() )
		{
			$img = "";
			
			if ( preg_match( "/^\d+$/", $r['pips'] ) )
			{
				for ($i = 1; $i <= $r['pips']; $i++)
				{
					$img .= $row['A_STAR'];
					
				}
			}
			else
			{
				$img = "<img src='html/team_icons/{$r['pips']}' border='0'>";
			}
				
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$r['title']."</b>" ,
													  $r['posts'],
													  $img,
													  "<a href='{$SKIN->base_url}&act=mem&code=rank_edit&id={$r['id']}'>編輯</a>",
													  "<a href='{$SKIN->base_url}&act=mem&code=rank_delete&id={$r['id']}'>刪除</a>",
											 )      );
			
		}
									     
		
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'do_add_rank'  ),
												  2 => array( 'act'   , 'mem'       ),
									     )      );
		
		//+-------------------------------
		
		$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_input( "title" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>最少所需發表數</b>" ,
												  $SKIN->form_input( "posts" )
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b>格點數量</b><br>(或者格點圖片)" ,
												  $SKIN->form_input( "pips" )
									     )      );
									     									     
		$ADMIN->html .= $SKIN->end_form("增加這個等級");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//+---------------------------------------------------------------------------------
	
	function add_rank() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------
		// check for input
		//+-------------------------------
		
		foreach( array( 'posts', 'title', 'pips' ) as $field )
		{
			if ($IN[ $field ] == "")
			{
				$ADMIN->error("您必須完成整個表單");
			}
		}
		
		//+-------------------------------
		// Add it to the DB
		//+-------------------------------
		
		$db_string = $DB->compile_db_insert_string( array (
															 'posts'  => trim($IN['posts']),
															 'title'  => trim($IN['title']),
															 'pips'   => trim($IN['pips']),
												  )       );
												  
		$DB->query("INSERT INTO ibf_titles (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
		
		$ADMIN->done_screen("Rank Added", "會員等級控制", "act=mem&code=title" );					
		
		
	}
	
	//+---------------------------------------------------------------------------------
	
	function delete_rank() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------
		// check for input
		//+-------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無符合的 ID");
		}
		
		$DB->query("DELETE FROM ibf_titles WHERE id='".$IN['id']."'");
		
		$ADMIN->save_log("移除等級設定");
		
		$ADMIN->done_screen("等級已移除", "會員等級控制", "act=mem&code=title" );
		
	}
	
	//+---------------------------------------------------------------------------------
	
	function edit_rank() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//+-------------------------------
		// check for input
		//+-------------------------------
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無符合的 ID");
		}
		
		//+-------------------------------
		
		foreach( array( 'posts', 'title', 'pips' ) as $field )
		{
			if ($IN[ $field ] == "")
			{
				$ADMIN->error("您必須完成整個表單");
			}
		}
		
		//+-------------------------------
		// Add it to the DB
		//+-------------------------------
		
		$db_string = $DB->compile_db_update_string( array (
															 'posts'  => trim($IN['posts']),
															 'title'  => trim($IN['title']),
															 'pips'   => trim($IN['pips']),
												  )       );
												  
		$DB->query("UPDATE ibf_titles SET $db_string WHERE id='".$IN['id']."'");
		
		$ADMIN->save_log("編輯等級設定");
		
		$ADMIN->done_screen("等級已編輯", "會員等級控制", "act=mem&code=title" );					
		
		
	}
	
	//+---------------------------------------------------------------------------------
	
	function rank_setup($mode='edit') {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "會員等級設定";
		
		$ADMIN->page_detail = "如果您要在會員名稱底下顯示格點，請輸入格點數；如果您要使用自訂的圖片，只要在格點欄輸入圖片名稱即可。注意，這些自訂圖片必須放在您安裝的 'html/team_icons' 目錄內。";
		
		if ($mode == 'edit')
		{
			$form_code = 'do_rank_edit';
			
			if ($IN['id'] == "")
			{
				$ADMIN->error("沒有設定等級 ID，請重試");
			}
			
			$DB->query("SELECT * from ibf_titles WHERE id='".$IN['id']."'");
			$rank = $DB->fetch_row();
			
			$button = "完成編輯";
		}
		else
		{
			$form_code = 'do_add_rank';
			$rank = array( 'posts' => "", 'title' => "", 'pips' => "");
			$button = "增加這個等級";
		}
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $form_code  ),
												  2 => array( 'act'   , 'mem'       ),
												  3 => array( 'id'    , $rank['id'] ),
									     )      );
		
		//+-------------------------------
		
		$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_input( "title", $rank['title'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>最少所需發表數</b>" ,
												  $SKIN->form_input( "posts", $rank['posts'] )
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b>格點數目</b><br>(或者格點圖片)" ,
												  $SKIN->form_input( "pips", $rank['pips'] )
									     )      );
									     									     
		$ADMIN->html .= $SKIN->end_form($button);
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
		
	}
	
	//+---------------------------------------------------------------------------------


	//+---------------------------------------------------------------------------------
	//
	// DELETE MEMBER SET UP
	//
	//+---------------------------------------------------------------------------------

	function delete_form() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;

		$ADMIN->page_title = "會員帳號刪除";

		$ADMIN->page_detail = "輸入部份或完整的使用者名稱以搜尋要刪除的會員，或者設定刪除表單.";


		$mem_group[0] = array( '0', '任何會員群組' );

		$DB->query("SELECT g_id, g_title FROM ibf_groups ORDER BY g_title");

		while ( $r = $DB->fetch_row() )
		{
			$mem_group[] = array( $r['g_id'] , $r['g_title'] );
		}

		//+-------------------------------

		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'delete2' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );

		//+-------------------------------

		$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_input( "USER_NAME" )
									     )      );

		$ADMIN->html .= $SKIN->end_form("尋找會員帳號");

		$ADMIN->html .= $SKIN->end_table();

		//+-------------------------------
		//+-------------------------------

		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'prune' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );

		//+-------------------------------

		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );

		//+-------------------------------

		$ADMIN->html .= $SKIN->start_table( "<u>或者</u> 移除會員條件為..." );


		$ADMIN->html .= $SKIN->add_td_row( array( "<b>會員超過 [x] 天以上未曾發表</b><br>([x] = 輸入的數字)<br>(留空以略過查詢)" ,
												  $SKIN->form_input( "last_post", '60')
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b><u>以及</u> 會員少於 [x] 篇文章</b><br>([x] = 輸入的數字)<br>(留空以略過查詢)" ,
												  $SKIN->form_input( "posts", '100')
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b><u>以及</u> 會員已經註冊 [x] 天以上</b><br>([x] = 輸入的數字)<br>(留空以略過查詢)" ,
												  $SKIN->form_input( "joined", '365')
									     )      );

		$ADMIN->html .= $SKIN->add_td_row( array( "<b><u>以及</u> 會員群組為...</b>" ,
												  $SKIN->form_dropdown( "mgroup",
																		$mem_group,
												  						0
												  					  )
									     )      );

		$ADMIN->html .= $SKIN->end_form("刪除會員");

		$ADMIN->html .= $SKIN->end_table();

		$ADMIN->output();


	}

	//+---------------------------------------------------------------------------------

	function prune_confirm() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;

		//-----------------------------
		// Make sure we have *something*
		//------------------------------

		$blanks = 0;

		foreach( array( 'posts', 'last_post', 'joined' ) as $field )
		{
			if ($IN[ $field ] == "")
			{
				$blanks++;
			}
		}

		if ($blanks == 3)
		{
			$ADMIN->error("您至少必須指定一個欄位以用於刪除查詢");
		}

		$time_now = time();

		$query = "SELECT COUNT(uid) as mcount FROM ibf_members WHERE";

		$add_query = array();

		if ($IN['joined'] > 0)
		{
			$j = $time_now - ($IN['joined'] *60*60*24);
			$add_query[] = " user_regdate < $j ";
		}

		if ($IN['last_post'] > 0)
		{
			$l = $time_now - ($IN['last_post'] *60*60*24);
			$add_query[] = " last_post < $l ";
		}

		if ($IN['posts'] > 0)
		{
			$add_query[] = " posts < ".$IN['posts']." ";
		}

		if ($IN['mgroup'] > 0)
		{
			$add_query[] = " mgroup='".$IN['mgroup']."' ";
		}

		$add_query[] = ' uid > 0';

		$additional_query = implode( "AND", $add_query );

		$this_query = trim( $query.$additional_query );

		$pass_query = addslashes(urlencode($additional_query));

		//--------------------------------
		// Run the query
		//--------------------------------

		$DB->query($this_query);

		$count = $DB->fetch_row();

		if ($count['mcount'] < 1)
		{
			$ADMIN->error("無法找到任何符合刪除標準的會員，請返回重試");
		}

		if ($count['mcount'] < 101)
		{
			$DB->query("SELECT uid, uname FROM ibf_members WHERE $additional_query");

			$member_arr = array();

			while ( $mem = $DB->fetch_row() )
			{
				$member_arr[] = $std->make_profile_link($mem['uname'], $mem['uid']);
			}
		}

		$ADMIN->page_title = "會員刪除";

		$ADMIN->page_detail = "請確認您的動作.";

		//+-------------------------------

		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doprune' ),
												  2 => array( 'act'   , 'mem'     ),
												  3 => array( 'query' , $pass_query ),
									     )      );

		//+-------------------------------

		$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>" ,
												  $count['mcount']
									     )      );

		if ( count($member_arr) > 0 )
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>要刪除的會員</b>" ,
													  implode( ', ', $member_arr )
											 )      );
		}

		$ADMIN->html .= $SKIN->end_form("完成會員刪除");

		$ADMIN->html .= $SKIN->end_table();

		$ADMIN->output();

	}


	//+---------------------------------------------------------------------------------

	
	
	function doprune() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//-----------------------------
		// Make sure we have *something*
		//------------------------------
		
		$query = trim(urldecode($std->txt_stripslashes($IN['query'])));
		
		$query = str_replace( "&lt;", "<", $query );
		$query = str_replace( "&gt;", ">", $query );
		
		if ($query == "")
		{
			$ADMIN->error("刪除查詢錯誤，沒有查詢被使用");
		}
		
		//-----------------------------
		// Get the member ids...
		//------------------------------
		
		$ids = array();
		
		$DB->query("SELECT uid FROM ibf_members WHERE ".$query);
		
		if ( $DB->get_num_rows() )
		{
			while ($i = $DB->fetch_row())
			{
				$ids[] = $i['uid'];
			}
		}
		else
		{
			$ADMIN->error("無法找到任何符合刪除標準的會員");
		}
		
		$id_string = implode( "," , $ids );
		
		$id_count = count($ids);
		
		// Convert their posts and topics into guest postings..
		
		$DB->query("UPDATE ibf_posts SET author_id='0' WHERE author_id IN(".$id_string.")");
		
		$DB->query("UPDATE ibf_topics SET starter_id='0' WHERE starter_id IN(".$id_string.")");
		
		// Delete member...
		
		$DB->query("DELETE from ibf_members WHERE uid IN(".$id_string.")");
		
		$DB->query("DELETE from ibf_pfields_content WHERE member_id IN(".$id_string.")");
		
		// Delete member messages...
		
		$DB->query("DELETE from ibf_messages WHERE member_id IN (".$id_string.")");
		
		// Delete member subscriptions.
		
		$DB->query("DELETE from ibf_tracker WHERE member_id IN (".$id_string.")");
		
		// Delete from validating..
		
		$DB->query("DELETE FROM ibf_validating WHERE member_id IN (".$id_string.")");
		
		// Set the stats DB straight.
		
		$DB->query("SELECT uid, uname FROM ibf_members WHERE mgroup <> '".$INFO['auth_group']."' ORDER BY user_regdate DESC LIMIT 0,1");
		
		$memb = $DB->fetch_row();
		
		$DB->query("SELECT COUNT(uid) as members from ibf_members WHERE mgroup <> '".$INFO['auth_group']."'");
		$r = $DB->fetch_row();
		// Remove "guest" account...
		$r['members']--;
		$r['members'] < 1 ? 0 : $r['members'];
		
		$DB->query("UPDATE ibf_stats SET ".
			             "MEM_COUNT={$r['members']}, ".
			             "LAST_MEM_NAME='" . $memb['uname'] . "', ".
			             "LAST_MEM_ID='"   . $memb['uid']   . "'");
		
		// Blow me melon farmer
		
		$ADMIN->save_log("由刪除表單移除 $id_count 位會員");
		
		if ( USE_MODULES == 1 )
		{
			$this->modules->register_class(&$this);
			$this->modules->on_delete($ids);
		}
		
		$ADMIN->done_screen("會員帳號已刪除", "會員控制", "act=mem&code=edit" );
		
	}
	
	//+---------------------------------------------------------------------------------
	
	function delete_lookup_form() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['USER_NAME'] == "")
		{
			$ADMIN->error("您沒有選擇要尋找的會員名稱！");
		}
		
		$DB->query("SELECT uid, uname FROM ibf_members WHERE uname LIKE '".$DB->oopsAddSlashes($IN['USER_NAME'])."%'"); /* 中文修正 */
		
		if (! $DB->get_num_rows() )
		{
			$ADMIN->error("抱歉，我們無法找到任何會員符合您輸入的搜尋字串");
		}
		
		$form_array = array();
		
		while ( $r = $DB->fetch_row() )
		{
			$form_array[] = array( $r['uid'] , $r['uname'] );
		}
		
		
		
		$ADMIN->page_title = "刪除一位會員";
		
		$ADMIN->page_detail = "請選擇要刪除的會員";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dodelete' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );
		
		//+-------------------------------
		
		$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( "MEMBER_ID", $form_array )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("刪除會員");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//+---------------------------------------------------------------------------------
	
	//+---------------------------------------------------------------------------------
	//
	// DO DELETE
	//
	//+---------------------------------------------------------------------------------
	
	function dodelete() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['MEMBER_ID'] == "")
		{
			$ADMIN->error("無法解析會員 id");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_members WHERE uid='".$IN['MEMBER_ID']."'");
		$mem = $DB->fetch_row();
		
		//+-------------------------------
		
		if ($mem['uid'] == "")
		{
			$ADMIN->error("無法解析會員 id");
		}
		
		// Convert their posts and topics into guest postings..
		
		$DB->query("UPDATE ibf_posts SET author_id='0' WHERE author_id='".$IN['MEMBER_ID']."'");
		
		$DB->query("UPDATE ibf_topics SET starter_id='0' WHERE starter_id='".$IN['MEMBER_ID']."'");
		
		// Delete member...
		
		$DB->query("DELETE from ibf_members WHERE uid='".$IN['MEMBER_ID']."'");
		$DB->query("DELETE from ibf_pfields_content WHERE member_id='".$IN['MEMBER_ID']."'");
		$DB->query("DELETE from ibf_member_extra WHERE id='".$IN['MEMBER_ID']."'");
		
		// Delete member messages...
		
		$DB->query("DELETE from ibf_messages WHERE member_id='".$IN['MEMBER_ID']."'");
		$DB->query("DELETE from ibf_contacts WHERE member_id='".$IN['MEMBER_ID']."' or contact_id='".$IN['MEMBER_ID']."'");
		
		// Delete member subscriptions.
		
		$DB->query("DELETE from ibf_tracker WHERE member_id='".$IN['MEMBER_ID']."'");
		$DB->query("DELETE from ibf_forum_tracker WHERE member_id='".$IN['MEMBER_ID']."'");
		$DB->query("DELETE from ibf_warn_logs WHERE wlog_mid='".$IN['MEMBER_ID']."'");
		
		// Delete from validating..
		
		$DB->query("DELETE FROM ibf_validating WHERE member_id='".$IN['MEMBER_ID']."'");
		
		// Set the stats DB straight.
		
		$DB->query("SELECT uid, uname FROM ibf_members WHERE mgroup <> '".$INFO['auth_group']."' ORDER BY user_regdate DESC LIMIT 0,1");
		
		$memb = $DB->fetch_row();
		
		$DB->query("SELECT COUNT(uid) as members from ibf_members WHERE mgroup <> '".$INFO['auth_group']."'");
		$r = $DB->fetch_row();
		// Remove "guest" account...
		$r['members']--;
		$r['members'] < 1 ? 0 : $r['members'];
		
		$DB->query("UPDATE ibf_stats SET ".
			             "MEM_COUNT={$r['members']}, ".
			             "LAST_MEM_NAME='" . $memb['uname'] . "', ".
			             "LAST_MEM_ID='"   . $memb['uid']   . "'");
		
		if ( USE_MODULES == 1 )
		{
			$this->modules->register_class(&$this);
			$this->modules->on_delete($IN['MEMBER_ID']);
		}
		
		// Blow me melon farmer
		$mem['uname'] = $DB->oopsAddSlashes($mem['uname']); /* 中文修正 */
		$ADMIN->save_log("刪除會員 '{$mem['uname']}'");
		
		$ADMIN->done_screen("會員帳號已刪除", "會員控制", "act=mem&code=edit" );
		
	}
		
	
	//+---------------------------------------------------------------------------------
	//
	// ADD MEMBER FORM
	//
	//+---------------------------------------------------------------------------------
	
	function add_form() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "預先註冊會員";
		
		$ADMIN->page_detail = "您可以使用這個表單預先註冊會員";
		
		$DB->query("SELECT g_id, g_title FROM ibf_groups ORDER BY g_title");
		
		while ( $r = $DB->fetch_row() )
		{
			if ($INFO['admin_group'] == $r['g_id'])
			{
				if ($MEMBER['mgroup'] != $INFO['admin_group'])
				{
					continue;
				}
			}
			$mem_group[] = array( $r['g_id'] , $r['g_title'] );
		}
		
		//+-------------------------------
		
		$custom_output = "";
		$field_data     = array();
		
		$DB->query("SELECT * from ibf_pfields_content WHERE member_id='".$IN['MEMBER_ID']."'");
		
		while ( $content = $DB->fetch_row() )
		{
			foreach($content as $k => $v)
			{
				if ( preg_match( "/^field_(\d+)$/", $k, $match) )
				{
					$field_data[ $match[1] ] = $v;
				}
			}
		}
		
		$DB->query("SELECT * from ibf_pfields_data WHERE fshowreg=1 ORDER BY forder");
		
		while( $row = $DB->fetch_row() )
		{
			$form_element = "";
			
			if ( $row['ftype'] == 'drop' )
			{
				$carray = explode( '|', trim($row['fcontent']) );
				
				$d_content = array();
				
				foreach( $carray as $entry )
				{
					$value = explode( '=', $entry );
					
					$ov = trim($value[0]);
					$td = trim($value[1]);
					
					if ($ov and $td)
					{
						$d_content[] = array( $ov, $td );
					}
				}
				
				$form_element = $SKIN->form_dropdown( 'field_'.$row['fid'], $d_content, "" );
				
			}
			else if ( $row['ftype'] == 'area' )
			{
				$form_element = $SKIN->form_textarea( 'field_'.$row['fid'], "" );
			}
			else
			{
				$form_element = $SKIN->form_input( 'field_'.$row['fid'], "" );
			}
			
			$custom_out .= $SKIN->add_td_row( array( "<b>{$row['ftitle']}</b><br>{$row['desc']}" , $form_element ) );
			
		}
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doadd' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );
		
		//+-------------------------------
		
		$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_input( "name" )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>密碼</b>" ,
												  $SKIN->form_input( "password", "", 'password' )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件位址</b>" ,
												  $SKIN->form_input( "email" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>會員群組</b>" ,
												  $SKIN->form_dropdown( "mgroup",
																		$mem_group,
												  						$mem['mgroup']
												  					  )
									     )      );
									     
		if ($custom_out != "")
		{
			$ADMIN->html .= $custom_out;
		}
									     						     
		$ADMIN->html .= $SKIN->end_form("註冊會員");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
		
	}
	
	function do_add()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		foreach( array('name', 'password', 'email', 'mgroup') as $field )
		{
			if ($IN[ $field ] == "")
			{
				$ADMIN->error("您必須完成整個表單！");
			}
		}
		
		//----------------------------------
		// Do we already have such a member?
		//----------------------------------
		
		$DB->query("SELECT uid FROM ibf_members WHERE LOWER(uname)='".$IN['name']."'");
		
		if ( $DB->get_num_rows() )
		{
			$ADMIN->error("我們已經有這個名稱的會員，請另選其他的");
		}
		
		//----------------------------------
		// Custom profile field stuff
		//----------------------------------
		
		$custom_fields = array();
		
		$DB->query("SELECT * from ibf_pfields_data");
		
		$have_custom = $DB->get_num_rows();
		
		while ( $row = $DB->fetch_row() )
		{
			$custom_fields[ 'field_'.$row['fid'] ] = $IN[ 'field_'.$row['fid'] ];
		}
		
		//+--------------------------------------------
		//| Find the highest member id, and increment it
		//| auto_increment not used for guest id 0 val.
		//+--------------------------------------------
		
		$DB->query("SELECT MAX(uid) as new_id FROM ibf_members");
		$r = $DB->fetch_row();
		
		$member_id = $r['new_id'] + 1;
		
		$db_string = $DB->compile_db_insert_string( array (
															 'uid'            => $member_id,
															 'uname'          => trim($IN['name']),
															 'pass'           => md5(trim($IN['password'])),
															 'email'          => trim(strtolower($IN['email'])),
															 'mgroup'         => $IN['mgroup'],
															 'user_regdate'   => time(),
															 'posts'          => 0,
															 'ip_address'     => $IN['ip_address'],
															 'timezone_offset'=> 0,
															 'attachsig'      => 1,
															 'view_avs'       => 1,
															 'view_pop'       => 1,
															 'view_img'       => 1,
															 'vdirs'          => "in:Inbox|sent:Sent Items",
												  )       );
												  
		$DB->query("INSERT INTO ibf_members (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
		
		//$member_id = $DB->get_insert_id();
		
		//+--------------------------------------------
		//| Insert into the custom profile fields DB
		//+--------------------------------------------
		
		if ( count($custom_fields) > 0 )
		{
		
			$custom_fields['member_id'] = $member_id;
				
			$db_string = $DB->compile_db_insert_string($custom_fields);
				
			$DB->query("INSERT INTO ibf_pfields_content (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");
		
		}
		
		unset($db_string);
		
		//+--------------------------------------------
		
		$DB->query("UPDATE ibf_stats SET ".
			             "MEM_COUNT=MEM_COUNT+1, ".
			             "LAST_MEM_NAME='" . trim($IN['name']) . "', ".
			             "LAST_MEM_ID='"   . $member_id   . "'");
			             
		$ADMIN->save_log("建立新會員帳號 '{$IN['name']}'");
		
		$ADMIN->done_screen("會員帳號已建立", "會員控制", "act=mem&code=edit" );												 
		
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// SEARCH FORM, SEARCH FOR MEMBER
	//
	//+---------------------------------------------------------------------------------
	
	function search_form() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "編輯會員";
		
		$ADMIN->page_detail = "搜尋會員";
		
		$mem_group = array( 0 => array( 'all', '任何群組') );
		
		$DB->query("SELECT g_id, g_title FROM ibf_groups ORDER BY g_title");
		
		while ( $r = $DB->fetch_row() )
		{
			$mem_group[] = array( $r['g_id'] , $r['g_title'] );
		}
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'stepone' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );
		
		//+-------------------------------
		
		$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_input( "USER_NAME" )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("尋找會員");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'advancedsearch' ),
												  2 => array( 'act'   , 'mem'     ),
									     )      );
		
		//+-------------------------------
		
		$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_input( "name" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>電子郵件位址包含...</b>" ,
												  $SKIN->form_input( "email" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>IP 位址包含...</b>" ,
												  $SKIN->form_input( "ip_address" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>AIM 名稱包含...</b>" ,
												  $SKIN->form_input( "aim_name" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>ICQ 號碼包含...</b>" ,
												  $SKIN->form_input( "icq_number" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>Yahoo! 身分包含...</b>" ,
												  $SKIN->form_input( "yahoo" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>簽名檔包含...</b>" ,
												  $SKIN->form_input( "signature" )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>最後發表從...</b>" ,
												  $SKIN->form_simple_input( "last_post" ). '... 天前至今'
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>最後活動從...</b>" ,
												  $SKIN->form_simple_input( "last_activity" ). '... 天前至今'
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>隸屬於群組...</b>" ,
												  $SKIN->form_dropdown( "mgroup", $mem_group )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("查詢會員資料庫");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
		
	}
	
	//+---------------------------------------------------------------------------------
	
	function do_advanced_search($basic=0) {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$page_query = "";
		$un_all = "";
		
		if ( $IN['showsusp'] == 1 )
		{
			$rq  = "temp_ban <> '' and temp_ban is not null";
			$un_all = "<a href='{$SKIN->base_url}&act=mem&code=unsuspend&mid=all'>復權所有帳號</a>&nbsp;&middot&nbsp;";
		}
		else
		{
			if ($basic == 0)
			{
				$query = array();
				
				foreach( array('uname','email','ip_address','user_aim','user_icq','user_yim','signature','last_post','last_activity','mgroup') as $bit )
				{
					$IN[ $bit ] = urldecode(trim($IN[ $bit ]));
					
					$page_query .= '&'.$bit.'='.urlencode($IN[ $bit ]);
					
					if ($IN[ $bit ] != "")
					{
						if ($bit == 'last_post' or $bit == 'last_activity')
						{
							$dateline = time() - ($IN[ $bit ]*60*60*24);
							$query[] = 'm.'.$bit.' > '."'$dateline'";
						}
						else if ($bit == 'mgroup')
						{
							if ($IN['mgroup'] != 'all')
							{
								$query[] = "m.mgroup=".$IN['mgroup'];
							}
						}
						else
						{
							$query[] = 'm.'.$bit." LIKE '%".$DB->oopsAddSlashes($IN[$bit])."%'"; /* 中文修正 */
						}
					}
				}
				
				if (count($query) < 1)
				{
					$ADMIN->error("在送出搜尋表單之前請至少完成一個欄位 （不包含會員名稱）");
				}
				
				$rq = implode( " AND ", $query );
			}
			else
			{
				// Basic username search
				
				if ( $IN['decode'] )
				{
					$IN['USER_NAME'] = trim(urldecode($IN['USER_NAME']));
				}
				else
				{
					$IN['USER_NAME'] = trim($IN['USER_NAME']);
				}
			
				if ($IN['USER_NAME'] == "")
				{
					$ADMIN->error("您沒有選擇要尋找的會員名稱！");
				}
				
				$page_query = "&decode=1&USER_NAME=".urlencode($IN['USER_NAME']);
			
				$rq = "uname LIKE '".$DB->oopsAddSlashes($IN['USER_NAME'])."%'"; /* 中文修正 */
			}
		}
		
		$st = intval($IN['st']);
		
		if ($st < 1)
		{
			$st = 0;
		}
		
		$query = "SELECT m.uid, m.email, m.uname, m.mgroup, m.ip_address, m.posts, m.temp_ban, g.g_title
		          FROM ibf_members m
		           LEFT JOIN ibf_groups g ON (g.g_id=m.mgroup)
		          WHERE $rq ORDER BY m.uname LIMIT $st,50";
		
		//+-------------------------------
		// Get the number of results
		//+-------------------------------
		
		$DB->query("SELECT COUNT(m.uid) as count FROM ibf_members m WHERE $rq");
		
		$count = $DB->fetch_row();
		
		if ($count['count'] < 1)
		{
			if ($IN['showsusp'])
			{
				$ADMIN->error("目前沒有停權的會員帳號");
			}
			else
			{
				$ADMIN->error("您的會員資料庫檢索查詢沒有傳回任何相符的結果，請返回重試");
			}
		}
		
		$ADMIN->page_title = "您的會員搜尋結果";
		
		$ADMIN->page_detail = "您的搜尋結果";
		
		//+-------------------------------
		
		$pages = $std->build_pagelinks( array( 'TOTAL_POSS'  => $count['count'],
											   'PER_PAGE'    => 50,
											   'CUR_ST_VAL'  => $IN['st'],
											   'L_SINGLE'    => $un_all."單頁",
											   'L_MULTI'     => $un_all."多頁",
											   'BASE_URL'    => $SKIN->base_url."&act=mem&showsusp={$IN['showsusp']}&code={$IN['code']}".$page_query,
											 )
									  );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "IP 位址 "  , "15%" );
		$SKIN->td_header[] = array( "群組"        , "10%" );
		$SKIN->td_header[] = array( "文章數"        , "10%" );
		$SKIN->td_header[] = array( "電子郵件"        , "15%" );
		$SKIN->td_header[] = array( "編輯"         , "16%" );
		$SKIN->td_header[] = array( "變更"       , "16%" );
		$SKIN->td_header[] = array( "封鎖"          , "17%" );
		
		//+-------------------------------
		
		$title = $IN['showsusp'] == 1 ? '停權的帳號' : '搜尋結果';
		
		$ADMIN->html .= $SKIN->start_table( "{$count['count']} ".$title );
		
		//+-------------------------------
		// Run the query
		//+-------------------------------
		
		$DB->query($query);
		
		while ( $r = $DB->fetch_row() )
		{
			$tban = "";
			$sus_link = "";
			
			if ( $r['temp_ban'] != "" )
			{
				$s_ban = $std->hdl_ban_line( $r['temp_ban'] );
				
				$sus_link = " - <a href='{$SKIN->base_url}&act=mem&code=unsuspend&mid={$r['uid']}'>復權</a>";
				
				$tban = '&nbsp;&nbsp;<span style="font-size:10px">(停權至 '.$ADMIN->get_date( $s_ban['date_end'], 'LONG') .$sus_link.')</span>';
			}
			
			$ADMIN->html .= $SKIN->add_td_basic( "<img src='html/sys-img/item.gif' border='0' alt='-'>&nbsp;<a style='font-size:12px' title='檢視此會員個人資料' href='{$INFO['board_url']}/index.{$INFO['php_ext']}?act=Profile&MID={$r['uid']}' target='blank'>{$r['uname']}</a> $tban", "left", "pformstrip" );
			$ADMIN->html .= $SKIN->add_td_row( array( "{$r['ip_address']}",
													  $r['g_title'],
													  "<center>".$r['posts']."</center>",
													  "<center>".$r['email']."</center>",
													  "<center><strong><a href='{$SKIN->base_url}&act=mem&code=doform&MEMBER_ID={$r['uid']}' title='編輯此會員帳號'>編輯資料</a></strong></center>",
													  "<center><a href='{$SKIN->base_url}&act=mem&code=changename&mid={$r['uid']}' title='變更此會員名稱'>變更名稱</a></center>",
													  "<center><a href='{$SKIN->base_url}&act=mem&code=banmember&mid={$r['uid']}' title='停權會員'>停權帳號</a></span></center>"
									     	 )      );
		
		}
		
		$ADMIN->html .= $SKIN->add_td_basic($pages, 'right', 'pformstrip');
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
		
		
	}
	
	//+---------------------------------------------------------------------------------
	//
	// DO EDIT FORM
	//
	//+---------------------------------------------------------------------------------
	
	function do_edit_form() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $ibforums;
		
		require ROOT_PATH."sources/lib/post_parser.php";
		
		$parser = new post_parser();
		
		if ($IN['MEMBER_ID'] == "")
		{
			$ADMIN->error("無法解析會員 id");
		}
		
		//+-------------------------------
		
		$DB->query("SELECT * FROM ibf_members WHERE uid='".$IN['MEMBER_ID']."'");
		$mem = $DB->fetch_row();
		
		//+-------------------------------
		
		if ($mem['uid'] == "")
		{
			$ADMIN->error("無法解析會員 id");
		}
		
		//+-------------------------------
		
		$mem_group = array();
		$show_fixed = FALSE;
		
		$units = array( 0 => array( 'h', '小時' ), 1 => array( 'd', '天' ) );
		
		$DB->query("SELECT g_id, g_title FROM ibf_groups ORDER BY g_title");
		
		while ( $r = $DB->fetch_row() )
		{
			// Ensure only root admins can promote to root admin grou...
			// oh screw it, I can't be bothered explaining stuff tonight
			
			if ($INFO['admin_group'] == $r['g_id'])
			{
				if ($MEMBER['mgroup'] != $INFO['admin_group'])
				{
					continue;
				}
			}
			
			$mem_group[] = array( $r['g_id'] , $r['g_title'] );
		}
		
		// is this a non root editing a root?
		
		if ($MEMBER['mgroup'] != $INFO['admin_group'])
		{
			if ($mem['mgroup'] == $INFO['admin_group'])
			{
				$show_fixed = TRUE;
			}
		}
		
		//+-------------------------------
		
		$lang_array = array();
		
		$DB->query("SELECT ldir, lname FROM ibf_languages");
		
		while ( $l = $DB->fetch_row() )
		{
			$lang_array[] = array( $l['ldir'], $l['lname'] );
		}
 		
 		//+-------------------------------
 		
 		$DB->query("SELECT uid, sid, sname, default_set, hidden FROM ibf_skins");
 		
 		$skin_array = array();
 		
 		$def_skin = "";
 			
		if ( $DB->get_num_rows() )
		{
			while ( $s = $DB->fetch_row() )
			{
				if ($s['default_set'] == 1)
				{
					$def_skin = $s['sid'];
				}
				
				if ($s['hidden'] == 1)
				{
					$hidden = " *(Hidden)";
				}
				else
				{
					$hidden = "";
				}
				
				$skin_array[] = array( $s['sid'], $s['sname'].$hidden );
			   
			}
		}
			
		//+-------------------------------
		
		if ($INFO['default_language'] == "")
		{
			$INFO['default_language'] = 'tw';
		}
		
		//-----------------------------------------------
		// Custom profile fields stuff
		//-----------------------------------------------
		
		$custom_output = "";
		$field_data     = array();
		
		$DB->query("SELECT * from ibf_pfields_content WHERE member_id='".$IN['MEMBER_ID']."'");
		
		while ( $content = $DB->fetch_row() )
		{
			foreach($content as $k => $v)
			{
				if ( preg_match( "/^field_(\d+)$/", $k, $match) )
				{
					$field_data[ $match[1] ] = $v;
				}
			}
		}
		
		$DB->query("SELECT * from ibf_pfields_data ORDER BY forder");
		
		while( $row = $DB->fetch_row() )
		{
			$form_element = "";
			
			if ( $row['ftype'] == 'drop' )
			{
				$carray = explode( '|', trim($row['fcontent']) );
				
				$d_content = array();
				
				foreach( $carray as $entry )
				{
					$value = explode( '=', $entry );
					
					$ov = trim($value[0]);
					$td = trim($value[1]);
					
					if ($ov != "" and $td !="")
					{
						$d_content[] = array( $ov, $td );
					}
				}
				
				$form_element = $SKIN->form_dropdown( 'field_'.$row['fid'], $d_content, $field_data[$row['fid']] );
				
			}
			else if ( $row['ftype'] == 'area' )
			{
				$form_element = $SKIN->form_textarea( 'field_'.$row['fid'], $field_data[$row['fid']] );
			}
			else
			{
				$form_element = $SKIN->form_input( 'field_'.$row['fid'], $field_data[$row['fid']] );
			}
			
			$custom_out .= $SKIN->add_td_row( array( "<b>{$row['ftitle']}</b><br>{$row['desc']}" , $form_element ) );
			
		}
		
		//+-------------------------------
		//| Perms masks section
		//+-------------------------------
		
		$perm_masks = array();
		
		
		$DB->query("SELECT * FROM ibf_forum_perms");
		
		while ( $r = $DB->fetch_row() )
		{
			$perm_masks[] = array( $r['perm_id'], $r['perm_name'] );
		}
		
		//+-------------------------------
		
		
		$ADMIN->page_title = "編輯會員： ".$mem['uname']." (ID: ".$mem['uid'].")";
		
		$ADMIN->page_detail = "您可以從這裡改變會員設定";
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'doedit'  ),
												  2 => array( 'act'   , 'mem'     ),
												  3 => array( 'mid'   , $mem['uid'] ),
												  4 => array( 'curpass', $mem['pass'] ),
											) );
		
		//+-------------------------------
		
		$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>註冊時的 IP 位址</b>" ,$mem['ip_address'] )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>移除會員的照片</b>" ,
												  $SKIN->form_checkbox("remove_photo", 0)
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>警告層級</b>" ,
												  $SKIN->form_input("warn_level", $mem['warn_level'])
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>會員頭銜</b>" ,
												  $SKIN->form_input("title", $mem['title'])
									     )      );
		
		if ($show_fixed != TRUE)
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>會員群組</b>" ,
													  $SKIN->form_dropdown( "mgroup",
																			$mem_group,
																			$mem['mgroup']
																		  )
											 )      );
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>會員群組</b>" ,
													  $SKIN->form_hidden( array( 1 => array( 'mgroup' , $mem['mgroup'] ) ) )."<b>Root Admin</b> (Can't Change)",
											 )      );
		}
									     
		//+-------------------------------
		// Sort out perm id stuff
		//+-------------------------------
									     
		$ADMIN->html .=
		"<script type='text/javascript'>
			
			var show   = '';
		";
		
		foreach ($perm_masks as $id => $d)
		{
			$ADMIN->html .= " 		perms_$d[0] = '$d[1]';\n";
		}
		
		$ADMIN->html .=
		"
			
		 	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);
					}
				}
				
				if ( show != '' )
				{
					document.forms[0].override.checked = true;
				}
				else
				{
					document.forms[0].override.checked = false;
				}
			}
			
			function show_me()
			{
				if (show == '')
				{
					show = '未偵測到變動\\n點按多重選擇方塊啟動';
				}
				
				alert('已選的權限遮蔽\\n---------------------------------\\n' + show);
			}
			
		</script>";
		
		$arr = explode( ",",$mem['org_perm_id'] );
		
		$ch_ch = ( $mem['org_perm_id'] ) ? 'checked' : '';
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>凌駕群組討論區權限遮蔽為...</b><br>您可以選擇一個以上" ,
												  "<input type='checkbox' name='override' value='1' $ch_ch> <b>凌駕群組權限遮蔽為...</b><br>".
												  $SKIN->form_multiselect( "permid[]",
																		$perm_masks,
																		$arr, 5, 'onfocus="saveit(this)" onchange="saveit(this)"'
																	  )."<br><input style='margin-top:5px' id='editbutton' type='button' onclick='show_me();' value='顯示所選的遮蔽'>"
										 ) , "subforum"   );
	
		//-----------------------------------------------------------------------------------------------
		// Mod posts bit
		//-----------------------------------------------------------------------------------------------
		
		$mod_tick = 0;
		$mod_arr  = array();
		
		if ( $mem['mod_posts'] == 1 )
		{
			$mod_tick = 'checked';
		}
		elseif ($mem['mod_posts'] > 0)
		{
			$mod_arr = $std->hdl_ban_line( $mem['mod_posts'] );
			
			$hours  = ceil( ( $mod_arr['date_end'] - time() ) / 3600 );
				
			if ( $hours > 24 and ( ($hours / 24) == ceil($hours / 24) ) )
			{
				$mod_arr['units']    = 'd';
				$mod_arr['timespan'] = $hours / 24;
			}
			else
			{
				$mod_arr['units']    = 'h';
				$mod_arr['timespan'] = $hours;
			}
			
			$mod_extra = "<br /><span style='color:red'>限制進行中 - 剩餘的時間已重新計算</span>";
		}
						     
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>要求板主預覽此會員的所有文章？</b><br>如果是，此會員發表的所有文章將會放在管制佇列中。若要取消，剔選方塊及清空數字欄。" ,
												  "<input type='checkbox' name='mod_indef' value='1' $mod_tick> 板主永久預覽
												  <br /><b>或者</b> ".$SKIN->form_input('mod_timespan', $mod_arr['timespan'], "text", "", '5' ) . '&nbsp;' . $SKIN->form_dropdown('mod_units', $units, $mod_arr['units'] ).$mod_extra
									     )      );
									     
		
		$post_tick = 0;
		$post_arr  = array();
		
		if ( $mem['restrict_post'] == 1 )
		{
			$post_tick = 'checked';
		}
		else if( $mem['restrict_post'] > 0 )
		{
			$post_arr = $std->hdl_ban_line( $mem['restrict_post'] );
			
			$hours  = ceil( ( $post_arr['date_end'] - time() ) / 3600 );
				
			if ( $hours > 24 and ( ($hours / 24) == ceil($hours / 24) ) )
			{
				$post_arr['units']    = 'd';
				$post_arr['timespan'] = $hours / 24;
			}
			else
			{
				$post_arr['units']    = 'h';
				$post_arr['timespan'] = $hours;
			}
			
			$post_extra = "<br /><span style='color:red'>限制進行中 - 剩餘的時間已重新計算</span>";
		}
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>限制 {$mem['uname']} 發表？</b><br>若要取消限制，剔選方塊及清空數字欄。" ,
												  "<input type='checkbox' name='post_indef' value='1' $post_tick> 永久限制發表
												  <br /><b>或者</b> ".$SKIN->form_input('post_timespan', $post_arr['timespan'], "text", "", '5' ) . '&nbsp;' . $SKIN->form_dropdown('post_units', $units, $post_arr['units'] ).$post_extra
									     ) , "subforum"     );
									     
									     
		//-----------------------------------------------------------------------------------------------
									     
		$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><br>(如果不想重設密碼請留空！)" ,
												  $SKIN->form_input("password")
									     )      );
									     
		$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_dropdown( "language",
																		$lang_array,
												  						$mem['language'] != "" ? $mem['language'] : $INFO['default_language']
												  					  )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>面板選擇</b>" ,
												  $SKIN->form_dropdown( "skin",
																		$skin_array,
												  						$mem['skin'] != "" ? $mem['skin'] : $def_skin
												  					  )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>隱藏此會員的電子郵件位址？</b>" ,
												  $SKIN->form_yes_no("hide_email", $mem['user_viewemail'] )
									     )      );
									     						     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>PM 電子郵件通知？</b>" ,
												  $SKIN->form_yes_no("email_pm", $mem['email_pm'] )
									     )      );		
		
		$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_input("email", $mem['email'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>AIM 身分</b>" ,
												  $SKIN->form_input("aim_name", $mem['user_aim'])
									     )      );							     						     
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>ICQ 號碼</b>" ,
												  $SKIN->form_input("icq_number", $mem['user_icq'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>Yahoo 身分</b>" ,
												  $SKIN->form_input("yahoo", $mem['user_yim'])
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>MSN 身分</b>" ,
												  $SKIN->form_input("msnname", $mem['user_msnm'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>網站位址</b>" ,
												  $SKIN->form_input("website", $mem['url'])
									     )      );
									     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------+
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------+
		
		$ADMIN->html .= $SKIN->start_table( "其他資訊" );
									     							     							     
		//+-------------------------------
		
		$mem['signature'] = $parser->unconvert( $mem['signature'] );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>個人圖片</b>" ,
												  $SKIN->form_input("avatar", $mem['user_avatar'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>個人圖片大小</b>" ,
												  $SKIN->form_input("avatar_size", $mem['avatar_size'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>發表數</b>" ,
												  $SKIN->form_input("posts", $mem['posts'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>位置</b>" ,
												  $SKIN->form_input("location", $mem['user_from'])
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>興趣</b>" ,
												  $SKIN->form_textarea("interests", str_replace( '<br>', "\n",$mem['user_intrest']))
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>簽名檔</b>" ,
												  $SKIN->form_textarea("signature", $mem['signature'])
									     )      );
		
		
									     							     							     							     
		//+-------------------------------
		
		if ($custom_out != "")
		{
		
			$ADMIN->html .= $SKIN->end_table();
			
			
			$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
			$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
			
			//+-------------------------------+
			
			$ADMIN->html .= $SKIN->start_table( "自訂個人資料欄位" );
			
			
			$ADMIN->html .= $custom_out;
											 
		}
								     							     							     
		//+-------------------------------
									     							     							     							     
		$ADMIN->html .= $SKIN->end_form("編輯此會員");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
			
			
	}


	//+---------------------------------------------------------------------------------
	
	function do_edit()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $ibforums, $HTTP_POST_VARS;
		
		$DB->query("SELECT uname, mgroup, pass FROM ibf_members WHERE uid='".$IN['mid']."'");
		
		$memb = $DB->fetch_row();
		
		$password = "";
		
		if ($IN['password'] != "")
		{
			$password = ", pass='".md5($IN['password'])."'";
		}
		
		require ROOT_PATH."sources/lib/post_parser.php";
		
		$parser = new post_parser();
		
		$IN['signature'] = $parser->convert( array ('TEXT'      => $IN['signature'],
													'SMILIES'   => 0,
													'CODE'      => $INFO['sig_allow_ibc'],
													'HTML'      => $INFO['sig_allow_html'],
													'SIGNATURE' => 1
										   )       );
		
		if ( $IN['override'] == 1 )
		{
			$permid = implode( ",", $HTTP_POST_VARS['permid'] );
		}
		else
		{
			$permid = "";
		}
		
		$restrict_post = 0;
		$mod_queue     = 0;
		
		if ( $IN['mod_indef'] == 1 )
		{
			$mod_queue = 1;
		}
		elseif ( $IN['mod_timespan'] > 0 )
		{
			$mod_queue = $std->hdl_ban_line( array( 'timespan' => intval($IN['mod_timespan']), 'unit' => $IN['mod_units']  ) );
		}
		
		
		if ( $IN['post_indef'] == 1 )
		{
			$restrict_post = 1;
		}
		elseif ( $IN['post_timespan'] > 0 )
		{
			$restrict_post = $std->hdl_ban_line( array( 'timespan' => intval($IN['post_timespan']), 'unit' => $IN['post_units']  ) );
		}
		
		$db_string = $DB->compile_db_update_string( array (
															'restrict_post'=> $restrict_post,
															'mgroup'       => $IN['mgroup'],
															'title'        => $IN['title'],
															'language'     => $IN['language'],
															'skin'         => $IN['skin'],
															'user_viewemail'=> $IN['hide_email'],
															'email_pm'     => $IN['email_pm'],
															'email'        => $IN['email'],
															'user_aim'     => $IN['aim_name'],
															'user_icq'     => $IN['icq_number'],
															'user_yim'     => $IN['yahoo'],
															'user_msnm'    => $IN['msnname'],
															'url'          => $IN['website'],
															'user_avatar'  => $IN['avatar'],
															'avatar_size'  => $IN['avatar_size'],
															'posts'        => $IN['posts'],
															'user_from'    => $IN['location'],
															'user_intrest' => $IN['interests'],
															'signature'    => $IN['signature'],
															'mod_posts'    => $mod_queue,
															'org_perm_id'  => $permid,
															'warn_level'   => $IN['warn_level'],
												  )       );
												  
		$DB->query("UPDATE ibf_members SET $db_string".$password." WHERE uid='".$IN['mid']."'");
		
		//----------------------------------
		// Remove photo?
		//----------------------------------
		
		if ( $IN['remove_photo'] )
		{
			$DB->query("SELECT id FROM ibf_member_extra WHERE id={$IN['mid']}");
		
			if ( $DB->get_num_rows() )
			{
				$DB->query("UPDATE ibf_member_extra SET photo_location='', photo_type='', photo_dimensions='' WHERE id={$IN['mid']}");
			}
			else
			{
				$DB->query("INSERT INTO ibf_member_extra SET photo_location='', photo_type='', photo_dimensions='', id={$IN['mid']}");
			}
			
			foreach( array( 'swf', 'jpg', 'jpeg', 'gif', 'png' ) as $ext )
			{
				if ( @file_exists( $INFO['upload_dir']."/photo-".$IN['mid'].".".$ext ) )
				{
					@unlink( $INFO['upload_dir']."/photo-".$IN['mid'].".".$ext );
				}
			}
		}
		
		//----------------------------------
		// Custom profile field stuff
		//----------------------------------
		
		$custom_fields = array();
		
		$DB->query("SELECT * from ibf_pfields_data");
		
		while ( $row = $DB->fetch_row() )
		{
			$custom_fields[ 'field_'.$row['fid'] ] = str_replace( '<br>', "\n", $IN[ 'field_'.$row['fid'] ] );
		}
		
		if ( count($custom_fields) > 0 )
		{
			// Do we already have an entry in the content table?
			
			$DB->query("SELECT member_id FROM ibf_pfields_content WHERE member_id='".$IN['mid']."'");
			$test = $DB->fetch_row();
			
			if ( $test['member_id'] )
			{
				// We have it, so simply update
				
				$db_string = $DB->compile_db_update_string($custom_fields);
				
				$DB->query("UPDATE ibf_pfields_content SET $db_string WHERE member_id='".$IN['mid']."'");
			}
			else
			{
				$custom_fields['member_id'] = $IN['mid'];
				
				$db_string = $DB->compile_db_insert_string($custom_fields);
				
				$DB->query("INSERT INTO ibf_pfields_content (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");
			}
		}
		//$memb['uname'] = addslashes($memb['uname']);
		
		if ( USE_MODULES == 1 )
		{
			$this->modules->register_class(&$this);
			
			if ( md5($IN['password']) != $memb['pass'] )
			{
				$this->modules->on_pass_change($IN['mid'], $IN['password']);
			}
			
			if ( $memb['mgroup'] != $IN['mgroup'] )
			{
				$this->modules->on_group_change($IN['mid'], $IN['mgroup']);
			}
			
			$mem_array = array(
							    'title'        => $IN['title'],
								'user_aim'     => $IN['aim_name'],
								'user_icq'     => $IN['icq_number'],
								'user_yim'     => $IN['yahoo'],
								'user_msnm'    => $IN['msnname'],
								'url'          => $IN['website'],
								'user_from'    => $IN['location'],
								'user_intrest' => $IN['interests'],
								'uid'		   => $IN['mid']
							  );
			
			$this->modules->on_profile_update($mem_array, $custom_fields);
		}
		$memb['uname'] = $DB->oopsAddSlashes($memb['uname']); /* 中文修正 */
		$ADMIN->save_log("編輯會員 '{$memb['uname']}' 的帳號");
		
		$ADMIN->done_screen("會員已編輯", "會員控制", "act=mem&code=edit" );
		
	}


	
	
		
}


?>
