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