| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 | <?phpdeclare(strict_types=1);/** * +---------------------------------------------------------------------- * | Ueditor编辑器后台控制器制器 * +---------------------------------------------------------------------- */namespace app\sys\controller;use think\App;use ueditor\Uploader;class Ueditor extends Base{    protected $config;    public function __construct(App $app)    {        parent::__construct($app);        $this->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名称 */}
 |