Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

replace_links()   D

Complexity

Conditions 29
Paths 49

Size

Total Lines 135
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 29
eloc 101
nc 49
nop 1
dl 0
loc 135
rs 4.4524
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3
4
use XoopsModules\Newbb;
5
6
define('REAL_MODULE_NAME', 'modules/newbb');  //this is the Real Module directory
7
define('SEO_MODULE_NAME', 'modules/newbb');  //this is SEO Name for rewrite Hack
8
9
//ob_start('seo_urls');
10
11
/**
12
 * @param $s
13
 * @return mixed
14
 */
15
function seo_urls($s)
16
{
17
    $XPS_URL     = str_replace('/', '\/', quotemeta(XOOPS_URL));
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
    $module_name = str_replace('/', '\/', quotemeta(SEO_MODULE_NAME));
19
20
    $search = [
21
22
        // Search URLs of modules' directry.
23
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(index.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
24
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(viewpost.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
25
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(rss.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
26
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(viewforum.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
27
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(viewtopic.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
28
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(newtopic.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
29
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/' . $module_name . '\/(.*)([^>\'\"]*)([\'\"]{1})([^>]*)>/i'
30
    ];
31
32
    $s = preg_replace_callback($search, 'replace_links', $s);
33
34
    return $s;
35
}
36
37
/**
38
 * @param $matches
39
 * @return string
40
 */
41
function replace_links($matches)
42
{
43
    switch ($matches[5]) {
44
        case 'index.php':
45
            $add_to_url = '';
46
            $req_string = $matches[6];
47
            if (!empty($matches[6])) {
48
                //                replacing cat=x
49
                if (preg_match('/cat=(\d+)/', $matches[6], $mvars)) {
50
                    $add_to_url = 'c-' . $mvars[1] . '/' . forum_seo_cat($mvars[1]) . '';
51
                    $req_string = preg_replace('/cat=\d+/', '', $matches[6]);
52
                } else {
53
                    return $matches['0'];
54
                }
55
            }
56
            break;
57
        case 'viewpost.php':
58
            $add_to_url = '';
59
            $req_string = $matches[6];
60
            if (!empty($matches[6])) {
61
                //                replacing status=x
62
                if (preg_match('/status=([a-z]+)/', $matches[6], $mvars)) {
63
                    $add_to_url = 'viewpost.php' . $matches[6];
64
                    $req_string = preg_replace('/status=([a-z])+/', '', $matches[6]);
65
                } else {
66
                    return $matches['0'];
67
                }
68
            } else {
69
                $add_to_url = 'viewpost.php' . $matches[6];
70
            }
71
            break;
72
        case 'rss.php':
73
            $add_to_url = '';
74
            $req_string = $matches[6];
75
            if (!empty($matches[6])) {
76
                //                replacing c=x
77
                if (preg_match('/c=(\d+)/', $matches[6], $mvars)) {
78
                    $add_to_url = 'rc-';
79
                    if ($mvars[1] > 0) {
80
                        $add_to_url .= $mvars[1] . '/' . forum_seo_cat($mvars[1]) . '';
81
                    } else {
82
                        $add_to_url .= $mvars[1] . '/rss.html';
83
                    }
84
                    $req_string = preg_replace('/c=\d+/', '', $matches[6]);
85
                } elseif (preg_match('/f=(\d+)/', $matches[6], $mvars)) {
86
                    $add_to_url = 'rf-';
87
                    if ($mvars[1] > 0) {
88
                        $add_to_url .= $mvars[1] . '/' . forum_seo_forum($mvars[1]) . '';
89
                    } else {
90
                        $add_to_url .= $mvars[1] . '/rss.html';
91
                    }
92
                    $req_string = preg_replace('/f=\d+/', '', $matches[6]);
93
                } else {
94
                    return $matches['0'];
95
                }
96
                //$add_to_url .= 'rss-feed.html';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
            }
98
            break;
99
        case 'viewforum.php':
100
            $add_to_url = '';
101
            $req_string = $matches[6];
102
            if (!empty($matches[6])) {
103
                //                replacing forum=x
104
                if (preg_match('/forum=(\d+)/', $matches[6], $mvars)) {
105
                    $add_to_url = 'f-' . $mvars[1] . '/' . forum_seo_forum($mvars[1]) . '';
106
                    $req_string = preg_replace('/forum=\d+/', '', $matches[6]);
107
                } else {
108
                    return $matches['0'];
109
                }
110
            }
111
            break;
112
        case 'viewtopic.php':
113
            $add_to_url = '';
114
            $req_string = $matches[6];
115
            if (!empty($matches[6])) {
116
                //                replacing topic_id=x
117
                if (preg_match('/topic_id=(\d+)/', $matches[6], $mvars)) {
118
                    $add_to_url = 't-' . $mvars[1] . '/' . forum_seo_topic($mvars[1]) . '';
119
                    $req_string = preg_replace('/topic_id=\d+/', '', $matches[6]);
120
                } //replacing post_id=x
121
                elseif (preg_match('/post_id=(\d+)/', $matches[6], $mvars)) {
122
                    $add_to_url = 'p-' . $mvars[1] . '/' . forum_seo_post($mvars[1]) . '';
123
                    $req_string = preg_replace('/post_id=\d+/', '', $matches[6]);
124
                } else {
125
                    return $matches['0'];
126
                }
127
            }
128
            break;
129
        case 'print.php':
130
            $add_to_url = '';
131
            $req_string = $matches[6];
132
            if (!empty($matches[6])) {
133
                //                replacing topic_id=x
134
                if (preg_match('/topic_id=(\d+)/', $matches[6], $mvars)) {
135
                    $add_to_url = 'pr-' . $mvars[1] . '/' . forum_seo_topic($mvars[1]) . '';
136
                    $req_string = preg_replace('/topic_id=\d+/', '', $matches[6]);
137
                } //replacing post_id=x
138
                elseif (preg_match('/post_id=(\d+)/', $matches[6], $mvars)) {
139
                    $add_to_url = 'pr-' . $mvars[1] . '/' . forum_seo_post($mvars[1]) . '';
140
                    $req_string = preg_replace('/post_id=\d+/', '', $matches[6]);
141
                } else {
142
                    return $matches['0'];
143
                }
144
            }
145
            break;
146
        case 'makepdf.php':
147
            $add_to_url = '';
148
            $req_string = $matches[6];
149
            if (!empty($matches[6])) {
150
                //                replacing topic_id=x
151
                if (preg_match('/topic_id=(\d+)/', $matches[6], $mvars)) {
152
                    $add_to_url = 'pdf-' . $mvars[1] . '/' . forum_seo_topic($mvars[1]) . '';
153
                    $req_string = preg_replace('/topic_id=\d+/', '', $matches[6]);
154
                } //replacing post_id=x
155
                elseif (preg_match('/post_id=(\d+)/', $matches[6], $mvars)) {
156
                    $add_to_url = 'pdf-' . $mvars[1] . '/' . forum_seo_post($mvars[1]) . '';
157
                    $req_string = preg_replace('/post_id=\d+/', '', $matches[6]);
158
                } else {
159
                    return $matches['0'];
160
                }
161
            }
162
            break;
163
        default:
164
            $req_string = $matches[6];
165
            $add_to_url = $matches[5];
166
            //if ($add_to_url === '') $add_to_url ='index.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167
            break;
168
    }
169
    if ('?' === $req_string) {
170
        $req_string = '';
171
    }
172
    $ret = '<' . $matches[1] . $matches[2] . $matches[3] . '=' . $matches[4] . XOOPS_URL . '/' . SEO_MODULE_NAME . '/' . $add_to_url . $req_string . $matches[7] . $matches[8] . '>';
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
173
174
    //$ret = '<'.$matches[1].$matches[2].$matches[3].'='.$matches[4].XOOPS_URL.'/'.REAL_MODULE_NAME.'/'.$add_to_url.$req_string.$matches[7].$matches[8].'>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
175
    return $ret;
176
}
177
178
/**
179
 * @param $_cat_id
180
 * @return bool|mixed
181
 */
182
function forum_seo_cat($_cat_id)
183
{
184
    xoops_load('XoopsCache');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

184
    /** @scrutinizer ignore-call */ 
185
    xoops_load('XoopsCache');
Loading history...
185
    $key = 'newbb_seo_cat';
186
    $ret = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
187
    if ($ret = \XoopsCache::read($key)) {
0 ignored issues
show
Bug introduced by
The type XoopsCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
188
        $ret = @$ret[$_cat_id];
189
        if ($ret) {
190
            return $ret;
191
        }
192
    }
193
    $query  = 'SELECT cat_id, cat_title FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_categories');
194
    $result = $GLOBALS['xoopsDB']->query($query);
195
    $_ret   = [];
196
    while (false !== ($res = $GLOBALS['xoopsDB']->fetchArray($result))) {
197
        $_ret[$res['cat_id']] = forum_seo_title($res['cat_title']);
198
    }
199
    XoopsCache::write($key, $_ret);
200
    $ret = \XoopsCache::read($key);
201
    $ret = $ret[$_cat_id];
202
203
    return $ret;
204
}
205
206
/**
207
 * @param $_cat_id
208
 * @return bool|mixed
209
 */
210
function forum_seo_forum($_cat_id)
211
{
212
    xoops_load('XoopsCache');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

212
    /** @scrutinizer ignore-call */ 
213
    xoops_load('XoopsCache');
Loading history...
213
    $key = 'newbb_seo_forum';
214
    $ret = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
215
    if ($ret = \XoopsCache::read($key)) {
216
        $ret = @$ret[$_cat_id];
217
        if ($ret) {
218
            return $ret;
219
        }
220
    }
221
    $query  = 'SELECT forum_id, forum_name    FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_forums');
222
    $result = $GLOBALS['xoopsDB']->query($query);
223
    $_ret   = [];
224
    while (false !== ($res = $GLOBALS['xoopsDB']->fetchArray($result))) {
225
        $_ret[$res['forum_id']] = forum_seo_title($res['forum_name']);
226
    }
227
    XoopsCache::write($key, $_ret);
228
    $ret = \XoopsCache::read($key);
229
    $ret = $ret[$_cat_id];
230
231
    return $ret;
232
}
233
234
/**
235
 * @param $_cat_id
236
 * @return mixed|string
237
 */
238
function forum_seo_topic($_cat_id)
239
{
240
    $query  = 'SELECT    topic_title    FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_topics') . ' WHERE topic_id = ' . $_cat_id;
241
    $result = $GLOBALS['xoopsDB']->query($query);
242
    $res    = $GLOBALS['xoopsDB']->fetchArray($result);
243
    $ret    = forum_seo_title($res['topic_title']);
244
245
    $moduleDirName = basename(__DIR__);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
246
    /** @var Newbb\TopicHandler $topicsHandler */
247
    $topicsHandler = Newbb\Helper::getInstance()->getHandler('Topic');
248
    $criteria      = new \CriteriaCompo(new \Criteria('topic_id', $_cat_id, '='));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
249
    $fields        = ['topic_title'];
250
    $ret0          = $topicsHandler->getAll($criteria, $fields, false);
0 ignored issues
show
Unused Code introduced by
The assignment to $ret0 is dead and can be removed.
Loading history...
251
252
    return $ret;
253
}
254
255
/**
256
 * @param $_cat_id
257
 * @return mixed|string
258
 */
259
function forum_seo_post($_cat_id)
260
{
261
    $query  = 'SELECT    subject    FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_posts') . ' WHERE post_id = ' . $_cat_id;
262
    $result = $GLOBALS['xoopsDB']->query($query);
263
    $res    = $GLOBALS['xoopsDB']->fetchArray($result);
264
    $ret    = forum_seo_title($res['subject']);
265
266
    return $ret;
267
}
268
269
/**
270
 * @param  string $title
271
 * @param  bool   $withExt
272
 * @return mixed|string
273
 */
274
function forum_seo_title($title = '', $withExt = true)
275
{
276
    /**
277
     * if XOOPS ML is present, let's sanitize the title with the current language
278
     */
279
    $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
280
    if (method_exists($myts, 'formatForML')) {
281
        $title = $myts->formatForML($title);
282
    }
283
284
    // Transformation de la chaine en minuscule
285
    // Codage de la chaine afin d'�viter les erreurs 500 en cas de caract�res impr�vus
286
    $title = rawurlencode(strtolower($title));
287
288
    // Transformation des ponctuations
289
    //                 Tab     Space      !        "        #        %        &        '        (        )        ,        /        :        ;        <        =        >        ?        @        [        \        ]        ^        {        |        }        ~       .
290
    $pattern = [
291
        '/%09/',
292
        '/%20/',
293
        '/%21/',
294
        '/%22/',
295
        '/%23/',
296
        '/%25/',
297
        '/%26/',
298
        '/%27/',
299
        '/%28/',
300
        '/%29/',
301
        '/%2C/',
302
        '/%2F/',
303
        '/%3A/',
304
        '/%3B/',
305
        '/%3C/',
306
        '/%3D/',
307
        '/%3E/',
308
        '/%3F/',
309
        '/%40/',
310
        '/%5B/',
311
        '/%5C/',
312
        '/%5D/',
313
        '/%5E/',
314
        '/%7B/',
315
        '/%7C/',
316
        '/%7D/',
317
        '/%7E/',
318
        '/\./',
319
        '/%2A/'
320
    ];
321
    $rep_pat = [
322
        '-',
323
        '-',
324
        '',
325
        '',
326
        '',
327
        '-100',
328
        '',
329
        '-',
330
        '',
331
        '',
332
        '',
333
        '-',
334
        '',
335
        '',
336
        '',
337
        '-',
338
        '',
339
        '',
340
        '-at-',
341
        '',
342
        '-',
343
        '',
344
        '-',
345
        '',
346
        '-',
347
        '',
348
        '-',
349
        '',
350
        ''
351
    ];
352
    $title   = preg_replace($pattern, $rep_pat, $title);
353
354
    // Transformation des caractères accentués
355
    //                  è         é        ê         ë         ç         à         â         ä        î         ï        ù         ü         û         ô        ö
356
    $pattern = [
357
        '/%B0/',
358
        '/%E8/',
359
        '/%E9/',
360
        '/%EA/',
361
        '/%EB/',
362
        '/%E7/',
363
        '/%E0/',
364
        '/%E2/',
365
        '/%E4/',
366
        '/%EE/',
367
        '/%EF/',
368
        '/%F9/',
369
        '/%FC/',
370
        '/%FB/',
371
        '/%F4/',
372
        '/%F6/',
373
        '/%E3%BC/',
374
        '/%E3%96/',
375
        '/%E3%84/',
376
        '/%E3%9C/',
377
        '/%E3%FF/',
378
        '/%E3%B6/',
379
        '/%E3%A4/',
380
        '/%E3%9F/'
381
    ];
382
    $rep_pat = [
383
        '-',
384
        'e',
385
        'e',
386
        'e',
387
        'e',
388
        'c',
389
        'a',
390
        'a',
391
        'a',
392
        'i',
393
        'i',
394
        'u',
395
        'u',
396
        'u',
397
        'o',
398
        'o',
399
        'ue',
400
        'oe',
401
        'ae',
402
        'ue',
403
        'ss',
404
        'oe',
405
        'ae',
406
        'ss'
407
    ];
408
    $title   = preg_replace($pattern, $rep_pat, $title);
409
410
    /*$string = str_replace(' ', '-', $title);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
411
    $string = iconv('utf-8', 'ascii//translit', $string);
412
    $string = preg_replace('#[^a-z0-9\-\.]#si', '', $string);
413
    $title  = str_replace('\/','', $string);  */
414
415
    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 $var 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

415
    if (count(/** @scrutinizer ignore-type */ $title) > 0) {
Loading history...
416
        if ($withExt) {
417
            $title .= '.html';
418
        }
419
420
        return $title;
421
    } else {
422
        return '';
423
    }
424
}
425