Issues (2407)

application/controller/account/download.php (8 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerAccountDownload extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        if (!$this->customer->isLogged()) {
28
            $this->session->data['redirect'] = $this->url->link('account/download', '', true);
29
30
            $this->response->redirect($this->url->link('account/login', '', true));
31
        }
32
33
        $this->load->language('account/download');
34
35
        $this->document->setTitle($this->language->get('heading_title'));
36
37
        $data['breadcrumbs'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
38
39
        $data['breadcrumbs'][] = array(
40
            'text' => $this->language->get('text_home'),
41
            'href' => $this->url->link('common/home')
42
        );
43
44
        $data['breadcrumbs'][] = array(
45
            'text' => $this->language->get('text_account'),
46
            'href' => $this->url->link('account/account', '', true)
47
        );
48
49
        $data['breadcrumbs'][] = array(
50
            'text' => $this->language->get('text_downloads'),
51
            'href' => $this->url->link('account/download', '', true)
52
        );
53
54
        $this->load->model('account/download');
55
56
        $data['heading_title'] = $this->language->get('heading_title');
57
58
        $data['text_empty'] = $this->language->get('text_empty');
59
60
        $data['column_order_id'] = $this->language->get('column_order_id');
61
        $data['column_name'] = $this->language->get('column_name');
62
        $data['column_size'] = $this->language->get('column_size');
63
        $data['column_date_added'] = $this->language->get('column_date_added');
64
65
        $data['button_download'] = $this->language->get('button_download');
66
        $data['button_continue'] = $this->language->get('button_continue');
67
68
        if (isset($this->request->get['page'])) {
69
            $page = $this->request->get['page'];
70
        } else {
71
            $page = 1;
72
        }
73
74
        $data['downloads'] = array();
75
76
        $download_total = $this->model_account_download->getTotalDownloads();
77
78
        $results = $this->model_account_download->getDownloads(($page - 1) * $this->config->get('config_limit_store'), $this->config->get('config_limit_store'));
79
80
        foreach ($results as $result) {
81
            if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $result['filename'])) {
82
                $size = filesize($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $result['filename']);
83
84
                $i = 0;
85
86
                $suffix = array(
87
                    'B',
88
                    'KB',
89
                    'MB',
90
                    'GB',
91
                    'TB',
92
                    'PB',
93
                    'EB',
94
                    'ZB',
95
                    'YB'
96
                );
97
98
                while (($size / 1024) > 1) {
99
                    $size = $size / 1024;
100
                    $i++;
101
                }
102
103
                $data['downloads'][] = array(
104
                    'order_id'   => $result['order_id'],
105
                    'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
106
                    'name'       => $result['name'],
107
                    'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
0 ignored issues
show
substr($size, 0, strpos($size, '.') + 4) of type string is incompatible with the type double expected by parameter $val of round(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                    'size'       => round(/** @scrutinizer ignore-type */ substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
Loading history...
108
                    'href'       => $this->url->link('account/download/download', 'download_id=' . $result['download_id'], true)
109
                );
110
            }
111
        }
112
113
        $pagination = new \Divine\Engine\Library\Pagination();
114
        $pagination->total = $download_total;
115
        $pagination->page = $page;
116
        $pagination->limit = $this->config->get($this->config->get('config_theme') . '_product_limit');
117
        $pagination->url = $this->url->link('account/download', 'page={page}', true);
118
119
        $data['pagination'] = $pagination->render();
120
121
        $data['results'] = sprintf($this->language->get('text_pagination'), ($download_total) ? (($page - 1) * $this->config->get($this->config->get('config_theme') . '_product_limit')) + 1 : 0, ((($page - 1) * $this->config->get($this->config->get('config_theme') . '_product_limit')) > ($download_total - $this->config->get($this->config->get('config_theme') . '_product_limit'))) ? $download_total : ((($page - 1) * $this->config->get($this->config->get('config_theme') . '_product_limit')) + $this->config->get($this->config->get($this->config->get('config_theme') . '_theme') . '_product_limit')), $download_total, ceil($download_total / $this->config->get($this->config->get('config_theme') . '_product_limit')));
122
123
        $data['continue'] = $this->url->link('account/account', '', true);
124
125
        $data['column'] = $this->load->controller('common/column');
126
127
        $data['content_top'] = $this->load->controller('common/content_top');
128
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
129
        $data['footer'] = $this->load->controller('common/footer');
130
        $data['header'] = $this->load->controller('common/header');
131
132
        $this->response->setOutput($this->load->view('account/download', $data));
133
    }
134
135
    public function download()
136
    {
137
        if (!$this->customer->isLogged()) {
138
            $this->session->data['redirect'] = $this->url->link('account/download', '', true);
139
140
            $this->response->redirect($this->url->link('account/login', '', true));
141
        }
142
143
        $this->load->model('account/download');
144
145
        if (isset($this->request->get['download_id'])) {
146
            $download_id = $this->request->get['download_id'];
147
        } else {
148
            $download_id = 0;
149
        }
150
151
        $download_info = $this->model_account_download->getDownload($download_id);
152
153
        if ($download_info) {
154
            $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $download_info['filename'];
155
            $mask = basename($download_info['mask']);
156
157
            if (!headers_sent()) {
158
                if (file_exists($file)) {
159
                    header('Content-Type: application/octet-stream');
160
                    header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
161
                    header('Expires: 0');
162
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
163
                    header('Pragma: public');
164
                    header('Content-Length: ' . filesize($file));
165
166
                    if (ob_get_level()) {
167
                        ob_end_clean();
168
                    }
169
170
                    readfile($file, 'rb');
0 ignored issues
show
'rb' of type string is incompatible with the type boolean expected by parameter $use_include_path of readfile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
                    readfile($file, /** @scrutinizer ignore-type */ 'rb');
Loading history...
171
172
                    exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
173
                } else {
174
                    exit('Error: Could not find file ' . $file . '!');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
175
                }
176
            } else {
177
                exit('Error: Headers already sent out!');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
178
            }
179
        } else {
180
            $this->response->redirect($this->url->link('account/download', '', true));
181
        }
182
    }
183
}
184