Ueditor.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php namespace App\Http\Controllers\Admin;
  2. /**
  3. * 百度编辑器接口
  4. *
  5. *
  6. * @author 刘相欣
  7. *
  8. */
  9. class Ueditor extends Auth
  10. {
  11. protected $config;
  12. protected $type;
  13. protected $_basepath = 'uploads/ueditor/';
  14. /**
  15. * 将内容存到Storage中,返回转存后的文件路径
  16. *
  17. * @param String $action 方法
  18. * @param String $ext 后缀
  19. * @param String $content 内容
  20. */
  21. private function save_storage_content($action,$ext,$content){
  22. // 后缀或者内容为空
  23. if ( !$ext || !$content ) return ['error'=>'上传内容或者文件格式有误'];
  24. // 获得用户路径
  25. $user_path = $this->_get_user_path($action);
  26. // 如果不存在目录
  27. if( !is_dir($user_path) ) mkdir($user_path,0755,true);
  28. // 文件名称
  29. $filename = $user_path.uniqid().'.'. $ext;
  30. // 写入数据
  31. $result = @file_put_contents($filename,$content);
  32. // 失败提示
  33. if( !$result ) return ['error'=>'上传失败'];
  34. // 返回路径
  35. return request()->root().'/'.$filename;
  36. }
  37. /**
  38. * 上传
  39. */
  40. public function upload()
  41. {
  42. // 接收参数
  43. $this->type = request('edit_type','');
  44. $action = request('action');
  45. // 获取配置
  46. $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./static/ueditor/config.json")), true);
  47. // 赋值通用
  48. $this->config = $CONFIG;
  49. // 判断方法,调用对应方式
  50. switch ($action) {
  51. case 'config':
  52. $result = response()->json($CONFIG);
  53. break;
  54. /* 上传图片 */
  55. case 'uploadimage':
  56. $result = $this->_ueditor_upload(['size' => 1024*1024*100,'ext'=>['jpg', 'gif', 'png', 'jpeg']],'images');
  57. break;
  58. /* 上传涂鸦 */
  59. case 'uploadscrawl':
  60. $result = $this->_ueditor_upload_scrawl('scrawl');
  61. break;
  62. /* 上传视频 */
  63. case 'uploadvideo':
  64. $result = $this->_ueditor_upload(['size' => 1024*1024*100 ,'ext'=>['mp4', 'avi', 'wmv','rm','rmvb','mkv']],'video');
  65. break;
  66. /* 上传文件 */
  67. case 'uploadfile':
  68. $result = $this->_ueditor_upload(['ext'=>['txt','pdf','doc','docx','xls','xlsx','zip','rar','ppt','pptx']],'file');
  69. break;
  70. /* 列出图片 */
  71. case 'listimage':
  72. $result = $this->_ueditor_list('images');
  73. break;
  74. /* 列出文件 */
  75. case 'listfile':
  76. $result = $this->_ueditor_list('file');
  77. break;
  78. /* 抓取远程文件 */
  79. case 'catchimage':
  80. $result = $this->_ueditor_upload_catch();
  81. break;
  82. default:
  83. $result = response()->json(['state'=>'请求地址错误']);
  84. break;
  85. }
  86. // 返回结果
  87. return $result;
  88. }
  89. /**
  90. * 编辑列表
  91. *
  92. * @param String $action 方法
  93. *
  94. *
  95. * */
  96. private function _ueditor_list($action)
  97. {
  98. // 配置
  99. $config = $this->config;
  100. // 判断文件后缀
  101. $ext_allow = $config['imageAllowFiles'];
  102. // 默认图片列表长度
  103. $listSize = $this->config['imageManagerListSize'];
  104. /* 判断类型 */
  105. if( $action == 'file' ) {
  106. // 如果是文件
  107. $listSize = $this->config['fileManagerListSize'];
  108. // 判断文件后缀
  109. $ext_allow = $config['fileAllowFiles'];
  110. }
  111. // 允许的文件对象
  112. foreach ($ext_allow as $key => $value) {
  113. // 去除点
  114. $ext_allow[$key] = ltrim($value,'.');
  115. }
  116. // 合并字符串
  117. $ext_allow = implode(',',$ext_allow);
  118. // 列表长度
  119. $size = request('size',$listSize);
  120. // 开始
  121. $start = request('start',0);
  122. // 获得用户路径
  123. $user_path = $this->_get_user_path($action);
  124. // 取得文件
  125. $files = (array) glob($user_path.'*.{'.$ext_allow.'}', GLOB_BRACE);
  126. // 循环处理
  127. foreach ($files as $key => $value) {
  128. // 组装路径
  129. $files[$key] = request()->root().'/'.$value;
  130. }
  131. // 总数量
  132. $total = count($files);
  133. // 列表数据
  134. $list = array_slice($files,$start,$size);
  135. // 返回数据
  136. return response()->json(['state'=>'SUCCESS','list'=>$list,'start'=>$start,'total'=>$total]);
  137. }
  138. /**
  139. * 用户目录
  140. *
  141. * */
  142. private function _get_user_path($action){
  143. // 当前登录用户
  144. if( admin('uid') ) return $this->_basepath.$action.'/admin/'.admin('uid').'/'.date('Ymd').'/';
  145. }
  146. /**
  147. * 上传
  148. *
  149. */
  150. private function _ueditor_upload($config=[],$action='') {
  151. // 如果没有文件
  152. if( !$action ) return response()->json(['state'=>'上传方法有误','url'=>'','title'=>'','original'=>'']);
  153. // 如果没有文件
  154. if( !request()->hasFile('upfile') ) return response()->json(['state'=>'未接收到文件','url'=>'','title'=>'','original'=>'']);
  155. // 本地上传
  156. $file = request()->file('upfile');
  157. // 如果没有文件
  158. if( !$file->isValid() ) return response()->json(['state'=>'上传错误','url'=>'','title'=>'','original'=>'']);
  159. // 配置合并配置
  160. if( !empty($config) ) $config = array_merge($this->config,$config);
  161. // 获取文件后缀名
  162. $ext = $file->extension();
  163. // 获取文件大小
  164. $size = $file->getSize();
  165. // 验证文件大小
  166. if( isset($config['size']) && $size >= $config['size'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']);
  167. // 验证文件格式
  168. if( isset($config['ext']) && !in_array($ext,$config['ext'] ) ) return response()->json(['state'=>'请上传允许的格式','url'=>'','title'=>'','original'=>'']);
  169. // 获取文件名
  170. $filename = ltrim($file->getClientOriginalName(),'/');
  171. // 文件名
  172. $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8');
  173. // 获得用户路径
  174. $user_path = $this->_get_user_path($action);
  175. // 保存文件
  176. $info = $file->move($user_path,$filename);
  177. // 如果有错误
  178. if( !$info ) return ['error'=>$file->getError()];
  179. // 移动失败
  180. if( isset($objectPath['error']) ) return response()->json(['state'=>$objectPath['error'],'url'=>'','title'=>'','original'=>'']);
  181. // 数据
  182. $url = request()->root().'/'.$user_path.$filename;
  183. // 状态成功
  184. return response()->json(['state' => 'SUCCESS','url'=>$url,'title'=>$filename,'original'=>$filename]);
  185. }
  186. /**
  187. * 涂鸦
  188. *
  189. */
  190. private function _ueditor_upload_scrawl($action='scrawl'){
  191. // 接收数据
  192. $data = request($this->config['scrawlFieldName'],'');
  193. // 未接收到涂鸦数据
  194. if ( empty ($data) ) return response()->json(['state'=>'涂鸦内容不能为空','url'=>'','title'=>'','original'=>'']);
  195. // base64转码接收到的涂鸦数据
  196. $img = base64_decode($data);
  197. // 大小
  198. $size = strlen($img);
  199. // 验证文件大小
  200. if( isset($this->config['scrawlMaxSize']) && $size >= $this->config['scrawlMaxSize'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']);
  201. // 调用本地存储方法
  202. $savepath = $this->save_storage_content($action,'png',$img);
  203. // 存储失败
  204. if ( isset($savepath['error']) ) return response()->json(['state'=>$savepath['error'],'url'=>'','title'=>'','original'=>'']);
  205. // 状态成功
  206. return response()->json(['state' => 'SUCCESS','url'=>$savepath,'title'=>'','original'=>'']);
  207. }
  208. //抓取远程文件
  209. private function _ueditor_upload_catch(){
  210. // 暂不支持
  211. return response()->json(['state' => '暂不支持抓取远程文件','list' => null]);
  212. }
  213. }