1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 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 | |
---|
24 | // {{{ requires |
---|
25 | require_once CLASS_REALDIR . 'batch/SC_Batch.php'; |
---|
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,png,jpg,gif,swf"; |
---|
42 | |
---|
43 | /** |
---|
44 | * 除外するファイル名をカンマ区切りで羅列. |
---|
45 | */ |
---|
46 | var $excludes = "distinfo.php"; |
---|
47 | |
---|
48 | /** |
---|
49 | * バッチ処理を実行する. |
---|
50 | * |
---|
51 | * @param string $target アップデータファイルのディレクトリパス |
---|
52 | * @return void |
---|
53 | */ |
---|
54 | function execute($target = ".") { |
---|
55 | $msg = ''; |
---|
56 | $oldMask = umask(0); |
---|
57 | $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容 |
---|
58 | $bkupPath = DATA_REALDIR . 'downloads/backup/update_' . time() . '/'; |
---|
59 | $bkupPathFile = $bkupPath . 'files/'; |
---|
60 | $this->lfMkdirRecursive($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)) { |
---|
69 | $msg = 'バックアップディレクトリの作成に失敗しました'; |
---|
70 | $arrLog['err'][] = $msg; |
---|
71 | $this->printLog($msg); |
---|
72 | return $arrLog; |
---|
73 | } |
---|
74 | |
---|
75 | $includeArray = explode(',', $this->includes); |
---|
76 | $excludeArray = explode(',', $this->excludes); |
---|
77 | $fileArrays = $this->listdirs($target); |
---|
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") { |
---|
93 | include_once($path); |
---|
94 | } |
---|
95 | |
---|
96 | // 除外ファイルをスキップ |
---|
97 | if (in_array($fileName, $excludeArray)) { |
---|
98 | //$arrLog['ok'][] = "次のファイルは除外されました: " . $path; |
---|
99 | $msg = "次のファイルは除外されました: " . $path; |
---|
100 | $this->printLog($msg); |
---|
101 | continue; |
---|
102 | } |
---|
103 | |
---|
104 | // sha1 を取得 |
---|
105 | $sha1 = sha1_file($path); |
---|
106 | |
---|
107 | //$arrLog[] = $sha1 . " => " . $path; |
---|
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 { |
---|
120 | $msg = "ハッシュ値が一致しないため, コピー先が取得できません: " . $path; |
---|
121 | $arrLog['err'][] = $msg; |
---|
122 | $this->printLog($msg); |
---|
123 | break 2; |
---|
124 | } |
---|
125 | |
---|
126 | if (file_exists($out) && $sha1 == sha1_file($out)) { |
---|
127 | $msg = "同じ内容のファイルをスキップしました: " . $out; |
---|
128 | $this->printLog($msg); |
---|
129 | continue; |
---|
130 | } |
---|
131 | |
---|
132 | // バックアップを作成 |
---|
133 | if (file_exists($out)) { |
---|
134 | $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME); |
---|
135 | $bkupDistInfoArray[sha1_file($out)] = $out; |
---|
136 | |
---|
137 | if (!@copy($out, $bkupTo)) { |
---|
138 | $msg = "バックアップファイルの作成に失敗しました: " . $out . ' -> ' . $bkupTo; |
---|
139 | $arrLog['err'][] = $msg; |
---|
140 | $this->printLog($msg); |
---|
141 | break 2; |
---|
142 | } |
---|
143 | $msg = "バックアップファイルの作成に成功しました: " . $out . ' -> ' . $bkupTo; |
---|
144 | $this->printLog($msg); |
---|
145 | } |
---|
146 | |
---|
147 | // ファイルを書き出しモードで開く |
---|
148 | $handle = @fopen($out, 'w'); |
---|
149 | if (!$handle) { |
---|
150 | // ディレクトリ作成を行ってリトライ |
---|
151 | $this->lfMkdirRecursive($out); |
---|
152 | $handle = @fopen($out, 'w'); |
---|
153 | if (!$handle) { |
---|
154 | $msg = "コピー先に書き込み権限がありません: " . $out; |
---|
155 | $arrLog['err'][] = $msg; |
---|
156 | $this->printLog($msg); |
---|
157 | continue; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | // 取得した内容を書き込む |
---|
162 | if (fwrite($handle, $contents) === false) { |
---|
163 | $msg = "コピー先に書き込み権限がありません: " . $out; |
---|
164 | $arrLog['err'][] = $msg; |
---|
165 | $this->printLog($msg); |
---|
166 | continue; |
---|
167 | } |
---|
168 | |
---|
169 | $msg = "ファイルのコピーに成功しました: " . $out; |
---|
170 | $arrLog['ok'][] = $msg; |
---|
171 | $this->printLog($msg); |
---|
172 | // ファイルを閉じる |
---|
173 | fclose($handle); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | $src = $this->makeDistInfo($bkupDistInfoArray); |
---|
179 | if (is_writable($bkupPath)) { |
---|
180 | $handle = @fopen($bkupPath . 'distinfo.php', 'w'); |
---|
181 | @fwrite($handle, $src); |
---|
182 | @fclose($handle); |
---|
183 | $msg = "distinfoファイルの作成に成功しました: " . $bkupPath . 'distinfo.php'; |
---|
184 | $this->printLog($msg); |
---|
185 | } else { |
---|
186 | $msg = "distinfoファイルの作成に失敗しました: " . $bkupPath . 'distinfo.php'; |
---|
187 | $arrLog['err'][] = $msg; |
---|
188 | $this->printLog($msg); |
---|
189 | } |
---|
190 | umask($oldMask); |
---|
191 | return $arrLog; |
---|
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 . '/*'); |
---|
204 | if (is_array($dirs) && count($dirs) > 0) { |
---|
205 | foreach ($dirs as $d) $alldirs[] = $d; |
---|
206 | } |
---|
207 | if (is_array($dirs)) { |
---|
208 | foreach ($dirs as $dir) $this->listdirs($dir); |
---|
209 | } |
---|
210 | return $alldirs; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * mkdir -p |
---|
215 | * |
---|
216 | * @param string $path 絶対パス |
---|
217 | */ |
---|
218 | function lfMkdirRecursive($path){ |
---|
219 | $path = dirname($path); |
---|
220 | |
---|
221 | // HTML_REALDIR/DATA_REALDIRの判別 |
---|
222 | if (preg_match("@\Q".HTML_REALDIR."\E@", $path) > 0) { |
---|
223 | $dir = str_replace("\\", "/", HTML_REALDIR); |
---|
224 | $path = preg_replace("@\Q".HTML_REALDIR."\E@", "", $path); |
---|
225 | } elseif (preg_match("@\Q".DATA_REALDIR."\E@", $path) > 0) { |
---|
226 | $dir = str_replace("\\", "/", DATA_REALDIR); |
---|
227 | $path = preg_replace("@\Q".DATA_REALDIR."\E@", "", $path); |
---|
228 | } else { |
---|
229 | $dir = ""; |
---|
230 | } |
---|
231 | $arrDirs = explode("/", str_replace("\\", "/", $path)); |
---|
232 | |
---|
233 | foreach($arrDirs as $n){ |
---|
234 | $dir .= $n . '/'; |
---|
235 | if(!file_exists($dir)) { |
---|
236 | if (!@mkdir($dir)) break; |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | function makeDistInfo($bkupDistInfoArray) { |
---|
242 | $src = "<?php\n" |
---|
243 | . '$distifo = array(' . "\n"; |
---|
244 | |
---|
245 | foreach ($bkupDistInfoArray as $key => $value) { |
---|
246 | $src .= "'${key}' => '${value}',\n"; |
---|
247 | } |
---|
248 | $src .= ");\n?>"; |
---|
249 | |
---|
250 | return $src; |
---|
251 | } |
---|
252 | |
---|
253 | function printLog($msg) { |
---|
254 | GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . 'logs/ownersstore_batch_update.log'); |
---|
255 | } |
---|
256 | } |
---|
257 | ?> |
---|