PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/webroot/lib/vendor/kucha/ueditor/UEditorAction.php

https://gitlab.com/tangsengjiu/Talk
PHP | 281 lines | 195 code | 23 blank | 63 comment | 25 complexity | c42468c38ee6aa4c9619676ae91509f5 MD5 | raw file
  1. <?php
  2. namespace kucha\ueditor;
  3. use Yii;
  4. use yii\base\Action;
  5. use yii\helpers\ArrayHelper;
  6. class UEditorAction extends Action
  7. {
  8. /**
  9. * @var array
  10. */
  11. public $config = [];
  12. public function init()
  13. {
  14. //close csrf
  15. Yii::$app->request->enableCsrfValidation = false;
  16. //默认设置
  17. $_config = require(__DIR__ . '/config.php');
  18. //load config file
  19. $this->config = ArrayHelper::merge($_config, $this->config);
  20. parent::init();
  21. }
  22. public function run()
  23. {
  24. $this->handleAction();
  25. }
  26. /**
  27. * 处理action
  28. */
  29. protected function handleAction()
  30. {
  31. $action = Yii::$app->request->get('action');
  32. switch ($action) {
  33. case 'config':
  34. $result = json_encode($this->config);
  35. break;
  36. /* 上传图片 */
  37. case 'uploadimage':
  38. /* 上传涂鸦 */
  39. case 'uploadscrawl':
  40. /* 上传视频 */
  41. case 'uploadvideo':
  42. /* 上传文件 */
  43. case 'uploadfile':
  44. $result = $this->actionUpload();
  45. break;
  46. /* 列出图片 */
  47. case 'listimage':
  48. /* 列出文件 */
  49. case 'listfile':
  50. $result = $this->actionList();
  51. break;
  52. /* 抓取远程文件 */
  53. case 'catchimage':
  54. $result = $this->actionCrawler();
  55. break;
  56. default:
  57. $result = json_encode(array(
  58. 'state' => '请求地址出错'
  59. ));
  60. break;
  61. }
  62. /* 输出结果 */
  63. if (isset($_GET["callback"])) {
  64. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  65. echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  66. } else {
  67. echo json_encode(array(
  68. 'state' => 'callback参数不合法'
  69. ));
  70. }
  71. } else {
  72. echo $result;
  73. }
  74. }
  75. /**
  76. * 上传
  77. * @return string
  78. */
  79. protected function actionUpload()
  80. {
  81. $base64 = "upload";
  82. switch (htmlspecialchars($_GET['action'])) {
  83. case 'uploadimage':
  84. $config = array(
  85. "pathFormat" => $this->config['imagePathFormat'],
  86. "maxSize" => $this->config['imageMaxSize'],
  87. "allowFiles" => $this->config['imageAllowFiles']
  88. );
  89. $fieldName = $this->config['imageFieldName'];
  90. break;
  91. case 'uploadscrawl':
  92. $config = array(
  93. "pathFormat" => $this->config['scrawlPathFormat'],
  94. "maxSize" => $this->config['scrawlMaxSize'],
  95. "allowFiles" => $this->config['scrawlAllowFiles'],
  96. "oriName" => "scrawl.png"
  97. );
  98. $fieldName = $this->config['scrawlFieldName'];
  99. $base64 = "base64";
  100. break;
  101. case 'uploadvideo':
  102. $config = array(
  103. "pathFormat" => $this->config['videoPathFormat'],
  104. "maxSize" => $this->config['videoMaxSize'],
  105. "allowFiles" => $this->config['videoAllowFiles']
  106. );
  107. $fieldName = $this->config['videoFieldName'];
  108. break;
  109. case 'uploadfile':
  110. default:
  111. $config = array(
  112. "pathFormat" => $this->config['filePathFormat'],
  113. "maxSize" => $this->config['fileMaxSize'],
  114. "allowFiles" => $this->config['fileAllowFiles']
  115. );
  116. $fieldName = $this->config['fileFieldName'];
  117. break;
  118. }
  119. /* 生成上传实例对象并完成上传 */
  120. $up = new Uploader($fieldName, $config, $base64);
  121. /**
  122. * 得到上传文件所对应的各个参数,数组结构
  123. * array(
  124. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  125. * "url" => "", //返回的地址
  126. * "title" => "", //新文件名
  127. * "original" => "", //原始文件名
  128. * "type" => "" //文件类型
  129. * "size" => "", //文件大小
  130. * )
  131. */
  132. /* 返回数据 */
  133. return json_encode($up->getFileInfo());
  134. }
  135. /**
  136. * 获取已上传的文件列表
  137. * @return string
  138. */
  139. protected function actionList()
  140. {
  141. /* 判断类型 */
  142. switch ($_GET['action']) {
  143. /* 列出文件 */
  144. case 'listfile':
  145. $allowFiles = $this->config['fileManagerAllowFiles'];
  146. $listSize = $this->config['fileManagerListSize'];
  147. $path = $this->config['fileManagerListPath'];
  148. break;
  149. /* 列出图片 */
  150. case 'listimage':
  151. default:
  152. $allowFiles = $this->config['imageManagerAllowFiles'];
  153. $listSize = $this->config['imageManagerListSize'];
  154. $path = $this->config['imageManagerListPath'];
  155. }
  156. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  157. /* 获取参数 */
  158. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  159. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  160. $end = (int)$start + (int)$size;
  161. /* 获取文件列表 */
  162. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
  163. $files = $this->getfiles($path, $allowFiles);
  164. if (!count($files)) {
  165. return json_encode(array(
  166. "state" => "no match file",
  167. "list" => array(),
  168. "start" => $start,
  169. "total" => count($files)
  170. ));
  171. }
  172. /* 获取指定范围的列表 */
  173. $len = count($files);
  174. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
  175. $list[] = $files[$i];
  176. }
  177. //倒序
  178. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  179. // $list[] = $files[$i];
  180. //}
  181. /* 返回数据 */
  182. $result = json_encode(array(
  183. "state" => "SUCCESS",
  184. "list" => $list,
  185. "start" => $start,
  186. "total" => count($files)
  187. ));
  188. return $result;
  189. }
  190. /**
  191. * 抓取远程图片
  192. * @return string
  193. */
  194. protected function actionCrawler()
  195. {
  196. /* 上传配置 */
  197. $config = array(
  198. "pathFormat" => $this->config['catcherPathFormat'],
  199. "maxSize" => $this->config['catcherMaxSize'],
  200. "allowFiles" => $this->config['catcherAllowFiles'],
  201. "oriName" => "remote.png"
  202. );
  203. $fieldName = $this->config['catcherFieldName'];
  204. /* 抓取远程图片 */
  205. $list = array();
  206. if (isset($_POST[$fieldName])) {
  207. $source = $_POST[$fieldName];
  208. } else {
  209. $source = $_GET[$fieldName];
  210. }
  211. foreach ($source as $imgUrl) {
  212. $item = new Uploader($imgUrl, $config, "remote");
  213. $info = $item->getFileInfo();
  214. array_push($list, array(
  215. "state" => $info["state"],
  216. "url" => $info["url"],
  217. "size" => $info["size"],
  218. "title" => htmlspecialchars($info["title"]),
  219. "original" => htmlspecialchars($info["original"]),
  220. "source" => htmlspecialchars($imgUrl)
  221. ));
  222. }
  223. /* 返回抓取数据 */
  224. return json_encode(array(
  225. 'state' => count($list) ? 'SUCCESS' : 'ERROR',
  226. 'list' => $list
  227. ));
  228. }
  229. /**
  230. * 遍历获取目录下的指定类型的文件
  231. * @param $path
  232. * @param $allowFiles
  233. * @param array $files
  234. * @return array|null
  235. */
  236. protected function getfiles($path, $allowFiles, &$files = array())
  237. {
  238. if (!is_dir($path)) return null;
  239. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  240. $handle = opendir($path);
  241. while (false !== ($file = readdir($handle))) {
  242. if ($file != '.' && $file != '..') {
  243. $path2 = $path . $file;
  244. if (is_dir($path2)) {
  245. $this->getfiles($path2, $allowFiles, $files);
  246. } else {
  247. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  248. $files[] = array(
  249. 'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  250. 'mtime' => filemtime($path2)
  251. );
  252. }
  253. }
  254. }
  255. }
  256. return $files;
  257. }
  258. }