<?php
header("Content-Type: text/html; charset=UTF-8");
header('P3P: CP=CAO PSA OUR');
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();

$initscript = '';

$methodname = 'WebVisitor.BeginSession';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
	'<openid>' . strtoxml($openid) . '</openid>' .
	'<visitorcode>' . strtoxml($visitorcode) . '</visitorcode>' .
	'<sessionid>' . strtoxml($sessionid) . '</sessionid>' . 
	'<userid>' . strtoxml($userid) . '</userid>' . 
	'<srcurl>' . strtoxml($srcurl) . '</srcurl>' . 
	'<clientipaddr>' . strtoxml($clientipaddr) . '</clientipaddr>' . 
	'<clientlocation>' . strtoxml($clientlocation) . '</clientlocation>' . 
	'<clientos>' . strtoxml($clientos) . '</clientos>' . 
	'<webbrowser>' . strtoxml($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, 1000)) {
	$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;

	$initscript = 
		'<script language="javascript">' .
		'openid = "' . $openid . '";' .
		'visitorid = "' . $visitorid . '";' .
		'sessionid = "' . $sessionid . '";' .
		'userid = ' . $userid . ';' .
		'showname = "' . $showname . '";' .
		'function initSession() { ' .
	    'g_oldtitle = document.title; ' .
		'var objTitle = window.parent.document.getElementById("webvisitordialog_title");' .
		'if (objTitle != "undefined") objTitle.innerHTML = "' . $showname . '";';

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

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

	$initscript .= ' };</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>fff</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<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>
<style type="text/css">
html, body {
margin:0;
padding:0;
width:100%;
height:100%;
}
body {
border:none;
overflow:hidden;
font-family:SimHei; 
font-size:11pt; 
}
div, ul, li { 
margin:0;
padding:0;
overflow:hidden; 
}
input, span {   
vertical-align: middle;
}
table {
margin:0;
padding:0;
}
a {
color:#0000cc; 
text-decoration:underline;
vertical-align: middle;  
}
img {
margin:0;
padding:0;
border:0;
vertical-align: middle;
}
#msgbox {
clear:both;
margin:0;
padding:0 0 20px 0;
width:550px;
height:242px;
overflow-x:hidden;
overflow-y:auto;
background-color:#ffffff;
font-family:"Arial";
}
#notice {
clear:both;
margin:0;
padding:7px;
font-family:"Arial";
font-size:12px;
color:#9d9d9d;
background:#ffffcc;
}
#toolbar {
margin:0;
padding:0;
height:20px;
font-size:9pt;
color:#72a0d3;
background:#f1f1f1;
}
.IconBtn {
margin:2px 0 0 0;
padding:2px 0 0 0;
width:26px;
text-align:center;
float:left;
cursor:pointer; 
}
#FontSelectWin {
position:absolute;
left:0;
top:0;
padding:2px 2px 0 2px;
width:201px;
height:60px;
border:1px solid #000000;
background-color:#F0F8FB;
display:none;
overflow:hidden;
}
#EmotionSelectWin {
position:absolute;
left:0;
top:0;
padding:2px 2px 0 2px;
width:322px;
height:178px;
border:1px solid #000000;
background-color:#F0F8FB;
display:none;
overflow:hidden;
}
#ServiceEvaluteWin {
position:absolute;
left:0;
top:0;
padding:2px 2px 0 2px;
width:420px;
height:60px;
border:1px solid #000000;
background-color:#F0F8FB;
display:none;
overflow:hidden;
}
#inputbox {
height:60px;
}
#content {
margin:0;
padding:4px;
border:0;
overflow:hidden;
background-color:#fff;
color: #000;
font-family:"Arial";
font-size: 11pt;
line-height:11pt;
outline:0;
resize:none;
word-wrap:break-word;
word-break:break-all;
}
#inputbtn {
height:27px;
background:#f1f1f1;
}
#inputInfo {
margin:0;
padding:7px 0 0 5px;
font-family:"Arial";
font-size:12px;
color:#9d9d9d;
}
#sendTypeText {
float:left;
margin:0;
padding:7px 0 0 0;
width:140px;
font-family:"Arial";
font-size:12px;
color:#9d9d9d;
text-align:center;
}
.notice{ padding-left:10px;height:30px; background:#ffffcc; line-height:30px;position:relative;}
.notice .about{ float:right; padding-right:30px; position:relative;}
.notice .about a{ color:#59a541; text-decoration:underline;}
.notice .about a.close{ width:16px; height:30px; background:url(images/close.gif) no-repeat 0 7px; display:block; position:absolute; right:5px; top:0;}
</style>
</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>

<table id="client" width="550px" height="420px" cellSpacing="0" cellPadding="0" border="0">
<tr>
	<td id="chatbox" colspan="2" valign="top">
		<div id="msgbox">
			<div id="notice">安装FastMsg客户端，交易更安全<a href="javascript:void(0);" target="_blank" style="float:right;color:#59a541">下载FastMsg</a><a href="javascript:void(0);" class="close" id="close_tip"></a></div>
		</div>
	</td>
</tr>
<tr>
	<td id="toolbar" colspan="2" valign="top">
		<div id="EmotionBtn" class="IconBtn" onmouseup="SelectEmotions();return false;">
			<img width="16px" height="16px" border="0" src="images/emotion.ico" title="聊天表情" />
		</div>
	</td>
</tr>
<tr>
	<td id="inputbox" colspan="2" valign="top">
		<textarea id="content" onkeyup="checkKeyUp();"></textarea>
	</td>
</tr>
<tr>
	<td id="inputbtn" colspan="2" valign="top">
		<div style="float:left;"><p id="inputInfo">您可以输入1000字</p></div>
		<div style="float:right;margin:0;padding:0;width:235px;">
			<div id="sendTypeText">按 Enter 键发送</div>
			<div style="float:left;margin:2px 0 0 0;padding:0;"><img width="65px" height="23px" border="0" src="images/send.png" style="cursor:pointer;" onclick="sendMessage();"/></div>
			<div style="float:left;margin:2px 0 0 5px;padding:0;"><img width="14px" height="23px" border="0" src="images/send2.png" style="cursor:pointer;" onclick="changeSendType();"/></div>
		</div>
	</td>
</tr>
</table>

<?php echo $initscript; ?>

<script language="javascript">
function realignControls() {
var ClientWidth = parseInt(document.body.offsetWidth);
var ClientHeight = parseInt(document.body.offsetHeight);
var obj = document.getElementById("client");
obj.style.width = ClientWidth + "px";
obj.style.height = ClientHeight + "px";
var obj = document.getElementById("chatbox");
obj.style.width = ClientWidth + "px";
obj.style.height = (ClientHeight - 24 - 60 - 27) + "px";
var obj = document.getElementById("msgbox");
obj.style.width = ClientWidth + "px";
obj.style.height = (ClientHeight - 20 - 24 - 60 - 27) + "px";
var obj = document.getElementById("toolbar");
obj.style.width = ClientWidth + "px";
obj.style.height = 24 + "px";
var obj = document.getElementById("inputbox");
obj.style.width = ClientWidth + "px";
obj.style.height = 60 + "px";
var obj = document.getElementById("content");
obj.style.width = ClientWidth + "px";
obj.style.height = 50 + "px";
var obj = document.getElementById("inputbtn");
obj.style.width = ClientWidth + "px";
obj.style.height = 27 + "px";
}
window.onload = function() {
  realignControls();
  initSession();
  OnPageLoad();
};
window.onresize = function() { 
	realignControls(); 
};
document.onkeydown = function() { 
	clearblinktitle(); 
};
document.onmousedown = onmousedown;  // for all others
</script>

</body>
</html>
