Init.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\common\behavior;
  3. use app\common\model\Config as ConfigModel;
  4. /**
  5. * 初始化配置信息行为
  6. * 将系统配置信息合并到本地配置
  7. * @package app\common\behavior
  8. */
  9. class Init{
  10. private $webdb;
  11. public function run(&$params){
  12. header('Access-Control-Allow-Origin: *');
  13. header("Access-Control-Allow-Credentials: true");
  14. header("Access-Control-Allow-Headers: *");
  15. header("Access-Control-Expose-Headers:*");
  16. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  17. if ($_SERVER['REQUEST_METHOD']=='OPTIONS' || request()->isOptions()) {
  18. die('OPTIONS请求仅响应给ajax跨域!!!');
  19. }
  20. // 如果是安装系统,不需要执行
  21. if(defined('BIND_MODULE')&&BIND_MODULE=='install') return;
  22. if(empty(get_cookie('user_sid'))){ //分配每个用户一个唯一字串
  23. set_cookie('user_sid',rands(10));
  24. define('FIRST_LOAD',true); //首次访问
  25. }
  26. if(input('get.in')!=''){
  27. set_cookie('browser_type',input('get.in'));
  28. }
  29. if(input('get.in')=='pc'||get_cookie('browser_type')=='pc'){
  30. //define('IN_WAP',false);
  31. }elseif(input('get.in')=='wap'||get_cookie('browser_type')=='wap'){
  32. if(ENTRANCE!=='admin'){ //后台,强制不要用WAP
  33. define('IN_WAP',true);
  34. }
  35. }elseif(in_wap()){
  36. define('IN_WAP',true);
  37. }
  38. //define('IN_WAP',true);
  39. if(IN_WAP===true){
  40. config('template.cache_prefix','wap_');
  41. }else{
  42. config('paginate',['type'=>'page\Pc_page','var_page'=>'page','list_rows'=>20]); //分页参数设置
  43. }
  44. if (request()->header('inapi')) {
  45. set_cookie('inApi',request()->header('inapi'),3600*24*7);
  46. }
  47. define('TEMPLATE_PATH',ROOT_PATH.'template/');
  48. define('IS_POST',request()->isPost()?true:false);
  49. // 获取前台访问网址,是否放在根目录
  50. //$base_file = request()->baseFile();
  51. $web_path = '/'; // substr($base_file,0,strripos($base_file,'/')+1);
  52. define('PUBLIC_URL',$web_path.'public/'); //静态文件访问网址,是浏览器访问的网址,不是硬盘路径
  53. define('PUBLIC_PATH',ROOT_PATH.'public'.DS); //静态文件硬盘路径,是磁盘路径,不是浏览器的访问路径
  54. define('UPLOAD_URL',PUBLIC_URL.'uploads/'); //上传的附件访问网址,是浏览器访问的网址,不是硬盘路径
  55. define('UPLOAD_PATH',PUBLIC_PATH.'uploads'.DS); //上传的附件硬盘路径,是磁盘路径,不是浏览器的访问路径
  56. define('STATIC_URL',PUBLIC_URL.'static/'); //图片JS及CSS的访问网址,是浏览器访问的网址,不是硬盘路径
  57. define('STATIC_PATH',PUBLIC_PATH.'static'.DS); //图片JS及CSS的硬盘路径,是磁盘路径,不是浏览器的访问路径
  58. define('PLUGINS_PATH',ROOT_PATH.'plugins'.DS); //插件文件的硬盘路径
  59. define('CACHE_DIR',ROOT_PATH.'runtime'.DS); //缓存目录的硬盘路径
  60. define('QUN',cache('cache_modules_config') ? modules_config('qun')['name'] : '社群'); //圈子名称
  61. // 模板输出字符串内容替换
  62. $view_replace_str = [
  63. '__UPLOADS__'=>PUBLIC_URL.'uploads',
  64. '__STATIC__'=>PUBLIC_URL.'static',
  65. '__DOMAIN__'=>request()->domain(),
  66. '__QUN__'=>QUN,
  67. ];
  68. define('ADMIN_FILENAME',config('admin.filename')?:'admin.php'); //后台入口文件名
  69. //存入配置文件方便调用
  70. config('view_replace_str',$view_replace_str);
  71. $module='';
  72. $dispatch=request()->dispatch();
  73. if(isset($dispatch['module'])){
  74. $module=$dispatch['module'][0];
  75. }
  76. $this->webdb=cache('webdb');
  77. if(empty($this->webdb)){
  78. $this->webdb=ConfigModel::getConfig();
  79. cache('webdb',$this->webdb);
  80. }
  81. //定义根目录访问频道二级页
  82. if (ENTRANCE==='index' && empty($module) && $this->webdb['set_module_index']==$this->webdb['set_module_wapindex'] && config('default_module')==$this->webdb['set_module_index']) {
  83. $module = $this->webdb['set_module_index'];
  84. $dispatch = [
  85. 'type'=>'module',
  86. 'module'=>[
  87. $this->webdb['set_module_index'],'','',
  88. ],
  89. ];
  90. request()->dispatch($dispatch);
  91. }
  92. if($this->webdb['www_url']){
  93. request()->domain($this->webdb['www_url']); //解决有的服务器无法识别https的问题,需要在后台定义域名网址
  94. }else{
  95. //空间不能识别https
  96. if(($_SERVER['HTTP_X_CLIENT_SCHEME']=='https'||$_SERVER['REDIRECT_HTTP_X_CLIENT_SCHEME']=='https')&&!strstr(request()->domain(),'https://')){
  97. request()->domain(str_replace('http://','https://',request()->domain()));
  98. }
  99. }
  100. //把相应的插件或频道模块的二维数组插入到一维数组去使用
  101. if($dispatch['module'][1]=='plugin'&&$dispatch['module'][2]=='execute'){
  102. $plugin_name=input('plugin_name');
  103. if($plugin_name&&is_array($this->webdb['P__'.$plugin_name])){
  104. $this->webdb=array_merge($this->webdb,$this->webdb['P__'.$plugin_name]);
  105. }
  106. }elseif($dispatch['module'][0]&&$this->webdb['M__'.$dispatch['module'][0]]){
  107. $this->webdb=array_merge($this->webdb,$this->webdb['M__'.$dispatch['module'][0]]);
  108. }
  109. if( (isset($_GET['qun_wxapp_appid'])&&empty($_GET['qun_wxapp_appid'])) || isset($_SERVER['HTTP_WXAPPID'])&&empty($_SERVER['HTTP_WXAPPID']) ){
  110. cookie('qun_wxapp_appid',null);
  111. }elseif ( ($qun_wxapp_appid = input('qun_wxapp_appid')?:(request()->header('wxappid')?:cookie('qun_wxapp_appid')) )!=false ) {
  112. $ar = wxapp_cfg($qun_wxapp_appid);
  113. if ($ar) {
  114. foreach($ar AS $_key=>$rs){
  115. $this->webdb['_'.$_key] = $this->webdb[$_key];
  116. if (in_array($_key, ['appid','uid','status','aid']) || $this->webdb[$_key]==='') {
  117. unset($ar[$_key]);
  118. continue ;
  119. }
  120. }
  121. $this->webdb = array_merge($this->webdb,$ar?:[]);
  122. }else{
  123. $qun_wxapp_appid = '';
  124. }
  125. cookie('qun_wxapp_appid',$qun_wxapp_appid?:null);
  126. }
  127. $this->webdb['QB_VERSION']='X1.0'; //系统版本号
  128. config('webdb',$this->webdb);
  129. //md5style=xxx 前台风格预览
  130. if(input('get.md5style') && mymd5(input('get.md5style'),'DE')){
  131. cookie('index_style',input('get.md5style'),1800);
  132. delete_dir(RUNTIME_PATH.'temp');
  133. }
  134. if(cookie('index_style') && mymd5(cookie('index_style'),'DE')){
  135. $index_style = mymd5(cookie('index_style'),'DE'); //前台风格预览演示
  136. }elseif(in_wap()){
  137. $index_style = $this->webdb['wapstyle']?:'default'; //前台手机版风格
  138. }else{
  139. if(IN_WAP===true){
  140. $index_style = $this->webdb['wapstyle']?:'default'; //前台手机版风格
  141. }else{
  142. $index_style = $this->webdb['style']?:'default'; //前台电脑版风格
  143. }
  144. }
  145. if(in_wap()){
  146. $member_style=$this->webdb['member_wapstyle']?:'default'; //手机会员中心风格
  147. }else{
  148. if(IN_WAP===true){
  149. $member_style=$this->webdb['member_wapstyle']?:'default'; //手机会员中心风格
  150. }else{
  151. $member_style=$this->webdb['member_style']?:'default'; //电脑会员中心风格
  152. }
  153. }
  154. $admin_style=$this->webdb['admin_style']?:'default'; //后台风格
  155. config('template.index_style',$index_style);
  156. config('template.member_style',$member_style);
  157. config('template.admin_style',$admin_style);
  158. //print_r($index_style);exit;
  159. if(empty(request()->root())){
  160. request()->root('/index.php');
  161. }
  162. if(in_array(ENTRANCE,['index','member','admin'])){ //设置模板独立目录
  163. config('template.view_base',TEMPLATE_PATH.ENTRANCE.'_style/'.config('template.'.ENTRANCE.'_style').'/');
  164. config('template.'.ENTRANCE.'_style')!='default'&&config('template.default_view_base',TEMPLATE_PATH.ENTRANCE.'_style/default/');
  165. }
  166. if(ENTRANCE==='admin'){
  167. if($module==''){
  168. header('Location: '.url('admin/index'));
  169. exit;
  170. }elseif($module!='admin'){
  171. // 定义模块的后台目录名
  172. config('url_controller_layer','admin');
  173. // 定义模块的后台模板路径目录
  174. config('template.view_path',APP_PATH.$module.'/view/admin/');
  175. }else{
  176. config('template.view_path',APP_PATH.$module.'/view/');
  177. }
  178. }elseif(ENTRANCE==='index'){
  179. if($module==''||$module=='index'){
  180. $module||$module='index'; //省略跳转处理
  181. config('template.view_path',APP_PATH.$module.'/view/'.$index_style.'/');
  182. $index_style=='default'||config('template.default_view_path',APP_PATH.$module.'/view/default/');
  183. }else{
  184. if($this->webdb['hiden_index_php']){
  185. \think\Url::root('/'); //隐藏index.php
  186. }
  187. if(!modules_config($module)&&$module!='api'){
  188. //钩子可以方便扩展开发404跳转插件
  189. get_hook('cannot_find_module');
  190. hook_listen('cannot_find_module');
  191. if(is_dir(APP_PATH.$module)){
  192. showerr('当前频道已关闭!');
  193. }else{
  194. $array = @include(RUNTIME_PATH.'url_cfg.php');
  195. if ($array[$module]) { //解决二级目录路由冲突的问题
  196. header('location:'.url($array[$module]));
  197. exit;
  198. }
  199. showerr('当前频道不存在!!',404);
  200. }
  201. }
  202. // 定义模块的前台文件目录
  203. config('url_controller_layer','index');
  204. // 定义模块的前台模板路径
  205. config('template.view_path',APP_PATH.$module.'/view/index/'.$index_style.'/');
  206. $index_style=='default'||config('template.default_view_path',APP_PATH.$module.'/view/index/default/');
  207. }
  208. }elseif(ENTRANCE==='member'){
  209. if($module===''){
  210. header('Location: '.url('member/index'));
  211. exit;
  212. }elseif($module=='member'){
  213. config('template.view_path',APP_PATH.$module.'/view/'.$member_style.'/');
  214. $member_style=='default'||config('template.default_view_path',APP_PATH.$module.'/view/default/');
  215. }else{
  216. // 定义模块的会员中心目录名
  217. config('url_controller_layer','member');
  218. // 修改视图模板路径
  219. config('template.view_path',APP_PATH.$module.'/view/member/'.$member_style.'/');
  220. $member_style=='default'||config('template.default_view_path',APP_PATH.$module.'/view/member/default/');
  221. }
  222. }
  223. query("SET sql_mode=''");
  224. }
  225. }