OSDN Git Service

シャットダウンイベント追加。
[magic3/magic3.git] / include / container / baseFrameContainer.php
index 8f29b83..b16c68b 100644 (file)
@@ -8,7 +8,7 @@
  *
  * @package    Magic3 Framework
  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
- * @copyright  Copyright 2006-2013 Magic3 Project.
+ * @copyright  Copyright 2006-2017 Magic3 Project.
  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
  * @version    SVN: $Id$
  * @link       http://www.magic3.org
@@ -19,14 +19,15 @@ require_once(M3_SYSTEM_INCLUDE_PATH . '/common/core.php');
 class BaseFrameContainer extends Core
 {
        protected $_db; // DB接続オブジェクト
-       //const ADMIN_TEMPLATE = '_admin';              // 管理画面用テンプレート
+       private $joomlaBufArray = array();                      // Joomla!データ受け渡し用
        const SYSTEM_TEMPLATE = '_system';              // システム画面用テンプレート
        const M_ADMIN_TEMPLATE = 'm/_admin';    // 携帯用管理画面テンプレート
        const ERR_MESSAGE_ACCESS_DENY = 'Access denied.';               // ウィジェットアクセスエラーのメッセージ
        const SITE_ACCESS_EXCEPTION_IP = 'site_access_exception_ip';            // アクセス制御、例外とするIP
        const CONFIG_KEY_MSG_TEMPLATE = 'msg_template';                 // メッセージ用テンプレート取得キー
 //     const CF_MOBILE_AUTO_REDIRECT = 'mobile_auto_redirect';         // 携帯の自動遷移
-       
+       const TEMPLATE_GENERATOR_THEMLER = 'themler';                   // テンプレート作成アプリケーション(Themler)
+               
        /**
         * コンストラクタ
         */
@@ -52,13 +53,36 @@ class BaseFrameContainer extends Core
                // 最小限の設定が行われていない場合,DBに接続できない場合は、インストール画面へ
                if (!defined('M3_STATE_IN_INSTALL')){
                        if (($this->gEnv->canUseDb() && $this->gSystem->canInitSystem()) ||             // システム初期化モードのとき
-                               !$this->gConfig->isConfigured() ||                                                                      // 設定ファイルに設定がないとき
-                               !$this->gEnv->canUseDb()){                                                                                      // DBがまだ作成されていないとき
+                               !$this->gConfig->isConfigured()){                                                                       // 設定ファイルに設定がないとき(初回インストール)
                                
-                               // インストーラファイルがない場合は回復
-                               $this->gInstance->getFileManager()->recoverInstaller();
+                               // 旧システムがある場合はadminディレクトリまでアクセスしてインストーラを実行。
+                               $isRedirect = false;
+                               if ($this->_isExistsOldSystemDir()){            // ディレクトリアクセス権がない場合も旧システムが存在するものとしてリダイレクトを行う
+                                       if ($this->gEnv->isAdminDirAccess()) $isRedirect = true;
+                               } else {
+                                       $isRedirect = true;
+                               }
+                               
+                               if ($isRedirect){
+                                       // インストーラファイルがない場合は回復
+                                       $this->gInstance->getFileManager()->recoverInstaller();
 
-                               $this->gPage->redirectToInstall();
+                                       $this->gPage->redirectToInstall();
+                               } else {
+                                       // サイト非公開(システムメンテナンス)表示
+                                       $this->gPage->showError(503);
+                               }
+                               return;
+                       } else if ($this->gConfig->isConfigured() && !$this->gEnv->canUseDb()){         // DB接続失敗のとき
+                               if ($this->gEnv->isAdminDirAccess()){           // 管理画面の場合のみインストーラ起動
+                                       // インストーラファイルがない場合は回復
+                                       $this->gInstance->getFileManager()->recoverInstaller();
+
+                                       $this->gPage->redirectToInstall();
+                               } else {
+                                       // サーバ内部エラーメッセージ表示
+                                       $this->gPage->showError(500);
+                               }
                                return;
                        }
                }
@@ -111,16 +135,32 @@ class BaseFrameContainer extends Core
                        $errMessage = '';       // エラーメッセージ
                        $messageDetail = '';    // 詳細メッセージ
                        
-                       // ページID,ページサブIDからアクセス権をチェック
-                       $isPublicUrl = $this->gPage->canAccessUrl($isActivePage);
-                       if (!$isPublicUrl && !$isSystemManageUser){// システム運用可能ユーザかどうか
+                       // ページID,ページサブID以外のURLパラメータをチェック。ページマネージャーでの処理(startPage())の結果を反映。
+                       $ret = $this->gPage->isSystemPage();            // システム制御ページへ遷移するかどうか
+                       if ($ret){
+                               // ページが見つかりません画面へ遷移
                                $canAccess = false;
-                               $isErrorAccess = true;          // 不正アクセスかどうか
-                               $errMessage = 'ユーザに公開されていないページへのアクセス。';
+                               $toAdminType = 4;
+                       }
+                       
+                       // ページID,ページサブIDからアクセス権をチェック
+                       if ($canAccess){
+                               $isPublicUrl = $this->gPage->canAccessUrl($isActivePage, $errCode);
+                               if (!$isPublicUrl && !$isSystemManageUser){// システム運用可能ユーザかどうか
+                                       $canAccess = false;
+                                       $isErrorAccess = true;          // 不正アクセスかどうか
+                                       $errMessage = 'ユーザに公開されていないページへのアクセス。';
                                
-                               if (!$isActivePage) $toAdminType = 3;           // 有効なアクセスポイントでない場合は存在しないページとする
+                                       if (!$isActivePage){
+                                               if ($errCode == 1){                     // ページIDが不正な場合
+                                                       $toAdminType = 4;
+                                               } else {
+                                                       $toAdminType = 3;               // 有効なアクセスポイント、ページでない場合は存在しないページとする
+                                               }
+                                       }
+                               }
                        }
-
+                       
                        // ################### ユーザアクセス制御 ######################
                        // クッキーがないため権限を識別できない場合でも、管理者として処理する場合があるので、サブクラスの_checkAccess()メソッドは必ず通るようにする
                        if ($canAccess){                // アクセス可能な場合はユーザをチェック
@@ -128,7 +168,7 @@ class BaseFrameContainer extends Core
                                        // 管理画面へのアクセスを制御
                                        $canAccess = $this->_checkAccess($request);             // サブクラスメソッドの呼び出し
                                        
-                                       // 一般画面から直接管理画面が呼ばれた場合は一旦ログインへ遷移
+                                       // フロント画面から直接管理画面が呼ばれた場合は一旦ログインへ遷移
                                        if (!$canAccess && 
                                                ($cmd == M3_REQUEST_CMD_CONFIG_WIDGET ||                                                // ウィジェットの設定
                                                $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET)){                     // 表示位置を表示するとき(ウィジェット付き)
@@ -136,7 +176,7 @@ class BaseFrameContainer extends Core
                                                $toAdminType = 1;               // ログイン画面へ
                                        }
                                } else {                        // _checkAccess()がないときは、標準のアクセス制御
-                                       // 一般画面へのアクセスを制御
+                                       // フロント画面へのアクセスを制御
                                        $canAccess = $this->_accessSite($request);              // サイト公開制御
                                        if ($canAccess){
                                                if ($cmd == M3_REQUEST_CMD_LOGIN ||                                     // ログイン画面を表示のとき
@@ -152,7 +192,8 @@ class BaseFrameContainer extends Core
                                                        $cmd != M3_REQUEST_CMD_FIND_WIDGET &&                           // ウィジェットを検索
                                                        $cmd != M3_REQUEST_CMD_DO_WIDGET &&                                     // ウィジェット単体実行
                                                        $cmd != M3_REQUEST_CMD_PREVIEW &&                                       // サイトのプレビューを表示
-                                                       $cmd != M3_REQUEST_CMD_RSS){            // RSS配信
+                                                       $cmd != M3_REQUEST_CMD_RSS &&                                           // RSS配信
+                                                       $cmd != M3_REQUEST_CMD_CSS){                                            // CSS生成
                                                        
                                                        // 標準のアクセスでは、上記コマンド以外は受け付けない
                                                        $canAccess = false;
@@ -179,7 +220,8 @@ class BaseFrameContainer extends Core
                                                        $cmd != M3_REQUEST_CMD_FIND_WIDGET &&                           // ウィジェットを検索
                                                        $cmd != M3_REQUEST_CMD_DO_WIDGET &&                                     // ウィジェット単体実行
                                                        $cmd != M3_REQUEST_CMD_PREVIEW &&                                       // サイトのプレビューを表示
-                                                       $cmd != M3_REQUEST_CMD_RSS){            // RSS配信
+                                                       $cmd != M3_REQUEST_CMD_RSS &&                                           // RSS配信
+                                                       $cmd != M3_REQUEST_CMD_CSS){                                            // CSS生成
                                                        
                                                        $isErrorAccess = true;          // 不正アクセス
                                                        $errMessage = '不正なコマンドの実行。';
@@ -216,6 +258,10 @@ class BaseFrameContainer extends Core
                                                        $this->gPage->setSystemHandleMode(10/*サイト非公開中*/);
                                                }
                                                break;
+                                       case 4:         // 不正なページIDの指定
+                                               $messageDetail = '不正なページIDの指定';
+                                               $this->gPage->setSystemHandleMode(12/*存在しないページ*/);
+                                               break;
                                        default:                // アクセス不可画面へ
                                                // システム制御モードに変更
                                                $this->gPage->setSystemHandleMode(11/*アクセス不可*/);
@@ -225,7 +271,7 @@ class BaseFrameContainer extends Core
                                $this->_showSystemPage($request, $toAdminType);
                                                
                                // 不正アクセスの場合は、アクセスエラーログを残す
-                               if ($isErrorAccess) $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2200, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
+                               if ($isErrorAccess) $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2201, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
                                return;
                        }
                        // #################### URLの遷移 #######################
@@ -258,12 +304,12 @@ class BaseFrameContainer extends Core
                        if ($isSystemAdmin){
                                // ポジションの表示画面のアクセスは、すべて管理機能URLで受け付ける
                                // ページIDを再設定
-                               $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
+/*                             $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
                                if (empty($pageId)) $pageId = $this->gEnv->getDefaultPageId();          // 値がないときはデフォルトのページIDを設定
                                $this->gEnv->setCurrentPageId($pageId);
                                $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
                                if (!empty($pageSubId)) $this->gEnv->setCurrentPageSubId($pageSubId);
-                               
+                       */
                                $this->gPage->showPosition(1);                  // ポジションを表示
                        } else {
                                return;
@@ -274,12 +320,12 @@ class BaseFrameContainer extends Core
                        if ($isSystemAdmin){
                                // ポジションの表示画面のアクセスは、すべて管理機能URLで受け付ける
                                // ページIDを再設定
-                               $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
+/*                             $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
                                if (empty($pageId)) $pageId = $this->gEnv->getDefaultPageId();          // 値がないときはデフォルトのページIDを設定
                                $this->gEnv->setCurrentPageId($pageId);
                                $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
                                if (!empty($pageSubId)) $this->gEnv->setCurrentPageSubId($pageSubId);
-                               
+                               */
                                $this->gPage->showPosition(2);                  // ウィジェット付きポジションを表示
                        } else {
                                return;
@@ -309,6 +355,8 @@ class BaseFrameContainer extends Core
                        if (!empty($openBy)) $this->gPage->showWidget();        // ウィジェット表示
                } else if ($cmd == M3_REQUEST_CMD_RSS){         // RSS配信
                        $createPage = false;            // ウィジェット単体処理モードに設定
+               } else if ($cmd == M3_REQUEST_CMD_CSS){         // CSS生成
+               
                } else if ($this->gEnv->isServerConnector()){           // サーバ接続の場合
                        $createPage = false;            // ウィジェット単体処理モードに設定
                }
@@ -323,41 +371,44 @@ class BaseFrameContainer extends Core
                        
                        if (empty($cacheData)){         // キャッシュデータがないときは画面を作成
                                // カレントのテンプレートを決定
-                               $curTemplateId = $this->_defineTemplate($request);
-               
+                               $curTemplateId = $this->_defineTemplate($request, $subTemplateId);
+
                                // 画面を作成
-                               $pageData = $this->_createPage($request, $curTemplateId);
+                               $pageData = $this->_createPage($request, $curTemplateId, $subTemplateId);
                                
-                               // 使用した非共通ウィジェットの数をチェック
+                               // ##### 非共通ウィジェットがページ上になくてもエラーとしない #####
+                               $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
+                               echo $pageData;
+                                       
+/*                             // 使用した非共通ウィジェットの数をチェック
                                $nonSharedWidgetCount = $this->gPage->getNonSharedWidgetCount();
                                if ($nonSharedWidgetCount == -1){               // カウントなしの場合
                                        $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
                                        echo $pageData;
                                } else {
+                                       // ***** WordPressテンプレートの場合は非共通のウィジェットが使用されていなくても表示可とする *****
+                                       if ($this->gEnv->getCurrentTemplateType() == 100) $nonSharedWidgetCount = 1;
+                                       
                                        if ($isSystemAdmin || $nonSharedWidgetCount > 0){
                                                $this->gCache->setPageCache($request, $pageData);               // キャッシュデータを設定
                                                echo $pageData;
                                        } else {                // 管理者以外で、非共通のウィジェットが使用されていないページはアクセス不可とする
                                                $errMessage = 'ユーザに公開されていないページへのアクセス。';
-                                               $messageDetail = 'アクセスポイント状態=公開, 要因: 共有ウィジェットのみのページへのアクセスは不可。';
-                                               $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2200, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
+                                               $messageDetail = 'アクセスポイント状態=公開, 要因: グローバルウィジェットのみのページへのアクセスはできません。ページには1つ以上のローカルウィジェットが必要です。';
+                                               $this->gOpeLog->writeUserAccess(__METHOD__, '不正なアクセスを検出しました。' . $errMessage, 2202, 'アクセスをブロックしました。URL: ' . $this->gEnv->getCurrentRequestUri() . ', ' . $messageDetail);
 
                                                // アクセス不可ページへ遷移
                                                $this->gPage->redirect('?' . M3_REQUEST_PARAM_PAGE_SUB_ID . '=_accessdeny');
-                                               // システム制御モードに変更
-                                               //$this->gPage->setSystemHandleMode(11/*アクセス不可*/);
-
-                                               // システム制御画面を表示
-                                               //$this->_showSystemPage($request, 0/*アクセス不可画面*/);
                                        }
-                               }
+                               }*/
                        } else {
                                echo $cacheData;
                        }
                        
