ControllerStartupUrlFormatter   F
last analyzed

Complexity

Total Complexity 120

Size/Duplication

Total Lines 610
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 314
c 0
b 0
f 0
dl 0
loc 610
rs 2
wmc 120

9 Methods

Rating   Name   Duplication   Size   Complexity  
F rewrite() 0 200 51
A getPathByProduct() 0 36 6
A getQueryString() 0 11 2
A __construct() 0 49 5
B getPathByBlogCategory() 0 47 8
B getPathByCategory() 0 46 8
B validate() 0 29 7
A getPathByArticle() 0 31 6
F index() 0 113 27

How to fix   Complexity   

Complex Class

Complex classes like ControllerStartupUrlFormatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ControllerStartupUrlFormatter, and based on these observations, apply Extract Interface, too.

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 ControllerStartupUrlFormatter 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 $cache_data = null;
26
    private $languages = array();
27
    private $config_language;
28
    private $url_sheme = 'https';
29
    private $ssl_routes = array(
30
        'checkout/',
31
        'account/',
32
    );
33
34
    // exceptions routes
35
    private $valide_routes = array(
36
        'tracking',
37
        'utm_source',
38
        'utm_campaign',
39
        'utm_medium',
40
        'type',
41
        'source',
42
        'block',
43
        'position',
44
        'keyword',
45
        'yclid',
46
        'gclid'
47
    );
48
49
    public function __construct($registry)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
