[15665] | 1 | <?php |
---|
| 2 | /* |
---|
[16582] | 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[15665] | 5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
[16582] | 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. |
---|
[15665] | 22 | */ |
---|
| 23 | |
---|
| 24 | // {{{ requires |
---|
[15895] | 25 | require_once(CLASS_PATH . "batch/SC_Batch.php"); |
---|
[15665] | 26 | |
---|
| 27 | /** |
---|
| 28 | * アップデート機能 のバッチクラス. |
---|
| 29 | * |
---|
| 30 | * XXX Singleton にするべき... |
---|
| 31 | * |
---|
| 32 | * @package Batch |
---|
| 33 | * @author LOCKON CO.,LTD. |
---|
| 34 | * @version $Id$ |
---|
| 35 | */ |
---|
| 36 | class SC_Batch_Update extends SC_Batch { |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * 変換したいファイルの拡張子をカンマ区切りで羅列. |
---|
| 40 | */ |
---|
[16984] | 41 | var $includes = "php,inc,tpl,css,sql,js,png,jpg,gif,swf"; |
---|
[15665] | 42 | |
---|
| 43 | /** |
---|
| 44 | * 除外するファイル名をカンマ区切りで羅列. |
---|
| 45 | */ |
---|
[16862] | 46 | var $excludes = "distinfo.php"; |
---|
[15665] | 47 | |
---|
| 48 | /** |
---|
| 49 | * バッチ処理を実行する. |
---|
| 50 | * |
---|
| 51 | * @param string $target アップデータファイルのディレクトリパス |
---|
| 52 | * @return void |
---|
| 53 | */ |
---|
| 54 | function execute($target = ".") { |
---|
[17141] | 55 | $msg = ''; |
---|
[16862] | 56 | $oldMask = umask(0); |
---|
| 57 | $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容 |
---|
| 58 | $bkupPath = DATA_PATH . 'downloads/backup/update_' . time() . '/'; |
---|
| 59 | $bkupPathFile = $bkupPath . 'files/'; |
---|
| 60 | $this->mkdir_p($bkupPathFile . 'dummy'); |
---|
| 61 | |
---|
| 62 | $arrLog = array( |
---|
| 63 | 'err' => array(), |
---|
| 64 | 'ok' => array(), |
---|
| 65 | 'buckup_path' => $bkupPath |
---|
| 66 | ); |
---|
| 67 | |
---|
| 68 | if (!is_writable($bkupPath) || !is_writable($bkupPathFile)) { |
---|
[17141] | 69 | $msg = 'バックアップディレクトリの作成に失敗しました'; |
---|
| 70 | $arrLog['err'][] = $msg; |
---|
| 71 | $this->printLog($msg); |
---|
[16862] | 72 | return $arrLog; |
---|
| 73 | } |
---|
| 74 | |
---|
[15665] | 75 | $includeArray = explode(',', $this->includes); |
---|
| 76 | $excludeArray = explode(',', $this->excludes); |
---|
[16343] | 77 | $fileArrays = $this->listdirs($target); |
---|
[15665] | 78 | |
---|
| 79 | foreach ($fileArrays as $path) { |
---|
| 80 | if (is_file($path)) { |
---|
| 81 | |
---|
| 82 | // ファイル名を取得 |
---|
| 83 | $fileName = pathinfo($path, PATHINFO_BASENAME); |
---|
| 84 | |
---|
| 85 | // 拡張子を取得 |
---|
| 86 | $suffix = pathinfo($path, PATHINFO_EXTENSION); |
---|
| 87 | |
---|
| 88 | // distinfo の変数定義 |
---|
| 89 | $distinfo = isset($distinfo) ? $distinfo : ""; |
---|
| 90 | |
---|
| 91 | // distinfo.php を読み込む |
---|
| 92 | if ($fileName == "distinfo.php") { |
---|
[16420] | 93 | include_once($path); |
---|
[15665] | 94 | } |
---|
| 95 | |
---|
| 96 | // 除外ファイルをスキップ |
---|
| 97 | if (in_array($fileName, $excludeArray)) { |
---|
[17141] | 98 | //$arrLog['ok'][] = "次のファイルは除外されました: " . $path; |
---|
| 99 | $msg = "次のファイルは除外されました: " . $path; |
---|
| 100 | $this->printLog($msg); |
---|
[15665] | 101 | continue; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // sha1 を取得 |
---|
| 105 | $sha1 = sha1_file($path); |
---|
| 106 | |
---|
[16862] | 107 | //$arrLog[] = $sha1 . " => " . $path; |
---|
[15665] | 108 | |
---|
| 109 | // 変換対象を順に処理 |
---|
| 110 | foreach ($includeArray as $include) { |
---|
| 111 | if ($suffix == $include) { |
---|
| 112 | |
---|
| 113 | // ファイル内容を取得 |
---|
| 114 | $contents = file_get_contents($path); |
---|
| 115 | |
---|
| 116 | // 書き出し先を取得 |
---|
| 117 | if (!empty($distinfo[$sha1])) { |
---|
| 118 | $out = $distinfo[$sha1]; |
---|
| 119 | } else { |
---|
[17141] | 120 | $msg = "ハッシュ値が一致しないため, コピー先が取得できません: " . $path; |
---|
| 121 | $arrLog['err'][] = $msg; |
---|
| 122 | $this->printLog($msg); |
---|
[16862] | 123 | break 2; |
---|
[15665] | 124 | } |
---|
| 125 | |
---|
[17193] | 126 | if (file_exists($out) && $sha1 == sha1_file($out)) { |
---|
[17141] | 127 | $msg = "同じ内容のファイルをスキップしました: " . $out; |
---|
| 128 | $this->printLog($msg); |
---|
[17193] | 129 | continue; |
---|
[17141] | 130 | } |
---|
[16862] | 131 | |
---|
| 132 | // バックアップを作成 |
---|
| 133 | if (file_exists($out)) { |
---|
| 134 | $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME); |
---|
[16884] | 135 | $bkupDistInfoArray[sha1_file($out)] = $out; |
---|
[16862] | 136 | |
---|
| 137 | if (!@copy($out, $bkupTo)) { |
---|
[17141] | 138 | $msg = "バックアップファイルの作成に失敗しました: " . $out . ' -> ' . $bkupTo; |
---|
| 139 | $arrLog['err'][] = $msg; |
---|
| 140 | $this->printLog($msg); |
---|
[16862] | 141 | break 2; |
---|
| 142 | } |
---|
[17141] | 143 | $msg = "バックアップファイルの作成に成功しました: " . $out . ' -> ' . $bkupTo; |
---|
| 144 | $this->printLog($msg); |
---|
[16862] | 145 | } |
---|
| 146 | |
---|
[15665] | 147 | // ファイルを書き出しモードで開く |
---|
[16420] | 148 | $handle = @fopen($out, "w"); |
---|
[15665] | 149 | if (!$handle) { |
---|
[16420] | 150 | // ディレクトリ作成を行ってリトライ |
---|
| 151 | $this->mkdir_p($out); |
---|
| 152 | $handle = @fopen($out, "w"); |
---|
| 153 | if (!$handle) { |
---|
[17141] | 154 | $msg = "コピー先に書き込み権限がありません: " . $out; |
---|
| 155 | $arrLog['err'][] = $msg; |
---|
| 156 | $this->printLog($msg); |
---|
[16420] | 157 | continue; |
---|
| 158 | } |
---|
[15665] | 159 | } |
---|
| 160 | |
---|
| 161 | // 取得した内容を書き込む |
---|
| 162 | if (fwrite($handle, $contents) === false) { |
---|
[17141] | 163 | $msg = "コピー先に書き込み権限がありません: " . $out; |
---|
| 164 | $arrLog['err'][] = $msg; |
---|
| 165 | $this->printLog($msg); |
---|
[15665] | 166 | continue; |
---|
| 167 | } |
---|
| 168 | |
---|
[17141] | 169 | $msg = "ファイルのコピーに成功しました: " . $out; |
---|
| 170 | $arrLog['ok'][] = $msg; |
---|
| 171 | $this->printLog($msg); |
---|
[15665] | 172 | // ファイルを閉じる |
---|
| 173 | fclose($handle); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
[16862] | 178 | $src = $this->makeDistInfo($bkupDistInfoArray); |
---|
| 179 | if (is_writable($bkupPath)) { |
---|
| 180 | $handle = @fopen($bkupPath . 'distinfo.php', "w"); |
---|
| 181 | @fwrite($handle, $src); |
---|
| 182 | @fclose($handle); |
---|
[17141] | 183 | $msg = "distinfoファイルの作成に成功しました: " . $bkupPath . 'distinfo.php'; |
---|
| 184 | $this->printLog($msg); |
---|
[16862] | 185 | } else { |
---|
[17141] | 186 | $msg = "distinfoファイルの作成に失敗しました: " . $bkupPath . 'distinfo.php'; |
---|
| 187 | $arrLog['err'][] = $msg; |
---|
| 188 | $this->printLog($msg); |
---|
[16862] | 189 | } |
---|
| 190 | umask($oldMask); |
---|
[16420] | 191 | return $arrLog; |
---|
[15665] | 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * $dir を再帰的に辿ってパス名を配列で返す. |
---|
| 196 | * |
---|
| 197 | * @param string 任意のパス名 |
---|
| 198 | * @return array $dir より下層に存在するパス名の配列 |
---|
| 199 | * @see http://www.php.net/glob |
---|
| 200 | */ |
---|
| 201 | function listdirs($dir) { |
---|
| 202 | static $alldirs = array(); |
---|
| 203 | $dirs = glob($dir . '/*'); |
---|
[16786] | 204 | if (is_array($dirs) && count($dirs) > 0) { |
---|
[15665] | 205 | foreach ($dirs as $d) $alldirs[] = $d; |
---|
| 206 | } |
---|
[16786] | 207 | if (is_array($dirs)) { |
---|
| 208 | foreach ($dirs as $dir) $this->listdirs($dir); |
---|
| 209 | } |
---|
[15665] | 210 | return $alldirs; |
---|
| 211 | } |
---|
[16420] | 212 | |
---|
| 213 | /** |
---|
| 214 | * mkdir -p |
---|
| 215 | * |
---|
| 216 | * @param string $path 絶対パス |
---|
| 217 | */ |
---|
| 218 | function mkdir_p($path){ |
---|
| 219 | $path = dirname($path); |
---|
| 220 | $path = str_replace ('\\', '/', $path); |
---|
| 221 | |
---|
| 222 | $arrDirs = explode("/", $path); |
---|
| 223 | $dir = ''; |
---|
| 224 | |
---|
| 225 | foreach($arrDirs as $n){ |
---|
| 226 | $dir .= $n . '/'; |
---|
| 227 | if(!file_exists($dir)) { |
---|
[16862] | 228 | if (!@mkdir($dir)) break; |
---|
[16420] | 229 | } |
---|
| 230 | } |
---|
[16862] | 231 | } |
---|
| 232 | |
---|
| 233 | function makeDistInfo($bkupDistInfoArray) { |
---|
| 234 | $src = "<?php\n" |
---|
| 235 | . '$distifo = array(' . "\n"; |
---|
| 236 | |
---|
| 237 | foreach ($bkupDistInfoArray as $key => $value) { |
---|
| 238 | $src .= "'${key}' => '${value}',\n"; |
---|
| 239 | } |
---|
| 240 | $src .= ");\n?>"; |
---|
[16865] | 241 | |
---|
| 242 | return $src; |
---|
[16862] | 243 | } |
---|
[17141] | 244 | |
---|
| 245 | function printLog($msg) { |
---|
| 246 | GC_Utils::gfPrintLog($msg, DATA_PATH . 'logs/ownersstore_batch_update.log'); |
---|
| 247 | } |
---|
[15665] | 248 | } |
---|
| 249 | ?> |
---|