<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * @package     [[]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Download
{
    var $session;
    var $auth;
    var $dlitem;
    var $membercd;

    var $o;
    var $r;
    var $d;
    var $fid;
    var $did;

    var $data;
    var $send_did;

    var $img_s;

    /**
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Download", "Download#execute");

        $this->membercd = $this->auth->getAuthData("membercd");
        $this->data = $this->dlitem->getDownloadOK($this->o, $this->r, $this->membercd);

        if (empty($this->data)) {
            $log->debug("no data", "Download#execute");
            return 'error';
        }

        $fid_key = $this->data["orderno"] ."_". $this->data["orderrowno"] ."_". $this->membercd;
        if ($this->fid!==sha1($fid_key)) {
            $log->debug("not equal fid", "Download#execute");
            return 'error';
        }

        // ダウンロードファイルのチェック
        $download_file = $this->dlitem->getDownloadFile($this->data["itemcd"], $this->data["structcd"]);
        if (!is_file($download_file)) {
            $log->debug("no download_file ". $download_file, "Download#execute");
            return 'error';
        } else {
            // キーの生成
            $this->send_did = sha1($this->data["orderno"] ."_". $download_file);
        }

        // 画像
        if (is_file(IMAGE_DIR ."/3/". $this->data["itemcd"] ."-". $this->data["structcd"] . "S.jpg")) {
            $this->img_s = "img.php?c=". $this->data["itemcd"] ."&sc=". $this->data["structcd"] ."&s=S&d=3";
        } else {
            // 構成情報の画像が登録されていない場合、基本情報の画像を使用
            $this->img_s = "img.php?c=" . $this->data["itemcd"] . "&s=S&d=1";
        }

        if ($this->d==='true') {
            if ($this->data["maxdlcount"] == 0) {
                $log->debug("dont downloading", "Download#execute");
                return 'error';
            }
            if ($this->did!==sha1($this->data["orderno"] ."_". $download_file)) {
                $log->debug("not equal did", "Download#execute");
                return 'error';
            }
            $this->doDownload($download_file);
        }

        return 'success';
    }

    function doDownload($download_file)
    {
        $log =& LogFactory::getLog();
        $log->debug("doDownload " . $download_file, "Download#execute");

        $ext = pathinfo($download_file);
        $filename = $this->data["itemnm"] ."_". $this->data["variationnm"] .".". $ext["extension"];

        header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header ("Cache-Control: public");
        header ("Pragma: public");
        header ("Content-Transfer-Encoding: binary");
        header ("Content-type: application/octet-stream");
        header ("Content-Length: ". filesize($download_file));
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
            header ("Content-Disposition: inline ; filename=". mb_convert_encoding($filename, "SJIS", "UTF-8"));
        } elseif (strstr($_SERVER['HTTP_USER_AGENT'], "Safari")) {
            // filenameは、固定
            $filename = "download_file.". $ext["extension"];
            header ("Content-Disposition: inline ; filename=".$filename);
        } else {
            header ("Content-Disposition: inline ; filename=".$filename);
        }
        ob_clean();
        flush();
        readfile($download_file);

        $log->debug("now downloading " . $filename, "Download#execute");
        $this->dlitem->doDownload($this->o, $this->r, $this->membercd);

        $location = $this->dlitem->getDownloadURL($this->fid, $this->o,$this->r);
        $log->debug("download url " . $location, "Download#execute");
        exit;
    }
}
?>
