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