UeditorController.php
12.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use app\user\model\AssetModel;
use cmf\controller\HomeBaseController;
use cmf\lib\Upload;
/**
* 百度编辑器文件上传处理控制器
* Class Ueditor
* @package app\asset\controller
*/
class UeditorController extends HomeBaseController
{
private $stateMap = [ //上传状态映射表,国际化用户需考虑此处数据的国际化
"SUCCESS", //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
"文件大小超出 upload_max_filesize 限制",
"文件大小超出 MAX_FILE_SIZE 限制",
"文件未被完整上传",
"没有文件被上传",
"上传文件为空",
"ERROR_TMP_FILE" => "临时文件错误",
"ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件",
"ERROR_SIZE_EXCEED" => "文件大小超出网站限制",
"ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许",
"ERROR_CREATE_DIR" => "目录创建失败",
"ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限",
"ERROR_FILE_MOVE" => "文件保存时出错",
"ERROR_FILE_NOT_FOUND" => "找不到上传文件",
"ERROR_WRITE_CONTENT" => "写入文件内容错误",
"ERROR_UNKNOWN" => "未知错误",
"ERROR_DEAD_LINK" => "链接不可用",
"ERROR_HTTP_LINK" => "链接不是http链接",
"ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确"
];
/**
* 初始化
*/
function _initialize()
{
$adminId = cmf_get_current_admin_id();
$userId = cmf_get_current_user_id();
if (empty($adminId) && empty($userId)) {
exit("非法上传!");
}
}
/**
* 处理上传处理
*/
function upload()
{
error_reporting(E_ERROR);
header("Content-Type: text/html; charset=utf-8");
$action = $this->request->param('action');
switch ($action) {
case 'config':
$result = $this->ueditorConfig();
break;
/* 上传图片 */
case 'uploadimage':
$result = $this->ueditorUpload("image");
break;
/* 上传涂鸦 */
case 'uploadscrawl':
$result = $this->ueditorUpload("image");
break;
/* 上传视频 */
case 'uploadvideo':
$result = $this->ueditorUpload("video");
break;
/* 上传文件 */
case 'uploadfile':
$result = $this->ueditorUpload("file");
break;
/* 列出图片 */
case 'listimage':
$result = "";
break;
/* 列出文件 */
case 'listfile':
$result = "";
break;
/* 抓取远程文件 */
case 'catchimage':
$result = $this->_get_remote_image();
break;
default:
$result = json_encode(['state' => '请求地址出错']);
break;
}
/* 输出结果 */
if (isset($_GET["callback"]) && false) {//TODO 跨域上传
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode([
'state' => 'callback参数不合法'
]);
}
} else {
exit($result);
}
}
/**
* 获取远程图片
*/
private function _get_remote_image()
{
$source = $this->request->param('source');
$item = [
"state" => "",
"url" => "",
"size" => "",
"title" => "",
"original" => "",
"source" => ""
];
$date = date("Ymd");
$uploadSetting = cmf_get_upload_setting();
$uploadMaxFileSize = $uploadSetting["image"]['upload_max_filesize'];
$uploadMaxFileSize = empty($uploadMaxFileSize) ? 2048 : $uploadMaxFileSize;//默认2M
$allowedExts = explode(',', $uploadSetting["image"]["extensions"]);
$strSavePath = ROOT_PATH . 'public' . DS . "ueditor" . DS . $date . DS;
//远程抓取图片配置
$config = [
"savePath" => $strSavePath, //保存路径
"allowFiles" => $allowedExts,// [".gif", ".png", ".jpg", ".jpeg", ".bmp"], //文件允许格式
"maxSize" => $uploadMaxFileSize //文件大小限制,单位KB
];
$storage_setting = cmf_get_cmf_settings('storage');
$qiniu_domain = $storage_setting['Qiniu']['domain'];
$no_need_domains = [$qiniu_domain];
$list = [];
foreach ($source as $imgUrl) {
$host = str_replace(['http://', 'https://'], '', $imgUrl);
$host = explode('/', $host);
$host = $host[0];
if (in_array($host, $no_need_domains)) {
continue;
}
$return_img = $item;
$return_img['source'] = $imgUrl;
$imgUrl = htmlspecialchars($imgUrl);
$imgUrl = str_replace("&", "&", $imgUrl);
//http开头验证
if (strpos($imgUrl, "http") !== 0) {
$return_img['state'] = $this->stateMap['ERROR_HTTP_LINK'];
array_push($list, $return_img);
continue;
}
//获取请求头
// is_sae()
if (!cmf_is_sae()) {//SAE下无效
$heads = get_headers($imgUrl);
//死链检测
if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
$return_img['state'] = $this->stateMap['ERROR_DEAD_LINK'];
array_push($list, $return_img);
continue;
}
}
//格式验证(扩展名验证和Content-Type验证)
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
$return_img['state'] = $this->stateMap['ERROR_HTTP_CONTENTTYPE'];
array_push($list, $return_img);
continue;
}
//打开输出缓冲区并获取远程图片
ob_start();
$context = stream_context_create(
[
'http' => [
'follow_location' => false // don't follow redirects
]
]
);
//请确保php.ini中的fopen wrappers已经激活
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
//大小验证
$uriSize = strlen($img); //得到图片大小
$allowSize = 1024 * $config['maxSize'];
if ($uriSize > $allowSize) {
$return_img['state'] = $this->stateMap['ERROR_SIZE_EXCEED'];
array_push($list, $return_img);
continue;
}
$file = uniqid() . strrchr($imgUrl, '.');
$savePath = $config['savePath'];
$tmpName = $savePath . $file;
//创建保存位置
if (!file_exists($savePath)) {
mkdir("$savePath", 0777, true);
}
$file_write_result = cmf_file_write($tmpName, $img);
if ($file_write_result) {
if (config('FILE_UPLOAD_TYPE') == 'Qiniu') {
//todo qiniu code
}
if (config('FILE_UPLOAD_TYPE') == 'Local') {
$file = $strSavePath . $file;
$return_img['state'] = 'SUCCESS';
$return_img['url'] = $file;
array_push($list, $return_img);
}
} else {
$return_img['state'] = $this->stateMap['ERROR_WRITE_CONTENT'];
array_push($list, $return_img);
}
}
return json_encode([
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
]);
}
/**
* 文件上传
* @param string $fileType 文件类型
* @return string
*/
private function ueditorUpload($fileType = 'image')
{
$uploader = new Upload();
$uploader->setFileType($fileType);
$uploader->setFormName('upfile');
$result = $uploader->upload();
if ($result === false) {
return json_encode([
'state' => $uploader->getError()
]);
} else {
return json_encode([
'state' => 'SUCCESS',
'url' => $result['url'],
'title' => $result['name'],
'original' => $result['name']
]);
}
}
/**
* 获取百度编辑器配置
*/
private function ueditorConfig()
{
$config_text = preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./static/js/ueditor/config.json"));
$config = json_decode($config_text, true);
$upload_setting = cmf_get_upload_setting();
$config['imageMaxSize'] = $upload_setting['file_types']['image']['upload_max_filesize'] * 1024;
$config['imageAllowFiles'] = array_map([$this, 'ueditorExtension'], explode(",", $upload_setting['file_types']['image']['extensions']));
$config['scrawlMaxSize'] = $upload_setting['file_types']['image']['upload_max_filesize'] * 1024;
//
$config['catcherMaxSize'] = $upload_setting['file_types']['image']['upload_max_filesize'] * 1024;
$config['catcherAllowFiles'] = array_map([$this, 'ueditorExtension'], explode(",", $upload_setting['file_types']['image']['extensions']));
$config['videoMaxSize'] = $upload_setting['file_types']['video']['upload_max_filesize'] * 1024;
$config['videoAllowFiles'] = array_map([$this, 'ueditorExtension'], explode(",", $upload_setting['file_types']['video']['extensions']));
$config['fileMaxSize'] = $upload_setting['file_types']['file']['upload_max_filesize'] * 1024;
$config['fileAllowFiles'] = array_map([$this, 'ueditorExtension'], explode(",", $upload_setting['file_types']['file']['extensions']));
return json_encode($config);
}
/**
* 格式化后缀
* @param $str
* @return string
*/
private function ueditorExtension($str)
{
return "." . trim($str, '.');
}
/**
* @function imageManager
*/
public function imageManager()
{
header("Content-Type: text/html; charset=utf-8");
//需要遍历的目录列表,最好使用缩略图地址,否则当网速慢时可能会造成严重的延时
$paths = [C("TMPL_PARSE_STRING.__UPLOAD__"), 'upload/'];
$files = [];
foreach ($paths as $path) {
$tmp = $this->getfiles($path);
if ($tmp) {
$files = array_merge($files, $tmp);
}
}
if (!count($files)) return;
rsort($files, SORT_STRING);
$str = "";
foreach ($files as $file) {
$str .= ROOT_PATH . '/' . $file . "ue_separate_ue";
}
echo $str;
}
/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param array $files
* @return array
*/
private function getfiles($path, $allowFiles, &$files = [])
{
if (!is_dir($path)) return null;
if (substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
$this->getfiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = [
'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime' => filemtime($path2)
];
}
}
}
}
return $files;
}
}