ControllerCatalogDownload::delete()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
nc 9
nop 0
dl 0
loc 33
rs 8.8333
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 ControllerCatalogDownload 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
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('catalog/download');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('catalog/download');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('catalog/download');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('catalog/download');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_catalog_download->addDownload($this->request->post);
48
49
            $this->session->data['success'] = $this->language->get('text_success');
50
51
            $url = '';
52
53
            if (isset($this->request->get['sort'])) {
54
                $url .= '&sort=' . $this->request->get['sort'];
55
            }
56
57
            if (isset($this->request->get['order'])) {
58
                $url .= '&order=' . $this->request->get['order'];
59
            }
60
61
            if (isset($this->request->get['page'])) {
62
                $url .= '&page=' . $this->request->get['page'];
63
            }
64
65
            $this->response->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('catalog/download');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('catalog/download');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_catalog_download->editDownload($this->request->get['download_id'], $this->request->post);
81
82
            $this->session->data['success'] = $this->language->get('text_success');
83
84
            $url = '';
85
86
            if (isset($this->request->get['sort'])) {
87
                $url .= '&sort=' . $this->request->get['sort'];
88
            }
89
90
            if (isset($this->request->get['order'])) {
91
                $url .= '&order=' . $this->request->get['order'];
92
            }
93
94
            if (isset($this->request->get['page'])) {
95
                $url .= '&page=' . $this->request->get['page'];
96
            }
97
98
            $this->response->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('catalog/download');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('catalog/download');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $download_id) {
114
                $this->model_catalog_download->deleteDownload($download_id);
115
            }
116
117
            $this->session->data['success'] = $this->language->get('text_success');
118
119
            $url = '';
120
121
            if (isset($this->request->get['sort'])) {
122
                $url .= '&sort=' . $this->request->get['sort'];
123
            }
124
125
            if (isset($this->request->get['order'])) {
126
                $url .= '&order=' . $this->request->get['order'];
127
            }
128
129
            if (isset($this->request->get['page'])) {
130
                $url .= '&page=' . $this->request->get['page'];
131
            }
132
133
            $this->response->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true));
134
        }
135
136
        $this->getList();
137
    }
138
139
    protected function getList()
