OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / admin_main / include / container / admin_mainWidgetlistWidgetContainer.php
1 <?php
2 /**
3  * コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_mainWidgetlistWidgetContainer.php 5175 2012-09-07 05:10:38Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() .    '/admin_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_mainDb.php');
18 require_once($gEnvManager->getLibPath()                         . '/pcl/pclzip.lib.php' );
19 require_once($gEnvManager->getCurrentWidgetContainerPath()              . '/admin_mainDef.php');                        // 定義クラス
20
21 class admin_mainWidgetlistWidgetContainer extends admin_mainBaseWidgetContainer
22 {
23         private $db;    // DB接続オブジェクト
24         private $newWidget = array();           // 新規追加ウィジェット
25         private $widgetTypeArray;               // ウィジェットタイプ
26         private $widgetType;                    // 現在のウィジェットタイプ
27         private $showDetail;                    // 詳細表示するかどうか
28         private $defaultImageSize = 32;         // ウィジェット画像サイズ
29         private $isExistsWidgetList;            // ウィジェットが存在するかどうか
30         const SCRIPT_FILE_EXT = 'js';           // JavaScriptファイル拡張子
31         const CSS_FILE_EXT = 'css';             // cssファイル拡張子
32         const PHP_FILE_EXT = 'php';             // phpファイル拡張子
33         const NOT_FOUND_WIDGET_ICON_FILE = '/images/system/notfound32.png';             // ウィジェットが見つからないアイコン
34         
35         /**
36          * コンストラクタ
37          */
38         function __construct()
39         {
40                 // 親クラスを呼び出す
41                 parent::__construct();
42                 
43                 // DB接続オブジェクト作成
44                 $this->db = new admin_mainDb();
45                 
46                 // ウィジェットタイプメニュー項目
47                 $this->widgetTypeArray = array( array(  'name' => $this->_('For PC'),                   'value' => '0'),        // PC用
48                                                                                 array(  'name' => $this->_('For Mobile'),               'value' => '1'),        // 携帯用
49                                                                                 array(  'name' => $this->_('For Smartphone'),   'value' => '2'));       // スマートフォン用
50         }
51         /**
52          * テンプレートファイルを設定
53          *
54          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
55          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
56          *
57          * @param RequestManager $request               HTTPリクエスト処理クラス
58          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
59          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
60          */
61         function _setTemplate($request, &$param)
62         {       
63                 return 'widgetlist.tmpl.html';
64         }
65         /**
66          * ヘルプデータを設定
67          *
68          * ヘルプの設定を行う場合はヘルプIDを返す。
69          * ヘルプデータの読み込むディレクトリは「自ウィジェットディレクトリ/include/help」に固定。
70          *
71          * @param RequestManager $request               HTTPリクエスト処理クラス
72          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
73          * @return string                                               ヘルプID。ヘルプデータはファイル名「help_[ヘルプID].php」で作成。ヘルプを使用しない場合は空文字列「''」を返す。
74          */
75         function _setHelp($request, &$param)
76         {       
77                 return 'widgetlist';
78         }
79         /**
80          * テンプレートにデータ埋め込む
81          *
82          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
83          *
84          * @param RequestManager $request               HTTPリクエスト処理クラス
85          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
86          * @param                                                               なし
87          */
88         function _assign($request, &$param)
89         {
90                 $act = $request->trimValueOf('act');
91                 $selectedItemNo = $request->trimValueOf('no');          // 処理対象の項目番号
92                 $serial = $request->trimValueOf('serial');              // シリアルNo
93                 $widgetId = $request->trimValueOf('widget');            // 処理対象のウィジェット
94                 $this->widgetType = $request->trimValueOf('item_type');// 現在のウィジェットタイプ
95                 if ($this->widgetType == '') $this->widgetType = '0';           // デフォルトはPC用ウィジェット
96                 $this->showDetail = ($request->trimValueOf('item_show_detail') == 'on') ? 1 : 0;                // 詳細表示するかどうか
97                                 
98                 if ($act == 'readnew'){         // ウィジェット再読み込みのとき
99                         $addWidgetCount = 0;
100                         // ウィジェット一覧取得
101                         if ($this->db->getAllWidgetIdList($rows)){
102                                 // ウィジェットディレクトリチェック
103                                 switch ($this->widgetType){
104                                         case '0':               // PC用テンプレート
105                                         default:
106                                                 $searchPath = $this->gEnv->getWidgetsPath();
107                                                 break;
108                                         case '1':               // 携帯用テンプレート
109                                                 $searchPath = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_MOBILE;
110                                                 break;
111                                         case '2':               // スマートフォン用テンプレート
112                                                 $searchPath = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_SMARTPHONE;
113                                                 break;
114                                 }
115                                 
116                                 if (is_dir($searchPath)){
117                                         $dir = dir($searchPath);
118                                         while (($file = $dir->read()) !== false){
119                                                 $filePath = $searchPath . '/' . $file;
120                                                 // ディレクトリかどうかチェック
121                                                 if (strncmp($file, '.', 1) != 0 && $file != '..' && is_dir($filePath)
122                                                                 && strncmp($file, '_', 1) != 0          // 「_」で始まる名前のディレクトリは読み込まない
123                                                                 && strlen($file) > 1){          // 1文字のディレクトリは読み込まない
124                                                         // ウィジェットIDを作成
125                                                         switch ($this->widgetType){
126                                                                 case '0':               // PC用テンプレート
127                                                                 default:
128                                                                         $widgetId = $file;
129                                                                         break;
130                                                                 case '1':               // 携帯用テンプレート
131                                                                         $widgetId = M3_DIR_NAME_MOBILE . '/' . $file;
132                                                                         break;
133                                                                 case '2':               // スマートフォン用テンプレート
134                                                                         $widgetId = M3_DIR_NAME_SMARTPHONE . '/' . $file;
135                                                                         break;
136                                                         }
137                                                         
138                                                         // DBに登録されていない場合は登録
139                                                         for ($i = 0; $i < count($rows); $i++){
140                                                                 if ($widgetId == $rows[$i]['wd_id']) break;
141                                                         }
142                                                         if ($i == count($rows)){
143                                                                 // ディレクトリ内のスクリプト、CSSの状況を取得
144                                                                 $ret = $this->getDirStatus($filePath, $hasScripts, $hasCss, $hasAdmin);
145                                                                 
146                                                                 // ウィジェットを登録
147                                                                 if ($ret){
148                                                                         $this->db->addNewWidget($widgetId, $file, intval($this->widgetType), $hasScripts, $hasCss, $hasAdmin);
149                                                                 } else {
150                                                                         $this->db->addNewWidget($widgetId, $file, intval($this->widgetType));
151                                                                 }
152                                                                 $this->newWidget[] = $widgetId;                 // 新規ウィジェットID保存
153                                                                 $addWidgetCount++;              // ウィジェット追加
154                                                         }
155                                                 }
156                                         }
157                                         $dir->close();
158                                 }
159                         } else {
160                         }
161                         // 終了メッセージを表示
162                         if ($addWidgetCount > 0){
163                                 //$msg = '新規ウィジェットを追加しました(追加数=' . $addWidgetCount . ')';
164                                 $msg = sprintf($this->_('New widgets added. (widgets count=%d)'), $addWidgetCount);                     // 新規ウィジェットを追加しました(追加数=%d)
165                         } else {
166                                 //$msg = '新規ウィジェットはありません';
167                                 $msg = $this->_('No new widgets added.');               // 新規ウィジェットはありません
168                         }
169                         $this->setMsg(self::MSG_GUIDANCE, $msg);
170                 } else if ($act == 'updateline'){               // 行更新のとき
171                         // 変更可能値
172                         $updateName = $request->trimValueOf('item' . $selectedItemNo . '_name');                                // 名前
173                         $updateAvailable = ($request->trimValueOf('item' . $selectedItemNo . '_available') == 'on') ? 1 : 0;            // 利用可能かどうか
174                         $updateActive = ($request->trimValueOf('item' . $selectedItemNo . '_active') == 'on') ? 1 : 0;          // ウィジェット実行可能かどうか
175                         
176                         $ret = $this->db->updateWidget($serial, $updateName, $updateAvailable, $updateActive);
177                         if ($ret){              // データ更新成功のとき
178                                 //$this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
179                                 $this->setMsg(self::MSG_GUIDANCE, $this->_('Line updated.'));           // データを更新しました
180                         } else {
181                                 //$this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
182                                 $this->setMsg(self::MSG_APP_ERR, $this->_('Failed in updating line.')); // データ更新に失敗しました
183                         }
184                 } else if ($act == 'deleteline'){               // ウィジェット削除のとき
185                         // ウィジェットが参照状況をチェック、参照されている場合は削除できない
186                         if ($this->db->getWidget($serial, $row)){
187                                 // ウィジェットディレクトリ取得
188                                 $widgetId = $row['wd_id'];
189                                 $widgetPath = $this->gEnv->getWidgetsPath() . '/' . $widgetId;
190                                 
191                                 // ウィジェットディレクトリの削除権限のチェック
192                                 if (is_writable($widgetPath) || !is_dir($widgetPath)){          // 削除可能か、すでにディレクトリがないとき
193                                         // インストール初期化フラグを一旦リセット
194                                         $this->db->updateIsWidgetInitialized($widgetId, false/*未初期化*/);
195                                                                                         
196                                         // ウィジェットのアンインストール処理
197                                         $saveWidgetId = $this->gEnv->getCurrentWidgetId();// ウィジェットID保存
198                                         $this->gEnv->setCurrentWidgetId($widgetId);// ウィジェットID一時設定
199                                         $this->gLaunch->goInstallWidget(1);             // アンインストール
200                                         $this->gEnv->setCurrentWidgetId($saveWidgetId);// ウィジェットID戻す
201
202                                         // エラーメッセージを取得
203                                         $this->addMsg($this->gInstance->getMessageManager()->getErrorMessage(), 
204                                                                         $this->gInstance->getMessageManager()->getWarningMessage(), $this->gInstance->getMessageManager()->getGuidanceMessage());
205                                                                                                                 
206                                         // ディレクトリ削除
207                                         if ((is_dir($widgetPath) && rmDirectory($widgetPath)) || !is_dir($widgetPath)){// 削除成功か、ディレクトリが存在しないとき
208                                                 $ret = $this->db->deleteWidget($serial);
209                                                 //$this->setMsg(self::MSG_GUIDANCE, 'ウィジェットを削除しました(ウィジェットID:' . $widgetId . ')');
210                                                 $this->setMsg(self::MSG_GUIDANCE, sprintf($this->_('Widget deleted. (widget ID: %s)'), $widgetId));     // ウィジェットを削除しました(ウィジェットID:%s)
211                                         } else {
212                                                 //$this->setMsg(self::MSG_APP_ERR, 'ウィジェットのディレクトリが削除できませんでした(ディレクトリ:' . $widgetPath . ')');
213                                                 $this->setMsg(self::MSG_APP_ERR, sprintf($this->_('Failed in deleting widget directory. (directory: %s)'), $widgetPath));       // ウィジェットのディレクトリが削除できませんでした(ディレクトリ:%s)
214                                         }
215                                 } else {
216                                         //$this->setMsg(self::MSG_APP_ERR, 'ウィジェットのディレクトリの削除権限がありません(ディレクトリ:' . $widgetPath . ')');
217                                         $this->setMsg(self::MSG_APP_ERR, sprintf($this->_('You are not allowed to delete widget directory. (directory: %s)'), $widgetPath));            // ウィジェットのディレクトリの削除権限がありません(ディレクトリ:%s)
218                                 }
219                         } else {
220                                 //$this->setMsg(self::MSG_APP_ERR, '削除対象のウィジェットが見つかりません');
221                                 $this->setMsg(self::MSG_APP_ERR, $this->_('Widget not found.'));                // 削除対象のウィジェットが見つかりません
222                         }
223                 } else if ($act == 'upload'){           // ファイルアップロードの場合
224                         $replaceWidget = ($request->trimValueOf('item_replace') == 'on') ? 1 : 0;               // ウィジェットを置き換えるかどうか
225
226                         // アップロードされたファイルか?セキュリティチェックする
227                         if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
228                                 $uploadFilename = $_FILES['upfile']['name'];            // アップロードされたファイルのファイル名取得
229                                 
230                                 // ファイル名の解析
231                                 $pathParts = pathinfo($uploadFilename);
232                                 $ext = $pathParts['extension'];         // 拡張子
233                                 $widgetName = basename($uploadFilename, '.' . $ext);            // 拡張子をはずす
234                                 $widgetId = $widgetName;
235                         
236                                 // ウィジェットIDを修正
237                                 switch ($this->widgetType){
238                                         case '0':               // PC用テンプレート
239                                         default:
240                                                 break;
241                                         case '1':               // 携帯用テンプレート
242                                                 $widgetId = M3_DIR_NAME_MOBILE . '/' . $widgetId;
243                                                 break;
244                                         case '2':               // スマートフォン用テンプレート
245                                                 $widgetId = M3_DIR_NAME_SMARTPHONE . '/' . $widgetId;
246                                                 break;
247                                 }
248                                 
249                                 // ファイル拡張子のチェック
250                                 if ($ext != 'zip'){
251                                         //$msg = 'zip圧縮のファイルのみアップロード可能です';
252                                         $msg = $this->_('Only zip format file is allowed to upload.');  // zip圧縮のファイルのみアップロード可能です
253                                         $this->setAppErrorMsg($msg);
254                                 }
255                                 
256                                 // テンポラリディレクトリの書き込み権限をチェック
257                                 if (!is_writable($this->gEnv->getWorkDirPath())){
258                                         //$msg = '一時ディレクトリに書き込み権限がありません。ディレクトリ:' . $this->gEnv->getWorkDirPath();
259                                         $msg = sprintf($this->_('You are not allowed to write temporary directory. (directory: %s)'), $this->gEnv->getWorkDirPath());   // 一時ディレクトリに書き込み権限がありません。(ディレクトリ:%s)
260                                         $this->setAppErrorMsg($msg);
261                                 }
262                                 
263                                 if ($this->getMsgCount() == 0){         // エラーが発生していないとき
264                                         if (!$replaceWidget){           // ウィジェット置き換えないとき
265                                                 // 同じIDのウィジェットがないかチェック
266                                                 if ($this->db->isExistsWidgetId($widgetId)){
267                                                         //$msg = 'ウィジェットがすでに存在します(ウィジェットID:' . $widgetId . ')';
268                                                         $msg = sprintf($this->_('The widget already exists. (widget ID: %s)'), $widgetId);              // ウィジェットがすでに存在します(ウィジェットID:%s)
269                                                         $this->setAppErrorMsg($msg);
270                                                 }
271                                         }
272                                 }
273                                 if ($this->getMsgCount() == 0){         // エラーが発生していないとき
274                                         // ファイルを保存するサーバディレクトリを指定
275                                         $tmpFile = tempnam($this->gEnv->getWorkDirPath(), M3_SYSTEM_WORK_UPLOAD_FILENAME_HEAD);
276
277                                         // アップされたテンポラリファイルを保存ディレクトリにコピー
278                                         $ret = move_uploaded_file($_FILES['upfile']['tmp_name'], $tmpFile);
279                                         if ($ret){
280                                                 // 解凍先ディレクトリ
281                                                 switch ($this->widgetType){
282                                                         case '0':               // PC用テンプレート
283                                                         default:
284                                                                 $extDir = $this->gEnv->getWidgetsPath();
285                                                                 break;
286                                                         case '1':               // 携帯用テンプレート
287                                                                 $extDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_MOBILE;
288                                                                 break;
289                                                         case '2':               // スマートフォン用テンプレート
290                                                                 $extDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_SMARTPHONE;
291                                                                 break;
292                                                 }
293                                         
294                                                 // zipファイルを解凍
295                                                 $zipFile = new PclZip($tmpFile);
296                                                 if (($zipList = $zipFile->listContent()) == 0){
297                                                         //$msg = 'zipファイルの内容のリスト取得に失敗しました(要因: ' . $zipFile->errorName(true) . ')';
298                                                         $msg = sprintf($this->_('Failed in getting file list from zip file. (detail: %s)'), $zipFile->errorName(true));         // zipファイルの内容のリスト取得に失敗しました(要因: %s)
299                                                         $this->setAppErrorMsg($msg);
300                                                 } else {
301                                                         // zipファイル名とディレクトリ名が同じであるかチェック
302                                                         $dirName = $widgetName . '/';
303                                                         if (strncmp($zipList[0]['filename'], $dirName, strlen($dirName)) == 0){
304                                                                 $widgetPath = $this->gEnv->getWidgetsPath() . '/' . $widgetId;// ウィジェットのディレクトリ
305                                                                 if ($replaceWidget){            // ウィジェット置き換える場合
306                                                                         // ディレクトリ削除
307                                                                         if ((is_dir($widgetPath) && rmDirectory($widgetPath)) || !is_dir($widgetPath)){// 削除成功か、ディレクトリが存在しないとき
308                                                                         } else {
309                                                                                 //$this->setMsg(self::MSG_APP_ERR, 'ウィジェットのディレクトリが削除できませんでした');
310                                                                                 $this->setMsg(self::MSG_APP_ERR, $this->_('Failed in deleting widget directory.'));     // ウィジェットのディレクトリが削除できませんでした
311                                                                         }
312                                                                 }
313                                                                 
314                                                                 if ($this->getMsgCount() == 0){         // エラーが発生していないとき                                      
315                                                                         $ret = $zipFile->extract(PCLZIP_OPT_PATH, $extDir);
316                                                                         if ($ret){
317                                                                                 // ウィジェット新規登録のときだけウィジェット登録処理を実行
318                                                                                 // ウィジェット情報が登録されているかチェック
319                                                                                 $registWidget = false;                  // ウィジェットを登録するかどうか
320                                                                                 if (!$replaceWidget || ($replaceWidget && !$this->db->isExistsWidgetId($widgetId))) $registWidget = true;
321                                                                                 
322                                                                                 // ウィジェットの新規登録。ウィジェット情報がない場合は作成しておく
323                                                                                 if ($registWidget){
324                                                                                         // ウィジェットの初期化状態を取得
325                                                                                         // ウィジェットが初期化されている場合はデータをそのまま残す→スクリプトを実行しない
326                                                                                         $initialized = $this->db->isWidgetInitialized($widgetId);
327                                                                                 
328                                                                                         // ディレクトリ内のスクリプト、CSSの状況を取得
329                                                                                         $ret = $this->getDirStatus($widgetPath, $hasScripts, $hasCss, $hasAdmin);
330                                                                                 
331                                                                                         // ウィジェットを登録
332                                                                                         if ($ret){
333                                                                                                 $ret = $this->db->addNewWidget($widgetId, $widgetName, intval($this->widgetType), $hasScripts, $hasCss, $hasAdmin);
334                                                                                         } else {
335                                                                                                 $ret = $this->db->addNewWidget($widgetId, $widgetName, intval($this->widgetType));
336                                                                                         }
337                                                                                         
338                                                                                         if ($initialized) $this->db->updateIsWidgetInitialized($widgetId, true/*初期化済み*/);
339                                                                                         
340                                                                                         // インストールタイプ
341                                                                                         $installType = 0;               // インストール
342                                                                                 } else {
343                                                                                         // インストール初期化フラグを一旦リセット
344                                                                                         $this->db->updateIsWidgetInitialized($widgetId, false/*未初期化*/);
345                                                                                         
346                                                                                         // インストールタイプ
347                                                                                         $installType = 2;               // アップデート
348                                                                                 }
349                                                                                 
350                                                                                 // ウィジェットのインストール処理
351                                                                                 $saveWidgetId = $this->gEnv->getCurrentWidgetId();// ウィジェットID保存
352                                                                                 $this->gEnv->setCurrentWidgetId($widgetId);// ウィジェットID一時設定
353                                                                                 //$this->gLaunch->goInstallWidget(0);           // インストール
354                                                                                 $this->gLaunch->goInstallWidget($installType);          // インストール
355                                                                                 $this->gEnv->setCurrentWidgetId($saveWidgetId);// ウィジェットID戻す
356                                                                         
357                                                                                 // インストールが完了したときは初期化済みに設定
358                                                                                 $this->db->updateIsWidgetInitialized($widgetId, true/*初期化済み*/);
359                                                                                 
360                                                                                 // エラーメッセージを取得
361                                                                                 $this->addMsg($this->gInstance->getMessageManager()->getErrorMessage(), 
362                                                                                                                 $this->gInstance->getMessageManager()->getWarningMessage(), $this->gInstance->getMessageManager()->getGuidanceMessage());
363                                                         
364                                                                                 // バージョンを通知
365                                                                                 if ($this->getMsgCount(self::MSG_APP_ERR) == 0 && $this->getMsgCount(self::MSG_USER_ERR) == 0){         // エラーが発生していないとき
366                                                                                         // バージョン取得
367                                                                                         $ret = $this->_db->getWidgetInfo($widgetId, $row);
368                                                                                         if ($ret) $version = $row['wd_version'];                // ウィジェットのバージョン
369                                                                                         
370                                                                                         switch ($installType){
371                                                                                                 case 0:         // インストール
372                                                                                                         //$msg = 'ウィジェットのインストールが完了しました。';
373                                                                                                         $msg = $this->_('Widget installed successfully.');              // ウィジェットのインストールが完了しました。
374                                                                                                         break;
375                                                                                                 case 2:         // アップデート
376                                                                                                         //$msg = 'ウィジェットの更新が完了しました。';
377                                                                                                         $msg = $this->_('Widget updated successfully.');                // ウィジェットの更新が完了しました。
378                                                                                                         break;
379                                                                                         }
380                                                                                         //$msg .= 'バージョンは' . $version . 'です。';
381                                                                                         $msg .= ' ' . sprintf($this->_('Current version is %s.'), $version);                    // バージョンは$sです。
382                                                                                         $this->setGuidanceMsg($msg);
383                                                                                         
384                                                                                         //$msg = 'ファイルのアップロードが完了しました(ウィジェットID:' . $widgetId . ')';
385                                                                                         $msg = sprintf($this->_('File uploaded. (widget ID: %s)'), $widgetId);          // ファイルのアップロードが完了しました(ウィジェットID: %s)
386                                                                                         $this->setGuidanceMsg($msg);
387                                                                                 } else {
388                                                                                         //$msg = 'ファイルのアップロードに失敗しました';
389                                                                                         $msg = $this->_('Failed in uploading file.');           // ファイルのアップロードに失敗しました
390                                                                                         $this->setAppErrorMsg($msg);
391                                                                                 }
392
393                                                                                 $this->newWidget[] = $widgetId;                 // 新規ウィジェットID保存
394                                                                         } else {
395                                                                                 //$msg = 'ファイルのアップロードに失敗しました(要因: ' . $zipFile->errorName(true) . ')';
396                                                                                 $msg = sprintf($this->_('Failed in uploading file. (detail: %s)'), $zipFile->errorName(true));  // ファイルのアップロードに失敗しました(要因: %s)
397                                                                                 $this->setAppErrorMsg($msg);
398                                                                         }
399                                                                 }
400                                                         } else {
401                                                                 //$msg = 'zipファイルのファイル名とディレクトリ名が異なっているか、全角文字が含まれています';
402                                                                 $msg = $this->_('The zip filename is different from directory name.');                  // zipファイルのファイル名とディレクトリ名が異なっているか、全角文字が含まれています
403                                                                 $this->setAppErrorMsg($msg);
404                                                         }
405                                                 }
406                                         } else {
407                                                 //$msg = 'ファイルのアップロードに失敗しました';
408                                                 $msg = $this->_('Failed in uploading file.');           // ファイルのアップロードに失敗しました
409                                                 $this->setAppErrorMsg($msg);
410                                         }
411                                         // テンポラリファイル削除
412                                         unlink($tmpFile);
413                                 }
414                         } else {
415                                 //$msg = 'アップロードファイルが見つかりません(要因:アップロード可能なファイルのMaxサイズを超えている可能性があります - ' . $this->gSystem->getMaxFileSizeForUpload() . 'バイト)';
416                                 $msg = sprintf($this->_('Uploded file not found. (detail: The file may be over maximum size to be allowed to upload. Size %s bytes.'), $this->gSystem->getMaxFileSizeForUpload());      // アップロードファイルが見つかりません(要因:アップロード可能なファイルのMaxサイズを超えている可能性があります。%sバイト)
417                                 $this->setAppErrorMsg($msg);
418                         }
419                 } else if ($act == 'download'){         // ファイルダウンロードのとき
420                         switch ($this->widgetType){
421                                 case '0':               // PC用テンプレート
422                                 default:
423                                         $widgetsDir = $this->gEnv->getWidgetsPath();            // ウィジェットディレクトリ
424                                         $widgetDir = $widgetsDir . '/' . $widgetId;             // ダウンロードするウィジェットのディレクトリ
425                                         $downloadFilename = $widgetId . '.zip';                         // ダウンロード時のファイル名
426                                         break;
427                                 case '1':               // 携帯用テンプレート
428                                         $widgetsDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_MOBILE;         // ウィジェットディレクトリ
429                                         $widgetDir = $this->gEnv->getWidgetsPath() . '/' . $widgetId;                           // ダウンロードするウィジェットのディレクトリ
430                                         list($dir, $filename) = explode('/', $widgetId);                // 先頭の「m/」を削除
431                                         $downloadFilename = $filename . '.zip';                                 // ダウンロード時のファイル名
432                                         break;
433                                 case '2':               // スマートフォン用テンプレート
434                                         $widgetsDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_SMARTPHONE;             // ウィジェットディレクトリ
435                                         $widgetDir = $this->gEnv->getWidgetsPath() . '/' . $widgetId;                           // ダウンロードするウィジェットのディレクトリ
436                                         list($dir, $filename) = explode('/', $widgetId);                // 先頭の「s/」を削除
437                                         $downloadFilename = $filename . '.zip';                                 // ダウンロード時のファイル名
438                                         break;
439                         }
440                         $tmpFile = tempnam($this->gEnv->getWorkDirPath(), M3_SYSTEM_WORK_DOWNLOAD_FILENAME_HEAD);               // zip処理用一時ファイル
441                         
442                         // zip圧縮
443                         $zipFile = new PclZip($tmpFile);
444                         $ret = $zipFile->create($widgetDir, PCLZIP_OPT_REMOVE_PATH, $widgetsDir);
445                         if ($ret){
446                                 // ページ作成処理中断
447                                 $this->gPage->abortPage();
448                                 
449                                 // ダウンロード処理
450                                 $ret = $this->gPage->downloadFile($tmpFile, $downloadFilename, true/*実行後ファイル削除*/);
451                                 
452                                 // システム強制終了
453                                 $this->gPage->exitSystem();
454                         } else {
455                                 //$msg = 'ファイルのダウンロードに失敗しました(要因: ' . $zipFile->errorName(true) . ')';
456                                 $msg = sprintf($this->_('Failed in downloading file. (detail: %s)'), $zipFile->errorName(true));                // ファイルのダウンロードに失敗しました(要因: %s)
457                                 $this->setAppErrorMsg($msg);
458                                 
459                                 // テンポラリファイル削除
460                                 unlink($tmpFile);
461                         }
462                 } else if ($act == 'changedetail'){             // 詳細表示の変更のとき
463                         // 画面設定値を更新
464                         $this->gDisp->setAdminConfig(admin_mainDef::CFG_SHOW_WIDGET_DETAIL, strval($this->showDetail));
465                 }
466                 // 詳細設定状況を再取得
467                 $this->showDetail = intval($this->gDisp->getAdminConfig(admin_mainDef::CFG_SHOW_WIDGET_DETAIL));
468                 
469                 // ウィジェットのタイプごとの処理
470                 switch ($this->widgetType){
471                         case '0':               // PC用テンプレート
472                         default:
473                                 $installDir = $this->gEnv->getWidgetsPath();// ウィジェット格納ディレクトリ
474                                 break;
475                         case '1':               // 携帯用テンプレート
476                                 $installDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_MOBILE;// ウィジェット格納ディレクトリ
477                                 break;
478                         case '2':               // スマートフォン用テンプレート
479                                 $installDir = $this->gEnv->getWidgetsPath() . '/' . M3_DIR_NAME_SMARTPHONE;// ウィジェット格納ディレクトリ
480                                 break;
481                 }
482
483                 // 表示制御
484                 if (!empty($this->showDetail)){         // 詳細表示のとき
485                         $this->tmpl->setAttribute('show_dir', 'visibility', 'visible');// ディレクトリ表示
486                         $this->tmpl->setAttribute('show_list_detail', 'visibility', 'visible');// 一覧を詳細表示
487                 }
488                 // ウィジェットタイプ選択メニュー作成
489                 $this->createWidgetTypeMenu();
490                 
491                 // ウィジェットリストを取得
492                 $this->db->getAllWidgetList(intval($this->widgetType), array($this, 'widgetListLoop'));
493                 if (!$this->isExistsWidgetList) $this->tmpl->setAttribute('widgetlist', 'visibility', 'hidden');// ウィジェットがないときは、一覧を表示しない
494                 
495                 // 画面にデータを埋め込む
496                 $checkedStr = '';
497                 if (!empty($this->showDetail)) $checkedStr = 'checked';
498                 $this->tmpl->addVar("_widget", "show_detail", $checkedStr);             // 詳細表示
499                 $this->tmpl->addVar("show_dir", "install_dir", $installDir);// インストールディレクトリを設定
500                 
501                 // テキストをローカライズ
502                 $localeText = array();
503                 $localeText['msg_update_line'] = $this->_('Update line?');              // 行を更新しますか?
504                 $localeText['msg_delete_line'] = $this->_('Delete widget?');            // このウィジェットを削除しますか?
505                 $localeText['msg_no_upload_file'] = $this->_('File not selected.');             // アップロードするファイルが選択されていません
506                 $localeText['msg_upload_file'] = $this->_('Upload file.');              // ファイルをアップロードします
507                 $localeText['label_widget_list'] = $this->_('Widget List');                     // ウィジェット一覧
508                 $localeText['label_widget_type'] = $this->_('Widget Type:');                    // ウィジェットタイプ:
509                 $localeText['label_install_dir'] = $this->_('Install Directory:');                      // インストールディレクトリ:
510                 $localeText['label_read_new'] = $this->_('Reload directory');                   // ディレクトリ再読み込み
511                 $localeText['label_show_detail'] = $this->_('Show detail');                     // 詳細表示
512                 $localeText['label_widget_name'] = $this->_('Name');                    // 名前
513                 $localeText['label_widget_version'] = $this->_('Version');                      // バージョン
514                 $localeText['label_widget_available'] = $this->_('Available');                  // 配置可
515                 $localeText['label_widget_active'] = $this->_('Active');                        // 実行可
516                 $localeText['label_widget_date'] = $this->_('Release Date');                    // リリース日
517                 $localeText['label_widget_operation'] = $this->_('Operation');                  // 操作
518                 $localeText['label_widget_upload'] = $this->_('Widget Upload (zip compressed file)');                   // ウィジェットアップロード(zip圧縮ファイル)
519                 $localeText['msg_replace_widget'] = $this->_('Replace widget if exists.');                      // ウィジェットが存在する場合は置き換え
520                 $localeText['label_upload'] = $this->_('Upload');                       // アップロード
521                 $this->setLocaleText($localeText);
522         }
523         /**
524          * ウィジェットのディレクトリの状態を取得
525          *
526          * @param string $path                  ウィジェットディレクトリパス
527          * @param bool $hasScripts              スクリプトファイルが存在するかどうか
528          * @param bool $hasCss                  CSSファイルが存在するかどうか
529          * @param bool $hasAdmin                管理画面があるかどうか
530          * @return bool                                 true=取得成功、false=取得失敗
531          */
532         function getDirStatus($path, &$hasScripts, &$hasCss, &$hasAdmin)
533         {
534                 // 戻り値初期化
535                 $ret = true;
536                 $hasScripts = false;
537                 $hasCss = false;
538                 $hasAdmin = false;
539         
540                 // スクリプトディレクトリをチェック
541                 $searchPath = $path . '/' . M3_DIR_NAME_SCRIPTS;
542                 if (is_dir($searchPath)){
543                         $dir = dir($searchPath);
544                         while (($file = $dir->read()) !== false){
545                                 $filePath = $searchPath . '/' . $file;
546                                 $pathParts = pathinfo($file);
547                                 $ext = $pathParts['extension'];         // 拡張子
548                                         
549                                 // ファイルかどうかチェック
550                                 if (strncmp($file, '.', 1) != 0 && $file != '..' && is_file($filePath)
551                                         && strncmp($file, '_', 1) != 0 &&       // 「_」で始まる名前のファイルは読み込まない
552                                         $ext == self::SCRIPT_FILE_EXT){         // 拡張子をチェック
553                                         $hasScripts = true;
554                                         break;
555                                 }
556                         }
557                         $dir->close();
558                 }
559                 // CSSディレクトリをチェック
560                 $searchPath = $path . '/' . M3_DIR_NAME_CSS;
561                 if (is_dir($searchPath)){
562                         $dir = dir($searchPath);
563                         while (($file = $dir->read()) !== false){
564                                 $filePath = $searchPath . '/' . $file;
565                                 $pathParts = pathinfo($file);
566                                 $ext = $pathParts['extension'];         // 拡張子
567                                         
568                                 // ファイルかどうかチェック
569                                 if (strncmp($file, '.', 1) != 0 && $file != '..' && is_file($filePath)
570                                         && strncmp($file, '_', 1) != 0 &&       // 「_」で始まる名前のファイルは読み込まない
571                                         $ext == self::CSS_FILE_EXT){            // 拡張子をチェック
572                                         $hasCss = true;
573                                         break;
574                                 }
575                         }
576                         $dir->close();
577                 }
578                 // adminディレクトリをチェック
579                 $searchPath = $path . '/' . M3_DIR_NAME_ADMIN;
580                 if (is_dir($searchPath)){
581                         $dir = dir($searchPath);
582                         while (($file = $dir->read()) !== false){
583                                 $filePath = $searchPath . '/' . $file;
584                                 $pathParts = pathinfo($file);
585                                 $ext = $pathParts['extension'];         // 拡張子
586                                         
587                                 // ファイルかどうかチェック
588                                 if (strncmp($file, '.', 1) != 0 && $file != '..' && is_file($filePath)
589                                         && strncmp($file, '_', 1) != 0 &&       // 「_」で始まる名前のファイルは読み込まない
590                                         $ext == self::PHP_FILE_EXT){            // 拡張子をチェック
591                                         $hasAdmin = true;
592                                         break;
593                                 }
594                         }
595                         $dir->close();
596                 }
597                 return $ret;
598         }
599         /**
600          * タイプ選択メニュー作成
601          *
602          * @return なし
603          */
604         function createWidgetTypeMenu()
605         {
606                 for ($i = 0; $i < count($this->widgetTypeArray); $i++){
607                         $value = $this->widgetTypeArray[$i]['value'];
608                         $name = $this->widgetTypeArray[$i]['name'];
609                         
610                         $selected = '';
611                         if ($value == $this->widgetType) $selected = 'selected';
612                         
613                         $row = array(
614                                 'value'    => $value,                   // ページID
615                                 'name'     => $name,                    // ページ名
616                                 'selected' => $selected                                                                                                         // 選択中かどうか
617                         );
618                         $this->tmpl->addVars('item_type_list', $row);
619                         $this->tmpl->parseTemplate('item_type_list', 'a');
620                 }
621         }
622         /**
623          * ウィジェットリスト、取得したデータをテンプレートに設定する
624          *
625          * @param int $index                    行番号(0~)
626          * @param array $fetchedRow             フェッチ取得した行
627          * @param object $param                 未使用
628          * @return bool                                 true=処理続行の場合、false=処理終了の場合
629          */
630         function widgetListLoop($index, $fetchedRow, $param)
631         {
632                 // ウィジェットが存在するかどうかチェック
633                 $isExistsWidget = false;
634                 $widgetId = $fetchedRow['wd_id'];// ウィジェットID
635                 $widgetDir = $this->gEnv->getWidgetsPath() . '/' . $widgetId;                   // ウィジェットのディレクトリ
636                 if (file_exists($widgetDir)) $isExistsWidget = true;
637                 
638                 // 詳細表示の設定
639                 if ($this->showDetail){
640                         $this->tmpl->addVar('widgetlist', 'widgettype', 'detail');              // 詳細表示
641                 }
642                 
643                 $available = '';                                // 利用可能かどうか
644                 if ($fetchedRow['wd_available']){
645                         $available = 'checked';
646                 }
647                 $active = '';                   // ウィジェット実行可能かどうか
648                 if ($fetchedRow['wd_active']){
649                         $active = 'checked';
650                 }
651                 
652                 // 編集不可項目のときは、ボタンを使用不可にする
653                 $buttonEnabled = '';
654                 $availableDisabled = '';
655                 $activeDisabled = '';
656                 if (!$fetchedRow['wd_editable']){
657                         $buttonEnabled = 'disabled';
658                         $availableDisabled = 'disabled';
659                         $activeDisabled = 'disabled';
660                 }
661                 
662                 // 管理画面がないときは、詳細ボタンを使用不可にする
663                 $detailButtonEnabled = '';
664                 if (!$fetchedRow['wd_has_admin']) $detailButtonEnabled = 'disabled';
665                 
666                 // 新規に追加されたウィジェットかチェック
667                 $idText = $this->convertToDispString($widgetId);
668                 for ($i = 0; $i < count($this->newWidget); $i++){
669                         if ($this->newWidget[$i] == $widgetId){
670                                 $idText = '<b><font color="green">' . $this->convertToDispString($widgetId) . '</font></b>';
671                                 break;
672                         }
673                 }
674                 // ウィジェットの画像を設定
675                 if ($isExistsWidget){           // ウィジェットが存在するとき
676                         $iconTitle = '';
677                         $iconUrl = $this->gDesign->getWidgetIconUrl($widgetId, $this->defaultImageSize);
678                 } else {
679                         //$iconTitle = self::NOT_FOUND_WIDGET_MESSAGE;          // ウィジェットが見つかりません
680                         $iconTitle = $this->_('Widget not found.');             // ウィジェットが見つかりません
681                         $iconUrl = $this->gEnv->getRootUrl() . self::NOT_FOUND_WIDGET_ICON_FILE;
682                 }
683                 $imageTag = '<img class="widget_obj" src="' . $this->getUrl($iconUrl) . '" ';
684                 $imageTag .= 'width="' . $this->defaultImageSize . '"';
685                 $imageTag .= ' height="' . $this->defaultImageSize . '"';
686                 $imageTag .= ' border="0" alt="' . $iconTitle . '" title="' . $iconTitle . '" />';
687                 
688                 // ヘルプの作成
689                 $helpText = '';
690                 $title = $fetchedRow['wd_name'];
691                 if (!empty($title)){
692                         $helpText = $this->gInstance->getHelpManager()->createHelpText($title, $fetchedRow['wd_description']);
693                 }
694                 $idText = '<span ' . $helpText . '>' . $idText . '</span>';
695                                 
696                 // ボタンの状態
697                 $downloadButton = '';
698                 if (!$isExistsWidget) $downloadButton = 'disabled';
699                 if (!empty($fetchedRow['wd_license_type']))  $downloadButton = 'disabled';// ライセンスのチェック
700                 
701                 $row = array(
702                         'no' => $index + 1,                                                                                                     // 行番号
703                         'serial' => $this->convertToDispString($fetchedRow['wd_serial']),                       // シリアル番号
704                         'id' => $this->convertToDispString($widgetId),                  // ID
705                         'id_text' => $idText,
706                         'name' => $this->convertToDispString($fetchedRow['wd_name']),           // 名前
707                         'version' => $this->convertToDispString($fetchedRow['wd_version']),             // バージョン
708                         'release_dt' => $this->convertToDispDate($fetchedRow['wd_release_dt']), // リリース日時
709                         'available' => $available,                                                                                              // 利用可能かどうか
710                         'active' => $active,                                                                                                    // ウィジェット実行可能かどうか
711                         'available_disabled' => $availableDisabled,                                                     // 利用可能かどうか、使用制御
712                         'active_disabled' => $activeDisabled,                                                                   // ウィジェット実行可能かどうか、使用制御
713                         'update_button' => $buttonEnabled,                                                                      // 更新ボタンの使用制御
714                         'delete_button' => $buttonEnabled,                                                                      // 削除ボタンの使用制御
715                         'detail_button' => $detailButtonEnabled,                                                        // 詳細ボタンの使用制御
716                         'download_button' => $downloadButton,                                                           // ダウンロードボタン
717                         'image_tag' => $imageTag,               // 画像
718                         'label_widget_config' => $this->_('Configure'),                 // 設定
719                         'label_update' => $this->_('Update'),                   // 更新
720                         'label_delete' => $this->_('Delete'),                   // 削除
721                         'label_download' => $this->_('Download')                        // ダウンロード
722                 );
723                 $this->tmpl->addVars('widgetlist', $row);
724                 $this->tmpl->parseTemplate('widgetlist', 'a');
725                 
726                 $this->isExistsWidgetList = true;               // ウィジェットが存在する
727                 return true;
728         }
729 }
730 ?>