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

Revision 22688, 7.7 KB checked in by h_yoshimoto, 11 years ago (diff)

#2181 不要そうな関数を削除

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    protected function log($msg) {
163        $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg);
164        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
165    }
166   
167    /**
168     * カラム追加クエリの追加
169     *
170     * @param type $table
171     * @param type $col
172     * @param type $type
173     */
174    function sqlAterTableAdd($table_name, $col_name, $col_type) {
175        $sql = "ALTER TABLE $table_name ADD $col_name $col_type ";
176        $this->sql($sql);
177    }
178   
179    /**
180     * カラム削除クエリの追加
181     *
182     * @param type $table
183     * @param type $col
184     * @param type $type
185     */
186    function sqlAterTableDrop($table_name, $col_name) {
187        $sql = "ALTER TABLE $table_name DROP $col_name";
188        $this->sql($sql);
189    }
190   
191   
192    function sqlInsert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
193        $this->arrInstallData['insert'][] = array(
194            'table' => $table,
195            'arrVal' => $arrVal,
196            'arrSql' => $arrSql,
197            'arrSqlVal' => $arrSqlVal,
198            'form' =>$from,
199            'arrFromVal' => $arrFromVal
200        );
201    }
202   
203    function sqlUpdate($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
204        $this->arrInstallData['update'][] = array(
205            'table' => $table,
206            'arrVal' => $arrVal,
207            'where' => $where,
208            'arrWhereVal' => $arrWhereVal,
209            'arrRawSql' =>$arrRawSql,
210            'arrRawSqlVal' => $arrRawSqlVal
211        );
212    }
213   
214    /**
215     *
216     * @param string $sql
217     * @param type $params
218     */
219    protected function verifySql($sql, $params) {
220        // FIXME $paramsのチェックも行いたい.
221        $objQuery =& SC_Query_Ex::getSingletonInstance();
222       
223        // force runを有効にし, システムエラーを回避する
224        $objQuery->force_run = true;
225
226        // prepareでSQLを検証
227        $sth = $objQuery->prepare($sql);
228
229        if (PEAR::isError($sth)) {
230            $error_message = $sth->message . ":" . $sth->userinfo;
231        }
232        // force_runをもとに戻す.
233        $objQuery->force_run = false;
234       
235        return $error_message;
236    }
237}
Note: See TracBrowser for help on using the repository browser.