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