/
editor.class.php
executable file
·152 lines (144 loc) · 4.57 KB
/
editor.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* @link http://kodcloud.com/
* @author warlee | e-mail:kodcloud@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kodcloud.com/tools/license/license.txt
*/
class editor extends Controller{
function __construct() {
parent::__construct();
}
// 多文件编辑器
public function index(){
$this->themeSet();
$this->display('editor.html');
}
// 单文件编辑
public function edit(){
$this->themeSet();
$this->display('edit.html');
}
private function themeSet(){
$setClass = '';
//获取编辑器配置数据
$editorConfig = $this->config['editorDefault'];
$configFile = USER.'data/editor_config.php';
if (!file_exists(iconv_system($configFile))) {//不存在则创建
$sql=FileCache::save($configFile,$editorConfig);
}else{
$editorConfig=FileCache::load($configFile);
}
$blackTheme = array("ambiance","idle_fingers","monokai","pastel_on_dark","twilight",
"solarized_dark","tomorrow_night_blue","tomorrow_night_eighties");
if(in_array($editorConfig['theme'],$blackTheme)){
$setClass = 'class="code-theme-black"';
}
$this->assign('editorConfig',json_encode($editorConfig));//获取编辑器配置信息
$this->assign('codeThemeBlack',$setClass);//获取编辑器配置信息
}
// 获取文件数据
public function fileGet(){
if(isset($this->in['fileUrl'])){
$pass = $this->config['settingSystem']['systemPassword'];
$fileUrl = $this->in['fileUrl'];
if(!request_url_safe($fileUrl)){
show_json(LNG('url error!'),false);
}
$urlInfo = parse_url_query($fileUrl);
if( isset($urlInfo['fid']) &&
strlen(Mcrypt::decode($urlInfo['fid'],$pass)) != 0
){
$filepath = Mcrypt::decode($urlInfo['fid'],$pass);
$displayName = get_path_this($filepath);
if(isset($urlInfo['downFilename'])){
$displayName = rawurldecode($urlInfo['downFilename']);
}
}else{
$displayName = rawurldecode($urlInfo['name']);
$filepath = $fileUrl.'&accessToken='.access_token_get();
}
}else{
$displayName = rawurldecode($this->in['filename']);
$filepath =_DIR($this->in['filename']);
if (!file_exists($filepath)){
show_json(LNG('not_exists'),false);
}
if (!path_readable($filepath)){
show_json(LNG('no_permission_read_all'),false);
}
if (filesize($filepath) >= 1024*1024*20){
show_json(LNG('edit_too_big'),false);
}
}
$fileContents=file_get_contents($filepath);//文件内容
//echo $fileContents;exit;
if(isset($this->in['charset']) && $this->in['charset']){
$charset = strtolower($this->in['charset']);
}else{
$charset = get_charset($fileContents);
}
if ($charset !='' &&
$charset !='utf-8' &&
function_exists("mb_convert_encoding")
){
$fileContents = @mb_convert_encoding($fileContents,'utf-8',$charset);
}
$data = array(
'ext' => get_path_ext($displayName),
'name' => iconv_app(get_path_this($displayName)),
'filename' => $displayName,
'charset' => $charset,
'base64' => true,// 部分防火墙编辑文件误判问题处理
'content' => base64_encode($fileContents)
);
show_json($data);
}
public function fileSave(){
$fileStr = rawurldecode($this->in['filestr']);
$path =_DIR($this->in['path']);
if(isset($this->in['create_file']) && !file_exists($path)){//不存在则创建
if(!@touch($path)){
show_json(LNG('create_error'),false);
}
}
if (!path_writeable($path)) show_json(LNG('no_permission_write_file'),false);
//支持二进制文件读写操作(base64方式)
if(isset($this->in['base64'])){
$fileStr = base64_decode($fileStr);
}
$charset = strtolower($this->in['charset']);
if(isset($this->in['charsetSave'])){
$charset = strtolower($this->in['charsetSave']);
}
if ( $charset !='' &&
$charset != 'utf-8' &&
$charset != 'ascii' &&
function_exists("mb_convert_encoding")
) {
$fileStr = @mb_convert_encoding($fileStr,$charset,'utf-8');
}
$fp=fopen($path,'wb');
fwrite($fp,$fileStr);
fclose($fp);
show_json(LNG('save_success'));
}
/*
* 获取编辑器配置信息
*/
public function setConfig(){
$file = USER.'data/editor_config.php';
if (!path_writeable(iconv_system($file))) {//配置不可写
show_json(LNG('no_permission_write_file'),false);
}
$key= $this->in['k'];
$value = $this->in['v'];
if ($key !='' && $value != '') {
$sql=new FileCache($file);
$sql->set($key,$value);//没有则添加一条
show_json(LNG('setting_success'));
}else{
show_json(LNG('error'),false);
}
}
}