140
    {
141
        if (isset($this->request->get['sort'])) {
142
            $sort = $this->request->get['sort'];
143
        } else {
144
            $sort = 'dd.name';
145
        }
146
147
        if (isset($this->request->get['order'])) {
148
            $order = $this->request->get['order'];
149
        } else {
150
            $order = 'ASC';
151
        }
152
153
        if (isset($this->request->get['page'])) {
154
            $page = $this->request->get['page'];
155
        } else {
156
            $page = 1;
157
        }
158
159
        $url = '';
160
161
        if (isset($this->request->get['sort'])) {
162
            $url .= '&sort=' . $this->request->get['sort'];
163
        }
164
165
        if (isset($this->request->get['order'])) {
166
            $url .= '&order=' . $this->request->get['order'];
167
        }
168
169
        if (isset($this->request->get['page'])) {
170
            $url .= '&page=' . $this->request->get['page'];
171
        }
172
173
        $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...
174
175
        $data['breadcrumbs'][] = array(
176
            'text' => $this->language->get('text_home'),
177
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
178
        );
179
180
        $data['breadcrumbs'][] = array(
181
            'text' => $this->language->get('heading_title'),
182
            'href' => $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('catalog/download/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('catalog/download/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['downloads'] = array();
189
190
        $filter_data = array(
191
            'sort'  => $sort,
192
            'order' => $order,
193
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
194
            'limit' => $this->config->get('config_limit_admin')
195
        );
196
197
        $download_total = $this->model_catalog_download->getTotalDownloads();
198
199
        $results = $this->model_catalog_download->getDownloads($filter_data);
200
201
        foreach ($results as $result) {
202
            $data['downloads'][] = array(
203
                'download_id' => $result['download_id'],
204
                'name'        => $result['name'],
205
                'date_added'  => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
206
                'edit'        => $this->url->link('catalog/download/edit', 'token=' . $this->session->data['token'] . '&download_id=' . $result['download_id'] . $url, true)
207
            );
208
        }
209
210
        $data['heading_title'] = $this->language->get('heading_title');
211
212
        $data['text_list'] = $this->language->get('text_list');
213
        $data['text_no_results'] = $this->language->get('text_no_results');
214
        $data['text_confirm'] = $this->language->get('text_confirm');
215
216
        $data['column_name'] = $this->language->get('column_name');
217
        $data['column_date_added'] = $this->language->get('column_date_added');
218
        $data['column_action'] = $this->language->get('column_action');
219
220
        $data['button_add'] = $this->language->get('button_add');
221
        $data['button_edit'] = $this->language->get('button_edit');
222
        $data['button_delete'] = $this->language->get('button_delete');
223
224
        if (isset($this->error['warning'])) {
225
            $data['error_warning'] = $this->error['warning'];
226
        } else {
227
            $data['error_warning'] = '';
228
        }
229
230
        if (isset($this->session->data['success'])) {
231
            $data['success'] = $this->session->data['success'];
232
233
            unset($this->session->data['success']);
234
        } else {
235
            $data['success'] = '';
236
        }
237
238
        if (isset($this->request->post['selected'])) {
239
            $data['selected'] = (array)$this->request->post['selected'];
240
        } else {
241
            $data['selected'] = array();
242
        }
243
244
        $url = '';
245
246
        if ($order == 'ASC') {
247
            $url .= '&order=DESC';
248
        } else {
249
            $url .= '&order=ASC';
250
        }
251
252
        if (isset($this->request->get['page'])) {
253
            $url .= '&page=' . $this->request->get['page'];
254
        }
255
256
        $data['sort_name'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . '&sort=dd.name' . $url, true);
257
        $data['sort_date_added'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . '&sort=d.date_added' . $url, true);
258
259
        $url = '';
260
261
        if (isset($this->request->get['sort'])) {
262
            $url .= '&sort=' . $this->request->get['sort'];
263
        }
264
265
        if (isset($this->request->get['order'])) {
266
            $url .= '&order=' . $this->request->get['order'];
267
        }
268
269
        $pagination = new \Divine\Engine\Library\Pagination();
270
        $pagination->total = $download_total;
271
        $pagination->page = $page;
272
        $pagination->limit = $this->config->get('config_limit_admin');
273
        $pagination->url = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
274
275
        $data['pagination'] = $pagination->render();
276
277
        $data['results'] = sprintf($this->language->get('text_pagination'), ($download_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($download_total - $this->config->get('config_limit_admin'))) ? $download_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $download_total, ceil($download_total / $this->config->get('config_limit_admin')));
278
279
        $data['sort'] = $sort;
280
        $data['order'] = $order;
281
282
        $data['header'] = $this->load->controller('common/header');
283
        $data['column'] = $this->load->controller('common/column_left');
284
        $data['footer'] = $this->load->controller('common/footer');
285
286
        $this->response->setOutput($this->load->view('catalog/download_list', $data));
287
    }
288
289
    protected function getForm()
290
    {
291
        $data['heading_title'] = $this->language->get('heading_title');
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...
292
293
        $data['text_form'] = !isset($this->request->get['download_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
294
        $data['text_loading'] = $this->language->get('text_loading');
295
296
        $data['entry_name'] = $this->language->get('entry_name');
297
        $data['entry_filename'] = $this->language->get('entry_filename');
298
        $data['entry_mask'] = $this->language->get('entry_mask');
299
300
        $data['help_filename'] = $this->language->get('help_filename');
301
        $data['help_mask'] = $this->language->get('help_mask');
302
303
        $data['button_save'] = $this->language->get('button_save');
304
        $data['button_cancel'] = $this->language->get('button_cancel');
305
        $data['button_upload'] = $this->language->get('button_upload');
306
307
        if (isset($this->error['warning'])) {
308
            $data['error_warning'] = $this->error['warning'];
309
        } else {
310
            $data['error_warning'] = '';
311
        }
312
313
        if (isset($this->error['name'])) {
314
            $data['error_name'] = $this->error['name'];
315
        } else {
316
            $data['error_name'] = array();
317
        }
318
319
        if (isset($this->error['filename'])) {
320
            $data['error_filename'] = $this->error['filename'];
321
        } else {
322
            $data['error_filename'] = '';
323
        }
324
325
        if (isset($this->error['mask'])) {
326
            $data['error_mask'] = $this->error['mask'];
327
        } else {
328
            $data['error_mask'] = '';
329
        }
330
331
        $url = '';
332
333
        if (isset($this->request->get['sort'])) {
334
            $url .= '&sort=' . $this->request->get['sort'];
335
        }
336
337
        if (isset($this->request->get['order'])) {
338
            $url .= '&order=' . $this->request->get['order'];
339
        }
340
341
        if (isset($this->request->get['page'])) {
342
            $url .= '&page=' . $this->request->get['page'];
343
        }
344
345
        $data['breadcrumbs'] = array();
346
347
        $data['breadcrumbs'][] = array(
348
            'text' => $this->language->get('text_home'),
349
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
350
        );
351
352
        $data['breadcrumbs'][] = array(
353
            'text' => $this->language->get('heading_title'),
354
            'href' => $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true)
355
        );
356
357
        if (!isset($this->request->get['download_id'])) {
358
            $data['action'] = $this->url->link('catalog/download/add', 'token=' . $this->session->data['token'] . $url, true);
359
        } else {
360
            $data['action'] = $this->url->link('catalog/download/edit', 'token=' . $this->session->data['token'] . '&download_id=' . $this->request->get['download_id'] . $url, true);
361
        }
362
363
        $data['cancel'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, true);
364
365
        $this->load->model('localisation/language');
366
367
        $data['languages'] = $this->model_localisation_language->getLanguages();
368
369
        if (isset($this->request->get['download_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
370
            $download_info = $this->model_catalog_download->getDownload($this->request->get['download_id']);
371
        }
372
373
        $data['token'] = $this->session->data['token'];
374
375
        if (isset($this->request->get['download_id'])) {
376
            $data['download_id'] = $this->request->get['download_id'];
377
        } else {
378
            $data['download_id'] = 0;
379
        }
380
381
        if (isset($this->request->post['download_description'])) {
382
            $data['download_description'] = $this->request->post['download_description'];
383
        } elseif (isset($this->request->get['download_id'])) {
384
            $data['download_description'] = $this->model_catalog_download->getDownloadDescriptions($this->request->get['download_id']);
385
        } else {
386
            $data['download_description'] = array();
387
        }
388
389
        if (isset($this->request->post['filename'])) {
390
            $data['filename'] = $this->request->post['filename'];
391
        } elseif (!empty($download_info)) {
392
            $data['filename'] = $download_info['filename'];
393
        } else {
394
            $data['filename'] = '';
395
        }
396
397
        if (isset($this->request->post['mask'])) {
398
            $data['mask'] = $this->request->post['mask'];
399
        } elseif (!empty($download_info)) {
400
            $data['mask'] = $download_info['mask'];
401
        } else {
402
            $data['mask'] = '';
403
        }
404
405
        $data['header'] = $this->load->controller('common/header');
406
        $data['column'] = $this->load->controller('common/column_left');
407
        $data['footer'] = $this->load->controller('common/footer');
408
409
        $this->response->setOutput($this->load->view('catalog/download_form', $data));
410
    }
411
412
    protected function validateForm()
413
    {
414
        if (!$this->user->hasPermission('modify', 'catalog/download')) {
415
            $this->error['warning'] = $this->language->get('error_permission');
416
        }
417
418
        foreach ($this->request->post['download_description'] as $language_id => $value) {
419
            if ((\voku\helper\UTF8::strlen($value['name']) < 3) || (\voku\helper\UTF8::strlen($value['name']) > 64)) {
420
                $this->error['name'][$language_id] = $this->language->get('error_name');
421
            }
422
        }
423
424
        if ((\voku\helper\UTF8::strlen($this->request->post['filename']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['filename']) > 128)) {
425
            $this->error['filename'] = $this->language->get('error_filename');
426
        }
427
428
        if (!is_file($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $this->request->post['filename'])) {
429
            $this->error['filename'] = $this->language->get('error_exists');
430
        }
431
432
        if ((\voku\helper\UTF8::strlen($this->request->post['mask']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['mask']) > 128)) {
433
            $this->error['mask'] = $this->language->get('error_mask');
434
        }
435
436
        return !$this->error;
437
    }
438
439
    protected function validateDelete()
440
    {
441
        if (!$this->user->hasPermission('modify', 'catalog/download')) {
442
            $this->error['warning'] = $this->language->get('error_permission');
443
        }
444
445
        $this->load->model('catalog/product');
446
447
        foreach ($this->request->post['selected'] as $download_id) {
448
            $product_total = $this->model_catalog_product->getTotalProductsByDownloadId($download_id);
449
450
            if ($product_total) {
451
                $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
452
            }
453
        }
454
455
        return !$this->error;
456
    }
457
458
    public function upload()
459
    {
460
        $this->load->language('catalog/download');
461
462
        $json = array();
463
464
        // Check user has permission
465
        if (!$this->user->hasPermission('modify', 'catalog/download')) {
466
            $json['error'] = $this->language->get('error_permission');
467
        }
468
469
        if (!$json) {
470
            if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
471
                // Sanitize the filename
472
                $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
473
474
                // Validate the filename length
475
                if ((\voku\helper\UTF8::strlen($filename) < 3) || (\voku\helper\UTF8::strlen($filename) > 128)) {
476
                    $json['error'] = $this->language->get('error_filename');
477
                }
478
479
                // Allowed file extension types
480
                $allowed = array();
481
482
                $extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_ext_allowed'));
483
484
                $filetypes = explode("\n", $extension_allowed);
485
486
                foreach ($filetypes as $filetype) {
487
                    $allowed[] = trim($filetype);
488
                }
489
490
                if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
491
                    $json['error'] = $this->language->get('error_filetype');
492
                }
493
494
                // Allowed file mime types
495
                $allowed = array();
496
497
                $mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_mime_allowed'));
498
499
                $filetypes = explode("\n", $mime_allowed);
500
501
                foreach ($filetypes as $filetype) {
502
                    $allowed[] = trim($filetype);
503
                }
504
505
                if (!in_array($this->request->files['file']['type'], $allowed)) {
506
                    $json['error'] = $this->language->get('error_filetype');
507
                }
508
509
                // Check to see if any PHP files are trying to be uploaded
510
                $content = file_get_contents($this->request->files['file']['tmp_name']);
511
512
                if (preg_match('/\<\?php/i', $content)) {
513
                    $json['error'] = $this->language->get('error_filetype');
514
                }
515
516
                // Return any upload error
517
                if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
518
                    $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
519
                }
520
            } else {
521
                $json['error'] = $this->language->get('error_upload');
522
            }
523
        }
524
525
        if (!$json) {
526
            $file = $filename . '.' . (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(32, 'SR');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename does not seem to be defined for all execution paths leading up to this point.
Loading history...
527
528
            move_uploaded_file($this->request->files['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $file);
529
530
            $json['filename'] = $file;
531
            $json['mask'] = $filename;
532
533
            $json['success'] = $this->language->get('text_upload');
534
        }
535
536
        $this->response->addHeader('Content-Type: application/json');
537
        $this->response->setOutput(json_encode($json));
538
    }
539
540
    public function autocomplete()
541
    {
542
        $json = array();
543
544
        if (isset($this->request->get['filter_name'])) {
545
            $this->load->model('catalog/download');
546
547
            $filter_data = array(
548
                'filter_name' => $this->request->get['filter_name'],
549
                'start'       => 0,
550
                'limit'       => 5
551
            );
552
553
            $results = $this->model_catalog_download->getDownloads($filter_data);
554
555
            foreach ($results as $result) {
556
                $json[] = array(
557
                    'download_id' => $result['download_id'],
558
                    'name'        => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
559
                );
560
            }
561
        }
562
563
        $sort_order = array();
564
565
        foreach ($json as $key => $value) {
566
            $sort_order[$key] = $value['name'];
567
        }
568
569
        array_multisort($sort_order, SORT_ASC, $json);
570
571
        $this->response->addHeader('Content-Type: application/json');
572
        $this->response->setOutput(json_encode($json));
573
    }
574
}
575