PollUtility   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
c 0
b 0
f 0
dl 0
loc 166
rs 10
wmc 24

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setVoteCookie() 0 32 10
A getVisibilityArray() 0 17 2
A getHostByAddrWithCache() 0 20 4
A dbTableExists() 0 8 1
A getVoteCookie() 0 14 5
A commentMode() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xoopspoll;
4
5
/*
6
 Description: Poll Class Definition
7
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
*/
15
16
/**
17
 *  XoopsPoll Utility Class Elements
18
 *
19
 * @copyright ::  {@link https://xoops.org/ XOOPS Project}
20
 * @license   ::    {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later}
21
 * @subpackage:: class
22
 * @since     ::      1.40
23
 * @access::     public
24
 */
25
26
use XoopsModules\Xoopspoll;
27
28
/**
29
 * Class PollUtility
30
 */
31
class PollUtility
32
{
33
    /**
34
     * gets the name of the host located at a specific ip address
35
     * @param string $ipAddr the ip address of the host
36
     * @return string host name
37
     */
38
    public static function getHostByAddrWithCache(string &$ipAddr): string
39
    {
40
        static $ipArray = [];
41
        $retVal  = &$ipAddr;
42
        $options = ['flags' => \FILTER_FLAG_NO_PRIV_RANGE, \FILTER_FLAG_NO_RES_RANGE];
43
        if (\filter_var($ipAddr, \FILTER_VALIDATE_IP, $options)) {
44
            if (\array_key_exists($ipAddr, $ipArray)) {
45
                $retVal = $ipArray[$ipAddr];
46
            } else {
47
                $hostAddr = \gethostbyaddr($ipAddr);
48
                if ($hostAddr === $ipAddr) {
49
                    $retVal = &$ipAddr;
50
                } else {
51
                    $ipArray[$ipAddr] = \htmlspecialchars($hostAddr, \ENT_QUOTES | \ENT_HTML5);
52
                    $retVal           = $ipArray[$ipAddr];
53
                }
54
            }
55
        }
56
57
        return $retVal;
58
    }
59
60
    /**
61
     * Returns the global comment mode for this module
62
     * @static
63
     */
64
    public static function commentMode()
65
    {
66
        static $mConfig;
67
        if (null === $mConfig) {
68
            $mHandler = \xoops_getHandler('module');
69
            $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

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