source: branches/version-2_12-multilang/data/class/batch/SC_Batch_Update.php @ 22040

Revision 22040, 9.0 KB checked in by pineray, 12 years ago (diff)

#1890 コード中のメッセージを集約

SC_Batch_Update.php, SC_Helper_CSV.php, SC_Helper_Customer.php の文字列を差し替え.

  • 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-2012 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        $msg = '';
53        $oldMask = umask(0);
54        $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容
55        $bkupPath = DATA_REALDIR . 'downloads/backup/update_' . time() . '/';
56        $bkupPathFile = $bkupPath . 'files/';
57        $this->lfMkdirRecursive($bkupPathFile . 'dummy');
58
59        $arrLog = array(
60            'err' =>  array(),
61            'ok'  => array(),
62            'buckup_path' => $bkupPath
63        );
64
65        if (!is_writable($bkupPath) || !is_writable($bkupPathFile)) {
66            $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_FAIL_MAKEBACKUPDIR');
67            $arrLog['err'][] = $msg;
68            $this->printLog($msg);
69            return $arrLog;
70        }
71
72        $includeArray = explode(',', $this->includes);
73        $excludeArray = explode(',', $this->excludes);
74        $fileArrays = $this->listdirs($target);
75
76        foreach ($fileArrays as $path) {
77            if (is_file($path)) {
78
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 = SC_I18n_Ex::t('SC_BATCH_UPDATE_EXCLUDE_FILE') . $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                        // ファイル内容を取得
111                        $contents = file_get_contents($path);
112
113                        // 書き出し先を取得
114                        if (!empty($distinfo[$sha1])) {
115                            $out = $distinfo[$sha1];
116                        } else {
117                            $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_HASH_UNMATCH') . $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 = SC_I18n_Ex::t('SC_BATCH_UPDATE_SKIP_SAME') . $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 = SC_I18n_Ex::t('SC_BATCH_UPDATE_FAIL_MAKEBACKUPFILE') . $out . ' -> ' . $bkupTo;
136                                $arrLog['err'][] = $msg;
137                                $this->printLog($msg);
138                                break 2;
139                            }
140                            $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_FAIL_MAKEBACKUPFILE') . $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 = SC_I18n_Ex::t('SC_BATCH_UPDATE_UNWRITABLE') . $out;
152                                $arrLog['err'][] = $msg;
153                                $this->printLog($msg);
154                                continue;
155                            }
156                        }
157
158                        // 取得した内容を書き込む
159                        if (fwrite($handle, $contents) === false) {
160                            $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_UNWRITABLE') . $out;
161                            $arrLog['err'][] = $msg;
162                            $this->printLog($msg);
163                            continue;
164                        }
165
166                        $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_SUCCESS_FILECOPY') . $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 = SC_I18n_Ex::t('SC_BATCH_UPDATE_SUCCESS_MAKEDISTINFO') . $bkupPath . 'distinfo.php';
181            $this->printLog($msg);
182        } else {
183            $msg = SC_I18n_Ex::t('SC_BATCH_UPDATE_FAIL_MAKEDISTINFO') . $bkupPath . 'distinfo.php';
184            $arrLog['err'][] = $msg;
185            $this->printLog($msg);
186        }
187        umask($oldMask);
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        static $alldirs = array();
200        $dirs = glob($dir . '/*');
201        if (is_array($dirs) && count($dirs) > 0) {
202            foreach ($dirs as $d) $alldirs[] = $d;
203        }
204        if (is_array($dirs)) {
205            foreach ($dirs as $dir) $this->listdirs($dir);
206        }
207        return $alldirs;
208    }
209
210    /**
211     * mkdir -p
212     *
213     * @param string $path 絶対パス
214     */
215    function lfMkdirRecursive($path) {
216        $path = dirname($path);
217
218        // HTML_REALDIR/DATA_REALDIRの判別
219        if (preg_match("@\Q".HTML_REALDIR."\E@", $path) > 0) {
220            $dir = str_replace('\\', '/', HTML_REALDIR);
221            $path = preg_replace("@\Q".HTML_REALDIR."\E@", '', $path);
222        } elseif (preg_match("@\Q".DATA_REALDIR."\E@", $path) > 0) {
223            $dir = str_replace('\\', '/', DATA_REALDIR);
224            $path = preg_replace("@\Q".DATA_REALDIR."\E@", '', $path);
225        } else {
226            $dir = '';
227        }
228        $arrDirs = explode('/', str_replace('\\', '/', $path));
229
230        foreach ($arrDirs as $n) {
231            $dir .= $n . '/';
232            if (!file_exists($dir)) {
233                if (!@mkdir($dir)) break;
234            }
235        }
236    }
237
238    function makeDistInfo($bkupDistInfoArray) {
239        $src = "<?php\n"
240             . '$distifo = array(' . "\n";
241
242        foreach ($bkupDistInfoArray as $key => $value) {
243            $src .= "'${key}' => '${value}',\n";
244        }
245        $src .= ");\n?>";
246
247        return $src;
248    }
249
250    function printLog($msg) {
251        GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . 'logs/ownersstore_batch_update.log');
252    }
253}
Note: See TracBrowser for help on using the repository browser.