UeditorController.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 百度编辑器控制器入口
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class UeditorController extends CommonController
  11. {
  12. private $current_action;
  13. private $current_config;
  14. private $current_result;
  15. /**
  16. * [_initialize 前置操作-继承公共前置方法]
  17. * @author Devil
  18. * @blog http://gong.gg/
  19. * @version 0.0.1
  20. * @datetime 2016-12-03T12:39:08+0800
  21. */
  22. public function _initialize()
  23. {
  24. // 调用父类前置方法
  25. parent::_initialize();
  26. // 登录校验
  27. $this->Is_Login();
  28. }
  29. /**
  30. * [Index 附件上传入口]
  31. * @author Devil
  32. * @blog http://gong.gg/
  33. * @version 0.0.1
  34. * @datetime 2016-12-06T21:31:53+0800
  35. */
  36. public function Index()
  37. {
  38. // 配置信息
  39. $this->current_config = C('ueditor_config');
  40. $this->current_action = isset($_GET['action']) ? trim($_GET['action']) : '';
  41. switch($this->current_action)
  42. {
  43. // 配置信息
  44. case 'config':
  45. $this->current_result = json_encode($this->current_config);
  46. break;
  47. /* 上传图片 */
  48. case 'uploadimage':
  49. /* 上传涂鸦 */
  50. case 'uploadscrawl':
  51. /* 上传视频 */
  52. case 'uploadvideo':
  53. /* 上传文件 */
  54. case 'uploadfile':
  55. $this->ActionUpload();
  56. break;
  57. /* 列出图片 */
  58. case 'listimage':
  59. /* 列出文件 */
  60. case 'listfile':
  61. $this->ActionList();
  62. break;
  63. /* 抓取远程文件 */
  64. case 'catchimage':
  65. $this->ActionCrawler();
  66. break;
  67. default:
  68. $this->current_result = json_encode(array(
  69. 'state'=> '请求地址出错'
  70. ));
  71. }
  72. // 输出结果
  73. if(isset($_GET["callback"]))
  74. {
  75. if(preg_match("/^[\w_]+$/", $_GET["callback"]))
  76. {
  77. echo htmlspecialchars($_GET["callback"]) . '(' . $this->current_result . ')';
  78. } else {
  79. echo json_encode(array(
  80. 'state'=> 'callback参数不合法'
  81. ));
  82. }
  83. } else {
  84. echo $this->current_result;
  85. }
  86. exit();
  87. }
  88. /**
  89. * [ActionUpload 上传配置]
  90. * @author Devil
  91. * @blog http://gong.gg/
  92. * @version 0.0.1
  93. * @datetime 2017-01-17T22:45:06+0800
  94. */
  95. private function ActionUpload()
  96. {
  97. $base64 = "upload";
  98. switch(htmlspecialchars($this->current_action))
  99. {
  100. case 'uploadimage':
  101. $temp_config = array(
  102. "pathFormat" => $this->current_config['imagePathFormat'],
  103. "maxSize" => $this->current_config['imageMaxSize'],
  104. "allowFiles" => $this->current_config['imageAllowFiles']
  105. );
  106. $field_name = $this->current_config['imageFieldName'];
  107. break;
  108. case 'uploadscrawl':
  109. $temp_config = array(
  110. "pathFormat" => $this->current_config['scrawlPathFormat'],
  111. "maxSize" => $this->current_config['scrawlMaxSize'],
  112. "allowFiles" => $this->current_config['scrawlAllowFiles'],
  113. "oriName" => "scrawl.png"
  114. );
  115. $field_name = $this->current_config['scrawlFieldName'];
  116. $base64 = "base64";
  117. break;
  118. case 'uploadvideo':
  119. $temp_config = array(
  120. "pathFormat" => $this->current_config['videoPathFormat'],
  121. "maxSize" => $this->current_config['videoMaxSize'],
  122. "allowFiles" => $this->current_config['videoAllowFiles']
  123. );
  124. $field_name = $this->current_config['videoFieldName'];
  125. break;
  126. case 'uploadfile':
  127. default:
  128. $temp_config = array(
  129. "pathFormat" => $this->current_config['filePathFormat'],
  130. "maxSize" => $this->current_config['fileMaxSize'],
  131. "allowFiles" => $this->current_config['fileAllowFiles']
  132. );
  133. $field_name = $this->current_config['fileFieldName'];
  134. }
  135. /* 生成上传实例对象并完成上传 */
  136. $up = new \My\Uploader($field_name, $temp_config, $base64);
  137. /**
  138. * 得到上传文件所对应的各个参数,数组结构
  139. * array(
  140. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  141. * "url" => "", //返回的地址
  142. * "title" => "", //新文件名
  143. * "original" => "", //原始文件名
  144. * "type" => "" //文件类型
  145. * "size" => "", //文件大小
  146. * )
  147. */
  148. // 返回数据
  149. $this->current_result = json_encode($up->getFileInfo());
  150. }
  151. /**
  152. * [ActionList 文件列表]
  153. * @author Devil
  154. * @blog http://gong.gg/
  155. * @version 0.0.1
  156. * @datetime 2017-01-17T22:55:16+0800
  157. */
  158. private function ActionList()
  159. {
  160. /* 判断类型 */
  161. switch($this->current_action)
  162. {
  163. /* 列出文件 */
  164. case 'listfile':
  165. $allow_files = $this->current_config['fileManagerAllowFiles'];
  166. $list_size = $this->current_config['fileManagerListSize'];
  167. $path = $this->current_config['fileManagerListPath'];
  168. break;
  169. /* 列出图片 */
  170. case 'listimage':
  171. default:
  172. $allow_files = $this->current_config['imageManagerAllowFiles'];
  173. $list_size = $this->current_config['imageManagerListSize'];
  174. $path = $this->current_config['imageManagerListPath'];
  175. }
  176. $allow_files = substr(str_replace(".", "|", join("", $allow_files)), 1);
  177. /* 获取参数 */
  178. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $list_size;
  179. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  180. $end = $start + $size;
  181. /* 获取文件列表 */
  182. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
  183. $files = $this->GetFilesList($path, $allow_files);
  184. if (!count($files)) {
  185. $this->current_result = json_encode(array(
  186. "state" => "no match file",
  187. "list" => array(),
  188. "start" => $start,
  189. "total" => count($files)
  190. ));
  191. }
  192. /* 获取指定范围的列表 */
  193. $len = count($files);
  194. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--)
  195. {
  196. $list[] = $files[$i];
  197. }
  198. //倒序
  199. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  200. // $list[] = $files[$i];
  201. //}
  202. /* 返回数据 */
  203. $this->current_result = json_encode(array(
  204. "state" => "SUCCESS",
  205. "list" => $list,
  206. "start" => $start,
  207. "total" => count($files)
  208. ));
  209. }
  210. /**
  211. * [GetFilesList 遍历获取目录下的指定类型的文件]
  212. * @author Devil
  213. * @blog http://gong.gg/
  214. * @version 0.0.1
  215. * @datetime 2017-01-17T23:24:59+0800
  216. * @param [string] $path [路径地址]
  217. * @param [string] $allow_files [允许的文件]
  218. * @param [array] &$files [数据]
  219. * @return [array] [数据]
  220. */
  221. private function GetFilesList($path, $allow_files, &$files = array())
  222. {
  223. if(!is_dir($path)) return null;
  224. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  225. $handle = opendir($path);
  226. while(false !== ($file = readdir($handle)))
  227. {
  228. if($file != '.' && $file != '..')
  229. {
  230. $path2 = $path . $file;
  231. if(is_dir($path2))
  232. {
  233. $this->GetFilesList($path2, $allow_files, $files);
  234. } else {
  235. if(preg_match("/\.(".$allow_files.")$/i", $file))
  236. {
  237. $files[] = array(
  238. 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  239. 'mtime'=> filemtime($path2)
  240. );
  241. }
  242. }
  243. }
  244. }
  245. return $files;
  246. }
  247. /**
  248. * [ActionCrawler 抓取远程文件]
  249. * @author Devil
  250. * @blog http://gong.gg/
  251. * @version 0.0.1
  252. * @datetime 2017-01-17T23:08:29+0800
  253. */
  254. private function ActionCrawler()
  255. {
  256. $temp_config = array(
  257. "pathFormat" => $this->current_config['catcherPathFormat'],
  258. "maxSize" => $this->current_config['catcherMaxSize'],
  259. "allowFiles" => $this->current_config['catcherAllowFiles'],
  260. "oriName" => "remote.png"
  261. );
  262. $field_name = $this->current_config['catcherFieldName'];
  263. /* 抓取远程图片 */
  264. $list = array();
  265. $source = isset($_POST[$field_name]) ? $_POST[$field_name] : $_GET[$field_name];
  266. foreach($source as $imgUrl)
  267. {
  268. $item = new \My\Uploader($imgUrl, $temp_config, "remote");
  269. $info = $item->getFileInfo();
  270. array_push($list, array(
  271. "state" => $info["state"],
  272. "url" => $info["url"],
  273. "size" => $info["size"],
  274. "title" => htmlspecialchars($info["title"]),
  275. "original" => htmlspecialchars($info["original"]),
  276. "source" => htmlspecialchars($imgUrl)
  277. ));
  278. }
  279. /* 返回抓取数据 */
  280. $this->current_result = json_encode(array(
  281. 'state'=> count($list) ? 'SUCCESS':'ERROR',
  282. 'list'=> $list
  283. ));
  284. }
  285. }
  286. ?>