| Total Complexity | 53 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SysUtility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SysUtility, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 37 | class SysUtility |
||
| 38 | { |
||
| 39 | use VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
|
|
|||
| 40 | use ServerStats; // getServerStats Trait |
||
| 41 | use FilesManagement; // Files Management Trait |
||
| 42 | |||
| 43 | //--------------- Common module methods ----------------------------- |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Access the only instance of this class |
||
| 47 | * |
||
| 48 | * @return SysUtility |
||
| 49 | * |
||
| 50 | */ |
||
| 51 | public static function getInstance() |
||
| 52 | { |
||
| 53 | static $instance; |
||
| 54 | if (null === $instance) { |
||
| 55 | $instance = new static(); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $instance; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $text |
||
| 63 | * @param $form_sort |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function selectSorting($text, $form_sort) |
||
| 67 | { |
||
| 68 | global $start, $order, $file_cat, $sort, $xoopsModule; |
||
| 69 | |||
| 70 | $select_view = ''; |
||
| 71 | $moduleDirName = basename(dirname(__DIR__)); |
||
| 72 | /** @var Helper $helper */ |
||
| 73 | $helper = Helper::getInstance(); |
||
| 74 | |||
| 75 | //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16'); |
||
| 76 | $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16')); |
||
| 77 | |||
| 78 | $select_view = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
| 79 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
| 80 | if ($form_sort == $sort) { |
||
| 81 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
| 82 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
| 83 | } else { |
||
| 84 | $sel1 = 'asc.png'; |
||
| 85 | $sel2 = 'desc.png'; |
||
| 86 | } |
||
| 87 | $select_view .= ' <a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
| 88 | $select_view .= '<a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
| 89 | $select_view .= '</form>'; |
||
| 90 | |||
| 91 | return $select_view; |
||
| 92 | } |
||
| 93 | |||
| 94 | /***************Blocks***************/ |
||
| 95 | /** |
||
| 96 | * @param array $cats |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public static function blockAddCatSelect($cats) |
||
| 100 | { |
||
| 101 | $cat_sql = ''; |
||
| 102 | if (is_array($cats) && !empty($cats)) { |
||
| 103 | $cat_sql = '(' . current($cats); |
||
| 104 | array_shift($cats); |
||
| 105 | foreach ($cats as $cat) { |
||
| 106 | $cat_sql .= ',' . $cat; |
||
| 107 | } |
||
| 108 | $cat_sql .= ')'; |
||
| 109 | } |
||
| 110 | |||
| 111 | return $cat_sql; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $content |
||
| 116 | */ |
||
| 117 | public static function metaKeywords($content) |
||
| 118 | { |
||
| 119 | global $xoopsTpl, $xoTheme; |
||
| 120 | $myts = \MyTextSanitizer::getInstance(); |
||
| 121 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 122 | if (null !== $xoTheme && is_object($xoTheme)) { |
||
| 123 | $xoTheme->addMeta('meta', 'keywords', strip_tags($content)); |
||
| 124 | } else { // Compatibility for old Xoops versions |
||
| 125 | $xoopsTpl->assign('xoops_metaKeywords', strip_tags($content)); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $content |
||
| 131 | */ |
||
| 132 | public static function metaDescription($content) |
||
| 133 | { |
||
| 134 | global $xoopsTpl, $xoTheme; |
||
| 135 | $myts = \MyTextSanitizer::getInstance(); |
||
| 136 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 137 | if (null !== $xoTheme && is_object($xoTheme)) { |
||
| 138 | $xoTheme->addMeta('meta', 'description', strip_tags($content)); |
||
| 139 | } else { // Compatibility for old Xoops versions |
||
| 140 | $xoopsTpl->assign('xoops_metaDescription', strip_tags($content)); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param $tableName |
||
| 146 | * @param $columnName |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public static function enumerate($tableName, $columnName) |
||
| 151 | { |
||
| 152 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 153 | |||
| 154 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
| 155 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
| 156 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
| 157 | |||
| 158 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
| 159 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 160 | if (!$result) { |
||
| 161 | exit($GLOBALS['xoopsDB']->error()); |
||
| 162 | } |
||
| 163 | |||
| 164 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
| 165 | $enumList = explode(',', str_replace("'", '', substr($row['COLUMN_TYPE'], 5, - 6))); |
||
| 166 | return $enumList; |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array|string $tableName |
||
| 172 | * @param int $id_field |
||
| 173 | * @param int $id |
||
| 174 | * |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | public static function cloneRecord($tableName, $id_field, $id) |
||
| 178 | { |
||
| 179 | $new_id = false; |
||
| 180 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 181 | // copy content of the record you wish to clone |
||
| 182 | $sql = "SELECT * FROM $table WHERE $id_field='$id' "; |
||
| 183 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($GLOBALS['xoopsDB']->query($sql), MYSQLI_ASSOC); |
||
| 184 | if (!$tempTable) { |
||
| 185 | exit($GLOBALS['xoopsDB']->error()); |
||
| 186 | } |
||
| 187 | // set the auto-incremented id's value to blank. |
||
| 188 | unset($tempTable[$id_field]); |
||
| 189 | // insert cloned copy of the original record |
||
| 190 | $sql = "INSERT INTO $table (" . implode(', ', array_keys($tempTable)) . ") VALUES ('" . implode("', '", array_values($tempTable)) . "')"; |
||
| 191 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 192 | if (!$result) { |
||
| 193 | exit($GLOBALS['xoopsDB']->error()); |
||
| 194 | } |
||
| 195 | // Return the new id |
||
| 196 | $new_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 197 | |||
| 198 | return $new_id; |
||
| 199 | } |
||
| 200 | /** |
||
| 201 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 202 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 203 | * www.cakephp.org |
||
| 204 | * |
||
| 205 | * @param string $text String to truncate. |
||
| 206 | * @param int $length Length of returned string, including ellipsis. |
||
| 207 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 208 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 209 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 210 | * |
||
| 211 | * @return string Trimmed string. |
||
| 212 | */ |
||
| 213 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 214 | { |
||
| 215 | $openTags = []; |
||
| 216 | if ($considerHtml) { |
||
| 217 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 218 | if (strlen(preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 219 | return $text; |
||
| 220 | } |
||
| 221 | // splits all html-tags to scanable lines |
||
| 222 | preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); |
||
| 223 | $total_length = strlen($ending); |
||
| 224 | //$openTags = []; |
||
| 225 | $truncate = ''; |
||
| 226 | foreach ($lines as $line_matchings) { |
||
| 227 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 228 | if (!empty($line_matchings[1])) { |
||
| 229 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 230 | if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 231 | // do nothing |
||
| 232 | // if tag is a closing tag |
||
| 233 | } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 234 | // delete tag from $openTags list |
||
| 235 | $pos = array_search($tag_matchings[1], $openTags); |
||
| 236 | if (false !== $pos) { |
||
| 237 | unset($openTags[$pos]); |
||
| 238 | } |
||
| 239 | // if tag is an opening tag |
||
| 240 | } elseif (preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 241 | // add tag to the beginning of $openTags list |
||
| 242 | array_unshift($openTags, strtolower($tag_matchings[1])); |
||
| 243 | } |
||
| 244 | // add html-tag to $truncate'd text |
||
| 245 | $truncate .= $line_matchings[1]; |
||
| 246 | } |
||
| 247 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 248 | $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 249 | if ($total_length + $content_length > $length) { |
||
| 250 | // the number of characters which are left |
||
| 251 | $left = $length - $total_length; |
||
| 252 | $entities_length = 0; |
||
| 253 | // search for html entities |
||
| 254 | if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { |
||
| 255 | // calculate the real length of all entities in the legal range |
||
| 256 | foreach ($entities[0] as $entity) { |
||
| 257 | if ($entity[1] + 1 - $entities_length <= $left) { |
||
| 258 | $left--; |
||
| 259 | $entities_length += strlen($entity[0]); |
||
| 260 | } else { |
||
| 261 | // no more characters left |
||
| 262 | break; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | $truncate .= substr($line_matchings[2], 0, $left + $entities_length); |
||
| 267 | // maximum lenght is reached, so get off the loop |
||
| 268 | break; |
||
| 269 | } else { |
||
| 270 | $truncate .= $line_matchings[2]; |
||
| 271 | $total_length += $content_length; |
||
| 272 | } |
||
| 273 | // if the maximum length is reached, get off the loop |
||
| 274 | if ($total_length >= $length) { |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } elseif (strlen($text) <= $length) { |
||
| 279 | return $text; |
||
| 280 | } else { |
||
| 281 | $truncate = substr($text, 0, $length - strlen($ending)); |
||
| 282 | } |
||
| 283 | |||
| 284 | // if the words shouldn't be cut in the middle... |
||
| 285 | if (!$exact) { |
||
| 286 | // ...search the last occurance of a space... |
||
| 287 | $spacepos = mb_strrpos($truncate, ' '); |
||
| 288 | if (isset($spacepos)) { |
||
| 289 | // ...and cut the text in this position |
||
| 290 | $truncate = substr($truncate, 0, $spacepos); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | // add the defined ending to the text |
||
| 294 | $truncate .= $ending; |
||
| 295 | if ($considerHtml) { |
||
| 296 | // close all unclosed html-tags |
||
| 297 | foreach ($openTags as $tag) { |
||
| 298 | $truncate .= '</' . $tag . '>'; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | return $truncate; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param \Xmf\Module\Helper $helper |
||
| 307 | * @param array|null $options |
||
| 308 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
| 309 | */ |
||
| 310 | public static function getEditor($helper = null, $options = null) |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $fieldname |
||
| 346 | * @param string $table |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public static function fieldExists($fieldname, $table) |
||
| 351 | { |
||
| 352 | global $xoopsDB; |
||
| 353 | $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
||
| 354 | |||
| 355 | return ($xoopsDB->getRowsNum($result) > 0); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 360 | * |
||
| 361 | * @param string $folder The full path of the directory to check |
||
| 362 | */ |
||
| 363 | public static function prepareFolder($folder) |
||
| 364 | { |
||
| 365 | try { |
||
| 366 | if (!@mkdir($folder) && !is_dir($folder)) { |
||
| 367 | throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder)); |
||
| 368 | } |
||
| 369 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 370 | } catch (\Exception $e) { |
||
| 371 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $tablename |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public static function tableExists($tablename) |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Add a field to a mysql table |
||
| 394 | * |
||
| 395 | * @param $field |
||
| 396 | * @param $table |
||
| 397 | * @return bool|\mysqli_result |
||
| 398 | */ |
||
| 399 | public static function addField($field, $table) |
||
| 405 | } |
||
| 406 | } |
||
| 407 |