<?php
define('ROBOT',true);
include_once("include/common.inc.php");
//机器人信息
$botname=option('botname');
//生成唯一用户标识uid
if(!$uid){//管理员
  $cookie=_cookie('robot');
  list($uid,$username)=explode("\t",StrCode($cookie,'DECODE'));
  if(!$uid){
    $uid=$_SESSION['onez_userid'];
    if(!$uid){
      $uid='_'.uniqid('');
      $_SESSION['onez_userid']=$uid;
      $cookie=StrCode("$uid\tONEZ_ROBOT","ENCODE");
      _cookie('robot',$cookie);
    }
  }
}
InitGP('action');
//$action=DEBUG;
if($action){
  include_once("api/onez.php");
}
//获取上次访问信息，如果没有，自动生成
$H=DB()->one('history','*',"uid='$uid'");
if(!$H){
  $cometimes=1;
  DB()->insert('history',array(
    'uid'=>$uid,
    'firsttime'=>$time,
    'lasttime'=>$time,
    'firstip'=>$onlineip,
    'lastip'=>$onlineip,
    'cometimes'=>$cometimes,
  ));
  $hid=DB()->insert_id();
}else{
  foreach($H as $k=>$v)$$k=$v;
  if(!$_SESSION['cometimes']){
    $cometimes++;
    $_SESSION['cometimes']=$cometimes;
  }
  DB()->update('history',array(
    'uid'=>$uid,
    'lasttime'=>$time,
    'lastip'=>$onlineip,
    'cometimes'=>$cometimes,
  ),"hid='$hid'");
}

include_once('plugins/toolbar.php');
$title=option('homename');
include('header.php');
if(option('showmode')=='dialog'){
?>
<script type="text/javascript">
if(self==top){
  window.location.href='index.php';
}
</script>
<?}
//常用变量
$vars=array(
  'hid'=>$hid,
  'msg_fail'=>option('msg_fail'),
);
?>
<div id="hiddenBox" style="position:absolute;top:-99999px;"></div>
<div class="topAD"><?=option('dialogTopData')?></div>
<div class="toolbarbk">
  <span style="display:<?=option('showmenu')=='N' ? 'none' : ''?>">
    <a href="admin.php" onfocus="this.blur()" target="_blank"><img src="images/icons/Admin.gif" /></a>
  </span>
</div>
<div id="loader"></div>
<div class="mainTable">
  <div id="boxLeft">
    <div id="dtl">
      <div id="dtr">
        <div id="dtc"><div id="alertBox"><?=option('toptip')?></div></div>
      </div>
    </div>
    <div class="clear"></div>
    <div id="showbox" style="height:<?=(int)option('dialogHeight')?>px" class="showbox scroll" onclick="CloseAllDiv()">
    <?
    //欢迎语
    $welcome='';
    for($i=1;$i<=$cometimes;$i++){
      $data=option('welcome'.$i);
      if(strip_tags($data)){
        $welcome=$data;
      }
    }
    $welcome=preg_replace('/\{\$(.+?)\}/ie',"onezVarA('$1')",$welcome);
    $welcome=preg_replace('/\{([0-9,]+)\}/ie',"onezVarB('$1')",$welcome);
    if($welcome){
      echo'<div class="im_from">'.$botname.'&nbsp;&nbsp;'.date('H:i:s',$time).'</div>';
      echo'<div class="im_content">'.$welcome.'</div>';
    }?>
    </div>
    <div id="toolbox">
      <?foreach($toolbarkeyA as $k=>$v){
      if($v){?>
      <a href="javascript:<?=$toolbarkeyB[$k]?>" title="<?=$v?>"><img src="<?=$toolbarkeyC[$k]?>" /></a>
      <?}else{?>
      <span></span>
      <?}}?>
    </div>
    <textarea id="inputbox" onkeydown="input_onkeydown(event)" onclick="CloseAllDiv()"></textarea>
    
    <div id="dbl"><div id="dbr"><div id="dbc"></div></div></div>
    <div class="clear"></div>
    <div class="buttonBox">
      <a class="button" href="javascript:void(0)" onclick="showDefault()" style="display:none" id="btn_default"><span>&nbsp;主菜单&nbsp;</span></a>
      <a class="button" href="javascript:void(0)" onclick="clearbox()"><span>&nbsp;清空&nbsp;</span></a>
      <a class="button" href="javascript:void(0)" onclick="Send()"><span>&nbsp;发送&nbsp;</span></a>
    </div>
    <div id="HotKey" onclick="SendKey(TheKey=='1'?'2':'1')"></div>
  </div>
  <div id="boxRight" class="scroll" style="width:<?=(int)option('dialogRightWidth')?>px"><?=option('dialogRightData')?></div>
</div>
<?=$HTML?>
<script type="text/javascript">
var botname="<?=$botname?>";
var homepage="<?=$homepage?>";
var TheKey=getcookie('sendkey');
var vars=<?=onez_json($vars)?>;
<?=$JS?>
</script>
<script type="text/javascript" src="js/dialog.js"></script>
<script type="text/javascript" src="js/robot.js"></script>
<?php include('footer.php');?>