Ueditor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * | Ueditor编辑器后台控制器制器
  6. * +----------------------------------------------------------------------
  7. */
  8. namespace app\sys\controller;
  9. use think\App;
  10. use ueditor\Uploader;
  11. class Ueditor extends Base
  12. {
  13. protected $config;
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. $this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($this->app->getRootPath() . "/extend/ueditor/config.json")), true);
  18. }
  19. public function index()
  20. {
  21. return json($this->config);
  22. }
  23. public function config()
  24. {
  25. $result = json($this->config);
  26. return $this->output($result);
  27. }
  28. public function uploadimage()
  29. {
  30. $result = $this->upload('uploadimage');
  31. return $this->output($result);
  32. }
  33. public function uploadscrawl()
  34. {
  35. $result = $this->upload('uploadscrawl');
  36. return $this->output($result);
  37. }
  38. public function uploadvideo()
  39. {
  40. $result = $this->upload('uploadvideo');
  41. return $this->output($result);
  42. }
  43. public function uploadfile()
  44. {
  45. $result = $this->upload('uploadfile');
  46. return $this->output($result);
  47. }
  48. public function listimage()
  49. {
  50. $result = $this->listfiles('listimage');
  51. return $this->output($result);
  52. }
  53. public function listfile()
  54. {
  55. $result = $this->listimage('listfile');
  56. return $this->output($result);
  57. }
  58. public function catchimage()
  59. {
  60. return $this->crawler();
  61. }
  62. /**
  63. * 输出结果
  64. */
  65. public function output($result)
  66. {
  67. if (isset($_GET["callback"])) {
  68. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  69. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  70. } else {
  71. return json(array(
  72. 'state' => 'callback参数不合法'
  73. ));
  74. }
  75. } else {
  76. return $result;
  77. }
  78. }
  79. /**
  80. * 空操作
  81. */
  82. public function _empty()
  83. {
  84. return json([
  85. 'state' => '请求地址出错'
  86. ]);
  87. }
  88. protected function upload($type = "")
  89. {
  90. $base64 = "upload";
  91. switch ($type) {
  92. case 'uploadimage':
  93. $uploadconfig = [
  94. "pathFormat" => $this->config['imagePathFormat'],
  95. "maxSize" => $this->config['imageMaxSize'],
  96. "allowFiles" => $this->config['imageAllowFiles']
  97. ];
  98. $fieldName = $this->config['imageFieldName'];
  99. break;
  100. case 'uploadurlimage':
  101. $uploadconfig = [
  102. "pathFormat" => $this->config['imagePathFormat'],
  103. "maxSize" => $this->config['imageMaxSize'],
  104. "allowFiles" => $this->config['imageAllowFiles']
  105. ];
  106. $fieldName = $this->config['imageFieldName'];
  107. $base64 = "uploadurlimage";
  108. break;
  109. case 'uploadscrawl':
  110. $uploadconfig = array(
  111. "pathFormat" => $this->config['scrawlPathFormat'],
  112. "maxSize" => $this->config['scrawlMaxSize'],
  113. "allowFiles" => $this->config['scrawlAllowFiles'],
  114. "oriName" => "scrawl.png"
  115. );
  116. $fieldName = $this->config['scrawlFieldName'];
  117. $base64 = "base64";
  118. break;
  119. case 'uploadvideo':
  120. $uploadconfig = array(
  121. "pathFormat" => $this->config['videoPathFormat'],
  122. "maxSize" => $this->config['videoMaxSize'],
  123. "allowFiles" => $this->config['videoAllowFiles']
  124. );
  125. $fieldName = $this->config['videoFieldName'];
  126. break;
  127. case 'uploadfile':
  128. default:
  129. $uploadconfig = array(
  130. "pathFormat" => $this->config['filePathFormat'],
  131. "maxSize" => $this->config['fileMaxSize'],
  132. "allowFiles" => $this->config['fileAllowFiles']
  133. );
  134. $fieldName = $this->config['fileFieldName'];
  135. break;
  136. }
  137. /* 生成上传实例对象并完成上传 */
  138. $up = new Uploader($fieldName, $uploadconfig, $base64);
  139. /**
  140. * 得到上传文件所对应的各个参数,数组结构
  141. * array(
  142. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  143. * "url" => "", //返回的地址
  144. * "title" => "", //新文件名
  145. * "original" => "", //原始文件名
  146. * "type" => "" //文件类型
  147. * "size" => "", //文件大小
  148. * )
  149. */
  150. /* 返回数据 */
  151. return json($up->getFileInfo());
  152. }
  153. protected function listfiles($type = "")
  154. {
  155. /* 判断类型 */
  156. switch ($type) {
  157. /* 列出文件 */
  158. case 'listfile':
  159. $allowFiles = $this->config['fileManagerAllowFiles'];
  160. $listSize = $this->config['fileManagerListSize'];
  161. $path = $this->config['fileManagerListPath'];
  162. break;
  163. /* 列出图片 */
  164. case 'listimage':
  165. default:
  166. $allowFiles = $this->config['imageManagerAllowFiles'];
  167. $listSize = $this->config['imageManagerListSize'];
  168. $path = $this->config['imageManagerListPath'];
  169. }
  170. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  171. /* 获取参数 */
  172. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  173. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  174. $end = $start + $size;
  175. /* 获取文件列表 */
  176. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
  177. $files = getfiles($path, $allowFiles);
  178. if (!count($files)) {
  179. return json(array(
  180. "state" => "no match file",
  181. "list" => array(),
  182. "start" => $start,
  183. "total" => count($files)
  184. ));
  185. }
  186. /* 获取指定范围的列表 */
  187. $len = count($files);
  188. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
  189. $list[] = $files[$i];
  190. }
  191. //倒序
  192. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  193. // $list[] = $files[$i];
  194. //}
  195. /* 返回数据 */
  196. $result = json(array(
  197. "state" => "SUCCESS",
  198. "list" => $list,
  199. "start" => $start,
  200. "total" => count($files)
  201. ));
  202. return $result;
  203. }
  204. protected function crawler()
  205. {
  206. set_time_limit(0);
  207. /* 上传配置 */
  208. $config = array(
  209. "pathFormat" => $this->config['catcherPathFormat'],
  210. "maxSize" => $this->config['catcherMaxSize'],
  211. "allowFiles" => $this->config['catcherAllowFiles'],
  212. "oriName" => "remote.png"
  213. );
  214. $fieldName = $this->config['catcherFieldName'];
  215. /* 抓取远程图片 */
  216. $list = array();
  217. if (isset($_POST[$fieldName])) {
  218. $source = $_POST[$fieldName];
  219. } else {
  220. $source = $_GET[$fieldName];
  221. }
  222. foreach ($source as $imgUrl) {
  223. $item = new Uploader($imgUrl, $config, "remote");
  224. $info = $item->getFileInfo();
  225. array_push($list, array(
  226. "state" => $info["state"],
  227. "url" => $info["url"],
  228. "size" => $info["size"],
  229. "title" => htmlspecialchars($info["title"]),
  230. "original" => htmlspecialchars($info["original"]),
  231. "source" => htmlspecialchars($imgUrl)
  232. ));
  233. }
  234. /* 返回抓取数据 */
  235. return json(array(
  236. 'state' => count($list) ? 'SUCCESS' : 'ERROR',
  237. 'list' => $list
  238. ));
  239. }
  240. public function action($action = "")
  241. {
  242. switch ($action) {
  243. case 'config':
  244. $result = json($this->config);
  245. break;
  246. /* 上传图片 */
  247. case 'uploadimage':
  248. /* 上传涂鸦 */
  249. case 'uploadscrawl':
  250. /* 上传视频 */
  251. case 'uploadvideo':
  252. /* 上传文件 */
  253. case 'uploadfile':
  254. $result = $this->upload();
  255. break;
  256. /* 列出图片 */
  257. case 'listimage':
  258. $result = $this->listfiles();
  259. break;
  260. /* 列出文件 */
  261. case 'listfile':
  262. $result = $this->listfiles();
  263. break;
  264. /* 抓取远程文件 */
  265. case 'catchimage':
  266. $result = $this->crawler();
  267. break;
  268. default:
  269. $result = json(array(
  270. 'state' => '请求地址出错'
  271. ));
  272. break;
  273. }
  274. /* 输出结果 */
  275. if (isset($_GET["callback"])) {
  276. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  277. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  278. } else {
  279. return json(array(
  280. 'state' => 'callback参数不合法'
  281. ));
  282. }
  283. } else {
  284. return $result;
  285. }
  286. }
  287. // /* 上传图片配置项 */
  288. // "imageActionName": "uploadimage", /* 执行上传图片的action名称 */
  289. // /* 涂鸦图片上传配置项 */
  290. // "scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */
  291. // /* 截图工具上传 */
  292. // "snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */
  293. // /* 抓取远程图片配置 */
  294. // "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
  295. // /* 上传视频配置 */
  296. // "videoActionName": "uploadvideo", /* 执行上传视频的action名称 */
  297. // /* 上传文件配置 */
  298. // "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
  299. // /* 列出指定目录下的图片 */
  300. // "imageManagerActionName": "listimage", /* 执行图片管理的action名称 */
  301. // /* 列出指定目录下的文件 */
  302. // "fileManagerActionName": "listfile", /* 执行文件管理的action名称 */
  303. }