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

Revision 22682, 9.6 KB checked in by adachi, 11 years ago (diff)

#2181 SQLを検証し、問題ある場合はロールバック

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                $arrErr[] = $error_message;
129            }
130        }
131       
132        if (count($arrErr) > 0) {
133            $this->log("verify sql: err");
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        // プラグインのディレクトリコピー
146        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
147
148        foreach ($arrCopyDirectories as $directory) {
149            $this->log("exec dir copy: " . $directory['src'] . ' -> ' . $directory['dist']);
150            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
151            SC_Utils::copyDirectory(
152                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
153                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
154        }
155
156        // プラグインのファイルコピー
157        $arrCopyFiles = $this->arrInstallData['copy_file'];
158
159        foreach ($arrCopyFiles as $file) {
160            $this->log("exec file copy: " . $file['src'] . ' -> ' . $file['dist']);
161            // ファイルコピー
162            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
163                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
164        }
165       
166        $this->log("end");         
167    }
168
169    public function copyFile($src, $dist) {
170        $this->arrInstallData['copy_file'][] = array(
171            'src'  => $src,
172            'dist' => $dist
173        );
174    }
175 
176    public function copyDirectory($src, $dist) {
177        $this->arrInstallData['copy_directory'][] = array(
178            'src'  => $src,
179            'dist' => $dist
180        );       
181    }
182   
183    public function removeFile($dist) {
184        $this->arrInstallData['remove_file'][] = array(
185            'dist' => $dist
186        );
187    }
188   
189    public function removeDirectory($dist) {
190       $this->arrInstallData['remove_file'][] = array(
191            'dist' => $dist
192        );     
193    }
194
195    public function sql($sql, array $params = array()) {
196        $this->arrInstallData['sql'][] = array(
197            'sql'    => $sql,
198            'params' => $params
199        );
200    }
201   
202    public function query($sql, array $params = array()) {
203        $this->sql($sql, $params);
204    }
205   
206    protected function log($msg) {
207        $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg);
208        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
209    }
210   
211    /**
212     * カラム追加クエリの追加
213     *
214     * @param type $table
215     * @param type $col
216     * @param type $type
217     */
218    function sqlAterTableAdd($table_name, $col_name, $col_type) {
219        $sql = ("ALTER TABLE $table_name ADD $col_name $col_type ");
220        $this->sql($sql);
221    }
222   
223    /**
224     * カラム削除クエリの追加
225     *
226     * @param type $table
227     * @param type $col
228     * @param type $type
229     */
230    function sqlAterTableDrop($table_name, $col_name) {
231        $sql = ("ALTER TABLE $table_name DROP $col_name");
232        $this->sql($sql);
233    }
234   
235   
236    function sqlInsert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
237        $this->arrInstallData['insert'] = array(
238            'params'   => array(
239                'table' => $table,
240                'arrVal' => $arrVal,
241                'arrSql' => $arrSql,
242                'arrSqlVal' => $arrSqlVal,
243                'form' =>$from,
244                'arrFromVal' => $arrFromVal)
245        );
246    }
247   
248    function sqlUpdate($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
249        $this->arrInstallData['update'] = array(
250            'params'   => array(
251                'table' => $table,
252                'arrVal' => $arrVal,
253                'where' => $where,
254                'arrWhereVal' => $arrWhereVal,
255                'arrRawSql' =>$arrRawSql,
256                'arrRawSqlVal' => $arrRawSqlVal)
257        );
258    }
259   
260    /**
261     *
262     * @param string $sql
263     * @param type $params
264     */
265    protected function verifySql($sql, $params) {
266        // FIXME $paramsのチェックも行いたい.
267        $objQuery =& SC_Query_Ex::getSingletonInstance();
268       
269        // force runを有効にし, システムエラーを回避する
270        $objQuery->force_run = true;
271
272        // prepareでSQLを検証
273        $sth = $objQuery->prepare($sql);
274
275        if (PEAR::isError($sth)) {
276            $error_message = $sth->message . ":" . $sth->userinfo;
277        }
278        // force_runをもとに戻す.
279        $objQuery->force_run = false;
280       
281        return $error_message;
282    }
283}
Note: See TracBrowser for help on using the repository browser.