source: branches/camp/camp-2_13-plugin/data/class/plugin/SC_Plugin_Installer.php @ 22683

Revision 22683, 10.3 KB checked in by adachi, 11 years ago (diff)

#2181 insertとかを動くように修正

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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 */
23class SC_Plugin_Installer {
24   
25    protected $exec_func;
26   
27    protected $plugin_code;
28   
29    protected $arrPlugin;
30   
31    protected $arrInstallData;
32   
33    public function __construct($exec_func, $arrPlugin) {
34        define('PLUGIN_LOG_REALFILE', DATA_REALDIR . "logs/plugin.log");
35        $this->exec_func   = $exec_func;
36        $this->plugin_code = $arrPlugin['plugin_code'];
37        $this->arrPlugin   = $arrPlugin;
38        $this->arrInstallData = array();
39        $this->arrInstallData['sql'] = array();
40        $this->arrInstallData['copy_file'] = array();
41        $this->arrInstallData['copy_direcrtory'] = array();
42        $this->arrInstallData['insert'] = array();
43        $this->arrInstallData['update'] = array();
44        $this->arrInstallData['remove_file'] = array();
45        $this->arrInstallData['remove_directory'] = array();
46    }
47   
48    public function execInstall() {
49        GC_Utils_Ex::gfPrintLog("start install: " . $this->arrPlugin['plugin_code']);
50       
51        $plugin_code = $this->arrPlugin['plugin_code'];
52
53        $objQuery =& SC_Query::getSingletonInstance();
54       
55        // テーブル作成SQLなどを実行
56        $arrSql = $this->arrInstallData['sql'];
57       
58        foreach ($arrSql as $sql) {
59            GC_Utils_Ex::gfPrintLog("exec sql:" . $sql['sql']);
60            $objQuery->query($sql['sql'], $sql['params']);
61        }
62       
63        $arrInsertQuery = $this->arrInstallData['insert'];
64        foreach ($arrInsertQuery as $insertQuery) {
65            $objQuery->insert(
66                    $insertQuery['table'],
67                    $insertQuery['arrVal'],
68                    $insertQuery['arrSql'],
69                    $insertQuery['arrSqlVal'],
70                    $insertQuery['form'],
71                    $insertQuery['arrFromVal']
72                    );
73        }
74       
75           
76        $arrInsertQuery = $this->arrInstallData['update'];
77        foreach ($arrInsertQuery as $insertQuery) {
78            $objQuery->update(
79                    $insertQuery['table'],
80                    $insertQuery['arrVal'],
81                    $insertQuery['where'],
82                    $insertQuery['arrWhereVal'],
83                    $insertQuery['arrRawSql'],
84                    $insertQuery['arrRawSqlVal']
85                    );
86        }
87                           
88        // プラグインのディレクトリコピー
89        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
90
91        foreach ($arrCopyDirectories as $directory) {
92            GC_Utils_Ex::gfPrintLog("exec dir copy:" . $directory['src']);
93            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
94            SC_Utils::copyDirectory(
95                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
96                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
97        }
98
99        // プラグインのファイルコピー
100        $arrCopyFiles = $this->arrInstallData['copy_file'];
101
102        foreach ($arrCopyFiles as $file) {
103            GC_Utils_Ex::gfPrintLog("exec file copy:" . $file['src']);
104            // ファイルコピー
105            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
106                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
107        }
108
109        GC_Utils_Ex::gfPrintLog("end install: " . $this->arrPlugin['plugin_code']);
110    }
111   
112    public function execPlugin() {
113        $this->log("start");
114       
115        $plugin_code = $this->arrPlugin['plugin_code'];
116
117
118        // テーブル作成SQLなどを実行
119        $arrSql = $this->arrInstallData['sql'];
120        $arrErr = array();
121
122        // SQLの検証
123        foreach ($arrSql as $sql) {
124            $this->log("verify sql: " . $sql['sql']);
125            $error_message = $this->verifySql($sql['sql'], $sql['params']);
126            if (!is_null($error_message)) {
127                $this->log("verify sql: invalid sql " . $sql['sql']);
128                $this->log("verify sql: $error_message");
129                $arrErr[] = $error_message;
130            }
131        }
132       
133        if (count($arrErr) > 0) {
134            return $arrErr;
135        }
136       
137        $objQuery =& SC_Query_Ex::getSingletonInstance();
138       
139        // SQLの実行
140        foreach ($arrSql as $sql) {
141            $this->log("exec sql: " . $sql['sql']);
142            $objQuery->query($sql['sql'], $sql['params']);
143        }
144       
145        $arrInsertQuery = $this->arrInstallData['insert'];
146        foreach ($arrInsertQuery as $query) {
147            $objQuery->insert(
148                    $query['table'],
149                    $query['arrVal'],
150                    $query['arrSql'],
151                    $query['arrSqlVal'],
152                    $query['form'],
153                    $query['arrFromVal']
154            );
155        }
156       
157        $arrUpdateQuery = $this->arrInstallData['update'];
158        foreach ($arrUpdateQuery as $query) {
159            $objQuery->update(
160                    $query['table'],
161                    $query['arrVal'],
162                    $query['where'],
163                    $query['arrWhereVal'],
164                    $query['arrRawSql'],
165                    $query['arrRawSqlVal']
166            );
167        }
168       
169        // プラグインのディレクトリコピー
170        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
171
172        foreach ($arrCopyDirectories as $directory) {
173            $this->log("exec dir copy: " . $directory['src'] . ' -> ' . $directory['dist']);
174            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
175            SC_Utils::copyDirectory(
176                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
177                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
178        }
179
180        // プラグインのファイルコピー
181        $arrCopyFiles = $this->arrInstallData['copy_file'];
182
183        foreach ($arrCopyFiles as $file) {
184            $this->log("exec file copy: " . $file['src'] . ' -> ' . $file['dist']);
185            // ファイルコピー
186            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
187                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
188        }
189       
190        $this->log("end");         
191    }
192
193    public function copyFile($src, $dist) {
194        $this->arrInstallData['copy_file'][] = array(
195            'src'  => $src,
196            'dist' => $dist
197        );
198    }
199 
200    public function copyDirectory($src, $dist) {
201        $this->arrInstallData['copy_directory'][] = array(
202            'src'  => $src,
203            'dist' => $dist
204        );       
205    }
206   
207    public function removeFile($dist) {
208        $this->arrInstallData['remove_file'][] = array(
209            'dist' => $dist
210        );
211    }
212   
213    public function removeDirectory($dist) {
214       $this->arrInstallData['remove_file'][] = array(
215            'dist' => $dist
216        );     
217    }
218
219    public function sql($sql, array $params = array()) {
220        $this->arrInstallData['sql'][] = array(
221            'sql'    => $sql,
222            'params' => $params
223        );
224    }
225   
226    public function query($sql, array $params = array()) {
227        $this->sql($sql, $params);
228    }
229   
230    protected function log($msg) {
231        $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg);
232        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
233    }
234   
235    /**
236     * カラム追加クエリの追加
237     *
238     * @param type $table
239     * @param type $col
240     * @param type $type
241     */
242    function sqlAterTableAdd($table_name, $col_name, $col_type) {
243        $sql = "ALTER TABLE $table_name ADD $col_name $col_type ";
244        $this->sql($sql);
245    }
246   
247    /**
248     * カラム削除クエリの追加
249     *
250     * @param type $table
251     * @param type $col
252     * @param type $type
253     */
254    function sqlAterTableDrop($table_name, $col_name) {
255        $sql = "ALTER TABLE $table_name DROP $col_name";
256        $this->sql($sql);
257    }
258   
259   
260    function sqlInsert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
261        $this->arrInstallData['insert'][] = array(
262            'table' => $table,
263            'arrVal' => $arrVal,
264            'arrSql' => $arrSql,
265            'arrSqlVal' => $arrSqlVal,
266            'form' =>$from,
267            'arrFromVal' => $arrFromVal
268        );
269    }
270   
271    function sqlUpdate($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
272        $this->arrInstallData['update'][] = array(
273            'table' => $table,
274            'arrVal' => $arrVal,
275            'where' => $where,
276            'arrWhereVal' => $arrWhereVal,
277            'arrRawSql' =>$arrRawSql,
278            'arrRawSqlVal' => $arrRawSqlVal
279        );
280    }
281   
282    /**
283     *
284     * @param string $sql
285     * @param type $params
286     */
287    protected function verifySql($sql, $params) {
288        // FIXME $paramsのチェックも行いたい.
289        $objQuery =& SC_Query_Ex::getSingletonInstance();
290       
291        // force runを有効にし, システムエラーを回避する
292        $objQuery->force_run = true;
293
294        // prepareでSQLを検証
295        $sth = $objQuery->prepare($sql);
296
297        if (PEAR::isError($sth)) {
298            $error_message = $sth->message . ":" . $sth->userinfo;
299        }
300        // force_runをもとに戻す.
301        $objQuery->force_run = false;
302       
303        return $error_message;
304    }
305}
Note: See TracBrowser for help on using the repository browser.