source: branches/version-2_12-dev/data/class/batch/SC_Batch_Update.php @ 22567

Revision 22567, 9.2 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • 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     */
39    var $includes = 'php,inc,tpl,css,sql,js,png,jpg,gif,swf,txt,doc,pdf';
40
41    /**
42     * 除外するファイル名をカンマ区切りで羅列.
43     */
44    var $excludes = 'distinfo.php';
45
46    /**
47     * バッチ処理を実行する.
48     *
49     * @param string $target アップデータファイルのディレクトリパス
50     * @return void
51     */
52    function execute($target = '.')
53    {
54        $msg = '';
55        $oldMask = umask(0);
56        $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容
57        $bkupPath = DATA_REALDIR . 'downloads/backup/update_' . time() . '/';
58        $bkupPathFile = $bkupPath . 'files/';
59        $this->lfMkdirRecursive($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            $msg = 'バックアップディレクトリの作成に失敗しました';
69            $arrLog['err'][] = $msg;
70            $this->printLog($msg);
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                // ファイル名を取得
82                $fileName = pathinfo($path, PATHINFO_BASENAME);
83
84                // 拡張子を取得
85                $suffix = pathinfo($path, PATHINFO_EXTENSION);
86
87                // distinfo の変数定義
88                $distinfo = isset($distinfo) ? $distinfo : '';
89
90                // distinfo.php を読み込む
91                if ($fileName == 'distinfo.php') {
92                    include_once $path;
93                }
94
95                // 除外ファイルをスキップ
96                if (in_array($fileName, $excludeArray)) {
97                    //$arrLog['ok'][] = '次のファイルは除外されました: ' . $path;
98                    $msg = '次のファイルは除外されました: ' . $path;
99                    $this->printLog($msg);
100                    continue;
101                }
102
103                // sha1 を取得
104                $sha1 = sha1_file($path);
105
106                //$arrLog[] = $sha1 . ' => ' . $path;
107
108                // 変換対象を順に処理
109                foreach ($includeArray as $include) {
110                    if ($suffix == $include) {
111
112                        // ファイル内容を取得
113                        $contents = file_get_contents($path);
114
115                        // 書き出し先を取得
116                        if (!empty($distinfo[$sha1])) {
117                            $out = $distinfo[$sha1];
118                        } else {
119                            $msg = 'ハッシュ値が一致しないため, コピー先が取得できません: ' . $path;
120                            $arrLog['err'][] = $msg;
121                            $this->printLog($msg);
122                            break 2;
123                        }
124
125                        if (file_exists($out) && $sha1 == sha1_file($out)) {
126                            $msg = '同じ内容のファイルをスキップしました: ' . $out;
127                            $this->printLog($msg);
128                            continue;
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->lfMkdirRecursive($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    {
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    {
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    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    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.