1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Moduleinstaller\Common; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
Utility Class Definition |
7
|
|
|
|
8
|
|
|
You may not change or alter any portion of this comment or credits of |
9
|
|
|
supporting developers from this source code or any supporting source code |
10
|
|
|
which is considered copyrighted (c) material of the original comment or credit |
11
|
|
|
authors. |
12
|
|
|
|
13
|
|
|
This program is distributed in the hope that it will be useful, but |
14
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
20
|
|
|
* @copyright https://xoops.org 2000-2020 © XOOPS Project |
21
|
|
|
* @author ZySpec <[email protected]> |
22
|
|
|
* @author Mamba <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use XoopsModules\Moduleinstaller\{ |
27
|
|
|
Helper |
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class SysUtility |
32
|
|
|
*/ |
33
|
|
|
class SysUtility |
34
|
|
|
{ |
35
|
|
|
use VersionChecks; //checkVerXoops, checkVerPhp Traits |
|
|
|
|
36
|
|
|
use ServerStats; // getServerStats Trait |
37
|
|
|
use FilesManagement; // Files Management Trait |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
41
|
|
|
* www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
42
|
|
|
* www.cakephp.org |
43
|
|
|
* |
44
|
|
|
* @TODO: Refactor to consider HTML5 & void (self-closing) elements |
45
|
|
|
* @TODO: Consider using https://github.com/jlgrall/truncateHTML/blob/master/truncateHTML.php |
46
|
|
|
* |
47
|
|
|
* @param string $text String to truncate. |
48
|
|
|
* @param int|null $length Length of returned string, including ellipsis. |
49
|
|
|
* @param string|null $ending Ending to be appended to the trimmed string. |
50
|
|
|
* @param bool|null $exact If false, $text will not be cut mid-word |
51
|
|
|
* @param bool|null $considerHtml If true, HTML tags would be handled correctly |
52
|
|
|
* |
53
|
|
|
* @return string Trimmed string. |
54
|
|
|
*/ |
55
|
|
|
public static function truncateHtml( |
56
|
|
|
string $text, |
57
|
|
|
?int $length = null, |
58
|
|
|
?string $ending = null, |
59
|
|
|
?bool $exact = null, |
60
|
|
|
?bool $considerHtml = null |
61
|
|
|
): string { |
62
|
|
|
$length ??= 100; |
63
|
|
|
$ending ??= '...'; |
64
|
|
|
$exact ??= false; |
65
|
|
|
$considerHtml ??= true; |
66
|
|
|
$openTags = []; |
67
|
|
|
if ($considerHtml) { |
68
|
|
|
// if the plain text is shorter than the maximum length, return the whole text |
69
|
|
|
if (\mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
70
|
|
|
return $text; |
71
|
|
|
} |
72
|
|
|
// splits all html-tags to scanable lines |
73
|
|
|
\preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
74
|
|
|
$totalLength = (int)\mb_strlen($ending); |
75
|
|
|
//$openTags = []; |
76
|
|
|
$truncate = ''; |
77
|
|
|
foreach ($lines as $lineMatchings) { |
78
|
|
|
// if there is any html-tag in this line, handle it and add it (uncounted) to the output |
79
|
|
|
if (!empty($lineMatchings[1])) { |
80
|
|
|
// if it's an "empty element" with or without xhtml-conform closing slash |
81
|
|
|
if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $lineMatchings[1])) { |
82
|
|
|
// do nothing |
83
|
|
|
// if tag is a closing tag |
84
|
|
|
} elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/', $lineMatchings[1], $tagMatchings)) { |
85
|
|
|
// delete tag from $openTags list |
86
|
|
|
$pos = \array_search($tagMatchings[1], $openTags, true); |
87
|
|
|
if (false !== $pos) { |
88
|
|
|
unset($openTags[$pos]); |
89
|
|
|
} |
90
|
|
|
// if tag is an opening tag |
91
|
|
|
} elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $lineMatchings[1], $tagMatchings)) { |
92
|
|
|
// add tag to the beginning of $openTags list |
93
|
|
|
\array_unshift($openTags, \mb_strtolower($tagMatchings[1])); |
94
|
|
|
} |
95
|
|
|
// add html-tag to $truncate'd text |
96
|
|
|
$truncate .= $lineMatchings[1]; |
97
|
|
|
} |
98
|
|
|
// calculate the length of the plain text part of the line; handle entities as one character |
99
|
|
|
$contentLength = (int)\mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $lineMatchings[2])); |
100
|
|
|
if (($totalLength + $contentLength) > $length) { |
101
|
|
|
// the number of characters which are left |
102
|
|
|
$left = $length - $totalLength; |
103
|
|
|
$entitiesLength = 0; |
104
|
|
|
// search for html entities |
105
|
|
|
if (\preg_match_all('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', $lineMatchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
106
|
|
|
// calculate the real length of all entities in the legal range |
107
|
|
|
foreach ($entities[0] as $entity) { |
108
|
|
|
if ($left >= $entity[1] + 1 - $entitiesLength) { |
109
|
|
|
$left--; |
110
|
|
|
$entitiesLength += \mb_strlen($entity[0]); |
111
|
|
|
} else { |
112
|
|
|
// no more characters left |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
$truncate .= \mb_substr($lineMatchings[2], 0, $left + $entitiesLength); |
118
|
|
|
// maximum length is reached, so get off the loop |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
$truncate .= $lineMatchings[2]; |
122
|
|
|
$totalLength += $contentLength; |
123
|
|
|
|
124
|
|
|
// if the maximum length is reached, get off the loop |
125
|
|
|
if ($totalLength >= $length) { |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
if (\mb_strlen($text) <= $length) { |
131
|
|
|
return $text; |
132
|
|
|
} |
133
|
|
|
$truncate = \mb_substr($text, 0, $length - \mb_strlen($ending)); |
134
|
|
|
} |
135
|
|
|
// if the words shouldn't be cut in the middle... |
136
|
|
|
if (!$exact) { |
137
|
|
|
// ...search the last occurance of a space... |
138
|
|
|
$spacepos = \mb_strrpos($truncate, ' '); |
139
|
|
|
if (isset($spacepos)) { |
140
|
|
|
// ...and cut the text in this position |
141
|
|
|
$truncate = \mb_substr($truncate, 0, $spacepos); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
// add the defined ending to the text |
145
|
|
|
$truncate .= $ending; |
146
|
|
|
if ($considerHtml) { |
147
|
|
|
// close all unclosed html-tags |
148
|
|
|
foreach ($openTags as $tag) { |
149
|
|
|
$truncate .= '</' . $tag . '>'; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $truncate; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param \XoopsModules\Moduleinstaller\Helper|null $helper |
158
|
|
|
* @param array|null $options |
159
|
|
|
* @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
160
|
|
|
*/ |
161
|
|
|
public static function getEditor(?Helper $helper = null, ?array $options = null) |
162
|
|
|
{ |
163
|
|
|
/** @var Helper $helper */ |
164
|
|
|
if (null === $options) { |
165
|
|
|
$options = []; |
166
|
|
|
$options['name'] = 'Editor'; |
167
|
|
|
$options['value'] = 'Editor'; |
168
|
|
|
$options['rows'] = 10; |
169
|
|
|
$options['cols'] = '100%'; |
170
|
|
|
$options['width'] = '100%'; |
171
|
|
|
$options['height'] = '400px'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (null === $helper) { |
175
|
|
|
$helper = Helper::getInstance(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$isAdmin = $helper->isUserAdmin(); |
179
|
|
|
|
180
|
|
|
if (\class_exists('XoopsFormEditor')) { |
181
|
|
|
if ($isAdmin) { |
182
|
|
|
$descEditor = new \XoopsFormEditor(\ucfirst((string) $options['name']), $helper->getConfig('editorAdmin'), $options, $nohtml = false, $onfailure = 'textarea'); |
183
|
|
|
} else { |
184
|
|
|
$descEditor = new \XoopsFormEditor(\ucfirst((string) $options['name']), $helper->getConfig('editorUser'), $options, $nohtml = false, $onfailure = 'textarea'); |
185
|
|
|
} |
186
|
|
|
} else { |
187
|
|
|
$descEditor = new \XoopsFormDhtmlTextArea(\ucfirst((string) $options['name']), $options['name'], $options['value']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// $form->addElement($descEditor); |
191
|
|
|
|
192
|
|
|
return $descEditor; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $fieldname |
197
|
|
|
* @param string $table |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public static function fieldExists(string $fieldname, string $table): bool |
202
|
|
|
{ |
203
|
|
|
global $xoopsDB; |
204
|
|
|
$sql ="SHOW COLUMNS FROM $table LIKE '$fieldname'"; |
205
|
|
|
$result = self::queryFAndCheck($xoopsDB, $sql); |
206
|
|
|
|
207
|
|
|
return ($xoopsDB->getRowsNum($result) > 0); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Clone a record in a dB |
212
|
|
|
* |
213
|
|
|
* @TODO need to exit more gracefully on error. Should throw/trigger error and then return false |
214
|
|
|
* |
215
|
|
|
* @param string $tableName name of dB table (without prefix) |
216
|
|
|
* @param string $idField name of field (column) in dB table |
217
|
|
|
* @param int $id item id to clone |
218
|
|
|
* @return int|null |
219
|
|
|
*/ |
220
|
|
|
public static function cloneRecord(string $tableName, string $idField, int $id): ?int |
221
|
|
|
{ |
222
|
|
|
$newId = null; |
223
|
|
|
$tempTable = []; |
224
|
|
|
$table = $GLOBALS['xoopsDB']->prefix($tableName); |
225
|
|
|
// copy content of the record you wish to clone |
226
|
|
|
$sql = "SELECT * FROM $table WHERE $idField='" . $id . "' "; |
227
|
|
|
$result = $GLOBALS['xoopsDB']->query($sql); |
228
|
|
|
if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
229
|
|
|
$tempTable = $GLOBALS['xoopsDB']->fetchArray($result, \MYSQLI_ASSOC); |
230
|
|
|
} |
231
|
|
|
if (!$tempTable) { |
232
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
233
|
|
|
} |
234
|
|
|
// set the auto-incremented id's value to blank. |
235
|
|
|
unset($tempTable[$idField]); |
236
|
|
|
// insert cloned copy of the original record |
237
|
|
|
$sql = "INSERT INTO $table (" . \implode(', ', \array_keys($tempTable)) . ") VALUES ('" . \implode("', '", $tempTable) . "')"; |
238
|
|
|
$result = $GLOBALS['xoopsDB']->queryF($sql); |
239
|
|
|
if ($result) { |
240
|
|
|
// Return the new id |
241
|
|
|
$newId = $GLOBALS['xoopsDB']->getInsertId(); |
242
|
|
|
} else { |
243
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
244
|
|
|
} |
245
|
|
|
return $newId; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Check if dB table exists |
250
|
|
|
* |
251
|
|
|
* @param string $tablename dB tablename with prefix |
252
|
|
|
* @return bool true if table exists |
253
|
|
|
*/ |
254
|
|
|
public static function tableExists(string $tablename): bool |
255
|
|
|
{ |
256
|
|
|
$ret = false; |
257
|
|
|
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
258
|
|
|
$GLOBALS['xoopsLogger']->addDeprecated( |
259
|
|
|
\basename(\dirname(__DIR__, 2)) . ' Module: ' . __FUNCTION__ . ' function is deprecated, please use Xmf\Database\Tables method(s) instead.' . " Called from {$trace[0]['file']} line {$trace[0]['line']}" |
260
|
|
|
); |
261
|
|
|
$sql = "SHOW TABLES LIKE '$tablename'"; |
262
|
|
|
$result = self::queryFAndCheck($GLOBALS['xoopsDB'], $sql); |
|
|
|
|
263
|
|
|
|
264
|
|
|
return $ret; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Query and check if the result is a valid result set |
269
|
|
|
* |
270
|
|
|
* @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
271
|
|
|
* @param string $sql a valid MySQL query |
272
|
|
|
* @param int $limit number of records to return |
273
|
|
|
* @param int $start offset of first record to return |
274
|
|
|
* |
275
|
|
|
* @return \mysqli_result query result |
276
|
|
|
*/ |
277
|
|
|
public static function queryAndCheck(\XoopsMySQLDatabase $xoopsDB, string $sql, $limit = 0, $start = 0): \mysqli_result |
278
|
|
|
{ |
279
|
|
|
$result = $xoopsDB->query($sql, $limit, $start); |
280
|
|
|
|
281
|
|
|
if (!$xoopsDB->isResultSet($result)) { |
282
|
|
|
throw new \RuntimeException( |
283
|
|
|
\sprintf(\_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), \E_USER_ERROR); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $result; |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* QueryF and check if the result is a valid result set |
291
|
|
|
* |
292
|
|
|
* @param \XoopsMySQLDatabase $xoopsDB XOOPS Database |
293
|
|
|
* @param string $sql a valid MySQL query |
294
|
|
|
* @param int $limit number of records to return |
295
|
|
|
* @param int $start offset of first record to return |
296
|
|
|
* |
297
|
|
|
* @return \mysqli_result query result |
298
|
|
|
*/ |
299
|
|
|
public static function queryFAndCheck(\XoopsMySQLDatabase $xoopsDB, string $sql, $limit = 0, $start = 0): \mysqli_result |
300
|
|
|
{ |
301
|
|
|
$result = $xoopsDB->queryF($sql, $limit, $start); |
302
|
|
|
|
303
|
|
|
if (!$xoopsDB->isResultSet($result)) { |
304
|
|
|
throw new \RuntimeException( |
305
|
|
|
\sprintf(\_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), \E_USER_ERROR |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $result; |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Create a safe Criteria object that handles empty values, different data types, |
314
|
|
|
* and various operators. |
315
|
|
|
* |
316
|
|
|
* @param string $column Database field name |
317
|
|
|
* @param mixed $values Array or scalar value(s) to be used in the criteria |
318
|
|
|
* @param string $operator SQL operator (e.g., 'IN', 'LIKE', '=', '>') |
319
|
|
|
* @param \Criteria|null $noMatchCriteria Optional criteria to use when no valid values are provided |
320
|
|
|
* @return \Criteria |
321
|
|
|
*/ |
322
|
|
|
public static function createSafeCriteria(string $column, $values, string $operator = 'IN', ?\Criteria $noMatchCriteria = null): \Criteria |
323
|
|
|
{ |
324
|
|
|
if (!empty($values)) { |
325
|
|
|
$values = (array)$values; |
326
|
|
|
|
327
|
|
|
// Filter out null, empty string, and false values |
328
|
|
|
$values = array_filter( |
329
|
|
|
$values, function ($value) { |
330
|
|
|
return $value !== null && $value !== '' && $value !== false; |
331
|
|
|
} |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
if (empty($values)) { |
335
|
|
|
// Return a criteria that always evaluates to false |
336
|
|
|
return $noMatchCriteria ?? new \Criteria($column, -1, '='); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
switch ($operator) { |
340
|
|
|
case 'IN': |
341
|
|
|
// Pass the array directly; Criteria handles it |
342
|
|
|
return new \Criteria($column, $values, 'IN'); |
|
|
|
|
343
|
|
|
case 'LIKE': |
344
|
|
|
$criteria = new \CriteriaCompo(); |
345
|
|
|
foreach ($values as $value) { |
346
|
|
|
$criteria->add(new \Criteria($column, '%' . $value . '%', 'LIKE'), 'OR'); |
347
|
|
|
} |
348
|
|
|
return $criteria; |
|
|
|
|
349
|
|
|
default: |
350
|
|
|
// For other operators, use the first value |
351
|
|
|
return new \Criteria($column, reset($values), $operator); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
// If values are empty, return a criteria that matches no records |
356
|
|
|
return $noMatchCriteria ?? new \Criteria($column, -1, '='); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|