Issues (2407)

application/controller/blog/article.php (13 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 ControllerBlogArticle 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();
0 ignored issues
show
The private property $error is not used, and could be removed.
Loading history...
26
27
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('blog/article');
30
31
        $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...
32
33
        $data['breadcrumbs'][] = array(
34
            'text'      => $this->language->get('text_home'),
35
            'href'      => $this->url->link('common/home'),
36
            'separator' => false
37
        );
38
39
        $configblog_name = $this->config->get('configblog_name');
40
41
        if (!empty($configblog_name)) {
42
            $name = $this->config->get('configblog_name');
43
        } else {
44
            $name = $this->language->get('text_blog');
45
        }
46
47
        $data['breadcrumbs'][] = array(
48
            'text' => $name,
49
            'href' => $this->url->link('blog/latest')
50
        );
51
52
        $this->load->model('blog/category');
53
54
55
        if (isset($this->request->get['blog_category_id'])) {
56
            $blog_category_id = '';
57
58
            foreach (explode('_', $this->request->get['blog_category_id']) as $path_id) {
59
                if (!$blog_category_id) {
60
                    $blog_category_id = $path_id;
61
                } else {
62
                    $blog_category_id .= '_' . $path_id;
63
                }
64
65
                $category_info = $this->model_blog_category->getCategory($path_id);
66
67
                if ($category_info) {
68
                    $data['breadcrumbs'][] = array(
69
                        'text'      => $category_info['name'],
70
                        'href'      => $this->url->link('blog/category', 'blog_category_id=' . $blog_category_id)
71
                    );
72
                }
73
            }
74
        }
75
76
77
78
79
80
        if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_tag'])) {
81
            $url = '';
82
83
            if (isset($this->request->get['filter_name'])) {
84
                $url .= '&filter_name=' . $this->request->get['filter_name'];
85
            }
86
87
            if (isset($this->request->get['filter_tag'])) {
88
                $url .= '&filter_tag=' . $this->request->get['filter_tag'];
89
            }
90
91
            if (isset($this->request->get['filter_description'])) {
92
                $url .= '&filter_description=' . $this->request->get['filter_description'];
93
            }
94
95
            if (isset($this->request->get['filter_news_id'])) {
96
                $url .= '&filter_news_id=' . $this->request->get['filter_news_id'];
97
            }
98
        }
99
100
        if (isset($this->request->get['article_id'])) {
101
            $article_id = (int)$this->request->get['article_id'];
102
        } else {
103
            $article_id = 0;
104
        }
105
106
        $this->load->model('blog/article');
107
108
        $article_info = $this->model_blog_article->getArticle($article_id);
