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