<?php
!defined('IN_ONEZ') && exit('Access Denied');
#名称：仿QQ群聊天系统 - 图片发布插件
#标识：chat.group.plugin.image

class onezphp_chat_group_plugin_image extends onezphp{
  function __construct(){
    
  }
  function bind_chat_group_admin_group_edit($form){
    $form->add(array('label'=>'只有管理员可以发送图片和截图','type'=>'checkbox','key'=>'only_master_image','hint'=>'','group'=>''));
  }
  function bind_chat_group_pc_talk(){
    global $G;
    if($G['group']['only_master_image'] && !$G['ismaster']){
      return;
    }
    include(dirname(__FILE__).'/php/pc.talk.php');
  }
  function bind_h5_chat_toolbar(){
    return '<div class="talk-btn-ico talk-btn-image talk-click"></div>';
  }
  function bind_h5_chat_js(){
    return '';
  }
  function upload(){
    $file='/plugins/'.$this->token.'/'.date('Y/m/d').'/'.uniqid().'.jpg';
    
    // 指定允许其他域名访问  
    header('Access-Control-Allow-Origin:*');  
    // 响应类型  
    header('Access-Control-Allow-Methods:POST');  
    // 响应头设置  
    header('Access-Control-Allow-Headers:x-requested-with,content-type');
    
    $tmpfile=$_FILES['file']['tmp_name'];
    if(!$tmpfile || !file_exists($tmpfile)){
      onez()->output(array('message'=>'文件无效','code'=>0));
    }
    $data=onez()->read($tmpfile);
    onez()->write(ONEZ_CACHE_PATH.$file,$data);
    
    
	  exit('ok'.ONEZ_CACHE_URL.$file);
  }
  function base64(){
    header('Access-Control-Allow-Origin:*');
    header('Access-Control-Allow-Methods:POST');
    header('Access-Control-Allow-Headers:x-requested-with,content-type');
    $data=$_REQUEST['data'];
    $data=base64_decode($data);
    
    $file='/plugins/'.$this->token.'/'.date('Y/m/d').'/'.uniqid().'.jpg';
    
    onez()->mkdirs(dirname(ONEZ_CACHE_PATH.$file));
    $im=imagecreatefromstring($data);
    imagejpeg($im,ONEZ_CACHE_PATH.$file,100);
    imagedestroy($im);
    @unlink($temp_file);
    
    $result=array();
    if(file_exists(ONEZ_CACHE_PATH.$file)){
      $result['status']='ok';
      $result['url']=ONEZ_CACHE_URL.$file;
    }else{
      onez()->error('上传失败');
    }
    onez()->output($result);
  	break;
  }
}