Kindeditor.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Core\Config;
  4. /**
  5. *
  6. * KindEditor
  7. * @author lishumingoo@gmail.com
  8. *
  9. */
  10. class Kindeditor extends Controller{
  11. private $IMG_UPLOAD_PATH;
  12. private $IMG_UPLOAD_URL;
  13. public function __construct() {
  14. header('Content-type: application/json; charset=UTF-8');
  15. include_once Config::getInstance()->get('App_Path') . '/include/JSON.php';
  16. $uploadConfig = Config::getInstance()->get('upload');
  17. $this->IMG_UPLOAD_PATH = $uploadConfig['path'];
  18. $this->IMG_UPLOAD_URL = $uploadConfig['url'];
  19. }
  20. public function doUpdateJson() {
  21. //文件保存目录路径
  22. $save_path = $this->IMG_UPLOAD_PATH . '/';
  23. //文件保存目录URL
  24. $save_url = $this->IMG_UPLOAD_URL . '/';
  25. //定义允许上传的文件扩展名
  26. $ext_arr = array(
  27. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  28. 'flash' => array('swf', 'flv'),
  29. 'media' => array('swf', 'flv', 'mp3', 'mp4', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'amr', 'rm', 'rmvb'),
  30. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
  31. 'mp3' => array('mp3'),
  32. );
  33. //最大文件大小
  34. $max_size = 3000000;
  35. $save_path = realpath($save_path) . '/';
  36. //PHP上传失败
  37. if (!empty($_FILES['imgFile']['error'])) {
  38. switch($_FILES['imgFile']['error']){
  39. case '1':
  40. $error = '超过php.ini允许的大小。';
  41. break;
  42. case '2':
  43. $error = '超过表单允许的大小。';
  44. break;
  45. case '3':
  46. $error = '图片只有部分被上传。';
  47. break;
  48. case '4':
  49. $error = '请选择图片。';
  50. break;
  51. case '6':
  52. $error = '找不到临时目录。';
  53. break;
  54. case '7':
  55. $error = '写文件到硬盘出错。';
  56. break;
  57. case '8':
  58. $error = 'File upload stopped by extension。';
  59. break;
  60. case '999':
  61. default:
  62. $error = '未知错误。';
  63. }
  64. $this->alert($error);
  65. }
  66. //有上传文件时
  67. if (empty($_FILES) === false) {
  68. //原文件名
  69. $file_name = $_FILES['imgFile']['name'];
  70. //服务器上临时文件名
  71. $tmp_name = $_FILES['imgFile']['tmp_name'];
  72. //文件大小
  73. $file_size = $_FILES['imgFile']['size'];
  74. //检查文件名
  75. if (!$file_name) {
  76. $this->alert("请选择文件。");
  77. }
  78. //检查目录
  79. if (@is_dir($save_path) === false) {
  80. $this->alert("上传目录不存在。");
  81. }
  82. //检查目录写权限
  83. if (@is_writable($save_path) === false) {
  84. $this->alert("上传目录没有写权限。");
  85. }
  86. //检查是否已上传
  87. if (@is_uploaded_file($tmp_name) === false) {
  88. $this->alert("上传失败。");
  89. }
  90. //检查文件大小
  91. if ($file_size > $max_size) {
  92. $this->alert("上传文件大小超过限制。");
  93. }
  94. //检查目录名
  95. $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
  96. if (empty($ext_arr[$dir_name])) {
  97. $this->alert("目录名不正确。");
  98. }
  99. //获得文件扩展名
  100. $temp_arr = explode(".", $file_name);
  101. $file_ext = array_pop($temp_arr);
  102. $file_ext = trim($file_ext);
  103. $file_ext = strtolower($file_ext);
  104. //检查扩展名
  105. if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
  106. $this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
  107. }
  108. //创建文件夹
  109. $ymd = date("Ymd");
  110. $ymd = date("Y") . '/' . date('md');
  111. $save_path .= $dir_name . "/" . $ymd . "/";
  112. $save_url .= $dir_name . "/" . $ymd . "/";
  113. if (!file_exists($save_path)) {
  114. if (!mkdir($save_path, 0775, true)) {
  115. $this->alert("创建目录失败:{$save_path}");
  116. }
  117. }
  118. //新文件名
  119. $new_file_name = date("His") . '_' . rand(10000, 99999) . '.' . $file_ext;
  120. //移动文件
  121. $file_path = $save_path . $new_file_name;
  122. if (move_uploaded_file($tmp_name, $file_path) === false) {
  123. $this->alert("上传文件失败。");
  124. }
  125. @chmod($file_path, 0777);
  126. $file_url = $save_url . $new_file_name;
  127. $file_info = getimagesize($file_path);
  128. $width = $file_info[0];
  129. $height = $file_info[1];
  130. $json = new \Services_JSON();
  131. if ($_GET['size']) {
  132. echo $json->encode(array('error' => 0, 'url' => $file_url, 'width' => $width, 'height' => $height));
  133. } else {
  134. echo $json->encode(array('error' => 0, 'url' => $file_url));
  135. }
  136. exit;
  137. }
  138. }
  139. public function doFileManagerJson() {
  140. //根目录路径,可以指定绝对路径,比如 /var/www/attached/
  141. $root_path = $this->IMG_UPLOAD_PATH . '/';
  142. //根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
  143. $root_url = $this->IMG_UPLOAD_URL . '/';
  144. //图片扩展名
  145. $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
  146. //目录名
  147. $dir_name = empty($_GET['dir']) ? '' : trim($_GET['dir']);
  148. if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {
  149. echo "Invalid Directory name.";
  150. exit;
  151. }
  152. if ($dir_name !== '') {
  153. $root_path .= $dir_name . "/";
  154. $root_url .= $dir_name . "/";
  155. if (!file_exists($root_path)) {
  156. mkdir($root_path);
  157. }
  158. }
  159. //根据path参数,设置各路径和URL
  160. if (empty($_GET['path'])) {
  161. $current_path = realpath($root_path) . '/';
  162. $current_url = $root_url;
  163. $current_dir_path = '';
  164. $moveup_dir_path = '';
  165. } else {
  166. $current_path = realpath($root_path) . '/' . $_GET['path'];
  167. $current_url = $root_url . $_GET['path'];
  168. $current_dir_path = $_GET['path'];
  169. $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
  170. }
  171. echo realpath($root_path);
  172. //排序形式,name or size or type
  173. $order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);
  174. //不允许使用..移动到上一级目录
  175. if (preg_match('/\.\./', $current_path)) {
  176. echo 'Access is not allowed.';
  177. exit;
  178. }
  179. //最后一个字符不是/
  180. if (!preg_match('/\/$/', $current_path)) {
  181. echo 'Parameter is not valid.';
  182. exit;
  183. }
  184. //目录不存在或不是目录
  185. if (!file_exists($current_path) || !is_dir($current_path)) {
  186. echo 'Directory does not exist.';
  187. exit;
  188. }
  189. //遍历目录取得文件信息
  190. $file_list = array();
  191. if ($handle = opendir($current_path)) {
  192. $i = 0;
  193. while (false !== ($filename = readdir($handle))) {
  194. if ($filename{0} == '.') continue;
  195. $file = $current_path . $filename;
  196. if (is_dir($file)) {
  197. $file_list[$i]['is_dir'] = true; //是否文件夹
  198. $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
  199. $file_list[$i]['filesize'] = 0; //文件大小
  200. $file_list[$i]['is_photo'] = false; //是否图片
  201. $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断
  202. } else {
  203. $file_list[$i]['is_dir'] = false;
  204. $file_list[$i]['has_file'] = false;
  205. $file_list[$i]['filesize'] = filesize($file);
  206. $file_list[$i]['dir_path'] = '';
  207. $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  208. $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
  209. $file_list[$i]['filetype'] = $file_ext;
  210. }
  211. $file_list[$i]['filename'] = $filename; //文件名,包含扩展名
  212. $file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
  213. $i++;
  214. }
  215. closedir($handle);
  216. }
  217. //排序
  218. function cmp_func($a, $b) {
  219. global $order;
  220. if ($a['is_dir'] && !$b['is_dir']) {
  221. return -1;
  222. } else if (!$a['is_dir'] && $b['is_dir']) {
  223. return 1;
  224. } else {
  225. if ($order == 'size') {
  226. if ($a['filesize'] > $b['filesize']) {
  227. return 1;
  228. } else if ($a['filesize'] < $b['filesize']) {
  229. return -1;
  230. } else {
  231. return 0;
  232. }
  233. } else if ($order == 'type') {
  234. return strcmp($a['filetype'], $b['filetype']);
  235. } else {
  236. return strcmp($a['filename'], $b['filename']);
  237. }
  238. }
  239. }
  240. usort($file_list, 'cmp_func');
  241. $result = array();
  242. //相对于根目录的上一级目录
  243. $result['moveup_dir_path'] = $moveup_dir_path;
  244. //相对于根目录的当前目录
  245. $result['current_dir_path'] = $current_dir_path;
  246. //当前目录的URL
  247. $result['current_url'] = $current_url;
  248. //文件数
  249. $result['total_count'] = count($file_list);
  250. //文件列表数组
  251. $result['file_list'] = $file_list;
  252. //输出JSON字符串
  253. $json = new \Services_JSON();
  254. echo $json->encode($result);
  255. }
  256. public function alert($msg) {
  257. $json = new \Services_JSON();
  258. echo $json->encode(array('error' => 1, 'message' => $msg));
  259. exit;
  260. }
  261. }