50
    {
51
        parent::__construct($registry);
52
53
        // cache
54
        $this->cache_data = $this->cache->get('url_formatter');
55
56
        if (!$this->cache_data) {
57
            $query = $this->db->query("
58
                SELECT LOWER(`keyword`) as 'keyword', 
59
                    `query` 
60
                FROM url_alias
61
            ");
62
63
            $this->cache_data = array();
64
65
            foreach ($query->rows as $row) {
66
                $this->cache_data['keywords'][$row['keyword']] = $row['query'];
67
                $this->cache_data['queries'][$row['query']] = $row['keyword'];
68
            }
69
70
            if (!isset($this->cache_data['queries']['common/home'])) {
71
                $this->cache_data['queries']['common/home'] = '';
72
                $this->cache_data['keywords'][''] = 'common/home';
73
            }
74
75
            $this->cache->set(
76
                'url_formatter',
77
                $this->cache_data
78
            );
79
        }
80
        //
81
82
        $query = $this->db->query("
83
            SELECT `value` 
84
            FROM `setting` 
85
            WHERE `key` = 'config_language'
86
        ");
87
88
        $this->config_language = $query->row['value'];
89
90
        $query = $this->db->query("
91
            SELECT * 
92
            FROM language 
93
            WHERE status = '1'
94
        ");
95
96
        foreach ($query->rows as $result) {
97
            $this->languages[$result['code']] = $result;
98
        }
99
    }
100
101
    public function index()
102
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
103
104
        // Add rewrite to url class
105
        if ($this->config->get('config_seo_url')) {
106
            $this->url->addRewrite($this);
107
        } else {
108
            return;
109
        }
110
111
        // Для cron скриптов, типа YML feed
112
        if (php_sapi_name() === 'cli') {
113
            return;
114
        }
115
116
        // Decode URL
117
        if (!isset($this->request->get['_route_'])) {
118
            $this->validate();
119
        } else {
120
            $route_ = $this->request->get['_route_'];
121
122
            unset($this->request->get['_route_']);
123
124
            $parts = explode('/', trim(\voku\helper\UTF8::strtolower($route_), '/'));
125
126
            list($last_part) = explode('.', array_pop($parts));
127
128
            array_push($parts, $last_part);
129
130
            $rows = array();
131
132
            foreach ($parts as $keyword) {
133
                if (isset($this->cache_data['keywords'][$keyword])) {
134
                    $rows[] = array('keyword' => $keyword, 'query' => $this->cache_data['keywords'][$keyword]);
135
                }
136
            }
137
138
            if (count($rows) == sizeof($parts)) {
139
                $queries = array();
140
141
                foreach ($rows as $row) {
142
                    $queries[\voku\helper\UTF8::strtolower($row['keyword'])] = $row['query'];
143
                }
144
145
                reset($parts);
146
147
                foreach ($parts as $part) {
148
                    $url = explode('=', $queries[$part], 2);
149
150
                    if ($url[0] == 'category_id') {
151
                        if (!isset($this->request->get['path'])) {
152
                            $this->request->get['path'] = $url[1];
153
                        } else {
154
                            $this->request->get['path'] .= '_' . $url[1];
155
                        }
156
                    } elseif ($url[0] == 'blog_category_id') {
157
                        if (!isset($this->request->get['blog_category_id'])) {
158
                            $this->request->get['blog_category_id'] = $url[1];
159
                        } else {
160
                            $this->request->get['blog_category_id'] .= '_' . $url[1];
161
                        }
162
                    } elseif (count($url) > 1) {
163
                        $this->request->get[$url[0]] = $url[1];
164
                    }
165
                }
166
            } else {
167
                $this->request->get['route'] = 'error/not_found';
168
            }
169
170
            if (isset($this->request->get['product_id'])) {
171
                $this->request->get['route'] = 'product/product';
172
173
                if (!isset($this->request->get['path'])) {
174
                    $path = $this->getPathByProduct($this->request->get['product_id']);
175
176
                    if ($path) {
177
                        $this->request->get['path'] = $path;
178
                    }
179
                }
180
            } elseif (isset($this->request->get['path'])) {
181
                $this->request->get['route'] = 'product/category';
182
183
            //blog
184
            } elseif (isset($this->request->get['article_id'])) {
185
                $this->request->get['route'] = 'blog/article';
186
187
                if (!isset($this->request->get['blog_category_id'])) {
188
                    $blog_category_id = $this->getPathByArticle($this->request->get['article_id']);
189
190
                    if ($blog_category_id) {
191
                        $this->request->get['blog_category_id'] = $blog_category_id;
192
                    }
193
                }
194
            } elseif (isset($this->request->get['blog_category_id'])) {
195
                $this->request->get['route'] = 'blog/category';
196
197
            //blog
198
            } elseif (isset($this->request->get['manufacturer_id'])) {
199
                $this->request->get['route'] = 'product/manufacturer/info';
200
            } elseif (isset($this->request->get['information_id'])) {
201
                $this->request->get['route'] = 'information/information';
202
            } elseif (isset($this->cache_data['queries'][$route_])) {
203
                $this->response->redirect($this->cache_data['queries'][$route_], 301);
204
            } else {
205
                if (isset($queries[$parts[0]])) {
206
                    $this->request->get['route'] = $queries[$parts[0]];
207
                }
208
            }
209
210
            $this->validate();
211
212
            if (isset($this->request->get['route'])) {
213
                return new \Divine\Engine\Core\Action($this->request->get['route']);
214
            }
215
        }
216
    }
217
218
    public function rewrite($link)
219
    {
220
        if (!$this->config->get('config_seo_url')) {
221
            return $link;
222
        }
223
224
        $seo_url = '';
225
226
        $url_info = parse_url(str_replace('&amp;', '&', $link));
227
228
        $data = array();
229
230
        parse_str($url_info['query'], $data);
231
232
        $route = $data['route'];
233
234
        unset($data['route']);
235
236
        $url_info['scheme'] = $this->url_sheme;
237
238
        foreach ($this->ssl_routes as $ssl_route) {
239
            if (stripos($route, $ssl_route) === 0) {
240
                $url_info['scheme'] = 'https';
241
            }
242
        }
243
244
        switch ($route) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
245
246
            case 'product/product':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
247
248
                if (isset($data['product_id'])) {
249
                    $tmp = $data;
250
                    $data = array();
251
252
                    if ($this->config->get('config_seo_url_include_path')) {
253
                        $data['path'] = $this->getPathByProduct($tmp['product_id']);
254
                        if (!$data['path']) {
255
                            return $link;
256
                        }
257
                    }
258
259
                    $data['product_id'] = $tmp['product_id'];
260
261
                    // --- add valide routes
262
                    foreach ($this->valide_routes as $valide_route) {
263
                        if (isset($tmp[$valide_route])) {
264
                            $data[$valide_route] = $tmp[$valide_route];
265
                        }
266
                    }
267
                    // --- add valide routes
268
                }
269
270
                break;
271
272
            case 'product/category':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
273
274
                if (isset($data['path'])) {
275
                    $category = explode('_', $data['path']);
276
                    $category = end($category);
277
                    $data['path'] = $this->getPathByCategory($category);
278
279
                    if (!$data['path']) {
280
                        return $link;
281
                    }
282
                }
283
284
                break;
285
286
            case 'blog/article':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
287
288
                if (isset($data['article_id'])) {
289
                    $tmp = $data;
290
                    $data = array();
291
                    if ($this->config->get('config_seo_url_include_path')) {
292
                        $data['blog_category_id'] = $this->getPathByArticle($tmp['article_id']);
293
                        if (!$data['blog_category_id']) {
294
                            return $link;
295
                        }
296
                    }
297
                    $data['article_id'] = $tmp['article_id'];
298
                }
299
300
                break;
301
302
            case 'blog/category':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
303
304
                if (isset($data['blog_category_id'])) {
305
                    $blog_category_id = explode('_', $data['blog_category_id']);
306
                    $blog_category_id = end($blog_category_id);
307
                    $data['blog_category_id'] = $this->getPathByBlogCategory($blog_category_id);
308
309
                    if (!$data['blog_category_id']) {
310
                        return $link;
311
                    }
312
                }
313
314
                break;
315
316
            case 'product/product/review':
317
            case 'information/information/info':
318
            case 'information/information/agree':
319
                return $link;
320
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
321
            default:
322
                break;
323
        }
324
325
        $link = $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '');
326
327
        //fix subfolder ($url_info['path'])
328
        $link .= $url_info['path'] . '?route=' . $route . (count($data) ? '&amp;' . urldecode(http_build_query($data, '', '&amp;')) : '');
329
330
        $queries = array();
331
332
        foreach ($data as $key => $value) {
333
            switch ($key) {
334
                case 'product_id':
335
                case 'article_id':
336
                case 'manufacturer_id':
337
                case 'category_id':
338
                case 'information_id':
339
                case 'order_id':
340
                case 'download_id':
341
                case 'search':
342
                case 'sub_category':
343
                case 'description':
344
                    $queries[] = $key . '=' . $value;
345
                    unset($data[$key]);
346
                    $postfix = 1;
347
                    break;
348
                case 'path':
349
                    $categories = explode('_', $value);
350
                    foreach ($categories as $category) {
351
                        $queries[] = 'category_id=' . $category;
352
                    }
353
                    unset($data[$key]);
354
                    break;
355
                case 'blog_category_id':
356
                    $blog_categories = explode('_', $value);
357
                    foreach ($blog_categories as $blog_category) {
358
                        $queries[] = 'blog_category_id=' . $blog_category;
359
                    }
360
                    unset($data[$key]);
361
                    break;
362
                default:
363
                    break;
364
            }
365
        }
366
367
        if (empty($queries)) {
368
            $queries[] = $route;
369
        }
370
371
        $rows = array();
372
373
        foreach ($queries as $query) {
374
            if (isset($this->cache_data['queries'][$query])) {
375
                $rows[] = array('query' => $query, 'keyword' => $this->cache_data['queries'][$query]);
376
            }
377
        }
378
379
        if (count($rows) == count($queries)) {
380
            $aliases = array();
381
382
            foreach ($rows as $row) {
383
                $aliases[$row['query']] = $row['keyword'];
384
            }
385
386
            foreach ($queries as $query) {
387
                $seo_url .= '/' . rawurlencode($aliases[$query]);
388
            }
389
        }
390
391
        if ($seo_url == '') {
392
            return $link;
393
        }
394
395
        $seo_url = trim($seo_url, '/');
396
397
        //fix subfolder
398
        $path = rtrim($url_info['path'], '/index.php');
399
400
        $seo_url = $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . $path . '/' . $seo_url;
401
402
        if (isset($postfix)) {
403
            $seo_url .= trim($this->config->get('config_seo_url_postfix'));
404
        } else {
405
            $seo_url .= '/';
406
        }
407
408
        //fix subfolder
409
        if (substr($seo_url, -2) == '//') {
410
            $seo_url = substr($seo_url, 0, -1);
411
        }
412
413
        if (count($data)) {
414
            $seo_url .= '?' . urldecode(http_build_query($data, '', '&amp;'));
415
        }
416
417
        return $seo_url;
418
    }
