SettingController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\RouteModel;
  13. use cmf\controller\AdminBaseController;
  14. use think\Db;
  15. /**
  16. * Class SettingController
  17. * @package app\admin\controller
  18. * @adminMenuRoot(
  19. * 'name' =>'设置',
  20. * 'action' =>'default',
  21. * 'parent' =>'',
  22. * 'display'=> true,
  23. * 'order' => 0,
  24. * 'icon' =>'cogs',
  25. * 'remark' =>'系统设置入口'
  26. * )
  27. */
  28. class SettingController extends AdminBaseController
  29. {
  30. /**
  31. * 网站信息
  32. * @adminMenu(
  33. * 'name' => '网站信息',
  34. * 'parent' => 'default',
  35. * 'display'=> true,
  36. * 'hasView'=> true,
  37. * 'order' => 0,
  38. * 'icon' => '',
  39. * 'remark' => '网站信息',
  40. * 'param' => ''
  41. * )
  42. */
  43. public function site()
  44. {
  45. $content = hook_one('admin_setting_site_view');
  46. if (!empty($content)) {
  47. return $content;
  48. }
  49. $noNeedDirs = [".", "..", ".svn", 'fonts'];
  50. $adminThemesDir = WEB_ROOT . config('template.cmf_admin_theme_path') . config('template.cmf_admin_default_theme') . '/public/assets/themes/';
  51. $adminStyles = cmf_scan_dir($adminThemesDir . '*', GLOB_ONLYDIR);
  52. $adminStyles = array_diff($adminStyles, $noNeedDirs);
  53. $cdnSettings = cmf_get_option('cdn_settings');
  54. $cmfSettings = cmf_get_option('cmf_settings');
  55. $adminSettings = cmf_get_option('admin_settings');
  56. $adminThemes = [];
  57. $themes = cmf_scan_dir(WEB_ROOT . config('template.cmf_admin_theme_path') . '/*', GLOB_ONLYDIR);
  58. foreach ($themes as $theme) {
  59. if (strpos($theme, 'admin_') === 0) {
  60. array_push($adminThemes, $theme);
  61. }
  62. }
  63. if (APP_DEBUG && false) { // TODO 没确定要不要可以设置默认应用
  64. $apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  65. $apps = array_diff($apps, $noNeedDirs);
  66. $this->assign('apps', $apps);
  67. }
  68. $this->assign('site_info', cmf_get_option('site_info'));
  69. $this->assign("admin_styles", $adminStyles);
  70. $this->assign("templates", []);
  71. $this->assign("admin_themes", $adminThemes);
  72. $this->assign("cdn_settings", $cdnSettings);
  73. $this->assign("admin_settings", $adminSettings);
  74. $this->assign("cmf_settings", $cmfSettings);
  75. return $this->fetch();
  76. }
  77. /**
  78. * 网站信息设置提交
  79. * @adminMenu(
  80. * 'name' => '网站信息设置提交',
  81. * 'parent' => 'site',
  82. * 'display'=> false,
  83. * 'hasView'=> false,
  84. * 'order' => 10000,
  85. * 'icon' => '',
  86. * 'remark' => '网站信息设置提交',
  87. * 'param' => ''
  88. * )
  89. */
  90. public function sitePost()
  91. {
  92. if ($this->request->isPost()) {
  93. $result = $this->validate($this->request->param(), 'SettingSite');
  94. if ($result !== true) {
  95. $this->error($result);
  96. }
  97. $options = $this->request->param('options/a');
  98. cmf_set_option('site_info', $options);
  99. $cmfSettings = $this->request->param('cmf_settings/a');
  100. $bannedUsernames = preg_replace("/[^0-9A-Za-z_\\x{4e00}-\\x{9fa5}-]/u", ",", $cmfSettings['banned_usernames']);
  101. $cmfSettings['banned_usernames'] = $bannedUsernames;
  102. cmf_set_option('cmf_settings', $cmfSettings);
  103. $cdnSettings = $this->request->param('cdn_settings/a');
  104. cmf_set_option('cdn_settings', $cdnSettings);
  105. $adminSettings = $this->request->param('admin_settings/a');
  106. // $routeModel = new RouteModel();
  107. // if (!empty($adminSettings['admin_password'])) {
  108. // $routeModel->setRoute($adminSettings['admin_password'] . '$', 'admin/Index/index', [], 2, 5000);
  109. // } else {
  110. // $routeModel->deleteRoute('admin/Index/index', []);
  111. // }
  112. // $routeModel->getRoutes(true);
  113. if (!empty($adminSettings['admin_theme'])) {
  114. $result = cmf_set_dynamic_config([
  115. 'template' => [
  116. 'cmf_admin_default_theme' => $adminSettings['admin_theme']
  117. ]
  118. ]);
  119. if ($result === false) {
  120. $this->error('配置写入失败!');
  121. }
  122. }
  123. cmf_set_option('admin_settings', $adminSettings);
  124. $this->success("保存成功!", '');
  125. }
  126. }
  127. /**
  128. * 密码修改
  129. * @adminMenu(
  130. * 'name' => '密码修改',
  131. * 'parent' => 'default',
  132. * 'display'=> false,
  133. * 'hasView'=> true,
  134. * 'order' => 10000,
  135. * 'icon' => '',
  136. * 'remark' => '密码修改',
  137. * 'param' => ''
  138. * )
  139. */
  140. public function password()
  141. {
  142. return $this->fetch();
  143. }
  144. /**
  145. * 密码修改提交
  146. * @adminMenu(
  147. * 'name' => '密码修改提交',
  148. * 'parent' => 'password',
  149. * 'display'=> false,
  150. * 'hasView'=> false,
  151. * 'order' => 10000,
  152. * 'icon' => '',
  153. * 'remark' => '密码修改提交',
  154. * 'param' => ''
  155. * )
  156. */
  157. public function passwordPost()
  158. {
  159. if ($this->request->isPost()) {
  160. $data = $this->request->param();
  161. if (empty($data['old_password'])) {
  162. $this->error("原始密码不能为空!");
  163. }
  164. if (empty($data['password'])) {
  165. $this->error("新密码不能为空!");
  166. }
  167. $userId = cmf_get_current_admin_id();
  168. $admin = Db::name('user')->where("id", $userId)->find();
  169. $oldPassword = $data['old_password'];
  170. $password = $data['password'];
  171. $rePassword = $data['re_password'];
  172. if (cmf_compare_password($oldPassword, $admin['user_pass'])) {
  173. if ($password == $rePassword) {
  174. if (cmf_compare_password($password, $admin['user_pass'])) {
  175. $this->error("新密码不能和原始密码相同!");
  176. } else {
  177. Db::name('user')->where('id', $userId)->update(['user_pass' => cmf_password($password)]);
  178. $this->success("密码修改成功!");
  179. }
  180. } else {
  181. $this->error("密码输入不一致!");
  182. }
  183. } else {
  184. $this->error("原始密码不正确!");
  185. }
  186. }
  187. }
  188. /**
  189. * 上传限制设置界面
  190. * @adminMenu(
  191. * 'name' => '上传设置',
  192. * 'parent' => 'default',
  193. * 'display'=> true,
  194. * 'hasView'=> true,
  195. * 'order' => 10000,
  196. * 'icon' => '',
  197. * 'remark' => '上传设置',
  198. * 'param' => ''
  199. * )
  200. */
  201. public function upload()
  202. {
  203. $uploadSetting = cmf_get_upload_setting();
  204. $this->assign('upload_setting', $uploadSetting);
  205. return $this->fetch();
  206. }
  207. /**
  208. * 上传限制设置界面提交
  209. * @adminMenu(
  210. * 'name' => '上传设置提交',
  211. * 'parent' => 'upload',
  212. * 'display'=> false,
  213. * 'hasView'=> false,
  214. * 'order' => 10000,
  215. * 'icon' => '',
  216. * 'remark' => '上传设置提交',
  217. * 'param' => ''
  218. * )
  219. */
  220. public function uploadPost()
  221. {
  222. if ($this->request->isPost()) {
  223. //TODO 非空验证
  224. $uploadSetting = $this->request->post();
  225. cmf_set_option('upload_setting', $uploadSetting);
  226. $this->success('保存成功!');
  227. }
  228. }
  229. /**
  230. * 清除缓存
  231. * @adminMenu(
  232. * 'name' => '清除缓存',
  233. * 'parent' => 'default',
  234. * 'display'=> false,
  235. * 'hasView'=> true,
  236. * 'order' => 10000,
  237. * 'icon' => '',
  238. * 'remark' => '清除缓存',
  239. * 'param' => ''
  240. * )
  241. */
  242. public function clearCache()
  243. {
  244. $content = hook_one('admin_setting_clear_cache_view');
  245. if (!empty($content)) {
  246. return $content;
  247. }
  248. cmf_clear_cache();
  249. return $this->fetch();
  250. }
  251. }