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

Revision 23126, 9.2 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

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