source: branches/version-2_13-dev/data/class/batch/SC_Batch_Update.php @ 22856

Revision 22856, 9.2 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
  • 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    var $includes = 'php,inc,tpl,css,sql,js,png,jpg,gif,swf,txt,doc,pdf';
39
40    /**
41     * 除外するファイル名をカンマ区切りで羅列.
42     */
43    var $excludes = 'distinfo.php';
44
45    /**
46     * バッチ処理を実行する.
47     *
48     * @param string $target アップデータファイルのディレクトリパス
49     * @return void
50     */
51    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            return $arrLog;
71        }
72
73        $includeArray = explode(',', $this->includes);
74        $excludeArray = explode(',', $this->excludes);
75        $fileArrays = $this->listdirs($target);
76
77        foreach ($fileArrays as $path) {
78            if (is_file($path)) {
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') {
90                    include_once $path;
91                }
92
93                // 除外ファイルをスキップ
94                if (in_array($fileName, $excludeArray)) {
95                    //$arrLog['ok'][] = '次のファイルは除外されました: ' . $path;
96                    $msg = '次のファイルは除外されました: ' . $path;
97                    $this->printLog($msg);
98                    continue;
99                }
100
101                // sha1 を取得
102                $sha1 = sha1_file($path);
103
104                //$arrLog[] = $sha1 . ' => ' . $path;
105
106                // 変換対象を順に処理
107                foreach ($includeArray as $include) {
108                    if ($suffix == $include) {
109                        // ファイル内容を取得
110                        $contents = file_get_contents($path);
111
112                        // 書き出し先を取得
113                        if (!empty($distinfo[$sha1])) {
114                            $out = $distinfo[$sha1];
115                        } else {
116                            $msg = 'ハッシュ値が一致しないため, コピー先が取得できません: ' . $path;
117                            $arrLog['err'][] = $msg;
118                            $this->printLog($msg);
119                            break 2;
120                        }
121
122                        if (file_exists($out) && $sha1 == sha1_file($out)) {
123                            $msg = '同じ内容のファイルをスキップしました: ' . $out;
124                            $this->printLog($msg);
125                            continue;
126                        }
127
128                        // バックアップを作成
129                        if (file_exists($out)) {
130                            $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME);
131                            $bkupDistInfoArray[sha1_file($out)] = $out;
132
133                            if (!@copy($out, $bkupTo)) {
134                                $msg = 'バックアップファイルの作成に失敗しました: ' . $out . ' -> ' . $bkupTo;
135                                $arrLog['err'][] = $msg;
136                                $this->printLog($msg);
137                                break 2;
138                            }
139                            $msg = 'バックアップファイルの作成に成功しました: ' . $out . ' -> ' . $bkupTo;
140                            $this->printLog($msg);
141                        }
142
143                        // ファイルを書き出しモードで開く
144                        $handle = @fopen($out, 'w');
145                        if (!$handle) {
146                            // ディレクトリ作成を行ってリトライ
147                            $this->lfMkdirRecursive($out);
148                            $handle = @fopen($out, 'w');
149                            if (!$handle) {
150                                $msg = 'コピー先に書き込み権限がありません: ' . $out;
151                                $arrLog['err'][] = $msg;
152                                $this->printLog($msg);
153                                continue;
154                            }
155                        }
156
157                        // 取得した内容を書き込む
158                        if (fwrite($handle, $contents) === false) {
159                            $msg = 'コピー先に書き込み権限がありません: ' . $out;
160                            $arrLog['err'][] = $msg;
161                            $this->printLog($msg);
162                            continue;
163                        }
164
165                        $msg =  'ファイルのコピーに成功しました: ' . $out;
166                        $arrLog['ok'][] = $msg;
167                        $this->printLog($msg);
168                        // ファイルを閉じる
169                        fclose($handle);
170                    }
171                }
172            }
173        }
174        $src = $this->makeDistInfo($bkupDistInfoArray);
175        if (is_writable($bkupPath)) {
176            $handle = @fopen($bkupPath . 'distinfo.php', 'w');
177            @fwrite($handle, $src);
178            @fclose($handle);
179            $msg = 'distinfoファイルの作成に成功しました: ' . $bkupPath . 'distinfo.php';
180            $this->printLog($msg);
181        } else {
182            $msg = 'distinfoファイルの作成に失敗しました: ' . $bkupPath . 'distinfo.php';
183            $arrLog['err'][] = $msg;
184            $this->printLog($msg);
185        }
186        umask($oldMask);
187
188        return $arrLog;
189    }
190
191    /**
192     * $dir を再帰的に辿ってパス名を配列で返す.
193     *
194     * @param string 任意のパス名
195     * @return array $dir より下層に存在するパス名の配列
196     * @see http://www.php.net/glob
197     */
198    function listdirs($dir)
199    {
200        static $alldirs = array();
201        $dirs = glob($dir . '/*');
202        if (is_array($dirs) && count($dirs) > 0) {
203            foreach ($dirs as $d) $alldirs[] = $d;
204        }
205        if (is_array($dirs)) {
206            foreach ($dirs as $dir) $this->listdirs($dir);
207        }
208
209        return $alldirs;
210    }
211
212    /**
213     * mkdir -p
214     *
215     * @param string $path 絶対パス
216     */
217    function lfMkdirRecursive($path)
218    {
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    {
243        $src = "<?php\n"
244             . '$distifo = array(' . "\n";
245
246        foreach ($bkupDistInfoArray as $key => $value) {
247            $src .= "'${key}' => '${value}',\n";
248        }
249        $src .= ");\n?>";
250
251        return $src;
252    }
253
254    function printLog($msg)
255    {
256        GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . 'logs/ownersstore_batch_update.log');
257    }
258}
Note: See TracBrowser for help on using the repository browser.