1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace XoopsModules\Suico\Common; |
||
6 | |||
7 | /* |
||
8 | Utility Class Definition |
||
9 | You may not change or alter any portion of this comment or credits of |
||
10 | supporting developers from this source code or any supporting source code |
||
11 | which is considered copyrighted (c) material of the original comment or credit |
||
12 | authors. |
||
13 | |||
14 | This program is distributed in the hope that it will be useful, but |
||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | * @license https://www.fsf.org/copyleft/gpl.html GNU public license |
||
21 | * @copyright https://xoops.org 2000-2020 © XOOPS Project |
||
22 | * @author ZySpec <[email protected]> |
||
23 | * @author Mamba <[email protected]> |
||
24 | */ |
||
25 | |||
26 | use Xmf\Request; |
||
27 | use XoopsFormDhtmlTextArea; |
||
28 | use XoopsFormEditor; |
||
29 | use XoopsModules\Suico; |
||
30 | use XoopsModules\Suico\Helper; |
||
31 | |||
32 | /** |
||
33 | * Class SysUtility |
||
34 | */ |
||
35 | class SysUtility |
||
36 | { |
||
37 | use VersionChecks; |
||
38 | use ServerStats; |
||
39 | use FilesManagement; |
||
40 | |||
41 | // Files Management Trait |
||
42 | //--------------- Custom module methods ----------------------------- |
||
43 | /** |
||
44 | * @param $text |
||
45 | * @param $form_sort |
||
46 | * @return string |
||
47 | */ |
||
48 | public static function selectSorting($text, $form_sort) |
||
49 | { |
||
50 | global $start, $order, $file_cat, $sort, $xoopsModule; |
||
51 | $select_view = ''; |
||
52 | $moduleDirName = \basename(\dirname(__DIR__, 2)); |
||
53 | $helper = Helper::getInstance(); |
||
54 | // $pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getModule()->getInfo('modicons16'); |
||
55 | $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16')); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
56 | $select_view = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
57 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
58 | if ($form_sort == $sort) { |
||
59 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
60 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
61 | } else { |
||
62 | $sel1 = 'asc.png'; |
||
63 | $sel2 = 'desc.png'; |
||
64 | } |
||
65 | $select_view .= ' <a href="' . Request::getString('PHP_SELF', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
66 | $select_view .= '<a href="' . Request::getString('PHP_SELF', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
67 | $select_view .= '</form>'; |
||
68 | return $select_view; |
||
69 | } |
||
70 | |||
71 | /***************Blocks***************/ |
||
72 | /** |
||
73 | * @param array $cats |
||
74 | * @return string |
||
75 | */ |
||
76 | public static function blockAddCatSelect($cats) |
||
77 | { |
||
78 | $cat_sql = ''; |
||
79 | if (\is_array($cats) && !empty($cats)) { |
||
80 | $cat_sql = '(' . \current($cats); |
||
81 | \array_shift($cats); |
||
82 | foreach ($cats as $cat) { |
||
83 | $cat_sql .= ',' . $cat; |
||
84 | } |
||
85 | $cat_sql .= ')'; |
||
86 | } |
||
87 | return $cat_sql; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param $content |
||
92 | */ |
||
93 | public static function metaKeywords($content) |
||
94 | { |
||
95 | global $xoopsTpl, $xoTheme; |
||
96 | $myts = \MyTextSanitizer::getInstance(); |
||
97 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
98 | if (null !== $xoTheme && \is_object($xoTheme)) { |
||
99 | $xoTheme->addMeta('meta', 'keywords', \strip_tags($content)); |
||
100 | } else { // Compatibility for old Xoops versions |
||
101 | $xoopsTpl->assign('xoops_metaKeywords', \strip_tags($content)); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param $content |
||
107 | */ |
||
108 | public static function metaDescription($content) |
||
109 | { |
||
110 | global $xoopsTpl, $xoTheme; |
||
111 | $myts = \MyTextSanitizer::getInstance(); |
||
112 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
113 | if (null !== $xoTheme && \is_object($xoTheme)) { |
||
114 | $xoTheme->addMeta('meta', 'description', \strip_tags($content)); |
||
115 | } else { // Compatibility for old Xoops versions |
||
116 | $xoopsTpl->assign('xoops_metaDescription', \strip_tags($content)); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $tableName |
||
122 | * @param $columnName |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public static function enumerate($tableName, $columnName) |
||
127 | { |
||
128 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
129 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
130 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
131 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
132 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
133 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
134 | if (!$result) { |
||
135 | exit($GLOBALS['xoopsDB']->error()); |
||
136 | } |
||
137 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
138 | $enumList = \explode(',', \str_replace("'", '', \mb_substr($row['COLUMN_TYPE'], 5, -6))); |
||
139 | return $enumList; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param array|string $tableName |
||
144 | * @param int $id_field |
||
145 | * @param int $id |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public static function cloneRecord($tableName, $id_field, $id) |
||
150 | { |
||
151 | $new_id = false; |
||
152 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
153 | // copy content of the record you wish to clone |
||
154 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($GLOBALS['xoopsDB']->query("SELECT * FROM $table WHERE $id_field='$id' "), \MYSQLI_ASSOC) or exit('Could not select record'); |
||
155 | // set the auto-incremented id's value to blank. |
||
156 | unset($tempTable[$id_field]); |
||
157 | // insert cloned copy of the original record |
||
158 | $result = $GLOBALS['xoopsDB']->queryF("INSERT INTO $table (" . \implode(', ', \array_keys($tempTable)) . ") VALUES ('" . \implode("', '", $tempTable) . "')") or exit($GLOBALS['xoopsDB']->error()); |
||
159 | if ($result) { |
||
160 | // Return the new id |
||
161 | $new_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
162 | } |
||
163 | return $new_id; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
168 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
169 | * www.cakephp.org |
||
170 | * |
||
171 | * @param string $text String to truncate. |
||
172 | * @param int $length Length of returned string, including ellipsis. |
||
173 | * @param string $ending Ending to be appended to the trimmed string. |
||
174 | * @param bool $exact If false, $text will not be cut mid-word |
||
175 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
176 | * |
||
177 | * @return string Trimmed string. |
||
178 | */ |
||
179 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
180 | { |
||
181 | $openTags = []; |
||
182 | if ($considerHtml) { |
||
183 | // if the plain text is shorter than the maximum length, return the whole text |
||
184 | if (\mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
185 | return $text; |
||
186 | } |
||
187 | // splits all html-tags to scanable lines |
||
188 | \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
||
189 | $total_length = mb_strlen($ending); |
||
190 | //$openTags = []; |
||
191 | $truncate = ''; |
||
192 | foreach ($lines as $line_matchings) { |
||
193 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
194 | if (!empty($line_matchings[1])) { |
||
195 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
196 | if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
197 | // do nothing |
||
198 | // if tag is a closing tag |
||
199 | } elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
200 | // delete tag from $openTags list |
||
201 | $pos = \array_search($tag_matchings[1], $openTags); |
||
202 | if (false !== $pos) { |
||
203 | unset($openTags[$pos]); |
||
204 | } |
||
205 | // if tag is an opening tag |
||
206 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
207 | // add tag to the beginning of $openTags list |
||
208 | \array_unshift($openTags, \mb_strtolower($tag_matchings[1])); |
||
209 | } |
||
210 | // add html-tag to $truncate'd text |
||
211 | $truncate .= $line_matchings[1]; |
||
212 | } |
||
213 | // calculate the length of the plain text part of the line; handle entities as one character |
||
214 | $content_length = \mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
215 | if ($total_length + $content_length > $length) { |
||
216 | // the number of characters which are left |
||
217 | $left = $length - $total_length; |
||
218 | $entities_length = 0; |
||
219 | // search for html entities |
||
220 | if (\preg_match_all('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
||
221 | // calculate the real length of all entities in the legal range |
||
222 | foreach ($entities[0] as $entity) { |
||
223 | if ($left >= $entity[1] + 1 - $entities_length) { |
||
224 | $left--; |
||
225 | $entities_length += \mb_strlen($entity[0]); |
||
226 | } else { |
||
227 | // no more characters left |
||
228 | break; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | $truncate .= \mb_substr($line_matchings[2], 0, $left + $entities_length); |
||
233 | // maximum lenght is reached, so get off the loop |
||
234 | break; |
||
235 | } |
||
236 | $truncate .= $line_matchings[2]; |
||
237 | $total_length += $content_length; |
||
238 | // if the maximum length is reached, get off the loop |
||
239 | if ($total_length >= $length) { |
||
240 | break; |
||
241 | } |
||
242 | } |
||
243 | } elseif (\mb_strlen($text) <= $length) { |
||
244 | return $text; |
||
245 | } |
||
246 | $truncate = \mb_substr($text, 0, $length - \mb_strlen($ending)); |
||
247 | // if the words shouldn't be cut in the middle... |
||
248 | if (!$exact) { |
||
249 | // ...search the last occurance of a space... |
||
250 | $spacepos = \mb_strrpos($truncate, ' '); |
||
251 | if (isset($spacepos)) { |
||
252 | // ...and cut the text in this position |
||
253 | $truncate = \mb_substr($truncate, 0, $spacepos); |
||
254 | } |
||
255 | } |
||
256 | // add the defined ending to the text |
||
257 | $truncate .= $ending; |
||
258 | if ($considerHtml) { |
||
259 | // close all unclosed html-tags |
||
260 | foreach ($openTags as $tag) { |
||
261 | $truncate .= '</' . $tag . '>'; |
||
262 | } |
||
263 | } |
||
264 | return $truncate; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param \Xmf\Module\Helper $helper |
||
269 | * @param array|null $options |
||
270 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
271 | */ |
||
272 | public static function getEditor( |
||
273 | $helper = null, |
||
274 | $options = null |
||
275 | ) { |
||
276 | /** @var Helper $helper */ |
||
277 | if (null === $options) { |
||
278 | $options = []; |
||
279 | $options['name'] = 'Editor'; |
||
280 | $options['value'] = 'Editor'; |
||
281 | $options['rows'] = 10; |
||
282 | $options['cols'] = '100%'; |
||
283 | $options['width'] = '100%'; |
||
284 | $options['height'] = '400px'; |
||
285 | } |
||
286 | if (null === $helper) { |
||
287 | $helper = Helper::getInstance(); |
||
288 | } |
||
289 | $isAdmin = $helper->isUserAdmin(); |
||
290 | if (\class_exists('XoopsFormEditor')) { |
||
291 | if ($isAdmin) { |
||
292 | $descEditor = new XoopsFormEditor( |
||
293 | \ucfirst($options['name']), $helper->getConfig( |
||
294 | 'editorAdmin' |
||
295 | ), $options, $nohtml = false, $onfailure = 'textarea' |
||
296 | ); |
||
297 | } else { |
||
298 | $descEditor = new XoopsFormEditor( |
||
299 | \ucfirst($options['name']), $helper->getConfig( |
||
300 | 'editorUser' |
||
301 | ), $options, $nohtml = false, $onfailure = 'textarea' |
||
302 | ); |
||
303 | } |
||
304 | } else { |
||
305 | $descEditor = new XoopsFormDhtmlTextArea( |
||
306 | \ucfirst( |
||
307 | $options['name'] |
||
308 | ), $options['name'], $options['value'], '100%', '100%' |
||
309 | ); |
||
310 | } |
||
311 | // $form->addElement($descEditor); |
||
312 | return $descEditor; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @param $fieldname |
||
317 | * @param $table |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | public function fieldExists( |
||
322 | $fieldname, |
||
323 | $table |
||
324 | ) { |
||
325 | global $xoopsDB; |
||
326 | $result = $xoopsDB->queryF("SHOW COLUMNS FROM ${table} LIKE '${fieldname}'"); |
||
327 | return $xoopsDB->getRowsNum($result) > 0; |
||
328 | } |
||
329 | } |
||
330 |