Passed
Pull Request — master (#18)
by Michael
04:31
created

Utility::getHostByAddrWithCache()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.7998
1
<?php
2
3
namespace XoopsModules\Xoopspoll;
4
5
use XoopsModules\Xoopspoll;
6
use XoopsModules\Xoopspoll\Common;
7
use XoopsModules\Xoopspoll\Constants;
8
9
/**
10
 * Class Utility
11
 */
12
class Utility extends Common\SysUtility
13
{
14
    //--------------- Custom module methods -----------------------------
15
16
    /**
17
     * gets the name of the host located at a specific ip address
18
     * @param string $ipAddr the ip address of the host
19
     * @return string host name
20
     */
21
    public static function getHostByAddrWithCache(&$ipAddr)
22
    {
23
        static $ipArray = [];
24
        $retVal  = &$ipAddr;
25
        $options = ['flags' => \FILTER_FLAG_NO_PRIV_RANGE, \FILTER_FLAG_NO_RES_RANGE];
26
        if (\filter_var($ipAddr, \FILTER_VALIDATE_IP, $options)) {
27
            if (\array_key_exists($ipAddr, $ipArray)) {
28
                $retVal = $ipArray[$ipAddr];
29
            } else {
30
                $hostAddr = \gethostbyaddr($ipAddr);
31
                if ($hostAddr === $ipAddr) {
32
                    $retVal = &$ipAddr;
33
                } else {
34
                    $ipArray[$ipAddr] = \htmlspecialchars($hostAddr, \ENT_QUOTES | \ENT_HTML5);
35
                    $retVal           = $ipArray[$ipAddr];
36
                }
37
            }
38
        }
39
40
        return $retVal;
41
    }
42
43
    /**
44
     * Returns the global comment mode for this module
45
     * @static
46
     */
47
    public static function commentMode()
48
    {
49
        static $mConfig;
50
        if (!isset($mConfig)) {
51
            $mHandler = \xoops_getHandler('module');
52
            $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

52
            /** @scrutinizer ignore-call */ 
53
            $mod      = $mHandler->getByDirname('xoopspoll');
Loading history...
53
            $cHandler = \xoops_getHandler('config');
54
            $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

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