Upload.class.php
8.6 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
<?php
/**
* 默认控制器
*/
class Controller_Upload extends Controller_Abstract
{
/**
* @var integer 上传成功返回的编码
* @since 1.1
*/
const SUCCESS_UPLOAD_CODE = 12;
public function init()
{
$admin = Util_Utils_SafeCookie::get('_admin');
if(empty($admin))
{
die('页面不存在');
}
}
/**
* 后台插件上传图片首页
*
*/
public function indexAction()
{
$data = array('error' => 0, 'url' => '', 'message'=> '', 'relaPath' => '');
$dir = strtolower($this->_request->query('dir'));
if($dir == 'media' || isset($_FILES['video'])) //视频上传
{
$video = json_decode(Lib_Video::saveVideo($_FILES), true);
$data['relaPath'] = $video['video_url'];
$data['url'] = Lib_Video::getVideoUrl($video['video_url']);
}
else
{
$url = Lib_Images::saveImage($_FILES);
if(empty($url))
{
$data['error'] = 1;
$data['message'] = '上传文件失败';
}
else
{
$data['relaPath'] = $url;
$data['url'] = Lib_Images::getImageUrl($url, 'source', 'fragmentimg');
}
}
return json_encode($data);
}
/**
* 后台插件上传视频处理
*
* @return json
*/
public function videoAction()
{
$data = array('error' => 0, 'url' => '', 'message'=> '', 'relaPath' => '');
if (isset($_GET["upload_ret"]))
{
$decodedRet = base64_decode( $_GET["upload_ret"]);
$retObj = json_decode($decodedRet, true);
$data['relaPath'] = $retObj['key'];
$data['url'] = VIDEO_SITE_DOMAIN.'/'.$retObj['key'];
}
if(isset($_GET['error']))
{
$data['error'] = 1;
$data['message'] = '上传文件失败';
}
return json_encode($data);
}
/**
* 上传文件[流]
*
* @param string key (密钥)
* @param string format (返回格式xml|json)
*/
public function upfileAction()
{
$format = $this->_request->query('format', 'json');
// 验证是否是post提交
if (!$this->_request->isPost())
{
return $this->_response($format, Config_Code_Upload::$errorPost['code'], Config_Code_Upload::$errorPost['msg']);
}
// 验证key密钥是否有效
$key = $this->_request->query('key', '');
if ($key === '' || !is_string($key))
{
return $this->_response($format, Config_Code_Upload::$errorNoKey['code'], Config_Code_Upload::$errorNoKey['msg']);
}
$key = Util_Utils_Function::base64_str_decode($key);
// 验证key密钥中是否存在项目
$keyData = json_decode(Util_Utils_AuthCode::decode($key, Config_File_Upload::$key), true);
if (!array_key_exists('_project', $keyData))
{
return $this->_response($format, Config_Code_Upload::$errorValidKey['code'], Config_Code_Upload::$errorValidKey['msg']);
}
// 验证Key密钥中项目是否存在
$project = $keyData['_project'];
$projectConfig = Lib_Images::genConfig($project);
if (array() === $projectConfig)
{
return $this->_response($format, Config_Code_Upload::$errorNoProject['code'], Config_Code_Upload::$errorNoProject['msg'] . $project);
}
$tag = $this->_request->query('tag', '');
if($tag == 'flash')
{
//flash上传,type有问题
foreach($_FILES as $key => $file)
{
if($file['type'] == 'application/octet-stream')
{
$_FILES[$key]['type'] = 'image/jpeg';
}
}
}
// 上传文件[流]操作
if (array() !== $_FILES) // 文件
{
$upload = new Util_Upload_File($_FILES, $projectConfig['serverPath']);
$upload->setFormat($projectConfig['format']);
}
elseif (false !== ($file = $this->_request->requestRawBody())) // 文件流
{
//读取头信息高位
$fileExtCode = substr($file, 0, 2);
$format = '';
if(strlen($fileExtCode))
{
$fileExtCode = unpack('C2char',$fileExtCode);
$fileExtCode = intval(implode('',$fileExtCode));
}
$format = Util_Common_Io_FileExt::getMine(Util_Common_Io_FileExt::getExtByCode($fileExtCode));
if(!preg_match($projectConfig['format'], $format))
{
return $this->_response($format, 10, '不支持上传的文件类型');
}
$upload = new Util_Upload_FileStream($file, $projectConfig['serverPath']);
}
else // 文件[流]不存在
{
return $this->_response($format, Config_Code_Upload::$errorNoUploadFile['code'], Config_Code_Upload::$errorNoUploadFile['msg']);
}
$upload->setMaxFileSize($projectConfig['upload_max_size']);
$upload->setPrefix($projectConfig['randNode']);
if (isset($keyData['file_path']) && is_string($keyData['file_path']))
{
$upload->setNewFile($keyData['file_path']);
}
// 验证文件是否有效
$check = $upload->check();
if ($check['code'] !== 0)
{
return $this->_response($format, $check['code'], $check['mes']);
}
// 上传并构建返回结果, 同时检查上传文件是否有错
$result = $this->genResult($format, $upload, $check['result'], $project, $projectConfig);
if (isset($result['hasError']))
{
return $this->_response($format, Config_Code_Upload::$errorUploadFailed['code'], Config_Code_Upload::$errorUploadFailed['msg']);
}
return $this->_response($format, self::SUCCESS_UPLOAD_CODE, $check['mes'], $result);
}
/**
* 上传并构建返回的结果
*
* @param object $upload (上传类)
* @param array $files (文件列表)
* @return array
*/
protected function genResult($format, &$upload, $files, $project, &$projectConfig)
{
$result = array();
$file = null;
$writed = array();
$isJson = strtolower($format) === 'json';
foreach ($files as $id => $info)
{
$writed = $upload->writeOne($id);
if ($writed['code'] !== self::SUCCESS_UPLOAD_CODE)
{
$result['hasError'] = true;
break;
}
$file = $writed['result']['hit'][$id];
$result[$id] = $this->_genJsonResult($file, $project, $projectConfig);
}
return $result;
}
/**
* 返回默认的图片信息
*
* @return array
*/
private function _getDefaultMeta()
{
return array('width' => 200, 'height' => 300, 'size' => 200000, 'mine' => 'image/jpeg',);
}
/**
* 构建JSON格式的返回结果
*
* @param string $url (相对地址)
* @param string $project (项目名称)
* @param array $projectConfig (项目配置)
* @return void
*/
private function _genJsonResult($params, $project, &$projectConfig)
{
$result = array();
// 预处理生成缩略图片
$data = Lib_Images::getImageInfo($params['relaPath'], $project);
if (array() === $data)
{
$data = $this->_getDefaultMeta();
}
$result['relaPath'] = $params['relaPath'];
$result['sourcePath'] = $projectConfig['sourceUrl'] . $params['relaPath'];
$result['absPath'] = $result['sourcePath'];
$result['width'] = $data['width'];
$result['height'] = $data['height'];
$result['size'] = $data['size'];
$result['mine'] = $data['mine'];
return $result;
}
/**
* 构建返回结果
*
* @param string $format (返回格式 xml|json)
* @param integer $code (错误编号)
* @param string $msg (错误提示)
* @param mixed $data (返回数据)
* @return string | void
*/
private function _response($format = 'xml', $code = 0, $msg = '', $data = '')
{
$response = '';
switch (strtolower($format))
{
case 'json':
header('Content-Type: application/json; charset=utf-8');
$response = Lib_Utils::result($code, $msg, $data);
break;
case 'xml':
default:
header('Content-Type: text/xml; charset=utf-8');
$response = Util_Utils_Convert::array2xml(array('Code' => $code, 'Msg' => $msg, 'Data' => $data,), 'File');
$response = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
{$response}
</Response>
EOD;
break;
}
header("Access-Control-Allow-Origin: *");//让html5支持跨域上传
return $response;
}
}