|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Tag; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Utility |
|
7
|
|
|
*/ |
|
8
|
|
|
class Utility extends Common\SysUtility |
|
9
|
|
|
{ |
|
10
|
|
|
//--------------- Custom module methods ----------------------------- |
|
11
|
|
|
/** |
|
12
|
|
|
* Load Tag module configs |
|
13
|
|
|
* |
|
14
|
|
|
* @return array|mixed|null |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function tag_load_config() |
|
17
|
|
|
{ |
|
18
|
|
|
static $moduleConfig; |
|
19
|
|
|
|
|
20
|
|
|
if (null === $moduleConfig) { |
|
21
|
|
|
$moduleConfig = []; |
|
22
|
|
|
$helper = \XoopsModules\Tag\Helper::getInstance(); |
|
23
|
|
|
if ($GLOBALS['xoopsModule'] instanceof \XoopsModule |
|
24
|
|
|
&& !empty($GLOBALS['xoopsModuleConfig']) |
|
25
|
|
|
&& ($helper->getDirname() === $GLOBALS['xoopsModule']->getVar('dirname', 'n'))) { |
|
26
|
|
|
$moduleConfig = $GLOBALS['xoopsModuleConfig']; |
|
27
|
|
|
} else { |
|
28
|
|
|
$mid = $helper->getModule()->getVar('mid'); |
|
29
|
|
|
/** @var \XoopsConfigHandler $configHandler */ |
|
30
|
|
|
$configHandler = \xoops_getHandler('config'); |
|
31
|
|
|
|
|
32
|
|
|
$criteria = new \Criteria('conf_modid', $mid); |
|
|
|
|
|
|
33
|
|
|
$configs = $configHandler->getConfigs($criteria); |
|
34
|
|
|
/** @var \XoopsConfigItem $obj */ |
|
35
|
|
|
foreach ($configs as $obj) { |
|
36
|
|
|
$moduleConfig[$obj->getVar('conf_name')] = $obj->getConfValueForOutput(); |
|
37
|
|
|
} |
|
38
|
|
|
unset($configs, $criteria); |
|
39
|
|
|
} |
|
40
|
|
|
if (\is_file($helper->path('include/plugin.php'))) { |
|
41
|
|
|
$customConfig = require $helper->path('include/plugin.php'); |
|
42
|
|
|
$moduleConfig = \array_merge($moduleConfig, $customConfig); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $moduleConfig; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Define URL_DELIMITER Constant |
|
51
|
|
|
* |
|
52
|
|
|
* return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function tag_define_url_delimiter(): void |
|
55
|
|
|
{ |
|
56
|
|
|
if (\defined('URL_DELIMITER')) { |
|
57
|
|
|
if (!\in_array(URL_DELIMITER, ['?', '/'], true)) { |
|
58
|
|
|
exit('Security Violation'); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
$moduleConfig = self::tag_load_config(); |
|
62
|
|
|
if (empty($moduleConfig['do_urw'])) { |
|
63
|
|
|
\define('URL_DELIMITER', '?'); |
|
64
|
|
|
} else { |
|
65
|
|
|
\define('URL_DELIMITER', '/'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the tag delimiter |
|
72
|
|
|
* |
|
73
|
|
|
* @return array|mixed |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function tag_get_delimiter() |
|
76
|
|
|
{ |
|
77
|
|
|
\XoopsModules\Tag\Helper::getInstance()->loadLanguage('config'); |
|
78
|
|
|
//xoops_loadLanguage('config', 'tag'); |
|
79
|
|
|
$retVal = [',', ' ', '|', ';']; |
|
80
|
|
|
|
|
81
|
|
|
if (!empty($GLOBALS['tag_delimiter'])) { |
|
82
|
|
|
$retVal = $GLOBALS['tag_delimiter']; |
|
83
|
|
|
} else { |
|
84
|
|
|
$moduleConfig = self::tag_load_config(); |
|
|
|
|
|
|
85
|
|
|
if (!empty($GLOBALS['moduleConfig']['tag_delimiter'])) { |
|
86
|
|
|
$retVal = $GLOBALS['moduleConfig']['tag_delimiter']; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $retVal; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Function to parse arguments for a page according to $_SERVER['REQUEST_URI'] |
|
95
|
|
|
* |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $args array of indexed variables: name and value pass-by-reference |
|
98
|
|
|
* @param mixed $args_string array of string variable values pass-by-reference |
|
99
|
|
|
* @return bool true on args parsed |
|
100
|
|
|
*/ |
|
101
|
|
|
|
|
102
|
|
|
/* known issues: |
|
103
|
|
|
* - "/" in a string |
|
104
|
|
|
* - "&" in a string |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function tag_parse_args(&$args, &$args_string): bool |
|
107
|
|
|
{ |
|
108
|
|
|
$args_abb = [ |
|
109
|
|
|
'c' => 'catid', |
|
110
|
|
|
'm' => 'modid', |
|
111
|
|
|
's' => 'start', |
|
112
|
|
|
't' => 'tag', |
|
113
|
|
|
]; |
|
114
|
|
|
$args = []; |
|
115
|
|
|
$args_string = []; |
|
116
|
|
|
if (\preg_match('/[^\?]*\.php[\/|\?]([^\?]*)/i', $_SERVER['REQUEST_URI'], $matches)) { |
|
117
|
|
|
$vars = \preg_split('/[\/|&]/', $matches[1]); |
|
118
|
|
|
$vars = \array_map('\trim', $vars); |
|
119
|
|
|
foreach ($vars as $var) { |
|
120
|
|
|
if (\is_numeric($var)) { |
|
121
|
|
|
$args_string[] = $var; |
|
122
|
|
|
} elseif (false !== mb_strpos($var, '=')) { |
|
123
|
|
|
\parse_str($var, $args); |
|
124
|
|
|
} elseif (\is_numeric(mb_substr($var, 1))) { |
|
125
|
|
|
$args[$args_abb[mb_strtolower($var[0])]] = (int)mb_substr($var, 1); |
|
126
|
|
|
} else { |
|
127
|
|
|
$args_string[] = \urldecode($var); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return !((0 == \count($args) + \count($args_string))); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Function to parse tags(keywords) upon defined delimiters |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $text_tag text to be parsed |
|
139
|
|
|
* |
|
140
|
|
|
* @return array containing parsed tags |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function tag_parse_tag(string $text_tag): array |
|
143
|
|
|
{ |
|
144
|
|
|
$tags = []; |
|
145
|
|
|
if (!empty($text_tag)) { |
|
146
|
|
|
$delimiters = self::tag_get_delimiter(); |
|
147
|
|
|
$tags_raw = \explode(',', \str_replace($delimiters, ',', $text_tag)); |
|
148
|
|
|
$tags = \array_filter(\array_map('\trim', $tags_raw)); // removes all array elements === false |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $tags; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|