7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
14 * @link http://www.magic3.org
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/_installBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/_installDb.php');
19 class _installInputparamWidgetContainer extends _installBaseWidgetContainer
21 private $dbFixed; // DB接続を固定するかどうか
26 function __construct()
29 parent::__construct();
34 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
35 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
37 * @param RequestManager $request HTTPリクエスト処理クラス
38 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
39 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
41 function _setTemplate($request, &$param)
43 return 'inputparam.tmpl.html';
48 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
50 * @param RequestManager $request HTTPリクエスト処理クラス
51 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
54 function _assign($request, &$param)
56 // ########### 画面項目の表示制御 ############
57 // PostgreSQLのPDOが使用可能かどうか
60 // PostgreSQLのPDOが使用可能かどうか
61 if (extension_loaded('pdo_pgsql')){
62 $this->tmpl->setAttribute("db_pgsql", "visibility", "visible");
65 $this->tmpl->setAttribute("db_pgsql", "visibility", "hidden");
68 if (extension_loaded('pdo_mysql')){
69 $this->tmpl->setAttribute("db_mysql", "visibility", "visible");
72 $this->tmpl->setAttribute("db_mysql", "visibility", "hidden");
74 if (!$canUsePgsql && !$canUseMysql) $this->setMsg(self::MSG_USER_ERR, $this->_('Can\'t use database.')); // DBが使用できません
77 $isConfigured = false; // 設定ファイルが作成されたかどうか
78 $isTested = false; // 接続テスト完了かどうか
79 $act = $request->trimValueOf('act');
80 $lang = $request->trimValueOf('lang');
81 if (empty($act)){ // 初期状態
82 // 使用可能なDBが1つのときはデフォルトとする
83 if ($canUsePgsql && !$canUseMysql){
84 $dbtype = M3_DB_TYPE_PGSQL;
85 } else if (!$canUsePgsql && $canUseMysql){
86 $dbtype = M3_DB_TYPE_MYSQL;
89 // 設定ファイルがある場合は、現在値を取得
90 if ($this->gConfig->isConfigured()){
91 $rooturl = $this->gConfig->getSystemRootUrl();
92 $dsn = $this->gConfig->getDbConnectDsn();
94 $pos = strpos($dsn, ':');
98 $dbtype = trim(substr($dsn, 0, $pos));
103 $dsnParams = explode(";", substr($dsn, $pos+1));
104 for ($i = 0; $i < count($dsnParams); $i++){
105 list($key, $value) = explode("=", $dsnParams[$i]);
107 $value = trim($value);
110 } else if ($key == 'dbname'){
114 $dbuser = $this->gConfig->getDbConnectUser();
115 $password = $this->gConfig->getDbConnectPassword();
116 } else {// 設定値がないときはデフォルト値を設定
117 $sytemRootUrl = $this->gEnv->calcSystemRootUrl();
118 if (empty($sytemRootUrl)) $sytemRootUrl = 'http://' . $_SERVER['SERVER_NAME'] . '/' . M3_SYSTEM_ROOT_DIR_NAME;
119 $rooturl = $sytemRootUrl;
120 $hostname = 'localhost';
125 } else if ($act == 'save' || $act == 'testdb'){
126 // ### 設定ファイルを更新した場合は次の画面へ遷移 ###
128 $rooturl = $request->trimValueOf('rooturl');
129 $this->checkUrl($rooturl, 'ルートURL');
130 $hostname = $request->trimValueOf('hostname');
131 //$this->checkSingleByte($hostname, "ホスト名");// IPアドレスを入力したとき「.」が除かれる
132 $dbuser = $request->trimValueOf('dbuser');
133 $this->checkSingleByte($dbuser, $this->_('User')); // DBユーザ
134 $password = $request->trimValueOf('password');
135 $dbname = $request->trimValueOf('dbname');
136 $this->checkSingleByte($dbname, $this->_('Database Name')); // DB名
139 $dbtype = $request->trimValueOf('dbtype');
140 if ($dbtype != M3_DB_TYPE_PGSQL && $dbtype != M3_DB_TYPE_MYSQL){ // DBが選択されていないとき
141 $this->setMsg(self::MSG_USER_ERR, $this->_('Database Type not selected.')); // DB種別が設定されていません
144 if ($this->getMsgCount() == 0){// 入力チェックOKの場合
145 if ($act == 'save'){ // 設定ファイルに保存の場合
147 $dsn = $dbtype . ':' . 'host=' . $hostname . ';dbname=' . $dbname;
148 $updateParam = array();
149 $this->gConfig->setDbConnectDsn($dsn); // 接続先DB
150 $this->gConfig->setDbConnectUser($dbuser);// 接続ユーザ
151 $this->gConfig->setDbConnectPassword($password); // パスワード
152 $this->gConfig->setSystemRootUrl($rooturl); // システムのルートURL
153 $ret = $this->gConfig->updateConfigFile($msg);
157 $this->setMsg(self::MSG_GUIDANCE, $this->_('Configration updated.')); // 設定値を更新しました
158 $isConfigured = true;
160 $this->setMsg(self::MSG_APP_ERR, $msg);
161 $this->setMsg(self::MSG_APP_ERR, $this->_('Failed in updating configration.')); // 設定値の更新に失敗しました
163 } else if ($act == 'testdb'){ // DB接続テスト
164 $dsn = $dbtype . ':' . 'host=' . $hostname . ';dbname=' . $dbname;
165 $db = new _installDB();
168 $db->displayErrMessage(false); // エラーメッセージの画面出力を抑止
169 if ($db->testDbConnection($dsn, $dbuser, $password)){
170 $db->displayErrMessage(true); // エラーメッセージの画面出力解除
172 $isErr = false; // エラーありかどうか
173 $msg = '<b><font color="green">' . $this->_('Succeeded in connecting database.') . '</font></b>'; // 接続正常
175 // MySQLの場合はその他のテストを実行
176 if ($dbtype == M3_DB_TYPE_MYSQL){
181 $ret = $db->testDBSqlMode($dsn, $dbuser, $password, $mode);
182 if ($ret && !empty($mode)){ // 取得可能な場合のみチェック
183 $this->setMsg(self::MSG_APP_ERR, $this->_('Invalid sql_mode. It requires blank value.') . '<br />sql_mode=' . $mode); // sql_modeが不正です。空文字列に設定して下さい。
184 $errMsgSub = $this->_('Invalid sql_mode.');// sql_mode不正
189 // 日本語の場合は日本語エンコーディングをチェック
190 if (!$isErr && $lang == 'ja'){
191 $ret = $db->testDBEncoding($dsn, $dbuser, $password, $rows);
192 if ($ret){ // 取得可能な場合のみチェック
193 foreach ($rows as $row){
194 $name = $row['Variable_name'];
195 $value = $row['Value'];
197 $errMsg .= $name . ' ' . $value;
198 if ($name != 'character_set_filesystem'){
199 if ($value != 'utf8'){
209 $this->setMsg(self::MSG_APP_ERR, $this->_('Invalid encoding for Japanese.') . '<br />' . $errMsg); // 日本語を使用するためのエンコード設定が不正です。
210 $errMsgSub = $this->_('Incollect encoding.');// エンコードエラー
215 if ($isErr){ // エラーの場合
217 $msg .= '<b><font color="red">' . $errMsgSub . '</font></b>';
220 if ($db->testDbTable($dsn, $dbuser, $password)){
221 $msg .= '<b><font color="green">' . $this->_('Succeeded in creating table.') . '</font></b>'; // テーブル作成正常
223 $msg .= '<b><font color="red">' . $this->_('Failed in creating table.') . '</font></b>'; // テーブル作成エラー
225 $isTested = true; // 接続テスト完了かどうか
229 $message = $this->_('Failed in connecting database. Confirm the db connection information.');
230 $errors = $db->getErrMsg();
231 for ($i = 0; $i < count($errors); $i++){
232 $message .= '<br />' . $errors[$i];
234 $this->setMsg(self::MSG_APP_ERR, $message);
236 $msg .= '<b><font color="red">' . $this->_('Failed in connecting.') . '</font></b>'; // 接続エラー
238 $this->tmpl->addVar('_widget', 'db_test', $msg);
243 // インストール定義ファイルがある場合は設定値を取得
244 if (defined('M3_INSTALL_PRE_FIXED_DB') && M3_INSTALL_PRE_FIXED_DB){ // DB接続を固定する場合
245 $dbname = defined('M3_INSTALL_DB_NAME') ? M3_INSTALL_DB_NAME : '';
246 $dbuser = defined('M3_INSTALL_DB_USER') ? M3_INSTALL_DB_USER : '';
247 // $password = defined('M3_INSTALL_DB_PASSWORD') ? M3_INSTALL_DB_PASSWORD : '';
249 $this->dbFixed = true; // DB接続を固定
253 if ($dbtype == M3_DB_TYPE_PGSQL){
254 $this->tmpl->addVar("db_pgsql", "checked", "checked");
255 } else if ($dbtype == M3_DB_TYPE_MYSQL){
256 $this->tmpl->addVar("db_mysql", "checked", "checked");
258 $this->tmpl->addVar("_widget", "root_url", $rooturl);
259 $this->tmpl->addVar("_widget", "hostname", $hostname);
260 $this->tmpl->addVar("_widget", "dbname", $dbname);
261 $this->tmpl->addVar("_widget", "dbuser", $dbuser);
262 $this->tmpl->addVar("_widget", "password", $password);
264 if ($this->dbFixed){ // DB接続を固定する場合
265 $this->tmpl->setAttribute("static_hostname", "visibility", "visible");
266 $this->tmpl->addVar("static_hostname", "hostname", $hostname);
268 $this->tmpl->setAttribute("static_dbname", "visibility", "visible");
269 $this->tmpl->addVar("static_dbname", "dbname", $dbname);
271 $this->tmpl->setAttribute("static_dbuser", "visibility", "visible");
272 $this->tmpl->addVar("static_dbuser", "dbuser", $dbuser);
274 // $this->tmpl->setAttribute("static_password", "visibility", "visible");
275 // $this->tmpl->addVar("static_password", "password", $password);
277 $this->tmpl->setAttribute("input_hostname", "visibility", "visible");
278 $this->tmpl->addVar("input_hostname", "hostname", $hostname);
280 $this->tmpl->setAttribute("input_dbname", "visibility", "visible");
281 $this->tmpl->addVar("input_dbname", "dbname", $dbname);
283 $this->tmpl->setAttribute("input_dbuser", "visibility", "visible");
284 $this->tmpl->addVar("input_dbuser", "dbuser", $dbuser);
286 // $this->tmpl->setAttribute("input_password", "visibility", "visible");
287 // $this->tmpl->addVar("input_password", "password", $password);
289 $this->tmpl->setAttribute("input_password", "visibility", "visible");
290 $this->tmpl->addVar("input_password", "password", $password);
292 // 設定ファイルの内容をみて、ボタンを制御
293 if (!$this->gConfig->isConfigured() && !$isConfigured) $this->tmpl->addVar('_widget', 'button_disabled', 'disabled');
296 $buttonTestConnection = $buttonGoBack = $buttonUpdateConfig = $buttonGoNext = 'btn-primary';
297 if ($this->gConfig->isConfigured()){ // 設定が保存されているとき
298 $buttonGoNext = 'btn-success'; // 「次へ」ボタンをアクティブ化
299 } else if ($isConfigured){
300 $buttonGoNext = 'btn-success'; // 「次へ」ボタンをアクティブ化
301 } else if ($isTested){
302 $buttonUpdateConfig = 'btn-success'; // 「設定値を更新」ボタンをアクティブ化
304 $buttonTestConnection = 'btn-success'; // 「接続テスト」ボタンをアクティブ化
306 $this->tmpl->addVar("_widget", "button_test_connection", $buttonTestConnection);
307 $this->tmpl->addVar("_widget", "button_go_back", $buttonGoBack);
308 $this->tmpl->addVar("_widget", "button_update_config", $buttonUpdateConfig);
309 $this->tmpl->addVar("_widget", "button_go_next", $buttonGoNext);
312 $localeText = array();
313 $localeText['label_update_config'] = $this->_('Update Config'); // 設定値を更新
314 $localeText['label_test_connection'] = $this->_('Test Connection');// 接続テスト
315 $localeText['msg_change_config'] = $this->_('Configration changed.\\nUpdate configration?');// 設定値が変更されています\n設定値を保存しますか?
316 $localeText['msg_update_config'] = $this->_('Update configration?');//設定値を更新しますか?
317 $localeText['title_input_site_info'] = $this->_('Input Site Information'); // サイト情報入力
319 $localeText['label_site_info'] = $this->_('Site Information'); // サイト情報
320 $localeText['label_root_url'] = $this->_('Root URL'); // ルートURL
321 $localeText['label_db_info'] = $this->_('Database Information'); // DB接続情報
322 $localeText['label_db_type'] = $this->_('Database Type'); // DB種別
323 $localeText['label_db_hostname'] = $this->_('Hostname'); // ホスト名
324 $localeText['label_db_name'] = $this->_('Database Name'); // DB名
325 $localeText['label_db_user'] = $this->_('User'); // DBユーザ
326 $localeText['label_db_password'] = $this->_('Password'); // パスワード
327 $this->setLocaleText($localeText);