PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Inc/Lib/ORG/Net/Keditor.class.php

http://iiccms.googlecode.com/
PHP | 233 lines | 185 code | 13 blank | 35 comment | 49 complexity | f7a351cf904bc0a88937ee24d6970985 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * +---------------------------
  4. * kindeitor????
  5. * ??:??
  6. * ???luofei614@126.com
  7. * ???www.3g4k.com
  8. * +---------------------------
  9. */
  10. class Keditor extends Think {
  11. public $jspath = "/Public/editor/kindeditor.js";
  12. protected $config = array ();
  13. public $form = "form1";
  14. public function show($config = "") {
  15. static $incjs = false;
  16. if (! $incjs) {
  17. $result = "<script charset='utf-8' src='{$this->jspath}'></script>";
  18. $incjs = true;
  19. }
  20. if (empty ( $config )) {
  21. $config = implode ( ",", $this->config );
  22. $config = "{{$config}}";
  23. }
  24. $result .= "<script>KE.show({$config});</script>";
  25. return $result;
  26. }
  27. public static function upload($save_path = './Public/Upload/', $save_url = '/Public/Upload/', $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp'), $max_size = 1000000) {
  28. import ( "ORG.Net.JSON" );
  29. //??????
  30. function alert($msg) {
  31. header ( 'Content-type: text/html; charset=UTF-8' );
  32. $json = new Services_JSON ();
  33. echo $json->encode ( array ('error' => 1, 'message' => $msg ) );
  34. exit ();
  35. }
  36. if (empty ( $_FILES ) === false) {
  37. //????
  38. $file_name = $_FILES ['imgFile'] ['name'];
  39. //?????????
  40. $tmp_name = $_FILES ['imgFile'] ['tmp_name'];
  41. //????
  42. $file_size = $_FILES ['imgFile'] ['size'];
  43. //?????
  44. if (! $file_name) {
  45. alert ( "??????" );
  46. }
  47. //????
  48. if (@is_dir ( $save_path ) === false) {
  49. alert ( "????????".$save_path );
  50. }
  51. //???????
  52. if (@is_writable ( $save_path ) === false) {
  53. alert ( "??????????" );
  54. }
  55. //???????
  56. if (@is_uploaded_file ( $tmp_name ) === false) {
  57. alert ( "?????????????" );
  58. }
  59. //??????
  60. if ($file_size > $max_size) {
  61. alert ( "???????????" );
  62. }
  63. //???????
  64. $temp_arr = explode ( ".", $file_name );
  65. $file_ext = array_pop ( $temp_arr );
  66. $file_ext = trim ( $file_ext );
  67. $file_ext = strtolower ( $file_ext );
  68. //?????
  69. if (in_array ( $file_ext, $ext_arr ) === false) {
  70. alert ( "????????????????" );
  71. }
  72. //????
  73. $new_file_name = date ( "YmdHis" ) . '_' . rand ( 10000, 99999 ) . '.' . $file_ext;
  74. //????
  75. $file_path = $save_path . $new_file_name;
  76. if (move_uploaded_file ( $tmp_name, $file_path ) === false) {
  77. alert ( "???????" );
  78. }
  79. @chmod ( $file_path, 0644 );
  80. $file_url = $save_url . $new_file_name;
  81. $imgid = $save_path . $new_file_name;
  82. header ( 'Content-type: text/html; charset=UTF-8' );
  83. //$json = new Services_JSON ();
  84. //echo $json->encode ( array ('error' => 0, 'url' => $file_url, 'imgid' => $imgid ) );
  85. $all_info=array();
  86. $all_info['return']=array ('error' => 0, 'url' => $file_url, 'imgid' => $imgid );
  87. $all_info['file']['save_path']=$save_path;
  88. $all_info['file']['save_url']=$save_url;
  89. $all_info['file']['new_file_name']=$new_file_name;
  90. return $all_info;
  91. //exit ();
  92. }
  93. }
  94. public static function filemanager($root_path = "/Public/Upload/", $root_url = "/Public/Upload/", $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp')) {
  95. import ( "ORG.Net.JSON" );
  96. if (empty ( $_GET ['path'] )) {
  97. $current_path = realpath ( $root_path ) . '/';
  98. $current_url = $root_url;
  99. $current_dir_path = '';
  100. $moveup_dir_path = '';
  101. } else {
  102. $current_path = realpath ( $root_path ) . '/' . $_GET ['path'];
  103. $current_url = $root_url . $_GET ['path'];
  104. $current_dir_path = $_GET ['path'];
  105. $moveup_dir_path = preg_replace ( '/(.*?)[^\/]+\/$/', '$1', $current_dir_path );
  106. }
  107. //?????name or size or type
  108. $order = empty ( $_GET ['order'] ) ? 'name' : strtolower ( $_GET ['order'] );
  109. //?????..????????
  110. if (preg_match ( '/\.\./', $current_path )) {
  111. echo 'Access is not allowed.';
  112. exit ();
  113. }
  114. //????????/
  115. if (! preg_match ( '/\/$/', $current_path )) {
  116. echo 'Parameter is not valid.';
  117. exit ();
  118. }
  119. //??????????
  120. if (! file_exists ( $current_path ) || ! is_dir ( $current_path )) {
  121. echo 'Directory does not exist.';
  122. exit ();
  123. }
  124. //??????????
  125. $file_list = array ();
  126. if ($handle = opendir ( $current_path )) {
  127. $i = 0;
  128. while ( false !== ($filename = readdir ( $handle )) ) {
  129. if ($filename {0} == '.')
  130. continue;
  131. $file = $current_path . $filename;
  132. if (is_dir ( $file )) {
  133. $file_list [$i] ['is_dir'] = true; //?????
  134. $file_list [$i] ['has_file'] = (count ( scandir ( $file ) ) > 2); //?????????
  135. $file_list [$i] ['filesize'] = 0; //????
  136. $file_list [$i] ['is_photo'] = false; //????
  137. $file_list [$i] ['filetype'] = ''; //???????????
  138. } else {
  139. $file_list [$i] ['is_dir'] = false;
  140. $file_list [$i] ['has_file'] = false;
  141. $file_list [$i] ['filesize'] = filesize ( $file );
  142. $file_list [$i] ['dir_path'] = '';
  143. $file_ext = strtolower ( array_pop ( explode ( '.', trim ( $file ) ) ) );
  144. $file_list [$i] ['is_photo'] = in_array ( $file_ext, $ext_arr );
  145. $file_list [$i] ['filetype'] = $file_ext;
  146. }
  147. $file_list [$i] ['filename'] = $filename; //?????????
  148. $file_list [$i] ['datetime'] = date ( 'Y-m-d H:i:s', filemtime ( $file ) ); //????????
  149. $i ++;
  150. }
  151. closedir ( $handle );
  152. }
  153. function cmp_func($a, $b) {
  154. global $order;
  155. if ($a ['is_dir'] && ! $b ['is_dir']) {
  156. return - 1;
  157. } else if (! $a ['is_dir'] && $b ['is_dir']) {
  158. return 1;
  159. } else {
  160. if ($order == 'size') {
  161. if ($a ['filesize'] > $b ['filesize']) {
  162. return 1;
  163. } else if ($a ['filesize'] < $b ['filesize']) {
  164. return - 1;
  165. } else {
  166. return 0;
  167. }
  168. } else if ($order == 'type') {
  169. return strcmp ( $a ['filetype'], $b ['filetype'] );
  170. } else {
  171. return strcmp ( $a ['filename'], $b ['filename'] );
  172. }
  173. }
  174. }
  175. usort ( $file_list, 'cmp_func' );
  176. $result = array ();
  177. //????????????
  178. $result ['moveup_dir_path'] = $moveup_dir_path;
  179. //???????????
  180. $result ['current_dir_path'] = $current_dir_path;
  181. //?????URL
  182. $result ['current_url'] = $current_url;
  183. //???
  184. $result ['total_count'] = count ( $file_list );
  185. //??????
  186. $result ['file_list'] = $file_list;
  187. //??JSON???
  188. header ( 'Content-type: application/json; charset=UTF-8' );
  189. $json = new Services_JSON ();
  190. echo $json->encode ( $result );
  191. }
  192. public static function delimg($imgfield) {
  193. $imgs = explode ( "|", $imgfield );
  194. for($i = 0; $i <= count ( $imgs ) - 2; $i ++) {
  195. @(unlink ( $imgs [$i] ));
  196. }
  197. }
  198. public function __set($name, $value) {
  199. if (strpos ( $value, "function" ) === FALSE and strpos ( $value, "[" ) === FALSE and ! is_int ( $value )) {
  200. if (is_bool ( $value )) {
  201. $val = $value ? "true" : "false";
  202. } else {
  203. $val = "'{$value}'";
  204. }
  205. } else {
  206. $val = $value;
  207. }
  208. if ($name == "items" and $value == "little") { //????
  209. $val = "['fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist', '|', 'emoticons', 'image', 'link']
  210. ";
  211. }
  212. if ($name == "afterCreate" and $value == "ctrlenter") { //ctrenter????
  213. $val = "function(id){KE.event.ctrl(document, 13, function() {KE.util.setData(id);document.forms['{$this->form}'].submit();});KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {KE.util.setData(id);document.forms['{$this->form}'].submit();}); }";
  214. }
  215. $this->config [$name] = "{$name}:{$val}";
  216. }
  217. public function __get($name) {
  218. return $this->config [$name];
  219. }
  220. }