Ueditor.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 'uploadscrawl':
  101. $uploadconfig = array(
  102. "pathFormat" => $this->config['scrawlPathFormat'],
  103. "maxSize" => $this->config['scrawlMaxSize'],
  104. "allowFiles" => $this->config['scrawlAllowFiles'],
  105. "oriName" => "scrawl.png"
  106. );
  107. $fieldName = $this->config['scrawlFieldName'];
  108. $base64 = "base64";
  109. break;
  110. case 'uploadvideo':
  111. $uploadconfig = array(
  112. "pathFormat" => $this->config['videoPathFormat'],
  113. "maxSize" => $this->config['videoMaxSize'],
  114. "allowFiles" => $this->config['videoAllowFiles']
  115. );
  116. $fieldName = $this->config['videoFieldName'];
  117. break;
  118. case 'uploadfile':
  119. default:
  120. $uploadconfig = array(
  121. "pathFormat" => $this->config['filePathFormat'],
  122. "maxSize" => $this->config['fileMaxSize'],
  123. "allowFiles" => $this->config['fileAllowFiles']
  124. );
  125. $fieldName = $this->config['fileFieldName'];
  126. break;
  127. }
  128. /* 生成上传实例对象并完成上传 */
  129. $up = new Uploader($fieldName, $uploadconfig, $base64);
  130. /**
  131. * 得到上传文件所对应的各个参数,数组结构
  132. * array(
  133. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  134. * "url" => "", //返回的地址
  135. * "title" => "", //新文件名
  136. * "original" => "", //原始文件名
  137. * "type" => "" //文件类型
  138. * "size" => "", //文件大小
  139. * )
  140. */
  141. /* 返回数据 */
  142. return json($up->getFileInfo());
  143. }
  144. protected function listfiles($type = "")
  145. {
  146. /* 判断类型 */
  147. switch ($type) {
  148. /* 列出文件 */
  149. case 'listfile':
  150. $allowFiles = $this->config['fileManagerAllowFiles'];
  151. $listSize = $this->config['fileManagerListSize'];
  152. $path = $this->config['fileManagerListPath'];
  153. break;
  154. /* 列出图片 */
  155. case 'listimage':
  156. default:
  157. $allowFiles = $this->config['imageManagerAllowFiles'];
  158. $listSize = $this->config['imageManagerListSize'];
  159. $path = $this->config['imageManagerListPath'];
  160. }
  161. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  162. /* 获取参数 */
  163. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  164. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  165. $end = $start + $size;
  166. /* 获取文件列表 */
  167. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
  168. $files = getfiles($path, $allowFiles);
  169. if (!count($files)) {
  170. return json(array(
  171. "state" => "no match file",
  172. "list" => array(),
  173. "start" => $start,
  174. "total" => count($files)
  175. ));
  176. }
  177. /* 获取指定范围的列表 */
  178. $len = count($files);
  179. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
  180. $list[] = $files[$i];
  181. }
  182. //倒序
  183. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  184. // $list[] = $files[$i];
  185. //}
  186. /* 返回数据 */
  187. $result = json(array(
  188. "state" => "SUCCESS",
  189. "list" => $list,
  190. "start" => $start,
  191. "total" => count($files)
  192. ));
  193. return $result;
  194. }
  195. protected function crawler()
  196. {
  197. set_time_limit(0);
  198. /* 上传配置 */
  199. $config = array(
  200. "pathFormat" => $this->config['catcherPathFormat'],
  201. "maxSize" => $this->config['catcherMaxSize'],
  202. "allowFiles" => $this->config['catcherAllowFiles'],
  203. "oriName" => "remote.png"
  204. );
  205. $fieldName = $this->config['catcherFieldName'];
  206. /* 抓取远程图片 */
  207. $list = array();
  208. if (isset($_POST[$fieldName])) {
  209. $source = $_POST[$fieldName];
  210. } else {
  211. $source = $_GET[$fieldName];
  212. }
  213. foreach ($source as $imgUrl) {
  214. $item = new Uploader($imgUrl, $config, "remote");
  215. $info = $item->getFileInfo();
  216. array_push($list, array(
  217. "state" => $info["state"],
  218. "url" => $info["url"],
  219. "size" => $info["size"],
  220. "title" => htmlspecialchars($info["title"]),
  221. "original" => htmlspecialchars($info["original"]),
  222. "source" => htmlspecialchars($imgUrl)
  223. ));
  224. }
  225. /* 返回抓取数据 */
  226. return json(array(
  227. 'state' => count($list) ? 'SUCCESS' : 'ERROR',
  228. 'list' => $list
  229. ));
  230. }
  231. public function action($action = "")
  232. {
  233. switch ($action) {
  234. case 'config':
  235. $result = json($this->config);
  236. break;
  237. /* 上传图片 */
  238. case 'uploadimage':
  239. /* 上传涂鸦 */
  240. case 'uploadscrawl':
  241. /* 上传视频 */
  242. case 'uploadvideo':
  243. /* 上传文件 */
  244. case 'uploadfile':
  245. $result = $this->upload();
  246. break;
  247. /* 列出图片 */
  248. case 'listimage':
  249. $result = $this->listfiles();
  250. break;
  251. /* 列出文件 */
  252. case 'listfile':
  253. $result = $this->listfiles();
  254. break;
  255. /* 抓取远程文件 */
  256. case 'catchimage':
  257. $result = $this->crawler();
  258. break;
  259. default:
  260. $result = json(array(
  261. 'state' => '请求地址出错'
  262. ));
  263. break;
  264. }
  265. /* 输出结果 */
  266. if (isset($_GET["callback"])) {
  267. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  268. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  269. } else {
  270. return json(array(
  271. 'state' => 'callback参数不合法'
  272. ));
  273. }
  274. } else {
  275. return $result;
  276. }
  277. }
  278. // /* 上传图片配置项 */
  279. // "imageActionName": "uploadimage", /* 执行上传图片的action名称 */
  280. // /* 涂鸦图片上传配置项 */
  281. // "scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */
  282. // /* 截图工具上传 */
  283. // "snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */
  284. // /* 抓取远程图片配置 */
  285. // "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
  286. // /* 上传视频配置 */
  287. // "videoActionName": "uploadvideo", /* 执行上传视频的action名称 */
  288. // /* 上传文件配置 */
  289. // "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
  290. // /* 列出指定目录下的图片 */
  291. // "imageManagerActionName": "listimage", /* 执行图片管理的action名称 */
  292. // /* 列出指定目录下的文件 */
  293. // "fileManagerActionName": "listfile", /* 执行文件管理的action名称 */
  294. }