AdminSettingController.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class AdminSettingController extends BaseController {
  5. //保存配置
  6. public function saveConfig(){
  7. $login_user = $this->checkLogin();
  8. $this->checkAdmin();
  9. $register_open = intval(I("register_open")) ;
  10. $history_version_count = intval(I("history_version_count")) ;
  11. $oss_open = intval(I("oss_open")) ;
  12. $home_page = intval(I("home_page")) ;
  13. $home_item = intval(I("home_item")) ;
  14. $oss_setting = I("oss_setting") ;
  15. D("Options")->set("history_version_count" ,$history_version_count) ;
  16. D("Options")->set("register_open" ,$register_open) ;
  17. D("Options")->set("home_page" ,$home_page) ;
  18. D("Options")->set("home_item" ,$home_item) ;
  19. if ($oss_open) {
  20. $this->checkComposerPHPVersion();
  21. D("Options")->set("oss_setting" , json_encode( $oss_setting)) ;
  22. }
  23. D("Options")->set("oss_open" ,$oss_open) ;
  24. $this->sendResult(array());
  25. }
  26. //加载配置
  27. public function loadConfig(){
  28. $login_user = $this->checkLogin();
  29. $this->checkAdmin();
  30. $oss_open = D("Options")->get("oss_open" ) ;
  31. $register_open = D("Options")->get("register_open" ) ;
  32. $history_version_count = D("Options")->get("history_version_count" ) ;
  33. $oss_setting = D("Options")->get("oss_setting" ) ;
  34. $home_page = D("Options")->get("home_page" ) ;
  35. $home_item = D("Options")->get("home_item" ) ;
  36. $ldap_form = json_decode($ldap_form,1);
  37. $oss_setting = json_decode($oss_setting,1);
  38. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  39. if ($register_open === false) {
  40. $this->sendResult(array());
  41. }else{
  42. $array = array(
  43. "oss_open"=>$oss_open ,
  44. "register_open"=>$register_open ,
  45. "history_version_count"=>$history_version_count ,
  46. "home_page"=>$home_page ,
  47. "home_item"=>$home_item ,
  48. "oss_setting"=>$oss_setting ,
  49. );
  50. $this->sendResult($array);
  51. }
  52. }
  53. //保存Ldap配置
  54. public function saveLdapConfig(){
  55. $login_user = $this->checkLogin();
  56. $this->checkAdmin();
  57. $ldap_open = intval(I("ldap_open")) ;
  58. $ldap_form = I("ldap_form") ;
  59. if ($ldap_open) {
  60. if (!$ldap_form['user_field']) {
  61. $ldap_form['user_field'] = 'cn';
  62. }
  63. if( !extension_loaded( 'ldap' ) ) {
  64. $this->sendError(10011,"你尚未安装php-ldap扩展。如果是普通PHP环境,请手动安装之。如果是使用之前官方docker镜像,则需要重新安装镜像。方法是:备份 /showdoc_data 整个目录,然后全新安装showdoc,接着用备份覆盖/showdoc_data 。然后递归赋予777可写权限。");
  65. return ;
  66. }
  67. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  68. if (!$ldap_conn) {
  69. $this->sendError(10011,"Can't connect to LDAP server");
  70. return ;
  71. }
  72. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  73. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  74. if (!$rs) {
  75. $this->sendError(10011,"Can't bind to LDAP server");
  76. return ;
  77. }
  78. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  79. $data = ldap_get_entries($ldap_conn, $result);
  80. for ($i=0; $i<$data["count"]; $i++) {
  81. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  82. if (!$ldap_user) {
  83. continue ;
  84. }
  85. //如果该用户不在数据库里,则帮助其注册
  86. if(!D("User")->isExist($ldap_user)){
  87. D("User")->register($ldap_user,$ldap_user.time());
  88. }
  89. }
  90. D("Options")->set("ldap_form" , json_encode( $ldap_form)) ;
  91. }
  92. D("Options")->set("ldap_open" ,$ldap_open) ;
  93. $this->sendResult(array());
  94. }
  95. //加载Ldap配置
  96. public function loadLdapConfig(){
  97. $login_user = $this->checkLogin();
  98. $this->checkAdmin();
  99. $ldap_open = D("Options")->get("ldap_open" ) ;
  100. $ldap_form = D("Options")->get("ldap_form" ) ;
  101. $ldap_form = json_decode($ldap_form,1);
  102. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  103. if ($register_open === false) {
  104. $this->sendResult(array());
  105. }else{
  106. $array = array(
  107. "ldap_open"=>$ldap_open ,
  108. "ldap_form"=>$ldap_form ,
  109. );
  110. $this->sendResult($array);
  111. }
  112. }
  113. //保存Oauth2配置
  114. public function saveOauth2Config(){
  115. $login_user = $this->checkLogin();
  116. $this->checkAdmin();
  117. $this->checkComposerPHPVersion();
  118. $oauth2_open = intval(I("oauth2_open")) ;
  119. $oauth2_form = I("oauth2_form") ;
  120. D("Options")->set("oauth2_form" , json_encode( $oauth2_form)) ;
  121. D("Options")->set("oauth2_open" ,$oauth2_open) ;
  122. $this->sendResult(array());
  123. }
  124. //加载Oauth2配置
  125. public function loadOauth2Config(){
  126. $login_user = $this->checkLogin();
  127. $this->checkAdmin();
  128. $oauth2_open = D("Options")->get("oauth2_open" ) ;
  129. $oauth2_form = D("Options")->get("oauth2_form" ) ;
  130. $oauth2_form = json_decode($oauth2_form,1);
  131. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  132. if ($register_open === false) {
  133. $this->sendResult(array());
  134. }else{
  135. $array = array(
  136. "oauth2_open"=>$oauth2_open ,
  137. "oauth2_form"=>$oauth2_form ,
  138. );
  139. $this->sendResult($array);
  140. }
  141. }
  142. public function getLoginSecretKey(){
  143. $login_user = $this->checkLogin();
  144. $this->checkAdmin();
  145. $login_secret_key = D("Options")->get("login_secret_key") ;
  146. if(!$login_secret_key){
  147. $login_secret_key = md5("rgrsfsrfsrf".time().rand());
  148. D("Options")->set("login_secret_key",$login_secret_key) ;
  149. }
  150. $this->sendResult(array("login_secret_key"=>$login_secret_key));
  151. }
  152. public function resetLoginSecretKey(){
  153. $login_user = $this->checkLogin();
  154. $this->checkAdmin();
  155. $login_secret_key = md5("rgrsfsrfsrf".time().rand());
  156. D("Options")->set("login_secret_key",$login_secret_key) ;
  157. $this->sendResult(array("login_secret_key"=>$login_secret_key));
  158. }
  159. public function checkLdapLogin(){
  160. $username = 'admin';
  161. $password = '123456';
  162. $ldap_open = D("Options")->get("ldap_open" ) ;
  163. $ldap_form = D("Options")->get("ldap_form" ) ;
  164. $ldap_form = json_decode($ldap_form,1);
  165. if (!$ldap_open) {
  166. return ;
  167. }
  168. if (!$ldap_form['user_field']) {
  169. $ldap_form['user_field'] = 'cn';
  170. }
  171. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  172. if (!$ldap_conn) {
  173. $this->sendError(10011,"Can't connect to LDAP server");
  174. return ;
  175. }
  176. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  177. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  178. if (!$rs) {
  179. $this->sendError(10011,"Can't bind to LDAP server");
  180. return ;
  181. }
  182. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  183. $data = ldap_get_entries($ldap_conn, $result);
  184. for ($i=0; $i<$data["count"]; $i++) {
  185. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  186. $dn = $data[$i]["dn"] ;
  187. if ($ldap_user == $username) {
  188. //如果该用户不在数据库里,则帮助其注册
  189. $userInfo = D("User")->isExist($username) ;
  190. if(!$userInfo){
  191. D("User")->register($ldap_user,$ldap_user.time());
  192. }
  193. $rs2=ldap_bind($ldap_conn, $dn , $password);
  194. if ($rs2) {
  195. D("User")->updatePwd($userInfo['uid'], $password);
  196. $this->sendResult(array());
  197. return ;
  198. }
  199. }
  200. }
  201. $this->sendError(10011,"用户名或者密码错误");
  202. }
  203. }