<?php

/* ========================================================================
 * $Id: kindeditor.php 3969 2018-08-29 22:35:31Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
#名称：KindEditor编辑器
#标识：kindeditor

class onezphp_kindeditor extends onezphp{
  function __construct(){
    
  }
  function form_code($arr){
    $this->arr=$arr;
    $html='';
    #只加载一次
    $height=$arr['height'];
    !$height && $height='600';
    $formkz=onez('bootstrap_form_kz')->init($arr);
    $formkz->input->attr('tagname','textarea')
               ->attr('class','ueditor')
               ->attr('style','height:'.$height.'px')
    ->is_end=1;
    $formkz->input=onez('html')->create('div')->html($this->code($arr));
    
    
    $html.=$formkz->code();
    $html.=$this->auto($arr);
    return $html;
  }
  function form_save(&$value,$arr=false){
    global $G;
    !$arr && $arr=$this->arr;
    $value=$_REQUEST[$arr['key']];
  }
  function code($arr){
    global $G;
    #只加载一次
    if(!$this->loaded){
      $this->loaded=1;
      $html.='
	<link rel="stylesheet" href="'.$this->url.'/lib/themes/default/default.css" />
	<link rel="stylesheet" href="'.$this->url.'/lib/plugins/code/prettify.css" />
	<script charset="utf-8" src="'.$this->url.'/lib/kindeditor.js"></script>
	<script charset="utf-8" src="'.$this->url.'/lib/lang/zh-CN.js"></script>
	<script charset="utf-8" src="'.$this->url.'/lib/plugins/code/prettify.js"></script>
	<script charset="utf-8" src="'.$this->url.'/lib/plugins/onez/snapshot.js"></script>
	
';
      if($arr['cache']){
        $html.='<script charset="utf-8" src="'.$this->url.'/lib/plugins/onez/autosave.js"></script>';
      }
    }
    if($G['userid']){
      onez('scookie')->call('editor_userid',$G['userid']);
    }
    $height=$arr['height'];
    !$height && $height='400';
    if(!$arr['full'] && !$arr['items']){
      $items=array('fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link', '|', 'insertfile', 'table');
      $option.='items:'.json_encode($items).',';
    }
    $arr['items'] && $option.='items:'.json_encode($arr['items']).',';
    $option.="width:'100%',";
    $html.=<<<ONEZ
<textarea class="kindeditor $arr[classname]" name="$arr[key]" id="input-$arr[key]" style="height:{$height}px">$arr[value]</textarea>
<script type="text/javascript">
KindEditor.ready(function(K) {
	window.editor$arr[key] = K.create('#input-$arr[key]', {
		cssPath : '{$this->url}/lib/plugins/code/prettify.css',
		uploadJson : '{$this->url}/lib/php/upload_json.php',
		fileManagerJson : '{$this->url}/lib/php/file_manager_json.php',
		allowFileManager : true,
    $option
		afterBlur : function() {
      this.sync();
			var self = this;
      $(document).trigger('onez-kindeditor-blur',{
        key:'$arr[key]',
        editor:this,
        obj:K(self.edit.doc),
        id:'#input-$arr[key]'
      });
		},
		afterCreate : function() {
			var self = this;
      $(document).trigger('onez-kindeditor-create',{
        key:'$arr[key]',
        editor:this,
        obj:K(self.edit.doc),
        id:'#input-$arr[key]'
      });
		}
	});
	prettyPrint();
});
</script>
ONEZ;
    return $html;
  }
  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;
  }
}