419
420
    private function getPathByProduct($product_id)
421
    {
422
        $product_id = (int) $product_id;
423
424
        if ($product_id < 1) {
425
            return false;
426
        }
427
428
        static $path = null;
429
430
        if (!is_array($path)) {
431
            $path = $this->cache->get('product.seopath');
432
433
            if (!is_array($path)) {
434
                $path = array();
435
            }
436
        }
437
438
        if (!isset($path[$product_id])) {
439
            $query = $this->db->query("
440
                SELECT category_id 
441
                FROM product_to_category 
442
                WHERE product_id = '" . $product_id . "' 
443
                ORDER BY main_category DESC 
444
                LIMIT 1
445
            ");
446
447
            $path[$product_id] = $this->getPathByCategory($query->num_rows ? (int) $query->row['category_id'] : 0);
448
449
            $this->cache->set(
450
                'product.seopath',
451
                $path
452
            );
453
        }
454
455
        return $path[$product_id];
456
    }
457
458
    private function getPathByCategory($category_id)
459
    {
460
        $category_id = (int) $category_id;
461
462
        if ($category_id < 1) {
463
            return false;
464
        }
465
466
        static $path = null;
467
468
        if (!is_array($path)) {
469
            $path = $this->cache->get('category.seopath');
470
471
            if (!is_array($path)) {
472
                $path = array();
473
            }
474
        }
475
476
        if (!isset($path[$category_id])) {
477
            $max_level = 10;
478
479
            $sql = "SELECT CONCAT_WS('_'";
480
481
            for ($i = $max_level - 1; $i >= 0; --$i) {
482
                $sql .= ",t$i.category_id";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $i instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
483
            }
484
485
            $sql .= ") AS path FROM category t0";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ) AS path FROM category t0 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
486
487
            for ($i = 1; $i < $max_level; ++$i) {
488
                $sql .= " LEFT JOIN category t$i ON (t$i.category_id = t" . ($i - 1) . ".parent_id)";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $i instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Comprehensibility introduced by
The string literal .parent_id) does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
489
            }
490
491
            $sql .= " WHERE t0.category_id = '" . $category_id . "'";
492
493
            $query = $this->db->query($sql);
494
495
            $path[$category_id] = $query->num_rows ? $query->row['path'] : false;
496
497
            $this->cache->set(
498
                'category.seopath',
499
                $path
500
            );
501
        }
