shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

download.php (4860B)


      1 <?php
      2 class ControllerAccountDownload extends Controller {
      3 	public function index() {
      4 		if (!$this->customer->isLogged()) {
      5 			$this->session->data['redirect'] = $this->url->link('account/download', '', true);
      6 
      7 			$this->response->redirect($this->url->link('account/login', '', true));
      8 		}
      9 
     10 		$this->load->language('account/download');
     11 
     12 		$this->document->setTitle($this->language->get('heading_title'));
     13 
     14 		$data['breadcrumbs'] = array();
     15 
     16 		$data['breadcrumbs'][] = array(
     17 			'text' => $this->language->get('text_home'),
     18 			'href' => $this->url->link('common/home')
     19 		);
     20 
     21 		$data['breadcrumbs'][] = array(
     22 			'text' => $this->language->get('text_account'),
     23 			'href' => $this->url->link('account/account', '', true)
     24 		);
     25 
     26 		$data['breadcrumbs'][] = array(
     27 			'text' => $this->language->get('text_downloads'),
     28 			'href' => $this->url->link('account/download', '', true)
     29 		);
     30 
     31 		$this->load->model('account/download');
     32 
     33 		if (isset($this->request->get['page'])) {
     34 			$page = $this->request->get['page'];
     35 		} else {
     36 			$page = 1;
     37 		}
     38 
     39 		$data['downloads'] = array();
     40 
     41 		$download_total = $this->model_account_download->getTotalDownloads();
     42 
     43 		$results = $this->model_account_download->getDownloads(($page - 1) * $this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit'), $this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit'));
     44 
     45 		foreach ($results as $result) {
     46 			if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
     47 				$size = filesize(DIR_DOWNLOAD . $result['filename']);
     48 
     49 				$i = 0;
     50 
     51 				$suffix = array(
     52 					'B',
     53 					'KB',
     54 					'MB',
     55 					'GB',
     56 					'TB',
     57 					'PB',
     58 					'EB',
     59 					'ZB',
     60 					'YB'
     61 				);
     62 
     63 				while (($size / 1024) > 1) {
     64 					$size = $size / 1024;
     65 					$i++;
     66 				}
     67 
     68 				$data['downloads'][] = array(
     69 					'order_id'   => $result['order_id'],
     70 					'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
     71 					'name'       => $result['name'],
     72 					'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
     73 					'href'       => $this->url->link('account/download/download', 'download_id=' . $result['download_id'], true)
     74 				);
     75 			}
     76 		}
     77 
     78 		$pagination = new Pagination();
     79 		$pagination->total = $download_total;
     80 		$pagination->page = $page;
     81 		$pagination->limit = $this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit');
     82 		$pagination->url = $this->url->link('account/download', 'page={page}', true);
     83 
     84 		$data['pagination'] = $pagination->render();
     85 
     86 		$data['results'] = sprintf($this->language->get('text_pagination'), ($download_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($download_total - 10)) ? $download_total : ((($page - 1) * 10) + 10), $download_total, ceil($download_total / 10));
     87 		
     88 		$data['continue'] = $this->url->link('account/account', '', true);
     89 
     90 		$data['column_left'] = $this->load->controller('common/column_left');
     91 		$data['column_right'] = $this->load->controller('common/column_right');
     92 		$data['content_top'] = $this->load->controller('common/content_top');
     93 		$data['content_bottom'] = $this->load->controller('common/content_bottom');
     94 		$data['footer'] = $this->load->controller('common/footer');
     95 		$data['header'] = $this->load->controller('common/header');
     96 
     97 		$this->response->setOutput($this->load->view('account/download', $data));
     98 	}
     99 
    100 	public function download() {
    101 		if (!$this->customer->isLogged()) {
    102 			$this->session->data['redirect'] = $this->url->link('account/download', '', true);
    103 
    104 			$this->response->redirect($this->url->link('account/login', '', true));
    105 		}
    106 
    107 		$this->load->model('account/download');
    108 
    109 		if (isset($this->request->get['download_id'])) {
    110 			$download_id = $this->request->get['download_id'];
    111 		} else {
    112 			$download_id = 0;
    113 		}
    114 
    115 		$download_info = $this->model_account_download->getDownload($download_id);
    116 
    117 		if ($download_info) {
    118 			$file = DIR_DOWNLOAD . $download_info['filename'];
    119 			$mask = basename($download_info['mask']);
    120 
    121 			if (!headers_sent()) {
    122 				if (file_exists($file)) {
    123 					header('Content-Type: application/octet-stream');
    124 					header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
    125 					header('Expires: 0');
    126 					header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    127 					header('Pragma: public');
    128 					header('Content-Length: ' . filesize($file));
    129 
    130 					if (ob_get_level()) {
    131 						ob_end_clean();
    132 					}
    133 
    134 					readfile($file, 'rb');
    135 
    136 					exit();
    137 				} else {
    138 					exit('Error: Could not find file ' . $file . '!');
    139 				}
    140 			} else {
    141 				exit('Error: Headers already sent out!');
    142 			}
    143 		} else {
    144 			$this->response->redirect($this->url->link('account/download', '', true));
    145 		}
    146 	}
    147 }