Seo::generateUrl()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
nc 10
nop 3
dl 0
loc 28
rs 8.4444
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 * $Id
7
 * Module: Publisher
8
 * Author: Sudhaker Raj <https://xoops.biz>
9
 * Licence: GNU
10
 */
11
12
// define PUBLISHER_SEO_ENABLED in mainfile.php, possible values
13
//   are "rewrite" & "path-info"
14
15
/**
16
 * Create a title for the shortUrl field of an article
17
 *
18
 * @credit psylove
19
 *
20
 * @return string sort_url for the article
21
 * @var    string $withExt do we add an html extension or not
22
 * @var    string $title   title of the article
23
 */
24
require_once \dirname(__DIR__) . '/include/common.php';
25
26
/**
27
 * Class Seo
28
 */
29
class Seo
30
{
31
    /**
32
     * @param string $title
33
     * @param bool   $withExt
34
     *
35
     * @return mixed|string
36
     */
37
    public static function getTitle($title = '', $withExt = true)
38
    {
39
        /**
40
         * if XOOPS ML is present, let's sanitize the title with the current language
41
         */
42
        $myts = \MyTextSanitizer::getInstance();
43
        if (\method_exists($myts, 'formatForML')) {
44
            $title = $myts->formatForML($title);
45
        }
46
47
        // Transformation de la chaine en minuscule
48
        // Codage de la chaine afin d'éviter les erreurs 500 en cas de caractères imprévus
49
        $title = \rawurlencode(\mb_strtolower($title));
50
51
        // Transformation des ponctuations
52
        $pattern    = [
53
            '/%09/', // Tab
54
            '/%20/', // Space
55
            '/%21/', // !
56
            '/%22/', // "
57
            '/%23/', // #
58
            '/%25/', // %
59
            '/%26/', // &
60
            '/%27/', // '
61
            '/%28/', // (
62
            '/%29/', // )
63
            '/%2C/', // ,
64
            '/%2F/', // /
65
            '/%3A/', // :
66
            '/%3B/', // ;
67
            '/%3C/', // <
68
            '/%3D/', // =
69
            '/%3E/', // >
70
            '/%3F/', // ?
71
            '/%40/', // @
72
            '/%5B/', // [
73
            '/%5C/', // \
74
            '/%5D/', // ]
75
            '/%5E/', // ^
76
            '/%7B/', // {
77
            '/%7C/', // |
78
            '/%7D/', // }
79
            '/%7E/', // ~
80
            '/\./', // .
81
        ];
82
        $repPattern = ['-', '-', '', '', '', '-100', '', '-', '', '', '', '-', '', '', '', '-', '', '', '-at-', '', '-', '', '-', '', '-', '', '-', ''];
83
        $title      = \preg_replace($pattern, $repPattern, $title);
84
85
        // Transformation des caractères accentués
86
        //                  è        é        ê        ë        ç        à        â        ä        î        ï        ù        ü        û        ô        ö
87
        $pattern    = ['/%B0/', '/%E8/', '/%E9/', '/%EA/', '/%EB/', '/%E7/', '/%E0/', '/%E2/', '/%E4/', '/%EE/', '/%EF/', '/%F9/', '/%FC/', '/%FB/', '/%F4/', '/%F6/'];
88
        $repPattern = ['-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o'];
89
        $title      = \preg_replace($pattern, $repPattern, $title);
90
91
        if (\count($title) > 0) {
0 ignored issues
show
Bug introduced by
$title of type string is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

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

91
        if (\count(/** @scrutinizer ignore-type */ $title) > 0) {
Loading history...
92
            if ($withExt) {
93
                $title .= '.html';
94
            }
95
96
            return $title;
97
        }
98
99
        return '';
100
    }
101
102
    /**
103
     * @param              $op
104
     * @param              $id
105
     * @param string|array $shortUrl
106
     *
107
     * @return string
108
     */
109
    public static function generateUrl($op, $id, $shortUrl = '')
110
    {
111
        $helper = Helper::getInstance();
112
        if ('none' !== $helper->getConfig('seo_url_rewrite')) {
113
            if (!empty($shortUrl)) {
114
                $shortUrl .= '.html';
115
            }
116
117
            if ('htaccess' === $helper->getConfig('seo_url_rewrite')) {
118
                // generate SEO url using htaccess
119
                return XOOPS_URL . '/' . $helper->getConfig('seo_module_name') . ".{$op}.{$id}/{$shortUrl}";
120
            }
121
122
            if ('path-info' === $helper->getConfig('seo_url_rewrite')) {
123
                // generate SEO url using path-info
124
                return PUBLISHER_URL . "/index.php/{$op}.{$id}/{$shortUrl}";
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Publisher\PUBLISHER_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
125
            }
126
            exit('Unknown SEO method.');
0 ignored issues
show
Best Practice introduced by
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...
127
        }
128
        // generate classic url
129
        switch ($op) {
130
            case 'category':
131
                return PUBLISHER_URL . "/{$op}.php?categoryid={$id}";
132
            case 'item':
133
            case 'print':
134
                return PUBLISHER_URL . "/$op.php?itemid={$id}";
135
            default:
136
                exit('Unknown SEO operation.');
0 ignored issues
show
Best Practice introduced by
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...
137
        }
138
    }
139
}
140