config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($this->app->getRootPath() . "/extend/ueditor/config.json")), true); } public function index() { return json($this->config); } public function config() { $result = json($this->config); return $this->output($result); } public function uploadimage() { $result = $this->upload('uploadimage'); return $this->output($result); } public function uploadscrawl() { $result = $this->upload('uploadscrawl'); return $this->output($result); } public function uploadvideo() { $result = $this->upload('uploadvideo'); return $this->output($result); } public function uploadfile() { $result = $this->upload('uploadfile'); return $this->output($result); } public function listimage() { $result = $this->listfiles('listimage'); return $this->output($result); } public function listfile() { $result = $this->listimage('listfile'); return $this->output($result); } public function catchimage() { return $this->crawler(); } /** * 输出结果 */ public function output($result) { if (isset($_GET["callback"])) { if (preg_match("/^[\w_]+$/", $_GET["callback"])) { return htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { return json(array( 'state' => 'callback参数不合法' )); } } else { return $result; } } /** * 空操作 */ public function _empty() { return json([ 'state' => '请求地址出错' ]); } protected function upload($type = "") { $base64 = "upload"; switch ($type) { case 'uploadimage': $uploadconfig = [ "pathFormat" => $this->config['imagePathFormat'], "maxSize" => $this->config['imageMaxSize'], "allowFiles" => $this->config['imageAllowFiles'] ]; $fieldName = $this->config['imageFieldName']; break; case 'uploadurlimage': $uploadconfig = [ "pathFormat" => $this->config['imagePathFormat'], "maxSize" => $this->config['imageMaxSize'], "allowFiles" => $this->config['imageAllowFiles'] ]; $fieldName = $this->config['imageFieldName']; $base64 = "uploadurlimage"; break; case 'uploadscrawl': $uploadconfig = array( "pathFormat" => $this->config['scrawlPathFormat'], "maxSize" => $this->config['scrawlMaxSize'], "allowFiles" => $this->config['scrawlAllowFiles'], "oriName" => "scrawl.png" ); $fieldName = $this->config['scrawlFieldName']; $base64 = "base64"; break; case 'uploadvideo': $uploadconfig = array( "pathFormat" => $this->config['videoPathFormat'], "maxSize" => $this->config['videoMaxSize'], "allowFiles" => $this->config['videoAllowFiles'] ); $fieldName = $this->config['videoFieldName']; break; case 'uploadfile': default: $uploadconfig = array( "pathFormat" => $this->config['filePathFormat'], "maxSize" => $this->config['fileMaxSize'], "allowFiles" => $this->config['fileAllowFiles'] ); $fieldName = $this->config['fileFieldName']; break; } /* 生成上传实例对象并完成上传 */ $up = new Uploader($fieldName, $uploadconfig, $base64); /** * 得到上传文件所对应的各个参数,数组结构 * array( * "state" => "", //上传状态,上传成功时必须返回"SUCCESS" * "url" => "", //返回的地址 * "title" => "", //新文件名 * "original" => "", //原始文件名 * "type" => "" //文件类型 * "size" => "", //文件大小 * ) */ /* 返回数据 */ return json($up->getFileInfo()); } protected function listfiles($type = "") { /* 判断类型 */ switch ($type) { /* 列出文件 */ case 'listfile': $allowFiles = $this->config['fileManagerAllowFiles']; $listSize = $this->config['fileManagerListSize']; $path = $this->config['fileManagerListPath']; break; /* 列出图片 */ case 'listimage': default: $allowFiles = $this->config['imageManagerAllowFiles']; $listSize = $this->config['imageManagerListSize']; $path = $this->config['imageManagerListPath']; } $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1); /* 获取参数 */ $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize; $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0; $end = $start + $size; /* 获取文件列表 */ $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path; $files = getfiles($path, $allowFiles); if (!count($files)) { return json(array( "state" => "no match file", "list" => array(), "start" => $start, "total" => count($files) )); } /* 获取指定范围的列表 */ $len = count($files); for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) { $list[] = $files[$i]; } //倒序 //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){ // $list[] = $files[$i]; //} /* 返回数据 */ $result = json(array( "state" => "SUCCESS", "list" => $list, "start" => $start, "total" => count($files) )); return $result; } protected function crawler() { set_time_limit(0); /* 上传配置 */ $config = array( "pathFormat" => $this->config['catcherPathFormat'], "maxSize" => $this->config['catcherMaxSize'], "allowFiles" => $this->config['catcherAllowFiles'], "oriName" => "remote.png" ); $fieldName = $this->config['catcherFieldName']; /* 抓取远程图片 */ $list = array(); if (isset($_POST[$fieldName])) { $source = $_POST[$fieldName]; } else { $source = $_GET[$fieldName]; } foreach ($source as $imgUrl) { $item = new Uploader($imgUrl, $config, "remote"); $info = $item->getFileInfo(); array_push($list, array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) )); } /* 返回抓取数据 */ return json(array( 'state' => count($list) ? 'SUCCESS' : 'ERROR', 'list' => $list )); } public function action($action = "") { switch ($action) { case 'config': $result = json($this->config); break; /* 上传图片 */ case 'uploadimage': /* 上传涂鸦 */ case 'uploadscrawl': /* 上传视频 */ case 'uploadvideo': /* 上传文件 */ case 'uploadfile': $result = $this->upload(); break; /* 列出图片 */ case 'listimage': $result = $this->listfiles(); break; /* 列出文件 */ case 'listfile': $result = $this->listfiles(); break; /* 抓取远程文件 */ case 'catchimage': $result = $this->crawler(); break; default: $result = json(array( 'state' => '请求地址出错' )); break; } /* 输出结果 */ if (isset($_GET["callback"])) { if (preg_match("/^[\w_]+$/", $_GET["callback"])) { return htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { return json(array( 'state' => 'callback参数不合法' )); } } else { return $result; } } // /* 上传图片配置项 */ // "imageActionName": "uploadimage", /* 执行上传图片的action名称 */ // /* 涂鸦图片上传配置项 */ // "scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */ // /* 截图工具上传 */ // "snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */ // /* 抓取远程图片配置 */ // "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], // /* 上传视频配置 */ // "videoActionName": "uploadvideo", /* 执行上传视频的action名称 */ // /* 上传文件配置 */ // "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */ // /* 列出指定目录下的图片 */ // "imageManagerActionName": "listimage", /* 执行图片管理的action名称 */ // /* 列出指定目录下的文件 */ // "fileManagerActionName": "listfile", /* 执行文件管理的action名称 */ }