<?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
+---------------------------------------------------------------------------
|
|   > Email Logs Stuff
|   > Module written by Matt Mecham
|   > Date started: 11nd September 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/


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


$idx = new ad_emaillogs();


class ad_emaillogs
{

	var $base_url;
	var $colours = array();

	function ad_emaillogs()
	{
		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);
		}
		
		//---------------------------------------
		
		// Make sure we're a root admin, or else!
		
		if ($MEMBER['mgroup'] != $INFO['admin_group'])
		{
			$ADMIN->error("抱歉，這些功能僅限最高管理群組使用");
		}
		

		switch($IN['code'])
		{
		
			case 'list':
				$this->list_current();
				break;
				
			case 'remove':
				$this->remove_entries();
				break;
				
		    case 'viewemail':
		    	$this->view_email();
		    	break;
				
				
			//-------------------------
			default:
				$this->list_current();
				break;
		}
		
	}
	
	//---------------------------------------------
	// View a single email.
	//---------------------------------------------
	
	function view_email()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試");
		}
		
		$id = intval($IN['id']);
		
		$DB->query("SELECT email.*, m.uid, m.uname, mem.uid as to_id, mem.uname as to_name FROM ibf_email_logs email
					 LEFT JOIN ibf_members m ON (m.uid=email.from_member_id)
					 LEFT JOIN ibf_members mem ON (mem.uid=email.to_member_id)
				    WHERE email.email_id=$id");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試 ($id)");
		}
		
		$SKIN->td_header[] = array( "&nbsp;" , "100%" );
		
		$ADMIN->html .= $SKIN->start_table( $row['email_subject'] );
	
		
		
		$row['email_date'] = $ADMIN->get_date( $row['email_date'], 'LONG' );
		
		$ADMIN->html .= $SKIN->add_td_row( array(
													"<strong>寄件人：</strong> {$row['uname']} &lt;{$row['from_email_address']}&gt;
													<br /><strong>收件人：</strong> {$row['to_name']} &lt;{$row['to_email_address']}&gt;
													<br /><strong>傳送日期：</strong> {$row['email_date']}
													<br /><strong>寄件 IP：</strong> {$row['from_ip_address']}
													<br /><strong>郵件標題：</strong> {$row['email_subject']}
													<hr>
													<br />{$row['email_content']}
												    "
										 )      );
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
	}
	

	
	//---------------------------------------------
	// Remove row(s)
	//---------------------------------------------
	
	function remove_entries()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ( $IN['type'] == 'all' )
		{
			$DB->query("DELETE FROM ibf_email_logs");
		}
		else
		{
			$ids = array();
		
			foreach ($IN as $k => $v)
			{
				if ( preg_match( "/^id_(\d+)$/", $k, $match ) )
				{
					if ($IN[ $match[0] ])
					{
						$ids[] = $match[1];
					}
				}
			}
			
			//-------------------
			
			if ( count($ids) < 1 )
			{
				$ADMIN->error("您沒有選擇任何要批准或刪除的電子郵件日誌條目");
			}
			
			$DB->query("DELETE FROM ibf_email_logs WHERE email_id IN (".implode(',', $ids ).")");
		}
		
		$ADMIN->save_log("移除電子郵件日誌");
		
		$std->boink_it($ADMIN->base_url."&act=emaillog");
		exit();
	
	
	}
	
	

	
	
	//-------------------------------------------------------------
	// SHOW ALL LANGUAGE PACKS
	//-------------------------------------------------------------
	
	function list_current()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->html .= $SKIN->js_pop_win();
		
		$form_array = array();
		
		$start = intval($IN['st']);
	
		$ADMIN->page_detail = "已儲存的電子郵件日誌";
		$ADMIN->page_title  = "電子郵件日誌經理";
		
		//+-------------------------------
		// Check URL parameters
		//+-------------------------------
		
		$url_query = array();
		$db_query  = array();
		
		if ( $IN['type'] != "" )
		{
			$ADMIN->page_title .= " (搜尋結果)";
		
			switch( $IN['type'] )
			{
				case 'fromid':
					$url_query[] = 'type=fromid';
					$url_query[] = 'id='.intval($IN['id']);
					$db_query[]  = 'email.from_member_id='.intval($IN['id']);
					break;
				case 'toid':
					$url_query[] = 'type=toid';
					$url_query[] = 'id='.intval($IN['id']);
					$db_query[]  = 'email.to_member_id='.intval($IN['id']);
					break;
				case 'subject':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					$db_query[]  = $IN['match'] == 'loose' ? "email.email_subject LIKE '%{$string}%'" : "email.email_subject='{$string}'";
					break;
				case 'content':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					$db_query[]  = $IN['match'] == 'loose' ? "email.email_content LIKE '%{$string}%'" : "email.email_content='{$string}'";
					break;
				case 'email_from':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					$db_query[]  = $IN['match'] == 'loose' ? "email.from_email_address LIKE '%{$string}%'" : "email.from_email_address='{$string}'";
					break;
				case 'email_to':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					$db_query[]  = $IN['match'] == 'loose' ? "email.to_email_address LIKE '%{$string}%'" : "email.to_email_address='{$string}'";
					break;
				case 'name_from':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					
					if ( $IN['match'] == 'loose' )
					{
						$DB->query("SELECT uid as id,uname as name FROM ibf_members WHERE uname LIKE '%{$string}%'");
						
						if ( ! $DB->get_num_rows() )
						{
							$ADMIN->error("在電子郵件日誌中找不到相符的");
						}
						
						$ids = array();
						
						while ( $r = $DB->fetch_row() )
						{
							$ids[] = $r['id'];
						}
						
						$db_query[] = 'email.from_member_id IN('.implode( ',', $ids ).')';
					}
					else
					{
						$DB->query("SELECT uid as id,uname as name FROM ibf_members WHERE uname='{$string}'");
						
						if ( ! $DB->get_num_rows() )
						{
							$ADMIN->error("在電子郵件日誌中找不到相符的");
						}
						
						$r = $DB->fetch_row();
						
						$db_query[] = 'email.from_member_id IN('.$r['id'].')';
					}
					
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					break;
				case 'name_to':
					$string = urldecode($IN['string']);
					if ( $string == "" )
					{
						$ADMIN->error("您必須輸入一些東西以進行搜尋");
					}
					
					if ( $IN['match'] == 'loose' )
					{
						$DB->query("SELECT uid as id,uname as name FROM ibf_members WHERE uname LIKE '%{$string}%'");
						
						if ( ! $DB->get_num_rows() )
						{
							$ADMIN->error("在電子郵件日誌中找不到相符的");
						}
						
						$ids = array();
						
						while ( $r = $DB->fetch_row() )
						{
							$ids[] = $r['uid'];
						}
						
						$db_query[] = 'email.to_member_id IN('.implode( ',', $ids ).')';
					}
					else
					{
						$DB->query("SELECT uid as id,uname as name FROM ibf_members WHERE uname='{$string}'");
						
						if ( ! $DB->get_num_rows() )
						{
							$ADMIN->error("在電子郵件日誌中找不到相符的");
						}
						
						$r = $DB->fetch_row();
						
						$db_query[] = 'email.to_member_id IN('.$r['uid'].')';
					}
					
					$url_query[] = 'type='.$IN['type'];
					$url_query[] = 'string='.urlencode($string);
					break;
				default:
					//
					break;
			}
		}
		
		//+-------------------------------
		// LIST 'EM
		//+-------------------------------
		
		$dbe = "";
		$url = "";
		
		if ( count($db_query) > 0 )
		{
			$dbe = ' WHERE '.implode(' AND ', $db_query );
		}
		
		if ( count($url_query) > 0 )
		{
			$url = '&'.implode( '&', $url_query);
		}
		
		$DB->query("SELECT count(email.email_id) as cnt FROM ibf_email_logs email".$dbe);
		
		$count = $DB->fetch_row();
		
		$links = $std->build_pagelinks( array( 'TOTAL_POSS'  => $count['cnt'],
											   'PER_PAGE'    => 25,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "單頁",
											   'L_MULTI'     => "分頁：",
											   'BASE_URL'    => $ADMIN->base_url.'&act=emaillog'.$url,
											 )
									  );
		
		
		$DB->query("SELECT email.*, m.uid, m.uname, mem.uid as to_id, mem.uname as to_name FROM ibf_email_logs email
					 LEFT JOIN ibf_members m ON (m.uid=email.from_member_id)
					 LEFT JOIN ibf_members mem ON (mem.uid=email.to_member_id) $dbe
					ORDER BY email_date DESC LIMIT $start,25");
					
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'remove'     ),
												  2 => array( 'act'   , 'emaillog'       ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;"         , "5%" );
		$SKIN->td_header[] = array( "來自會員"    , "20%" );
		$SKIN->td_header[] = array( "標題"        , "30%" );
		$SKIN->td_header[] = array( "給會員"      , "20%" );
		$SKIN->td_header[] = array( "傳送時間"      , "25%" );

		$ADMIN->html .= $SKIN->start_table( "已記錄電子郵件" );
		
		if ( $DB->get_num_rows() )
		{
			while ( $row = $DB->fetch_row() )
			{
			
				$row['email_date'] = $ADMIN->get_date( $row['email_date'], 'SHORT' );
				
				$ADMIN->html .= $SKIN->add_td_row( array( 
														  "<center><input type='checkbox' class='checkbox' name='id_{$row['email_id']}' value='1' /></center>",
														  "<a href='{$ADMIN->base_url}&act=emaillog&code=list&type=fromid&id={$row['uid']}' title='顯示全部來自此會員的'><img src='{$SKIN->img_url}/acp_search.gif' border='0' alt='..按照 id'></a>&nbsp;<b><a href='{$INFO['board_url']}/index.{$INFO['php_ext']}?act=Profile&MID={$row['uid']}' title='會員資料檔案 (新視窗)' target='blank'>{$row['uname']}</a></b>",
														  "<a href='javascript:pop_win(\"&act=emaillog&code=viewemail&id={$row['email_id']}\",400,400)' title='讀取電子郵件'>{$row['email_subject']}</a>",
														  "<a href='{$ADMIN->base_url}&act=emaillog&code=list&type=toid&id={$row['to_id']}' title='顯示全部寄給此會員的'><img src='{$SKIN->img_url}/acp_search.gif' border='0' alt='..by id'></a>&nbsp;<a href='{$INFO['board_url']}/index.{$INFO['php_ext']}?act=Profile&MID={$row['to_id']}'  title='會員資料檔案 (新視窗)' target='blank'>{$row['to_name']}</a>",
														  "{$row['email_date']}",
												 )      );
			
			
			}
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_basic("<center>沒有結果</center>");
		}
		
		$ADMIN->html .= $SKIN->add_td_basic('<div style="float:left;width:auto"><input type="submit" value="移除勾選的" id="button" />&nbsp;<input type="checkbox" id="checkbox" name="type" value="all" />&nbsp;全部移除？</div><div align="right">'.$links.'</div></form>', 'left', 'pformstrip');
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'list'     ),
												  2 => array( 'act'   , 'emaillog'       ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;"  , "100%" );
		
		$ADMIN->html .= $SKIN->start_table( "搜尋電子郵件日誌" );
		
		$form_array = array(
							  0 => array( 'subject'    , '電子郵件標題'    ),
							  1 => array( 'content'    , '電子郵件內容' ),
							  2 => array( 'email_from' , '來自電子郵件位址' ),
							  3 => array( 'email_to'   , '寄給電子郵件位址'   ),
							  4 => array( 'name_from'  , '來自會員名稱'),
							  5 => array( 'name_to'    , '寄給會員名稱' ),
						   );
						   
		$type_array = array(
							  0 => array( 'exact'      , '精確' ),
							  1 => array( 'loose'      , '含有'   ),
						   );
			
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜尋</b> &nbsp;"
												  . $SKIN->form_dropdown( "type", $form_array) ." "
												  . $SKIN->form_dropdown( "match", $type_array) ." "
												  . $SKIN->form_input( "string"),
										  		
								 )      );
								 
		$ADMIN->html .= $SKIN->end_form("搜尋");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->nav[] = array( 'act=emaillog', '電子郵件日誌 (全部顯示)' );
		
		$ADMIN->output();
	
	}
	
	
	
}


?>