PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/config/bootstrap.php

https://github.com/hashing/basercms
PHP | 310 lines | 243 code | 1 blank | 66 comment | 18 complexity | 4a32d70898f7c046b27b08f7b3e85d48 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * 起動スクリプト
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.config
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * Include files
  22. */
  23. require ROOT.DS.'baser'.DS.'config'.DS.'paths.php';
  24. require BASER.'basics.php';
  25. /* ConnectionManager ハック */
  26. // baserフォルダ内のデータソースも走査するようにした
  27. // TODO パスを追加をApp::build に移行したら明示的に読み込まなくてもよいかも
  28. App::import('Core', 'ConnectionManager', array('file'=>CAKE_CORE_INCLUDE_PATH.DS.'baser'.DS.'connection_manager.php'));
  29. App::import('Model', 'AppModel', array('file'=>CAKE_CORE_INCLUDE_PATH.DS.'baser'.DS.'models'.DS.'app_model.php'));
  30. App::import('Behavior', 'BcCache', array('file'=>CAKE_CORE_INCLUDE_PATH.DS.'baser'.DS.'models'.DS.'behaviors'.DS.'bc_cache.php'));
  31. App::import('Core', 'ClassRegistry');
  32. /**
  33. * Baserパス追加
  34. */
  35. $modelPaths[] = BASER_MODELS;
  36. $behaviorPaths[] = BASER_BEHAVIORS;
  37. $controllerPaths[] = BASER_CONTROLLERS;
  38. $componentPaths[] = BASER_COMPONENTS;
  39. $viewPaths[] = BASER_VIEWS;
  40. $viewPaths[] = WWW_ROOT;
  41. $helperPaths[] = BASER_HELPERS;
  42. $pluginPaths[] = BASER_PLUGINS;
  43. // Rewriteモジュールなしの場合、/index.php/css/style.css 等ではCSSファイルが読み込まれず、
  44. // $html->css / $javascript->link 等では、/app/webroot/css/style.css というURLが生成される。
  45. // 上記理由により以下のとおり変更
  46. // ・HelperのwebrootメソッドをRouter::urlでパス解決をするように変更し、/index.php/css/style.css というURLを生成させる。
  47. // ・走査URLをvendorsだけではなく、app/webroot内も追加
  48. $vendorPaths[] = WWW_ROOT;
  49. $vendorPaths[] = BASER_VENDORS;
  50. $localePaths[] = BASER_LOCALES;
  51. //$shellPaths[];
  52. /**
  53. * baserUrl取得
  54. */
  55. define('BC_BASE_URL', baseUrl());
  56. /**
  57. * vendors内の静的ファイルの読み込みの場合はスキップ
  58. */
  59. $uri = @$_SERVER['REQUEST_URI'];
  60. if (preg_match('/^'.preg_quote(BC_BASE_URL, '/').'css\//', $uri) ||
  61. preg_match('/^'.preg_quote(BC_BASE_URL, '/').'js\//', $uri) ||
  62. preg_match('/^'.preg_quote(BC_BASE_URL, '/').'img\//', $uri)) {
  63. $assets = array('js' , 'css', 'gif' , 'jpg' , 'png' );
  64. $ext = array_pop(explode('.', $uri));
  65. if(in_array($ext, $assets)){
  66. Configure::write('BcRequest.asset', true);
  67. return;
  68. }
  69. }
  70. /**
  71. * 配置パターン
  72. * Windows対策として、「\」を「/」へ変換してチェックする
  73. */
  74. if(!preg_match('/'.preg_quote(str_replace('\\', '/', docRoot()), '/').'/', ROOT)) {
  75. // CakePHP標準の配置
  76. define('BC_DEPLOY_PATTERN', 3);
  77. } elseif(ROOT.DS == WWW_ROOT) {
  78. // webrootをドキュメントルートにして、その中に app / baser / cake を配置
  79. define('BC_DEPLOY_PATTERN', 2);
  80. } else {
  81. // baserCMS配布時の配置
  82. define('BC_DEPLOY_PATTERN', 1);
  83. }
  84. /**
  85. * インストール状態
  86. */
  87. define('BC_INSTALLED', isInstalled());
  88. /**
  89. * 設定ファイル読み込み
  90. * install.php で設定している為、一旦読み込んで再設定
  91. */
  92. $baserSettings = array();
  93. $baserSettings['BcEnv'] = Configure::read('BcEnv');
  94. $baserSettings['BcApp'] = Configure::read('BcApp');
  95. if(Configure::load('baser')===false) {
  96. $config = array();
  97. include BASER_CONFIGS.'baser.php';
  98. Configure::write($config);
  99. }
  100. if(BC_INSTALLED && $baserSettings) {
  101. foreach ($baserSettings as $key1 => $settings) {
  102. if($settings) {
  103. foreach($settings as $key2 => $setting) {
  104. Configure::write($key1.'.'.$key2, $setting);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * tmpフォルダ確認
  111. */
  112. if(BC_INSTALLED) {
  113. checkTmpFolders();
  114. }
  115. /**
  116. * 文字コードの検出順を指定
  117. */
  118. mb_detect_order(Configure::read('BcEncode.detectOrder'));
  119. /**
  120. * セッションタイムアウト設定
  121. * core.php で設定された値よりも早い段階でログアウトしてしまうのを防止
  122. */
  123. if (function_exists('ini_set')) {
  124. $sessionTimeouts = array('high'=>10,'medium'=>100,'low'=>300);
  125. $securityLevel = Configure::read('Security.level');
  126. if (isset($sessionTimeouts[$securityLevel])) {
  127. $sessionTimeout = $sessionTimeouts[$securityLevel] * Configure::read('Session.timeout');
  128. ini_set('session.gc_maxlifetime', $sessionTimeout);
  129. } else {
  130. trigger_error('Security.level の設定が間違っています。', E_USER_WARNING);
  131. }
  132. }
  133. /**
  134. * パラメーター取得
  135. */
  136. $url = getUrlFromEnv(); // 環境変数からパラメータを取得
  137. $parameter = getUrlParamFromEnv();
  138. Configure::write('BcRequest.pureUrl',$parameter); // ※ requestActionに対応する為、routes.php で上書きされる
  139. /**
  140. * セッションスタート
  141. */
  142. App::import('Core','Session');
  143. $Session = new CakeSession();
  144. $Session->start();
  145. /**
  146. * パラメーター取得
  147. * モバイル判定・簡易リダイレクト
  148. */
  149. $agentSettings = Configure::read('BcAgent');
  150. if(!Configure::read('BcApp.mobile')) {
  151. unset($agentSettings['mobile']);
  152. }
  153. if(!Configure::read('BcApp.smartphone')) {
  154. unset($agentSettings['smartphone']);
  155. }
  156. $agentOn = false;
  157. if($agentSettings) {
  158. foreach($agentSettings as $key => $setting) {
  159. $agentOn = false;
  160. if(!empty($url)) {
  161. $parameters = explode('/',$url);
  162. if($parameters[0] == $setting['alias']) {
  163. $agentOn = true;
  164. }
  165. }
  166. if(!$agentOn && $setting['autoRedirect']) {
  167. $agentAgents = $setting['agents'];
  168. $agentAgents = implode('||', $agentAgents);
  169. $agentAgents = preg_quote($agentAgents, '/');
  170. $regex = '/'.str_replace('\|\|', '|', $agentAgents).'/i';
  171. if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) {
  172. $getParams = str_replace(BC_BASE_URL.$parameter, '', $_SERVER['REQUEST_URI']);
  173. if($getParams == '/' || '/index.php') {
  174. $getParams = '';
  175. }
  176. $redirect = true;
  177. // URLによる AUTO REDIRECT 設定
  178. if(isset($_GET[$setting['prefix'].'_auto_redirect'])) {
  179. if($_GET[$setting['prefix'].'_auto_redirect'] == 'on') {
  180. $_SESSION[$setting['prefix'].'_auto_redirect'] = 'on';
  181. } elseif($_GET[$setting['prefix'].'_auto_redirect'] == 'off') {
  182. $_SESSION[$setting['prefix'].'_auto_redirect'] = 'off';
  183. }
  184. }
  185. if(isset($_SESSION[$setting['prefix'].'_auto_redirect'])) {
  186. if($_SESSION[$setting['prefix'].'_auto_redirect'] == 'off') {
  187. $redirect = false;
  188. }
  189. }
  190. if(isset($_GET[$setting['prefix']])) {
  191. if($_GET[$setting['prefix']] == 'on') {
  192. $redirect = true;
  193. } elseif($_GET[$setting['prefix']] == 'off') {
  194. $redirect = false;
  195. }
  196. }
  197. if($redirect) {
  198. $redirectUrl = FULL_BASE_URL.BC_BASE_URL.$setting['alias'].'/'.$parameter.$getParams;
  199. header("HTTP/1.1 301 Moved Permanently");
  200. header("Location: ".$redirectUrl);
  201. exit();
  202. }
  203. }
  204. }
  205. if($agentOn) {
  206. Configure::write('BcRequest.agent', $key);
  207. Configure::write('BcRequest.agentPrefix', $setting['prefix']);
  208. Configure::write('BcRequest.agentAlias', $setting['alias']);
  209. break;
  210. }
  211. }
  212. }
  213. if($agentOn) {
  214. //======================================================================
  215. // /m/files/... へのアクセスの場合、/files/... へ自動リダイレクト
  216. // CMSで作成するページ内のリンクは、モバイルでアクセスすると、
  217. // 自動的に、/m/ 付のリンクに書き換えられてしまう為、
  218. // files内のファイルへのリンクがリンク切れになってしまうので暫定対策。
  219. //======================================================================
  220. $_parameter = preg_replace('/^'.Configure::read('BcRequest.agentAlias').'\//', '', $parameter);
  221. if(preg_match('/^files/', $_parameter)) {
  222. $redirectUrl = FULL_BASE_URL.'/'.$_parameter;
  223. header("HTTP/1.1 301 Moved Permanently");
  224. header("Location: ".$redirectUrl);
  225. exit();
  226. }
  227. }
  228. /**
  229. * Viewのキャッシュ設定
  230. */
  231. if(Configure::read('debug') > 0) {
  232. Configure::write('Cache.check', false);
  233. clearViewCache();
  234. }else {
  235. if(Configure::read('Session.start')) {
  236. // 管理ユーザーでログインしている場合、ページ機能の編集ページへのリンクを表示する為、キャッシュをオフにする。
  237. // ただし、現在の仕様としては、セッションでチェックしているので、ブラウザを閉じてしまった場合、一度管理画面を表示する必要がある。
  238. // TODO ブラウザを閉じても最初から編集ページへのリンクを表示する場合は、クッキーのチェックを行い、認証処理を行う必要があるが、
  239. // セキュリティ上の問題もあるので実装は検討が必要。
  240. // bootstrapで実装した場合、他ページへの負荷の問題もある
  241. if(isset($_SESSION['Auth']['User'])) {
  242. Configure::write('Cache.check', false);
  243. }
  244. }
  245. }
  246. if(BC_INSTALLED) {
  247. /**
  248. * データキャッシュ
  249. */
  250. Cache::config('_cake_data_', array(
  251. 'engine' => 'File',
  252. 'duration' => Configure::read('BcCache.dataCachetime'),
  253. 'probability' => 100,
  254. 'path' => CACHE.'datas',
  255. 'prefix' => 'cake_',
  256. 'lock' => false,
  257. 'serialize' => true
  258. ));
  259. /**
  260. * 環境情報キャッシュ
  261. */
  262. Cache::config('_cake_env_', array(
  263. 'engine' => 'File',
  264. 'duration' => Configure::read('BcCache.defaultCachetime'),
  265. 'probability' => 100,
  266. 'path' => CACHE.'environment',
  267. 'prefix' => 'cake_',
  268. 'lock' => false,
  269. 'serialize' => true
  270. ));
  271. /**
  272. * サイト基本設定を読み込む
  273. */
  274. loadSiteConfig();
  275. /**
  276. * テーマヘルパーのパスを追加する
  277. */
  278. $themePath = WWW_ROOT.'themed'.DS.Configure::read('BcSite.theme').DS;
  279. $helperPaths[] = $themePath.'helpers';
  280. /**
  281. * アップデート
  282. */
  283. if($parameter == 'maintenance/index') {
  284. Configure::write('BcRequest.isMaintenance', true);
  285. } else {
  286. Configure::write('BcRequest.isMaintenance', false);
  287. }
  288. $isUpdater = false;
  289. $bcSite = Configure::read('BcSite');
  290. if(preg_match('/^updaters(|\/index\/)/', $parameter)) {
  291. $isUpdater = true;
  292. }elseif(BC_INSTALLED && !Configure::read('BcRequest.isMaintenance') && (!empty($bcSite['version']) && (getVersion() > $bcSite['version']))) {
  293. if(preg_match('/^admin/', $parameter)) {
  294. sendUpdateMail();
  295. $message = 'baserCMSのアップデートURLを管理者メールアドレスに送信しました。<br /><br />メールが届かない場合は、管理者メールアドレスの設定がうまくいっていない可能性があります。<br />'.
  296. 'baserCMSのバージョンを前のバージョンに戻してシステム設定よりメール設定を行うか、<br />データベースの site_configs テーブルでメール設定を直接調整し、管理システムのURLへ再度アクセスしてください。';
  297. $layout = 'default';
  298. $Session->write('Message.flash', compact('message', 'layout'));
  299. }
  300. header('Location: '.topLevelUrl(false).baseUrl().'maintenance/index');exit();
  301. }
  302. Configure::write('BcRequest.isUpdater', $isUpdater);
  303. }
  304. ?>