You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.7 KiB

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