$v) { if (is_string($v)) { $v = html_entity_decode($v, ENT_QUOTES | ENT_HTML5); $v = mb_decode_numericentity($v, [0x0, 0x10000, 0, 0xfffff], 'UTF-8'); $data[$k] = \Normalizer::normalize(trim($v), \Normalizer::FORM_C); } elseif (null === $v) { $data[$k] = ''; } } } public static function convertToInt(&$data, $keys, $zerofill = false) { foreach ($keys as $key) { if (isset($data[$key])) { $data[$key] = '' === $data[$key] ? ($zerofill ? 0 : null) : (int) $data[$key]; } } } public static function dropColumn(&$data, $keys) { foreach ($keys as $key) { if (isset($data[$key])) { unset($data[$key]); } } } public static function fileCopy($from, $to) { system('rsync -aq '.escapeshellarg($from).' '.escapeshellarg($to), $ret); return 0 === $ret; } public static function fixHtml($text) { static $config = [ //'clean' => true, 'hide-comments' => true, 'indent' => true, 'output-xhtml' => true, 'preserve-entities' => true, 'show-body-only' => true, 'drop-proprietary-attributes' => true, 'logical-emphasis' => true, 'wrap' => 0, 'input-encoding' => 'utf8', 'output-encoding' => 'utf8', 'output-bom' => false, ]; $text = tidy_repair_string($text, $config); $text = preg_replace('/]*)>((?:\s*]*>.*<\/caption>)?\s*)]*)>/Us', '\2', $text); $text = preg_replace('/<\/tr>(\s*)<\/table>/s', '\1', $text); $text = tidy_repair_string($text, $config); return $text; } public static function tableExists($name) { global $xoopsDB; $prefix = $xoopsDB->prefix(); $sql = <<< SQL SELECT 1 FROM `${prefix}_${name}` LIMIT 1 SQL; if (!($res = $xoopsDB->query($sql))) { return false; } $xoopsDB->freeRecordSet($res); return true; } public static function makeDirectory($dirname) { $path = MYDUMPTOOL_OUTPUTDIR.('' !== $dirname ? '/'.$dirname : ''); if (!is_dir($path)) { if (!@mkdir($path, 0755, true)) { exit('Failed to create directory: '.$path.PHP_EOL); } } } public static function saveJson($fname, $data) { $path = MYDUMPTOOL_OUTPUTDIR.'/'.$fname; $data = json_encode($data, JSON_UNESCAPED_UNICODE); if (false === $data) { echo json_last_error_msg().PHP_EOL; exit('Failed to encode json data: '.$path.PHP_EOL); } if (false === file_put_contents($path, $data)) { exit('Failed to save json file: '.$path.PHP_EOL); } } public static function fileDownload($url, $fpath) { $fp = fopen($fpath, 'w'); if (false === $fp) { exit('Failed to open file: '.$fpath.PHP_EOL); } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_AUTOREFERER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_TIMEOUT, 10); $ret = curl_exec($curl); fclose($fp); if (false === $ret) { unlink($fpath); echo 'Failed to download file, unexpected error: '.$url.PHP_EOL; return false; } $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if (200 !== $code) { unlink($fpath); echo 'Failed to download file, invalid status code('.$code.'): '.$url.PHP_EOL; return false; } curl_close($curl); return true; } public static function getMimeType($fpath) { $finfo = finfo_open(FILEINFO_MIME); $mime = finfo_file($finfo, $fpath); finfo_close($finfo); $mime = preg_replace('/(;.*)$/', '', $mime); return $mime; } public static function getModule($dirname) { $moduleHandler = xoops_gethandler('module'); $moduleObj = $moduleHandler->getByDirname($dirname); return [ 'id' => (int) $moduleObj->get('mid'), 'name' => $moduleObj->get('name'), 'dirname' => $moduleObj->get('dirname'), 'version' => (int) $moduleObj->get('version'), 'lastupdate' => (int) $moduleObj->get('last_update'), ]; } public static function canModuleRead($dirname, $uid) { $moduleHandler = xoops_gethandler('module'); $moduleObj = $moduleHandler->getByDirname($dirname); $moduleId = (int) $moduleObj->get('mid'); $memberHandler = xoops_gethandler('member'); $groupIds = 0 === $uid ? [XOOPS_GROUP_ANONYMOUS] : $memberHandler->getGroupsByUser($uid); $groupPermHandler = xoops_gethandler('groupperm'); $ret = $groupPermHandler->checkRight('module_read', $moduleId, $groupIds); return $ret; } public static function mlang($s, $url, $lang) { if (!defined('CUBE_UTILS_ML_LANGS')) { exit('Error: cubeUtils is not loaded'.PHP_EOL); } $mLanguage = $lang; $mLanguages = explode(',', CUBE_UTILS_ML_LANGS); $mLanguageNames = explode(',', CUBE_UTILS_ML_LANGNAMES); // escape brackets inside of $s = preg_replace_callback('/(\]*)(\>)/isU', 'self::mlang_escapeBracketTextBox', $s); // escape brackets inside of $s = preg_replace_callback('/(\]*\>)(.*)(<\/textarea\>)/isU', 'self::mlang_escapeBracket', $s); // multilanguage image tag $langimages = explode(',', CUBE_UTILS_ML_LANGIMAGES); $langnames = explode(',', CUBE_UTILS_ML_LANGNAMES); @list($url, $query) = explode('?', $url); if (empty($query)) { $link_base = '?'.CUBE_UTILS_ML_PARAM_NAME.'='; } elseif (false === ($pos = strpos($query, CUBE_UTILS_ML_PARAM_NAME.'='))) { $link_base = '?'.htmlspecialchars($query, ENT_QUOTES).'&'.CUBE_UTILS_ML_PARAM_NAME.'='; } elseif ($pos < 2) { $link_base = '?'.CUBE_UTILS_ML_PARAM_NAME.'='; } else { $link_base = '?'.htmlspecialchars(substr($query, 0, $pos - 1), ENT_QUOTES).'&'.CUBE_UTILS_ML_PARAM_NAME.'='; } $link_base = $url.$link_base; $langimage_html = ''; foreach ($mLanguages as $l => $lang) { $langimage_html .= 'flag'; } $s = preg_replace('/\['.CUBE_UTILS_ML_IMAGETAG.'\]/', $langimage_html, $s); $s = preg_replace('/\['.CUBE_UTILS_ML_URLTAG.':([^\]]*?)\]/', $link_base.'$1', $s); // simple pattern to strip selected lang_tags $s = preg_replace('/\[(\/)?([^\]]+\|)?'.preg_quote($mLanguage).'(\|[^\]]+)?\](\
)?/i', '', $s); // eliminate description between the other language tags. foreach ($mLanguages as $lang) { if ($mLanguage == $lang) { continue; } $s = preg_replace_callback('/\[(?:^\/[^\]]+\|)?'.preg_quote($lang).'(?:\|[^\]]+)?\].*\[\/(?:^\/[^\]]+\|)?'.preg_quote($lang).'(?:\|[^\]]+)?(?:\]\
|\])/isU', 'self::mlang_checkNeverCross', $s); } // escape brackets inside of $s = preg_replace_callback('/(\]*)(\>)/isU', 'self::mlang_unEscapeBracketTextBox', $s); // escape brackets inside of $s = preg_replace_callback('/(\]*\>)(.*)(<\/textarea\>)/isU', 'self::mlang_unEscapeBracket', $s); return $s; } public static function mlang_escapeBracketTextBox($matches) { if (preg_match('/type=["\']?(?=text|hidden)["\']?/i', $matches[2])) { return $matches[1].str_replace('[', '__ml[ml__', $matches[2]).$matches[3]; } else { return $matches[1].$matches[2].$matches[3]; } } public static function mlang_escapeBracket($matches) { return $matches[1].str_replace('[', '__ml[ml__', $matches[2]).$matches[3]; } public static function mlang_unEscapeBracketTextBox($matches) { if (preg_match('/type=["\']?(?=text|hidden)["\']?/i', $matches[2])) { return $matches[1].str_replace('__ml[ml__', '[', $matches[2]).$matches[3]; } else { return $matches[1].$matches[2].$matches[3]; } } public static function mlang_unEscapeBracket($matches) { return $matches[1].str_replace('__ml[ml__', '[', $matches[2]).$matches[3]; } public static function mlang_checkNeverCross($matches) { return preg_match(CUBE_UTILS_ML_NEVERCROSSREGEX, $matches[0]) ? $matches[0] : ''; } }