<?php
header("Content-Type: text/html; charset=UTF-8");
session_start();

require("common.php");

function create_openid() {
    return strtoupper(md5(uniqid(mt_rand(), true)));
}

function create_visitorcode() {
    return strtoupper(md5(uniqid(mt_rand(), true)));
}

function getip(){   
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))   
           $ip = getenv("HTTP_CLIENT_IP");   
       else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))   
           $ip = getenv("HTTP_X_FORWARDED_FOR");   
       else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))   
           $ip = getenv("REMOTE_ADDR");   
       else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))   
           $ip = $_SERVER['REMOTE_ADDR'];   
       else  
           $ip = "unknown";   
   return($ip);   
}

function getbrowser() {
	$Agent = $_SERVER['HTTP_USER_AGENT'];
	$browser = '';
	$browserver = '';
	$Browser = array('Lynx', 'MOSAIC', 'AOL', 'Opera', 'JAVA', 'MacWeb', 'WebExplorer', 'OmniWeb');
	for($i = 0; $i <= 7; $i ++)
	{
		if(strpos($Agent, $Browser[$i]))
		{
			$browser = $Browser[$i];
			$browserver = '';
		}
	}
	if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent))
	{
		$temp = explode('(', $Agent);
		$Part = $temp[0];
		$temp = explode('/', $Part);
		$browserver = $temp[1];
		$temp = explode(' ', $browserver);
		$browserver = $temp[0];
		$browserver = preg_replace('/([d.]+)/', '.', $browserver);
		$browserver = $browserver;
		$browser = 'Netscape Navigator';
	}
	if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent)) 
	{
		$temp = explode('(', $Agent);
		$Part = $temp[1];
		$temp = explode(')', $Part);
		$browserver = $temp[1];
		$temp = explode(' ', $browserver);
		$browserver = $temp[2];
		$browserver = preg_replace('/([d.]+)/', '.', $browserver);
		$browserver = $browserver;
		$browser = 'Opera';
	}
	if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent))
	{
		$temp = explode('(', $Agent);
		$Part = $temp[1];
		$temp = explode(';', $Part);
		$Part = $temp[1];
		$temp = explode(' ', $Part);
		$browserver = $temp[2];
		$browserver = preg_replace('/([d.]+)/','.',$browserver);
		$browserver = $browserver;
		$browser = 'Internet Explorer';
	}
	if($browser != '')
	{
		$browseinfo = $browser.' '.$browserver;
	} 
	else
	{
		$browseinfo = false;
	}
	return $browseinfo;
}

function getOS () {
	$agent = $_SERVER['HTTP_USER_AGENT'];
	$os = false;
	if (eregi('win', $agent) && strpos($agent, '95'))
	{
		$os = 'Windows 95';
	}
	else if (eregi('win 9x', $agent) && strpos($agent, '4.90'))
	{
		$os = 'Windows ME';
	}
	else if (eregi('win', $agent) && ereg('98', $agent))
	{
		$os = 'Windows 98';
	}
	else if (eregi('win', $agent) && eregi('nt 5.2', $agent))
	{
		$os = 'Windows Server 2003';
	}
	else if (eregi('win', $agent) && eregi('nt 5.1', $agent))
	{
		$os = 'Windows XP';
	}
	else if (eregi('win', $agent) && eregi('nt 5', $agent))
	{
		$os = 'Windows Server 2000';
	}
	else if (eregi('win', $agent) && eregi('nt', $agent))
	{
		$os = 'Windows NT';
	}
	else if (eregi('win', $agent) && ereg('32', $agent))
	{
		$os = 'Windows 32';
	}
	else if (eregi('linux', $agent))
	{
		$os = 'Linux';
	}
	else if (eregi('unix', $agent))
	{
		$os = 'Unix';
	}
	else if (eregi('sun', $agent) && eregi('os', $agent))
	{
		$os = 'SunOS';
	}
	else if (eregi('ibm', $agent) && eregi('os', $agent))
	{
		$os = 'IBM OS/2';
	}
	else if (eregi('Mac', $agent) && eregi('PC', $agent))
	{
		$os = 'Macintosh';
	}
	else if (eregi('PowerPC', $agent))
	{
		$os = 'PowerPC';
	}
	else if (eregi('AIX', $agent))
	{
		$os = 'AIX';
	}
	else if (eregi('HPUX', $agent))
	{
	$os = 'HPUX';
	}
	else if (eregi('NetBSD', $agent))
	{
		$os = 'NetBSD';
	}
	else if (eregi('BSD', $agent))
	{
		$os = 'BSD';
	}
	else if (ereg('OSF1', $agent))
	{
		$os = 'OSF1';
	}
	else if (ereg('IRIX', $agent))
	{
		$os = 'IRIX';
	}
	else if (eregi('FreeBSD', $agent))
	{
		$os = 'FreeBSD';
	}
	else if (eregi('teleport', $agent))
	{
		$os = 'teleport';
	}
	else if (eregi('flashget', $agent))
	{
		$os = 'flashget';
	}
	else if (eregi('webzip', $agent))
	{
		$os = 'webzip';
	}
	else if (eregi('offline', $agent))
	{
		$os = 'offline';
	}
	else
	{
		$os = 'Unknown';
	}
	return $os;
}

