[22630] | 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 | |
---|
[22673] | 25 | protected $exec_func; |
---|
| 26 | |
---|
[22661] | 27 | protected $plugin_code; |
---|
| 28 | |
---|
[22630] | 29 | protected $arrPlugin; |
---|
| 30 | |
---|
| 31 | protected $arrInstallData; |
---|
| 32 | |
---|
[22673] | 33 | public function __construct($exec_func, $arrPlugin) { |
---|
| 34 | $this->exec_func = $exec_func; |
---|
| 35 | $this->plugin_code = $arrPlugin['plugin_code']; |
---|
[22661] | 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(); |
---|
[22682] | 41 | $this->arrInstallData['insert'] = array(); |
---|
| 42 | $this->arrInstallData['update'] = array(); |
---|
[22990] | 43 | $this->arrInstallData['delete'] = array(); |
---|
[22661] | 44 | $this->arrInstallData['remove_file'] = array(); |
---|
| 45 | $this->arrInstallData['remove_directory'] = array(); |
---|
[22630] | 46 | } |
---|
| 47 | |
---|
[22676] | 48 | public function execPlugin() { |
---|
[22673] | 49 | $this->log("start"); |
---|
| 50 | |
---|
| 51 | $plugin_code = $this->arrPlugin['plugin_code']; |
---|
| 52 | |
---|
| 53 | // テーブル作成SQLなどを実行 |
---|
| 54 | $arrSql = $this->arrInstallData['sql']; |
---|
[22682] | 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']); |
---|
[22683] | 63 | $this->log("verify sql: $error_message"); |
---|
[22682] | 64 | $arrErr[] = $error_message; |
---|
| 65 | } |
---|
| 66 | } |
---|
[22673] | 67 | |
---|
[22682] | 68 | if (count($arrErr) > 0) { |
---|
| 69 | return $arrErr; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 73 | |
---|
| 74 | // SQLの実行 |
---|
[22673] | 75 | foreach ($arrSql as $sql) { |
---|
| 76 | $this->log("exec sql: " . $sql['sql']); |
---|
| 77 | $objQuery->query($sql['sql'], $sql['params']); |
---|
| 78 | } |
---|
| 79 | |
---|
[22683] | 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 | |
---|
[22673] | 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 | |
---|
[22662] | 128 | public function copyFile($src, $dist) { |
---|
[22661] | 129 | $this->arrInstallData['copy_file'][] = array( |
---|
[22662] | 130 | 'src' => $src, |
---|
[22646] | 131 | 'dist' => $dist |
---|
| 132 | ); |
---|
[22630] | 133 | } |
---|
[22661] | 134 | |
---|
[22662] | 135 | public function copyDirectory($src, $dist) { |
---|
[22661] | 136 | $this->arrInstallData['copy_directory'][] = array( |
---|
[22662] | 137 | 'src' => $src, |
---|
[22646] | 138 | 'dist' => $dist |
---|
| 139 | ); |
---|
| 140 | } |
---|
[22661] | 141 | |
---|
[22662] | 142 | public function removeFile($dist) { |
---|
[22661] | 143 | $this->arrInstallData['remove_file'][] = array( |
---|
| 144 | 'dist' => $dist |
---|
| 145 | ); |
---|
[22630] | 146 | } |
---|
| 147 | |
---|
[22662] | 148 | public function removeDirectory($dist) { |
---|
[22990] | 149 | $this->arrInstallData['remove_directory'][] = array( |
---|
[22661] | 150 | 'dist' => $dist |
---|
| 151 | ); |
---|
| 152 | } |
---|
| 153 | |
---|
[22652] | 154 | public function sql($sql, array $params = array()) { |
---|
[22661] | 155 | $this->arrInstallData['sql'][] = array( |
---|
[22630] | 156 | 'sql' => $sql, |
---|
| 157 | 'params' => $params |
---|
| 158 | ); |
---|
| 159 | } |
---|
[22665] | 160 | |
---|
[22673] | 161 | protected function log($msg) { |
---|
[22676] | 162 | $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg); |
---|
[22673] | 163 | GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE); |
---|
| 164 | } |
---|
[22675] | 165 | |
---|
| 166 | /** |
---|
| 167 | * カラム追加クエリの追加 |
---|
| 168 | * |
---|
| 169 | * @param type $table |
---|
| 170 | * @param type $col |
---|
| 171 | * @param type $type |
---|
| 172 | */ |
---|
[22990] | 173 | function addColumn($table_name, $col_name, $col_type) { |
---|
[22683] | 174 | $sql = "ALTER TABLE $table_name ADD $col_name $col_type "; |
---|
[22675] | 175 | $this->sql($sql); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * カラム削除クエリの追加 |
---|
| 180 | * |
---|
| 181 | * @param type $table |
---|
| 182 | * @param type $col |
---|
| 183 | * @param type $type |
---|
| 184 | */ |
---|
[22990] | 185 | function dropColumn($table_name, $col_name) { |
---|
[22683] | 186 | $sql = "ALTER TABLE $table_name DROP $col_name"; |
---|
[22675] | 187 | $this->sql($sql); |
---|
| 188 | } |
---|
| 189 | |
---|
[22990] | 190 | function insert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) { |
---|
[22683] | 191 | $this->arrInstallData['insert'][] = array( |
---|
| 192 | 'table' => $table, |
---|
| 193 | 'arrVal' => $arrVal, |
---|
| 194 | 'arrSql' => $arrSql, |
---|
| 195 | 'arrSqlVal' => $arrSqlVal, |
---|
| 196 | 'form' =>$from, |
---|
| 197 | 'arrFromVal' => $arrFromVal |
---|
[22675] | 198 | ); |
---|
| 199 | } |
---|
| 200 | |
---|
[22990] | 201 | function update($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) { |
---|
[22683] | 202 | $this->arrInstallData['update'][] = array( |
---|
| 203 | 'table' => $table, |
---|
| 204 | 'arrVal' => $arrVal, |
---|
| 205 | 'where' => $where, |
---|
| 206 | 'arrWhereVal' => $arrWhereVal, |
---|
| 207 | 'arrRawSql' =>$arrRawSql, |
---|
| 208 | 'arrRawSqlVal' => $arrRawSqlVal |
---|
[22675] | 209 | ); |
---|
[22682] | 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * |
---|
| 214 | * @param string $sql |
---|
| 215 | * @param type $params |
---|
| 216 | */ |
---|
| 217 | protected function verifySql($sql, $params) { |
---|
| 218 | // FIXME $paramsのチェックも行いたい. |
---|
| 219 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 220 | |
---|
| 221 | // force runを有効にし, システムエラーを回避する |
---|
| 222 | $objQuery->force_run = true; |
---|
| 223 | |
---|
| 224 | // prepareでSQLを検証 |
---|
| 225 | $sth = $objQuery->prepare($sql); |
---|
| 226 | |
---|
| 227 | if (PEAR::isError($sth)) { |
---|
| 228 | $error_message = $sth->message . ":" . $sth->userinfo; |
---|
[22699] | 229 | $objQuery->force_run = false; |
---|
| 230 | return $error_message; |
---|
[22682] | 231 | } |
---|
[22699] | 232 | |
---|
| 233 | $sth->free(); |
---|
[22682] | 234 | // force_runをもとに戻す. |
---|
| 235 | $objQuery->force_run = false; |
---|
| 236 | |
---|
| 237 | return $error_message; |
---|
| 238 | } |
---|
[22675] | 239 | } |
---|