Page
[ class tree: Page ] [ index: Page ] [ all elements ]

Source for file LC_Page_Admin_Basis_ZipInstall.php

Documentation is available at LC_Page_Admin_Basis_ZipInstall.php

  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
  25. require_once CLASS_EX_REALDIR 'page_extends/admin/LC_Page_Admin_Ex.php';
  26. require_once DATA_REALDIR 'module/HTTP/Request.php';
  27.  
  28. /** CSV ファイルの最大行数 */
  29. define("ZIP_CSV_LINE_MAX"8192);
  30.  
  31. /** 画像の表示数量 */
  32. define("IMAGE_MAX"680);
  33.  
  34. /** 郵便番号CSV ファイルのパス */
  35. define("ZIP_CSV_REALFILE"DATA_REALDIR "downloads/KEN_ALL.CSV");
  36.  
  37. /** UTF-8 変換済みの郵便番号CSV ファイルのパス */
  38. define("ZIP_CSV_UTF8_REALFILE"DATA_REALDIR "downloads/KEN_ALL_utf-8.CSV");
  39.  
  40. /**
  41.  * 郵便番号DB登録 のページクラス.
  42.  *
  43.  * @package Page
  44.  * @author LOCKON CO.,LTD.
  45.  * @version $Id:LC_Page_Admin_Basis_ZipInstall.php 16741 2007-11-08 00:43:24Z adachi $
  46.  */
  47. class LC_Page_Admin_Basis_ZipInstall extends LC_Page_Admin_Ex {
  48.  
  49.     /** CSVの行数 */
  50.     var $tpl_line = 0;
  51.     var $tpl_mode;
  52.     var $exec;
  53.     var $tpl_count_mtb_zip;
  54.  
  55.     /** CSV の更新日時 */
  56.     var $tpl_csv_datetime;
  57.  
  58.     /** 日本郵便から取得した ZIP アーカイブファイルの保管パス */
  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=== || 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.         $safe_mode = (boolean)ini_get('safe_mode');
  236.         $max_execution_time
  237.             = is_numeric(ini_get('max_execution_time'))
  238.             ? intval(ini_get('max_execution_time'))
  239.             : intval(get_cfg_var('max_execution_time'))
  240.         ;
  241.  
  242.         $fp $this->openZipCsv();
  243.         while (!feof($fp)) {
  244.             $arrCSV fgetcsv($fpZIP_CSV_LINE_MAX);
  245.             if (empty($arrCSV)) continue;
  246.             $cntCurrentLine++;
  247.             if ($cntCurrentLine >= $start{
  248.                 $sqlval array();
  249.                 // $sqlval['code'] = $arrCSV[0];
  250.                 // $sqlval['old_zipcode'] = $arrCSV[1];
  251.                 $sqlval['zipcode'$arrCSV[2];
  252.                 // $sqlval['state_kana'] = $arrCSV[3];
  253.                 // $sqlval['city_kana'] = $arrCSV[4];
  254.                 // $sqlval['town_kana'] = $arrCSV[5];
  255.                 $sqlval['state'$arrCSV[6];
  256.                 $sqlval['city'$arrCSV[7];
  257.                 $sqlval['town'$arrCSV[8];
  258.                 // $sqlval['flg1'] = $arrCSV[9];
  259.                 // $sqlval['flg2'] = $arrCSV[10];
  260.                 // $sqlval['flg3'] = $arrCSV[11];
  261.                 // $sqlval['flg4'] = $arrCSV[12];
  262.                 // $sqlval['flg5'] = $arrCSV[13];
  263.                 // $sqlval['flg6'] = $arrCSV[14];
  264.                 $objQuery->insert("mtb_zip"$sqlval);
  265.                 $cntInsert++;
  266.             }
  267.  
  268.             // $disp_line件ごとに進捗表示する
  269.             if($cntCurrentLine $disp_line == && $img_cnt IMAGE_MAX{
  270.                 echo '<img src="' $img_path 'graph_1_w.gif">';
  271.                 SC_Utils_Ex::sfFlush();
  272.                 $img_cnt++;
  273.             }
  274.             // 暴走スレッドが残留する確率を軽減したタイムアウト防止のロジック
  275.             // TODO 動作が安定していれば、SC_Utils 辺りに移動したい。
  276.             if (!$safe_mode{
  277.                 // タイムアウトをリセット
  278.                 set_time_limit($max_execution_time);
  279.             }
  280.         }
  281.         fclose($fp);
  282.  
  283.         echo '<img src="' $img_path 'space_w.gif">';
  284.         echo '</div>' "\n";
  285.  
  286.         ?>
  287.         <script type="text/javascript" language="javascript">
  288.             <!--
  289.                 // 完了画面
  290.                 function complete() {
  291.                     document.open("text/html","replace");
  292.                     document.clear();
  293.                     document.write("<p>完了しました。<br />");
  294.                     document.write("<?php echo $cntInsert ?> 件を追加しました。</p>");
  295.                     document.write("<p><a href='?' target='_top'>戻る</a></p>");
  296.                     document.close();
  297.                 }
  298.                 // コンテンツを削除するため、タイムアウトで呼び出し。
  299.                 setTimeout("complete()", 0);
  300.             // -->
  301.         </script>
  302.         </body>
  303.         </html>
  304.         <?php
  305.     }
  306.  
  307.     function openZipCsv({
  308.         $this->convertZipCsv();
  309.         $fp fopen(ZIP_CSV_UTF8_REALFILE'r');
  310.         if (!$fp{
  311.             SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE ' の読み込みに失敗しました。');
  312.         }
  313.         return $fp;
  314.     }
  315.  
  316.     function convertZipCsv({
  317.         if (file_exists(ZIP_CSV_UTF8_REALFILE)) return;
  318.  
  319.         $fpr fopen(ZIP_CSV_REALFILE'r');
  320.         if (!$fpr{
  321.             SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE ' の読み込みに失敗しました。');
  322.         }
  323.  
  324.         $fpw fopen(ZIP_CSV_UTF8_REALFILE'w');
  325.         if (!$fpw{
  326.             SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE ' を開けません。');
  327.         }
  328.  
  329.         while (!feof($fpr)) {
  330.             fwrite($fpwmb_convert_encoding(fgets($fprZIP_CSV_LINE_MAX)CHAR_CODE'sjis-win'));
  331.         }
  332.  
  333.         fclose($fpw);
  334.         fclose($fpr);
  335.     }
  336.  
  337.     function countMtbZip({
  338.         $objQuery =SC_Query_Ex::getSingletonInstance();
  339.         return $objQuery->count('mtb_zip');
  340.     }
  341.  
  342.     function countZipCsv({
  343.         $line 0;
  344.         $fp $this->openZipCsv();
  345.  
  346.         // CSVの行数を数える
  347.         while (!feof($fp)) {
  348.             /*
  349.             // 正確にカウントする
  350.             $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX);
  351.             */
  352.             // 推測でカウントする
  353.             $tmp fgets($fpZIP_CSV_LINE_MAX);
  354.             if (empty($tmp)) continue;
  355.             $line++;
  356.         }
  357.         fclose($fp);
  358.  
  359.         return $line;
  360.     }
  361.  
  362.     /**
  363.      * 日本郵便から郵便番号 CSV の ZIP アーカイブファイルを取得
  364.      *
  365.      * @return void 
  366.      */
  367.    function lfDownloadZipFileFromJp({
  368.         // Proxy経由を可能とする。
  369.         // TODO Proxyの設定は「DATA_REALDIR . 'module/HTTP/Request.php'」内の「function HTTP_Request」へ記述する。いずれは、外部設定としたい。
  370.         $req new HTTP_Request();
  371.  
  372.         $req->setURL(ZIP_DOWNLOAD_URL);
  373.  
  374.         // 郵便番号CSVをdownloadする。
  375.         $res $req->sendRequest();
  376.         if (!$res{
  377.             SC_Utils_Ex::sfDispException(ZIP_DOWNLOAD_URL ' の取得に失敗しました。');
  378.         }
  379.  
  380.         // 郵便番号CSV(zip file)を保存する。
  381.         $fp fopen($this->zip_csv_temp_realfile'w');
  382.         if (!$fp{
  383.             SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' を開けません。');
  384.         }
  385.         $res fwrite($fp$req->getResponseBody());
  386.         if (!$res{
  387.             SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' への書き込みに失敗しました。');
  388.         }
  389.     }
  390.  
  391.     /**
  392.      * ZIP アーカイブファイルを展開して、郵便番号 CSV を上書き
  393.      *
  394.      * @return void 
  395.      */
  396.     function lfExtractZipFile({
  397.         $zip zip_open($this->zip_csv_temp_realfile);
  398.         if (!is_resource($zip)) {
  399.             SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' をオープンできません。');
  400.         }
  401.  
  402.         do {
  403.             $entry zip_read($zip);
  404.         while ($entry && zip_entry_name($entry!= 'KEN_ALL.CSV');
  405.  
  406.         if (!$entry{
  407.             SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' に対象ファイルが見つかりません。');
  408.         }
  409.  
  410.         // 展開時の破損を考慮し、別名で一旦展開する。
  411.         $tmp_csv_realfile ZIP_CSV_REALFILE '.tmp';
  412.  
  413.         $res zip_entry_open($zip$entry'rb');
  414.         if (!$res{
  415.             SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' の展開に失敗しました。');
  416.         }
  417.  
  418.         $fp fopen($tmp_csv_realfile'w');
  419.         if (!$fp{
  420.             SC_Utils_Ex::sfDispException($tmp_csv_realfile ' を開けません。');
  421.         }
  422.  
  423.         $res fwrite($fpzip_entry_read($entryzip_entry_filesize($entry)));
  424.         if ($res === FALSE{
  425.             SC_Utils_Ex::sfDispException($tmp_csv_realfile ' の書き込みに失敗しました。');
  426.         }
  427.  
  428.         fclose($fp);
  429.         zip_close($zip);
  430.  
  431.         // CSV 削除
  432.         $res unlink(ZIP_CSV_REALFILE);
  433.         if (!$res{
  434.             SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE ' を削除できません。');
  435.         }
  436.  
  437.         // CSV ファイル名変更
  438.         $res rename($tmp_csv_realfileZIP_CSV_REALFILE);
  439.         if (!$res{
  440.             SC_Utils_Ex::sfDispException('ファイル名を変更できません。: ' $tmp_csv_realfile ' -> ' ZIP_CSV_REALFILE);
  441.         }
  442.     }
  443.  
  444.     /**
  445.      * CSV の更新日時を取得
  446.      *
  447.      * @return string CSV の更新日時 (整形済みテキスト)
  448.      */
  449.     function lfGetCsvDatetime({
  450.         return date('Y/m/d H:i:s'filemtime(ZIP_CSV_REALFILE));
  451.     }
  452. }
  453. ?>

Documentation generated on Fri, 24 Feb 2012 14:01:07 +0900 by Seasoft