source: branches/version-2_12-dev/data/class/pages/admin/basis/LC_Page_Admin_Basis_ZipInstall.php @ 21514

Revision 21514, 13.4 KB checked in by Seasoft, 12 years ago (diff)

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

  • 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-2011 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/** CSV ファイルの最大行数 */
28define('ZIP_CSV_LINE_MAX', 8192);
29
30/** 画像の表示数量 */
31define('IMAGE_MAX', 680);
32
33/** 郵便番号CSV ファイルのパス */
34define('ZIP_CSV_REALFILE', DATA_REALDIR . 'downloads/KEN_ALL.CSV');
35
36/** UTF-8 変換済みの郵便番号CSV ファイルのパス */
37define('ZIP_CSV_UTF8_REALFILE', DATA_REALDIR . 'downloads/KEN_ALL_utf-8.CSV');
38
39/**
40 * 郵便番号DB登録 のページクラス.
41 *
42 * @package Page
43 * @author LOCKON CO.,LTD.
44 * @version $Id:LC_Page_Admin_Basis_ZipInstall.php 16741 2007-11-08 00:43:24Z adachi $
45 */
46class LC_Page_Admin_Basis_ZipInstall extends LC_Page_Admin_Ex {
47
48    /** CSVの行数 */
49    var $tpl_line = 0;
50    var $tpl_mode;
51    var $exec;
52    var $tpl_count_mtb_zip;
53
54    /** CSV の更新日時 */
55    var $tpl_csv_datetime;
56
57    /** 日本郵便から取得した ZIP アーカイブファイルの保管パス */
58    var $zip_csv_temp_realfile;
59
60    // }}}
61    // {{{ functions
62
63    /**
64     * Page を初期化する.
65     *
66     * @return void
67     */
68    function init() {
69        parent::init();
70        $this->tpl_mainpage = 'basis/zip_install.tpl';
71        $this->tpl_subno = 'zip_install';
72        $this->tpl_maintitle = '基本情報管理';
73        $this->tpl_subtitle = '郵便番号DB登録';
74        $this->tpl_mainno = 'basis';
75
76        $this->tpl_mode = $this->getMode();
77        $this->exec = (boolean)$_GET['exec'];
78        $this->zip_csv_temp_realfile = DATA_REALDIR . 'downloads/tmp/ken_all.zip';
79    }
80
81    /**
82     * Page のプロセス.
83     *
84     * @return void
85     */
86    function process() {
87        $this->action();
88        $this->sendResponse();
89    }
90
91    /**
92     * Page のアクション.
93     *
94     * @return void
95     */
96    function action() {
97        // パラメーター管理クラス
98        $objFormParam = new SC_FormParam_Ex();
99        // パラメーター情報の初期化
100        $this->lfInitParam($this->tpl_mode, $objFormParam);
101        $objFormParam->setParam($_GET);
102        $this->arrErr = $objFormParam->checkError();
103        $this->arrForm = $objFormParam->getHashArray();
104        $this->tpl_zip_download_url_empty = !defined('ZIP_DOWNLOAD_URL') || strlen(ZIP_DOWNLOAD_URL) === 0 || ZIP_DOWNLOAD_URL === false;
105        $this->tpl_zip_function_not_exists = !function_exists('zip_open');
106        $this->tpl_skip_update_csv = $this->tpl_zip_download_url_empty || $this->tpl_zip_function_not_exists;
107
108        if ($this->exec) {
109            if (!empty($this->arrErr)) {
110                SC_Utils_Ex::sfDispException();
111            }
112            switch ($this->tpl_mode) {
113                // 自動登録
114                case 'auto':
115                    $this->lfAutoCommitZip();
116                    break;
117                // DB手動登録
118                case 'manual':
119                    $this->insertMtbZip($this->arrForm['startRowNum']);
120                    break;
121            }
122            exit;
123        }
124
125        switch ($this->tpl_mode) {
126            // 削除
127            case 'delete':
128                $this->lfDeleteZip();
129
130                // 進捗・完了画面を表示しない
131                $this->tpl_mode = null;
132                break;
133
134            // 郵便番号CSV更新
135            case 'update_csv';
136                $this->lfDownloadZipFileFromJp();
137                $this->lfExtractZipFile();
138
139                // 進捗・完了画面を表示しない
140                $this->tpl_mode = null;
141                break;
142        }
143
144        $this->tpl_line = $this->countZipCsv();
145        $this->tpl_count_mtb_zip = $this->countMtbZip();
146        $this->tpl_csv_datetime = $this->lfGetCsvDatetime();
147        // XXX PHP4 を切捨てたら、ダウンロードの必要性チェックなども行いたい
148        // $arrHeader = get_headers(ZIP_DOWNLOAD_URL, 1);
149    }
150
151    /**
152     * デストラクタ.
153     *
154     * @return void
155     */
156    function destroy() {
157        parent::destroy();
158    }
159
160    function lfAutoCommitZip() {
161        $objQuery =& SC_Query_Ex::getSingletonInstance();
162
163        if (!$this->tpl_skip_update_csv) {
164            $this->lfDownloadZipFileFromJp();
165            $this->lfExtractZipFile();
166        }
167
168        $objQuery->begin();
169        $this->lfDeleteZip();
170        $this->insertMtbZip();
171        $objQuery->commit();
172    }
173
174    /**
175     * テーブルデータと UTF-8 変換済みの郵便番号 CSV を削除
176     *
177     * @return void
178     */
179    function lfDeleteZip() {
180        $objQuery =& SC_Query_Ex::getSingletonInstance();
181
182        // DB
183        $objQuery->delete('mtb_zip');
184
185        // UTF-8 変換済みの郵便番号 CSV
186        unlink(ZIP_CSV_UTF8_REALFILE);
187    }
188
189    /**
190     * パラメーター情報の初期化
191     *
192     * @return void
193     */
194    function lfInitParam($tpl_mode, &$objFormParam) {
195        if ($tpl_mode == 'manual') {
196            $objFormParam->addParam('開始行', 'startRowNum', INT_LEN, 'n', array('EXIST_CHECK', 'MAX_LENGTH_CHECK', 'NUM_CHECK'));
197        }
198    }
199
200    /**
201     * DB登録
202     *
203     * @return void
204     */
205    function insertMtbZip($start = 1) {
206        $objQuery =& SC_Query_Ex::getSingletonInstance();
207
208        $img_path = USER_URL . USER_PACKAGE_DIR . 'admin/img/basis/'; // 画像パスは admin 固定
209
210        ?>
211        <html xmlns='http://www.w3.org/1999/xhtml' lang='ja' xml:lang='ja'>
212        <head>
213            <meta http-equiv='Content-Type' content='text/html; charset=<?php echo CHAR_CODE ?>' />
214        </head>
215        <body>
216        <p>DB 登録進捗状況</p>
217        <div style='background-color: #494E5F;'>
218        <?php
219        // 一部のIEは256バイト以上受け取ってから表示を開始する。
220        SC_Utils_Ex::sfFlush(true);
221
222        echo '<img src="' . $img_path . 'zip_install_progress.gif"><br />';
223        echo '<img src="' . $img_path . 'space_w.gif">';
224        SC_Utils_Ex::sfFlush();
225
226        // 画像を一個表示する件数を求める。
227        $line_all = $this->countZipCsv();
228        $disp_line = intval($line_all / IMAGE_MAX);
229
230        /** 現在行(CSV形式。空行は除く。) */
231        $cntCurrentLine = 0;
232        /** 挿入した行数 */
233        $cntInsert = 0;
234        $img_cnt = 0;
235
236        $fp = $this->openZipCsv();
237        while (!feof($fp)) {
238            $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX);
239            if (empty($arrCSV)) continue;
240            $cntCurrentLine++;
241            if ($cntCurrentLine >= $start) {
242                $sqlval = array();
243                $sqlval['zipcode'] = $arrCSV[2];
244                $sqlval['state'] = $arrCSV[6];
245                $sqlval['city'] = $arrCSV[7];
246                $sqlval['town'] = $arrCSV[8];
247                $objQuery->insert('mtb_zip', $sqlval);
248                $cntInsert++;
249            }
250
251            // $disp_line件ごとに進捗表示する
252            if ($cntCurrentLine % $disp_line == 0 && $img_cnt < IMAGE_MAX) {
253                echo '<img src="' . $img_path . 'graph_1_w.gif">';
254                SC_Utils_Ex::sfFlush();
255                $img_cnt++;
256            }
257            SC_Utils_Ex::extendTimeOut();
258        }
259        fclose($fp);
260
261        echo '<img src="' . $img_path . 'space_w.gif">';
262        echo '</div>' . "\n";
263
264        ?>
265        <script type='text/javascript' language='javascript'>
266            <!--
267                // 完了画面
268                function complete() {
269                    document.open('text/html','replace');
270                    document.clear();
271                    document.write('<p>完了しました。<br />');
272                    document.write("<?php echo $cntInsert ?> 件を追加しました。</p>");
273                    document.write("<p><a href='?' target='_top'>戻る</a></p>");
274                    document.close();
275                }
276                // コンテンツを削除するため、タイムアウトで呼び出し。
277                setTimeout('complete()', 0);
278            // -->
279        </script>
280        </body>
281        </html>
282        <?php
283    }
284
285    function openZipCsv() {
286        $this->convertZipCsv();
287        $fp = fopen(ZIP_CSV_UTF8_REALFILE, 'r');
288        if (!$fp) {
289            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' の読み込みに失敗しました。');
290        }
291        return $fp;
292    }
293
294    function convertZipCsv() {
295        if (file_exists(ZIP_CSV_UTF8_REALFILE)) return;
296
297        $fpr = fopen(ZIP_CSV_REALFILE, 'r');
298        if (!$fpr) {
299            SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE . ' の読み込みに失敗しました。');
300        }
301
302        $fpw = fopen(ZIP_CSV_UTF8_REALFILE, 'w');
303        if (!$fpw) {
304            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' を開けません。');
305        }
306
307        while (!feof($fpr)) {
308            fwrite($fpw, mb_convert_encoding(fgets($fpr, ZIP_CSV_LINE_MAX), CHAR_CODE, 'sjis-win'));
309        }
310
311        fclose($fpw);
312        fclose($fpr);
313    }
314
315    function countMtbZip() {
316        $objQuery =& SC_Query_Ex::getSingletonInstance();
317        return $objQuery->count('mtb_zip');
318    }
319
320    function countZipCsv() {
321        $line = 0;
322        $fp = $this->openZipCsv();
323
324        // CSVの行数を数える
325        while (!feof($fp)) {
326            /*
327            // 正確にカウントする
328            $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX);
329            */
330            // 推測でカウントする
331            $tmp = fgets($fp, ZIP_CSV_LINE_MAX);
332            if (empty($tmp)) continue;
333            $line++;
334        }
335        fclose($fp);
336
337        return $line;
338    }
339
340    /**
341     * 日本郵便から郵便番号 CSV の ZIP アーカイブファイルを取得
342     *
343     * @return void
344     */
345   function lfDownloadZipFileFromJp() {
346        // Proxy経由を可能とする。
347        // TODO Proxyの設定は「data/module/HTTP/Request.php」内の「function HTTP_Request」へ記述する。いずれは、外部設定としたい。
348        $req = new HTTP_Request();
349
350        $req->setURL(ZIP_DOWNLOAD_URL);
351
352        // 郵便番号CSVをdownloadする。
353        $res = $req->sendRequest();
354        if (!$res) {
355            SC_Utils_Ex::sfDispException(ZIP_DOWNLOAD_URL . ' の取得に失敗しました。');
356        }
357
358        // 郵便番号CSV(zip file)を保存する。
359        $fp = fopen($this->zip_csv_temp_realfile, 'w');
360        if (!$fp) {
361            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' を開けません。');
362        }
363        $res = fwrite($fp, $req->getResponseBody());
364        if (!$res) {
365            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' への書き込みに失敗しました。');
366        }
367    }
368
369    /**
370     * ZIP アーカイブファイルを展開して、郵便番号 CSV を上書き
371     *
372     * @return void
373     */
374    function lfExtractZipFile() {
375        $zip = zip_open($this->zip_csv_temp_realfile);
376        if (!is_resource($zip)) {
377            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' をオープンできません。');
378        }
379
380        do {
381            $entry = zip_read($zip);
382        } while ($entry && zip_entry_name($entry) != 'KEN_ALL.CSV');
383
384        if (!$entry) {
385            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' に対象ファイルが見つかりません。');
386        }
387
388        // 展開時の破損を考慮し、別名で一旦展開する。
389        $tmp_csv_realfile = ZIP_CSV_REALFILE . '.tmp';
390
391        $res = zip_entry_open($zip, $entry, 'rb');
392        if (!$res) {
393            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' の展開に失敗しました。');
394        }
395
396        $fp = fopen($tmp_csv_realfile, 'w');
397        if (!$fp) {
398            SC_Utils_Ex::sfDispException($tmp_csv_realfile . ' を開けません。');
399        }
400
401        $res = fwrite($fp, zip_entry_read($entry, zip_entry_filesize($entry)));
402        if ($res === FALSE) {
403            SC_Utils_Ex::sfDispException($tmp_csv_realfile . ' の書き込みに失敗しました。');
404        }
405
406        fclose($fp);
407        zip_close($zip);
408
409        // CSV 削除
410        $res = unlink(ZIP_CSV_REALFILE);
411        if (!$res) {
412            SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE . ' を削除できません。');
413        }
414
415        // CSV ファイル名変更
416        $res = rename($tmp_csv_realfile, ZIP_CSV_REALFILE);
417        if (!$res) {
418            SC_Utils_Ex::sfDispException('ファイル名を変更できません。: ' . $tmp_csv_realfile . ' -> ' . ZIP_CSV_REALFILE);
419        }
420    }
421
422    /**
423     * CSV の更新日時を取得
424     *
425     * @return string CSV の更新日時 (整形済みテキスト)
426     */
427    function lfGetCsvDatetime() {
428        return date('Y/m/d H:i:s', filemtime(ZIP_CSV_REALFILE));
429    }
430}
Note: See TracBrowser for help on using the repository browser.