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
|
|
|
use XoopsFormEditor; |
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($options['name']), $helper->getConfig('editorAdmin'), $options, $nohtml = false, $onfailure = 'textarea'); |
183
|
|
|
} else { |
184
|
|
|
$descEditor = new XoopsFormEditor(\ucfirst($options['name']), $helper->getConfig('editorUser'), $options, $nohtml = false, $onfailure = 'textarea'); |
185
|
|
|
} |
186
|
|
|
} else { |
187
|
|
|
$descEditor = new \XoopsFormDhtmlTextArea(\ucfirst($options['name']), $options['name'], $options['value'], '100%', '100%'); |
|
|
|
|
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
|
|
|
$result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
205
|
|
|
|
206
|
|
|
return ($xoopsDB->getRowsNum($result) > 0); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Clone a record in a dB |
211
|
|
|
* |
212
|
|
|
* @TODO need to exit more gracefully on error. Should throw/trigger error and then return false |
213
|
|
|
* |
214
|
|
|
* @param string $tableName name of dB table (without prefix) |
215
|
|
|
* @param string $idField name of field (column) in dB table |
216
|
|
|
* @param int $id item id to clone |
217
|
|
|
* @return int|null |
218
|
|
|
*/ |
219
|
|
|
public static function cloneRecord(string $tableName, string $idField, int $id): ?int |
220
|
|
|
{ |
221
|
|
|
$newId = null; |
222
|
|
|
$tempTable = []; |
223
|
|
|
$table = $GLOBALS['xoopsDB']->prefix($tableName); |
224
|
|
|
// copy content of the record you wish to clone |
225
|
|
|
$sql = "SELECT * FROM $table WHERE $idField='" . $id . "' "; |
226
|
|
|
$result = $GLOBALS['xoopsDB']->query($sql); |
227
|
|
|
if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
228
|
|
|
$tempTable = $GLOBALS['xoopsDB']->fetchArray($result, \MYSQLI_ASSOC); |
229
|
|
|
} |
230
|
|
|
if (!$tempTable) { |
231
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
232
|
|
|
} |
233
|
|
|
// set the auto-incremented id's value to blank. |
234
|
|
|
unset($tempTable[$idField]); |
235
|
|
|
// insert cloned copy of the original record |
236
|
|
|
$sql = "INSERT INTO $table (" . \implode(', ', \array_keys($tempTable)) . ") VALUES ('" . \implode("', '", $tempTable) . "')"; |
237
|
|
|
$result = $GLOBALS['xoopsDB']->queryF($sql); |
238
|
|
|
if ($result) { |
239
|
|
|
// Return the new id |
240
|
|
|
$newId = $GLOBALS['xoopsDB']->getInsertId(); |
241
|
|
|
} else { |
242
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), \E_USER_ERROR); |
243
|
|
|
} |
244
|
|
|
return $newId; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Check if dB table exists |
249
|
|
|
* |
250
|
|
|
* @param string $tablename dB tablename with prefix |
251
|
|
|
* @return bool true if table exists |
252
|
|
|
*/ |
253
|
|
|
public static function tableExists(string $tablename): bool |
254
|
|
|
{ |
255
|
|
|
$ret = false; |
256
|
|
|
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
257
|
|
|
\trigger_error(__FUNCTION__ . " is deprecated, called from {$trace[0]['file']} line {$trace[0]['line']}"); |
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
|
|
|
$result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'"); |
262
|
|
|
|
263
|
|
|
if ($GLOBALS['xoopsDB']->isResultSet($result)) { |
264
|
|
|
$ret = $GLOBALS['xoopsDB']->getRowsNum($result) > 0; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $ret; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|