<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.3 Final
|   ========================================
|   by Matthew Mecham
|   (c) 2001 - 2003 Invision Power Services
|   http://www.invisionpower.com
|   ========================================
|   Web: http://www.invisionboard.com
|   Time: Wed, 21 Jan 2004 09:54:34 GMT
|   Release: 2c4ce01a2d8aa60f718f2246a5cd4a18
|   Email: matt@invisionpower.com
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > Admin Member Tool functions
|   > Module written by Matt Mecham
|   > Date started: 17th September 2003
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/

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

$idx = new ad_member_tools();

$root_path = "";

class ad_member_tools {

	var $base_url;
	var $modules = "";

	function ad_member_tools()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $ibforums;
		
		//---------------------------------------
		// 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=mtools', '會員工具首頁' );
		
		//---------------------------------------
		
		$ADMIN->page_title  = "會員工具箱";
		$ADMIN->page_detail = '您可以使用以下的工具來搜尋 IP 位址。';

		switch($IN['code'])
		{
			
			case 'showallips':
				$this->show_ips();
				break;
				
			case 'learnip':
				$this->learn_ip();
				break;
			
			//---------------------
			default:
				$this->show_index();
				break;
		}
		
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// LEARN ABOUT THE IP. It's very good.
	//
	//+---------------------------------------------------------------------------------
	
	
	function learn_ip()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ( $IN['ip'] == "" )
		{
			$this->show_index("你沒有輸入需要搜尋的  IP 位址");
		}
		
		$ip = $IN['ip'];
		
		$resolved = 'N/A - Partial IP Address';
		$exact    = 0;
		
		if ( substr_count( $ip, '.' ) == 3 )
		{
			$exact = 1;
		}
		
		if ( strstr( $ip, '*' ) )
		{
			$exact = 0;
			$ip    = str_replace( "*", "", $ip );
		}
			
