shopxo/app/service/UeditorService.php

323 lines
11 KiB
PHP
Raw Normal View History

2019-08-06 18:25:00 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2019-08-06 18:25:00 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2019-08-06 18:25:00 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use app\service\ResourcesService;
2021-07-06 23:57:03 +08:00
use app\service\SystemBaseService;
2019-08-06 18:25:00 +08:00
/**
* 百度编辑器附件服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class UeditorService
{
private static $current_action;
private static $current_config;
private static $params;
private static $path_type;
/**
* 请求入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-08-06
* @desc description
* @param [array] $params [输入参数]
*/
public static function Run($params = [])
{
// 配置信息
self::$params = $params;
2021-07-18 23:42:10 +08:00
self::$current_config = MyConfig('ueditor');
2019-08-06 18:25:00 +08:00
self::$current_action = isset($params['action']) ? $params['action'] : '';
2019-08-07 00:14:13 +08:00
self::$path_type = isset($params['path_type']) ? $params['path_type'] : PathToParams('path_type', 'other');
2019-08-06 18:25:00 +08:00
// action
2023-02-11 17:01:04 +08:00
$ret = DataReturn(MyLang('common_service.ueditor.action_error_tips'), -1);
2019-08-06 18:25:00 +08:00
switch(self::$current_action)
{
// 配置信息
case 'config':
2020-03-05 12:53:01 +08:00
$ret = DataReturn('success', 0, self::$current_config);
2019-08-06 18:25:00 +08:00
break;
/* 上传图片 */
case 'uploadimage':
/* 上传涂鸦 */
case 'uploadscrawl':
/* 上传视频 */
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
2020-03-04 18:48:09 +08:00
$ret = self::ActionUpload();
2019-08-06 18:25:00 +08:00
break;
/* 列出图片 */
case 'listimage':
/* 列出文件 */
case 'listfile':
/* 列出视频 */
case 'listvideo':
2020-03-04 18:48:09 +08:00
$ret = self::ActionList();
2019-08-06 18:25:00 +08:00
break;
/* 抓取远程文件 */
case 'catchimage':
2020-03-04 18:48:09 +08:00
$ret = self::ActionCrawler();
2019-08-06 18:25:00 +08:00
break;
/* 删除文件 */
case 'deletefile':
2020-03-04 18:48:09 +08:00
$ret = self::DeleteFile();
2020-03-05 12:53:01 +08:00
break;
2019-08-06 18:25:00 +08:00
}
2020-03-04 18:48:09 +08:00
return $ret;
2019-08-06 18:25:00 +08:00
}
/**
* 文件删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-10
* @desc description
*/
private static function DeleteFile()
{
2020-03-04 18:48:09 +08:00
return ResourcesService::AttachmentDelete(input());
2019-08-06 18:25:00 +08:00
}
/**
2023-02-11 17:01:04 +08:00
* 上传配置
2019-08-06 18:25:00 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-17T22:45:06+0800
*/
private static function ActionUpload()
{
$attachment_type = "file";
switch(htmlspecialchars(self::$current_action))
{
case 'uploadimage':
2023-02-11 17:01:04 +08:00
$temp_config = [
"pathFormat" => self::$current_config['imagePathFormat'],
"maxSize" => self::$current_config['imageMaxSize'],
"allowFiles" => self::$current_config['imageAllowFiles']
];
2019-08-06 18:25:00 +08:00
$field_name = self::$current_config['imageFieldName'];
$attachment_type = "image";
break;
case 'uploadscrawl':
2023-02-11 17:01:04 +08:00
$temp_config = [
"pathFormat" => self::$current_config['scrawlPathFormat'],
"maxSize" => self::$current_config['scrawlMaxSize'],
"allowFiles" => self::$current_config['scrawlAllowFiles'],
"oriName" => "scrawl.png"
];
2019-08-06 18:25:00 +08:00
$field_name = self::$current_config['scrawlFieldName'];
$attachment_type = "scrawl";
break;
case 'uploadvideo':
2023-02-11 17:01:04 +08:00
$temp_config = [
"pathFormat" => self::$current_config['videoPathFormat'],
"maxSize" => self::$current_config['videoMaxSize'],
"allowFiles" => self::$current_config['videoAllowFiles']
];
2019-08-06 18:25:00 +08:00
$field_name = self::$current_config['videoFieldName'];
$attachment_type = "video";
break;
case 'uploadfile':
default:
2023-02-11 17:01:04 +08:00
$temp_config = [
"pathFormat" => self::$current_config['filePathFormat'],
"maxSize" => self::$current_config['fileMaxSize'],
"allowFiles" => self::$current_config['fileAllowFiles']
];
2019-08-06 18:25:00 +08:00
$field_name = self::$current_config['fileFieldName'];
$attachment_type = "file";
}
/* 生成上传实例对象并完成上传 */
$up = new \base\Uploader($field_name, $temp_config, $attachment_type);
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
* "url" => "", //返回的地址
* "path" => "", //绝对地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* "hash" => "", //sha256值
* )
*/
$data = $up->getFileInfo();
if(isset($data['state']) && $data['state'] == 'SUCCESS')
{
if($attachment_type == 'scrawl')
{
$attachment_type = 'image';
}
2019-08-06 18:25:00 +08:00
$data['type'] = $attachment_type;
$data['path_type'] = self::$path_type;
2020-03-04 18:48:09 +08:00
return ResourcesService::AttachmentAdd($data);
2019-08-06 18:25:00 +08:00
}
2023-02-11 17:01:04 +08:00
return DataReturn(isset($data['state']) ? $data['state'] : MyLang('upload_fail'), -1);
2019-08-06 18:25:00 +08:00
}
/**
2023-02-11 17:01:04 +08:00
* 文件列表
2019-08-06 18:25:00 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-17T22:55:16+0800
*/
private static function ActionList()
{
/* 判断类型 */
switch(self::$current_action)
{
/* 列出视频 */
case 'listvideo':
$allow_files = self::$current_config['videoManagerAllowFiles'];
$list_size = self::$current_config['videoManagerListSize'];
$path = self::$current_config['videoManagerListPath'];
break;
/* 列出文件 */
case 'listfile':
$allow_files = self::$current_config['fileManagerAllowFiles'];
$list_size = self::$current_config['fileManagerListSize'];
$path = self::$current_config['fileManagerListPath'];
break;
/* 列出图片 */
case 'listimage':
default:
$allow_files = self::$current_config['imageManagerAllowFiles'];
$list_size = self::$current_config['imageManagerListSize'];
$path = self::$current_config['imageManagerListPath'];
}
$allow_files = substr(str_replace(".", "|", join("", $allow_files)), 1);
/* 获取参数 */
2020-03-05 12:53:01 +08:00
$size = isset(self::$params['size']) ? intval(self::$params['size']) : $list_size;
$start = isset(self::$params['start']) ? intval(self::$params['start']) : 0;
2019-08-06 18:25:00 +08:00
$end = $start + $size;
// 参数
$params = [
'm' => $start,
'n' => $size,
'where' => [
['type', '=', substr(self::$current_action, 4)],
['path_type', '=', self::$path_type]
],
];
// 搜索关键字
if(!empty(self::$params['keywords']))
{
$params['where'][] = ['original', 'like', '%'.self::$params['keywords'].'%'];
}
2020-03-04 18:48:09 +08:00
// 总数
$total = ResourcesService::AttachmentTotal($params['where']);
2019-08-06 18:25:00 +08:00
// 获取数据
2020-03-04 18:48:09 +08:00
if($total > 0)
2019-08-06 18:25:00 +08:00
{
2020-03-04 18:48:09 +08:00
$ret = ResourcesService::AttachmentList($params);
if(!empty($ret['data']))
{
return DataReturn('success', 0, [
'start' => $start,
'total' => $total,
'list' => $ret['data'],
]);
}
2019-08-06 18:25:00 +08:00
}
2020-03-04 18:48:09 +08:00
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('no_data'), -1);
2019-08-06 18:25:00 +08:00
}
/**
2023-02-11 17:01:04 +08:00
* 抓取远程文件
2019-08-06 18:25:00 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-17T23:08:29+0800
*/
private static function ActionCrawler()
{
$temp_config = array(
"pathFormat" => self::$current_config['catcherPathFormat'],
"maxSize" => self::$current_config['catcherMaxSize'],
"allowFiles" => self::$current_config['catcherAllowFiles'],
"oriName" => "remote.png"
);
$field_name = self::$current_config['catcherFieldName'];
// 当前站点域名或者附件域名不下载
2021-07-06 23:57:03 +08:00
$attachment_host = GetUrlHost(SystemBaseService::AttachmentHost());
// 抓取远程图片
2023-02-11 17:01:04 +08:00
$list = [];
2019-08-06 18:25:00 +08:00
$source = isset(self::$params[$field_name]) ? self::$params[$field_name] : self::$params[$field_name];
foreach($source as $imgUrl)
{
if(GetUrlHost($imgUrl) != $attachment_host)
2019-08-06 18:25:00 +08:00
{
$up = new \base\Uploader($imgUrl, $temp_config, "remote");
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
* "url" => "", //返回的地址
* "path" => "", //绝对地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* "hash" => "", //sha256值
* )
*/
$data = $up->getFileInfo();
if(isset($data['state']) && $data['state'] == 'SUCCESS')
2019-08-06 18:25:00 +08:00
{
$data['type'] = 'image';
$data['path_type'] = self::$path_type;
$ret = ResourcesService::AttachmentAdd($data);
if($ret['code'] == 0)
{
2021-07-27 12:29:31 +08:00
$ret['data']['source'] = htmlspecialchars($imgUrl);
array_push($list, $ret['data']);
}
2019-08-06 18:25:00 +08:00
}
}
}
2020-03-04 18:48:09 +08:00
if(!empty($list))
{
return DataReturn('success', 0, $list);
}
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('no_data'), -1);
2019-08-06 18:25:00 +08:00
}
}
?>