"请先登录", ); return Response::json($ue_result); };*/ /* end 检查是否登陆 */ $action = request()->get('action', ''); switch ($action) { case 'config': $cfg = self::getJsonConfig(); return response()->json($cfg); break; case 'UploadFile': case 'UploadImage': case 'UploadScrawl': case 'UploadSnapScreen': case 'UploadVideo': return self::actionUploadImage(); break; case 'CatchImage': $ue_result = array( 'state' => 'ERROR', 'list' => array(), ); //return response()->json($ue_result); return self::actionCatchImage(); //启用远程采集图片,取消这个注释 break; case 'ListImage': return self::actionListImage(1); break; case 'ListFile': return self::actionListImage(2); break; default: $ue_result = array( "state" => "非法请求Action", ); return response()->json($ue_result); break; } } private function actionUploadImage() { $Plupload = new PluploadController(); $arr_result = $Plupload->uEditorUploadResult(); $ue_result = array( 'state' => '', //状态 'url' => '', //地址 'title' => '', //生成的文件名 'original' => '', //原文件名 ); if ($arr_result['ErrorCode'] < 0) { $ue_result['state'] = $arr_result['Message']; } else { $ue_result['state'] = 'SUCCESS'; $ue_result['url'] = $arr_result['Content'][0]['link_url']; $ue_result['title'] = $arr_result['Content'][0]['file_origin_name']; $ue_result['original'] = $arr_result['Content'][0]['file_origin_name']; } return response()->json($ue_result); } private function actionUploadFile() { $Plupload = new PluploadController(); $arr_result = $Plupload->uEditorUploadResult(); $ue_result = array( 'state' => '', //状态 'url' => '', //地址 'title' => '', //生成的文件名 'original' => '', //原文件名 ); if ($arr_result['ErrorCode'] < 0) { $ue_result['state'] = $arr_result['Message']; } else { $ue_result['state'] = 'SUCCESS'; $ue_result['url'] = $arr_result['Content'][0]['link_url']; $ue_result['title'] = $arr_result['Content'][0]['file_origin_name']; $ue_result['original'] = $arr_result['Content'][0]['file_origin_name']; } return response()->json($ue_result); } private function actionCatchImage() { $request = request(); $arr_list = array(); $arr_return = array( 'state' => 'SUCCESS', 'list' => $arr_list, ); $cfg = self::getJsonConfig(); $sources = request()->get($cfg['catcherFieldName'], array()); if ($sources) { $upload_sys_info = get_upload_sys_info(); $upload_default_params = array( 'manager_uid' => $this->ctManagerUid, 'user_type' => 2, //0=游客, 1=前台用户, 2=后台用户 'is_appointed' => 0, //0-不固定高宽 1-固定高宽 'is_water' => 0, //0-无水印 1-加水印 'max_width' => 0, //最大宽度 'max_height' => 0, //最大高度 'max_size' => $upload_sys_info['upload_max_size'], //允许最大上传尺寸,单位K 'ext_type' => 1, //扩展名类型 0-不限制 1-图片 2-附件 3-flash 4-多媒体 'thumb_width' => array(160, 600, 120), 'thumb_height' => array(1600, 600, 120), 'thumb_water' => array(0, 0, 0), ); $scraper_referer = $request->get('scraper_referer', ''); //爬虫来源. $module_alias = $request->get('module_alias', ''); //模块 $related_id = intval($request->get('related_id', 0)); //关联ID $item_alias = $request->get('item_alias', ''); //子项别名 $item_id = intval($request->get('item_id', 0)); //子项ID $max_size = intval($request->get('max_size', 0)); //上传最大尺寸限制 $thumb_appointed = $request->get('thumb_appointed', ''); //是否强制尺寸高宽 $thumb_water = $request->get('thumb_water', ''); //是否加水印 $thumb_num = intval($request->get('thumb_num', 0)); //缩略图数量 $thumb_max_width = $request->get('thumb_max_width', ''); //缩略图最大宽度,多组用半角逗号分隔开 $thumb_max_height = $request->get('thumb_max_height', '');//缩略图最大高度,多组用半角逗号分隔开 $ext_type = intval($request->get('ext_type', 1)); //文件扩展名限制类型 0-不限制 1-图片 2-附件 3-flash 4-多媒体 $user_type = 2; //0=游客, 1=前台用户, 2=后台用户 $server_type = 0; //服务器类型, 0=本地服务器,1=自由可控远程服务器,3=第三方比如七牛 $need_origin_pic = 0; $unlink_origin_pic = $need_origin_pic == 0 ? true : false; //是否删除原图 $server_bucket = ''; //第三方空间名 $server_local_alias = 'img'; $origin_alias = 'web'; //判断外部提交的最大尺寸是否超出系统允许的最大尺寸 if ($max_size > $upload_default_params['max_size']) { $max_size = $upload_default_params['max_size']; } elseif (empty($max_size)) { $max_size = $upload_default_params['max_size']; } $water_picture = public_path('assets/sys/watermark.png'); //水印图 $water_picture_exists = File::exists($water_picture) ? true : false; $arr_thumb_max_width = $arr_thumb_max_height = $arr_thumb_water = $arr_thumb_appointed = array(); $arr_thumb_max_width = explode(',', $thumb_max_width); $arr_thumb_max_height = explode(',', $thumb_max_height); $arr_thumb_water = explode(',', $thumb_water); $arr_thumb_appointed = explode(',', $thumb_appointed); if ($max_size == -1) { $format_max_size = -1; } else { $format_max_size = $max_size * 1024; } //$now_date = Carbon::now()->format('Y-m-d H:i:s'); $arr_mimes = include(storage_path('app/data') . DIRECTORY_SEPARATOR . 'mime.php'); $img_base_url = ''; foreach ($sources as $k => $url) { $arr_head = get_remote_file_info($url, 'all'); if ($arr_head) { //已经存在 $file_size = $arr_head['Content-Length'] ? $arr_head['Content-Length'] : 0; //单位bytes $file_type = $arr_head['Content-Type'] ? $arr_head['Content-Type'] : ''; //文件类型 $file_no_dot_ext = file_ext($url); $file_ext = '.' . $file_no_dot_ext; if (!in_array($file_ext, $cfg['catcherAllowFiles'])) { $arr_list[$k]['state'] = '非法文件扩展名'; continue; } if ($file_size > $cfg['catcherMaxSize']) { $arr_list[$k]['state'] = '尺寸过大'; continue; } //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create( array('http' => array( 'follow_location' => false // don't follow redirects )) ); readfile($url, false, $context); $img = ob_get_contents(); ob_end_clean(); preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $url, $m); $file_origin_name = $m ? $m[1] : ''; $file_size = strlen($img); if ($file_size > $cfg['catcherMaxSize']) { $arr_list[$k]['state'] = '尺寸过大'; continue; } $dateDir = caesar_encode(Carbon::now()->format('Ymd'), '', 3); $file_name = $dateDir . $file_ext; $file_mime = isset($arr_mimes[$file_no_dot_ext]) ? $arr_mimes[$file_no_dot_ext] : 'image/jpeg'; //文件MIME $upload_save_dir = 'attachment'; // 目前写死,以后写配置里 todo. $save_root_path = public_path(''); $arr_path = self::getSaveDir($upload_save_dir, '', $save_root_path); $save_dir_path = $arr_path['path']; //绝对路径 $save_relative_dir_path = $arr_path['relative_path']; //相对路径 $save_native_file_path = $save_dir_path . $file_name; $save_relative_file_path = $save_relative_dir_path . $file_name; //相对路径 if (!File::put($save_native_file_path, $img)) { $arr_list[$k]['state'] = '文件移动失败'; continue; } $img_local_info = getimagesize($save_native_file_path); $width = $height = 0; if ($img_local_info !== false) { $width = $img_local_info[0]; $height = $img_local_info[1]; } $link_url = $img_base_url . $save_relative_file_path; $arr_list[$k] = array( 'state' => 'SUCCESS', 'url' => $link_url, 'size' => $file_size, 'title' => htmlspecialchars($file_name), 'original' => htmlspecialchars($file_origin_name), 'source' => htmlspecialchars($url), ); } else { //不存在 $arr_list[$k]['state'] = '远程图片不存在'; continue; } } //end foreach $arr_return['list'] = $arr_list; } return response()->json($arr_return); } private function actionListImage($type = 1) { $request = request(); $new_start = 0; $page = 1; $arr_list = $arr_data = $condition = $order_by = array(); if ($type == 1) { $condition = array( array('is_pic' => 1) ); } $order_by = array( array('attachment_id', 'DESC'), ); $arr_return = array( 'state' => 'SUCCESS', 'list' => array(), 'start' => 0, 'total' => 0, ); $cfg = self::getJsonConfig(); $sys_page_size = 20; $page_size = $cfg['imageManagerListSize'] ? $cfg['imageManagerListSize'] : $sys_page_size; $size = (int)$request->get('size', $page_size); if ($size < 0) { $size = $page_size; } $start = (int)$request->get('start', 0); if ($start < 0) { $start = 0; } $page = ceil($start / $size) + 1; if ($page < 1) { $page = 1; } $total_items = 0; $arr_return['state'] = 'no match file'; $arr_return['total'] = $total_items; $arr_return['start'] = $start; return response()->json($arr_return); } /** * 获得保存的目录 * @param string $save_path 保存目录 * @param string $sub_dir 子目录 * @param string $root_path 绝对路径根目录 * @return array */ private function getSaveDir($save_path, $sub_dir = '', $root_path = '') { $root_path = rtrim($root_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $dir = trim($save_path, '/') . '/'; if ($sub_dir) { $dir .= $sub_dir . '/'; } $dir_date = date("Ymd") . '/'; $dir .= $dir_date; $relative_path = $dir; $dir = $root_path . $dir; if (!File::isDirectory($dir)) { File::makeDirectory($dir, 0777, true, true); } return array( 'path' => $dir, 'relative_path' => $relative_path, ); } private function getJsonConfig() { $page_size = 20; $upload_sys_info = get_upload_sys_info(); $upload_max_size = $upload_sys_info['upload_max_size'] * 1024; $arr_config = array( //上传图片配置项 'imageActionName' => 'UploadImage', //执行上传图片的action名称 'imageFieldName' => 'upload_files', //提交的图片表单名称 'imageMaxSize' => $upload_max_size, //上传大小限制,单位B 'imageAllowFiles' => array('.gif', '.jpg', '.jpeg', '.png'), //上传图片格式显示 'imageCompressEnable' => true, //是否压缩图片,默认是true 'imageCompressBorder' => 3600, //图片压缩最长边限制 'imageInsertAlign' => 'none', //插入的图片浮动方式 'imageUrlPrefix' => '', //图片访问路径前缀 'imagePathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 //涂鸦图片上传配置项 'scrawlActionName' => 'UploadScrawl', //执行上传涂鸦的action名称 'scrawlFieldName' => 'upload_files', //提交的图片表单名称 'scrawlPathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 'scrawlMaxSize' => $upload_max_size, //上传大小限制,单位B 'scrawlUrlPrefix' => '', //图片访问路径前缀 'scrawlInsertAlign' => 'none', //插入的图片浮动方式 //截图工具上传 'snapscreenActionName' => 'UploadSnapScreen', //执行上传截图的action名称 'snapscreenPathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 'snapscreenUrlPrefix' => '', //图片访问路径前缀 'snapscreenInsertAlign' => 'none', //插入的图片浮动方式 //抓取远程图片配置 'catcherLocalDomain' => array( '127.0.0.1', 'localhost', ), 'catcherActionName' => 'CatchImage', //执行抓取远程图片的action名称 'catcherFieldName' => 'upload_files', //提交的图片列表表单名称 'catcherPathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 'catcherUrlPrefix' => '', //图片访问路径前缀 'catcherMaxSize' => $upload_max_size, //上传大小限制,单位B 'catcherAllowFiles' => array('.gif', '.jpg', '.jpeg', '.png', '.bmp'), //上传图片格式显示 //上传视频配置 'videoActionName' => 'UploadVideo', //执行上传视频的action名称 'videoFieldName' => 'upload_files', //提交的视频表单名称 'videoPathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 'videoUrlPrefix' => '', //视频访问路径前缀 'videoMaxSize' => $upload_max_size, //上传大小限制,单位B,默认100MB 'videoAllowFiles' => array('.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', '.webm', '.mp3', '.wav', '.mid', 'm3u8'), //上传视频格式显示 //上传文件配置 'fileActionName' => 'UploadFile', //controller里,执行上传视频的action名称 'fileFieldName' => 'upload_files', //提交的文件表单名称 'filePathFormat' => '', //上传保存路径,可以自定义保存路径和文件名格式 'fileUrlPrefix' => '', //文件访问路径前缀 'fileMaxSize' => $upload_max_size, //上传大小限制,单位B,默认50MB //上传文件格式显示 'fileAllowFiles' => array( '.gif', '.jpg', '.jpeg', '.png', '.bmp', '.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', 'm3u8', '.webm', '.mp3', '.wav', '.mid', '.rar', '.zip', '.tar', '.gz', '.7z', '.bz2', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.txt', '.md', '.xml' ), //列出指定目录下的图片 'imageManagerActionName' => 'ListImage', //执行图片管理的action名称 'imageManagerListPath' => '', //指定要列出图片的目录 'imageManagerListSize' => $page_size, //每次列出文件数量 'imageManagerUrlPrefix' => '', //图片访问路径前缀 'imageManagerInsertAlign' => 'none', //插入的图片浮动方式 'imageManagerAllowFiles' => array('.gif', '.jpg', '.jpeg', '.png', '.bmp'), //列出的文件类型 //列出指定目录下的文件 'fileManagerActionName' => 'ListFile', //执行文件管理的action名称 'fileManagerListPath' => '', //指定要列出文件的目录 'fileManagerUrlPrefix' => '', //文件访问路径前缀 'fileManagerListSize' => $page_size, //每次列出文件数量 'fileManagerAllowFiles' => array( '.gif', '.jpg', '.jpeg', '.png', '.bmp', '.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', 'm3u8', '.webm', '.mp3', '.wav', '.mid', '.rar', '.zip', '.tar', '.gz', '.7z', '.bz2', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.txt', '.md', '.xml' ), ); return $arr_config; } }