PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/controller/editor.class.php

https://gitlab.com/imxieke/XCloud
PHP | 123 lines | 95 code | 7 blank | 21 comment | 21 complexity | c6ea8b01a0af0c8dbab205538020bb59 MD5 | raw file
  1. <?php
  2. /*
  3. * @link http://www.kalcaddle.com/
  4. * @author warlee | e-mail:kalcaddle@qq.com
  5. * @copyright warlee 2014.(Shanghai)Co.,Ltd
  6. * @license http://kalcaddle.com/tools/licenses/license.txt
  7. */
  8. class editor extends Controller{
  9. function __construct() {
  10. parent::__construct();
  11. $this->tpl = TEMPLATE . 'editor/';
  12. }
  13. // 多文件编辑器
  14. public function index(){
  15. $this->display('editor.php');
  16. }
  17. // 单文件编辑
  18. public function edit(){
  19. $this->assign('editor_config',$this->getConfig());//获取编辑器配置信息
  20. $this->display('edit.php');
  21. }
  22. // 获取文件数据
  23. public function fileGet(){
  24. $filename=_DIR($this->in['filename']);
  25. if (!is_readable($filename)) show_json($this->L['no_permission_read'],false);
  26. if (filesize($filename) >= 1024*1024*20) show_json($this->L['edit_too_big'],false);
  27. $filecontents=file_get_contents($filename);//文件内容
  28. $charset=get_charset($filecontents);
  29. if ($charset!='' || $charset!='utf-8') {
  30. $filecontents=mb_convert_encoding($filecontents,'utf-8',$charset);
  31. }
  32. $data = array(
  33. 'ext' => get_path_ext($filename),
  34. 'name' => iconv_app(get_path_this($filename)),
  35. 'filename' => rawurldecode($this->in['filename']),
  36. 'charset' => $charset,
  37. 'content' => $filecontents
  38. );
  39. show_json($data);
  40. }
  41. public function fileSave(){
  42. $filestr = rawurldecode($this->in['filestr']);
  43. $charset = $this->in['charset'];
  44. $path =_DIR($this->in['path']);
  45. if (!is_writable($path)) show_json($this->L['no_permission_write_file'],false);
  46. if ($charset !='' || $charset != 'utf-8') {
  47. $filestr=mb_convert_encoding($filestr,$this->in['charset'],'utf-8');
  48. }
  49. $fp=fopen($path,'wb');
  50. fwrite($fp,$filestr);
  51. fclose($fp);
  52. show_json($this->L['save_success']);
  53. }
  54. /*
  55. * 获取编辑器配置信息
  56. */
  57. public function getConfig(){
  58. $default = array(
  59. 'font_size' => '15px',
  60. 'theme' => 'clouds',
  61. 'auto_wrap' => 0,
  62. 'display_char' => 0,
  63. 'auto_complete' => 1,
  64. 'function_list' => 1
  65. );
  66. $config_file = USER.'data/editor_config.php';
  67. if (!file_exists($config_file)) {//不存在则创建
  68. $sql=new fileCache($config_file);
  69. $sql->reset($default);
  70. }else{
  71. $sql=new fileCache($config_file);
  72. $default = $sql->get();
  73. }
  74. if (!isset($default['function_list'])) {
  75. $default['function_list'] = 1;
  76. }
  77. return json_encode($default);
  78. }
  79. /*
  80. * 获取编辑器配置信息
  81. */
  82. public function setConfig(){
  83. $file = USER.'data/editor_config.php';
  84. if (!is_writeable($file)) {//配置不可写
  85. show_json($this->L['no_permission_write_file'],false);
  86. }
  87. $key= $this->in['k'];
  88. $value = $this->in['v'];
  89. if ($key !='' && $value != '') {
  90. $sql=new fileCache($file);
  91. if(!$sql->update($key,$value)){
  92. $sql->add($key,$value);//没有则添加一条
  93. }
  94. show_json($this->L["setting_success"]);
  95. }else{
  96. show_json($this->L['error'],false);
  97. }
  98. }
  99. //-----------------------------------------------
  100. /*
  101. * 获取字符串编码
  102. * @param:$ext 传入字符串
  103. */
  104. private function _get_charset(&$str) {
  105. if ($str == '') return 'utf-8';
  106. //前面检测成功则,自动忽略后面
  107. $charset=strtolower(mb_detect_encoding($str,$this->config['check_charset']));
  108. if (substr($str,0,3)==chr(0xEF).chr(0xBB).chr(0xBF)){
  109. $charset='utf-8';
  110. }else if($charset=='cp936'){
  111. $charset='gbk';
  112. }
  113. if ($charset == 'ascii') $charset = 'utf-8';
  114. return strtolower($charset);
  115. }
  116. }