[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 | */ |
---|
| 41 | var $includes = "php,inc,tpl,css,sql,js"; |
---|
| 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 = ".") { |
---|
[16862] | 55 | $oldMask = umask(0); |
---|
| 56 | $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容 |
---|
| 57 | $bkupPath = DATA_PATH . 'downloads/backup/update_' . time() . '/'; |
---|
| 58 | $bkupPathFile = $bkupPath . 'files/'; |
---|
| 59 | $this->mkdir_p($bkupPathFile . 'dummy'); |
---|
| 60 | |
---|
| 61 | $arrLog = array( |
---|
| 62 | 'err' => array(), |
---|
| 63 | 'ok' => array(), |
---|
| 64 | 'buckup_path' => $bkupPath |
---|
| 65 | ); |
---|
| 66 | |
---|
| 67 | if (!is_writable($bkupPath) || !is_writable($bkupPathFile)) { |
---|
| 68 | $arrLog['err'][] = 'バックアップディレクトリの作成に失敗しました'; |
---|
| 69 | return $arrLog; |
---|
| 70 | } |
---|
| 71 | |
---|
[15665] | 72 | $includeArray = explode(',', $this->includes); |
---|
| 73 | $excludeArray = explode(',', $this->excludes); |
---|
[16343] | 74 | $fileArrays = $this->listdirs($target); |
---|
[15665] | 75 | |
---|
| 76 | foreach ($fileArrays as $path) { |
---|
| 77 | if (is_file($path)) { |
---|
| 78 | |
---|
| 79 | // ファイル名を取得 |
---|
| 80 | $fileName = pathinfo($path, PATHINFO_BASENAME); |
---|
| 81 | |
---|
| 82 | // 拡張子を取得 |
---|
| 83 | $suffix = pathinfo($path, PATHINFO_EXTENSION); |
---|
| 84 | |
---|
| 85 | // distinfo の変数定義 |
---|
| 86 | $distinfo = isset($distinfo) ? $distinfo : ""; |
---|
| 87 | |
---|
| 88 | // distinfo.php を読み込む |
---|
| 89 | if ($fileName == "distinfo.php") { |
---|
[16420] | 90 | include_once($path); |
---|
[15665] | 91 | } |
---|
| 92 | |
---|
| 93 | // 除外ファイルをスキップ |
---|
| 94 | if (in_array($fileName, $excludeArray)) { |
---|
[16862] | 95 | $arrLog['ok'][] = "次のファイルは除外されました: " . $path; |
---|
[15665] | 96 | continue; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | // sha1 を取得 |
---|
| 100 | $sha1 = sha1_file($path); |
---|
| 101 | |
---|
[16862] | 102 | //$arrLog[] = $sha1 . " => " . $path; |
---|
[15665] | 103 | |
---|
| 104 | // 変換対象を順に処理 |
---|
| 105 | foreach ($includeArray as $include) { |
---|
| 106 | if ($suffix == $include) { |
---|
| 107 | |
---|
| 108 | // ファイル内容を取得 |
---|
| 109 | $contents = file_get_contents($path); |
---|
| 110 | |
---|
| 111 | // 書き出し先を取得 |
---|
| 112 | if (!empty($distinfo[$sha1])) { |
---|
| 113 | $out = $distinfo[$sha1]; |
---|
| 114 | } else { |
---|
[16862] | 115 | $arrLog['err'][] = "ハッシュ値が一致しないため, コピー先が取得できません: " . $path; |
---|
| 116 | break 2; |
---|
[15665] | 117 | } |
---|
| 118 | |
---|
[16862] | 119 | // パーミッションチェック |
---|
| 120 | /** |
---|
| 121 | if ($check_only) { |
---|
| 122 | if(!is_writable($out)) { |
---|
| 123 | $this->mkdir_p($out); |
---|
| 124 | if (!is_writable($out)) { |
---|
| 125 | $arrLog['err'][] = "コピー先に書き込み権限がありません: " . $out; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | continue; |
---|
| 129 | }**/ |
---|
| 130 | |
---|
| 131 | // バックアップを作成 |
---|
| 132 | if (file_exists($out)) { |
---|
| 133 | $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME); |
---|
| 134 | $bkupDistInfoArray[sha1_file($out)] = $path; |
---|
| 135 | |
---|
| 136 | if (!@copy($out, $bkupTo)) { |
---|
| 137 | $arrLog['err'][] = "バックアップファイルの作成に失敗しました: " . $bkupTo; |
---|
| 138 | break 2; |
---|
| 139 | } |
---|
| 140 | $arrLog['ok'][] = "バックアップファイルの作成に成功しました: " . $bkupTo; |
---|
| 141 | } |
---|
| 142 | |
---|
[15665] | 143 | // ファイルを書き出しモードで開く |
---|
[16420] | 144 | $handle = @fopen($out, "w"); |
---|
[15665] | 145 | if (!$handle) { |
---|
[16420] | 146 | // ディレクトリ作成を行ってリトライ |
---|
| 147 | $this->mkdir_p($out); |
---|
| 148 | $handle = @fopen($out, "w"); |
---|
| 149 | if (!$handle) { |
---|
[16862] | 150 | $arrLog['err'][] = "コピー先に書き込み権限がありません: " . $out; |
---|
[16420] | 151 | continue; |
---|
| 152 | } |
---|
[15665] | 153 | } |
---|
| 154 | |
---|
| 155 | // 取得した内容を書き込む |
---|
| 156 | if (fwrite($handle, $contents) === false) { |
---|
[16862] | 157 | $arrLog['err'][] = "コピー先に書き込み権限がありません: " . $out; |
---|
[15665] | 158 | continue; |
---|
| 159 | } |
---|
| 160 | |
---|
[16862] | 161 | $arrLog['ok'][] = "ファイルのコピーに成功しました: " . $out; |
---|
[15665] | 162 | // ファイルを閉じる |
---|
| 163 | fclose($handle); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
[16862] | 168 | $src = $this->makeDistInfo($bkupDistInfoArray); |
---|
| 169 | if (is_writable($bkupPath)) { |
---|
| 170 | $handle = @fopen($bkupPath . 'distinfo.php', "w"); |
---|
| 171 | @fwrite($handle, $src); |
---|
| 172 | @fclose($handle); |
---|
| 173 | $arrLog['ok'][] = "バックアップ用ファイルの作成に成功しました: " . $bkupPath . 'distinfo.php'; |
---|
| 174 | } else { |
---|
[16865] | 175 | $arrLog['err'][] = "バックアップ用ファイルの作成に失敗しました: " . $bkupPath . 'distinfo.php'; |
---|
[16862] | 176 | } |
---|
| 177 | umask($oldMask); |
---|
[16420] | 178 | return $arrLog; |
---|
[15665] | 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * $dir を再帰的に辿ってパス名を配列で返す. |
---|
| 183 | * |
---|
| 184 | * @param string 任意のパス名 |
---|
| 185 | * @return array $dir より下層に存在するパス名の配列 |
---|
| 186 | * @see http://www.php.net/glob |
---|
| 187 | */ |
---|
| 188 | function listdirs($dir) { |
---|
| 189 | static $alldirs = array(); |
---|
| 190 | $dirs = glob($dir . '/*'); |
---|
[16786] | 191 | if (is_array($dirs) && count($dirs) > 0) { |
---|
[15665] | 192 | foreach ($dirs as $d) $alldirs[] = $d; |
---|
| 193 | } |
---|
[16786] | 194 | if (is_array($dirs)) { |
---|
| 195 | foreach ($dirs as $dir) $this->listdirs($dir); |
---|
| 196 | } |
---|
[15665] | 197 | return $alldirs; |
---|
| 198 | } |
---|
[16420] | 199 | |
---|
| 200 | /** |
---|
| 201 | * mkdir -p |
---|
| 202 | * |
---|
| 203 | * @param string $path 絶対パス |
---|
| 204 | */ |
---|
| 205 | function mkdir_p($path){ |
---|
| 206 | $path = dirname($path); |
---|
| 207 | $path = str_replace ('\\', '/', $path); |
---|
| 208 | |
---|
| 209 | $arrDirs = explode("/", $path); |
---|
| 210 | $dir = ''; |
---|
| 211 | |
---|
| 212 | foreach($arrDirs as $n){ |
---|
| 213 | $dir .= $n . '/'; |
---|
| 214 | if(!file_exists($dir)) { |
---|
[16862] | 215 | if (!@mkdir($dir)) break; |
---|
[16420] | 216 | } |
---|
| 217 | } |
---|
[16862] | 218 | } |
---|
| 219 | |
---|
| 220 | function makeDistInfo($bkupDistInfoArray) { |
---|
| 221 | $src = "<?php\n" |
---|
| 222 | . '$distifo = array(' . "\n"; |
---|
| 223 | |
---|
| 224 | foreach ($bkupDistInfoArray as $key => $value) { |
---|
| 225 | $src .= "'${key}' => '${value}',\n"; |
---|
| 226 | } |
---|
| 227 | $src .= ");\n?>"; |
---|
[16865] | 228 | |
---|
| 229 | return $src; |
---|
[16862] | 230 | } |
---|
[15665] | 231 | } |
---|
| 232 | ?> |
---|