Editpage.php 11 KB

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