KodExplorer/controller/editor.class.php

124 lines
3.8 KiB
PHP
Raw Normal View History

2016-12-21 08:01:06 +00:00
<?php
2015-03-22 20:54:54 +00:00
/*
* @link http://www.kalcaddle.com/
* @author warlee | e-mail:kalcaddle@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kalcaddle.com/tools/licenses/license.txt
*/
class editor extends Controller{
function __construct() {
parent::__construct();
$this->tpl = TEMPLATE . 'editor/';
}
// 多文件编辑器
public function index(){
2016-12-21 08:01:06 +00:00
$this->code_theme_set();
2015-03-22 20:54:54 +00:00
$this->display('editor.php');
}
// 单文件编辑
public function edit(){
2016-12-21 08:01:06 +00:00
$this->code_theme_set();
2015-03-22 20:54:54 +00:00
$this->display('edit.php');
}
2016-12-21 08:01:06 +00:00
private function code_theme_set(){
$set_class = '';
//获取编辑器配置数据
$editor_config = $this->config['editor_default'];
$config_file = USER.'data/editor_config.php';
2016-12-30 15:55:50 +00:00
if (!file_exists(iconv_system($config_file))) {//不存在则创建
2016-12-21 08:01:06 +00:00
$sql=fileCache::save($config_file,$editor_config);
}else{
$editor_config=fileCache::load($config_file);
}
$black_theme = array("ambiance","idle_fingers","monokai","pastel_on_dark","twilight",
"solarized_dark","tomorrow_night_blue","tomorrow_night_eighties");
if(in_array($editor_config['theme'],$black_theme)){
$set_class = 'class="code_theme_black"';
}
$this->assign('editor_config',json_encode($editor_config));//获取编辑器配置信息
$this->assign('code_theme_black',$set_class);//获取编辑器配置信息
}
2015-03-22 20:54:54 +00:00
// 获取文件数据
public function fileGet(){
$filename=_DIR($this->in['filename']);
2016-12-21 08:01:06 +00:00
if (!file_exists($filename)){
show_json($this->L['not_exists'],false);
}
2016-12-30 15:55:50 +00:00
if (!path_readable($filename)){
2016-12-21 08:01:06 +00:00
show_json($this->L['no_permission_read_all'],false);
}
if (filesize($filename) >= 1024*1024*40) show_json($this->L['edit_too_big'],false);
2015-03-22 20:54:54 +00:00
$filecontents=file_get_contents($filename);//文件内容
2015-10-25 15:39:11 +00:00
$charset=get_charset($filecontents);
2016-12-23 03:47:43 +00:00
2016-12-21 08:01:06 +00:00
if ($charset!='' &&
$charset!='utf-8' &&
function_exists("mb_convert_encoding")
){
$filecontents=@mb_convert_encoding($filecontents,'utf-8',$charset);
2015-03-22 20:54:54 +00:00
}
$data = array(
'ext' => get_path_ext($filename),
'name' => iconv_app(get_path_this($filename)),
'filename' => rawurldecode($this->in['filename']),
'charset' => $charset,
2016-12-21 08:01:06 +00:00
'base64' => false,
'content' => $filecontents
2015-03-22 20:54:54 +00:00
);
2016-12-21 08:01:06 +00:00
if(!json_encode(array("data"=>$filecontents))){
$data['content'] = base64_encode($filecontents);
$data['base64'] = true;
}
2016-12-23 03:47:43 +00:00
//unset($data['content']);
2015-03-22 20:54:54 +00:00
show_json($data);
}
public function fileSave(){
$filestr = rawurldecode($this->in['filestr']);
$charset = $this->in['charset'];
$path =_DIR($this->in['path']);
2016-12-21 08:01:06 +00:00
if(isset($this->in['create_file']) && !file_exists($path)){//不存在则创建
if(!@touch($path)){
show_json($this->L['create_error'],false);
}
}
if (!path_writeable($path)) show_json($this->L['no_permission_write_file'],false);
//支持二进制文件读写操作base64方式
if(isset($this->in['base64'])){
$filestr = base64_decode($filestr);
}else if ($charset !='' && $charset != 'utf-8' && $charset != 'ascii' &&
function_exists("mb_convert_encoding")
) {
$filestr=@mb_convert_encoding($filestr,$this->in['charset'],'utf-8');
2015-03-22 20:54:54 +00:00
}
$fp=fopen($path,'wb');
fwrite($fp,$filestr);
fclose($fp);
show_json($this->L['save_success']);
}
/*
* 获取编辑器配置信息
*/
public function setConfig(){
2016-12-21 08:01:06 +00:00
$file = USER.'data/editor_config.php';
2016-12-30 15:55:50 +00:00
if (!path_writeable(iconv_system($file))) {//配置不可写
2016-12-21 08:01:06 +00:00
show_json($this->L['no_permission_write_file'],false);
}
2015-03-22 20:54:54 +00:00
$key= $this->in['k'];
$value = $this->in['v'];
2016-12-21 08:01:06 +00:00
if ($key !='' && $value != '') {
$sql=new fileCache($file);
$sql->set($key,$value);//没有则添加一条
show_json($this->L["setting_success"]);
}else{
show_json($this->L['error'],false);
2015-03-22 20:54:54 +00:00
}
}
2016-12-21 08:01:06 +00:00
}