示例#1
0
 public function download($params = [])
 {
     //echo '<pre>'; print_r($params); echo '</pre>'; exit;
     $setups_map = ['NET' => ['setup-4.0.msi' => 'WinWrap-NET4.0-setup.msi'], 'WPF' => ['setup-4.0-wpf.msi' => 'WinWrap-WPF4.0-setup.msi'], 'AZW' => ['setup-4.0-azw.msi' => 'WinWrap-AZW4.0-setup.msi'], 'COM' => ['setup32.msi' => 'WinWrap-COM32-setup.msi', 'setup64.msi' => 'WinWrap-COM64-setup.msi']];
     $all = [];
     foreach ($setups_map as $setup_map) {
         $all += $setup_map;
     }
     //$setups_map['ALL'] = $all; // disable for now (1/8/15)
     // select the platform's setup_map
     $platform = $params['platform'];
     $setup_map = $setups_map[$platform];
     if (!isset($setup_map)) {
         error_404();
     }
     // select the evaluator
     $key = $params['key'];
     $evaluator = Evaluator::read_by_key($key);
     //Render::json($evaluator); exit; // debug
     $error = false;
     if (!isset($evaluator['id'])) {
         $error = 'Invalid evaluator key.';
     } else {
         if (Util2::expired($evaluator['agreement_date'], 60, $evaluator['expiration_date'])) {
             $error = 'Evaluation period has expired.';
         }
     }
     if ($error) {
         Render::html(Doc::by_name('message')['content'], ['title' => 'Download Request Failed', 'message' => $error]);
         return;
     }
     // create evaluator cerificate
     $readme = Template::render_doc_by_name('evaluate-readme');
     $certificate = new Certificate();
     $certificate->set_by_evaluator($evaluator, true);
     // add download record
     $version = file_get_contents(EVALDIR . 'version.txt');
     $model = ['evaluator_id' => $evaluator['id'], 'ip' => $_SERVER['REMOTE_ADDR'], 'platform' => $platform != 'ALL' ? $platform : implode(',', array_keys($setups_map)), 'version' => $version];
     $download = EvaluatorDownload::create($model);
     // create signed certificate
     $html = $certificate->sign();
     //Render::text($_SERVER['HTTP_HOST'] . SUBDIR . "\r\n" . $html); exit;
     // create a temporary file
     $path = tempnam("tmp", "zip");
     $zipname = 'WinWrap-Basic-Evaluation-' . $platform;
     $zip = new ZipArchive();
     $okay = $zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
     $okay |= $zip->addEmptyDir($zipname);
     $okay |= $zip->addFromString($zipname . '/readme.htm', $readme);
     $okay |= $zip->addFromString($zipname . '/Evaluation.htm', $html);
     $okay |= $zip->addFromString($zipname . '/version.txt', $version);
     foreach ($setup_map as $setup => $name) {
         $okay |= $zip->addFile(EVALDIR . $setup, $zipname . '/' . $name);
     }
     $okay |= $zip->close();
     if (!$okay) {
         Render::text("ZipArchive failed.");
         return;
     }
     //$contents = file_get_contents($path);
     //file_put_contents(TEMPDIR . 'x.zip', $contents);
     // send zip file
     if (true) {
         header('Cache-Control: max-age=288000');
         header('Content-Description: File Transfer');
         header('Content-Type: application/zip');
         //header("Content-Type: application/force-download");
         header('Content-Length: ' . filesize($path));
         header('Content-Transfer-Encoding: binary');
         header('Content-Disposition: attachment; filename="' . $zipname . '.zip"');
         ob_end_flush();
         readfile($path);
     }
     unlink($path);
     // set download date
     $update = ['download_date' => gmdate('Y-m-d')];
     EvaluatorDownload::update($update, $download['id']);
     // done
     exit;
 }