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

Revision 22685, 7.8 KB checked in by adachi, 11 years ago (diff)

#2181 execPluginに統合

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