-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathUEditorAction.php
286 lines (262 loc) · 9.24 KB
/
UEditorAction.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
<?php
namespace kucha\ueditor;
use Yii;
use yii\base\Action;
use yii\helpers\ArrayHelper;
class UEditorAction extends Action
{
/**
* @var array
*/
public $config = [];
public function init()
{
//close csrf
Yii::$app->request->enableCsrfValidation = false;
//默认设置
$_config = require(__DIR__ . '/config.php');
//load config file
$this->config = ArrayHelper::merge($_config, $this->config);
parent::init();
}
public function run()
{
$this->handleAction();
}
/**
* 处理action
*/
protected function handleAction()
{
$action = Yii::$app->request->get('action');
switch ($action) {
case 'config':
$result = json_encode($this->config);
break;
/* 上传图片 */
case 'uploadimage':
/* 上传涂鸦 */
case 'uploadscrawl':
/* 上传视频 */
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
$result = $this->actionUpload();
break;
/* 列出图片 */
case 'listimage':
/* 列出文件 */
case 'listfile':
$result = $this->actionList();
break;
/* 抓取远程文件 */
case 'catchimage':
$result = $this->actionCrawler();
break;
default:
$result = json_encode([
'state' => '请求地址出错'
]);
break;
}
/* 输出结果 */
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode([
'state' => 'callback参数不合法'
]);
}
} else {
echo $result;
}
}
/**
* 上传
* @return string
*/
protected function actionUpload()
{
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadimage':
$config = [
"pathRoot" => ArrayHelper::getValue($this->config, "imageRoot", $_SERVER['DOCUMENT_ROOT']),
"pathFormat" => $this->config['imagePathFormat'],
"maxSize" => $this->config['imageMaxSize'],
"allowFiles" => $this->config['imageAllowFiles']
];
$fieldName = $this->config['imageFieldName'];
break;
case 'uploadscrawl':
$config = [
"pathRoot" => ArrayHelper::getValue($this->config, "scrawlRoot", $_SERVER['DOCUMENT_ROOT']),
"pathFormat" => $this->config['scrawlPathFormat'],
"maxSize" => $this->config['scrawlMaxSize'],
"allowFiles" => $this->config['scrawlAllowFiles'],
"oriName" => "scrawl.png"
];
$fieldName = $this->config['scrawlFieldName'];
$base64 = "base64";
break;
case 'uploadvideo':
$config = [
"pathRoot" => ArrayHelper::getValue($this->config, "videoRoot", $_SERVER['DOCUMENT_ROOT']),
"pathFormat" => $this->config['videoPathFormat'],
"maxSize" => $this->config['videoMaxSize'],
"allowFiles" => $this->config['videoAllowFiles']
];
$fieldName = $this->config['videoFieldName'];
break;
case 'uploadfile':
default:
$config = [
"pathRoot" => ArrayHelper::getValue($this->config, "fileRoot", $_SERVER['DOCUMENT_ROOT']),
"pathFormat" => $this->config['filePathFormat'],
"maxSize" => $this->config['fileMaxSize'],
"allowFiles" => $this->config['fileAllowFiles']
];
$fieldName = $this->config['fileFieldName'];
break;
}
/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64);
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
* "url" => "", //返回的地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* )
*/
/* 返回数据 */
return json_encode($up->getFileInfo());
}
/**
* 获取已上传的文件列表
* @return string
*/
protected function actionList()
{
/* 判断类型 */
switch ($_GET['action']) {
/* 列出文件 */
case 'listfile':
$allowFiles = $this->config['fileManagerAllowFiles'];
$listSize = $this->config['fileManagerListSize'];
$path = $this->config['fileManagerListPath'];
break;
/* 列出图片 */
case 'listimage':
default:
$allowFiles = $this->config['imageManagerAllowFiles'];
$listSize = $this->config['imageManagerListSize'];
$path = $this->config['imageManagerListPath'];
}
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
/* 获取参数 */
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = (int)$start + (int)$size;
/* 获取文件列表 */
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
$files = $this->getfiles($path, $allowFiles);
if (!count($files)) {
return json_encode([
"state" => "no match file",
"list" => [],
"start" => $start,
"total" => count($files)
]);
}
/* 获取指定范围的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = []; $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
//for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
// $list[] = $files[$i];
//}
/* 返回数据 */
$result = json_encode([
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files)
]);
return $result;
}
/**
* 抓取远程图片
* @return string
*/
protected function actionCrawler()
{
/* 上传配置 */
$config = [
"pathFormat" => $this->config['catcherPathFormat'],
"maxSize" => $this->config['catcherMaxSize'],
"allowFiles" => $this->config['catcherAllowFiles'],
"oriName" => "remote.png"
];
$fieldName = $this->config['catcherFieldName'];
/* 抓取远程图片 */
$list = [];
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$item = new Uploader($imgUrl, $config, "remote");
$info = $item->getFileInfo();
array_push($list, [
"state" => $info["state"],
"url" => $info["url"],
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
]);
}
/* 返回抓取数据 */
return json_encode([
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
]);
}
/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param $allowFiles
* @param array $files
* @return array|null
*/
protected 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;
}
}