source: branches/version-2_13_0/data/class/plugin/SC_Plugin_Installer.php @ 23126

Revision 23126, 7.6 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

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