source: branches/version-2/data/class/batch/SC_Batch_Update.php @ 17132

Revision 17132, 8.7 KB checked in by adachi, 16 years ago (diff)

ログ出力の修正

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 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
25require_once(CLASS_PATH . "batch/SC_Batch.php");
26
27/**
28 * アップデート機能 のバッチクラス.
29 *
30 * XXX Singleton にするべき...
31 *
32 * @package Batch
33 * @author LOCKON CO.,LTD.
34 * @version $Id$
35 */
36class 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_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)) {
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 ($sha1 == sha1_file($out)) {
127                            $msg = "同じ内容のファイルをスキップしました: " . $out;
128                            $this->printLog($msg);
129                        }
130
131                        // バックアップを作成
132                        if (file_exists($out)) {
133                            $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME);
134                            $bkupDistInfoArray[sha1_file($out)] = $out;
135
136                            if (!@copy($out, $bkupTo)) {
137                                $msg = "バックアップファイルの作成に失敗しました: " . $out . ' -> ' . $bkupTo;
138                                $arrLog['err'][] = $msg;
139                                $this->printLog($msg);
140                                break 2;
141                            }
142                            $msg = "バックアップファイルの作成に成功しました: " . $out . ' -> ' . $bkupTo;
143                            $this->printLog($msg);
144                        }
145
146                        // ファイルを書き出しモードで開く
147                        $handle = @fopen($out, "w");
148                        if (!$handle) {
149                            // ディレクトリ作成を行ってリトライ
150                            $this->mkdir_p($out);
151                            $handle = @fopen($out, "w");
152                            if (!$handle) {
153                                $msg = "コピー先に書き込み権限がありません: " . $out;
154                                $arrLog['err'][] = $msg;
155                                $this->printLog($msg);
156                                continue;
157                            }
158                        }
159
160                        // 取得した内容を書き込む
161                        if (fwrite($handle, $contents) === false) {
162                            $msg = "コピー先に書き込み権限がありません: " . $out;
163                            $arrLog['err'][] = $msg;
164                            $this->printLog($msg);
165                            continue;
166                        }
167
168                        $msg =  "ファイルのコピーに成功しました: " . $out;
169                        $arrLog['ok'][] = $msg;
170                        $this->printLog($msg);
171                        // ファイルを閉じる
172                        fclose($handle);
173                    }
174                }
175            }
176        }
177        $src = $this->makeDistInfo($bkupDistInfoArray);
178        if (is_writable($bkupPath)) {
179            $handle = @fopen($bkupPath . 'distinfo.php', "w");
180            @fwrite($handle, $src);
181            @fclose($handle);
182            $msg = "distinfoファイルの作成に成功しました: " . $bkupPath . 'distinfo.php';
183            $this->printLog($msg);
184        } else {
185            $msg = "distinfoファイルの作成に失敗しました: " . $bkupPath . 'distinfo.php';
186            $arrLog['err'][] = $msg;
187            $this->printLog($msg);
188        }
189        umask($oldMask);
190        return $arrLog;
191    }
192
193    /**
194     * $dir を再帰的に辿ってパス名を配列で返す.
195     *
196     * @param string 任意のパス名
197     * @return array $dir より下層に存在するパス名の配列
198     * @see http://www.php.net/glob
199     */
200    function listdirs($dir) {
201        static $alldirs = array();
202        $dirs = glob($dir . '/*');
203        if (is_array($dirs) && count($dirs) > 0) {
204            foreach ($dirs as $d) $alldirs[] = $d;
205        }
206        if (is_array($dirs)) {
207            foreach ($dirs as $dir) $this->listdirs($dir);
208        }
209        return $alldirs;
210    }
211
212    /**
213     * mkdir -p
214     *
215     * @param string $path 絶対パス
216     */
217    function mkdir_p($path){
218        $path = dirname($path);
219        $path = str_replace ('\\', '/', $path);
220
221        $arrDirs = explode("/", $path);
222        $dir = '';
223
224        foreach($arrDirs as $n){
225            $dir .= $n . '/';
226            if(!file_exists($dir)) {
227                if (!@mkdir($dir)) break;
228            }
229        }
230    }
231
232    function makeDistInfo($bkupDistInfoArray) {
233        $src = "<?php\n"
234             . '$distifo = array(' . "\n";
235
236        foreach ($bkupDistInfoArray as $key => $value) {
237            $src .= "'${key}' => '${value}',\n";
238        }
239        $src .= ");\n?>";
240
241        return $src;
242    }
243
244    function printLog($msg) {
245        GC_Utils::gfPrintLog($msg, DATA_PATH . 'logs/ownersstore_batch_update.log');
246    }
247}
248?>
Note: See TracBrowser for help on using the repository browser.