OSDN Git Service

インストーラ更新。
[magic3/magic3.git] / widgets / _install / include / container / _installInputparamWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
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($gEnvManager->getCurrentWidgetContainerPath() .    '/_installBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/_installDb.php');
18
19 class _installInputparamWidgetContainer extends _installBaseWidgetContainer
20 {
21         private $dbFixed;                       // DB接続を固定するかどうか
22         
23         /**
24          * コンストラクタ
25          */
26         function __construct()
27         {
28                 // 親クラスを呼び出す
29                 parent::__construct();
30         }
31         /**
32          * テンプレートファイルを設定
33          *
34          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
35          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
36          *
37          * @param RequestManager $request               HTTPリクエスト処理クラス
38          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
39          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
40          */
41         function _setTemplate($request, &$param)
42         {       
43                 return 'inputparam.tmpl.html';
44         }
45         /**
46          * テンプレートにデータ埋め込む
47          *
48          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
49          *
50          * @param RequestManager $request               HTTPリクエスト処理クラス
51          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
52          * @return                                                              なし
53          */
54         function _assign($request, &$param)
55         {
56                 // ########### 画面項目の表示制御 ############
57                 // PostgreSQLのPDOが使用可能かどうか
58                 $canUsePgsql = false;
59                 $canUseMysql = false;
60                 // PostgreSQLのPDOが使用可能かどうか
61                 if (extension_loaded('pdo_pgsql')){
62                         $this->tmpl->setAttribute("db_pgsql", "visibility", "visible");
63                         $canUsePgsql = true;
64                 } else {
65                         $this->tmpl->setAttribute("db_pgsql", "visibility", "hidden");
66                 }
67                 // MySQLのPDOが使用可能かどうか
68                 if (extension_loaded('pdo_mysql')){
69                         $this->tmpl->setAttribute("db_mysql", "visibility", "visible");
70                         $canUseMysql = true;
71                 } else {
72                         $this->tmpl->setAttribute("db_mysql", "visibility", "hidden");
73                 }
74                 if (!$canUsePgsql && !$canUseMysql) $this->setMsg(self::MSG_USER_ERR, $this->_('Can\'t use database.'));                        // DBが使用できません
75                 
76                 $dbtype = '';
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;
87                         }
88                         
89                         // 設定ファイルがある場合は、現在値を取得
90                         if ($this->gConfig->isConfigured()){
91                                 $rooturl = $this->gConfig->getSystemRootUrl();
92                                 $dsn = $this->gConfig->getDbConnectDsn();
93                                 // DB種別を取得
94                                 $pos = strpos($dsn, ':');
95                                 if ($pos === false){
96                                         $pos = -1;
97                                 } else {
98                                         $dbtype = trim(substr($dsn, 0, $pos));
99                                 }
100                                 // ホスト名、DB名を取得
101                                 $hostname = '';
102                                 $dbname = '';
103                                 $dsnParams = explode(";", substr($dsn, $pos+1));
104                                 for ($i = 0; $i < count($dsnParams); $i++){
105                                         list($key, $value) = explode("=", $dsnParams[$i]);
106                                         $key = trim($key);
107                                         $value = trim($value);
108                                         if ($key == 'host'){
109                                                 $hostname = $value;
110                                         } else if ($key == 'dbname'){
111                                                 $dbname = $value;
112                                         }
113                                 }
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';
121                                 $dbname = '';
122                                 $dbuser = '';
123                                 $password = '';
124                         }
125                 } else if ($act == 'save' || $act == 'testdb'){
126                         // ### 設定ファイルを更新した場合は次の画面へ遷移 ###
127                         // 入力項目のエラーチェック
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名
137                         
138                         // 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種別が設定されていません
142                         }
143
144                         if ($this->getMsgCount() == 0){// 入力チェックOKの場合
145                                 if ($act == 'save'){            // 設定ファイルに保存の場合
146                                         // 設定ファイルの作成
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);
154                         
155                                         // 完了メッセージを出力
156                                         if ($ret){
157                                                 $this->setMsg(self::MSG_GUIDANCE, $this->_('Configration updated.'));           // 設定値を更新しました
158                                                 $isConfigured = true;
159                                         } else {
160                                                 $this->setMsg(self::MSG_APP_ERR, $msg);
161                                                 $this->setMsg(self::MSG_APP_ERR, $this->_('Failed in updating configration.'));         // 設定値の更新に失敗しました
162                                         }
163                                 } else if ($act == 'testdb'){           // DB接続テスト
164                                         $dsn = $dbtype . ':' . 'host=' . $hostname . ';dbname=' . $dbname;
165                                         $db = new _installDB();
166                                         
167                                         // 接続テスト
168                                         $db->displayErrMessage(false);          // エラーメッセージの画面出力を抑止
169                                         if ($db->testDbConnection($dsn, $dbuser, $password)){
170                                                 $db->displayErrMessage(true);           // エラーメッセージの画面出力解除
171                                                 
172                                                 $isErr = false;                 // エラーありかどうか
173                                                 $msg = '<b><font color="green">' . $this->_('Succeeded in connecting database.') . '</font></b>';                       // 接続正常
174                                                 
175                                                 // MySQLの場合はその他のテストを実行
176                                                 if ($dbtype == M3_DB_TYPE_MYSQL){
177                                                         $errMsg = '';
178                                                         $errMsgSub = '';
179                                                         
180                                                         // 「sql_mode」をチェック
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不正
185                                                                 
186                                                                 $isErr = true;
187                                                         }
188                                                         
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'];
196                                                                         
197                                                                                 $errMsg .= $name . ' ' . $value;
198                                                                                 if ($name != 'character_set_filesystem'){
199                                                                                         if ($value != 'utf8'){
200                                                                                                 $errMsg .= '(NG)';
201                                                                                                 $isErr = true;
202                                                                                         }
203                                                                                 }
204                                                                                 $errMsg .= '<br />';
205                                                                         }
206                                                                 }
207                                                                 // エラーありの場合
208                                                                 if ($isErr){
209                                                                         $this->setMsg(self::MSG_APP_ERR, $this->_('Invalid encoding for Japanese.') . '<br />' . $errMsg);              // 日本語を使用するためのエンコード設定が不正です。
210                                                                         $errMsgSub = $this->_('Incollect encoding.');// エンコードエラー
211                                                                 }
212                                                         }
213                                                 }
214                                                 
215                                                 if ($isErr){            // エラーの場合
216                                                         $msg .= ' => ';
217                                                         $msg .= '<b><font color="red">' . $errMsgSub . '</font></b>';                   
218                                                 } else {
219                                                         $msg .= ' => ';
220                                                         if ($db->testDbTable($dsn, $dbuser, $password)){
221                                                                 $msg .= '<b><font color="green">' . $this->_('Succeeded in creating table.') . '</font></b>';   // テーブル作成正常
222                                                         } else {
223                                                                 $msg .= '<b><font color="red">' . $this->_('Failed in creating table.') . '</font></b>';                        // テーブル作成エラー
224                                                         }
225                                                         $isTested = true;                       // 接続テスト完了かどうか
226                                                 }
227                                         } else {                // 接続エラーの場合
228                                                 // エラーメッセージ作成
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];
233                                                 }
234                                                 $this->setMsg(self::MSG_APP_ERR, $message);
235                                                 
236                                                 $msg .= '<b><font color="red">' . $this->_('Failed in connecting.') . '</font></b>';            // 接続エラー
237                                         }
238                                         $this->tmpl->addVar('_widget', 'db_test',       $msg);
239                                 }
240                         }
241                 }
242
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 : '';
248                         
249                         $this->dbFixed = true;                  // DB接続を固定
250                 }
251                         
252                 // 入力データを再設定
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");
257                 }
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);
263                 
264                 if ($this->dbFixed){                    // DB接続を固定する場合
265                         $this->tmpl->setAttribute("static_hostname", "visibility", "visible");
266                         $this->tmpl->addVar("static_hostname", "hostname",      $hostname);
267                         
268                         $this->tmpl->setAttribute("static_dbname", "visibility", "visible");
269                         $this->tmpl->addVar("static_dbname", "dbname",          $dbname);
270                         
271                         $this->tmpl->setAttribute("static_dbuser", "visibility", "visible");
272                         $this->tmpl->addVar("static_dbuser", "dbuser",          $dbuser);
273                         
274 //                      $this->tmpl->setAttribute("static_password", "visibility", "visible");
275 //                      $this->tmpl->addVar("static_password", "password",      $password);
276                 } else {
277                         $this->tmpl->setAttribute("input_hostname", "visibility", "visible");
278                         $this->tmpl->addVar("input_hostname", "hostname",       $hostname);
279                         
280                         $this->tmpl->setAttribute("input_dbname", "visibility", "visible");
281                         $this->tmpl->addVar("input_dbname", "dbname",           $dbname);
282                         
283                         $this->tmpl->setAttribute("input_dbuser", "visibility", "visible");
284                         $this->tmpl->addVar("input_dbuser", "dbuser",           $dbuser);
285                         
286 //                      $this->tmpl->setAttribute("input_password", "visibility", "visible");
287 //                      $this->tmpl->addVar("input_password", "password",       $password);
288                 }
289                 $this->tmpl->setAttribute("input_password", "visibility", "visible");
290                 $this->tmpl->addVar("input_password", "password",       $password);
291                 
292                 // 設定ファイルの内容をみて、ボタンを制御
293                 if (!$this->gConfig->isConfigured() && !$isConfigured) $this->tmpl->addVar('_widget', 'button_disabled', 'disabled');
294                 
295                 // 次の操作のボタンのカラーを設定
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';            // 「設定値を更新」ボタンをアクティブ化
303                 } else {
304                         $buttonTestConnection = 'btn-success';  // 「接続テスト」ボタンをアクティブ化
305                 }
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);
310                 
311                 // テキストをローカライズ
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');      // サイト情報入力
318                 
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);
328         }
329 }
330 ?>