1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | require_once(CLASS_EX_REALDIR . "page_extends/admin/LC_Page_Admin_Ex.php"); |
---|
26 | require_once(CLASS_EX_REALDIR . "helper_extends/SC_Helper_FileManager_Ex.php"); |
---|
27 | |
---|
28 | /** |
---|
29 | * システム情報 のページクラス. |
---|
30 | * |
---|
31 | * @package Page |
---|
32 | * @author LOCKON CO.,LTD. |
---|
33 | * @version $Id$ |
---|
34 | */ |
---|
35 | class LC_Page_Admin_System_Plugin extends LC_Page_Admin_Ex { |
---|
36 | |
---|
37 | // }}} |
---|
38 | // {{{ functions |
---|
39 | |
---|
40 | /** |
---|
41 | * Page を初期化する. |
---|
42 | * |
---|
43 | * @return void |
---|
44 | */ |
---|
45 | function init() { |
---|
46 | parent::init(); |
---|
47 | $this->tpl_mainpage = 'system/plugin.tpl'; |
---|
48 | $this->tpl_subnavi = 'system/subnavi.tpl'; |
---|
49 | $this->tpl_subno = 'plugin'; |
---|
50 | $this->tpl_mainno = 'system'; |
---|
51 | $this->tpl_subtitle = 'プラグイン管理'; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Page のプロセス. |
---|
56 | * |
---|
57 | * @return void |
---|
58 | */ |
---|
59 | function process() { |
---|
60 | $this->action(); |
---|
61 | $this->sendResponse(); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Page のアクション. |
---|
66 | * |
---|
67 | * @return void |
---|
68 | */ |
---|
69 | function action() { |
---|
70 | // パラメータ管理クラス |
---|
71 | $objFormParam = new SC_FormParam_Ex(); |
---|
72 | // パラメータ情報の初期化 |
---|
73 | $this->lfInitParam($objFormParam); |
---|
74 | $objFormParam->setParam($_POST); |
---|
75 | |
---|
76 | $mode = $this->getMode(); |
---|
77 | |
---|
78 | switch($mode) { |
---|
79 | case 'install': |
---|
80 | case 'uninstall': |
---|
81 | case 'enable': |
---|
82 | case 'disable': |
---|
83 | // エラーチェック |
---|
84 | $this->arrErr = $objFormParam->checkError(); |
---|
85 | |
---|
86 | if(count($this->arrErr) == 0) { |
---|
87 | $plugin_id = $objFormParam->getValue('plugin_id'); |
---|
88 | $plugin_code = $objFormParam->getValue('plugin_code'); |
---|
89 | |
---|
90 | // プラグインファイルを読み込み、modeで指定されたメソッドを実行 |
---|
91 | $this->arrErr = $this->lfExecPlugin($plugin_id, $plugin_code, $mode); |
---|
92 | } |
---|
93 | break; |
---|
94 | case 'upload': |
---|
95 | // プラグイン情報を設定 |
---|
96 | $plugin_code = $this->lfGetPluginCode($_FILES['plugin_file']['name']); |
---|
97 | $plugin_dir = $this->lfGetPluginDir($plugin_code); |
---|
98 | |
---|
99 | // ファイルアップロード情報を設定 |
---|
100 | $objUpFile = new SC_UploadFile_Ex(TEMPLATE_TEMP_REALDIR, $plugin_dir); |
---|
101 | $this->lfInitUploadFile($objUpFile); |
---|
102 | |
---|
103 | // エラーチェック |
---|
104 | $this->arrErr = $this->lfCheckErrorUploadFile($plugin_code, $plugin_dir); |
---|
105 | |
---|
106 | if(count($this->arrErr) == 0) { |
---|
107 | // 一時ディレクトリへアップロード |
---|
108 | $this->arrErr['plugin_file'] = $objUpFile->makeTempFile('plugin_file', false); |
---|
109 | |
---|
110 | if($this->arrErr['plugin_file'] == "") { |
---|
111 | // プラグイン保存ディレクトリへ解凍 |
---|
112 | $this->arrErr = $this->lfUploadPlugin($objUpFile, $plugin_dir, $plugin_code, $_FILES['plugin_file']['name']); |
---|
113 | |
---|
114 | // 完了メッセージアラート設定 |
---|
115 | if(count($this->arrErr) == 0) { |
---|
116 | $this->tpl_onload = "alert('プラグインをアップロードしました。');"; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | break; |
---|
121 | default: |
---|
122 | break; |
---|
123 | } |
---|
124 | |
---|
125 | // DBからプラグイン情報を取得 |
---|
126 | $this->plugins = SC_Helper_Plugin_Ex::getAllPlugin(); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * デストラクタ. |
---|
131 | * |
---|
132 | * @return void |
---|
133 | */ |
---|
134 | function destroy() { |
---|
135 | parent::destroy(); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * パラメータ初期化. |
---|
140 | * |
---|
141 | * @param object $objFormParam |
---|
142 | * @return void |
---|
143 | * |
---|
144 | */ |
---|
145 | function lfInitParam(&$objFormParam) { |
---|
146 | $objFormParam->addParam('mode', 'mode', INT_LEN, '', array('ALPHA_CHECK', 'MAX_LENGTH_CHECK')); |
---|
147 | $objFormParam->addParam('plugin_id', 'plugin_id', INT_LEN, '', array('NUM_CHECK', 'MAX_LENGTH_CHECK')); |
---|
148 | $objFormParam->addParam('plugin_code', 'plugin_code', MTEXT_LEN, '', array('ALPHA_CHECK', 'MAX_LENGTH_CHECK')); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * アップロードファイルパラメータ初期化. |
---|
153 | * |
---|
154 | * @param object $objUpFile SC_UploadFileのインスタンス. |
---|
155 | * @return void |
---|
156 | */ |
---|
157 | function lfInitUploadFile(&$objUpFile) { |
---|
158 | $objUpFile->addFile("プラグイン", 'plugin_file', array('tar', 'tar.gz'), TEMPLATE_SIZE, true, 0, 0, false); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * アップロードファイルのエラーチェック. |
---|
163 | * |
---|
164 | * @param string $plugin_code |
---|
165 | * @param string $plugin_dir |
---|
166 | * @return array エラー情報を格納した連想配列. |
---|
167 | * |
---|
168 | */ |
---|
169 | function lfCheckErrorUploadFile($plugin_code, $plugin_dir) { |
---|
170 | $arrErr = array(); |
---|
171 | |
---|
172 | // プラグイン重複チェック |
---|
173 | $plugins = SC_Helper_Plugin_Ex::getAllPlugin(); |
---|
174 | foreach($plugins as $val) { |
---|
175 | if($val['plugin_code'] == $plugin_code) { |
---|
176 | $arrErr['plugin_file'] = "※ 同名のプラグインがすでに登録されています。<br/>"; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | return $arrErr; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * プラグイン名(物理名)を取得する. |
---|
185 | * (アップロードされたファイル名をプラグイン名(物理名)とする). |
---|
186 | * |
---|
187 | * @param string $upload_file_name |
---|
188 | * @return string プラグイン名(物理名). |
---|
189 | * |
---|
190 | */ |
---|
191 | function lfGetPluginCode($upload_file_name) { |
---|
192 | $array_ext = explode(".", $upload_file_name); |
---|
193 | return $array_ext[0]; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * プラグイン保存ディレクトリのパスを取得する. |
---|
198 | * |
---|
199 | * @param string $plugin_code |
---|
200 | * @return string プラグイン保存ディレクトリ. |
---|
201 | * |
---|
202 | */ |
---|
203 | function lfGetPluginDir($plugin_code) { |
---|
204 | $plugin_dir = DATA_REALDIR . 'plugin/' . $plugin_code . '/'; |
---|
205 | return $plugin_dir; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * プラグインファイルのパスを取得する. |
---|
210 | * |
---|
211 | * @param string $plugin_code |
---|
212 | * @return string プラグインファイルパス. |
---|
213 | */ |
---|
214 | function lfGetPluginFilePath($plugin_code) { |
---|
215 | $plugin_file_path = $this->lfGetPluginDir($plugin_code) . $plugin_code . '.php'; |
---|
216 | return $plugin_file_path; |
---|
217 | } |
---|
218 | /** |
---|
219 | * プラグインをアップロードする. |
---|
220 | * |
---|
221 | * @param object $objUpFile |
---|
222 | * @param string $plugin_dir |
---|
223 | * @param string $plugin_code |
---|
224 | * @param string $plugin_file_name |
---|
225 | * @return array エラー情報を格納した連想配列. |
---|
226 | * |
---|
227 | */ |
---|
228 | function lfUploadPlugin(&$objUpFile, $plugin_dir, $plugin_code, $plugin_file_name) { |
---|
229 | $arrErr = array(); |
---|
230 | |
---|
231 | // 必須チェック |
---|
232 | $arrErr = $objUpFile->checkEXISTS('plugin_file'); |
---|
233 | |
---|
234 | if(count($arrErr) == 0) { |
---|
235 | // プラグイン保存ディレクトリ作成 |
---|
236 | if(file_exists($plugin_dir)) { |
---|
237 | $arrErr['plugin_file'] = "※ 同名のディレクトリがすでに存在します。<br/>"; |
---|
238 | } else { |
---|
239 | mkdir($plugin_dir); |
---|
240 | } |
---|
241 | |
---|
242 | if(count($arrErr) == 0) { |
---|
243 | // 一時ディレクトリからプラグイン保存ディレクトリへ移動 |
---|
244 | $objUpFile->moveTempFile(); |
---|
245 | |
---|
246 | // プラグイン保存ディレクトリへ解凍 |
---|
247 | SC_Helper_FileManager_Ex::unpackFile($plugin_dir . $plugin_file_name); |
---|
248 | |
---|
249 | // プラグイン情報をDB登録 |
---|
250 | $this->lfRegistData($plugin_dir, $plugin_code); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | return $arrErr; |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * プラグイン情報をDB登録. |
---|
259 | * |
---|
260 | * @param string $plugin_dir |
---|
261 | * @param string $plugin_code |
---|
262 | * @return void |
---|
263 | * |
---|
264 | */ |
---|
265 | function lfRegistData($plugin_dir, $plugin_code) { |
---|
266 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
267 | $sqlval = array(); |
---|
268 | |
---|
269 | $sqlval['plugin_id'] = $objQuery->nextVal('dtb_plugin_plugin_id'); |
---|
270 | $sqlval['plugin_code'] = $plugin_code; |
---|
271 | $sqlval['status'] = PLUGIN_STATUS_UPLOADED; |
---|
272 | $sqlval['enable'] = PLUGIN_ENABLE_FALSE; |
---|
273 | $sqlval['update_date'] = 'now()'; |
---|
274 | $objQuery->insert('dtb_plugin', $sqlval); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * プラグインファイルを読み込む. |
---|
279 | * |
---|
280 | * @param string $plugin_code |
---|
281 | * @return array エラー情報を格納した連想配列. |
---|
282 | */ |
---|
283 | function lfRequirePluginFile($plugin_code) { |
---|
284 | $arrErr = array(); |
---|
285 | $plugin_file_path = $this->lfGetPluginFilePath($plugin_code); |
---|
286 | |
---|
287 | if(file_exists($plugin_file_path)) { |
---|
288 | require_once($plugin_file_path); |
---|
289 | } else { |
---|
290 | $arrErr['plugin_error'] = "※ " . $plugin_code . ".phpが存在しないため実行できません。<br/>"; |
---|
291 | } |
---|
292 | |
---|
293 | return $arrErr; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * プラグインファイルを読み込み、指定されたメソッドを実行する. |
---|
298 | * |
---|
299 | * @param integer $plugin_id |
---|
300 | * @param string $plugin_code |
---|
301 | * @param string $exec_mode プラグイン実行メソッド名. |
---|
302 | * @return array エラー情報を格納した連想配列. |
---|
303 | * |
---|
304 | */ |
---|
305 | function lfExecPlugin($plugin_id, $plugin_code, $exec_mode) { |
---|
306 | $arrErr = array(); |
---|
307 | |
---|
308 | // プラグインファイル読み込み |
---|
309 | $arrErr = $this->lfRequirePluginFile($plugin_code); |
---|
310 | |
---|
311 | if(count($arrErr) == 0) { |
---|
312 | $plugin = new $plugin_code(); |
---|
313 | $arrErr = $plugin->$exec_mode($plugin_id); |
---|
314 | } |
---|
315 | |
---|
316 | return $arrErr; |
---|
317 | } |
---|
318 | } |
---|