source: branches/version-2_5-dev/data/class/batch/SC_Batch_Update.php @ 20032

Revision 20032, 9.2 KB checked in by Seasoft, 13 years ago (diff)

#626(表記の統一性の向上) 対応漏れ

  • EC-CUBE標準規約から外れる関数名を修正 (#700 でのマージ漏れか、コミュニティ版固有の関数があったと考えられる。)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
RevLine 
[15665]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[18701]5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
[15665]6 *
7 * http://www.lockon.co.jp/
[16582]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.
[15665]22 */
23
24// {{{ requires
[19805]25require_once(CLASS_REALDIR . "batch/SC_Batch.php");
[15665]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     */
[16984]41    var $includes = "php,inc,tpl,css,sql,js,png,jpg,gif,swf";
[15665]42
43    /**
44     * 除外するファイル名をカンマ区切りで羅列.
45     */
[16862]46    var $excludes = "distinfo.php";
[15665]47
48    /**
49     * バッチ処理を実行する.
50     *
51     * @param string $target アップデータファイルのディレクトリパス
52     * @return void
53     */
54    function execute($target = ".") {
[17141]55        $msg = '';
[16862]56        $oldMask = umask(0);
57        $bkupDistInfoArray = array(); //バックアップファイル用のdistinfoファイル内容
[19805]58        $bkupPath = DATA_REALDIR . 'downloads/backup/update_' . time() . '/';
[16862]59        $bkupPathFile = $bkupPath . 'files/';
[20032]60        $this->lfMkdirRecursive($bkupPathFile . 'dummy');
[16862]61
62        $arrLog = array(
63            'err' =>  array(),
64            'ok'  => array(),
65            'buckup_path' => $bkupPath
66        );
67
68        if (!is_writable($bkupPath) || !is_writable($bkupPathFile)) {
[17141]69            $msg = 'バックアップディレクトリの作成に失敗しました';
70            $arrLog['err'][] = $msg;
71            $this->printLog($msg);
[16862]72            return $arrLog;
73        }
74
[15665]75        $includeArray = explode(',', $this->includes);
76        $excludeArray = explode(',', $this->excludes);
[16343]77        $fileArrays = $this->listdirs($target);
[15665]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") {
[16420]93                    include_once($path);
[15665]94                }
95
96                // 除外ファイルをスキップ
97                if (in_array($fileName, $excludeArray)) {
[17141]98                    //$arrLog['ok'][] = "次のファイルは除外されました: " . $path;
99                    $msg = "次のファイルは除外されました: " . $path;
100                    $this->printLog($msg);
[15665]101                    continue;
102                }
103
104                // sha1 を取得
105                $sha1 = sha1_file($path);
106
[16862]107                //$arrLog[] = $sha1 . " => " . $path;
[15665]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 {
[17141]120                            $msg = "ハッシュ値が一致しないため, コピー先が取得できません: " . $path;
121                            $arrLog['err'][] = $msg;
122                            $this->printLog($msg);
[16862]123                            break 2;
[15665]124                        }
125
[17193]126                        if (file_exists($out) && $sha1 == sha1_file($out)) {
[17141]127                            $msg = "同じ内容のファイルをスキップしました: " . $out;
128                            $this->printLog($msg);
[17193]129                            continue;
[17141]130                        }
[16862]131
132                        // バックアップを作成
133                        if (file_exists($out)) {
134                            $bkupTo = $bkupPathFile . pathinfo($out, PATHINFO_BASENAME);
[16884]135                            $bkupDistInfoArray[sha1_file($out)] = $out;
[16862]136
137                            if (!@copy($out, $bkupTo)) {
[17141]138                                $msg = "バックアップファイルの作成に失敗しました: " . $out . ' -> ' . $bkupTo;
139                                $arrLog['err'][] = $msg;
140                                $this->printLog($msg);
[16862]141                                break 2;
142                            }
[17141]143                            $msg = "バックアップファイルの作成に成功しました: " . $out . ' -> ' . $bkupTo;
144                            $this->printLog($msg);
[16862]145                        }
146
[15665]147                        // ファイルを書き出しモードで開く
[16420]148                        $handle = @fopen($out, "w");
[15665]149                        if (!$handle) {
[16420]150                            // ディレクトリ作成を行ってリトライ
[20032]151                            $this->lfMkdirRecursive($out);
[16420]152                            $handle = @fopen($out, "w");
153                            if (!$handle) {
[17141]154                                $msg = "コピー先に書き込み権限がありません: " . $out;
155                                $arrLog['err'][] = $msg;
156                                $this->printLog($msg);
[16420]157                                continue;
158                            }
[15665]159                        }
160
161                        // 取得した内容を書き込む
162                        if (fwrite($handle, $contents) === false) {
[17141]163                            $msg = "コピー先に書き込み権限がありません: " . $out;
164                            $arrLog['err'][] = $msg;
165                            $this->printLog($msg);
[15665]166                            continue;
167                        }
168
[17141]169                        $msg =  "ファイルのコピーに成功しました: " . $out;
170                        $arrLog['ok'][] = $msg;
171                        $this->printLog($msg);
[15665]172                        // ファイルを閉じる
173                        fclose($handle);
174                    }
175                }
176            }
177        }
[16862]178        $src = $this->makeDistInfo($bkupDistInfoArray);
179        if (is_writable($bkupPath)) {
180            $handle = @fopen($bkupPath . 'distinfo.php', "w");
181            @fwrite($handle, $src);
182            @fclose($handle);
[17141]183            $msg = "distinfoファイルの作成に成功しました: " . $bkupPath . 'distinfo.php';
184            $this->printLog($msg);
[16862]185        } else {
[17141]186            $msg = "distinfoファイルの作成に失敗しました: " . $bkupPath . 'distinfo.php';
187            $arrLog['err'][] = $msg;
188            $this->printLog($msg);
[16862]189        }
190        umask($oldMask);
[16420]191        return $arrLog;
[15665]192    }
193
194    /**
195     * $dir を再帰的に辿ってパス名を配列で返す.
196     *
197     * @param string 任意のパス名
198     * @return array $dir より下層に存在するパス名の配列
199     * @see http://www.php.net/glob
200     */
201    function listdirs($dir) {
202        static $alldirs = array();
203        $dirs = glob($dir . '/*');
[16786]204        if (is_array($dirs) && count($dirs) > 0) {
[15665]205            foreach ($dirs as $d) $alldirs[] = $d;
206        }
[16786]207        if (is_array($dirs)) {
208            foreach ($dirs as $dir) $this->listdirs($dir);
209        }
[15665]210        return $alldirs;
211    }
[16420]212
213    /**
214     * mkdir -p
215     *
216     * @param string $path 絶対パス
217     */
[20032]218    function lfMkdirRecursive($path){
[16420]219        $path = dirname($path);
[17567]220       
[19805]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);
[17567]228        } else {
229            $dir = "";
230        }
231        $arrDirs = explode("/", str_replace("\\", "/", $path));
[16420]232
233        foreach($arrDirs as $n){
234            $dir .= $n . '/';
235            if(!file_exists($dir)) {
[16862]236                if (!@mkdir($dir)) break;
[16420]237            }
238        }
[16862]239    }
240
241    function makeDistInfo($bkupDistInfoArray) {
242        $src = "<?php\n"
243             . '$distifo = array(' . "\n";
244
245        foreach ($bkupDistInfoArray as $key => $value) {
246            $src .= "'${key}' => '${value}',\n";
247        }
248        $src .= ");\n?>";
[16865]249
250        return $src;
[16862]251    }
[17141]252
253    function printLog($msg) {
[19805]254        GC_Utils::gfPrintLog($msg, DATA_REALDIR . 'logs/ownersstore_batch_update.log');
[17141]255    }
[15665]256}
257?>
Note: See TracBrowser for help on using the repository browser.