function srcurl() {
	if( !empty( $_SERVER['HTTP_REFERER'] ) )
	{
		return  $_SERVER['HTTP_REFERER'];
	}
	else
	{
		return "";
	}
}

$openid = create_openid();
$visitorcode = isset($_COOKIE["visitorcode"]) ? $_COOKIE["visitorcode"] : "";
$sessionid = session_id();
$userid = isset($_GET["userid"]) ? $_GET["userid"] : "0";
$srcurl = srcurl();
$clientipaddr = getip();
$clientlocation = "";
$clientos = getOS();
$webbrowser = getbrowser();

if ($visitorcode == "") $visitorcode = create_visitorcode();

if ($userid == "0") {
	die("无法访问！");
}

$script = '';

$methodname = 'WebVisitor.BeginSession';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
	'<openid>' . $openid . '</openid>' .
	'<visitorcode>' . $visitorcode . '</visitorcode>' .
	'<sessionid>' . $sessionid . '</sessionid>' . 
	'<userid>' . $userid . '</userid>' . 
	'<srcurl>' . $srcurl . '</srcurl>' . 
	'<clientipaddr>' . $clientipaddr . '</clientipaddr>' . 
	'<clientlocation>' . $clientlocation . '</clientlocation>' . 
	'<clientos>' . $clientos . '</clientos>' . 
	'<webbrowser>' . $webbrowser . '</webbrowser>' . 
	'</request> ';
$responseinfo = '';
$responsexml = null;
$errorcode = ERRORCODE_UNKNOWN;

$cmd = new COM("OpenCom.Command");
$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
	$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
	$responsexml = simplexml_load_string($responseinfo);
	$errorcode = $responsexml->errorcode;
}
$cmd = null;

