-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKindEditorAction.php
349 lines (315 loc) · 11.7 KB
/
KindEditorAction.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
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
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of KindEditorAction
*
* @author Qinn Pan <Pan JingKui, [email protected]>
* @link http://www.pjkui.com
* @QQ 714428042
* @date 2015-3-4
*/
namespace pjkui\kindeditor;
use Yii;
use yii\base\Action;
use yii\helpers\ArrayHelper;
use pjkui\kindeditor\Services_JSON;
class KindEditorAction extends Action {
/**
*
* @var Array 保存配置的数组
*/
public $config;
public $php_path = null;
public $php_url = null;
public $root_path = null;
public $root_url = null;
public $save_path = null;
public $save_url = null;
public $max_size = 1000000; // 最大文件大小
public $ext_arr = [ // 定义允许上传的文件扩展名
'image' => ['gif', 'jpg', 'jpeg', 'png', 'bmp'],
'flash' => ['swf', 'flv'],
'media' => ['swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'],
'file' => ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'],
];
// public $save_path;
public function init() {
// close csrf
Yii::$app->request->enableCsrfValidation = false;
// 默认设置
// $this->php_path = dirname(__FILE__) . '/';
if ($this->php_path === null) {
$this->php_path = Yii::getAlias('@webroot') . '/';
}
if ($this->php_url === null) {
$this->php_url = Yii::getAlias('@web') . '/';
}
// 根目录路径,可以指定绝对路径,比如 /var/www/attached/
if ($this->root_path === null) {
$this->root_path = $this->php_path . 'upload/';
}
// 根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
if ($this->root_url === null) {
$this->root_url = $this->php_url . 'upload/';
}
// 文件保存目录路径
if ($this->save_path === null) {
$this->save_path = $this->root_path;
}
// 文件保存目录URL
if ($this->save_url === null) {
$this->save_url = $this->root_url;
}
// load config file
parent::init();
}
public function run() {
$this->handAction();
}
/**
* 处理动作
*/
public function handAction() {
// 获得action 动作
$action = Yii::$app->request->get('action');
switch ($action) {
case 'fileManagerJson':
$this->fileManagerJsonAction();
break;
case 'uploadJson':
$this->UploadJosnAction();
break;
default:
break;
}
}
// 排序
public function cmp_func($a, $b) {
global $order;
if ($a['is_dir'] && !$b['is_dir']) {
return -1;
} else if (!$a['is_dir'] && $b['is_dir']) {
return 1;
} else {
if ($order == 'size') {
if ($a['filesize'] > $b['filesize']) {
return 1;
} else if ($a['filesize'] < $b['filesize']) {
return -1;
} else {
return 0;
}
} else if ($order == 'type') {
return strcmp($a['filetype'], $b['filetype']);
} else {
return strcmp($a['filename'], $b['filename']);
}
}
}
/**
* 文件管理操作
* @author ${author}
*/
public function fileManagerJsonAction() {
// 图片扩展名
$ext_arr = $this->ext_arr['image'];
// 目录名
$dir_name = empty($_GET['dir']) ? '' : trim($_GET['dir']);
if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {
echo "Invalid Directory name.";
exit;
}
if ($dir_name !== '') {
$root_path= $this->root_path . $dir_name . "/";
$root_url=$this->root_url .$dir_name . "/";
if (!file_exists($root_path)) {
mkdir($root_path);
}
}
// 根据path参数,设置各路径和URL
if (empty($_GET['path'])) {
$current_path = realpath($root_path) . '/';
$current_url = $root_url;
$current_dir_path = '';
$moveup_dir_path = '';
} else {
$current_path = realpath($root_path) . '/' . $_GET['path'];
$current_url = $root_url . $_GET['path'];
$current_dir_path = $_GET['path'];
$moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
}
// echo realpath($this->root_path);
// 排序形式,name or size or type
$order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);
// 不允许使用..移动到上一级目录
if (preg_match('/\.\./', $current_path)) {
echo 'Access is not allowed.';
exit;
}
// 最后一个字符不是/
if (!preg_match('/\/$/', $current_path)) {
echo 'Parameter is not valid.';
exit;
}
// 目录不存在或不是目录
if (!file_exists($current_path) || !is_dir($current_path)) {
echo 'Directory does not exist.';
exit;
}
// 遍历目录取得文件信息
$file_list = array();
if ($handle = opendir($current_path)) {
$i = 0;
while (false !== ($filename = readdir($handle))) {
if ($filename[0] == '.')
continue;
$file = $current_path . $filename;
if (is_dir($file)) {
$file_list[$i]['is_dir'] = true; //是否文件夹
$file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
$file_list[$i]['filesize'] = 0; //文件大小
$file_list[$i]['is_photo'] = false; //是否图片
$file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断
} else {
$file_list[$i]['is_dir'] = false;
$file_list[$i]['has_file'] = false;
$file_list[$i]['filesize'] = filesize($file);
$file_list[$i]['dir_path'] = '';
$file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
$file_list[$i]['filetype'] = $file_ext;
}
$file_list[$i]['filename'] = $filename; //文件名,包含扩展名
$file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
$i++;
}
closedir($handle);
}
usort($file_list, [$this,'cmp_func']);
$result = array();
// 相对于根目录的上一级目录
$result['moveup_dir_path'] = $moveup_dir_path;
// 相对于根目录的当前目录
$result['current_dir_path'] = $current_dir_path;
// 当前目录的URL
$result['current_url'] = $current_url;
// 文件数
$result['total_count'] = count($file_list);
// 文件列表数组
$result['file_list'] = $file_list;
// 输出JSON字符串
header('Content-type: application/json; charset=UTF-8');
echo json_encode($result);
}
public function UploadJosnAction() {
// 定义允许上传的文件扩展名
$ext_arr = $this->ext_arr;
// PHP上传失败
if (!empty($_FILES['imgFile']['error'])) {
switch ($_FILES['imgFile']['error']) {
case '1':
$error = '超过php.ini允许的大小。';
break;
case '2':
$error = '超过表单允许的大小。';
break;
case '3':
$error = '图片只有部分被上传。';
break;
case '4':
$error = '请选择图片。';
break;
case '6':
$error = '找不到临时目录。';
break;
case '7':
$error = '写文件到硬盘出错。';
break;
case '8':
$error = 'File upload stopped by extension。';
break;
case '999':
default:
$error = '未知错误。';
}
$this->alert($error);
}
// 有上传文件时
if (empty($_FILES) === false) {
// 原文件名
$file_name = $_FILES['imgFile']['name'];
// 服务器上临时文件名
$tmp_name = $_FILES['imgFile']['tmp_name'];
// 文件大小
$file_size = $_FILES['imgFile']['size'];
// 检查文件名
if (!$file_name) {
$this->alert("请选择文件。");
}
// 检查目录
if (@is_dir($this->save_path) === false) {
mkdir($this->save_path); // 目录不存在在就创建目录
}
// 检查目录写权限
if (@is_writable($this->save_path) === false) {
$this->alert("上传目录没有写权限。");
}
// 检查是否已上传
if (@is_uploaded_file($tmp_name) === false) {
$this->alert("上传失败。");
}
// 检查文件大小
if ($file_size > $this->max_size) {
$this->alert("上传文件大小超过限制。");
}
// 检查目录名
$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
if (empty($ext_arr[$dir_name])) {
$this->alert("目录名不正确。");
}
// 获得文件扩展名
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
// 检查扩展名
if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
$this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
}
// 创建文件夹
if ($dir_name !== '') {
$save_path=$this->save_path . $dir_name . "/";
$save_url=$this->save_url . $dir_name . "/";
if (!file_exists($save_path)) {
mkdir($save_path);
}
}
$ymd = date("Ymd");
$save_path .= $ymd . "/";
$save_url .= $ymd . "/";
if (!file_exists($save_path)) {
mkdir($save_path);
}
// 新文件名
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
// 移动文件
$file_path = $save_path . $new_file_name;
if (move_uploaded_file($tmp_name, $file_path) === false) {
$this->alert("上传文件失败。");
}
@chmod($file_path, 0644);
$file_url = $save_url . $new_file_name;
header('Content-type: text/html; charset=UTF-8');
echo json_encode(array('error' => 0, 'url' => $file_url));
exit;
}
}
public function alert($msg) {
header('Content-type: text/html; charset=UTF-8');
echo json_encode(array('error' => 1, 'message' => $msg));
exit;
}
}