502
503
        return $path[$category_id];
504
    }
505
506
    //blog
507
    private function getPathByBlogCategory($blog_category_id)
508
    {
509
        $blog_category_id = (int) $blog_category_id;
510
511
        if ($blog_category_id < 1) {
512
            return false;
513
        }
514
515
        static $path = null;
516
517
        if (!is_array($path)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
518
519
            // $path = $this->cache->get('blog_category.seopath');
520
521
            if (!is_array($path)) {
522
                $path = array();
523
            }
524
        }
525
526
        if (!isset($path[$blog_category_id])) {
527
            $max_level = 10;
528
529
            $sql = "SELECT CONCAT_WS('_'";
530
531
            for ($i = $max_level - 1; $i >= 0; --$i) {
532
                $sql .= ",t$i.blog_category_id";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $i instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
533
            }
534
535
            $sql .= ") AS path FROM blog_category t0";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ) AS path FROM blog_category t0 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
536
537
            for ($i = 1; $i < $max_level; ++$i) {
538
                $sql .= " LEFT JOIN blog_category t$i ON (t$i.blog_category_id = t" . ($i - 1) . ".parent_id)";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $i instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Comprehensibility introduced by
The string literal .parent_id) does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
539
            }
540
541
            $sql .= " WHERE t0.blog_category_id = '" . $blog_category_id . "'";
542
543
            $query = $this->db->query($sql);
544
545
            $path[$blog_category_id] = $query->num_rows ? $query->row['path'] : false;
546
547
            // $this->cache->set(
548
            //     'blog_category.seopath',
549
            //     $path
550
            // );
551
        }
552
553
        return $path[$blog_category_id];
554
    }
555
556
    private function getPathByArticle($article_id)
557
    {
558
        $article_id = (int) $article_id;
559
560
        if ($article_id < 1) {
561
            return false;
562
        }
563
564
        static $path = null;
565
566
        if (!is_array($path)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
567
568
            // $path = $this->cache->get('article.seopath');
569
570
            if (!is_array($path)) {
571
                $path = array();
572
            }
573
        }
574
575
        if (!isset($path[$article_id])) {
576
            $query = $this->db->query("SELECT blog_category_id FROM article_to_blog_category WHERE article_id = '" . $article_id . "' ORDER BY main_blog_category DESC LIMIT 1");
577
578
            $path[$article_id] = $this->getPathByBlogCategory($query->num_rows ? (int) $query->row['blog_category_id'] : 0);
579
580
            // $this->cache->set(
581
            //     'article.seopath',
582
            //     $path
583
            // );
584
        }
585
586
        return $path[$article_id];
587
    }
588
589
    //blog
590
    private function validate()
591
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
592
593
        //fix flat link for xml feed
594
        if (isset($this->request->get['route'])) {
595
            $break_routes = [
596
                'error/not_found',
597
                'extension/feed/sitemap',
598
            ];
599
600
            if (in_array($this->request->get['route'], $break_routes)) {
601
                return;
602
            }
603
        }
604
605
        if (empty($this->request->get['route'])) {
606
            $this->request->get['route'] = 'common/home';
607
        }
608
609
        if (isset($this->request->server['HTTP_X_REQUESTED_WITH']) && strtolower($this->request->server['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
610
            return;
611
        }
612
613
        $url = str_replace('&amp;', '&', substr($this->config->get('config_url'), 0, strpos($this->config->get('config_url'), '/', 10)) . $this->request->server['REQUEST_URI']);
614
615
        $seo = str_replace('&amp;', '&', $this->url->link($this->request->get['route'], $this->getQueryString(array('route'))));
616
617
        if (rawurldecode($url) != rawurldecode($seo)) {
618
            $this->response->redirect($seo, 301);
619
        }
620
    }
621
622
    private function getQueryString($exclude = array())
623
    {
624
        if (!is_array($exclude)) {
625
            $exclude = array();
626
        }
627
628
        return urldecode(
629
            http_build_query(
630
                array_diff_key(
631
                    $this->request->get,
632
                    array_flip($exclude)
633
                )
634
            )
635
        );
636
    }
637
}
638