referDetail()   F
last analyzed

Complexity

Conditions 19
Paths 264

Size

Total Lines 94
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 63
c 3
b 0
f 0
dl 0
loc 94
rs 2.8833
cc 19
nc 264
nop 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
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
use XoopsModules\Statistics;
21
22
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
23
24
/** @var Statistics\Helper $helper */
25
$helper = Statistics\Helper::getInstance();
26
$helper->loadLanguage('blocks');
27
28
$GLOBALS['xoopsOption']['template_main'] = 'referdetail.tpl';
29
require_once XOOPS_ROOT_PATH . '/header.php';
30
31
function referDetail()
32
{
33
    global $xoopsDB, $xoopsTpl;
34
    /** @var Statistics\Helper $helper */
35
    $helper = Statistics\Helper::getInstance();
36
37
    // get any current blacklist
38
    $result = $xoopsDB->queryF('SELECT * FROM ' . $xoopsDB->prefix('stats_refer_blacklist'));
39
    [$id, $referer] = $xoopsDB->fetchRow($result);
40
    $referblacklist = unserialize(stripslashes($referer));
41
    if (!is_array($referblacklist)) { // something went wrong, or there is no data...
42
        $referblacklist = [];
43
    }
44
45
    // figure out if we are saving hits by refer and path or just refer
46
    $resultoption = $xoopsDB->queryF('SELECT options FROM ' . $xoopsDB->prefix('newblocks') . " WHERE name='Top Referers' AND dirname='statistics'");
47
    $optionsret   = $xoopsDB->fetchRow($resultoption);
48
    $options      = explode('|', $optionsret[0]);
49
    if ('' == $options[1]) {
50
        $options[1] = 0;
51
    }
52
53
    $showselfrefer = $options[2];
54
55
    $result    = $xoopsDB->queryF('SELECT refer, date, hits, referpath FROM ' . $xoopsDB->prefix('stats_refer') . ' ORDER BY hits DESC');
56
    $referhits = [];
57
    $cnt       = 0;
58
    while (list($refer, $date, $hits, $referpath) = $xoopsDB->fetchRow($result)) {
59
        if ((0 == $showselfrefer && !strcmp($refer, $_SERVER['HTTP_HOST'])) || '' == $refer) {
60
            continue;
61
        }
62
        // see if we have a blacklist
63
        $blacklisted = false;
64
65
        // check configs if we are ignoring or allowing
66
        if ('Allow' !== $helper->getConfig('refererspam')) {
67
            if (count($referblacklist) > 0) {
68
                $rbldelimited = '/' . implode('|', $referblacklist) . '/';
69
                if (preg_match($rbldelimited, $refer)) {
70
                    $blacklisted = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $blacklisted is dead and can be removed.
Loading history...
71
                    continue;
72
                }
73
            }
74
        }
75
76
        if (false === $blacklisted) {
77
            $referpathparts = explode('|', $referpath);
78
            if (($howmany = count($referpathparts)) < 3) {
79
                switch ($howmany) {
80
                    case 0:
81
                        for ($i = 0; $i < 3; ++$i) {
82
                            $referpathparts[$i] = '';
83
                        }
84
                        break;
85
                    case 1:
86
                        for ($i = 1; $i < 3; ++$i) {
87
                            $referpathparts[$i] = '';
88
                        }
89
                        break;
90
                    case 2:
91
                        $referpathparts[2] = '';
92
                        break;
93
                }
94
            }
95
96
            $referhits[$cnt]['pathing']   = $options[1];
97
            $referhits[$cnt]['elipses']   = B_STATS_ELIPSES;
98
            $referhits[$cnt]['pathstrip'] = B_STATS_DELPATH;
99
            $referhits[$cnt]['pathdns']   = B_STATS_PATHDNS;
100
            $referhits[$cnt]['path']      = $referpathparts[0];
101
            $referhits[$cnt]['query']     = $referpathparts[1];
102
            $referhits[$cnt]['fragment']  = $referpathparts[2];
103
            $referhits[$cnt]['refer']     = $refer;
104
            $referhits[$cnt]['hits']      = '';
105
            preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})/', $date, $regs);
106
            $referhits[$cnt]['year']  = $regs[1];
107
            $referhits[$cnt]['month'] = $regs[2];
108
            $referhits[$cnt]['day']   = $regs[3];
109
            $referhits[$cnt]['hour']  = B_STATS_HOUR . '&nbsp;' . $regs[4];
110
            if (mb_strlen($hits) < 3) {
111
                $hits = sprintf('%03d', $hits);
112
            }
113
            for ($i = 0, $iMax = mb_strlen($hits); $i < $iMax; ++$i) {
114
                //the <img src> tag
115
                $imgsrc                  = mb_substr($hits, $i, 1);
116
                $referhits[$cnt]['hits'] .= '<img src="' . XOOPS_URL . '/modules/statistics/assets/images/' . $imgsrc . '.gif" border = "0">';
117
            }
118
119
            ++$cnt;
120
        }
121
    }
122
123
    $xoopsTpl->assign('lang_refer_counterhead', B_STATS_REFERHEAD);
124
    $xoopsTpl->assign('lang_refer_referhits', $referhits);
125
}
126
127
referDetail();
128
129
require_once XOOPS_ROOT_PATH . '/footer.php';
130