XoopsModules25x /
xlanguage
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | |||
| 3 | namespace XoopsModules\Xlanguage\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 | * |
||
| 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 MyTextSanitizer; |
||
| 27 | use XoopsFormDhtmlTextArea; |
||
| 28 | use XoopsFormEditor; |
||
| 29 | use XoopsFormTextArea; |
||
| 30 | use XoopsModules\Xlanguage\{Helper |
||
| 31 | }; |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | /** @var Helper $helper */ |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Class SysUtility |
||
| 39 | */ |
||
| 40 | class SysUtility |
||
| 41 | { |
||
| 42 | use VersionChecks; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | //checkVerXoops, checkVerPhp Traits |
||
| 45 | |||
| 46 | use ServerStats; |
||
| 47 | |||
| 48 | // getServerStats Trait |
||
| 49 | |||
| 50 | use FilesManagement; |
||
| 51 | |||
| 52 | // Files Management Trait |
||
| 53 | |||
| 54 | /** |
||
| 55 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 56 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 57 | * www.cakephp.org |
||
| 58 | * |
||
| 59 | * @param string $text String to truncate. |
||
| 60 | * @param int $length Length of returned string, including ellipsis. |
||
| 61 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 62 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 63 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 64 | * |
||
| 65 | * @return string Trimmed string. |
||
| 66 | */ |
||
| 67 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 68 | { |
||
| 69 | $open_tags = []; |
||
| 70 | if ($considerHtml) { |
||
| 71 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 72 | if (mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 73 | return $text; |
||
| 74 | } |
||
| 75 | // splits all html-tags to scanable lines |
||
| 76 | \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
||
| 77 | $total_length = mb_strlen($ending); |
||
| 78 | $truncate = ''; |
||
| 79 | foreach ($lines as $line_matchings) { |
||
| 80 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 81 | if (!empty($line_matchings[1])) { |
||
| 82 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 83 | if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 84 | // do nothing |
||
| 85 | // if tag is a closing tag |
||
| 86 | } elseif (\preg_match('/^<\s*\/([^\S]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 87 | // delete tag from $open_tags list |
||
| 88 | $pos = \array_search($tag_matchings[1], $open_tags, true); |
||
| 89 | if (false !== $pos) { |
||
| 90 | unset($open_tags[$pos]); |
||
| 91 | } |
||
| 92 | // if tag is an opening tag |
||
| 93 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 94 | // add tag to the beginning of $open_tags list |
||
| 95 | \array_unshift($open_tags, mb_strtolower($tag_matchings[1])); |
||
| 96 | } |
||
| 97 | // add html-tag to $truncate'd text |
||
| 98 | $truncate .= $line_matchings[1]; |
||
| 99 | } |
||
| 100 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 101 | $content_length = mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 102 | if ($total_length + $content_length > $length) { |
||
| 103 | // the number of characters which are left |
||
| 104 | $left = $length - $total_length; |
||
| 105 | $entities_length = 0; |
||
| 106 | // search for html entities |
||
| 107 | 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)) { |
||
| 108 | // calculate the real length of all entities in the legal range |
||
| 109 | foreach ($entities[0] as $entity) { |
||
| 110 | if ($left >= $entity[1] + 1 - $entities_length) { |
||
| 111 | $left--; |
||
| 112 | $entities_length += mb_strlen($entity[0]); |
||
| 113 | } else { |
||
| 114 | // no more characters left |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | $truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length); |
||
| 120 | // maximum lenght is reached, so get off the loop |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | $truncate .= $line_matchings[2]; |
||
| 124 | $total_length += $content_length; |
||
| 125 | |||
| 126 | // if the maximum length is reached, get off the loop |
||
| 127 | if ($total_length >= $length) { |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | if (mb_strlen($text) <= $length) { |
||
| 133 | return $text; |
||
| 134 | } |
||
| 135 | $truncate = mb_substr($text, 0, $length - mb_strlen($ending)); |
||
| 136 | } |
||
| 137 | // if the words shouldn't be cut in the middle... |
||
| 138 | if (!$exact) { |
||
| 139 | // ...search the last occurance of a space... |
||
| 140 | $spacepos = mb_strrpos($truncate, ' '); |
||
| 141 | if (isset($spacepos)) { |
||
| 142 | // ...and cut the text in this position |
||
| 143 | $truncate = mb_substr($truncate, 0, $spacepos); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | // add the defined ending to the text |
||
| 147 | $truncate .= $ending; |
||
| 148 | if ($considerHtml) { |
||
| 149 | // close all unclosed html-tags |
||
| 150 | foreach ($open_tags as $tag) { |
||
| 151 | $truncate .= '</' . $tag . '>'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $truncate; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param null|Helper $helper |
||
| 160 | * @param array|null $options |
||
| 161 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
| 162 | */ |
||
| 163 | public static function getEditor($helper = null, $options = null) |
||
| 164 | { |
||
| 165 | if (null === $options) { |
||
| 166 | $options = []; |
||
| 167 | $options['name'] = 'Editor'; |
||
| 168 | $options['value'] = 'Editor'; |
||
| 169 | $options['rows'] = 10; |
||
| 170 | $options['cols'] = '100%'; |
||
| 171 | $options['width'] = '100%'; |
||
| 172 | $options['height'] = '400px'; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (null === $helper) { |
||
| 176 | $helper = Helper::getInstance(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $isAdmin = $helper->isUserAdmin(); |
||
| 180 | |||
| 181 | if (\class_exists('XoopsFormEditor')) { |
||
| 182 | if ($isAdmin) { |
||
| 183 | $descEditor = new XoopsFormEditor(\ucfirst($options['name']), $helper->getConfig('editorAdmin'), $options, $nohtml = false, $onfailure = 'textarea'); |
||
| 184 | } else { |
||
| 185 | $descEditor = new XoopsFormEditor(\ucfirst($options['name']), $helper->getConfig('editorUser'), $options, $nohtml = false, $onfailure = 'textarea'); |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | $descEditor = new \XoopsFormDhtmlTextArea(\ucfirst($options['name']), $options['name'], $options['value'], 5, 50); |
||
| 189 | } |
||
| 190 | |||
| 191 | // $form->addElement($descEditor); |
||
| 192 | |||
| 193 | return $descEditor; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $fieldname |
||
| 198 | * @param $table |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public static function fieldExists($fieldname, $table) |
||
| 203 | { |
||
| 204 | global $xoopsDB; |
||
| 205 | $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
||
| 206 | |||
| 207 | return ($xoopsDB->getRowsNum($result) > 0); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array|string $tableName |
||
| 212 | * @param int $id_field |
||
| 213 | * @param int $id |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public static function cloneRecord($tableName, $id_field, $id) |
||
| 218 | { |
||
| 219 | $new_id = false; |
||
|
0 ignored issues
–
show
|
|||
| 220 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 221 | // copy content of the record you wish to clone |
||
| 222 | $sql = "SELECT * FROM $table WHERE $id_field='$id' "; |
||
| 223 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($GLOBALS['xoopsDB']->query($sql), \MYSQLI_ASSOC); |
||
| 224 | if (!$tempTable) { |
||
| 225 | exit($GLOBALS['xoopsDB']->error()); |
||
|
0 ignored issues
–
show
|
|||
| 226 | } |
||
| 227 | // set the auto-incremented id's value to blank. |
||
| 228 | unset($tempTable[$id_field]); |
||
| 229 | // insert cloned copy of the original record |
||
| 230 | $sql = "INSERT INTO $table (" . \implode(', ', \array_keys($tempTable)) . ") VALUES ('" . \implode("', '", $tempTable) . "')"; |
||
| 231 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 232 | if (!$result) { |
||
| 233 | exit($GLOBALS['xoopsDB']->error()); |
||
|
0 ignored issues
–
show
|
|||
| 234 | } |
||
| 235 | // Return the new id |
||
| 236 | $new_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 237 | |||
| 238 | return $new_id; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $tablename |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public static function tableExists($tablename) |
||
| 247 | { |
||
| 248 | $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'"); |
||
| 249 | |||
| 250 | return $GLOBALS['xoopsDB']->getRowsNum($result) > 0; |
||
| 251 | } |
||
| 252 | } |
||
| 253 |