if ($errorcode == ERRORCODE_OK) {
	$visitorid = $responsexml->visitorid;
	$showname = $responsexml->showname;

	$script = 
		'<script language="javascript">' .
		'openid = "' . $openid . '";' .
		'visitorid = "' . $visitorid . '";' .
		'sessionid = "' . $sessionid . '";' .
		'userid = ' . $userid . ';' .
		'showname = "' . $showname . '";' .
		'window.onload = function() { OnPageLoad(); g_oldtitle = document.title; ';

    $historymessagecount = 0;
	foreach ($responsexml->messages->item as $messageitem) {
		$historymessagecount += 1;
		if ($messageitem->senderisvisitor == '1') {
			$script .= 'addMessage(makeTalkingMessage("访客' . $visitorid . '", "' . 
				$messageitem->content . '", "' . $messageitem->createtime . '", true));';
		} else {
			$script .= 'addMessage(makeTalkingMessage("' . $showname . '", "' . 
				$messageitem->content . '", "' . $messageitem->createtime . '", true));';
		}//if
	}//foreach

	if ($historymessagecount > 0) {
		$script .= 'addMessage("<div style=\'clear:both;margin:2px 0 10px 0;padding:0;width:530px;height:10px;border-bottom:1px solid #d8d8d8;\'></div>");';
	}

	$script .= ' };</script>';

	setcookie("visitorcode", $visitorcode, time()+3600*24*365*10);
} else {
	die("无法访问！");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?php require("info/title.php"); ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<link rel="icon" href="favicon.ico" type="image/x-icon" />
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="css/main.css">
	<script type="text/javascript" src="js/actions.js"></script>
	<script type="text/javascript" src="js/XHConn.js"></script>
	<script type="text/javascript" src="js/emotions.js"></script>
</head>
<body scroll="no">

<div id="emotion_handler" style="display:none" onclick="javascript:emotion_handler_click(this);"></div>

<div id="EmotionSelectWin" style="margin:0;padding:0;display:none">
	<iframe id="EmotionSelectFrame" style="width:100%; height:100%" frameborder="0"></iframe>
</div>

<div id="ServiceEvaluteWin" style="margin:0;padding:0;display:none">
	<iframe id="ServiceEvaluteFrame" style="width:100%; height:100%" frameborder="0"></iframe>
</div>

<div class="nav">
	<div class="image"><img width="16px" height="16px" border="0" align="absmiddle" src="favicon.ico"/></div> 
	<div class="lefttext"><?php require("info/title.php"); ?></div>
	<div class="righttext"><?php require("info/copyright.php"); ?></div>
</div>

<div style="margin:0;padding:0;width:100%;height:50px;"></div>

<!--Box: BEGIN-->
<table id="box" class="flatbox" style="margin:0 auto;">
	<tr>
		<td class="flatbox_topleft"></td>
		<td id="box_top" class="flatbox_top"></td>
		<td class="flatbox_topright"></td>
	</tr>
	<tr>
		<td id="box_left" class="flatbox_left"></td>
		<td class="flatbox_client" valign="top">		
		<!--box_client: BEGIN-->
		<table id="client" width="550px" height="420px" cellSpacing="0" cellPadding="0" border="0">
		<tr>
			<td valign="top">
				<div style="clear:both;margin:0;padding:0;width:550px;height:29px;overflow:hidden;border-bottom: 1px solid #cceaf2;background-color:#ebf7fa;">
					<div style="float:left;margin:0;padding:0 5px 0 5px;width:20px;height:20px;">
						<img width="20px" height="20px" border="0" src="images/call.gif" />
					</div>
					<div style="float:left;margin:0;padding:2px 5px 0 5px;height:23px;font-size:11pt;font-weight:bold;color:#a6c4e4;">
						<div><?php echo '您正在与 ' . $showname . '(' . $userid . ') 对话...'; ?></div>
					</div>
				</div>
				<div id="Chatboard" style="clear:both;margin:0;padding:10px 10px 20px 10px;width:530px;height:232px;overflow-x:hidden;overflow-y:auto;background-color:#ffffff;">
					<div style="clear:both;margin:0;padding:0;height:20px;font-size:11pt;color:#00a2e8;">欢迎使用<?php require("info/title.php"); ?>。本次对话记录将会自动保存。</div>
				</div>
			</td>
		</tr>
		<tr>
			<td valign="top" class="toolbar">
				<div id="EmotionBtn" class="IconBtn" onmouseover="IconBtnMouseOver(this);" onmousedown="IconBtnMouseDown(this);" onmouseup="IconBtnMouseUp(this);SelectEmotions();" onmouseout="IconBtnMouseOut(this);">
					<img width="16px" height="16px" border="0" src="images/emotion.ico" title="聊天表情" />
				</div>
			</td>
		</tr>
		<tr>
			<td valign="top" height="100px">
				<table width="550px" height="100px" cellSpacing="0" cellPadding="0" border="0">
				<tr>
					<td>
						<textarea id="txtEdit" class="content" onkeyup="checkKeyUp();"></textarea>
					</td>
					<td width="100px" valign="top" align="center">
						<div style="margin:0;padding:12px 0 0 0;width:77px;height:50px;">
							<table width="77px" height="50px" cellSpacing="0" cellPadding="0" border="0">
							<tr>
								<td><img width="53px" height="50px" border="0" src="images/send.png" style="cursor:pointer;" onclick="sendMessage();"/></td>
								<td><img width="24px" height="50px" border="0" src="images/send2.png" style="cursor:pointer;" onclick="changeSendType();"/></td>
							</tr>
							</table>
						</div>
						<div id="sendTypeText">Enter</div>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
		<!--box_client: END-->					
		</td>
		<td id="box_right" class="flatbox_right"></td>
	</tr>
	<tr>
		<td class="flatbox_bottomleft"></td>
		<td id="box_bottom" class="flatbox_bottom"></td>
		<td class="flatbox_bottomright"></td>
	</tr>
</table>
<!--Box: END-->

<?php echo $script; ?>

</body>
</html>
