1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xoopspoll; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Utility |
7
|
|
|
*/ |
8
|
|
|
class Utility extends Common\SysUtility |
9
|
|
|
{ |
10
|
|
|
//--------------- Custom module methods ----------------------------- |
11
|
|
|
/** |
12
|
|
|
* gets the name of the host located at a specific ip address |
13
|
|
|
* @param string $ipAddr the ip address of the host |
14
|
|
|
* @return string host name |
15
|
|
|
*/ |
16
|
|
|
public static function getHostByAddrWithCache(string &$ipAddr): string |
17
|
|
|
{ |
18
|
|
|
static $ipArray = []; |
19
|
|
|
$retVal = &$ipAddr; |
20
|
|
|
$options = ['flags' => \FILTER_FLAG_NO_PRIV_RANGE, \FILTER_FLAG_NO_RES_RANGE]; |
21
|
|
|
if (\filter_var($ipAddr, \FILTER_VALIDATE_IP, $options)) { |
22
|
|
|
if (\array_key_exists($ipAddr, $ipArray)) { |
23
|
|
|
$retVal = $ipArray[$ipAddr]; |
24
|
|
|
} else { |
25
|
|
|
$hostAddr = \gethostbyaddr($ipAddr); |
26
|
|
|
if ($hostAddr === $ipAddr) { |
27
|
|
|
$retVal = &$ipAddr; |
28
|
|
|
} else { |
29
|
|
|
$ipArray[$ipAddr] = \htmlspecialchars($hostAddr, \ENT_QUOTES | \ENT_HTML5); |
30
|
|
|
$retVal = $ipArray[$ipAddr]; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $retVal; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the global comment mode for this module |
40
|
|
|
* @static |
41
|
|
|
*/ |
42
|
|
|
public static function commentMode() |
43
|
|
|
{ |
44
|
|
|
static $mConfig; |
45
|
|
|
if (!isset($mConfig)) { |
46
|
|
|
$mHandler = \xoops_getHandler('module'); |
47
|
|
|
$mod = $mHandler->getByDirname('xoopspoll'); |
|
|
|
|
48
|
|
|
/** @var \XoopsConfigHandler $cHandler */ |
49
|
|
|
$cHandler = \xoops_getHandler('config'); |
50
|
|
|
$mConfig = &$cHandler->getConfigsByCat(0, $mod->getVar('mid')); |
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(): array |
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(string $cookieBaseName = 'voted_polls'): array |
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 bool|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, string $value, int $expires = 0, string $cookieBaseName = 'voted_polls'): bool |
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)) { |
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 string $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, string $tablename): bool |
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
|
|
|
|