source: branches/version-2_13-dev/data/class/plugin/SC_Plugin_Installer.php @ 23230

Revision 23230, 8.1 KB checked in by m_uehara, 11 years ago (diff)

#2363 r23177, r23181 - r23186, r23188 - r23191, r23194, r23197, r23199 - r23218, r23220, r23223 - r23225 をマージ

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    {
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['delete'] = array();
45        $this->arrInstallData['remove_file'] = array();
46        $this->arrInstallData['remove_directory'] = array();
47    }
48
49    public function execPlugin()
50    {
51        $this->log("start");
52
53        $plugin_code = $this->arrPlugin['plugin_code'];
54
55        // テーブル作成SQLなどを実行
56        $arrSql = $this->arrInstallData['sql'];
57        $arrErr = array();
58
59        // SQLの検証
60        foreach ($arrSql as $sql) {
61            $this->log("verify sql: " . $sql['sql']);
62            $error_message = $this->verifySql($sql['sql'], $sql['params']);
63            if (!is_null($error_message)) {
64                $this->log("verify sql: invalid sql " . $sql['sql']);
65                $this->log("verify sql: $error_message");
66                $arrErr[] = $error_message;
67            }
68        }
69
70        if (count($arrErr) > 0) {
71            return $arrErr;
72        }
73
74        $objQuery =& SC_Query_Ex::getSingletonInstance();
75
76        // SQLの実行
77        if(!SC_Utils_Ex::isBlank($arrSql)) {
78            foreach ($arrSql as $sql) {
79                $this->log("exec sql: " . $sql['sql']);
80                $objQuery->query($sql['sql'], $sql['params']);
81            }
82        }
83
84        $arrInsertQuery = $this->arrInstallData['insert'];
85        if(!SC_Utils_Ex::isBlank($arrInsertQuery)) {
86            foreach ($arrInsertQuery as $query) {
87                $objQuery->insert(
88                        $query['table'],
89                        $query['arrVal'],
90                        $query['arrSql'],
91                        $query['arrSqlVal'],
92                        $query['form'],
93                        $query['arrFromVal']
94                );
95            }
96        }
97
98        $arrUpdateQuery = $this->arrInstallData['update'];
99        if(!SC_Utils_Ex::isBlank($arrUpdateQuery)) {
100            foreach ($arrUpdateQuery as $query) {
101                $objQuery->update(
102                        $query['table'],
103                        $query['arrVal'],
104                        $query['where'],
105                        $query['arrWhereVal'],
106                        $query['arrRawSql'],
107                        $query['arrRawSqlVal']
108                );
109            }
110        }
111
112        // プラグインのディレクトリコピー
113        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
114
115        if(!SC_Utils_Ex::isBlank($arrCopyDirectories)) {
116            foreach ($arrCopyDirectories as $directory) {
117                $this->log("exec dir copy: " . $directory['src'] . ' -> ' . $directory['dist']);
118                // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
119                SC_Utils::copyDirectory(
120                        PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
121                        PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
122            }
123        }
124
125        // プラグインのファイルコピー
126        $arrCopyFiles = $this->arrInstallData['copy_file'];
127
128        if(!SC_Utils_Ex::isBlank($arrCopyFiles)) {
129            foreach ($arrCopyFiles as $file) {
130                $this->log("exec file copy: " . $file['src'] . ' -> ' . $file['dist']);
131                // ファイルコピー
132                copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
133                     PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
134            }
135        }
136
137        $this->log("end");
138    }
139
140    public function copyFile($src, $dist)
141    {
142        $this->arrInstallData['copy_file'][] = array(
143            'src'  => $src,
144            'dist' => $dist
145        );
146    }
147
148    public function copyDirectory($src, $dist)
149    {
150        $this->arrInstallData['copy_directory'][] = array(
151            'src'  => $src,
152            'dist' => $dist
153        );
154    }
155
156    public function removeFile($dist)
157    {
158        $this->arrInstallData['remove_file'][] = array(
159            'dist' => $dist
160        );
161    }
162
163    public function removeDirectory($dist)
164    {
165       $this->arrInstallData['remove_directory'][] = array(
166            'dist' => $dist
167        );
168    }
169
170    public function sql($sql, array $params = array())
171    {
172        $this->arrInstallData['sql'][] = array(
173            'sql'    => $sql,
174            'params' => $params
175        );
176    }
177
178    protected function log($msg)
179    {
180        $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg);
181        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
182    }
183
184    /**
185     * カラム追加クエリの追加
186     *
187     * @param type $table
188     * @param type $col
189     * @param type $type
190     */
191    public function addColumn($table_name, $col_name, $col_type)
192    {
193        $sql = "ALTER TABLE $table_name ADD $col_name $col_type ";
194        $this->sql($sql);
195    }
196
197    /**
198     * カラム削除クエリの追加
199     *
200     * @param type $table
201     * @param type $col
202     * @param type $type
203     */
204    public function dropColumn($table_name, $col_name)
205    {
206        $sql = "ALTER TABLE $table_name DROP $col_name";
207        $this->sql($sql);
208    }
209
210    public function insert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array())
211    {
212        $this->arrInstallData['insert'][] = array(
213            'table' => $table,
214            'arrVal' => $arrVal,
215            'arrSql' => $arrSql,
216            'arrSqlVal' => $arrSqlVal,
217            'form' =>$from,
218            'arrFromVal' => $arrFromVal
219        );
220    }
221
222    public function update($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array())
223    {
224        $this->arrInstallData['update'][] = array(
225            'table' => $table,
226            'arrVal' => $arrVal,
227            'where' => $where,
228            'arrWhereVal' => $arrWhereVal,
229            'arrRawSql' =>$arrRawSql,
230            'arrRawSqlVal' => $arrRawSqlVal
231        );
232    }
233
234    /**
235     *
236     * @param string $sql
237     * @param type   $params
238     */
239    protected function verifySql($sql, $params)
240    {
241        // FIXME $paramsのチェックも行いたい.
242        $objQuery =& SC_Query_Ex::getSingletonInstance();
243
244        // force runを有効にし, システムエラーを回避する
245        $objQuery->force_run = true;
246
247        // prepareでSQLを検証
248        $sth = $objQuery->prepare($sql);
249
250        if (PEAR::isError($sth)) {
251            $error_message = $sth->message . ":" . $sth->userinfo;
252            $objQuery->force_run = false;
253
254            return $error_message;
255        }
256
257        $sth->free();
258        // force_runをもとに戻す.
259        $objQuery->force_run = false;
260
261        return $error_message;
262    }
263}
Note: See TracBrowser for help on using the repository browser.