<?php

/* ========================================================================
 * $Id: group.php 10907 2017-06-12 17:49:14Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
!$group && onez('chat.group.common')->error('您要加入的群不存在');
$guser['key1']=='tickout' && onez('chat.group.common')->error('很抱歉，您无权进入本群。理由：'.($guser['tickout_because']?$guser['tickout_because']:'无'));
$G['grade']=='guest' && !$group['index3'] && onez()->location(onez()->href('/login.php?groupid='.$groupid.'&goto='.urlencode(onez()->href('/group.php?groupid='.$groupid))));

$action=onez()->gp('action');
if($action=='chklogin'){
  onez()->ok('加入成功');
}
$G['this']->data()->open('group_users')->update(array(
  'ip'=>onez()->ip(),
),"id='$guser[id]'");


if(!empty($_REQUEST['online'])){
  $online=onez()->gp('online');
  $G['this']->data()->open('group')->update(array(
    'online'=>$online,
    'index4'=>$online,
  ),"id='$groupid'");
}
$A=array('ver'=>'1.0');
if($action=='send'){
  $style=onez()->gp('style');
  $message=onez()->gp('message');
  if($G['grade']=='guest'){
    $A['print']=array(
      'msgid'=>time(),
      'type'=>'tip',
      'tip'=>'游客不能发言',
    );
  }elseif($guser['key1']!='ok'){
    $A['print']=array(
      'msgid'=>time(),
      'classname'=>'text-red',
      'type'=>'tip',
      'tip'=>'您已被系统禁言。',
    );
  }else{
    $msg=array(
      'upid'=>$groupid,
      'key1'=>$userid,
      'key2'=>$G['grade'],
      'nickname'=>$G['nickname'],
      'userid'=>$G['userid'],
      'style'=>$style,
      'message'=>$message,
    );
    $msgid=$G['this']->data()->open('message')->insert($msg);
    $A['print']=array(
      'type'=>'me',
      'msgid'=>$msgid,
      'nickname'=>$G['nickname'],
      'style'=>$style,
      'message'=>$message,
      'time'=>date('H:i:s'),
    );
    $A['msg']=array(
      'type'=>'getmsg',
      'id'=>$msgid,
      'nickname'=>$G['nickname'],
      'userid'=>$userid,
      'style'=>$style,
      'message'=>$message,
      'time'=>date('H:i:s'),
    );
  }
  onez()->output($A);
}elseif($action=='viewuser'){
  $uid=onez()->gp('uid');
  if(strpos($uid,'guest.')===false){
    $userid=(int)$uid;
    $grade='user';
    $u=$G['this']->data()->open('member')->one("id='$userid'");
    $nickname=onez('chat.group.common')->nickname($userid);
    $avatar=onez('chat.group.common')->avatar($userid);
    $guser=$G['this']->data()->open('group_users')->one("upid='$groupid' and userid='$userid' and token='user'");
  }else{
    $userid=intval(substr($uid,6));
    $grade='guest';
    $nickname=onez('chat.group.common')->nickname_guest($userid);
    $avatar=onez('chat.group.common')->avatar(0);
    $guser=$G['this']->data()->open('group_users')->one("upid='$groupid' and userid='$userid' and token='guest'");
  }
  
  $html=array();
  $html[]='<p><img src="'.$avatar.'" width="32" height="32" style="border-radius: 50%" /> '.$nickname.'</p>';
  $html[]='<p>加入时间: <code>'.date('Y-m-d H:i:s',$guser['addtime']).'</code>';
  $html[]='<p>个性签名: <code>'.($u['sign']?$u['sign']:'暂无个性签名').'</code>';
  $html[]='<p>';
  if($G['ismaster']){#群管理
    
  }
  $html[]='</p>';
  $A['info']=implode("",$html);
  onez()->output($A);
}elseif($action=='history'){
  $T=$G['this']->data()->open('message')->page("upid='$groupid' order by id desc");
  $msg=array();
  $today=date('Y-m-d');
  foreach($T[0] as $rs){
    $item=array(
      'type'=>'user',
      'msgid'=>$rs['id'],
      'userid'=>$rs['key1'],
      'nickname'=>$rs['nickname'],
      'style'=>$rs['style'],
      'message'=>$rs['message'],
      'time'=>date('Y-m-d H:i:s',$rs['addtime']),
    );
    if($rs['key2']=='guest'){
      $item['avatar']=onez('chat.group.common')->avatar(0);
    }else{
      $item['avatar']=onez('chat.group.common')->avatar($rs['key1']);
    }
    if($rs['key1']==$userid){
      $item['type']='me';
    }
    $item['time']=str_replace($today.' ','',$item['time']);
    $msg[]=$item;
  }
  if($msg){
    $newmsg=onez('call')->call('chat_group_message',$msg);
    if($newmsg){
      $msg=$newmsg[0];
    }
  }
  $A['history']=$msg;
  onez()->output($A);
}elseif($action=='notice'){
  $A['notice']=$group['notice'];
  onez()->output($A);
}elseif($action=='loaduser'){
  $uids=onez()->gp('uids');
  $user=$guest=array();
  foreach(explode(',',$uids) as $uid){
    if(strpos($uid,'guest.')===false){
      $uid=intval($uid);
      if($uid){
        $user[]=$uid;
      }
    }else{
      $uid=intval(substr($uid,6));
      if($uid){
        $guest[]=$uid;
      }
    }
  }
  $users=array();
  $or=array();
  if($user){
    $or[]="(token='user' and userid in (".implode(',',$user)."))";
  }
  if($guest){
    $or[]="(token='guest' and userid in (".implode(',',$guest)."))";
  }
  if($or){
    $T=$G['this']->data()->open('group_users')->record("(key1<>'tickout') and (".implode(' or ',$or).") order by id desc");
    $users=onez('chat.group.common')->userlist($T);
    if($users){
      $A['users']=$users;
    }
  }
  onez()->output($A);
}elseif($action=='userlist'){
  $T=$G['this']->data()->open('group_users')->record("(key1<>'tickout') and token='user' order by id desc");
  $users=onez('chat.group.common')->userlist($T);
  if($users){
    $A['users']=$users;
  }
  onez()->output($A);
#20170611新增
}elseif($action=='che'){
  $msgid=(string)onez()->gp('msgid');
  $G['this']->data()->open('message')->delete("id='$msgid'");
  $A['msg']=array(
    'type'=>'doit',
    'event'=>array(
      'action'=>'che',
      'msgid'=>$msgid,
    ),
  );
  onez()->output($A);
}elseif($action=='lockall'){
  $status=$group['index5']==1?0:1;
  $G['this']->data()->open('group')->update(array(
    'index5'=>$status,
  ),"id='$groupid'");
  $A['msg']=array(
    'type'=>'doit',
    'event'=>array(
      'action'=>'lockall',
      'status'=>$status,
    ),
  );
  onez()->output($A);
}
$G['title']=$group['name'];
include_once(dirname(__FILE__).'/header.php');
echo onez('ui')->css($baseurl.'/images/group.css?t='.filemtime(dirname(__FILE__).'/images/group.css'));
echo onez('ui')->js($baseurl.'/js/group.js?t='.filemtime(dirname(__FILE__).'/js/group.js'));
echo onez('ui')->js($baseurl.'/js/socket.io.js');
echo onez('ui')->js($baseurl.'/js/jquery.colorpicker.js');
$width_right=200;
$room=md5(onez()->homepage()."\t".$groupid);
if(empty($group['alpha'])){
  $group['alpha']=10;
}
$alpha=(100-$group['alpha'])/100;
echo '<body scroll="no" style="'.($group['bgcolor']?'background-color:'.$group['bgcolor']:'').'">';
?>

<section class="chat" style="background: rgba(255,255,255,<?php echo $alpha?>);">
  <section class="chat-header">
    <div class="pull-left">
      <ul class="chat-tab">
        <li data-key="chat" class="current">聊天</li>
        <li data-key="notice" data-url="<?php echo onez()->href('/notice.php?groupid='.$groupid)?>">公告</li>
        <?php if($G['userid']){?>
          <li onclick="openWin('<?php echo onez()->href('/modify.php?groupid='.$groupid)?>','修改资料')">修改资料</li>
          <li data-key="logout">退出</li>
        <?php }else{
          $goto=urlencode(onez()->href('/group.php?groupid='.$groupid));
          ?>
          <li data-key="login" data-url="<?php echo onez()->href('/login.php?groupid='.$groupid.'&goto='.$goto)?>">登录</li>
          <li data-key="register" data-url="<?php echo onez()->href('/register.php?groupid='.$groupid.'&goto='.$goto)?>">注册</li>
        <?php }?>
      </ul>
    </div>
    <div class="pull-right">
      <?php echo $group['ad_pc_tr']?>
    </div>
    <div class="clearfix"></div>
  </section>
  <section class="chat-body">
    <section class="tab-body" data-key="chat">
      <section class="chat-main" style="right:<?php echo $width_right?>px">
        <div class="btn-bar">
          <a href="javascript:;" class="btn-bar-open" title="隐藏侧边"></a>
          <a href="javascript:;" class="btn-bar-close" title="显示侧边"></a>
        </div>
        <section class="showbox">
          
        </section>
        <section class="chat-footer">
          <section class="chat-toolbar">
            <a href="javascript:;" class="icon-emote">表情</a>
          </section>
          <section class="chat-input">
            <textarea class="inputbox"></textarea>
          </section>
          <section class="chat-bottom">
            <div class="pull-left">
              <?php echo $group['ad_pc_bl']?>
            </div>
            <div class="pull-right">
            <?php if(onez()->gp('miniwin')){?>
              <button class="btn btn-sm btn-default" onclick="parent.closeWin()">关闭</button>
            <?php }?>
              <div class="btn-group">
                  <button type="button" class="btn btn-sm btn-info btn-send">发送</button>
                  <button type="button" class="btn btn-sm btn-info dropdown-toggle" data-toggle="dropdown">
                    <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu send-key-set" role="menu">
                      <li class="key-enter"><a href="#"><span class="glyphicon glyphicon-ok"></span> 按Enter键发送消息</a></li>
                      <li class="key-ctrl-enter"><a href="#"><span class="glyphicon glyphicon-ok"></span> 按Ctrl+Enter键发送消息</a></li>
                  </ul>
              </div>
            </div>
            <div class="clearfix"></div>
          </section>
        </section>




  <div class="box_emote"></div>




      </section>
      <section class="chat-right" style="width:<?php echo $width_right?>px">
        <section class="chat-notice">
          <div class="chat-notice-box"><?php echo $group['notice']?></div>
        </section>
        <section class="userlist">
          <dl>
            <dt title="展开群成员">群成员 <span class="online-info"></span></dt>
            <dd>
              <ul class="userlist-me"></ul>
              <ul class="userlist-master"></ul>
              <ul class="userlist-user"></ul>
              <ul class="userlist-guest"></ul>
              <ul class="userlist-loading"></ul>
            </dd>
          </dl>
        </section>
      </section>
    </section>
    <section class="tab-body" data-key="web">
      <iframe class="frame" width="100%" height="100%" frameborder="0" ></iframe>
    </section>
  </section>
</section>
<?php 
$plugins=onez('call')->call('chat_group_pc_plugins');
!$plugins && $plugins=array();
?>
<script type="text/javascript">
var onez={};
var group=new onezgroup('.chat',{
  baseurl:'<?php echo $baseurl?>',
  pluginurl:'<?php echo onez('chat.group.common')->url?>',
  groupid:'<?php echo $group['id']?>',
  history:'<?php echo $group['key1']?>',
  mode:'<?php echo $group['key2']?>',
  index5:'<?php echo $group['index5']?>',
  nickname:'<?php echo $G['nickname']?>',
  status:'<?php echo $guser['key1']?>',
  ismaster:<?php echo $G['ismaster']?'true':'false'?>,
  masters:<?php echo json_encode(array_keys($master))?>,
  plugins:<?php echo json_encode($plugins)?>,
  width_right:<?php echo $width_right?>
});
group.connect('ws://m.onez.cn:11111','<?php echo $room?>','<?php echo $userid?>');
</script>
<?php 
onez('call')->call('chat_group_pc_talk',$group);
?>
</body>
<?php 
include_once(dirname(__FILE__).'/footer.php');