Editpage.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Config;
  5. use think\console\Input;
  6. use think\Db;
  7. use think\Exception;
  8. class Editpage extends Backend
  9. {
  10. public function index()
  11. {
  12. $module = $this->request->request('module');
  13. $c = str_replace('.', '/', $this->request->request('c'));
  14. $a = $this->request->request('a');
  15. $type = $this->request->request('type');
  16. switch ($type) {
  17. case 'c':
  18. $data['filepath'] = $this->getControllerPath($c);
  19. $data['language_type'] = 'php';
  20. break;
  21. case 'm':
  22. $data['filepath'] = $this->getModelPath($c);
  23. $data['language_type'] = 'php';
  24. break;
  25. case 'v':
  26. $data['filepath'] = strtolower(APP_PATH . $module . '/view/' . $this->toUnderScore($c) . '/' . $this->toUnderScore($a) . '.html');
  27. $data['language_type'] = 'html';
  28. break;
  29. case 'j':
  30. $data['filepath'] = strtolower('assets/js/backend/' . $this->toUnderScore($c) . '.js');
  31. $data['language_type'] = 'javascript';
  32. break;
  33. default:
  34. $lang = strip_tags($this->request->langset());
  35. $lang = $lang ? $lang : config('default_lang');
  36. $data['filepath'] = strtolower(APP_PATH . $module . '/lang/' . $lang . '/' . $this->toUnderScore($c) . '.php');
  37. $data['language_type'] = 'php';
  38. break;
  39. }
  40. $editpage_config = get_addon_config('editpage');
  41. if (file_exists($data['filepath'])) {
  42. $data['size'] = filesize($data['filepath']);//返回文件的字节
  43. $data['size'] = round($data['size'] / 1024, 2);
  44. $data['fileatime'] = date('Y-m-d H:i:s', fileatime($data['filepath']));
  45. $data['filectime'] = date('Y-m-d H:i:s', filectime($data['filepath']));
  46. $data['code'] = file_get_contents($data['filepath']);
  47. } else {
  48. $data['code'] = '抱歉,没有找到相关文件!';
  49. $editpage_config['setreadonly'] = 1;
  50. }
  51. $data['editpage_config'] = $editpage_config;
  52. $this->assignconfig($data);
  53. $this->view->assign($data);
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 保存文件并备份
  58. *
  59. * @param 类型 $arg1 参数一的说明
  60. * @return array 返回类型
  61. * @example 示例
  62. * @author Created by xing <lx@xibuts.cn>
  63. */
  64. public function savefile()
  65. {
  66. if ($this->request->isPost()) {
  67. if (!config('app_debug')) {
  68. $this->error('只在调试模式下可用!');
  69. }
  70. $file = $this->request->request('file');
  71. $content = $this->request->request('content');
  72. if (!file_exists($file)) {
  73. $this->error('文件不存在!');
  74. }
  75. //文件备份
  76. $info = pathinfo($file);
  77. $backup_path = RUNTIME_PATH . 'editpage_backup/' . date('Ym') . '/';
  78. if (!is_dir($backup_path)) {
  79. mkdir($backup_path, 0777, true);
  80. }
  81. $backup_path .= date('YmdH') . $info['basename'];
  82. if (!file_exists($backup_path)) {
  83. copy($file, $backup_path);
  84. }
  85. //重新写入文件
  86. if (file_put_contents($file, trim($content)) !== false) {
  87. $this->success();
  88. } else {
  89. $this->error('文件写入失败');
  90. }
  91. }
  92. }
  93. /**
  94. * 命令行工具
  95. *
  96. * @param 类型 $arg1 参数一的说明
  97. * @return array 返回类型
  98. * @example 示例
  99. * @author Created by xing <lx@xibuts.cn>
  100. */
  101. public function command()
  102. {
  103. if ($this->request->isPost()) {
  104. //设置过滤方法
  105. $this->request->filter(['strip_tags', 'trim']);
  106. $content = $this->request->request('content');
  107. $contentArr = explode("\n", $content);
  108. $command = '';
  109. if (is_array($contentArr)) {
  110. $command = trim($contentArr[count($contentArr) - 1]);
  111. }
  112. if (strpos($command, 'php think') === 0) {
  113. $command = explode(' ', $command);
  114. if (isset($command[3])) {
  115. $data['type'] = $command[2];
  116. unset($command[0]);
  117. unset($command[1]);
  118. unset($command[2]);
  119. $data['params'] = json_encode(array_values($command));
  120. $data['command'] = "php think {$data['type']} " . implode(' ', $command);
  121. $data['executetime'] = time();
  122. }
  123. }
  124. if (isset($data)) {
  125. $commandName = "\\app\\admin\\command\\" . ucfirst($data['type']);
  126. $input = new Input(json_decode($data['params']));
  127. $output = new \addons\editpage\library\Output();
  128. $command = new $commandName($data['type']);
  129. try {
  130. $command->run($input, $output);
  131. $result = implode("\n", $output->getMessage());
  132. } catch (Exception $e) {
  133. $result = implode("\n", $output->getMessage()) . "\n";
  134. $result .= $e->getMessage();
  135. }
  136. $result = trim($result);
  137. $this->success($result);
  138. }
  139. exit();
  140. }
  141. $editpage_config = get_addon_config('editpage');
  142. $editpage_config['setreadonly'] = 0;
  143. $data['editpage_config'] = $editpage_config;
  144. $this->assignconfig($data);
  145. $this->view->assign($data);
  146. return $this->view->fetch();
  147. }
  148. /**
  149. * 得到控制器文件
  150. *
  151. * @param 类型 $arg1 参数一的说明
  152. * @return array 返回类型
  153. * @example 示例
  154. * @author Created by xing <lx@xibuts.cn>
  155. */
  156. private function getControllerPath($controller)
  157. {
  158. $controller = str_replace('\\', '/', $controller);
  159. if (stripos($controller, '/') !== false) {
  160. $controllerArr = explode('/', $controller);
  161. end($controllerArr);
  162. $key = key($controllerArr);
  163. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  164. } else {
  165. $controllerArr = [ucfirst($controller)];
  166. }
  167. $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
  168. $pathArr = $controllerArr;
  169. array_unshift($pathArr, '', 'application', 'admin', 'controller');
  170. $classFile = ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php";
  171. return $this->pathAction($classFile);
  172. }
  173. /**
  174. * 得到模型文件
  175. *
  176. * @param 类型 $arg1 参数一的说明
  177. * @return array 返回类型
  178. * @example 示例
  179. * @author Created by xing <lx@xibuts.cn>
  180. */
  181. private function getModelPath($controller)
  182. {
  183. $controller = str_replace('\\', '/', $controller);
  184. if (stripos($controller, '/') !== false) {
  185. $controllerArr = explode('/', $controller);
  186. end($controllerArr);
  187. $key = key($controllerArr);
  188. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  189. } else {
  190. $key = 0;
  191. $controllerArr = [ucfirst($controller)];
  192. }
  193. $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
  194. $pathArr = $controllerArr;
  195. array_unshift($pathArr, '', 'application', 'admin', 'controller');
  196. $classFile = $this->pathAction(ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php");
  197. $classContent = file_get_contents($classFile);
  198. $uniqueName = uniqid("FastAdmin") . $classSuffix;
  199. $classContent = str_replace(
  200. "class " . $controllerArr[$key] . $classSuffix . " ",
  201. 'class ' . $uniqueName . ' ',
  202. $classContent
  203. );
  204. $classContent = preg_replace("/namespace\s(.*);/", 'namespace ' . __NAMESPACE__ . ";", $classContent);
  205. $modelRegexArr = [
  206. "/\\\$this\->model\s*=\s*model\(['|\"](\w+)['|\"]\);/", "/\\\$this\->model\s*=\s*new\s+([a-zA-Z\\\]+);/"
  207. ];
  208. $modelRegex = preg_match($modelRegexArr[0], $classContent) ? $modelRegexArr[0] : $modelRegexArr[1];
  209. preg_match_all($modelRegex, $classContent, $matches);
  210. $module = $this->request->request('module');
  211. if (isset($matches[1]) && isset($matches[1][0]) && $matches[1][0]) {
  212. if (strpos($matches[1][0], '\\') === false) {
  213. $matches[1][0] = 'app\\' . $module . '\model\\' . $matches[1][0];
  214. }
  215. $model = $matches[1][0];
  216. } else {
  217. $model = 'app\\' . $module . '\model\\' . $controllerArr[$key];
  218. }
  219. if (!class_exists($model) && strpos($model, $module)) {
  220. $model = str_replace($module, 'common', $model);
  221. } elseif (!class_exists($model) && strpos($model, 'common')) {
  222. $model = str_replace('common', $module, $model);
  223. }
  224. if (class_exists($model)) {
  225. $func = new \ReflectionClass($model);
  226. $model = $func->getFileName();
  227. }
  228. return $this->pathAction($model);
  229. }
  230. /**
  231. * 对路径中的文件名处理
  232. *
  233. * @param 类型 $arg1 参数一的说明
  234. * @return array 返回类型
  235. * @example 示例
  236. * @author Created by xing <lx@xibuts.cn>
  237. */
  238. private function pathAction($path)
  239. {
  240. $path = str_replace('//', '/', $path);
  241. if (!file_exists($path)) {
  242. $filepath = strtolower($path);
  243. $info = pathinfo($filepath);
  244. if (isset($info['extension']) && $info['extension'] == 'php') {
  245. $filepath = $info['dirname'] . '/' . ucfirst($info['basename']);
  246. }
  247. } else {
  248. $filepath = $path;
  249. }
  250. return $filepath;
  251. }
  252. //下划线命名到驼峰命名
  253. private function toCamelCase($str)
  254. {
  255. $array = explode('_', $str);
  256. $result = $array[0];
  257. $len=count($array);
  258. if ($len>1) {
  259. for ($i=1; $i<$len; $i++) {
  260. $result.= ucfirst($array[$i]);
  261. }
  262. }
  263. return $result;
  264. }
  265. private function toUnderScore($str)
  266. {
  267. $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
  268. return '_' . strtolower($matchs[0]);
  269. }, $str);
  270. return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
  271. }
  272. }