Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
YOHOOD
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
whb
9 years ago
Commit
3842b1d2ef54c1bf49574616f495641c26395036
1 parent
b6faeae9
上传代码
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
264 additions
and
264 deletions
website/controller/Upload.class.php
website/controller/Upload.class.php
View file @
3842b1d
<?php
/**
* 默认控制器
*/
class
Controller_Upload
extends
Controller_Abstract
{
/**
* @var integer 上传成功返回的编码
* @since 1.1
*/
const
SUCCESS_UPLOAD_CODE
=
12
;
/**
* 后台插件上传图片首页
*
*/
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'
,
'xml'
);
// 验证是否是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
;
}
<?php
/**
* 默认控制器
*/
class
Controller_Upload
extends
Controller_Abstract
{
/**
* @var integer 上传成功返回的编码
* @since 1.1
*/
const
SUCCESS_UPLOAD_CODE
=
12
;
/**
* 后台插件上传图片首页
*
*/
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;
}
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment