<?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
+---------------------------------------------------------------------------
|
|   > Warn Log functions
|   > Module written by Matt Mecham
|   > Date started: 4th June 2003
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/


// Ensure we've not accessed this script directly:



$idx = new ad_warnlogs();


class ad_warnlogs {

	var $base_url;

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

		switch($IN['code'])
		{
		
			case 'view':
				$this->view();
				break;
				
			case 'viewcontact':
				$this->view_contact();
				break;
				
			case 'viewnote':
				$this->view_note();
				break;
				
			case 'remove':
				$this->remove();
				break;
				
				
			//-------------------------
			default:
				$this->list_current();
				break;
		}
		
	}
	
	//---------------------------------------------
	// View NOTE in da pop up innit
	//---------------------------------------------
	
	function view_note()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試。");
		}
		
		require( ROOT_PATH.'sources/lib/post_parser.php');
        
        $this->parser  = new post_parser(1);
		
		$id = intval($IN['id']);
		
		$DB->query("SELECT l.*, m.uid as a_id, m.uname as a_name, p.uid as p_id, p.uname as p_name
						FROM ibf_warn_logs l
						  LEFT JOIN ibf_members m ON (m.uid=l.wlog_mid)
						  LEFT JOIN ibf_members p ON (p.uid=l.wlog_addedby)
					    WHERE l.wlog_id=$id");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試 ($id)");
		}
		
		$SKIN->td_header[] = array( "&nbsp;" , "100%" );
		
		$content = preg_match( "#<content>(.+?)</content>#is", $row['wlog_notes'], $cont );
		
		$ADMIN->html .= $SKIN->start_table( "警告備註" );
	
		$row['date']  = $ADMIN->get_date( $row['wlog_date'], 'LONG' );
		
		$text   = $this->parser->convert( array(
													'TEXT'    => $cont[1],
													'SMILIES' => 1,
													'CODE'    => 1,
													'HTML'    => 0
										   )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array(
													"<strong>來自：</strong> {$row['p_name']}
													<br /><strong>給：</strong> {$row['a_name']}
													<br /><strong>寄送：</strong> {$row['date']}
													<hr>
													<br />$text
												    "
										 )      );
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
	
	
	}
	
	
	//---------------------------------------------
	// View contact in da pop up innit
	//---------------------------------------------
	
	function view_contact()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試。");
		}
		
		$id = intval($IN['id']);
		
		$DB->query("SELECT l.*, m.uid as a_id, m.uname as a_name, p.uid as p_id, p.uname as p_name
						FROM ibf_warn_logs l
						  LEFT JOIN ibf_members m ON (m.uid=l.wlog_mid)
						  LEFT JOIN ibf_members p ON (p.uid=l.wlog_addedby)
					    WHERE l.wlog_id=$id");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("無法解析該電子郵件 ID，請重試 ($id)");
		}
		
		$type = $row['wlog_contact'] == 'pm' ? "PM" : "EMAIL";
		
		$SKIN->td_header[] = array( "&nbsp;" , "100%" );
		
		$subject = preg_match( "#<subject>(.+?)</subject>#is", $row['wlog_contact_content'], $subj );
		$content = preg_match( "#<content>(.+?)</content>#is", $row['wlog_contact_content'], $cont );
		
		$ADMIN->html .= $SKIN->start_table( $type.": ".$subj[1] );
	
		
		
		$row['date'] = $ADMIN->get_date( $row['wlog_date'], 'LONG' );
		
		$ADMIN->html .= $SKIN->add_td_row( array(
													"<strong>來自：</strong> {$row['p_name']}
													<br /><strong>給：</strong> {$row['a_name']}
													<br /><strong>寄送：</strong> {$row['date']}
													<br /><strong>標題：</strong> $subj[1]
													<hr>
													<br />$cont[1]
												    "
										 )      );
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->print_popup();
	
	
	}
	
	
	
	
	//---------------------------------------------
	// Remove archived files
	//---------------------------------------------
	
	function view()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$start = $IN['st'] ? $IN['st'] : 0;
		
		$ADMIN->html .= $SKIN->js_pop_win();
		
		$ADMIN->page_detail = "檢視某會員的所有警告條目";
		$ADMIN->page_title  = "警告日誌經理";
	
		if ($IN['search_string'] == "")
		{
			$DB->query("SELECT COUNT(wlog_id) as count FROM ibf_warn_logs WHERE wlog_mid='".$IN['mid']."'");
			$row = $DB->fetch_row();
			
			$row_count = $row['count'];
			
			$query = "&act=warnlog&mid={$IN['mid']}&code=view";
			
			$DB->query("SELECT l.*, m.uid as a_id, m.uname as a_name, p.uid as p_id, p.uname as p_name
						FROM ibf_warn_logs l
						  LEFT JOIN ibf_members m ON (m.uid=l.wlog_mid)
						  LEFT JOIN ibf_members p ON (p.uid=l.wlog_addedby)
					    WHERE l.wlog_mid={$IN['mid']}
					    ORDER BY l.wlog_date DESC LIMIT $start,30");	
		}
		else
		{
			$IN['search_string'] = urldecode($IN['search_string']);
			
			if ( ($IN['search_type'] == 'notes') )
			{
				$dbq = "l.wlog_notes LIKE '%".$IN['search_string']."%'";
			}
			else
			{
				$dbq = "l.wlog_contact_content LIKE '%".$IN['search_string']."%'";
			}
		
			$DB->query("SELECT COUNT(l.wlog_id) as count FROM ibf_warn_logs l WHERE $dbq");
			$row = $DB->fetch_row();
			
			$row_count = $row['count'];
			
			$query = "&act=warnlog&code=view&search_type={$IN['search_type']}&search_string=".urlencode($IN['search_string']);
			
			$DB->query("SELECT l.*, m.uid as a_id, m.uname as a_name, p.uid as p_id, p.uname as p_name
						FROM ibf_warn_logs l
						  LEFT JOIN ibf_members m ON (m.uid=l.wlog_mid)
						  LEFT JOIN ibf_members p ON (p.uid=l.wlog_addedby)
					    WHERE $dbq
					    ORDER BY l.wlog_date DESC LIMIT $start,30");	
		}
		
		$links = $std->build_pagelinks( array( 'TOTAL_POSS'  => $row_count,
											   'PER_PAGE'    => 30,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "單頁",
											   'L_MULTI'     => "分頁： ",
											   'BASE_URL'    => $ADMIN->base_url.$query,
											 )
									  );
									  
		$ADMIN->page_detail = "您可以檢視由您的板主所增加的警告條目";
		$ADMIN->page_title  = "警告日誌經理";
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "類型"        , "5%" );
		$SKIN->td_header[] = array( "會員名稱" , "15%" );
		$SKIN->td_header[] = array( "已連絡"   , "5%" );
		$SKIN->td_header[] = array( "管制佇列"       , "10%" );
		$SKIN->td_header[] = array( "停權"        , "10%" );
		$SKIN->td_header[] = array( "禁止發表"     , "10%" );
		$SKIN->td_header[] = array( "日期"        , "15%" );
		$SKIN->td_header[] = array( "警告者"   , "15%" );
		$SKIN->td_header[] = array( "檢視備註"   , "10%" );
		
		$ADMIN->html .= $SKIN->start_table( "儲存警告日誌" );
		$ADMIN->html .= $SKIN->add_td_basic($links, 'right', 'pformstrip');
		
		$days = array( 'd' => "Days", 'h' => "Hours" );
		
		if ( $DB->get_num_rows() )
		{
			while ( $row = $DB->fetch_row() )
			{
			
				$row['wlog_date'] = $ADMIN->get_date( $row['wlog_date'], 'LONG' );
				
				$type = ( $row['wlog_type'] == 'pos' )      ? '<span style="color:green;font-weight:bold">-</span>' : '<span style="color:red;font-weight:bold">+</span>';
				$cont = ( $row['wlog_contact'] !=  'none' ) ? "<center><a href='javascript:pop_win(\"&act=warnlog&code=viewcontact&id={$row['wlog_id']}\",400,400)'>檢視</a></center>" : '&nbsp;';
				
				$mod     = preg_match( "#<mod>(.+?)</mod>#is"        , $row['wlog_notes'], $mm );
				$post    = preg_match( "#<post>(.+?)</post>#is"      , $row['wlog_notes'], $pm );
				$susp    = preg_match( "#<susp>(.+?)</susp>#is"      , $row['wlog_notes'], $sm );
				$content = preg_match( "#<content>(.+?)</content>#is", $row['wlog_notes'], $cm );
				
				$content = $cm[1];
				
				$mod  = trim($mm[1]);
				$post = trim($pm[1]);
				$susp = trim($sm[1]);
				
				list($v, $u, $i) = explode(',', $mod);
				
				if ( $i == 1 )
				{
					$mod = 'INDEF';
				}
				else if ( $v == "" )
				{
					$mod = '無';
				}
				else
				{
					$mod = $v.' '.$days[$u];
				}
				
				//----------
				
				list($v, $u, $i) = explode(',', $post);
				
				if ( $i == 1 )
				{
					$post = 'INDEF';
				}
				else if ( $v == "" )
				{
					$post = '無';
				}
				else
				{
					$post = $v.' '.$days[$u];
				}
				
				list($v, $u) = explode(',', $susp);
				
				if ( $v == "" )
				{
					$susp = '無';
				}
				else
				{
					$susp = $v.' '.$days[$u];
				}
				
				//----------
		
				$ADMIN->html .= $SKIN->add_td_row( array(
														  "<center>$type</center>",
														  "<b>{$row['a_name']}</b>",
														  $cont,
														  $mod,
														  $susp,
														  $post,
														  "{$row['wlog_date']}",
														  "<b>{$row['p_name']}</b>",
														  "<center><a href='javascript:pop_win(\"&act=warnlog&code=viewnote&id={$row['wlog_id']}\",400,400)'>檢視</a></center>"
												 )      );
			}
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_basic("<center>無結果</center>");
		}
		
		$ADMIN->html .= $SKIN->add_td_basic($links, 'right', 'pformstrip');
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
		
	}
	
	//---------------------------------------------
	// Remove archived files
	//---------------------------------------------
	
	function remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
	
		if ($IN['mid'] == "")
		{
			$ADMIN->error("您沒有選擇要移除的會員 ID");
		}
		
		$DB->query("DELETE FROM ibf_warn_logs WHERE wlog_mid={$IN['mid']}");
		
		$ADMIN->save_log("移除的警告日誌");
		
		$std->boink_it($ADMIN->base_url."&act=warnlog");	
	}
	
	

	
	
	//-------------------------------------------------------------
	// SHOW LOGS
	//-------------------------------------------------------------
	
	function list_current()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$form_array = array();
	
		$ADMIN->page_detail = "您可以檢視及移除由您的同事所完成的警告動作。<br />注意：移除此日誌不會減低會員的警告層級";
		$ADMIN->page_title  = "警告日誌經理";

		
		//+-------------------------------
		// VIEW LAST 5
		//+-------------------------------
		
		$DB->query("SELECT l.*, m.uid as a_id, m.uname as a_name, p.uid as p_id, p.uname as p_name
				     FROM ibf_warn_logs l
				     LEFT JOIN ibf_members m ON (m.uid=l.wlog_mid)
				     LEFT JOIN ibf_members p ON (p.uid=l.wlog_addedby)
				    ORDER BY l.wlog_date DESC LIMIT 0,10");
		
		$SKIN->td_header[] = array( "類型"            , "5%" );
		$SKIN->td_header[] = array( "被警告的會員"   , "25%" );
		$SKIN->td_header[] = array( "已知會"      , "5%" );
		$SKIN->td_header[] = array( "日期"            , "25%" );
		$SKIN->td_header[] = array( "警告者"       , "25%" );

		$ADMIN->html .= $SKIN->start_table( "最後 10 個警告條目" );

		if ( $DB->get_num_rows() )
		{
			while ( $row = $DB->fetch_row() )
			{
			
				$row['wlog_date'] = $ADMIN->get_date( $row['wlog_date'], 'LONG' );
				
				$type = ( $row['wlog_type'] == 'pos' ) ? '<span style="color:green;font-weight:bold">-</span>' : '<span style="color:red;font-weight:bold">+</span>';
				$cont = ( $row['wlog_contact'] !=  'none' ) ? "<center><img src='{$SKIN->img_url}/acp_check.gif' border='0' alt='X'></center>" : '&nbsp;';
				
				$ADMIN->html .= $SKIN->add_td_row( array(
														  "<center>$type</center>",
														  "<b>{$row['a_name']}</b>",
														  $cont,
														  "{$row['wlog_date']}",
														  "<b>{$row['p_name']}</b>",
												 )      );
			}
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_basic("<center>無結果</center>");
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "會員名稱"            , "30%" );
		$SKIN->td_header[] = array( "警告時間"           , "20%" );
		$SKIN->td_header[] = array( "根據會員全部檢視"     , "20%" );
		$SKIN->td_header[] = array( "根據會員全部移除"   , "30%" );
		
		$ADMIN->html .= $SKIN->start_table( "儲存的警告日誌" );
		
		$DB->query("SELECT l.*, m.uname, count(l.wlog_mid) as act_count from ibf_warn_logs l, ibf_members m WHERE m.uid=l.wlog_mid GROUP BY l.wlog_mid ORDER BY act_count DESC");
		
		while ( $r = $DB->fetch_row() )
		{
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>{$r['uname']}</b>",
													  "<center>{$r['act_count']}</center>",
													  "<center><a href='".$SKIN->base_url."&act=warnlog&code=view&mid={$r['wlog_mid']}'>檢視</a></center>",
													  "<center><a href='".$SKIN->base_url."&act=warnlog&code=remove&mid={$r['wlog_mid']}'>移除</a></center>",
											 )      );
		}
			
		
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'view'     ),
												  2 => array( 'act'   , 'warnlog'       ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		$ADMIN->html .= $SKIN->start_table( "搜尋警告日誌" );
		
		$form_array = array(
							  0 => array( 'notes'  , '條目備註' ),
							  1 => array( 'contact', '寄送的電子郵件/PM'  ),
						   );
			
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜尋...</b>" ,
										  		  $SKIN->form_input( "search_string")
								 )      );
								 
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搜尋於...</b>" ,
										  		  $SKIN->form_dropdown( "search_type", $form_array)
								 )      );
		
		$ADMIN->html .= $SKIN->end_form("Search");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		//+-------------------------------
		
		$ADMIN->output();
	
	}
	
	
	
}


?>