109
110
        if ($article_info) {
111
            $url = '';
112
113
            if (isset($this->request->get['blog_category_id'])) {
114
                $url .= '&blog_category_id=' . $this->request->get['blog_category_id'];
115
            }
116
117
            if (isset($this->request->get['filter_name'])) {
118
                $url .= '&filter_name=' . $this->request->get['filter_name'];
119
            }
120
121
            if (isset($this->request->get['filter_tag'])) {
122
                $url .= '&filter_tag=' . $this->request->get['filter_tag'];
123
            }
124
125
            if (isset($this->request->get['filter_description'])) {
126
                $url .= '&filter_description=' . $this->request->get['filter_description'];
127
            }
128
129
            if (isset($this->request->get['filter_news_id'])) {
130
                $url .= '&filter_news_id=' . $this->request->get['filter_news_id'];
131
            }
132
133
            $data['breadcrumbs'][] = array(
134
                'text' => $article_info['name'],
135
                'href' => $this->url->link('blog/article', 'article_id=' . $this->request->get['article_id'])
136
            );
137
138
            if ($article_info['meta_title']) {
139
                $this->document->setTitle($article_info['meta_title']);
140
            } else {
141
                $this->document->setTitle($article_info['name']);
142
            }
143
144
            if ($article_info['noindex'] <= 0) {
145
                $this->document->setRobots('noindex,follow');
146
            }
147
148
            $this->document->setDescription($article_info['meta_description']);
149
            $this->document->addLink($this->url->link('blog/article', 'article_id=' . $this->request->get['article_id']), 'canonical');
150
151
            if ($article_info['meta_h1']) {
152
                $data['heading_title'] = $article_info['meta_h1'];
153
            } else {
154
                $data['heading_title'] = $article_info['name'];
155
            }
156
157
            $data['text_select'] = $this->language->get('text_select');
158
            $data['text_write'] = $this->language->get('text_write');
159
            $data['text_login'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', '', true), $this->url->link('account/register', '', true));
160
            $data['text_loading'] = $this->language->get('text_loading');
161
            $data['text_note'] = $this->language->get('text_note');
162
            $data['text_share'] = $this->language->get('text_share');
163
            $data['text_wait'] = $this->language->get('text_wait');
164
            $data['button_buy_it'] = $this->language->get('button_buy_it');
165
            $data['entry_name'] = $this->language->get('entry_name');
166
            $data['entry_review'] = $this->language->get('entry_review');
167
            $data['entry_good'] = $this->language->get('entry_good');
168
            $data['entry_bad'] = $this->language->get('entry_bad');
169
170
            $data['text_go_back'] = $this->language->get('text_go_back');
171
172
            $data['button_continue'] = $this->language->get('button_continue');
173
174
            $this->load->model('blog/review');
175
176
            $data['text_related'] = $this->language->get('text_related');
177
            $data['text_related_product'] = $this->language->get('text_related_product');
178
179
            $data['article_id'] = $this->request->get['article_id'];
180
181
            $data['review_status'] = $this->config->get('configblog_review_status');
182
183
            if ($this->config->get('configblog_review_guest') || $this->customer->isLogged()) {
184
                $data['review_guest'] = true;
185
            } else {
186
                $data['review_guest'] = false;
187
            }
188
189
            if ($this->customer->isLogged()) {
190
                $data['customer_name'] = $this->customer->getFirstName() . '&nbsp;' . $this->customer->getLastName();
191
            } else {
192
                $data['customer_name'] = '';
193
            }
194
195
            $data['article_review'] = (int)$article_info['article_review'];
196
            $data['reviews'] = sprintf($this->language->get('text_reviews'), (int)$article_info['reviews']);
197
            $data['gstatus'] = (int)$article_info['gstatus'];
198
            $data['description'] = html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8');
199
200
            $data['articles'] = array();
201
202
            $data['button_more'] = $this->language->get('button_more');
203
            $data['text_views'] = $this->language->get('text_views');
204
205
206
207
            $results = $this->model_blog_article->getArticleRelated($this->request->get['article_id']);
208
209
            foreach ($results as $result) {
210
                if ($result['image']) {
211
                    $image = '/public_html/assets/images/' . $result['image'];
212
                } else {
213
                    $image = '/public_html/assets/images/no_image.png';
214
                }
215
216
                $data['articles'][] = array(
217
                    'article_id' => $result['article_id'],
218
                    'thumb'        => $image,
219
                    'name'         => $result['name'],
220
                    'description' => \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('configblog_article_description_length')) . '..',
0 ignored issues
show
Are you sure voku\helper\UTF8::substr...e_description_length')) of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

220
                    'description' => /** @scrutinizer ignore-type */ \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('configblog_article_description_length')) . '..',
Loading history...
221
                    'date_added'  => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
222
                    'viewed'      => $result['viewed'],
223
                    'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
224
                    'href'         => $this->url->link('blog/article', 'article_id=' . $result['article_id']),
225
                );
226
            }
227
228
229
            $data['products'] = array();
230
231
            $results = $this->model_blog_article->getArticleRelatedProduct($this->request->get['article_id']);
232
233
            foreach ($results as $result) {
234
                if ($result['image']) {
235
                    $image = '/public_html/assets/images/' . $result['image'];
236
                } else {
237
                    $image = '/public_html/assets/images/no_image.png';
238
                }
239
240
                if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
241
                    $price = $this->currency->format($result['price'], $this->session->data['currency']);
242
                } else {
243
                    $price = false;
244
                }
245
246
                if ((float)$result['special']) {
247
                    $special = $this->currency->format($result['special'], $this->session->data['currency']);
248
                } else {
249
                    $special = false;
250
                }
251
252
                if ((float)$result['special']) {
253
                    $yousave_percent = round(((($result['price'] - $result['special']) / $result['price']) * 100), 0);
254
                } else {
255
                    $yousave_percent = false;
256
                }
257
258
                $stickers = $this->getStickers($result['product_id']);
259
260
                $data['products'][] = array(
261
                    'product_id' => $result['product_id'],
262
                    'thumb'        => $image,
263
                    'name'         => $result['name'],
264
                    'description' => \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('configblog_article_description_length')) . '..',
