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

PollUtility   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHostByAddrWithCache() 0 20 4
A commentMode() 0 11 2
B setVoteCookie() 0 32 10
A getVisibilityArray() 0 17 2
A dbTableExists() 0 8 1
A getVoteCookie() 0 14 5
1
<?php
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 http://www.fsf.org/copyleft/gpl.html GNU public license}
21
 * @package   ::    xoopspoll
22
 * @subpackage:: class
23
 * @since     ::      1.40
24
 * @access::     public
25
 */
26
27
use XoopsModules\Xoopspoll;
28
29
 /**
30
 * Class PollUtility
31
 * @package XoopsModules\Xoopspoll
32
 */
33
class PollUtility
34
{
35
    /**
36
     * gets the name of the host located at a specific ip address
37
     * @param string $ipAddr the ip address of the host
38
     * @return string host name
39
     */
40
    public static function getHostByAddrWithCache(&$ipAddr)
41
    {
42
        static $ipArray = [];
43
        $retVal  = &$ipAddr;
44
        $options = ['flags' => \FILTER_FLAG_NO_PRIV_RANGE, \FILTER_FLAG_NO_RES_RANGE];
45
        if (\filter_var($ipAddr, \FILTER_VALIDATE_IP, $options)) {
46
            if (\array_key_exists($ipAddr, $ipArray)) {
47
                $retVal = $ipArray[$ipAddr];
48
            } else {
49
                $hostAddr = \gethostbyaddr($ipAddr);
50
                if ($hostAddr === $ipAddr) {
51
                    $retVal = &$ipAddr;
52
                } else {
53
                    $ipArray[$ipAddr] = \htmlspecialchars($hostAddr, \ENT_QUOTES | \ENT_HTML5);
54
                    $retVal           = $ipArray[$ipAddr];
55
                }
56
            }
57
        }
58
59
        return $retVal;
60
    }
61
62
    /**
63
     * Returns the global comment mode for this module
64
     * @static
65
     */
66
    public static function commentMode()
67
    {
68
        static $mConfig;
69
        if (null === $mConfig) {
70
            $mHandler = \xoops_getHandler('module');
71
            $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

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

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