UEditorAction.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace common\modules\attachment\actions;
  3. use Yii;
  4. use yii\base\Action;
  5. use yii\helpers\ArrayHelper;
  6. use yii\web\Response;
  7. class UEditorAction extends Action
  8. {
  9. /**
  10. * @var array
  11. */
  12. public $config = [];
  13. public function init()
  14. {
  15. //close csrf
  16. Yii::$app->request->enableCsrfValidation = false;
  17. parent::init();
  18. }
  19. public function run()
  20. {
  21. if (Yii::$app->request->get('callback',false)) {
  22. Yii::$app->response->format = Response::FORMAT_JSONP;
  23. } else {
  24. Yii::$app->response->format = Response::FORMAT_JSON;
  25. }
  26. $this->config = ArrayHelper::merge(require Yii::getAlias('@common/widgets/editor/ueditor') . '/config.php', $this->config);
  27. return $this->handleAction();
  28. }
  29. /**
  30. * 处理action
  31. */
  32. protected function handleAction()
  33. {
  34. $action = Yii::$app->request->get('action');
  35. switch ($action) {
  36. case 'config':
  37. $result = $this->config;
  38. break;
  39. /* 上传图片 */
  40. case 'uploadimage':
  41. /* 上传涂鸦 */
  42. case 'uploadscrawl':
  43. /* 上传视频 */
  44. case 'uploadvideo':
  45. /* 上传文件 */
  46. case 'uploadfile':
  47. $result = $this->actionUpload();
  48. break;
  49. /* 列出图片 */
  50. case 'listimage':
  51. /* 列出文件 */
  52. case 'listfile':
  53. $result = $this->actionList();
  54. break;
  55. /* 抓取远程文件 */
  56. case 'catchimage':
  57. $result = $this->actionCatch();
  58. break;
  59. default:
  60. $result = [
  61. 'state' => '请求地址出错'
  62. ];
  63. break;
  64. }
  65. /* 输出结果 */
  66. return $result;
  67. }
  68. /**
  69. * 上传
  70. * @return array
  71. */
  72. protected function actionUpload()
  73. {
  74. $base64 = "upload";
  75. switch (htmlspecialchars($_GET['action'])) {
  76. case 'uploadimage':
  77. $config = array(
  78. "pathRoot" => ArrayHelper::getValue($this->config, "imageRoot", $_SERVER['DOCUMENT_ROOT']),
  79. "pathFormat" => $this->config['imagePathFormat'],
  80. "maxSize" => $this->config['imageMaxSize'],
  81. "allowFiles" => $this->config['imageAllowFiles']
  82. );
  83. $fieldName = $this->config['imageFieldName'];
  84. break;
  85. case 'uploadscrawl':
  86. $config = array(
  87. "pathRoot" => ArrayHelper::getValue($this->config, "scrawlRoot", $_SERVER['DOCUMENT_ROOT']),
  88. "pathFormat" => $this->config['scrawlPathFormat'],
  89. "maxSize" => $this->config['scrawlMaxSize'],
  90. "allowFiles" => $this->config['scrawlAllowFiles'],
  91. "oriName" => "scrawl.png"
  92. );
  93. $fieldName = $this->config['scrawlFieldName'];
  94. $base64 = "base64";
  95. break;
  96. case 'uploadvideo':
  97. $config = array(
  98. "pathRoot" => ArrayHelper::getValue($this->config, "videoRoot", $_SERVER['DOCUMENT_ROOT']),
  99. "pathFormat" => $this->config['videoPathFormat'],
  100. "maxSize" => $this->config['videoMaxSize'],
  101. "allowFiles" => $this->config['videoAllowFiles']
  102. );
  103. $fieldName = $this->config['videoFieldName'];
  104. break;
  105. case 'uploadfile':
  106. default:
  107. $config = array(
  108. "pathRoot" => ArrayHelper::getValue($this->config, "fileRoot", $_SERVER['DOCUMENT_ROOT']),
  109. "pathFormat" => $this->config['filePathFormat'],
  110. "maxSize" => $this->config['fileMaxSize'],
  111. "allowFiles" => $this->config['fileAllowFiles']
  112. );
  113. $fieldName = $this->config['fileFieldName'];
  114. break;
  115. }
  116. $result = Yii::$app->runAction('/upload/ueditor-image-upload');
  117. return $result;
  118. /**
  119. * 得到上传文件所对应的各个参数,数组结构
  120. * array(
  121. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  122. * "url" => "", //返回的地址
  123. * "title" => "", //新文件名
  124. * "original" => "", //原始文件名
  125. * "type" => "" //文件类型
  126. * "size" => "", //文件大小
  127. * )
  128. */
  129. }
  130. /**
  131. * 获取已上传的文件列表
  132. * @return array
  133. */
  134. protected function actionList()
  135. {
  136. /* 判断类型 */
  137. switch ($_GET['action']) {
  138. /* 列出文件 */
  139. case 'listfile':
  140. $allowFiles = $this->config['fileManagerAllowFiles'];
  141. $listSize = $this->config['fileManagerListSize'];
  142. $path = $this->config['fileManagerListPath'];
  143. break;
  144. /* 列出图片 */
  145. case 'listimage':
  146. default:
  147. $allowFiles = $this->config['imageManagerAllowFiles'];
  148. $listSize = $this->config['imageManagerListSize'];
  149. $path = $this->config['imageManagerListPath'];
  150. }
  151. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  152. /* 获取参数 */
  153. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  154. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  155. $end = (int)$start + (int)$size;
  156. /* 获取文件列表 */
  157. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
  158. $files = $this->getfiles($path, $allowFiles);
  159. if (!count($files)) {
  160. return [
  161. "state" => "no match file",
  162. "list" => array(),
  163. "start" => $start,
  164. "total" => count($files)
  165. ];
  166. }
  167. /* 获取指定范围的列表 */
  168. $len = count($files);
  169. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
  170. $list[] = $files[$i];
  171. }
  172. //倒序
  173. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  174. // $list[] = $files[$i];
  175. //}
  176. /* 返回数据 */
  177. return [
  178. "state" => "SUCCESS",
  179. "list" => $list,
  180. "start" => $start,
  181. "total" => count($files)
  182. ];
  183. }
  184. /**
  185. * 抓取远程图片
  186. * @return array
  187. */
  188. protected function actionCatch()
  189. {
  190. $result = Yii::$app->runAction('/upload/ueditor-catch');
  191. return $result;
  192. }
  193. /**
  194. * 遍历获取目录下的指定类型的文件
  195. * @param $path
  196. * @param $allowFiles
  197. * @param array $files
  198. * @return array|null
  199. */
  200. protected function getfiles($path, $allowFiles, &$files = array())
  201. {
  202. if (!is_dir($path)) return null;
  203. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  204. $handle = opendir($path);
  205. while (false !== ($file = readdir($handle))) {
  206. if ($file != '.' && $file != '..') {
  207. $path2 = $path . $file;
  208. if (is_dir($path2)) {
  209. $this->getfiles($path2, $allowFiles, $files);
  210. } else {
  211. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  212. $files[] = array(
  213. 'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  214. 'mtime' => filemtime($path2)
  215. );
  216. }
  217. }
  218. }
  219. }
  220. return $files;
  221. }
  222. }