0bf498308a428c45846d73dcd4c6f9780b60beb0.svn-base 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * 原始的本地上传处理方法
  4. *
  5. * @author widuu <admin@widuu.com>
  6. * @document https://github.com/widuu/qiniu_ueditor_1.4.3
  7. */
  8. class Local extends Base{
  9. /**
  10. * 上传方法[基于Ueditor自带的Uploader]
  11. *
  12. * @author widuu <admin@widuu.com>
  13. */
  14. public function upload($method){
  15. $upload_config = $this->setUploadConfig($method);
  16. $fieldName = $upload_config['fieldName'];
  17. $base64 = $upload_config['base64'];
  18. unset($upload_config['fieldName']);
  19. unset($upload_config['base64']);
  20. $File = new LocalDriver($fieldName, $upload_config, $base64);
  21. return $File->getFileInfo();
  22. }
  23. /**
  24. * 删除文件方法
  25. *
  26. * @author widuu <admin@widuu.com>
  27. */
  28. public function remove(){
  29. $file = trim($_POST['key']);
  30. $config = $this->config;
  31. $root_path = $config['root_path'];
  32. //安全验证
  33. $file = str_replace("\\", "/", $file);
  34. $file_path = $root_path.$file;
  35. $pathlist = explode("/", $file);
  36. if ($pathlist[0] != 'uploads' && $pathlist[1] != 'uploads') {
  37. return array(
  38. 'state' => 'ERROR',
  39. 'error' => 'Folders start from uploads'
  40. );
  41. exit();
  42. }
  43. if (in_array('..', $pathlist)) {
  44. return array(
  45. 'state' => 'ERROR',
  46. 'error' => 'Folders start from uploads'
  47. );
  48. exit();
  49. }
  50. $file_path = $root_path.$file;
  51. if( file_exists($file_path) ){
  52. $result = @unlink($file_path);
  53. if( $result ){
  54. return array(
  55. 'state' => 'SUCCESS'
  56. );
  57. }else{
  58. return array(
  59. 'state' => 'ERROR',
  60. 'error' => 'delete file error'
  61. );
  62. }
  63. }
  64. return array(
  65. 'state' => 'ERROR',
  66. 'error' => 'file not exists'
  67. );
  68. }
  69. /**
  70. * 远程图片抓取 [采用原有ueditor方法]
  71. *
  72. * @author widuu <admin@widuu.com>
  73. */
  74. public function catchimage(){
  75. $ue_config = $this->getUeConfig();
  76. /* 上传配置 */
  77. $config = array(
  78. "pathFormat" => $ue_config['catcherPathFormat'],
  79. "maxSize" => $ue_config['catcherMaxSize'],
  80. "allowFiles" => $ue_config['catcherAllowFiles'],
  81. "oriName" => "remote.png",
  82. "rootPath" => $ue_config['rootPath']
  83. );
  84. $fieldName = $ue_config['catcherFieldName'];
  85. /* 抓取远程图片 */
  86. $list = array();
  87. if (isset($_POST[$fieldName])) {
  88. $source = $_POST[$fieldName];
  89. } else {
  90. $source = $_GET[$fieldName];
  91. }
  92. foreach ( $source as $img_url ) {
  93. $file = new LocalDriver($img_url, $config, "remote");
  94. $info = $file->getFileInfo();
  95. array_push($list, array(
  96. "state" => $info["state"],
  97. "url" => $info["url"],
  98. "size" => $info["size"],
  99. "title" => htmlspecialchars($info["title"]),
  100. "original" => htmlspecialchars($info["original"]),
  101. "source" => htmlspecialchars($img_url)
  102. ));
  103. }
  104. return array(
  105. 'state'=> count($list) ? 'SUCCESS':'ERROR',
  106. 'list'=> $list
  107. );
  108. }
  109. /**
  110. * 列出文件列表 [采用原有ueditor方法]
  111. *
  112. * @author widuu <admin@widuu.com>
  113. */
  114. public function listFile($method){
  115. $ue_config = $this->getUeConfig();
  116. if( $method == 'listimage'){
  117. $config_prefix = 'image';
  118. }else{
  119. $config_prefix = 'file';
  120. }
  121. $config = array(
  122. "allowFiles" => $ue_config[$config_prefix.'ManagerAllowFiles'],
  123. "listSize" => $ue_config[$config_prefix.'ManagerListSize'],
  124. "path" => $ue_config[$config_prefix.'ManagerListPath'],
  125. );
  126. $allow_files = substr(str_replace(".", "|", join("", $config['allowFiles'])), 1);
  127. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $config['listSize'];
  128. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  129. $end = $start + $size;
  130. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($config['path'], 0, 1) == "/" ? "":"/") . $config['path'];
  131. $files = array();
  132. $this->getFiles($path, $allow_files,$files);
  133. if (!count($files)) {
  134. return array(
  135. "state" => "no match file",
  136. "list" => array(),
  137. "start" => $start,
  138. "total" => count($files)
  139. );
  140. }
  141. /* 获取指定范围的列表 */
  142. $len = count($files);
  143. $php_config = $this->config;
  144. if( $php_config['orderby'] == 'desc' ){
  145. for ($i = $start, $list = array(); $i < $len && $i < $end; $i++){
  146. $list[] = $files[$i];
  147. }
  148. }else{
  149. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  150. $list[] = $files[$i];
  151. }
  152. }
  153. /* 返回数据 */
  154. $result = array(
  155. "state" => "SUCCESS",
  156. "list" => $list,
  157. "start" => $start,
  158. "total" => count($files)
  159. );
  160. return $result;
  161. }
  162. /**
  163. * 遍历获取目录下的指定类型的文件
  164. * @param $path
  165. * @param array $files
  166. * @return array
  167. */
  168. private function getFiles($path, $allowFiles, &$files = array()){
  169. if (!is_dir($path)) return null;
  170. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  171. $handle = opendir($path);
  172. while (false !== ($file = readdir($handle))) {
  173. if ($file != '.' && $file != '..') {
  174. $path2 = $path . $file;
  175. if (is_dir($path2)) {
  176. $this->getFiles($path2, $allowFiles, $files);
  177. } else {
  178. if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
  179. $files[] = array(
  180. 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  181. 'mtime'=> filemtime($path2)
  182. );
  183. }
  184. }
  185. }
  186. }
  187. $files = $this->list_sort_by($files, 'mtime', 'desc'); //按照时间排序
  188. return $files;
  189. }
  190. public function list_sort_by($list, $field, $sortby='asc') {
  191. if(is_array($list)){ //判断是否数组
  192. $refer = $resultSet = array(); //初始化数组变量
  193. foreach ($list as $i => $data) //foreach数组
  194. $refer[$i] = &$data[$field]; //存储要排序的数组字段键和值
  195. switch ($sortby) {//进行排序
  196. case 'asc': // 正向排序
  197. asort($refer);
  198. break;
  199. case 'desc':// 逆向排序
  200. arsort($refer);
  201. break;
  202. case 'nat': // 自然排序
  203. natcasesort($refer);
  204. break;
  205. }
  206. foreach ( $refer as $key=> $val)//重新组合排序后的数组
  207. $resultSet[] = &$list[$key];
  208. return $resultSet;
  209. }
  210. return false;
  211. }
  212. }