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