Passed
Pull Request — master (#18)
by Michael
02:44
created

Utility   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 163
rs 10
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHostByAddrWithCache() 0 20 4
A commentMode() 0 11 2
A getVoteCookie() 0 10 4
A getVisibilityArray() 0 17 2
B setVoteCookie() 0 32 10
A dbTableExists() 0 8 1
1
<?php
2
3
namespace XoopsModules\Xoopspoll;
4
5
/**
6
 * Class Utility
7
 */
8
class Utility extends Common\SysUtility
9
{
10
    //--------------- Custom module methods -----------------------------
11
12
    /**
13
     * gets the name of the host located at a specific ip address
14
     * @param string $ipAddr the ip address of the host
15
     * @return string host name
16
     */
17
    public static function getHostByAddrWithCache(&$ipAddr)
18
    {
19
        static $ipArray = [];
20
        $retVal  = &$ipAddr;
21
        $options = ['flags' => \FILTER_FLAG_NO_PRIV_RANGE, \FILTER_FLAG_NO_RES_RANGE];
22
        if (\filter_var($ipAddr, \FILTER_VALIDATE_IP, $options)) {
23
            if (\array_key_exists($ipAddr, $ipArray)) {
24
                $retVal = $ipArray[$ipAddr];
25
            } else {
26
                $hostAddr = \gethostbyaddr($ipAddr);
27
                if ($hostAddr === $ipAddr) {
28
                    $retVal = &$ipAddr;
29
                } else {
30
                    $ipArray[$ipAddr] = \htmlspecialchars($hostAddr, \ENT_QUOTES | \ENT_HTML5);
31
                    $retVal           = $ipArray[$ipAddr];
32
                }
33
            }
34
        }
35
36
        return $retVal;
37
    }
38
39
    /**
40
     * Returns the global comment mode for this module
41
     * @static
42
     */
43
    public static function commentMode()
44
    {
45
        static $mConfig;
46
        if (!isset($mConfig)) {
47
            $mHandler = \xoops_getHandler('module');
48
            $mod      = $mHandler->getByDirname('xoopspoll');
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

48
            /** @scrutinizer ignore-call */ 
49
            $mod      = $mHandler->getByDirname('xoopspoll');
Loading history...
49
            $cHandler = \xoops_getHandler('config');
50
            $mConfig  = &$cHandler->getConfigsByCat(0, $mod->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

50
            $mConfig  = &$cHandler->/** @scrutinizer ignore-call */ getConfigsByCat(0, $mod->getVar('mid'));
Loading history...
51
        }
52
53
        return $mConfig['com_rule'];
54
    }
55
56
    /**
57
     * Creates a visibility array from module default values
58
     * @return array visibility options available for a poll
59
     */
60
    public static function getVisibilityArray()
61
    {
62
        /**
63
         * {@internal Do NOT add/delete from $visOptions after the module has been installed}
64
         */
65
        static $visOptions = [];
66
        if (empty($visOptions)) {
67
            \xoops_loadLanguage('main', 'xoopspoll');
68
            $visOptions = [
69
                Constants::HIDE_NEVER  => \_MD_XOOPSPOLL_HIDE_NEVER,
70
                Constants::HIDE_END    => \_MD_XOOPSPOLL_HIDE_END,
71
                Constants::HIDE_VOTED  => \_MD_XOOPSPOLL_HIDE_VOTED,
72
                Constants::HIDE_ALWAYS => \_MD_XOOPSPOLL_HIDE_ALWAYS,
73
            ];
74
        }
75
76
        return $visOptions;
77
    }
78
79
    /**
80
     * Retrieves vote cookie from client system
81
     * The cookie name is based on the module directory. If module is
82
     * installed in default location then cookie name is 'voted_polls'
83
     * for backward compatibility. Otherwise cookie is named
84
     * '<dirname>_voted_polls' to allow for module to be cloned using
85
     * smartClone module.
86
     * @param string $cookieBaseName
87
     * @return array  contains cookie for polls, empty array if not found
88
     */
89
    public static function getVoteCookie($cookieBaseName = 'voted_polls')
90
    {
91
        $pollDir = \basename(\dirname(__DIR__));
92
        if ('xoopspoll' === $pollDir) {
93
            $pollCookie = !empty($_COOKIE[$cookieBaseName]) ? $_COOKIE[$cookieBaseName] : [];
94
        } else {
95
            $pollCookie = !empty($_COOKIE["{$pollDir}_{$cookieBaseName}"]) ? $_COOKIE["{$pollDir}_{$cookieBaseName}"] : [];
96
        }
97
98
        return $pollCookie;
99
    }
100
101
    /**
102
     * Sets vote cookie on client system
103
     * The cookie name is based on the module directory. If module is
104
     * installed in default location then cookie name is 'voted_polls'
105
     * for backward compatibility. Otherwise cookie is named
106
     * '<dirname>_voted_polls' to allow for module to be cloned using
107
     * smartClone module.
108
     * @param int|string $index          array index to set in cookie
109
     * @param string     $value          data to store in cookie
110
     * @param int        $expires        time when cookie expires
111
     * @param string     $cookieBaseName name of cookie (without directory prefix)
112
     * @return bool         success in setting cookie
113
     */
114
    public static function setVoteCookie($index, $value, $expires = 0, $cookieBaseName = 'voted_polls')
115
    {
116
        $pollDir = \basename(\dirname(__DIR__));
117
        $success = false;
118
        // do a little sanity check on $index and $cookieBaseName
119
        if (!\is_bool($index) && !empty($cookieBaseName)) {
0 ignored issues
show
introduced by
The condition is_bool($index) is always false.
Loading history...
120
            if ('xoopspoll' === $pollDir) {
121
                $cookieName = $cookieBaseName;
122
            } else {
123
                $cookieName = $pollDir . '_' . $cookieBaseName;
124
            }
125
            $iVal = (string)$index;
126
            if (!empty($iVal)) {
127
                $success = setcookie($cookieName[$index], $value, (int)$expires);
128
                if ($success) {
129
                    $clientCookie = self::getVoteCookie();
130
                    if (!\array_key_exists($index, $clientCookie) || $clientCookie[$index] !== $value) {
131
                        $success = false;
132
                    }
133
                }
134
            } else {  //clear the cookie
135
                $success = setcookie($cookieName, '', (int)$expires);
136
                if ($success) {
137
                    $clientCookie = self::getVoteCookie();
138
                    if (!empty($clientCookie)) {
139
                        $success = false;
140
                    }
141
                }
142
            }
143
        }
144
145
        return $success;
146
    }
147
148
    /**
149
     * Sets vote cookie on client system
150
     * The cookie name is based on the module directory. If module is
151
     * installed in default location then cookie name is 'voted_polls'
152
     * for backward compatibility. Otherwise cookie is named
153
     * '<dirname>_voted_polls' to allow for module to be cloned using
154
     * smartClone module.
155
     * @param \XoopsDatabase|null $db
156
     * @param                     $tablename
157
     * @return bool success in setting cookie
158
     * @internal param int|string $index array index to set in cookie
159
     * @internal param unknown_type $value data to store in cookie
160
     * @internal param int $expires time when cookie expires
161
     * @internal param string $cookieBaseName name of cookie (without directory prefix)
162
     */
163
    public static function dbTableExists(\XoopsDatabase $db, $tablename)
164
    {
165
        $tablename = \addslashes($tablename);
166
        $mytable   = $db->prefix((string)$tablename);
167
        $result    = $db->queryF("SHOW TABLES LIKE '{$mytable}'");
168
        $found     = $db->getRowsNum($result);
169
170
        return !empty($found);
171
    }
172
}
173