265
                    'price'        => $price,
266
                    'special'      => $special,
267
                    'yousave_percent' => $yousave_percent,
268
                    'sticker'     => $stickers,
269
                    'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
270
                    'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
271
                    'href'         => $this->url->link('product/product', 'product_id=' . $result['product_id']),
272
                );
273
            }
274
275
            $data['download_status'] = $this->config->get('configblog_article_download');
276
277
            $data['downloads'] = array();
278
279
            $results = $this->model_blog_article->getDownloads($this->request->get['article_id']);
280
281
            foreach ($results as $result) {
282
                if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $result['filename'])) {
283
                    $size = filesize($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $result['filename']);
284
285
                    $i = 0;
286
287
                    $suffix = array(
288
                        'B',
289
                        'KB',
290
                        'MB',
291
                        'GB',
292
                        'TB',
293
                        'PB',
294
                        'EB',
295
                        'ZB',
296
                        'YB'
297
                    );
298
299
                    while (($size / 10024) > 1) {
300
                        $size = $size / 10024;
301
                        $i++;
302
                    }
303
304
                    $data['downloads'][] = array(
305
                        'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
306
                        'name'       => $result['name'],
307
                        '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

307
                        'size'       => round(/** @scrutinizer ignore-type */ substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
Loading history...
308
                        'href'       => $this->url->link('blog/article/download', '&article_id=' . $this->request->get['article_id'] . '&download_id=' . $result['download_id'])
309
                    );
310
                }
311
            }
312
313
            $this->model_blog_article->updateViewed($this->request->get['article_id']);
314
315
            $data['column'] = $this->load->controller('common/column');
316
317
            $data['content_top'] = $this->load->controller('common/content_top');
318
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
319
            $data['footer'] = $this->load->controller('common/footer');
320
            $data['header'] = $this->load->controller('common/header');
321
322
            $this->response->setOutput($this->load->view('blog/article', $data));
323
        } else {
324
            $url = '';
325
326
            if (isset($this->request->get['blog_category_id'])) {
327
                $url .= '&blog_category_id=' . $this->request->get['blog_category_id'];
328
            }
329
330
            if (isset($this->request->get['filter_name'])) {
331
                $url .= '&filter_name=' . $this->request->get['filter_name'];
332
            }
333
334
            if (isset($this->request->get['filter_tag'])) {
335
                $url .= '&filter_tag=' . $this->request->get['filter_tag'];
336
            }
337
338
            if (isset($this->request->get['filter_description'])) {
339
                $url .= '&filter_description=' . $this->request->get['filter_description'];
340
            }
341
342
            if (isset($this->request->get['filter_news_id'])) {
343
                $url .= '&filter_news_id=' . $this->request->get['filter_news_id'];
344
            }
345
346
            $data['breadcrumbs'][] = array(
347
                'text' => $this->language->get('text_error'),
348
                'href' => $this->url->link('product/product', $url . '&product_id=' . $article_id)
349
            );
350
351
            $this->document->setTitle($this->language->get('text_error'));
352
353
            $data['heading_title'] = $this->language->get('text_error');
354
355
            $data['text_error'] = $this->language->get('text_error');
356
            $data['text_go_back'] = $this->language->get('text_go_back');
357
            $data['text_get_back'] = $this->language->get('text_get_back');
358
359
            $data['button_continue'] = $this->language->get('button_continue');
360
361
            $data['continue'] = $this->url->link('common/home');
362
363
            $this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');
364
365
            $data['column'] = $this->load->controller('common/column');
366
367
            $data['content_top'] = $this->load->controller('common/content_top');
368
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
369
            $data['footer'] = $this->load->controller('common/footer');
370
            $data['header'] = $this->load->controller('common/header');
371
372
            $this->response->setOutput($this->load->view('error/not_found', $data));
373
        }
374
    }
375
376
    public function download()