-                       // オプション出力(時間計測等)追加
-                       echo $this->gPage->getOptionContents($request);
-                       
+                       if ($cmd != M3_REQUEST_CMD_CSS){                // 画面出力(CSS生成以外)のとき
+                               // オプション出力(時間計測等)追加
+                               echo $this->gPage->getOptionContents($request);
+                       }
                } else {                // ウィジェット単体実行モードのとき
                        // ###################ウィジェット指定で出力の場合####################
                        // ウィジェット単体を直接実行するインターフェイスで、HTTPヘッダは送信しない。
@@ -371,7 +422,11 @@ class BaseFrameContainer extends Core
                        // ウィジェット単体オペレーションのときは、ウィジェット情報の単体実行許可があるかどうか判断(管理権限にかかわらず同じ動作)
                        if ($cmd == M3_REQUEST_CMD_DO_WIDGET ||         // ウィジェット単体実行
                                $cmd == M3_REQUEST_CMD_RSS){            // RSS配信
-                               if ($this->_db->getWidgetInfo($widgetId, $row)){
+                               if (empty($widgetId)){
+                                       $this->gOpeLog->writeUserAccess(__METHOD__, 'ウィジェットIDが設定されていません。', 2200,
+                                               '実行処理はキャンセルされました。');
+                                       return;
+                               } else if ($this->_db->getWidgetInfo($widgetId, $row)){                 // ウィジェット情報取得
                                        if ($cmd == M3_REQUEST_CMD_DO_WIDGET && !$row['wd_enable_operation']){  // ウィジェット単体実行
                                                // アクセスエラーのログを残す
                                                $this->_db->writeWidgetLog($widgetId, 1/*単体実行*/, $cmd, self::ERR_MESSAGE_ACCESS_DENY);
@@ -407,7 +462,7 @@ class BaseFrameContainer extends Core
                        // ################# パラメータチェック ################
                        if (!$isSystemManageUser && !$this->gAccess->isValidAdminKey() && $this->gEnv->isServerConnector()){            // サーバ接続の場合
                                // クエリーパラメータはウィジェットIDのみ正常とする
-                               $params = $this->gRequest->getQueryArray();
+                               $params = $request->getQueryArray();
                                $paramCount = count($params);
                                if (!($paramCount == 1 && !empty($params[M3_REQUEST_PARAM_WIDGET_ID]))){
                                        // アクセスエラーのログを残す
@@ -444,7 +499,7 @@ class BaseFrameContainer extends Core
                                $this->gPage->startWidgetXml($cmd);
 
                                // 指定のウィジェットを実行
-                               $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/index.php';
+                               $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
 
                                if (file_exists($widgetIndexFile)){
                                        // 実行のログを残す
@@ -458,13 +513,13 @@ class BaseFrameContainer extends Core
                                // ウィジェット用のタグを閉じる
                                $this->gPage->endWidgetXml($cmd);
                        } else if ($cmd == M3_REQUEST_CMD_RSS){         // RSS配信のとき
+                               ob_start();// バッファ作成
+                               
                                // ウィジェット用のHTMLヘッダを出力
                                $this->gPage->startWidgetRss($cmd);
-
-                               ob_start();// バッファ作成
                                
                                // 指定のウィジェットを実行
-                               $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/index.php';
+                               $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
 
                                if (file_exists($widgetIndexFile)){
                                        // ウィジェット定義ID、ページ定義のシリアル番号を取得
@@ -502,14 +557,16 @@ class BaseFrameContainer extends Core
                                // ウィジェット用のタグを閉じる
                                $this->gPage->endWidgetRss($cmd, $content);
                        } else {                // RSS配信以外のとき
+                               ob_start();// バッファ作成
+                                                       
                                // ウィジェット用のHTMLヘッダを出力
                                $this->gPage->startWidget($cmd);
-
+                               
                                // 指定のウィジェットを実行
                                if ($cmd == M3_REQUEST_CMD_CONFIG_WIDGET){              // ウィジェット設定のとき
-                                       $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/admin/index.php';                // 管理用画面
+                                       $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/admin/' . M3_FILENAME_INDEX;             // 管理用画面
                                } else {
-                                       $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/index.php';
+                                       $widgetIndexFile = $this->gEnv->getWidgetsPath() . '/' . $widgetId . '/' . M3_FILENAME_INDEX;
                                }
                                if (file_exists($widgetIndexFile)){
                                        // ウィジェット定義ID、ページ定義のシリアル番号を取得
@@ -540,8 +597,12 @@ class BaseFrameContainer extends Core
                                        echo 'file not found: ' . $widgetIndexFile;
                                }
                        
+                               // 現在のバッファ内容を取得し、バッファを破棄
+                               $content = ob_get_contents();
+                               ob_end_clean();
+                               
                                // ウィジェット用のタグを閉じる
-                               $this->gPage->endWidget($cmd);
+                               $this->gPage->endWidget($cmd, $content);
                        }
                
                        // 作業中のウィジェットIDを解除
@@ -558,7 +619,7 @@ class BaseFrameContainer extends Core
                                $srcContents = ob_get_contents();
                                ob_clean();
                                
-                               // 追加変換処理
+                               // 追加変換処理。HTMLヘッダ出力する。
                                $destContents = $this->gPage->lateLaunchWidget($request, $srcContents);
                                
                                echo $destContents;
@@ -589,12 +650,17 @@ class BaseFrameContainer extends Core
         *
         * @param RequestManager $request               HTTPリクエスト処理クラス
         * @param string $curTemplate                   テンプレートID
+        * @param string $subTemplateId                 サブページID
         * @return string                                               画面出力
         */
-       function _createPage($request, $curTemplate)
+       function _createPage($request, $curTemplate, $subTemplateId = '')
        {
+               $defaultIndexFile = M3_FILENAME_INDEX;                  // テンプレートの起動ファイル
+               
+               $cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);               // 実行コマンドを取得
+               
                // カレントのテンプレートIDを設定
-               $this->gEnv->setCurrentTemplateId($curTemplate);
+               $this->gEnv->setCurrentTemplateId($curTemplate, $subTemplateId);
 
                // テンプレート情報を取得
                $convType = 0;          // 変換処理タイプ(0=デフォルト(Joomla!v1.0)、-1=携帯用、1=Joomla!v1.5、2=Joomla!v2.5)
@@ -621,7 +687,332 @@ class BaseFrameContainer extends Core
                // サブクラスの前処理を実行
                if (method_exists($this, '_preBuffer')) $this->_preBuffer($request);
        
-               if ($convType >= 1){            // Joomla!v1.5,v2.5テンプレートのとき
+               if ($convType == 100){          // WordPressテンプレートのとき
+                       // WordPress用ベース定義値
+                       define('WP_DEBUG', true);                       // ##### エラーメッセージ表示制御(true時noticeを表示) #####
+                       define('WPINC', 'wp-includes');
+                       define('ABSPATH', $this->gEnv->getWordpressRootPath() . '/' );
+                       define('TEMPLATEPATH', $this->gEnv->getTemplatesPath() . '/' . $curTemplate);
+                       define('STYLESHEETPATH', $this->gEnv->getTemplatesPath() . '/' . $curTemplate);         // 子テンプレートを使用している場合は子テンプレートを示す。デフォルトはテンプレートを示す。
+                       define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
+                       define('WP_LANG_DIR', WP_CONTENT_DIR . '/languages');
+                       define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
+                       define('WP_CONTENT_URL', '/wp-content');                                // 定義自体は必要であるが使用しないのでダミー値で定義
+                       define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins');   // 定義自体は必要であるが使用しないのでダミー値で定義
+                       
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/load.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/default-constants.php');               // デフォルト値取得
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/plugin.php');
+
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/functions.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/default-filters.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/l10n.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-error.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-locale.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-user.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-walker.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-query.php');                          // コンテンツデータ取得
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-comment-query.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-term.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-page.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-theme.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-widget.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-widget-factory.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-list-util.php');
+//                     require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-nav-menu.php');
+//                     require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-dependency.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-post.php');                   // コンテンツAPIマネージャーからWP_Post型でデータを取得
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-post-type.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-embed.php');
+
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/query.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pluggable.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/user.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/widgets.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/comment.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/http.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/kses.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/script-loader.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/theme.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/link-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/category.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/category-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-thumbnail-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/comment-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/author-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/nav-menu-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/nav-menu.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/general-template.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/cache.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/shortcodes.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/formatting.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/post-formats.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/taxonomy.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/media.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/embed.php');
+//                     require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/option.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pomo/translations.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/pomo/mo.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/capabilities.php');
+                       require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/meta.php');
+               
+                       // Magic3用インターフェイス
+                       require_once($this->gEnv->getWordpressRootPath() . '/wpInit.php');              // 初期値取得
+                       require_once($this->gEnv->getWordpressRootPath() . '/contentApi.php');  // コンテンツ取得API
+                       require_once($this->gEnv->getWordpressRootPath() . '/menuApi.php');             // メニュー情報取得API
+                       require_once($this->gEnv->getWordpressRootPath() . '/WPRender.php');            // ウィジェット描画クラス
+
+                       // ##### データ初期化 #####
+                       wp_initial_constants();                 // WordPressその他定義値設定
+                       wp_cookie_constants();                  // クッキー用定義
+                       create_initial_post_types();    // WP_Post型データ型登録
+                       register_shutdown_function('shutdown_action_hook');             // 終了時イベント登録
+                       
+                       // プラグイン初期化
+                       $GLOBALS['wp_plugin_paths'] = array();                  // $wp_plugin_pathsは未使用?
+                       foreach (wp_get_active_and_valid_plugins() as $plugin) {// プラグインロード
+                               wp_register_plugin_realpath($plugin);
+                               include_once($plugin);
+                       }
+                       unset($plugin);
+
+                       // WordPressメインオブジェクト作成
+                       $GLOBALS['wp_version'] = '4.7.0';                       // 下位互換性チェックで引っかかるのでv4.7.0に定める
+                       $GLOBALS['locale'] = $this->gEnv->getCurrentLanguage();         // 表示言語を設定
+                       $GLOBALS['wp_embed'] = new WP_Embed();
+                       $GLOBALS['wp_the_query'] = new WP_Query();                              // $wp_the_queryは変更不可変数で$wp_queryは変更可変数
+                       $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
+                       $GLOBALS['wp'] = new WP();
+                       $GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
+                       $GLOBALS['gContentApi'] = new contentApi();                     // Magic3コンテンツAPIオブジェクト
+                       $GLOBALS['gMenuApi'] = new menuApi();                   // Magic3メニュー情報APIオブジェクト
+                       $GLOBALS['m3WpOptions'] = array();                              // 定義値初期化
+                       // テンプレートから参照可能にする
+                       global $wp_query;
+
+                       // ページに配置されているウィジェットの状況からWordPress以外の主コンテンツ用のプラグイン(WooCommerce等)をロード
+                       // setup_themeイベント処理を設定
+                       $GLOBALS['gContentApi']->loadPlugin();
+                       
+                       // テンプレート初期処理
+                       do_action('setup_theme');
+                       load_default_textdomain();                      // 言語リソースを読み込む
+                       m3WpInit();                                                     // 言語リソース読み込み後にMagic3用インターフェイス初期化。$GLOBALS['m3WpOptions']を初期化し、get_option()はここから使用可能にする。
+                       $GLOBALS['wp_locale'] = new WP_Locale();                // 言語リソース読み込み後に生成
+
+                       // functions.phpを読み込む。ファイル内で定義されている変数はグローバル変数に変換する。
+                       $this->_loadFileAsGlobal(TEMPLATEPATH . '/functions.php');
+
+                       do_action('after_setup_theme');         // wp-multibyte-patchプラグイン読み込み
+                       do_action('init');                                      // テンプレート側からの初期処理(ウィジェットのCSSの初期化等)
+                       do_action('wp_loaded');
+                       
+                       // ##### 起動PHPファイル取得。データ取得用パラメータ設定。#####
+                       // URLパラメータからコンテンツ形式を取得し、ページを選択
+                       $params = $request->getQueryArray();
+                       $paramCount = count($params);
+                       reset($params);
+                       $firstKey = key($params);
+//                     $firstValue = $params[$firstKey];
+                       
+                       // コンテンツタイプに合わせて起動PHPファイルを決める。デフォルトはindex.phpで一覧形式で表示。
+                       $pageTypeDefined = false;               // ページタイプ確定したかどうか
+                       $wpIndexFile = get_index_template();            // WordPress用テンプレート起動ファイル
+                       $contentType = $GLOBALS['gContentApi']->getContentType();
+                       switch ($contentType){
+                       case M3_VIEW_TYPE_CONTENT:              // 汎用コンテンツ
+                               if ($firstKey == M3_REQUEST_PARAM_CONTENT_ID || $firstKey == M3_REQUEST_PARAM_CONTENT_ID_SHORT){        // コンテンツIDのとき
+                                       // ページタイプを設定
+                                       $GLOBALS['gContentApi']->setPageType('page');
+                                       
+                                       // フルパスで返るので相対パスに修正
+//                                     $defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_page_template());            // 固定ページテンプレート
+                                       $wpIndexFile = get_page_template();             // 固定ページテンプレート
+                                       
+                                       // コンテンツID設定
+                                       $firstValue = $request->trimValueOf($firstKey);
+                                       $GLOBALS['gContentApi']->setContentId($firstValue);
+                                       
+                                       $pageTypeDefined = true;                // ページタイプ確定
+                               }
+                               break;
+                       case M3_VIEW_TYPE_PRODUCT:      // 製品
+                               if ($firstKey == M3_REQUEST_PARAM_PRODUCT_ID || $firstKey == M3_REQUEST_PARAM_PRODUCT_ID_SHORT){                // 製品IDのとき
+                                       // ページタイプを設定
+                                       $GLOBALS['gContentApi']->setPageType('single');
+                                       
+                                       // フルパスで返るので相対パスに修正
+                                       //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_single_template());                // 記事詳細テンプレート
+                                       $wpIndexFile = get_single_template();           // 記事詳細テンプレート
+                                       
+                                       // コンテンツID設定
+                                       $firstValue = $request->trimValueOf($firstKey);
+                                       $GLOBALS['gContentApi']->setContentId($firstValue);
+                                       
+                                       $pageTypeDefined = true;                // ページタイプ確定
+                               }
+                               break;
+                       case M3_VIEW_TYPE_BBS:  // BBS
+                               break;
+                       case M3_VIEW_TYPE_BLOG: // ブログ
+                               if ($firstKey == M3_REQUEST_PARAM_BLOG_ENTRY_ID || $firstKey == M3_REQUEST_PARAM_BLOG_ENTRY_ID_SHORT){          // ブログ記事IDのとき
+                                       // ページタイプを設定
+                                       $GLOBALS['gContentApi']->setPageType('single');
+                                       
+                                       // フルパスで返るので相対パスに修正
+                                       //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_single_template());                // 記事詳細テンプレート
+                                       $wpIndexFile = get_single_template();           // 記事詳細テンプレート
+                                       
+                                       // コンテンツID設定
+                                       $firstValue = $request->trimValueOf($firstKey);
+                                       $GLOBALS['gContentApi']->setContentId($firstValue);
+                                       
+                                       $pageTypeDefined = true;                // ページタイプ確定
+                               } else {
+                                       // カテゴリーが設定されている場合はカテゴリー画面を表示
+                                       $value = $request->trimValueOf(M3_REQUEST_PARAM_CATEGORY_ID);
+                                       if (!empty($value)){
+                                               // ページタイプを設定
+                                               $GLOBALS['gContentApi']->setPageType('category');                       // カテゴリー表示
+                               
+                                               // カテゴリー用テンプレート取得
+                                               $template = get_category_template();
+                                               if (empty($template)) $template = get_archive_template();               // カテゴリー用のテンプレートが取得できない場合はアーカイブ用テンプレートを取得
+                                               if (!empty($template)) $wpIndexFile = $template;
+
+                                               // フルパスで返るので相対パスに修正
+                                               //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $template);            // カテゴリーテンプレート
+                                               
+                                               $pageTypeDefined = true;                // ページタイプ確定
+                                       }
+                                       if (!$pageTypeDefined){
+                                               $year = $request->trimValueOf(M3_REQUEST_PARAM_YEAR);           // 年指定
+                                               $month = $request->trimValueOf(M3_REQUEST_PARAM_MONTH);         // 月指定
+                                               $day = $request->trimValueOf(M3_REQUEST_PARAM_DAY);             // 日指定
+
+                                               if (!empty($year)){                     // 年月日指定のとき
+                                                       // ページタイプを設定
+                                                       $GLOBALS['gContentApi']->setPageType('date');                   // 年月日表示
+                               
+                                                       // 年月日用テンプレート取得
+                                                       $template = get_date_template();
+                                                       if (empty($template)) $template = get_archive_template();               // 年月日用のテンプレートが取得できない場合はアーカイブ用テンプレートを取得
+                                                       if (!empty($template)) $wpIndexFile = $template;
+                                               
+                                                       // フルパスで返るので相対パスに修正
+                                                       //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $template);
+
+                                                       $pageTypeDefined = true;                // ページタイプ確定
+                                               }
+                                       }
+                                       // 検索条件が設定されている場合は検索画面を表示
+                                       if (!$pageTypeDefined){
+                                               $value = $request->trimValueOf('s');
+                                               if (!empty($value)){
+                                                       // ページタイプを設定
+                                                       $GLOBALS['gContentApi']->setPageType('search');                 // 検索結果表示
+                                       
+                                                       // フルパスで返るので相対パスに修正
+                                                       //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_search_template());                // 検索結果テンプレート
+                                                       $template = get_search_template();              // 検索結果テンプレート
+                                                       if (!empty($template)) $wpIndexFile = $template;
+                                                       
+                                                       $pageTypeDefined = true;                // ページタイプ確定
+                                               }
+                                       }
+                               }
+                               break;
+                       case M3_VIEW_TYPE_WIKI: // Wiki
+                               break;
+                       case M3_VIEW_TYPE_USER: // ユーザ作成コンテンツ
+                               break;
+                       case M3_VIEW_TYPE_EVENT:        // イベント
+                               break;
+                       case M3_VIEW_TYPE_PHOTO:        // フォトギャラリー
+                               break;
+                       default:
+                               // 検索キーワードが設定されている場合は検索結果画面を表示
+                               $value = $request->trimValueOf('s');
+                               if (!empty($value)){
+                                       // ページ指定されていない場合(フロント画面等)、検索結果表示用ページがある場合はリダイレクト
+                                       $subId = $request->trimValueOf(M3_REQUEST_PARAM_PAGE_SUB_ID);
+                                       if (empty($subId)){
+                                               $subId = $this->_db->getSubPageIdWithContent(M3_VIEW_TYPE_SEARCH, $this->gEnv->getCurrentPageId());// ページサブIDを取得
+                                               if (!empty($subId)){
+                                                       // リダイレクト用URLを作成
+                                                       $redirectUrl = $this->gEnv->createPageUrl();
+                                                       $redirectUrl .= '?' . M3_REQUEST_PARAM_PAGE_SUB_ID . '=' . $subId;
+                                                       //$redirectUrl .= '&s=' . urlencode($value);                                                    // 検索キーワード
+                                                       $redirectUrl .= '&' . M3_REQUEST_PARAM_OPERATION_TASK . '=search&' . M3_REQUEST_PARAM_KEYWORD . '=' . urlencode($value);                // 検索キーワード
+                                                       
+                                                       $this->gPage->redirect($redirectUrl);
+                                                       return;                         // ここで終了
+                                               }
+                                       }
+//                             } else {
+//                                     // 検索キーワードが設定されている場合は画面タイトルを設定
+//                                     $value = $request->trimValueOf(M3_REQUEST_PARAM_KEYWORD);
+//                                     if (!empty($value)){
+//                                     }
+                               }
+                               
+                               // ##### デフォルトのページタイトルを設定 #####
+                               $pageInfo = $this->gPage->getCurrentPageInfo();
+                               if (!empty($pageInfo)){
+                                       $pageTitle = $pageInfo['pg_name'];
+                                       if (!empty($pageTitle)) $GLOBALS['gContentApi']->setPostTitle($pageTitle);
+                               }
+                               
+                               // コンテンツタイプが設定されていないページ(お問合わせページ等)に場合は、固定ページ用のテンプレートを使用
+                               // ページタイプを設定
+                               $GLOBALS['gContentApi']->setPageType('page');
+                               
+                               // フルパスで返るので相対パスに修正
+                               //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_page_template());          // 固定ページテンプレート
+                               $wpIndexFile = get_page_template();             // 固定ページテンプレート
+                       
+                               $pageTypeDefined = true;                // ページタイプ確定
+                               break;
+                       }
+                       
+                       // コンテンツタイプが設定されているページでページタイプが設定されていないページの場合はデフォルトテンプレート(index.php)の代わりにホーム用テンプレートを取得
+                       if (!empty($contentType) && !$pageTypeDefined){
+                               // フルパスで返るので相対パスに修正
+                               //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, get_home_template());          // ホーム用テンプレート
+                               $wpIndexFile = get_home_template();             // ホーム用テンプレート
+                       }
+
+                       // サイトのトップページを表示する場合(コンテンツタイプが設定されていないページをデフォルトで表示する場合)は優先してフロント用テンプレートを表示
+                       if (empty($contentType)){
+                       //if (!$GLOBALS['gContentApi']->isHomeUrl()){
+//                     if ($defaultIndexFile == M3_FILENAME_INDEX){            // テンプレートの起動ファイル
+                               $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_PAGE_SUB_ID);
+                               if ($this->gEnv->getCurrentPageSubId() == $this->gEnv->getDefaultPageSubId() && empty($pageSubId)){             // デフォルトページを表示し「sub」なしに限定
+                                       $frontPageTemplate = get_front_page_template();
+                                       if (!empty($frontPageTemplate)){
+                                               //$defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $frontPageTemplate);   // フロントページテンプレート
+                                               $wpIndexFile = $frontPageTemplate;              // フロントページテンプレート
+                                       }
+                               }
+                       }
+
+                       // プラグインからの起動ファイルパス変換
+                       $wpIndexFile = apply_filters('template_include', $wpIndexFile);
+
+                       // Magic3用のテンプレート起動ファイルパスに変換
+//                     $defaultIndexFile = $this->_getRelativeTemplateIndexPath($curTemplate, $wpIndexFile);
+                       $defaultIndexFile = $wpIndexFile;
+                       
+                       // ##### テンプレート前処理(起動ファイル決定後に実行) #####
+                       do_action('template_redirect');
+                                               
+                       // WordPressオブジェクト作成
+                       wp();
+               } else if ($convType >= 1){             // Joomla!v1.5,v2.5テンプレートのとき
                        global $mainframe;
                        require_once($this->gEnv->getJoomlaRootPath() . '/mosDef.php');// Joomla定義読み込み
                        require_once($this->gEnv->getJoomlaRootPath() . '/JParameter.php');
@@ -645,6 +1036,9 @@ class BaseFrameContainer extends Core
                                                
                        // Joomla!v1.5用の設定
                        define('JPATH_BASE', dirname(__FILE__));
+                       define('JPATH_SITE', $this->gEnv->getSystemRootPath());
+                       define('JPATH_PLUGINS', $this->gEnv->getJoomlaRootPath() . '/class/plugins');                   // プラグインパス
+//                     define('JPATH_THEMES', $this->gEnv->getTemplatesPath());                                                                // テンプレートパス             ## テンプレート内でエラーが発生するのでここでは定義しない(2015/10/13)
                        define('DS', DIRECTORY_SEPARATOR);
                        $this->language = $this->gEnv->getCurrentLanguage();
                        $this->template = $curTemplate;
@@ -652,6 +1046,19 @@ class BaseFrameContainer extends Core
                        $this->baseurl          = $this->gEnv->getRootUrlByCurrentPage();
                        $this->direction = 'ltr';
                        $this->params   = $params;
+                       
+                       // サブテンプレート用の設定
+                       if ($this->gEnv->getCurrentTemplateGenerator() == self::TEMPLATE_GENERATOR_THEMLER){            // Themlerテンプレートの場合はサブテンプレート用のパラメータを設定
+                               // JRequest経由でレンダー側にサブテンプレートIDを渡す
+                               if (!empty($subTemplateId)) JRequest::injectSetVar('file_template_name', $subTemplateId);
+
+                               // サブテンプレートIDの渡し方は以下の方法もある(Themlerテンプレート1.39以降はこちらが有効)
+                               // サブテンプレートIDを埋め込む
+                               if (!empty($subTemplateId)) $this->setBuffer('<!--TEMPLATE ' . $subTemplateId . ' /-->', 'component');
+                       }
+                       
+                       // 現在のJoomla!ドキュメントを設定
+                       $this->gEnv->setJoomlaDocument($this);
                } else {                        // デフォルト(Joomla!v1.0テンプレート)テンプレートのとき(PC用および携帯用)
                        // Joomla!テンプレート共通の設定
                        define('_JEXEC', 1);
@@ -670,18 +1077,23 @@ class BaseFrameContainer extends Core
                }
 
                // ################### テンプレート読み込み ###################
-               // テンプレートのポジションタグからウィジェットが実行される
-               $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplate . '/index.php';
+               // フルパスに変換
+               if (strStartsWith($defaultIndexFile, '/')){                             // フルパス起動(WordPressテンプレート)のとき
+                       $templateIndexFile = $defaultIndexFile;
+               } else {
+                       $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplate . '/' . $defaultIndexFile;
+               }
+               
                if (file_exists($templateIndexFile)){
                        require_once($templateIndexFile);
                } else {                // テンプレートが存在しないとき
                        if ($this->gEnv->isSystemManageUser()){         // システム管理ユーザのとき
-                               echo 'template not found error: ' . $curTemplate;
+                               echo 'template not found error: ' . $curTemplate . ', path=' . $templateIndexFile;
                        } else {
                                // 一般向けにはメンテナンス画面を表示
                                $this->gPage->setSystemHandleMode(10/*サイト非公開中*/);
                                $this->_showSystemPage($request, 2/*サイト非公開画面*/);// システム制御画面を表示
-                                               
+                                       
                                // 運用ログに記録(一度だけ出力したい)
                                //$this->gOpeLog->writeFatal(__METHOD__, 'テンプレートが存在しません。メンテナンス画面を表示します。(テンプレートID=' . $curTemplate . ')', 1100);
                                return;
@@ -691,16 +1103,21 @@ class BaseFrameContainer extends Core
                // サブクラスの後処理の呼び出し
                if (method_exists($this, '_postBuffer')) $this->_postBuffer($request);
 
+               // ##### WordPressテンプレートの場合は終了イベントを実行。WordPressのエラーメッセージは画面に出力させる。 #####
+               if ($convType == 100){          // WordPressテンプレートのとき
+                       do_action('shutdown');
+               }
+               
                // 現在の出力内容を取得し、一旦内容をクリア
                $srcContents = ob_get_contents();
                ob_clean();
 
                // Joomla!タグの変換処理(ウィジェット実行)
-               if ($convType >= 1){            // Joomla!v1.5,v2.5テンプレートのとき
+               if ($convType >= 1 && $convType < 100){         // Joomla!v1.5,v2.5テンプレートのとき
                        $srcContents = $this->gPage->launchWidgetByJoomlaTag($srcContents, $convType);
                }
        
-               // 遅延実行ウィジェットの出力を埋め込む
+               // 遅延実行ウィジェットの出力を埋め込む。HTMLヘッダ出力する。
                $destContents = $this->gPage->lateLaunchWidget($request, $srcContents);
 
                // 携帯インターフェイスのときのときは、手動変換後、バイナリコード(絵文字等)を埋め込む
@@ -714,6 +1131,9 @@ class BaseFrameContainer extends Core
                        }
                }
                
+               // ##### CSS生成の場合は、すべてのウィジェット実行後出力を削除する #####
+               if ($cmd == M3_REQUEST_CMD_CSS) $destContents = '';             // CSS生成のとき
+
                // ページ作成終了処理(HTTPヘッダ出力)
                $destContents .= $this->gPage->endPage($request, true/*出力を取得*/);              // 最終HTMLを追加
                if ($this->gPage->isRedirect()) return '';// リダイレクトの場合ob_end_clean()を実行すると、ログインできないことがあるのでここで終了(2011/11/11)
@@ -721,18 +1141,18 @@ class BaseFrameContainer extends Core
                // バッファを破棄
                //ob_end_flush();
                ob_end_clean();
-
+               
                // 送信データを返す
                return $destContents;
        }
        /**
         * テンプレートを決定
         *
-        * @param RequestManager $request               HTTPリクエスト処理クラス
-        * @param bool $useSubClassDefine               ã\82µã\83\96ã\82¯ã\83©ã\82¹ã\81§ã\81®å®\9a義ã\82\92使ç\94¨ã\81\99ã\82\8bã\81\8bã\81©ã\81\86ã\81\8b
-        * @return string               テンプレート名
+        * @param RequestManager $request       HTTPリクエスト処理クラス
+        * @param string $subTemplateId         ã\83\86ã\83³ã\83\97ã\83¬ã\83¼ã\83\88IDã\81\8cå\8f\96å¾\97ã\81§ã\81\8dã\82\8bã\81¨ã\81\8dã\81¯ã\82µã\83\96ã\83\9aã\83¼ã\82¸IDã\81\8cè¿\94ã\82\8b
+        * @return string                                       テンプレート名
         */
-       function _defineTemplate($request, $useSubClassDefine = true)
+       function _defineTemplate($request, &$subTemplateId)
        {
                // ########### テンプレートID(ディレクトリ名)を設定 ############
                // テンプレートIDの指定の方法は2パターン
@@ -744,6 +1164,9 @@ class BaseFrameContainer extends Core
                //  2.セッションに持っている値
                //  3.DBのデフォルト値
                $curTemplate = '';
+               $subTemplateId = '';
+               $isSystemManageUser = $this->gEnv->isSystemManageUser();                // システム運用可能かどうか
+               $useSubClassDefine = true;                      // サブクラスでの定義を使用するかどうか
                
                // テンプレート変更のときは、セッションのテンプレートIDを変更
                $cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);               // 実行コマンドを取得
@@ -766,74 +1189,88 @@ class BaseFrameContainer extends Core
 
                // セッションにあるときは、セッションの値を使用(携帯でないとき)
                $pageId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_ID);
-               if ($curTemplate == ''){
+               if (empty($curTemplate)){
                        if ($cmd == M3_REQUEST_CMD_SHOW_POSITION ||                             // 表示位置を表示するとき
                                $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET){      // 表示位置を表示するとき(ウィジェット付き)
-                               
                                // URLの引数として、ページIDとページサブIDが指定されてくる
-                               if ($pageId == $this->gEnv->getDefaultPageId()){                // 通常サイトのとき
-                                       $curTemplate = $this->gSystem->defaultTemplateId();
-
-                                       // URLの引数でテンプレートIDが指定されている場合は設定
-                                       $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
-                                       if (!empty($templateId)) $curTemplate = $templateId;
-                               } else if ($pageId == $this->gEnv->getDefaultMobilePageId()){           // 携帯サイトのとき
-                                       $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
-                               } else if ($pageId == $this->gEnv->getDefaultSmartphonePageId()){               // スマートフォン用サイトのとき
-                                       $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
-                               } else if ($pageId == $this->gEnv->getDefaultAdminPageId() ||           // 管理サイトのとき
-                                                       $pageId == $this->gEnv->getDefaultRegistPageId()){              // 登録サイトのとき
-                                       //$curTemplate = '_admin';
-                                       $curTemplate = $this->gSystem->defaultAdminTemplateId();
-                               } else if (empty($pageId)){                     // ページIDが指定されていないときは、ウィジェットを表示しないテンプレートのみの表示
-                                       // テンプレートIDが指定されている場合はテンプレートを変更
-                                       $template = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);                // テンプレートIDを取得
-                                       if (!empty($template)) $curTemplate = $template;
+                               // URLの引数のテンプレートを優先し、引数で指定されていなければ、ページ用個別のテンプレートを取得する
+                               
+                               // URLの引数でテンプレートIDが指定されている場合は設定
+                               $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
+                               if (!empty($templateId)) $curTemplate = $templateId;
+                                       
+                               // ページ用個別に設定されたテンプレートがある場合は取得
+                               if (empty($curTemplate)){
+                                       $pageSubId = $request->trimValueOf(M3_REQUEST_PARAM_DEF_PAGE_SUB_ID);
+                                       $line = $this->gPage->getPageInfo($pageId, $pageSubId);
+                                       if (!empty($line)){
+                                               $pageTemplateId = $line['pn_template_id'];
+                                               $subTemplateId = $line['pn_sub_template_id'];           // サブテンプレートID
+                                       }
+                                       if (!empty($pageTemplateId)) $curTemplate = $pageTemplateId;
+                               }
+                               
+                               // 取得できなければデフォルトを取得
+                               if (empty($curTemplate)){
+                                       if ($pageId == $this->gEnv->getDefaultPageId()){                // 通常サイトのとき
+                                               $curTemplate = $this->gSystem->defaultTemplateId();
+                                               $subTemplateId = $this->gSystem->defaultSubTemplateId();
+                                       } else if ($pageId == $this->gEnv->getDefaultMobilePageId()){           // 携帯サイトのとき
+                                               $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
+                                       } else if ($pageId == $this->gEnv->getDefaultSmartphonePageId()){               // スマートフォン用サイトのとき
+                                               $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
+                                       } else if ($pageId == $this->gEnv->getDefaultAdminPageId() ||           // 管理サイトのとき
+                                                               $pageId == $this->gEnv->getDefaultRegistPageId()){              // 登録サイトのとき
+                                               $curTemplate = $this->gSystem->defaultAdminTemplateId();
+                                       } else if (empty($pageId)){                     // ページIDが指定されていないときは、ウィジェットを表示しないテンプレートのみの表示
+                                               // URLの引数でテンプレートIDが指定されている場合は設定
+       //                                      $templateId = $request->trimValueOf(M3_REQUEST_PARAM_TEMPLATE_ID);              // テンプレートIDを取得
+       //                                      if (!empty($templateId)) $curTemplate = $templateId;
+                                       }
                                }
                        } else {
+                               // ページ用のテンプレートがあるときは優先
+                               $pageTemplateId = $this->gPage->getTemplateIdFromCurrentPageInfo($subTemplateId);
+                               if (!empty($pageTemplateId)) $curTemplate = $pageTemplateId;
+
                                // テンプレートIDをセッションから取得
-                               if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
-                                       if (!$this->gEnv->getIsMobileSite() && !$this->gEnv->getIsSmartphoneSite()){
-                                               $curTemplate = $request->getSessionValue(M3_SESSION_CURRENT_TEMPLATE);// 携帯サイト、スマートフォンサイトでないときはセッション値を取得
+                               if (empty($curTemplate) && !$isSystemManageUser){                       // システム運用者はセッション値を使用できない
+                                       if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
+                                               if (!$this->gEnv->getIsMobileSite() && !$this->gEnv->getIsSmartphoneSite()){
+                                                       $curTemplate = $request->getSessionValue(M3_SESSION_CURRENT_TEMPLATE);// 携帯サイト、スマートフォンサイトでないときはセッション値を取得
+                                               }
                                        }
                                }
+                               
                                // オプションのテンプレートがある場合はオプションを優先
-                               $optionTemplate = $this->gPage->getOptionTemplateId();
+                               list($optionTemplate, $optionSubTemplate) = $this->gPage->getOptionTemplateId();
                                if (!empty($optionTemplate)){
                                        $curTemplate = $optionTemplate;
+                                       $subTemplateId = $optionSubTemplate;
                                        $templateDefined = true;                // テンプレート固定かどうか
                                }
-                       }
-               }
-
-               // セッションにないときは、DBより取得
-               if ($curTemplate == ''){
-                       // ページ用のテンプレートがあるときは優先
-                       $pageTemplateId = $this->gPage->getTemplateIdFromCurrentPageInfo();
-                       if (empty($pageTemplateId)){
-                               if ($this->gEnv->getIsMobileSite()){// 携帯用サイトの場合
-                                       $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
-                               } else if ($this->gEnv->getIsSmartphoneSite()){// スマートフォン用サイトの場合
-                                       $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
-                               } else {
-                                       $curTemplate = $this->gSystem->defaultTemplateId();
+                               
+                               // セッションにないときはデフォルトを取得
+                               if (empty($curTemplate)){
+                                       if ($this->gEnv->getIsMobileSite()){// 携帯用サイトの場合
+                                               $curTemplate = $this->gSystem->defaultMobileTemplateId();               // 携帯用デフォルトテンプレート
+                                       } else if ($this->gEnv->getIsSmartphoneSite()){// スマートフォン用サイトの場合
+                                               $curTemplate = $this->gSystem->defaultSmartphoneTemplateId();           // スマートフォン用デフォルトテンプレート
+                                       } else {
+                                               $curTemplate = $this->gSystem->defaultTemplateId();
+                                               $subTemplateId = $this->gSystem->defaultSubTemplateId();
+                                       }
                                }
-                       } else {
-                               $curTemplate = $pageTemplateId;
                        }
-//                     // オプションのテンプレートがある場合はオプションを優先
-//                     $optionTemplate = $this->gPage->getOptionTemplateId();
-//                     if (!empty($optionTemplate)) $curTemplate = $optionTemplate;
                }
-               
-               if ($curTemplate == ''){
+
+               if (empty($curTemplate)){
                        // テンプレートが1つもみつからないときは、管理用テンプレートを使用
-                       //$curTemplate = self::ADMIN_TEMPLATE;
                        $curTemplate = $this->gSystem->defaultAdminTemplateId();
                        echo 'template not found. viewing by administration template. [' . $curTemplate . ']';
                } else {        // セッションにテンプレートIDを保存
                        // テンプレートIDをセッションに残す場合
-                       if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
+/*                     if ($this->gSystem->useTemplateIdInSession()){          // セッションに保存する場合
                                if ($cmd == M3_REQUEST_CMD_SHOW_POSITION ||                             // 表示位置を表示するとき
                                        $cmd == M3_REQUEST_CMD_SHOW_POSITION_WITH_WIDGET){      // 表示位置を表示するとき(ウィジェット付き)
                                } else {
@@ -841,7 +1278,7 @@ class BaseFrameContainer extends Core
                                                $request->setSessionValue(M3_SESSION_CURRENT_TEMPLATE, $curTemplate);
                                        }
                                }
-                       }
+                       }*/
                }
                return $curTemplate;
        }
@@ -895,7 +1332,6 @@ class BaseFrameContainer extends Core
                        $curTemplateId = self::M_ADMIN_TEMPLATE;        // 携帯管理画面用テンプレート
                } else {                        // 携帯以外のサイトへのアクセスの場合
                        if ($type == 1){                        // ログインはデフォルトの管理画面テンプレートに固定
-                               //$curTemplateId = self::ADMIN_TEMPLATE;// 管理用テンプレート
                                $curTemplateId = $this->gSystem->defaultAdminTemplateId();
                        } else {
                                $curTemplateId = $this->gSystem->getSystemConfig(self::CONFIG_KEY_MSG_TEMPLATE);
@@ -903,7 +1339,7 @@ class BaseFrameContainer extends Core
                                        $curTemplateId = self::SYSTEM_TEMPLATE;// システム画面用テンプレート
                                } else {
                                        // テンプレートの存在チェック
-                                       $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplateId . '/index.php';
+                                       $templateIndexFile = $this->gEnv->getTemplatesPath() . '/' . $curTemplateId . '/' . M3_FILENAME_INDEX;
                                        if (!file_exists($templateIndexFile)) $curTemplateId = self::SYSTEM_TEMPLATE;// システム画面用テンプレート
                                }
                        }
@@ -946,10 +1382,15 @@ class BaseFrameContainer extends Core
        }
        function getBuffer($type = null, $name = null, $attribs = array())
        {
-               return '';
+               if (isset($this->joomlaBufArray[$type])){
+                       return $this->joomlaBufArray[$type];
+               } else {
+                       return '';
+               }
        }
        function setBuffer($contents, $type, $name = null)
        {
+               $this->joomlaBufArray[$type] = $contents;
                return;
        }
        /**
@@ -981,5 +1422,102 @@ class BaseFrameContainer extends Core
                $data['custom']         = $this->_custom;*/
                return $data;
        }
+       /**
+        * BASEタグ設定用
+        *
+        * @return string                               ベースパス
+        */
+       function getBase()
+       {
+               return '';
+       }
+        /**
+        * Adds a linked script to the page
+        *
+        * @param       string  $url            URL to the linked script
+        * @param       string  $type           Type of script. Defaults to 'text/javascript'
+        * @access   public
+        */
+       function addScript($url, $type="text/javascript") {
+               $this->_scripts[$url] = $type;
+       }
+       /**
+        * Adds a script to the page
+        *
+        * @access   public
+        * @param       string  $content   Script
+        * @param       string  $type   Scripting mime (defaults to 'text/javascript')
+        * @return   void
+        */
+       function addScriptDeclaration($content, $type = 'text/javascript')
+       {
+               if (!isset($this->_script[strtolower($type)])) {
+                       $this->_script[strtolower($type)] = $content;
+               } else {
+                       $this->_script[strtolower($type)] .= chr(13).$content;
+               }
+       }
+       /**
+        * WordPressテンプレートの起動ファイルパスを相対パスに変換
+        *
+        * @param string $templateId    テンプレートID
+        * @param string $path                  テンプレートの起動ファイル絶対パス
+        * @return string                               テンプレート内での相対パス。エラー発生の場合はデフォルト(index.php)を返す。
+        */
+       function _getRelativeTemplateIndexPath($templateId, $path)
+       {
+               $savedPath = $path;
+               $templatePath = $this->gEnv->getTemplatesPath() . '/' . $templateId . '/';
+               
+               // テンプレートまでのパスを削除
+               $path = str_replace($templatePath, '', $path);
+               if ($path == $savedPath) $path = M3_FILENAME_INDEX;
+               return $path;
+       }
+       /**
+        * 旧システムディレクトリが存在するかどうかを取得
+        *
+        * @return bool                         true=存在する、false=存在しない
+        */
+       function _isExistsOldSystemDir()
+       {
+               // 旧システムディレクトリは同ディレクト階層に存在し、ディレクトリ名の先頭に「_」が付加されているディレクトリ
+               $currentDir = $this->gEnv->getSystemRootPath();
+               $parentDir = dirname($currentDir);
+               $dirName = basename($currentDir);
+               
+               // ##### open_basedir等のアクセス制限が掛かっていてディレクトリが見えない場合はis_dir()はfalseを返す #####
+               // 親ディレクトリへのアクセス権をチェック
+               if (@is_dir($parentDir)){
+                       if (@is_dir($parentDir . '/_' . $dirName)){
+                               return true;
+                       } else {
+                               return false;
+                       }
+               } else {                // 親ディレクトリへのアクセス権がない場合は旧システムが存在すると判断する
+                       return true;
+               }
+       }
+       /**
+        * PHPファイルを読み込み、定義値をグローバル値に変換する
+        *
+        * @param string $path          ファイルパス
+        * @return bool                         true=ファイル読み込み完了、false=ファイル読み込み失敗
+        */
+       function _loadFileAsGlobal($path)
+       {
+               if (file_exists($path)){
+                       include($path);
+                       
+                       // グローバル変数に変換
+                       $vars = get_defined_vars();
+                       foreach($vars as $varName => $varValue){
+                               if (!isset($GLOBALS[$varName])) $GLOBALS[$varName] = $varValue;
+                       }
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 }
 ?>