OSDN Git Service

1acba2ff592148e143a8a3860c3ecd0c20aab99e
[magic3/magic3.git] / include / container / baseFrameContainer.php
1 <?php
2 /**
3  * フレームコンテナ作成用ベースクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2018 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id$
14  * @link       http://www.magic3.org
15  */
16 require_once(M3_SYSTEM_INCLUDE_PATH . '/db/systemDb.php');              // システムDBアクセスクラス
17 require_once(M3_SYSTEM_INCLUDE_PATH . '/common/core.php');
18
19 class BaseFrameContainer extends Core
20 {
21         protected $_db; // DB接続オブジェクト
22         private $joomlaBufArray = array();                      // Joomla!データ受け渡し用
23         private $templateCustomObj;                             // テンプレートカスタマイズパラメータオブジェクト
24         const SYSTEM_TEMPLATE = '_system';              // システム画面用テンプレート
25         const M_ADMIN_TEMPLATE = 'm/_admin';    // 携帯用管理画面テンプレート
26         const ERR_MESSAGE_ACCESS_DENY = 'Access denied.';               // ウィジェットアクセスエラーのメッセージ
27         const SITE_ACCESS_EXCEPTION_IP = 'site_access_exception_ip';            // アクセス制御、例外とするIP
28         const CONFIG_KEY_MSG_TEMPLATE = 'msg_template';                 // メッセージ用テンプレート取得キー
29 //      const CF_MOBILE_AUTO_REDIRECT = 'mobile_auto_redirect';         // 携帯の自動遷移
30         const TEMPLATE_GENERATOR_THEMLER = 'themler';                   // テンプレート作成アプリケーション(Themler)
31                 
32         /**
33          * コンストラクタ
34          */
35         function __construct()
36         {
37                 // 親クラスを呼び出す
38                 parent::__construct();
39                 
40                 // DBオブジェクト取得
41                 $this->_db = $this->gInstance->getSytemDbObject();
42         }
43         /**
44          * 起動マネージャから呼ばれる唯一のメソッド
45          *
46          * @param RequestManager $request               HTTPリクエスト処理クラス
47          */
48         function process($request)
49         {
50                 // パラメータを取得
51                 $cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);               // 実行コマンドを取得
52                                 
53                 // インストール画面への制御は、install.phpファイルの作成、削除で制御する
54                 // 最小限の設定が行われていない場合,DBに接続できない場合は、インストール画面へ
55                 if (!defined('M3_STATE_IN_INSTALL')){
56                         if (($this->gEnv->canUseDb() && $this->gSystem->canInitSystem()) ||             // システム初期化モードのとき
57                                 !$this->gConfig->isConfigured()){                                                                       // 設定ファイルに設定がないとき(初回インストール)
58                                 
59                                 // 旧システムがある場合はadminディレクトリまでアクセスしてインストーラを実行。
60                                 $isRedirect = false;
61                                 if ($this->_isExistsOldSystemDir()){            // ディレクトリアクセス権がない場合も旧システムが存在するものとしてリダイレクトを行う
62                                         if ($this->gEnv->isAdminDirAccess()) $isRedirect = true;
63                                 } else {
64                                         $isRedirect = true;
65                                 }
66                                 
67                                 if ($isRedirect){
68                                         // インストーラファイルがない場合は回復
69                                         $this->gInstance->getFileManager()->recoverInstaller();
70
71                                         $this->gPage->redirectToInstall();
72                                 } else {
73                                         // サイト非公開(システムメンテナンス)表示
74                                         $this->gPage->showError(503);
75                                 }
76                                 return;
77                         } else if ($this->gConfig->isConfigured() && !$this->gEnv->canUseDb()){         // DB接続失敗のとき
78                                 if ($this->gEnv->isAdminDirAccess()){           // 管理画面の場合のみインストーラ起動
79                                         // インストーラファイルがない場合は回復
80                                         $this->gInstance->getFileManager()->recoverInstaller();
81
82                                         $this->gPage->redirectToInstall();
83                                 } else {
84                                         // サーバ内部エラーメッセージ表示
85                                         $this->gPage->showError(500);
86                                 }
87                                 return;
88                         }
89                 }
90                 
91                 // 開始ログ出力
92                 //$this->gLog->info(__METHOD__, 'フレーム作成開始');
93
94                 // ページ作成開始
95                 // セッション変数読み込み。サブページIDの設定。
96                 $this->gPage->startPage($request);
97                 
98                 // パラメータ取得
99                 $isSystemAdmin = $this->gEnv->isSystemAdmin();          // 管理者権限があるかどうか
100                 $isSystemManageUser = $this->gEnv->isSystemManageUser();                // システム運用可能かどうか
101                         
102                 if (!defined('M3_STATE_IN_INSTALL')){           // インストールモード以外のとき
103                         // ############## ユーザごとの設定の読み込み ###################
104                         // 引数での言語設定取得、言語変更可能な場合は変更
105                         // 言語の優先順は、URLの言語設定、クッキーの言語設定の順
106                         if (!$this->gEnv->isAdminDirAccess()){          // 管理画面以外の場合
107                                 if ($this->gEnv->getCanChangeLang() && $this->gEnv->isMultiLanguageSite()){     // 言語変更可能で多言語対応サイトのとき
108                                         $lang = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_LANG);
109                                         if (empty($lang)){
110                                                 // 空の場合はクッキーから言語値を取得
111                                                 $lang = $request->getCookieValue(M3_COOKIE_LANG);
112                                         }
113
114                                         // アクセス可能な言語な場合は変更
115                                         if (in_array($lang, $this->gSystem->getAcceptLanguage())){
116                                                 $this->gEnv->setCurrentLanguage($lang);
117                                                 
118                                                 // クッキーに言語を保存
119                                                 $request->setCookieValue(M3_COOKIE_LANG, $lang);
120                                         } else {                // アクセス不可の場合はクッキーを削除
121                                                 // クッキーを削除
122                                                 $request->setCookieValue(M3_COOKIE_LANG, '', -1);
123                                         }
124                                 } else {
125                                         // クッキーを削除
126                                         $request->setCookieValue(M3_COOKIE_LANG, '', -1);
127                                 }
128                                 // 言語に依存する情報を取り込む
129                                 $this->gPage->loadLang();
130                         }
131                         // ################### URLアクセス制御 ######################
132                         // 非公開URLへは管理権限がないとアクセスできない
133                         $canAccess = true;              // アクセス可能かどうか
134                         $isErrorAccess = false;         // 不正アクセスかどうか
135                         $toAdminType = 0;               // 管理画面の遷移タイプ(0=アクセス不可、1=ログイン画面、2=サイト非公開画面, 3=存在しないページ)
136                         $errMessage = '';       // エラーメッセージ
137                         $messageDetail = '';    // 詳細メッセージ
138                         
139                         // ページID,ページサブID以外のURLパラメータをチェック。ページマネージャーでの処理(startPage())の結果を反映。
140                         $ret = $this->gPage->isSystemPage();            // システム制御ページへ遷移するかどうか
141                         if ($ret){
142                                 // ページが見つかりません画面へ遷移
143                                 $canAccess = false;
144                                 $toAdminType = 4;
145                         }
146                         
147                         // ページID,ページサブIDからアクセス権をチェック
148                         if ($canAccess){
149                                 $isPublicUrl = $this->gPage->canAccessUrl($isActivePage, $errCode);
150                                 if (!$isPublicUrl && !$isSystemManageUser){// システム運用可能ユーザかどうか
151                                         $canAccess = false;
152                                         $isErrorAccess = true;          // 不正アクセスかどうか
153                                         $errMessage = 'ユーザに公開されていないページへのアクセス。';
154                                 
155                                         if (!$isActivePage){
156                                                 if ($errCode == 1){                     // ページIDが不正な場合
157                                                         $toAdminType = 4;
158                                                 } else {
159                                                         $toAdminType = 3;               // 有効なアクセスポイント、ページでない場合は存在しないページとする
160                                                 }
161                                         }
162                                 }
163                         }
164                         
165                         // ################### ユーザアクセス制御 ######################
166                         // クッキーがないため権限を識別できない場合でも、管理者として処理する場合があるので、サブクラスの_checkAccess()メソッドは必ず通るようにする
167                         if ($canAccess){                // アクセス可能な場合はユーザをチェック
168                                 if (method_exists($this, '_checkAccess')){
169                                         // 管理画面へのアクセスを制御
170                                         $canAccess = $this->_checkAccess($request);             // サブクラスメソッドの呼び出し
171                                         
172                                         // フロント画面から直接管理画面が呼ばれた場合は一旦ログインへ遷移
173                                         if (!$canAccess && 
174                                                 ($cmd == M3_REQUEST_CMD_CONFIG_WIDGET ||                                                // ウィジェットの設定
175                                                 $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET)){                     // 表示位置を表示するとき(ウィジェット付き)
176                                                 // 管理画面で処理
177                                                 $toAdminType = 1;               // ログイン画面へ
178                                         }
179                                 } else {                        // _checkAccess()がないときは、標準のアクセス制御
180                                         // フロント画面へのアクセスを制御
181                                         $canAccess = $this->_accessSite($request);              // サイト公開制御
182                                         if ($canAccess){
183                                                 if ($cmd == M3_REQUEST_CMD_LOGIN ||                                     // ログイン画面を表示のとき
184                                                         $cmd == M3_REQUEST_CMD_LOGOUT){                         // ログアウトのとき
185
186                                                         // 管理画面で処理
187                                                         $canAccess = false;
188                                                         $toAdminType = 1;               // ログイン画面へ
189                                                 } else if ($cmd != '' &&                                                                // コマンドなし
190                                                         $cmd != M3_REQUEST_CMD_CHANGE_TEMPLATE &&                       // テンプレート変更
191                                                         $cmd != M3_REQUEST_CMD_SHOW_POSITION &&                         // 表示位置を表示するとき
192                                                         $cmd != M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET &&     // 表示位置を表示するとき(ウィジェット付き)
193                                                         $cmd != M3_REQUEST_CMD_FIND_WIDGET &&                           // ウィジェットを検索
194                                                         $cmd != M3_REQUEST_CMD_DO_WIDGET &&                                     // ウィジェット単体実行
195                                                         $cmd != M3_REQUEST_CMD_PREVIEW &&                                       // サイトのプレビューを表示
196                                                         $cmd != M3_REQUEST_CMD_RSS &&                                           // RSS配信
197                                                         $cmd != M3_REQUEST_CMD_CSS){                                            // CSS生成
198                                                         
199                                                         // 標準のアクセスでは、上記コマンド以外は受け付けない
200                                                         $canAccess = false;
201                                                         $isErrorAccess = true;          // 不正アクセス
202                                                         $errMessage = '不正なコマンドの実行。';
203                                                         $messageDetail = 'アクセスポイント状態=公開';
204                                                 }
205                                         } else {                // サイトアクセスできない場合は、管理画面でメッセージを表示
206                                                 if ($cmd == M3_REQUEST_CMD_LOGIN ||                                     // ログイン画面を表示のとき
207                                                         $cmd == M3_REQUEST_CMD_LOGOUT ||                                // ログアウトのとき
208                                                         $cmd == M3_REQUEST_CMD_PREVIEW){                                        // サイトのプレビューを表示
209                                                         $toAdminType = 1;               // ログイン画面へ
210                                                 } else {
211                                                         $toAdminType = 2;               // サイト非公開画面へ
212                                                 }
213                                                 
214                                                 // 不正なコマンドはログを残す
215                                                 if ($cmd != '' &&                                                               // コマンドなし
216                                                         $cmd != M3_REQUEST_CMD_LOGIN &&                         // ログイン画面を表示のとき
217                                                         $cmd != M3_REQUEST_CMD_LOGOUT &&                                        // ログアウトのとき
218                                                         $cmd != M3_REQUEST_CMD_CHANGE_TEMPLATE &&                       // テンプレート変更
219                                                         $cmd != M3_REQUEST_CMD_SHOW_POSITION &&                         // 表示位置を表示するとき
220                                                         $cmd != M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET &&     // 表示位置を表示するとき(ウィジェット付き)
221                                                         $cmd != M3_REQUEST_CMD_FIND_WIDGET &&                           // ウィジェットを検索
222                                                         $cmd != M3_REQUEST_CMD_DO_WIDGET &&                                     // ウィジェット単体実行
223                                                         $cmd != M3_REQUEST_CMD_PREVIEW &&                                       // サイトのプレビューを表示
224                                                         $cmd != M3_REQUEST_CMD_RSS &&                                           // RSS配信
225                                                         $cmd != M3_REQUEST_CMD_CSS){                                            // CSS生成
226                                                         
227                                                         $isErrorAccess = true;          // 不正アクセス
228                                                         $errMessage = '不正なコマンドの実行。';
229                                                         $messageDetail = 'アクセスポイント状態=非公開';
230                                                 }
231                                         }
232                                 }
233                                 // システム運用可能ユーザはアクセス可。
234                                 // ログアウトのときはすでに管理ユーザの可能性があるので、ログアウト時は変更しない
235                                 //if ($isSystemManageUser && $cmd != M3_REQUEST_CMD_LOGOUT) $canAccess = true;
236                                 if ($isSystemAdmin && $cmd != M3_REQUEST_CMD_LOGOUT) $canAccess = true;                 // 2011/8/31 システム管理者のみに変更
237                         }
238                         // #################### アクセスログ記録 #######################
239                         // DBが使用可能であれば、ログイン処理終了後、アクセスログを残す
240                         if ($this->gEnv->canUseDb()) $this->gAccess->accessLog();
241
242                         // アクセス不可のときはここで終了
243                         if (!$canAccess){
244                                 switch ($toAdminType){
245                                         case 1:                 // ログイン画面へ
246                                                 // システム制御モードに変更
247                                                 $this->gPage->setSystemHandleMode(1/*管理画面*/);
248                                                 break;
249                                         case 2:                 // サイト非公開画面へ
250                                                 $this->gPage->setSystemHandleMode(10/*サイト非公開中*/);
251                                                 break;
252                                         case 3:                 // 存在しないページ画面へ(システム運用可能ユーザ以外)
253                                                 // サイトが非公開の場合は、メンテナンス中画面のみ表示
254                                                 if ($this->_accessSite($request)){              // サイト公開中の場合
255                                                         $messageDetail = 'アクセスポイント状態=公開';
256                                                         $this->gPage->setSystemHandleMode(12/*存在しないページ*/);
257                                                 } else {
258                                                         $messageDetail = 'アクセスポイント状態=非公開';
259                                                         $this->gPage->setSystemHandleMode(10/*サイト非公開中*/);
260                                                 }
261                                                 break;
262                                         case 4:         // 不正なページIDの指定
263                                                 $messageDetail = '不正なページIDの指定';
264                                                 $this->gPage->setSystemHandleMode(12/*存在しないページ*/);
265                                                 break;
266                                         default:                // アクセス不可画面へ
267                                                 // システム制御モードに変更
268                                                 $this->gPage->setSystemHandleMode(11/*アクセス不可*/);
269                                                 break;
270                                 }
271                                 // システム制御画面を表示
272                                 $this->_showSystemPage($request, $toAdminType);
273                                                 
274                                 // 不正アクセスの場合は、アクセスエラーログを残す
275                                 if ($isErrorAccess) $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2201, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
276                                 return;
277                         }
278                         // #################### URLの遷移 #######################
279                         //if ($this->gSystem->getSystemConfig(self::CF_MOBILE_AUTO_REDIRECT)){          // 携帯自動遷移を行う場合
280                         if ($this->gSystem->mobileAutoRedirect()){              // 携帯自動遷移を行う場合
281                                 // 携帯のときは携帯用URLへ遷移
282                                 if ($this->gEnv->isMobile() && !$this->gEnv->getIsMobileSite()){
283                                         $this->gPage->redirect($this->gEnv->getDefaultMobileUrl(true/*携帯用パラメータ付加*/), true/*遷移時のダイアログ表示を抑止*/);
284                                         return;
285                                 }
286                         }
287                         if ($this->gSystem->smartphoneAutoRedirect()){          // スマートフォン自動遷移を行う場合
288                                 // スマートフォンのときはスマートフォンURLへ遷移
289                                 if ($this->gEnv->isSmartphone() && !$this->gEnv->getIsSmartphoneSite()){
290                                         $this->gPage->redirect($this->gEnv->getDefaultSmartphoneUrl());
291                                         return;
292                                 }
293                         }
294                 }
295
296                 // ################## 実行コマンドから処理を確定 ##################
297                 $openBy = $request->trimValueOf(M3_REQUEST_PARAM_OPEN_BY);              // ウィンドウオープンタイプ
298                 
299                 // 画面作成モードか、ウィジェット単体処理モードかを決定
300                 $createPage = true;             // 画面作成モード
301                 if ($cmd == M3_REQUEST_CMD_INIT_DB){    // DB初期化オペレーションのとき
302                 } else if ($cmd == M3_REQUEST_CMD_SHOW_POSITION){               // 表示位置を表示するとき
303                         // 管理者権限がある場合のみ実行可能
304                         //if ($isSystemAdmin){
305                         if ($isSystemManageUser){                       // システム運用者で表示可能(2018/8/5変更)
306                                 // ポジションの表示画面のアクセスは、すべて管理機能URLで受け付ける
307                                 // ページIDを再設定
308 /*                              $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
309                                 if (empty($pageId)) $pageId = $this->gEnv->getDefaultPageId();          // 値がないときはデフォルトのページIDを設定
310                                 $this->gEnv->setCurrentPageId($pageId);
311                                 $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
312                                 if (!empty($pageSubId)) $this->gEnv->setCurrentPageSubId($pageSubId);
313                         */
314                                 $this->gPage->showPosition(1);                  // ポジションを表示
315                         } else {
316                                 return;
317                         }
318                 } else if ($cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET){           // 表示位置を表示するとき(ウィジェット付き)
319                         // 管理者権限がある場合のみ実行可能
320 //                      if ($isSystemAdmin){
321                         if ($isSystemManageUser){                       // システム運用者で表示可能(2018/8/5変更)
322                                 // ポジションの表示画面のアクセスは、すべて管理機能URLで受け付ける
323                                 // ページIDを再設定
324 /*                              $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
325                                 if (empty($pageId)) $pageId = $this->gEnv->getDefaultPageId();          // 値がないときはデフォルトのページIDを設定
326                                 $this->gEnv->setCurrentPageId($pageId);
327                                 $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
328                                 if (!empty($pageSubId)) $this->gEnv->setCurrentPageSubId($pageSubId);
329                                 */
330                                 $this->gPage->showPosition(2);                  // ウィジェット付きポジションを表示
331                         } else {
332                                 return;
333                         }
334                 } else if ($cmd == M3_REQUEST_CMD_GET_WIDGET_INFO){             // ウィジェット各種情報取得(AJAX用)
335                         // ウィジェット情報取得
336                         $this->gPage->getWidgetInfoByAjax($request);
337                         return;
338                 } else if ($cmd == M3_REQUEST_CMD_SHOW_PHPINFO){        // phpinfoの表示
339                         // phpinfo画面を表示
340                         $this->_showPhpinfoPage($request);
341                         return;
342                 } else if ($cmd == M3_REQUEST_CMD_FIND_WIDGET){         // ウィジェットを検索し、前面表示
343                         // 目的のウィジェットのあるページサブIDへ遷移
344                         $this->gPage->redirectToUpdatedPageSubId($request);
345                         return;
346                 } else if ($cmd == M3_REQUEST_CMD_SHOW_WIDGET){         // ウィジェットの単体表示
347                         $createPage = false;            // ウィジェット単体処理モードに設定
348                         $this->gPage->showWidget();     // ウィジェット表示
349                 } else if ($cmd == M3_REQUEST_CMD_CONFIG_WIDGET){               // ウィジェットの設定管理
350                         $createPage = false;            // ウィジェット単体処理モードに設定
351                         $this->gPage->showWidget();     // ウィジェット表示
352                 } else if ($cmd == M3_REQUEST_CMD_DO_WIDGET){           // ウィジェット単体オペレーション
353                         $createPage = false;            // ウィジェット単体処理モードに設定
354                         
355                         // ウィンドウオープンタイプ指定のときは、テンプレートを表示する
356                         if (!empty($openBy)) $this->gPage->showWidget();        // ウィジェット表示
357                 } else if ($cmd == M3_REQUEST_CMD_RSS){         // RSS配信
358                         $createPage = false;            // ウィジェット単体処理モードに設定
359                 } else if ($cmd == M3_REQUEST_CMD_CSS){         // CSS生成
360                 
361                 } else if ($cmd == M3_REQUEST_CMD_CONFIG_TEMPLATE){             // テンプレートの設定
362                         $createPage = false;            // ウィジェット単体処理モードに設定
363                         $this->gPage->showWidget();     // ウィジェット表示
364                 } else if ($this->gEnv->isServerConnector()){           // サーバ接続の場合
365                         $createPage = false;            // ウィジェット単体処理モードに設定
366                 }
367
368                 // ################### クライアントへの出力方法の制御 ######################
369                 // ウィジェットIDの取得
370                 $widgetId = $request->trimValueOf(M3_REQUEST_PARAM_WIDGET_ID);
371                 if ($createPage){                               // 通常の画面作成の場合
372                         // 画面のキャッシュデータを取得
373                         $this->gCache->initCache($request);             // キャッシュ機能初期化
374                         $cacheData = $this->gCache->getPageCache($request);
375                         
376                         if (empty($cacheData)){         // キャッシュデータがないときは画面を作成
377                                 // カレントのテンプレートを決定
378                                 $curTemplateId = $this->_defineTemplate($request, $subTemplateId);
379
380                                 // 画面を作成
381                                 $pageData = $this->_createPage($request, $curTemplateId, $subTemplateId);
382                                 
383                                 // ##### 非共通ウィジェットがページ上になくてもエラーとしない #####
384                                 $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
385                                 echo $pageData;
386                                         
387 /*                              // 使用した非共通ウィジェットの数をチェック
388                                 $nonSharedWidgetCount = $this->gPage->getNonSharedWidgetCount();
389                                 if ($nonSharedWidgetCount == -1){               // カウントなしの場合
390                                         $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
391                                         echo $pageData;
392                                 } else {
393                                         // ***** WordPressテンプレートの場合は非共通のウィジェットが使用されていなくても表示可とする *****
394                                         if ($this->gEnv->getCurrentTemplateType() == 100) $nonSharedWidgetCount = 1;
395                                         
396                                         if ($isSystemAdmin || $nonSharedWidgetCount > 0){
397                                                 $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
398                                                 echo $pageData;
399                                         } else {                // 管理者以外で、非共通のウィジェットが使用されていないページはアクセス不可とする
400                                                 $errMessage = 'ユーザに公開されていないページへのアクセス。';
401                                                 $messageDetail = 'アクセスポイント状態=公開, 要因: グローバルウィジェットのみのページへのアクセスはできません。ページには1つ以上のローカルウィジェットが必要です。';
402                                                 $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2202, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
403
404                                                 // アクセス不可ページへ遷移
405                                                 $this->gPage->redirect('?' . M3_REQUEST_PARAM_PAGE_SUB_ID . '=_accessdeny');
406                                         }
407                                 }*/
408                         } else {
409                                 echo $cacheData;
410                         }
411                         
412                         if ($cmd != M3_REQUEST_CMD_CSS){                // 画面出力(CSS生成以外)のとき
413                                 // オプション出力(時間計測等)追加
414                                 echo $this->gPage->getOptionContents($request);
415                         }
416                 } else {                // ウィジェット単体実行モードのとき
417                         // ###################ウィジェット指定で出力の場合####################
418                         // ウィジェット単体を直接実行するインターフェイスで、HTTPヘッダは送信しない。
419                         // 以下のパターンで使用する。
420                         // ・Ajaxを使って、データをやり取りしたい場合
421                         // ・ウィジェット単体での実行(ウィジェットが生成したタグのみ)
422                         // ・ウィジェット単体での実行(HTMLやJavascriptの追加あり)
423                         // ・ウィジェット個別の設定(セキュリティの必要あり)
424
425                         // ################# アクセスチェック ################
426                         // ウィジェット単体オペレーションのときは、ウィジェット情報の単体実行許可があるかどうか判断(管理権限にかかわらず同じ動作)
427                         if ($cmd == M3_REQUEST_CMD_DO_WIDGET ||         // ウィジェット単体実行
428                                 $cmd == M3_REQUEST_CMD_RSS){            // RSS配信
429                                 if (empty($widgetId)){
430                                         $this->gOpeLog->writeUserAccess(__METHOD__, 'ウィジェットIDが設定されていません。', 2200,
431                                                 '実行処理はキャンセルされました。');
432                                         return;
433                                 } else if ($this->_db->getWidgetInfo($widgetId, $row)){                 // ウィジェット情報取得
434                                         if ($cmd == M3_REQUEST_CMD_DO_WIDGET && !$row['wd_enable_operation']){  // ウィジェット単体実行
435                                                 // アクセスエラーのログを残す
436                                                 $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd, self::ERR_MESSAGE_ACCESS_DENY);
437                                                 
438                                                 $this->gOpeLog->writeUserAccess(__METHOD__, 'このウィジェットは単体起動できません。(ウィジェットID: ' . $widgetId . ')', 2200,
439                                                 '実行処理はキャンセルされました。このウィジェットは単体起動できないウィジェットです。単体起動を許可するにはウィジェット情報(_widgets)の単体起動フラグ(wd_enable_operation)がtrueになっている必要があります。');
440                                                 return;
441                                         } else if ($cmd == M3_REQUEST_CMD_RSS && !$row['wd_has_rss']){                          // RSS配信
442                                                 // アクセスエラーのログを残す
443                                                 $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd, self::ERR_MESSAGE_ACCESS_DENY);
444                                                 
445                                                 $this->gOpeLog->writeUserAccess(__METHOD__, 'このウィジェットはRSS配信できません。(ウィジェットID: ' . $widgetId . ')', 2200,
446                                                 '実行処理はキャンセルされました。このウィジェットはRSS配信できないウィジェットです。RSS配信を許可するにはウィジェット情報(_widgets)のRSS配信フラグ(wd_has_rss)がtrueになっている必要があります。');
447                                                 return;
448                                         }
449                                 } else {
450                                         $this->gOpeLog->writeUserAccess(__METHOD__, 'このウィジェットは実行許可がありません。(ウィジェットID: ' . $widgetId . ')', 2200,
451                                                 '実行処理はキャンセルされました。ウィジェット情報(_widgets)が見つかりません。');
452                                         return;
453                                 }
454                         }
455                         
456                         // 管理権限がない場合は、ウィジェットのページへの配置状況からアクセス権限をチェックする
457                         if (!$isSystemManageUser && !$this->_db->canAccessWidget($widgetId)){
458                                 // アクセスエラーのログを残す
459                                 $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd, self::ERR_MESSAGE_ACCESS_DENY);
460                                 
461                                 $this->gOpeLog->writeUserAccess(__METHOD__, 'ウィジェットへの不正なアクセスを検出しました。(ウィジェットID: ' . $widgetId . ')', 2200,
462                                                 '実行処理はキャンセルされました。このウィジェットは一般ユーザに公開されているページ上に存在しないため単体実行できません。');
463                                 return;
464                         }
465                         
466                         // ################# パラメータチェック ################
467                         if (!$isSystemManageUser && $this->gEnv->isServerConnector()){          // サーバ接続の場合
468                                 // クエリーパラメータはウィジェットIDのみ正常とする
469                                 $params = $request->getQueryArray();
470                                 $paramCount = count($params);
471                                 if (!($paramCount == 1 && !empty($params[M3_REQUEST_PARAM_WIDGET_ID]))){
472                                         // アクセスエラーのログを残す
473                                         $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd, self::ERR_MESSAGE_ACCESS_DENY);
474                                 
475                                         $this->gOpeLog->writeUserAccess(__METHOD__, 'サーバ接続アクセスポイントへの不正なアクセスを検出しました。', 2200,
476                                                         '実行処理はキャンセルされました。URLのクエリー部が不正です。URL=' . $this->gEnv->getCurrentRequestUri());
477                                         return;
478                                 }
479                         }
480
481                         // 画面表示する場合はテンプレートを設定。画面に表示しない場合はテンプレートが必要ない。
482                         if ($this->gPage->getShowWidget()){
483                                 // 管理用テンプレートに固定
484                                 //$curTemplate = $this->_defineTemplate($request);
485                                 $curTemplate = $this->gSystem->defaultAdminTemplateId();
486
487                                 // カレントのテンプレートIDを設定
488                                 $this->gEnv->setCurrentTemplateId($curTemplate);
489                         }
490                         
491                         // ################### バッファリング開始 ######################
492                         // ob_end_flush()までの出力をバッファリングする
493                         ob_start();
494                         
495                         // サブクラスの前処理を実行
496                         if (method_exists($this, '_preBuffer')) $this->_preBuffer($request);
497                 
498                         // 作業中のウィジェットIDを設定
499                         $this->gEnv->setCurrentWidgetId($widgetId);
500                         
501                         if ($this->gEnv->isServerConnector()){          // サーバ接続の場合
502                                 // ウィジェット用のHTMLヘッダを出力
503                                 $this->gPage->startWidgetXml($cmd);
504
505                                 // 指定のウィジェットを実行
506                                 $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
507
508                                 if (file_exists($widgetIndexFile)){
509                                         // 実行のログを残す
510                                         $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd);
511
512                                         require_once($widgetIndexFile);
513                                 } else {
514                                         echo 'file not found: ' . $widgetIndexFile;
515                                 }
516                         
517                                 // ウィジェット用のタグを閉じる
518                                 $this->gPage->endWidgetXml($cmd);
519                         } else if ($cmd == M3_REQUEST_CMD_RSS){         // RSS配信のとき
520                                 ob_start();// バッファ作成
521                                 
522                                 // ウィジェット用のHTMLヘッダを出力
523                                 $this->gPage->startWidgetRss($cmd);
524                                 
525                                 // 指定のウィジェットを実行
526                                 $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
527
528                                 if (file_exists($widgetIndexFile)){
529                                         // ウィジェット定義ID、ページ定義のシリアル番号を取得
530                                         $configId = 0;          // 定義ID
531                                         $serial = 0;            // シリアル番号
532                                         if ($this->_db->getPageDefOnPageByWidgetId($this->gEnv->getCurrentPageId(), $this->gEnv->getCurrentPageSubId(), $widgetId, $row)){
533                                                 $configId = $row['pd_config_id'];               // 定義ID
534                                                 $serial = $row['pd_serial'];            // シリアル番号
535                                         }
536
537                                         // ウィジェット定義IDを設定
538                                         $this->gEnv->setCurrentWidgetConfigId($configId);
539                         
540                                         // ページ定義のシリアル番号を設定
541                                         $this->gEnv->setCurrentPageDefSerial($serial);
542                                 
543                                         // 実行のログを残す
544                                         $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd);
545
546                                         require_once($widgetIndexFile);
547                                         
548                                         // ウィジェット定義IDを解除
549                                         $this->gEnv->setCurrentWidgetConfigId('');
550                                 
551                                         // ページ定義のシリアル番号を解除
552                                         $this->gEnv->setCurrentPageDefSerial(0);
553                                 } else {
554                                         echo 'file not found: ' . $widgetIndexFile;
555                                 }
556                         
557                                 // 現在のバッファ内容を取得し、バッファを破棄
558                                 $content = ob_get_contents();
559                                 ob_end_clean();
560                                 
561                                 // ウィジェット用のタグを閉じる
562                                 $this->gPage->endWidgetRss($cmd, $content);
563                         } else {                // RSS配信以外のとき
564                                 ob_start();// バッファ作成
565                                                         
566                                 // ウィジェット用のHTMLヘッダを出力
567                                 $this->gPage->startWidget($cmd);
568                                 
569                                 // 指定のウィジェットを実行
570                                 if ($cmd == M3_REQUEST_CMD_CONFIG_WIDGET){              // ウィジェット設定のとき
571                                         $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/admin/' . M3_FILENAME_INDEX;             // 管理用画面
572                                 } else if ($cmd == M3_REQUEST_CMD_CONFIG_TEMPLATE){             // テンプレート設定の場合
573                                         $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
574                                         
575                                         // テンプレート設定画面を取得
576                                         $widgetIndexFile = $this->gEnv->getTemplatesPath() . '/' . $templateId . '/admin/' . M3_FILENAME_INDEX;
577                                 } else {
578                                         $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
579                                 }
580                                 if (file_exists($widgetIndexFile)){
581                                         // ウィジェット定義ID、ページ定義のシリアル番号を取得
582                                         $configId = 0;          // 定義ID
583                                         $serial = 0;            // シリアル番号
584                                         if ($this->_db->getPageDefOnPageByWidgetId($this->gEnv->getCurrentPageId(), $this->gEnv->getCurrentPageSubId(), $widgetId, $row)){
585                                                 $configId = $row['pd_config_id'];               // 定義ID
586                                                 $serial = $row['pd_serial'];            // シリアル番号
587                                         }
588
589                                         // ウィジェット定義IDを設定
590                                         $this->gEnv->setCurrentWidgetConfigId($configId);
591                         
592                                         // ページ定義のシリアル番号を設定
593                                         $this->gEnv->setCurrentPageDefSerial($serial);
594                                         
595                                         // 実行のログを残す
596                                         $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd);
597
598                                         require_once($widgetIndexFile);
599                                         
600                                         // ウィジェット定義IDを解除
601                                         $this->gEnv->setCurrentWidgetConfigId('');
602                                 
603                                         // ページ定義のシリアル番号を解除
604                                         $this->gEnv->setCurrentPageDefSerial(0);
605                                 } else {
606                                         echo 'file not found: ' . $widgetIndexFile;
607                                 }
608                         
609                                 // 現在のバッファ内容を取得し、バッファを破棄
610                                 $content = ob_get_contents();
611                                 ob_end_clean();
612                                 
613                                 // ウィジェット用のタグを閉じる
614                                 $this->gPage->endWidget($cmd, $content);
615                         }
616                 
617                         // 作業中のウィジェットIDを解除
618                         $this->gEnv->setCurrentWidgetId('');
619                         
620                         // サブクラスの後処理の呼び出し
621                         if (method_exists($this, '_postBuffer')) $this->_postBuffer($request);
622                         
623                         if ($cmd == M3_REQUEST_CMD_SHOW_WIDGET ||               // ウィジェットの単体表示
624                                 $cmd == M3_REQUEST_CMD_CONFIG_WIDGET ||         // ウィジェット設定のとき
625                                 $cmd == M3_REQUEST_CMD_CONFIG_TEMPLATE ||               // テンプレートの設定
626                                 ($cmd == M3_REQUEST_CMD_DO_WIDGET && !empty($openBy))){         // ウィンドウオープンタイプ指定でウィジェット単体実行のとき
627                                 
628                                 // 現在の出力内容を取得し、一旦内容をクリア
629                                 $srcContents = ob_get_contents();
630                                 ob_clean();
631                                 
632                                 // 追加変換処理。HTMLヘッダ出力する。
633                                 $destContents = $this->gPage->lateLaunchWidget($request, $srcContents);
634                                 
635                                 echo $destContents;
636                         }
637                         
638                         // ページ作成終了処理(HTTPヘッダ出力)
639                         $this->gPage->endPage($request);
640
641                         if ($cmd != M3_REQUEST_CMD_RSS){                // 画面出力(RSS配信以外)のとき
642                                 // オプション出力(時間計測等)追加
643                                 echo $this->gPage->getOptionContents($request);
644                         }
645
646                         // バッファ内容を送信(クライアントへの送信完了)
647                         ob_end_flush();
648                 }
649                 if (!defined('M3_STATE_IN_INSTALL')){           // インストールモード以外のとき
650                         // #################### アクセスログ記録 #######################
651                         // DBが使用可能であれば、アクセスログのユーザを登録
652                         if ($this->gEnv->canUseDb()) $this->gAccess->accessLogUser();
653                 }
654
655                 // 終了ログ出力
656                 //$this->gLog->info(__METHOD__, 'フレーム作成終了');
657         }
658         /**
659          * 画面を作成
660          *
661          * @param RequestManager $request               HTTPリクエスト処理クラス
662          * @param string $curTemplate                   テンプレートID
663          * @param string $subTemplateId                 サブページID
664          * @return string                                               画面出力
665          */
666         function _createPage($request, $curTemplate, $subTemplateId = '')
667         {
668                 $defaultIndexFile = M3_FILENAME_INDEX;                  // テンプレートの起動ファイル
669                 
670                 $cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);               // 実行コマンドを取得
671                 
672                 // カレントのテンプレートIDを設定
673                 $this->gEnv->setCurrentTemplateId($curTemplate, $subTemplateId);
674
675                 // テンプレート情報を取得
676                 $convType = 0;          // 変換処理タイプ(0=デフォルト(Joomla!v1.0)、-1=携帯用、1=Joomla!v1.5、2=Joomla!v2.5)
677                 if ($this->gEnv->getIsMobileSite()){
678                         $convType = -1;         // 携帯サイト用変換
679                 } else {
680                         // テンプレートタイプを取得(0=デフォルト(Joomla!v1.0),1=Joomla!v1.5,2=Joomla!v2.5)
681                         $convType = $this->gEnv->getCurrentTemplateType();
682                 }
683
684                 // バッファリングの準備
685                 if (method_exists($this, '_prepareBuffer')) $this->_prepareBuffer($request);
686         
687                 // ################### バッファリング開始 ######################
688                 // ob_end_flush()までの出力をバッファリングする
689                 if ($convType == -1){// 携帯用サイトの場合は出力エンコーディングを変更
690                         $mobileEncoding = $this->gEnv->getMobileEncoding();             // 携帯用エンコーディングを取得
691                         mb_http_output($mobileEncoding);
692                         ob_start("mb_output_handler"); // 出力のバッファリング開始
693                 } else {
694                         ob_start();
695                 }
696
697                 // サブクラスの前処理を実行
698                 if (method_exists($this, '_preBuffer')) $this->_preBuffer($request);
699         
700                 if ($convType == 100){          // WordPressテンプレートのとき
701                         // WordPress用ベース定義値
702                         define('WP_DEBUG', true);                       // ##### エラーメッセージ表示制御(true時noticeを表示) #####
703                         define('WPINC', 'wp-includes');
704                         define('ABSPATH', $this->gEnv->getWordpressRootPath() . '/' );
705                         define('TEMPLATEPATH', $this->gEnv->getTemplatesPath() . '/' . $curTemplate);
706                         define('STYLESHEETPATH', $this->gEnv->getTemplatesPath() . '/' . $curTemplate);         // 子テンプレートを使用している場合は子テンプレートを示す。デフォルトはテンプレートを示す。
707                         define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
708                         define('WP_LANG_DIR', WP_CONTENT_DIR . '/languages');
709                         define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
710                         define('WP_CONTENT_URL', '/wp-content');                                // 定義自体は必要であるが使用しないのでダミー値で定義
711                         define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins');   // 定義自体は必要であるが使用しないのでダミー値で定義
712                         
713                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/load.php');
714                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/default-constants.php');               // デフォルト値取得
715                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/plugin.php');
716
717                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/functions.php');
718                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/default-filters.php');
719                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/l10n.php');
720                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-error.php');
721                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp.php');
722                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-locale.php');
723                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-user.php');
724                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-walker.php');
725                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-query.php');                          // コンテンツデータ取得
726                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-comment-query.php');
727                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-term.php');
728                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-page.php');
729                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-theme.php');
730                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-widget.php');
731                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-widget-factory.php');
732                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-list-util.php');
733 //                      require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-nav-menu.php');
734 //                      require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-dependency.php');
735                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-post.php');                   // コンテンツAPIマネージャーからWP_Post型でデータを取得
736                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-post-type.php');
737                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-embed.php');
738                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-http.php');
739                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-http-requests-hooks.php');
740                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-http-proxy.php');
741
742                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/query.php');
743                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pluggable.php');
744                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post.php');
745                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/user.php');
746                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/widgets.php');
747                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/comment.php');
748                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/http.php');
749                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/kses.php');
750                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/script-loader.php');
751                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/theme.php');
752                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/template.php');
753                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/link-template.php');
754                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/category.php');
755                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/category-template.php');
756                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-template.php');
757                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-thumbnail-template.php');
758                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/comment-template.php');
759                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/author-template.php');
760                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/nav-menu-template.php');
761                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/nav-menu.php');
762                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/general-template.php');
763                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/cache.php');
764                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/shortcodes.php');
765                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/formatting.php');
766                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-formats.php');
767                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/taxonomy.php');
768                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/media.php');
769                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/embed.php');
770 //                      require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/option.php');
771                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pomo/translations.php');
772                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pomo/mo.php');
773                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/capabilities.php');
774                         require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/meta.php');
775                 
776                         // Magic3用インターフェイス
777                         require_once($this->gEnv->getWordpressRootPath() . '/wpInit.php');              // 初期値取得
778                         require_once($this->gEnv->getWordpressRootPath() . '/contentApi.php');  // コンテンツ取得API
779                         require_once($this->gEnv->getWordpressRootPath() . '/menuApi.php');             // メニュー情報取得API
780                         require_once($this->gEnv->getWordpressRootPath() . '/WPRender.php');            // ウィジェット描画クラス
781
782                         // ##### データ初期化 #####
783                         wp_initial_constants();                 // WordPressその他定義値設定
784                         wp_cookie_constants();                  // クッキー用定義
785                         create_initial_post_types();    // WP_Post型データ型登録
786                         register_shutdown_function('shutdown_action_hook');             // 終了時イベント登録
787                         
788                         // プラグイン初期化
789                         $GLOBALS['wp_plugin_paths'] = array();                  // $wp_plugin_pathsは未使用?
790                         foreach (wp_get_active_and_valid_plugins() as $plugin) {// プラグインロード
791                                 wp_register_plugin_realpath($plugin);
792                                 include_once($plugin);
793                         }
794                         unset($plugin);
795
796                         // WordPressメインオブジェクト作成
797                         $GLOBALS['wp_version'] = '4.7.0';                       // 下位互換性チェックで引っかかるのでv4.7.0に定める
798                         $GLOBALS['locale'] = $this->gEnv->getCurrentLanguage();         // 表示言語を設定
799                         $GLOBALS['wp_embed'] = new WP_Embed();
800                         $GLOBALS['wp_the_query'] = new WP_Query();                              // $wp_the_queryは変更不可変数で$wp_queryは変更可変数
801                         $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
802                         $GLOBALS['wp'] = new WP();
803                         $GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
804                         $GLOBALS['gContentApi'] = new contentApi();                     // Magic3コンテンツAPIオブジェクト
805                         $GLOBALS['gMenuApi'] = new menuApi();                   // Magic3メニュー情報APIオブジェクト
806                         $GLOBALS['m3WpOptions'] = array();                              // 定義値初期化
807                         // テンプレートから参照可能にする
808                         global $wp_query;
809
810                         // ページに配置されているウィジェットの状況からWordPress以外の主コンテンツ用のプラグイン(WooCommerce等)をロード
811                         // setup_themeイベント処理を設定
812                         $GLOBALS['gContentApi']->loadPlugin();
813                         
814                         // テンプレート初期処理
815                         do_action('setup_theme');
816                         load_default_textdomain();                      // 言語リソースを読み込む
817                         m3WpInit();                                                     // 言語リソース読み込み後にMagic3用インターフェイス初期化。$GLOBALS['m3WpOptions']を初期化し、get_option()はここから使用可能にする。
818                         $GLOBALS['wp_locale'] = new WP_Locale();                // 言語リソース読み込み後に生成
819
820                         // functions.phpを読み込む。ファイル内で定義されている変数はグローバル変数に変換する。
821                         $this->_loadFileAsGlobal(TEMPLATEPATH . '/functions.php');
822
823                         do_action('after_setup_theme');         // wp-multibyte-patchプラグイン読み込み
824                         do_action('init');                                      // テンプレート側からの初期処理(ウィジェットのCSSの初期化等)
825                         do_action('wp_loaded');
826                         
827                         // ##### 起動PHPファイル取得。データ取得用パラメータ設定。#####
828                         // URLパラメータからコンテンツ形式を取得し、ページを選択
829                         $params = $request->getQueryArray();
830                         $paramCount = count($params);
831                         reset($params);
832                         $firstKey = key($params);
833 //                      $firstValue = $params[$firstKey];
834                         
835                         // コンテンツタイプに合わせて起動PHPファイルを決める。デフォルトはindex.phpで一覧形式で表示。
836                         $pageTypeDefined = false;               // ページタイプ確定したかどうか
837                         $wpIndexFile = get_index_template();            // WordPress用テンプレート起動ファイル
838                         $contentType = $GLOBALS['gContentApi']->getContentType();
839                         switch ($contentType){
840                         case M3_VIEW_TYPE_CONTENT:              // 汎用コンテンツ
841                                 if ($firstKey == M3_REQUEST_PARAM_CONTENT_ID || $firstKey == M3_REQUEST_PARAM_CONTENT_ID_SHORT){        // コンテンツIDのとき
842                                         // ページタイプを設定
843                                         $GLOBALS['gContentApi']->setPageType('page');
844                                         
845                                         // フルパスで返るので相対パスに修正
846 //                                      $defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_page_template());            // 固定ページテンプレート
847                                         $wpIndexFile = get_page_template();             // 固定ページテンプレート
848                                         
849                                         // コンテンツID設定
850                                         $firstValue = $request->trimValueOf($firstKey);
851                                         $GLOBALS['gContentApi']->setContentId($firstValue);
852                                         
853                                         $pageTypeDefined = true;                // ページタイプ確定
854                                 }
855                                 break;
856                         case M3_VIEW_TYPE_PRODUCT:      // 製品
857                                 if ($firstKey == M3_REQUEST_PARAM_PRODUCT_ID || $firstKey == M3_REQUEST_PARAM_PRODUCT_ID_SHORT){                // 製品IDのとき
858                                         // ページタイプを設定
859                                         $GLOBALS['gContentApi']->setPageType('single');
860                                         
861                                         // フルパスで返るので相対パスに修正
862                                         //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_single_template());                // 記事詳細テンプレート
863                                         $wpIndexFile = get_single_template();           // 記事詳細テンプレート
864                                         
865                                         // コンテンツID設定
866                                         $firstValue = $request->trimValueOf($firstKey);
867                                         $GLOBALS['gContentApi']->setContentId($firstValue);
868                                         
869                                         $pageTypeDefined = true;                // ページタイプ確定
870                                 }
871                                 break;
872                         case M3_VIEW_TYPE_BBS:  // BBS
873                                 break;
874                         case M3_VIEW_TYPE_BLOG: // ブログ
875                                 if ($firstKey == M3_REQUEST_PARAM_BLOG_ENTRY_ID || $firstKey == M3_REQUEST_PARAM_BLOG_ENTRY_ID_SHORT){          // ブログ記事IDのとき
876                                         // ページタイプを設定
877                                         $GLOBALS['gContentApi']->setPageType('single');
878                                         
879                                         // フルパスで返るので相対パスに修正
880                                         //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_single_template());                // 記事詳細テンプレート
881                                         $wpIndexFile = get_single_template();           // 記事詳細テンプレート
882                                         
883                                         // コンテンツID設定
884                                         $firstValue = $request->trimValueOf($firstKey);
885                                         $GLOBALS['gContentApi']->setContentId($firstValue);
886                                         
887                                         $pageTypeDefined = true;                // ページタイプ確定
888                                 } else {
889                                         // カテゴリーが設定されている場合はカテゴリー画面を表示
890                                         $value = $request->trimValueOf(M3_REQUEST_PARAM_CATEGORY_ID);
891                                         if (!empty($value)){
892                                                 // ページタイプを設定
893                                                 $GLOBALS['gContentApi']->setPageType('category');                       // カテゴリー表示
894                                 
895                                                 // カテゴリー用テンプレート取得
896                                                 $template = get_category_template();
897                                                 if (empty($template)) $template = get_archive_template();               // カテゴリー用のテンプレートが取得できない場合はアーカイブ用テンプレートを取得
898                                                 if (!empty($template)) $wpIndexFile = $template;
899
900                                                 // フルパスで返るので相対パスに修正
901                                                 //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $template);            // カテゴリーテンプレート
902                                                 
903                                                 $pageTypeDefined = true;                // ページタイプ確定
904                                         }
905                                         if (!$pageTypeDefined){
906                                                 $year = $request->trimValueOf(M3_REQUEST_PARAM_YEAR);           // 年指定
907                                                 $month = $request->trimValueOf(M3_REQUEST_PARAM_MONTH);         // 月指定
908                                                 $day = $request->trimValueOf(M3_REQUEST_PARAM_DAY);             // 日指定
909
910                                                 if (!empty($year)){                     // 年月日指定のとき
911                                                         // ページタイプを設定
912                                                         $GLOBALS['gContentApi']->setPageType('date');                   // 年月日表示
913                                 
914                                                         // 年月日用テンプレート取得
915                                                         $template = get_date_template();
916                                                         if (empty($template)) $template = get_archive_template();               // 年月日用のテンプレートが取得できない場合はアーカイブ用テンプレートを取得
917                                                         if (!empty($template)) $wpIndexFile = $template;
918                                                 
919                                                         // フルパスで返るので相対パスに修正
920                                                         //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $template);
921
922                                                         $pageTypeDefined = true;                // ページタイプ確定
923                                                 }
924                                         }
925                                         // 検索条件が設定されている場合は検索画面を表示
926                                         if (!$pageTypeDefined){
927                                                 $value = $request->trimValueOf('s');
928                                                 if (!empty($value)){
929                                                         // ページタイプを設定
930                                                         $GLOBALS['gContentApi']->setPageType('search');                 // 検索結果表示
931                                         
932                                                         // フルパスで返るので相対パスに修正
933                                                         //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_search_template());                // 検索結果テンプレート
934                                                         $template = get_search_template();              // 検索結果テンプレート
935                                                         if (!empty($template)) $wpIndexFile = $template;
936                                                         
937                                                         $pageTypeDefined = true;                // ページタイプ確定
938                                                 }
939                                         }
940                                 }
941                                 break;
942                         case M3_VIEW_TYPE_WIKI: // Wiki
943                                 break;
944                         case M3_VIEW_TYPE_USER: // ユーザ作成コンテンツ
945                                 break;
946                         case M3_VIEW_TYPE_EVENT:        // イベント
947                                 break;
948                         case M3_VIEW_TYPE_PHOTO:        // フォトギャラリー
949                                 break;
950                         default:
951                                 // 検索キーワードが設定されている場合は検索結果画面を表示
952                                 $value = $request->trimValueOf('s');
953                                 if (!empty($value)){
954                                         // ページ指定されていない場合(フロント画面等)、検索結果表示用ページがある場合はリダイレクト
955                                         $subId = $request->trimValueOf(M3_REQUEST_PARAM_PAGE_SUB_ID);
956                                         if (empty($subId)){
957                                                 $subId = $this->_db->getSubPageIdWithContent(M3_VIEW_TYPE_SEARCH, $this->gEnv->getCurrentPageId());// ページサブIDを取得
958                                                 if (!empty($subId)){
959                                                         // リダイレクト用URLを作成
960                                                         $redirectUrl = $this->gEnv->createPageUrl();
961                                                         $redirectUrl .= '?' . M3_REQUEST_PARAM_PAGE_SUB_ID . '=' . $subId;
962                                                         //$redirectUrl .= '&s=' . urlencode($value);                                                    // 検索キーワード
963                                                         $redirectUrl .= '&' . M3_REQUEST_PARAM_OPERATION_TASK . '=search&' . M3_REQUEST_PARAM_KEYWORD . '=' . urlencode($value);                // 検索キーワード
964                                                         
965                                                         $this->gPage->redirect($redirectUrl);
966                                                         return;                         // ここで終了
967                                                 }
968                                         }
969 //                              } else {
970 //                                      // 検索キーワードが設定されている場合は画面タイトルを設定
971 //                                      $value = $request->trimValueOf(M3_REQUEST_PARAM_KEYWORD);
972 //                                      if (!empty($value)){
973 //                                      }
974                                 }
975                                 
976                                 // ##### デフォルトのページタイトルを設定 #####
977                                 $pageInfo = $this->gPage->getCurrentPageInfo();
978                                 if (!empty($pageInfo)){
979                                         $pageTitle = $pageInfo['pg_name'];
980                                         if (!empty($pageTitle)) $GLOBALS['gContentApi']->setPostTitle($pageTitle);
981                                 }
982                                 
983                                 // コンテンツタイプが設定されていないページ(お問合わせページ等)に場合は、固定ページ用のテンプレートを使用
984                                 // ページタイプを設定
985                                 $GLOBALS['gContentApi']->setPageType('page');
986                                 
987                                 // フルパスで返るので相対パスに修正
988                                 //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_page_template());          // 固定ページテンプレート
989                                 $wpIndexFile = get_page_template();             // 固定ページテンプレート
990                         
991                                 $pageTypeDefined = true;                // ページタイプ確定
992                                 break;
993                         }
994                         
995                         // コンテンツタイプが設定されているページでページタイプが設定されていないページの場合はデフォルトテンプレート(index.php)の代わりにホーム用テンプレートを取得
996                         if (!empty($contentType) && !$pageTypeDefined){
997                                 // フルパスで返るので相対パスに修正
998                                 //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_home_template());          // ホーム用テンプレート
999                                 $wpIndexFile = get_home_template();             // ホーム用テンプレート
1000                         }
1001
1002                         // サイトのトップページを表示する場合(コンテンツタイプが設定されていないページをデフォルトで表示する場合)は優先してフロント用テンプレートを表示
1003                         if (empty($contentType)){
1004                         //if (!$GLOBALS['gContentApi']->isHomeUrl()){
1005 //                      if ($defaultIndexFile == M3_FILENAME_INDEX){            // テンプレートの起動ファイル
1006                                 $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_PAGE_SUB_ID);
1007                                 if ($this->gEnv->getCurrentPageSubId() == $this->gEnv->getDefaultPageSubId() && empty($pageSubId)){             // デフォルトページを表示し「sub」なしに限定
1008                                         $frontPageTemplate = get_front_page_template();
1009                                         if (!empty($frontPageTemplate)){
1010                                                 //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $frontPageTemplate);   // フロントページテンプレート
1011                                                 $wpIndexFile = $frontPageTemplate;              // フロントページテンプレート
1012                                         }
1013                                 }
1014                         }
1015
1016                         // プラグインからの起動ファイルパス変換
1017                         $wpIndexFile = apply_filters('template_include', $wpIndexFile);
1018
1019                         // Magic3用のテンプレート起動ファイルパスに変換
1020 //                      $defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $wpIndexFile);
1021                         $defaultIndexFile = $wpIndexFile;
1022                         
1023                         // ##### テンプレート前処理(起動ファイル決定後に実行) #####
1024                         do_action('template_redirect');
1025                                                 
1026                         // WordPressオブジェクト作成。wpイベント実行。
1027                         wp();
1028                 } else if ($convType >= 1){             // Joomla!v1.5,v2.5テンプレートのとき
1029                         // Joomla!テンプレート共通の設定
1030                         define('_JEXEC', 1);
1031                         define('JPATH_PLATFORM', $this->gEnv->getJoomlaRootPath());
1032                         define('JPATH_BASE', dirname(__FILE__));
1033                         define('JPATH_SITE', $this->gEnv->getSystemRootPath());
1034                         define('JPATH_PLUGINS', $this->gEnv->getJoomlaRootPath() . '/class/plugins');                   // プラグインパス
1035 //                      define('JPATH_THEMES', $this->gEnv->getTemplatesPath());                                                                // テンプレートパス             ## テンプレート内でエラーが発生するのでここでは定義しない(2015/10/13)
1036                         define('DS', DIRECTORY_SEPARATOR);
1037                         
1038                         global $mainframe;
1039                         require_once($this->gEnv->getJoomlaRootPath() . '/mosDef.php');// Joomla定義読み込み
1040                         require_once($this->gEnv->getJoomlaRootPath() . '/JParameter.php');
1041                         require_once($this->gEnv->getJoomlaRootPath() . '/JRender.php');
1042                                                 
1043                         // 設定ファイルの読み込み
1044                         //$params = array();
1045                         $paramFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplate . '/params.ini';
1046                         if (is_readable($paramFile)){
1047                                 $content = file_get_contents($paramFile);
1048                                 $params = new JParameter($content);
1049                         } else {
1050                                 $params = new JParameter();
1051                         }
1052                         // テンプレートヘッダ画像上のテキスト設定(Joomla!テンプレート2.5以降)
1053                         $params->set('siteTitle',               $this->gEnv->getSiteName());            // サイト名
1054                         $params->set('siteSlogan',              $this->gSystem->getSiteDef(M3_TB_FIELD_SITE_SLOGAN));           // サイトスローガン
1055
1056                         // Joomla!v1.5用の設定
1057                         $this->language = $this->gEnv->getCurrentLanguage();
1058                         $this->template = $curTemplate;
1059                         //$this->baseurl  = $this->gEnv->getRootUrl();
1060                         $this->baseurl          = $this->gEnv->getRootUrlByCurrentPage();
1061                         $this->direction = 'ltr';
1062                         $this->params   = $params;
1063                         
1064                         // サブテンプレート用の設定
1065                         if ($this->gEnv->getCurrentTemplateGenerator() == self::TEMPLATE_GENERATOR_THEMLER){            // Themlerテンプレートの場合はサブテンプレート用のパラメータを設定
1066                                 // JRequest経由でレンダー側にサブテンプレートIDを渡す
1067                                 if (!empty($subTemplateId)) JRequest::injectSetVar('file_template_name', $subTemplateId);
1068
1069                                 // サブテンプレートIDの渡し方は以下の方法もある(Themlerテンプレート1.39以降はこちらが有効)
1070                                 // サブテンプレートIDを埋め込む
1071                                 if (!empty($subTemplateId)) $this->setBuffer('<!--TEMPLATE ' . $subTemplateId . ' /-->', 'component');
1072                         }
1073                         
1074                         // 現在のJoomla!ドキュメントを設定
1075                         $this->gEnv->setJoomlaDocument($this);
1076                 } else {                        // デフォルト(Joomla!v1.0テンプレート)テンプレートのとき(PC用および携帯用)
1077                         // Joomla!テンプレート共通の設定
1078                         define('_JEXEC', 1);
1079                         
1080                         // Joomlaテンプレート用定義
1081                         global $mosConfig_absolute_path;
1082                         global $mosConfig_live_site;
1083                         global $mosConfig_sitename;
1084                         global $mosConfig_favicon;
1085                         global $mosConfig_sef;
1086                         global $cur_template;
1087                         global $mainframe;
1088                         require_once($this->gEnv->getJoomlaRootPath() . '/mosDef.php');// Joomla定義読み込み
1089                         require_once($this->gEnv->getJoomlaRootPath() . '/mosFunc.php');
1090                         require_once($this->gEnv->getJoomlaRootPath() . '/includes/sef.php');
1091                 }
1092
1093                 // ################### テンプレート読み込み ###################
1094                 // フルパスに変換
1095                 if (strStartsWith($defaultIndexFile, '/')){                             // フルパス起動(WordPressテンプレート)のとき
1096                         $templateIndexFile = $defaultIndexFile;
1097                 } else {
1098                         $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplate . '/' . $defaultIndexFile;
1099                 }
1100                 
1101                 if (file_exists($templateIndexFile)){
1102                         require_once($templateIndexFile);
1103                 } else {                // テンプレートが存在しないとき
1104                         if ($this->gEnv->isSystemManageUser()){         // システム管理ユーザのとき
1105                                 echo 'template not found error: ' . $curTemplate . ', path=' . $templateIndexFile;
1106                         } else {
1107                                 // 一般向けにはメンテナンス画面を表示
1108                                 $this->gPage->setSystemHandleMode(10/*サイト非公開中*/);
1109                                 $this->_showSystemPage($request, 2/*サイト非公開画面*/);// システム制御画面を表示
1110                                         
1111                                 // 運用ログに記録(一度だけ出力したい)
1112                                 //$this->gOpeLog->writeFatal(__METHOD__, 'テンプレートが存在しません。メンテナンス画面を表示します。(テンプレートID=' . $curTemplate . ')', 1100);
1113                                 return;
1114                         }
1115                 }
1116
1117                 // サブクラスの後処理の呼び出し
1118                 if (method_exists($this, '_postBuffer')) $this->_postBuffer($request);
1119
1120                 // ##### WordPressテンプレートの場合は終了イベントを実行。WordPressのエラーメッセージは画面に出力させる。 #####
1121                 if ($convType == 100){          // WordPressテンプレートのとき
1122                         do_action('shutdown');
1123                 }
1124                 
1125                 // 現在の出力内容を取得し、一旦内容をクリア
1126                 $srcContents = ob_get_contents();
1127                 ob_clean();
1128
1129                 // Joomla!タグの変換処理(ウィジェット実行)
1130                 if ($convType >= 1 && $convType < 100){         // Joomla!v1.5,v2.5テンプレートのとき
1131                         $srcContents = $this->gPage->launchWidgetByJoomlaTag($srcContents, $convType);          // launchWidgetByJoomlaTag()は携帯変換(-1)は実行されない
1132                 }
1133         
1134                 // 遅延実行ウィジェットの出力を埋め込む。HTMLヘッダ出力する。
1135                 $destContents = $this->gPage->lateLaunchWidget($request, $srcContents);
1136
1137                 // 携帯インターフェイスのときのときは、手動変換後、バイナリコード(絵文字等)を埋め込む
1138                 if ($convType == -1){                   // 携帯アクセスポイントの場合
1139                         // 出力するコードに変換
1140                         $destContents = mb_convert_encoding($destContents, $mobileEncoding, M3_ENCODING);
1141         
1142                         // コンテンツ変換メソッドがある場合は実行
1143                         if (method_exists($this, '_convContents')){
1144                                 $destContents = $this->_convContents($destContents);// 絵文字埋め込み処理等
1145                         }
1146                 }
1147                 
1148                 // ##### CSS生成の場合は、すべてのウィジェット実行後出力を削除する #####
1149                 if ($cmd == M3_REQUEST_CMD_CSS) $destContents = '';             // CSS生成のとき
1150
1151                 // ページ作成終了処理(HTTPヘッダ出力)
1152                 $destContents .= $this->gPage->endPage($request, true/*出力を取得*/);              // 最終HTMLを追加
1153                 if ($this->gPage->isRedirect()) return '';// リダイレクトの場合ob_end_clean()を実行すると、ログインできないことがあるのでここで終了(2011/11/11)
1154                 
1155                 // バッファを破棄
1156                 //ob_end_flush();
1157                 ob_end_clean();
1158                 
1159                 // 送信データを返す
1160                 return $destContents;
1161         }
1162         /**
1163          * テンプレートを決定
1164          *
1165          * @param RequestManager $request       HTTPリクエスト処理クラス
1166          * @param string $subTemplateId         テンプレートIDが取得できるときはサブページIDが返る
1167          * @return string                                       テンプレート名
1168          */
1169         function _defineTemplate($request, &$subTemplateId)
1170         {
1171                 // ########### テンプレートID(ディレクトリ名)を設定 ############
1172                 // テンプレートIDの指定の方法は2パターン
1173                 //  1.サブクラスで固定に指定
1174                 //  2.コンテンツからの指定
1175                 //  3.セッションに保持
1176                 // テンプレートIDの優先順位
1177                 //  1.サブクラスの_setTemplate()で固定設定にしている場合の固定値
1178                 //  2.セッションに持っている値
1179                 //  3.DBのデフォルト値
1180                 $curTemplate = '';
1181                 $subTemplateId = '';
1182                 $isSystemManageUser = $this->gEnv->isSystemManageUser();                // システム運用可能かどうか
1183                 $useSubClassDefine = true;                      // サブクラスでの定義を使用するかどうか
1184                 
1185                 // テンプレート変更のときは、セッションのテンプレートIDを変更
1186                 $cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);               // 実行コマンドを取得
1187                 if ($cmd == M3_REQUEST_CMD_CHANGE_TEMPLATE){
1188                         // テンプレートIDをセッションに残す場合
1189                         if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
1190                                 $request->setSessionValue(M3_SESSION_CURRENT_TEMPLATE, $request->trimValueOf(M3_SYSTEM_TAG_CHANGE_TEMPLATE));
1191                         }
1192                 }
1193
1194                 // サブクラスでテンプレートIDを指定している場合はそちらを使用
1195                 $templateDefined = false;               // テンプレート固定かどうか
1196                 if ($useSubClassDefine){
1197                         $tmplStr = trim($this->_setTemplate($request));
1198                         if (strlen($tmplStr) > 0){
1199                                 $curTemplate = $tmplStr;
1200                                 $templateDefined = true;                // テンプレート固定かどうか
1201                         }
1202                 }
1203
1204                 // セッションにあるときは、セッションの値を使用(携帯でないとき)
1205                 $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
1206                 if (empty($curTemplate)){
1207                         if ($cmd == M3_REQUEST_CMD_SHOW_POSITION ||                             // 表示位置を表示するとき
1208                                 $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET){      // 表示位置を表示するとき(ウィジェット付き)
1209                                 // URLの引数として、ページIDとページサブIDが指定されてくる
1210                                 // URLの引数のテンプレートを優先し、引数で指定されていなければ、ページ用個別のテンプレートを取得する
1211                                 
1212                                 // URLの引数でテンプレートIDが指定されている場合は設定
1213                                 $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
1214                                 if (!empty($templateId)) $curTemplate = $templateId;
1215                                         
1216                                 // ページ用個別に設定されたテンプレートがある場合は取得
1217                                 if (empty($curTemplate)){
1218                                         $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
1219                                         $line = $this->gPage->getPageInfo($pageId, $pageSubId);
1220                                         if (!empty($line)){
1221                                                 $pageTemplateId = $line['pn_template_id'];
1222                                                 $subTemplateId = $line['pn_sub_template_id'];           // サブテンプレートID
1223                                         }
1224                                         if (!empty($pageTemplateId)) $curTemplate = $pageTemplateId;
1225                                 }
1226                                 
1227                                 // 取得できなければデフォルトを取得
1228                                 if (empty($curTemplate)){
1229                                         if ($pageId == $this->gEnv->getDefaultPageId()){                // 通常サイトのとき
1230                                                 $curTemplate = $this->gSystem->defaultTemplateId();
1231                                                 $subTemplateId = $this->gSystem->defaultSubTemplateId();
1232                                         } else if ($pageId == $this->gEnv->getDefaultMobilePageId()){           // 携帯サイトのとき
1233                                                 $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
1234                                         } else if ($pageId == $this->gEnv->getDefaultSmartphonePageId()){               // スマートフォン用サイトのとき
1235                                                 $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
1236                                         } else if ($pageId == $this->gEnv->getDefaultAdminPageId() ||           // 管理サイトのとき
1237                                                                 $pageId == $this->gEnv->getDefaultRegistPageId()){              // 登録サイトのとき
1238                                                 $curTemplate = $this->gSystem->defaultAdminTemplateId();
1239                                         } else if (empty($pageId)){                     // ページIDが指定されていないときは、ウィジェットを表示しないテンプレートのみの表示
1240                                                 // URLの引数でテンプレートIDが指定されている場合は設定
1241         //                                      $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
1242         //                                      if (!empty($templateId)) $curTemplate = $templateId;
1243                                         }
1244                                 }
1245                         } else {
1246                                 // ページ用のテンプレートがあるときは優先
1247                                 $pageTemplateId = $this->gPage->getTemplateIdFromCurrentPageInfo($subTemplateId);
1248                                 if (!empty($pageTemplateId)) $curTemplate = $pageTemplateId;
1249
1250                                 // テンプレートIDをセッションから取得
1251                                 if (empty($curTemplate) && !$isSystemManageUser){                       // システム運用者はセッション値を使用できない
1252                                         if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
1253                                                 if (!$this->gEnv->getIsMobileSite() && !$this->gEnv->getIsSmartphoneSite()){
1254                                                         $curTemplate = $request->getSessionValue(M3_SESSION_CURRENT_TEMPLATE);// 携帯サイト、スマートフォンサイトでないときはセッション値を取得
1255                                                 }
1256                                         }
1257                                 }
1258                                 
1259                                 // オプションのテンプレートがある場合はオプションを優先
1260                                 list($optionTemplate, $optionSubTemplate) = $this->gPage->getOptionTemplateId();
1261                                 if (!empty($optionTemplate)){
1262                                         $curTemplate = $optionTemplate;
1263                                         $subTemplateId = $optionSubTemplate;
1264                                         $templateDefined = true;                // テンプレート固定かどうか
1265                                 }
1266                                 
1267                                 // セッションにないときはデフォルトを取得
1268                                 if (empty($curTemplate)){
1269                                         if ($this->gEnv->getIsMobileSite()){// 携帯用サイトの場合
1270                                                 $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
1271                                         } else if ($this->gEnv->getIsSmartphoneSite()){// スマートフォン用サイトの場合
1272                                                 $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
1273                                         } else {
1274                                                 $curTemplate = $this->gSystem->defaultTemplateId();
1275                                                 $subTemplateId = $this->gSystem->defaultSubTemplateId();
1276                                         }
1277                                 }
1278                         }
1279                 }
1280
1281                 if (empty($curTemplate)){
1282                         // テンプレートが1つもみつからないときは、管理用テンプレートを使用
1283                         $curTemplate = $this->gSystem->defaultAdminTemplateId();
1284                         echo 'template not found. viewing by administration template. [' . $curTemplate . ']';
1285                 } else {        // セッションにテンプレートIDを保存
1286                         // テンプレートIDをセッションに残す場合
1287 /*                      if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
1288                                 if ($cmd == M3_REQUEST_CMD_SHOW_POSITION ||                             // 表示位置を表示するとき
1289                                         $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET){      // 表示位置を表示するとき(ウィジェット付き)
1290                                 } else {
1291                                         if (!$this->gEnv->getIsMobileSite() && !$this->gEnv->getIsSmartphoneSite() && !$templateDefined){               // PC用画面でサブクラス固定でないとき場合は保存
1292                                                 $request->setSessionValue(M3_SESSION_CURRENT_TEMPLATE, $curTemplate);
1293                                         }
1294                                 }
1295                         }*/
1296                 }
1297                 return $curTemplate;
1298         }
1299         /**
1300          * サイト公開制御
1301          *
1302          * @param RequestManager $request               HTTPリクエスト処理クラス
1303          * @return bool                                                 サイトにアクセスできるかどうか
1304          */
1305         function _accessSite($request)
1306         {
1307                 // サイトの公開状況を取得
1308                 $isOpen = $this->gSystem->siteInPublic();
1309                 if ($isOpen){
1310                         // PC用サイト、携帯用サイト、スマートフォン用サイトの公開状況をチェック
1311                         if ($this->gEnv->getIsPcSite()){
1312                                 if ($this->gSystem->sitePcInPublic()) return true;
1313                         } else if ($this->gEnv->getIsMobileSite()){
1314                                 if ($this->gSystem->siteMobileInPublic()) return true;
1315                         } else if ($this->gEnv->getIsSmartphoneSite()){
1316                                 if ($this->gSystem->siteSmartphoneInPublic()) return true;
1317                         }
1318                         return false;
1319                 } else {
1320                         // 例外とするIPアドレスをチェック
1321                         $ip = $this->gSystem->getSystemConfig(self::SITE_ACCESS_EXCEPTION_IP);
1322                         if (!empty($ip) && $ip = $request->trimServerValueOf('REMOTE_ADDR')){
1323                                 return true;
1324                         } else {
1325                                 return false;
1326                         }
1327                 }
1328         }
1329         /**
1330          * システム制御画面表示
1331          *
1332          * @param RequestManager $request               HTTPリクエスト処理クラス
1333          * @param int $type                                             画面タイプ(0=アクセス不可、1=ログイン画面、2=サイト非公開画面)
1334          * @return なし
1335          */
1336         function _showSystemPage($request, $type)
1337         {
1338                 // ページIDを設定
1339                 $pageId = 'admin_index';                // 管理画面を表示
1340                 $this->gEnv->setCurrentPageId($pageId);                                                         // ここでデフォルトページサブIDが再設定される
1341                 $this->gEnv->setCurrentPageSubId($this->gEnv->getDefaultPageSubId());// デフォルトページサブIDをカレントにする
1342                 
1343                 // テンプレートの設定
1344                 // DBで設定されている値を取得し、なければ管理用デフォルトテンプレートを使用
1345                 if ($this->gEnv->getIsMobileSite()){            // 携帯用サイトのアクセスの場合
1346                         $curTemplateId = self::M_ADMIN_TEMPLATE;        // 携帯管理画面用テンプレート
1347                 } else {                        // 携帯以外のサイトへのアクセスの場合
1348                         if ($type == 1){                        // ログインはデフォルトの管理画面テンプレートに固定
1349                                 $curTemplateId = $this->gSystem->defaultAdminTemplateId();
1350                         } else {
1351                                 $curTemplateId = $this->gSystem->getSystemConfig(self::CONFIG_KEY_MSG_TEMPLATE);
1352                                 if (empty($curTemplateId)){
1353                                         $curTemplateId = self::SYSTEM_TEMPLATE;// システム画面用テンプレート
1354                                 } else {
1355                                         // テンプレートの存在チェック
1356                                         $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplateId . '/' . M3_FILENAME_INDEX;
1357                                         if (!file_exists($templateIndexFile)) $curTemplateId = self::SYSTEM_TEMPLATE;// システム画面用テンプレート
1358                                 }
1359                         }
1360                 }
1361
1362                 // 画面を作成
1363                 $pageData = $this->_createPage($request, $curTemplateId);
1364                 echo $pageData;
1365         }
1366         /**
1367          * phpinfo画面表示
1368          *
1369          * @param RequestManager $request               HTTPリクエスト処理クラス
1370          * @return なし
1371          */
1372         function _showPhpinfoPage($request)
1373         {
1374                 // ################### バッファリング開始 ######################
1375                 // ob_end_flush()までの出力をバッファリングする
1376                 ob_start();
1377                 
1378                 phpinfo();
1379                 
1380                 // バッファ内容を送信(クライアントへの送信完了)
1381                 ob_end_flush();
1382         }
1383         /**
1384          * WordPressテンプレートの起動ファイルパスを相対パスに変換
1385          *
1386          * @param string $templateId    テンプレートID
1387          * @param string $path                  テンプレートの起動ファイル絶対パス
1388          * @return string                               テンプレート内での相対パス。エラー発生の場合はデフォルト(index.php)を返す。
1389          */
1390         function _getRelativeTemplateIndexPath($templateId, $path)
1391         {
1392                 $savedPath = $path;
1393                 $templatePath = $this->gEnv->getTemplatesPath() . '/' . $templateId . '/';
1394                 
1395                 // テンプレートまでのパスを削除
1396                 $path = str_replace($templatePath, '', $path);
1397                 if ($path == $savedPath) $path = M3_FILENAME_INDEX;
1398                 return $path;
1399         }
1400         /**
1401          * 旧システムディレクトリが存在するかどうかを取得
1402          *
1403          * @return bool                         true=存在する、false=存在しない
1404          */
1405         function _isExistsOldSystemDir()
1406         {
1407                 // 旧システムディレクトリは同ディレクト階層に存在し、ディレクトリ名の先頭に「_」が付加されているディレクトリ
1408                 $currentDir = $this->gEnv->getSystemRootPath();
1409                 $parentDir = dirname($currentDir);
1410                 $dirName = basename($currentDir);
1411                 
1412                 // ##### open_basedir等のアクセス制限が掛かっていてディレクトリが見えない場合はis_dir()はfalseを返す #####
1413                 // 親ディレクトリへのアクセス権をチェック
1414                 if (@is_dir($parentDir)){
1415                         if (@is_dir($parentDir . '/_' . $dirName)){
1416                                 return true;
1417                         } else {
1418                                 return false;
1419                         }
1420                 } else {                // 親ディレクトリへのアクセス権がない場合は旧システムが存在すると判断する
1421                         return true;
1422                 }
1423         }
1424         /**
1425          * PHPファイルを読み込み、定義値をグローバル値に変換する
1426          *
1427          * @param string $path          ファイルパス
1428          * @return bool                         true=ファイル読み込み完了、false=ファイル読み込み失敗
1429          */
1430         function _loadFileAsGlobal($path)
1431         {
1432                 if (file_exists($path)){
1433                         include($path);
1434                         
1435                         // グローバル変数に変換
1436                         $vars = get_defined_vars();
1437                         foreach($vars as $varName => $varValue){
1438                                 if (!isset($GLOBALS[$varName])) $GLOBALS[$varName] = $varValue;
1439                         }
1440                         return true;
1441                 } else {
1442                         return false;
1443                 }
1444         }
1445         /***********************************************************************************
1446          * 以下、テンプレート専用
1447          ***********************************************************************************/
1448         /**
1449          * [カスタムテンプレート用] テンプレートのヘッダ部に出力するCSSのデータを取得
1450          *
1451          * @return string                               CSS出力データ(HTMLタグ形式、または「/」から始まるCSSファイルの相対パス)。設定なしの場合は空文字列。
1452          */
1453         function getCustomTemplateHeadCssData()
1454         {
1455                 global $gEnvManager;
1456                 
1457                 if (!isset($this->templateCustomObj)){
1458                         $optionParams = $gEnvManager->getCurrentTemplateCustomParam();
1459                         if (empty($optionParams)){
1460                                 $this->templateCustomObj = array();
1461                         } else {
1462                                 $this->templateCustomObj = unserialize($optionParams);          // 連想配列に変換
1463                         }
1464                 }
1465                 return $this->templateCustomObj['head_css_data'];
1466         }
1467         /***********************************************************************************
1468          * 以下、Joomla!v1.5テンプレート専用
1469          ***********************************************************************************/
1470         /**
1471          * ウィジェット数を取得
1472          *
1473          * @param string $pos           ポジション
1474          * @return int                          ウィジェット数
1475          */
1476         function countModules($pos)
1477         {
1478                 $count = $this->gPage->getWidgetsCount($pos);
1479                 return $count;
1480         }
1481         function getBuffer($type = null, $name = null, $attribs = array())
1482         {
1483                 if (isset($this->joomlaBufArray[$type])){
1484                         return $this->joomlaBufArray[$type];
1485                 } else {
1486                         return '';
1487                 }
1488         }
1489         function setBuffer($contents, $type, $name = null)
1490         {
1491                 $this->joomlaBufArray[$type] = $contents;
1492                 return;
1493         }
1494         /**
1495          * 出力タイプ取得
1496          *
1497          * @return string                               出力タイプ
1498          */
1499         function getType()
1500         {
1501                 return 'html';
1502         }
1503         /**
1504          * HTMLヘッダ情報取得
1505          *
1506          * @return array                                ヘッダ情報
1507          */
1508         function getHeadData()
1509         {
1510                 $data = array();
1511                 /*$data['title']                = $this->title;
1512                 $data['description']= $this->description;
1513                 $data['link']           = $this->link;
1514                 $data['metaTags']       = $this->_metaTags;
1515                 $data['links']          = $this->_links;
1516                 $data['styleSheets']= $this->_styleSheets;
1517                 $data['style']          = $this->_style;
1518                 $data['scripts']        = $this->_scripts;
1519                 $data['script']         = $this->_script;
1520                 $data['custom']         = $this->_custom;*/
1521                 return $data;
1522         }
1523         /**
1524          * BASEタグ設定用
1525          *
1526          * @return string                               ベースパス
1527          */
1528         function getBase()
1529         {
1530                 return '';
1531         }
1532          /**
1533          * Adds a linked script to the page
1534          *
1535          * @param       string  $url            URL to the linked script
1536          * @param       string  $type           Type of script. Defaults to 'text/javascript'
1537          * @access   public
1538          */
1539         function addScript($url, $type="text/javascript") {
1540                 $this->_scripts[$url] = $type;
1541         }
1542         /**
1543          * Adds a script to the page
1544          *
1545          * @access   public
1546          * @param       string  $content   Script
1547          * @param       string  $type   Scripting mime (defaults to 'text/javascript')
1548          * @return   void
1549          */
1550         function addScriptDeclaration($content, $type = 'text/javascript')
1551         {
1552                 if (!isset($this->_script[strtolower($type)])) {
1553                         $this->_script[strtolower($type)] = $content;
1554                 } else {
1555                         $this->_script[strtolower($type)] .= chr(13).$content;
1556                 }
1557         }
1558         /**
1559          * Adds a linked stylesheet to the page
1560          *
1561          * @param   string  $url      URL to the linked style sheet
1562          * @param   array   $options  Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9')
1563          * @param   array   $attribs  Array of attributes. Example: array('id' => 'stylesheet', 'data-test' => 1)
1564          *
1565          * @return  JDocument instance of $this to allow chaining
1566          *
1567          * @since   11.1
1568          * @deprecated 4.0  The (url, mime, media, attribs) method signature is deprecated, use (url, options, attributes) instead.
1569          */
1570         public function addStyleSheet($url, $options = array(), $attribs = array())
1571         {
1572                 // B/C before 3.7.0
1573                 if (is_string($options))
1574                 {
1575                         JLog::add('The addStyleSheet method signature used has changed, use (url, options, attributes) instead.', JLog::WARNING, 'deprecated');
1576
1577                         $argList = func_get_args();
1578                         $options = array();
1579                         $attribs = array();
1580
1581                         // Old mime type parameter.
1582                         if (!empty($argList[1]))
1583                         {
1584                                 $attribs['type'] = $argList[1];
1585                         }
1586
1587                         // Old media parameter.
1588                         if (isset($argList[2]) && $argList[2])
1589                         {
1590                                 $attribs['media'] = $argList[2];
1591                         }
1592
1593                         // Old attribs parameter.
1594                         if (isset($argList[3]) && $argList[3])
1595                         {
1596                                 $attribs = array_replace($attribs, $argList[3]);
1597                         }
1598                 }
1599
1600                 // Default value for type.
1601                 if (!isset($attribs['type']) && !isset($attribs['mime']))
1602                 {
1603                         $attribs['type'] = 'text/css';
1604                 }
1605
1606                 $this->_styleSheets[$url] = isset($this->_styleSheets[$url]) ? array_replace($this->_styleSheets[$url], $attribs) : $attribs;
1607
1608                 if (isset($this->_styleSheets[$url]['options']))
1609                 {
1610                         $this->_styleSheets[$url]['options'] = array_replace($this->_styleSheets[$url]['options'], $options);
1611                 }
1612                 else
1613                 {
1614                         $this->_styleSheets[$url]['options'] = $options;
1615                 }
1616
1617                 return $this;
1618         }
1619 }
1620 ?>