377
    {
378
        $this->load->model('blog/article');
379
380
        if (isset($this->request->get['download_id'])) {
381
            $download_id = $this->request->get['download_id'];
382
        } else {
383
            $download_id = 0;
384
        }
385
386
        if (isset($this->request->get['article_id'])) {
387
            $article_id = $this->request->get['article_id'];
388
        } else {
389
            $article_id = 0;
390
        }
391
392
        $download_info = $this->model_blog_article->getDownload($article_id, $download_id);
393
394
        if ($download_info) {
395
            $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $download_info['filename'];
396
            $mask = basename($download_info['mask']);
397
398
            if (!headers_sent()) {
399
                if (file_exists($file)) {
400
                    header('Content-Description: File Transfer');
401
                    header('Content-Type: application/octet-stream');
402
                    header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
403
                    header('Content-Transfer-Encoding: binary');
404
                    header('Expires: 0');
405
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
406
                    header('Pragma: public');
407
                    header('Content-Length: ' . filesize($file));
408
409
                    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

409
                    readfile($file, /** @scrutinizer ignore-type */ 'rb');
Loading history...
410
411
                    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...
412
                } else {
413
                    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...
414
                }
415
            } else {
416
                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...
417
            }
418
        } else {
419
            $this->redirect('/index.php?route=account/download');
0 ignored issues
show
The method redirect() does not exist on ControllerBlogArticle. ( Ignorable by Annotation )

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

419
            $this->/** @scrutinizer ignore-call */ 
420
                   redirect('/index.php?route=account/download');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
420
        }
421
    }
422
423
    public function review()
424
    {
425
        $this->language->load('blog/article');
426
427
        $this->load->model('blog/review');
428
429
        $data['text_on'] = $this->language->get('text_on');
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...
430
        $data['text_no_reviews'] = $this->language->get('text_no_reviews');
431
432
        if (isset($this->request->get['page'])) {
433
            $page = $this->request->get['page'];
434
        } else {
435
            $page = 1;
436
        }
437
438
        $data['reviews'] = array();
439
440
        $review_total = $this->model_blog_review->getTotalReviewsByArticleId($this->request->get['article_id']);
441
442
        $results = $this->model_blog_review->getReviewsByArticleId($this->request->get['article_id'], ($page - 1) * 5, 5);
443
444
        foreach ($results as $result) {
445
            $data['reviews'][] = array(
446
                'author'     => $result['author'],
447
                'text'       => $result['text'],
448
                'reviews'    => sprintf($this->language->get('text_reviews'), (int)$review_total),
449
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
450
            );
451
        }
452
453
        $pagination = new \Divine\Engine\Library\Pagination();
454
        $pagination->total = $review_total;
455
        $pagination->page = $page;
456
        $pagination->limit = 5;
457
        $pagination->url = $this->url->link('blog/article/review', 'article_id=' . $this->request->get['article_id'] . '&page={page}');
458
459
        $data['pagination'] = $pagination->render();
460
461
        $data['results'] = sprintf($this->language->get('text_pagination'), ($review_total) ? (($page - 1) * 5) + 1 : 0, ((($page - 1) * 5) > ($review_total - 5)) ? $review_total : ((($page - 1) * 5) + 5), $review_total, ceil($review_total / 5));
462
463
        $this->response->setOutput($this->load->view('blog/review', $data));
464
    }
465
466
    public function write()
467
    {
468
        $this->load->language('blog/article');
469
470
        $json = array();
471
472
        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
473
            if ((\voku\helper\UTF8::strlen($this->request->post['name']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['name']) > 25)) {
474
                $json['error'] = $this->language->get('error_name');
475
            }
476
477
            if ((\voku\helper\UTF8::strlen($this->request->post['text']) < 25) || (\voku\helper\UTF8::strlen($this->request->post['text']) > 1000)) {
478
                $json['error'] = $this->language->get('error_text');
479
            }
480
481
            if (!isset($json['error'])) {
482
                $this->load->model('blog/review');
483
484
                $this->model_blog_review->addReview($this->request->get['article_id'], $this->request->post);
485
486
                $json['success'] = $this->language->get('text_success');
487
            }
488
        }
489
490
        $this->response->addHeader('Content-Type: application/json');
491
        $this->response->setOutput(json_encode($json));
492
    }
493
494
    private function getStickers($product_id)
495
    {
496
        $stickers = $this->model_catalog_product->getProductStickerbyProductId($product_id);
497
498
499
        if (!$stickers) {
500
            return;
501
        }
502
503
        $data['stickers'] = 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...
504
505
        foreach ($stickers as $sticker) {
506
            $data['stickers'][] = array(
507
                'position' => $sticker['position'],
508
                'image' => '/public_html/assets/images/' . $sticker['image']
509
            );
510
        }
511
512
        return $this->load->view('product/stickers', $data);
513
    }
514
}
515