		if ( $exact != 0 )
		{
			$resolved = gethostbyaddr($ip);
			$query    = "='".$ip."'";
		}
		else
		{
			$query    = " LIKE '".$ip."%'";
		}
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "{$IN['ip']} 的主機位址" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>IP 位址解析為</b>" ,
												  $resolved
										  )      );
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+--------------------------------------------------
		// Find registered members
		//+--------------------------------------------------
		
		$SKIN->td_header[] = array( "名稱"       , "30%" );
		$SKIN->td_header[] = array( "電子郵件"      , "20%" );
		$SKIN->td_header[] = array( "發表數"      , "10%" );
		$SKIN->td_header[] = array( "IP 位址"         , "20%" );
		$SKIN->td_header[] = array( "註冊日期" , "20%" );
		
		$ADMIN->html .= $SKIN->start_table( "註冊時使用該 IP 的會員" );
		
		$DB->query("SELECT uid, uname, email, posts, ip_address, user_regdate FROM ibf_members WHERE ip_address{$query} ORDER BY user_regdate DESC LIMIT 250");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->html .= $SKIN->add_td_basic( "找不到符合的結果", "center");
		}
		else
		{
			while ( $m = $DB->fetch_row() )
			{
				
				$ADMIN->html .= $SKIN->add_td_row( array( $m['uname'] ,
														  $m['email'],
														  $m['posts'],
														  $m['ip_address'],
														  $std->get_date( $m['user_regdate'], 'SHORT' )
												 )      );
			}
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+--------------------------------------------------
		// Find Names posted under
		//+--------------------------------------------------
		
		$SKIN->td_header[] = array( "名稱"       , "20%" );
		$SKIN->td_header[] = array( "電子郵件"      , "20%" );
		$SKIN->td_header[] = array( "IP 位址"         , "15%" );
		$SKIN->td_header[] = array( "第一次使用"  , "20%" );
		$SKIN->td_header[] = array( "檢視文章"  , "15%" );
		
		$ADMIN->html .= $SKIN->start_table( "發表時使用該 IP 的會員" );
		
		$DB->query("SELECT m.uid, m.uname, m.email, m.posts, m.user_regdate, p.pid, p.author_id, p.post_date, p.ip_address, p.topic_id
					FROM ibf_posts p
					 LEFT JOIN ibf_members m ON ( p.author_id=m.uid)
					WHERE p.ip_address{$query} GROUP BY p.author_id ORDER BY p.post_date DESC LIMIT 250");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->html .= $SKIN->add_td_basic( "找不到符合的結果", "center");
		}
		else
		{
			while ( $m = $DB->fetch_row() )
			{
				$ADMIN->html .= $SKIN->add_td_row( array( $m['uname'] ,
														  $m['email'],
														  $m['ip_address'],
														  $std->get_date( $m['post_date'], 'SHORT' ),
														  "<center><a href='index.php?showtopic={$m['topic_id']}&view=findpost&p={$m['pid']}' target='_blank'>檢視文章</a></center>",
												 )      );
			}
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+--------------------------------------------------
		// Find Names VOTED under
		//+--------------------------------------------------
		
		$SKIN->td_header[] = array( "名稱"       , "20%" );
		$SKIN->td_header[] = array( "電子郵件"      , "20%" );
		$SKIN->td_header[] = array( "IP 位址"         , "15%" );
		$SKIN->td_header[] = array( "第一次使用"  , "20%" );
		$SKIN->td_header[] = array( "檢視投票" , "15%" );
		
		$ADMIN->html .= $SKIN->start_table( "投票時使用該 IP 的會員" );
		
		$DB->query("SELECT m.uid, m.uname, m.email, m.posts, m.user_regdate, p.vote_date, p.ip_address, p.tid
					FROM ibf_voters p
					 LEFT JOIN ibf_members m ON ( p.member_id=m.uid)
					WHERE p.ip_address{$query} GROUP BY p.member_id ORDER BY p.vote_date DESC LIMIT 250");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->html .= $SKIN->add_td_basic( "找不到符合的結果", "center");
		}
		else
		{
			while ( $m = $DB->fetch_row() )
			{
				$ADMIN->html .= $SKIN->add_td_row( array( $m['uname'] ,
														  $m['email'],
														  $m['ip_address'],
														  $std->get_date( $m['vote_date'], 'SHORT' ),
														  "<center><a href='index.php?showtopic={$m['tid']}' target='_blank'>檢視投票</a></center>",
												 )      );
			}
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+--------------------------------------------------
		// Find Names EMAILING under
		//+--------------------------------------------------
		
		$SKIN->td_header[] = array( "名稱"       , "20%" );
		$SKIN->td_header[] = array( "電子郵件"      , "20%" );
		$SKIN->td_header[] = array( "IP 位址"         , "15%" );
		$SKIN->td_header[] = array( "第一次使用"  , "20%" );
		
		$ADMIN->html .= $SKIN->start_table( "發送電子郵件給其他會員時使用該 IP 的會員" );
		
		$DB->query("SELECT m.uid, m.uname, m.email, m.posts, m.user_regdate, p.email_date, p.from_ip_address
					FROM ibf_email_logs p
					 LEFT JOIN ibf_members m ON ( p.from_member_id=m.uid)
					WHERE p.from_ip_address{$query} GROUP BY p.from_member_id ORDER BY p.email_date DESC LIMIT 250");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->html .= $SKIN->add_td_basic( "找不到符合的結果", "center");
		}
		else
		{
			while ( $m = $DB->fetch_row() )
			{
				$ADMIN->html .= $SKIN->add_td_row( array( $m['uname'] ,
														  $m['email'],
														  $m['from_ip_address'],
														  $std->get_date( $m['email_date'], 'SHORT' ),
												 )      );
			}
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		//+--------------------------------------------------
		// Find Names VALIDATING under
		//+--------------------------------------------------
		
		$SKIN->td_header[] = array( "名稱"       , "20%" );
		$SKIN->td_header[] = array( "電子郵件"      , "20%" );
		$SKIN->td_header[] = array( "IP 位址"         , "15%" );
		$SKIN->td_header[] = array( "第一次使用"  , "20%" );
		
		$ADMIN->html .= $SKIN->start_table( "註冊認證時使用該 IP 的會員" );
		
		$DB->query("SELECT m.uid, m.uname, m.email, m.posts, m.user_regdate, p.entry_date, p.ip_address
					FROM ibf_validating p
					 LEFT JOIN ibf_members m ON ( p.member_id=m.uid)
					WHERE p.ip_address{$query} GROUP BY p.member_id ORDER BY p.entry_date DESC LIMIT 250");
		
		if ( ! $DB->get_num_rows() )
		{
			$ADMIN->html .= $SKIN->add_td_basic( "找不到符合的結果", "center");
		}
		else
		{
			while ( $m = $DB->fetch_row() )
			{
				$ADMIN->html .= $SKIN->add_td_row( array( $m['uname'] ,
														  $m['email'],
														  $m['ip_address'],
														  $std->get_date( $m['entry_date'], 'SHORT' ),
												 )      );
			}
		}
		
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//+---------------------------------------------------------------------------------
	//
	// SHOW ALL IPs
	//
	//+---------------------------------------------------------------------------------
	
	
	function show_ips()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ( $IN['name'] == "" and $IN['member_id'] == "" )
		{
			$this->show_index("您沒有輸入需要搜尋的名稱");
		}
		
		if ( $IN['member_id'] )
		{
			$id = intval($IN['member_id']);
			
			$DB->query("SELECT uid, uname, email, ip_address FROM ibf_members WHERE uid=$id");
			
			if ( ! $member = $DB->fetch_row() )
			{
				$this->show_index("無法找到會員的 id 是 '$id'");
			}
		}
		else
		{
			$name = addslashes($IN['name']);
			
			$DB->query("SELECT uid, uname, email, ip_address FROM ibf_members WHERE uname='$name'");
			
			if ( ! $member = $DB->fetch_row() )
			{
				$this->show_index( "我們無法找到和該會員名稱完全相符的結果，以下是一些選擇", $name );
			}
		}
		
		$DB->query("SELECT count(distinct(ip_address)) as cnt FROM ibf_posts WHERE author_id={$member['uid']}");
		
		$count = $DB->fetch_row();
		
		$st  = intval($IN['st']);
		$end = 50;
		
		$links = $std->build_pagelinks( array( 'TOTAL_POSS'  => $count['cnt'],
											   'PER_PAGE'    => $end,
											   'CUR_ST_VAL'  => $st,
											   'L_SINGLE'    => "單頁",
											   'L_MULTI'     => "分頁:",
											   'BASE_URL'    => $SKIN->base_url."&act=mtools&code=showallips&member_id={$member['uid']}",
									  )      );
		
		$master = array();
		$ips    = array();
		
		$DB->query("SELECT count(ip_address) as ip, ip_address, pid, topic_id, post_date FROM ibf_posts WHERE author_id={$member['uid']} GROUP BY ip_address ORDER BY ip DESC LIMIT $st, $end");
		
		while ( $r = $DB->fetch_row() )
		{
			$master[] = $r;
			$ips[]    = '"'.$r['ip_address'].'"';
		}
		
		$reg = array();
		
		if ( count($ips) > 0 )
		{
			$DB->query("SELECT uid, uname, ip_address FROM ibf_members WHERE ip_address IN (".implode(",",$ips).") AND uid != {$member['uid']}");
			
			while ( $i = $DB->fetch_row() )
			{
				$reg[ $i['ip_address'] ][] = $i;
			}
		}
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "IP 位址"          , "20%" );
		$SKIN->td_header[] = array( "使用次數"          , "10%" );
		$SKIN->td_header[] = array( "使用日期"           , "25%" );
		$SKIN->td_header[] = array( "使用於其他註冊" , "20%" );
		$SKIN->td_header[] = array( "IP 工具"             , "25%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "{$member['uname']} 有 ({$count['cnt']}) 個符合的 IP 位址" );
		
		foreach( $master as $idx => $r )
		{
			$ADMIN->html .= $SKIN->add_td_row( array( $r['ip_address'] ,
													  $r['ip'] ,
													  $std->get_date( $r['post_date'], 'SHORT' ),
													  "<center>". intval( count($reg[ $r['ip_address'] ]) ). "</center>",
													  "<center><a href='{$ADMIN->base_url}&act=mtools&code=learnip&ip={$r['ip_address']}'>知道更多關於這個 IP</a></center>"
											 )      );
		}
		
		$ADMIN->html .= $SKIN->add_td_basic( "$links", "center", "catrow2");
									     							     
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	//+---------------------------------------------------------------------------------
	//
	// Default Screen
	//
	//+---------------------------------------------------------------------------------
	
	
	function show_index($msg="", $membername="")
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		
		if ($msg != "")
		{
			$SKIN->td_header[] = array( "&nbsp;"  , "100%" );
			
			$ADMIN->html .= $SKIN->start_table( "訊息" );
			
			$ADMIN->html .= $SKIN->add_td_row( array( $msg ) );
			
			$ADMIN->html .= $SKIN->end_table();
		}
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'showallips'  ),
												  2 => array( 'act'   , 'mtools'     ),
									     )      );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "顯示一位會員發表時所有曾經使用的 IP 位址" );
		
		if ( $membername == "" )
		{
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>輸入會員名稱</b>" ,
													  $SKIN->form_input( "name", $std->txt_stripslashes($HTTP_POST_VARS['name']) )
											 )      );
		}
		else
		{
			$DB->query("SELECT uid, uname FROM ibf_members WHERE lower(uname) LIKE '{$membername}%'");
			
			if ( ! $DB->get_num_rows() )
			{
				$this->show_index("沒有任何會員名稱以 '$membername' 作為開始");
			}
			
			$mem_array = array();
			
			while ( $m = $DB->fetch_row() )
			{
				$mem_array[] = array( $m['uid'], $m['uname'] );
			}
			
			$ADMIN->html .= $SKIN->add_td_row( array( "<b>選擇會員</b>" ,
													  $SKIN->form_dropdown( "member_id", $mem_array )
											 )      );
		}
		
		$ADMIN->html .= $SKIN->end_form("獲取 IP 位址");
									     							     
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'learnip'  ),
												  2 => array( 'act'   , 'mtools'     ),
									     )      );
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "IP 多重工具" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>顯示關於這個 IP 位址的所有資料...</b>" ,
												   $SKIN->form_input( "ip", $std->txt_stripslashes($HTTP_POST_VARS['ip']) )
										  )      );
										  
		$ADMIN->html .= $SKIN->end_form("顯示!");
									     							     
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	}
	
	
	
	
	
	
	
}

?>