Passed
Pull Request — master (#70)
by Pierre-Henry
03:15
created

seo_url.php (8 issues)

Labels
Severity
1
<?php
2
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
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
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';
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';
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
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].'>';
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
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;
187
    if ($ret = \XoopsCache::read($key)) {
0 ignored issues
show
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
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;
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__);
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
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...
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);
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
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);
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) {
416
        if ($withExt) {
417
            $title .= '.html';
418
        }
419
420
        return $title;
421
    }
422
423
    return '';
424
}
425