SettingController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-04-30
  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. $oldconfig=cmf_get_option('site_info');
  98. $options = $this->request->param('options/a');
  99. cmf_set_option('site_info',$options);
  100. $cmfSettings = $this->request->param('cmf_settings/a');
  101. $bannedUsernames = preg_replace("/[^0-9A-Za-z_\\x{4e00}-\\x{9fa5}-]/u", ",", $cmfSettings['banned_usernames']);
  102. $cmfSettings['banned_usernames'] = $bannedUsernames;
  103. cmf_set_option('cmf_settings', $cmfSettings);
  104. $cdnSettings = $this->request->param('cdn_settings/a');
  105. cmf_set_option('cdn_settings', $cdnSettings);
  106. $adminSettings = $this->request->param('admin_settings/a');
  107. $routeModel = new RouteModel();
  108. if (!empty($adminSettings['admin_password'])) {
  109. $routeModel->setRoute($adminSettings['admin_password'] . '$', 'admin/Index/index', [], 2, 5000);
  110. } else {
  111. $routeModel->deleteRoute('admin/Index/index', []);
  112. }
  113. $routeModel->getRoutes(true);
  114. if (!empty($adminSettings['admin_theme'])) {
  115. $result = cmf_set_dynamic_config([
  116. 'template' => [
  117. 'cmf_admin_default_theme' => $adminSettings['admin_theme']
  118. ]
  119. ]);
  120. if ($result === false) {
  121. $this->error('配置写入失败!');
  122. }
  123. }
  124. cmf_set_option('admin_settings', $adminSettings);
  125. $config= Db::name("option")
  126. ->where("option_name='site_info'")
  127. ->value("option_value");
  128. $config=json_decode($config,true);
  129. setcaches("getConfigPub",$config);
  130. $this->resetcache('getConfigPub',$options);
  131. $this->success("保存成功!", '');
  132. }
  133. }
  134. /**
  135. * 密码修改
  136. * @adminMenu(
  137. * 'name' => '密码修改',
  138. * 'parent' => 'default',
  139. * 'display'=> false,
  140. * 'hasView'=> true,
  141. * 'order' => 10000,
  142. * 'icon' => '',
  143. * 'remark' => '密码修改',
  144. * 'param' => ''
  145. * )
  146. */
  147. public function password()
  148. {
  149. return $this->fetch();
  150. }
  151. /**
  152. * 密码修改提交
  153. * @adminMenu(
  154. * 'name' => '密码修改提交',
  155. * 'parent' => 'password',
  156. * 'display'=> false,
  157. * 'hasView'=> false,
  158. * 'order' => 10000,
  159. * 'icon' => '',
  160. * 'remark' => '密码修改提交',
  161. * 'param' => ''
  162. * )
  163. */
  164. public function passwordPost()
  165. {
  166. if ($this->request->isPost()) {
  167. $data = $this->request->param();
  168. if (empty($data['old_password'])) {
  169. $this->error("原始密码不能为空!");
  170. }
  171. if (empty($data['password'])) {
  172. $this->error("新密码不能为空!");
  173. }
  174. $userId = cmf_get_current_admin_id();
  175. $admin = Db::name('user')->where("id", $userId)->find();
  176. $oldPassword = $data['old_password'];
  177. $password = $data['password'];
  178. $rePassword = $data['re_password'];
  179. if (cmf_compare_password($oldPassword, $admin['user_pass'])) {
  180. if ($password == $rePassword) {
  181. if (cmf_compare_password($password, $admin['user_pass'])) {
  182. $this->error("新密码不能和原始密码相同!");
  183. } else {
  184. Db::name('user')->where('id', $userId)->update(['user_pass' => cmf_password($password)]);
  185. $this->success("密码修改成功!");
  186. }
  187. } else {
  188. $this->error("密码输入不一致!");
  189. }
  190. } else {
  191. $this->error("原始密码不正确!");
  192. }
  193. }
  194. }
  195. /**
  196. * 上传限制设置界面
  197. * @adminMenu(
  198. * 'name' => '上传设置',
  199. * 'parent' => 'default',
  200. * 'display'=> true,
  201. * 'hasView'=> true,
  202. * 'order' => 10000,
  203. * 'icon' => '',
  204. * 'remark' => '上传设置',
  205. * 'param' => ''
  206. * )
  207. */
  208. public function upload()
  209. {
  210. $uploadSetting = cmf_get_upload_setting();
  211. $this->assign('upload_setting', $uploadSetting);
  212. return $this->fetch();
  213. }
  214. /**
  215. * 上传限制设置界面提交
  216. * @adminMenu(
  217. * 'name' => '上传设置提交',
  218. * 'parent' => 'upload',
  219. * 'display'=> false,
  220. * 'hasView'=> false,
  221. * 'order' => 10000,
  222. * 'icon' => '',
  223. * 'remark' => '上传设置提交',
  224. * 'param' => ''
  225. * )
  226. */
  227. public function uploadPost()
  228. {
  229. if ($this->request->isPost()) {
  230. //TODO 非空验证
  231. $uploadSetting = $this->request->post();
  232. cmf_set_option('upload_setting', $uploadSetting);
  233. $this->success('保存成功!');
  234. }
  235. }
  236. /**
  237. * 清除缓存
  238. * @adminMenu(
  239. * 'name' => '清除缓存',
  240. * 'parent' => 'default',
  241. * 'display'=> false,
  242. * 'hasView'=> true,
  243. * 'order' => 10000,
  244. * 'icon' => '',
  245. * 'remark' => '清除缓存',
  246. * 'param' => ''
  247. * )
  248. */
  249. public function clearCache()
  250. {
  251. $content = hook_one('admin_setting_clear_cache_view');
  252. if (!empty($content)) {
  253. return $content;
  254. }
  255. cmf_clear_cache();
  256. return $this->fetch();
  257. }
  258. /**
  259. * 私密设置
  260. */
  261. public function configpri(){
  262. $siteinfo=cmf_get_option('site_info');
  263. $name_coin=$siteinfo['name_coin'];
  264. $name_votes=$siteinfo['name_votes'];
  265. $this->assign('config', cmf_get_option('configpri'));
  266. $this->assign("name_coin",$name_coin);
  267. $this->assign("name_votes",$name_votes);
  268. return $this->fetch();
  269. }
  270. /**
  271. * 私密设置提交
  272. */
  273. public function configpriPost(){
  274. if ($this->request->isPost()) {
  275. $options = $this->request->param('options');
  276. $oldconfigpri=cmf_get_option('configpri');
  277. cmf_set_option('configpri', $options);
  278. $config= Db::name("option")
  279. ->where("option_name='configpri'")
  280. ->value("option_value");
  281. $config=json_decode($config,true);
  282. setcaches("getConfigPri",$config);
  283. $this->resetcache('getConfigPri',$options);
  284. $this->success("保存成功!", '');
  285. }
  286. }
  287. protected function resetcache($key='',$info=[]){
  288. if($key!='' && $info){
  289. delcache($key);
  290. hMSet($key,$info);
  291. }
  292. }
  293. }