| Total Complexity | 65 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Utility 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 Utility, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Utility extends Common\SysUtility |
||
| 16 | { |
||
| 17 | //--------------- Custom module methods ----------------------------- |
||
| 18 | /** |
||
| 19 | * @param $value |
||
| 20 | * @param $out_charset |
||
| 21 | * @param $in_charset |
||
| 22 | * @return array|string |
||
| 23 | */ |
||
| 24 | public static function convertEncoding($value, $out_charset, $in_charset) |
||
| 25 | { |
||
| 26 | if (\is_array($value)) { |
||
| 27 | foreach ($value as $key => $val) { |
||
| 28 | $value[$key] = static::convertEncoding($val, $out_charset, $in_charset); |
||
| 29 | } |
||
| 30 | } else { |
||
| 31 | $value = static::convertItem($value, $out_charset, $in_charset); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $value; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $value |
||
| 39 | * @param $out_charset |
||
| 40 | * @param $in_charset |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public static function convertItem($value, $out_charset, $in_charset) |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | public static function createConfig() |
||
| 67 | { |
||
| 68 | $helper = Helper::getInstance(); |
||
| 69 | $languageHandler = $helper->getHandler('Language'); |
||
| 70 | |||
| 71 | return $languageHandler->createConfig(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | public static function loadConfig() |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Analyzes some PHP environment variables to find the most probable language |
||
| 88 | * that should be used |
||
| 89 | * |
||
| 90 | * @param string $str |
||
| 91 | * @param string $envType |
||
| 92 | * @return int|string |
||
| 93 | * @internal param $string $ string to analyze |
||
| 94 | * @internal param $integer $ type of the PHP environment variable which value is $str |
||
| 95 | * |
||
| 96 | * @global array the list of available translations |
||
| 97 | * @global string the retained translation keyword |
||
| 98 | * @access private |
||
| 99 | */ |
||
| 100 | public static function langDetect($str = '', $envType = '') |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string|bool |
||
| 128 | */ |
||
| 129 | public static function detectLang() |
||
| 130 | { |
||
| 131 | global $_SERVER; |
||
| 132 | // if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
| 133 | if (Request::hasVar('HTTP_ACCEPT_LANGUAGE', 'SERVER')) { |
||
| 134 | $HTTP_ACCEPT_LANGUAGE = Request::getString('HTTP_ACCEPT_LANGUAGE', '', 'SERVER'); |
||
| 135 | } |
||
| 136 | |||
| 137 | //if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
||
| 138 | if (Request::hasVar('HTTP_USER_AGENT', 'SERVER')) { |
||
| 139 | $HTTP_USER_AGENT = Request::getString('HTTP_USER_AGENT', '', 'SERVER'); |
||
| 140 | } |
||
| 141 | |||
| 142 | $lang = ''; |
||
| 143 | $xoops_lang = ''; |
||
| 144 | // 1. try to findout user's language by checking its HTTP_ACCEPT_LANGUAGE variable |
||
| 145 | |||
| 146 | if (empty($lang) && !empty($HTTP_ACCEPT_LANGUAGE)) { |
||
| 147 | $accepted = explode(',', $HTTP_ACCEPT_LANGUAGE); |
||
| 148 | reset($accepted); |
||
| 149 | foreach ($accepted as $iValue) { |
||
| 150 | $lang = static::langDetect($iValue, 1); |
||
| 151 | if (strncasecmp($lang, 'en', 2)) { |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | //This returns the most preferred language "q=1" |
||
| 158 | $lang = static::getPreferredLanguage(); |
||
| 159 | |||
| 160 | // 2. if not found in HTTP_ACCEPT_LANGUAGE, try to find user's language by checking its HTTP_USER_AGENT variable |
||
| 161 | if (empty($lang) && !empty($HTTP_USER_AGENT)) { |
||
| 162 | $lang = static::langDetect($HTTP_USER_AGENT, 2); |
||
| 163 | } |
||
| 164 | // 3. If we catch a valid language, configure it |
||
| 165 | if (!empty($lang)) { |
||
| 166 | $xoops_lang = isset($available_languages[$lang][1])?:''; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $xoops_lang; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param $output |
||
| 174 | * @return array|string|null |
||
| 175 | */ |
||
| 176 | public static function encodeCharSet($output) |
||
| 177 | { |
||
| 178 | global $xlanguage; |
||
| 179 | $output = static::cleanMultiLang($output); |
||
| 180 | // escape XML doc |
||
| 181 | if (\preg_match("/^\<\?[\s]?xml[\s]+version=([\"'])[^\>]+\\1[\s]+encoding=([\"'])[^\>]+\\2[\s]?\?\>/i", $output)) { |
||
| 182 | return $output; |
||
| 183 | } |
||
| 184 | $in_charset = $xlanguage['charset_base']; |
||
| 185 | $out_charset = $xlanguage['charset']; |
||
| 186 | |||
| 187 | $output = static::convertEncoding($output, $out_charset, $in_charset); |
||
| 188 | |||
| 189 | return $output; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $text |
||
| 194 | * @return array|string|string[]|null |
||
| 195 | */ |
||
| 196 | public static function cleanMultiLang($text) |
||
| 197 | { |
||
| 198 | global $xoopsConfig; |
||
| 199 | global $xlanguage_langs; |
||
| 200 | $patterns = []; |
||
| 201 | if (!isset($xlanguage_langs)) { |
||
| 202 | $xlanguage_langs = []; |
||
| 203 | $helper = Helper::getInstance(); |
||
| 204 | $languageHandler = $helper->getHandler('Language'); |
||
| 205 | $langs = $languageHandler->getAll(true); |
||
| 206 | // $langs = $GLOBALS['xlanguageHandler']->getAll(true); //mb |
||
| 207 | if (false !== $langs) { |
||
| 208 | foreach (\array_keys($langs) as $_lang) { |
||
| 209 | $xlanguage_langs[$_lang] = $langs[$_lang]->getVar('lang_code'); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | unset($langs); |
||
| 213 | } |
||
| 214 | if (empty($xlanguage_langs) || 0 == \count($xlanguage_langs)) { |
||
| 215 | return $text; |
||
| 216 | } |
||
| 217 | |||
| 218 | // escape brackets inside of <code>...</code> |
||
| 219 | $patterns[] = '/(\<code>.*\<\/code>)/isU'; |
||
| 220 | |||
| 221 | // escape brackets inside of <input type="..." value="..."> |
||
| 222 | $patterns[] = '/(\<input\b(?![^\>]*\btype=([\'"]?)(submit|image|reset|button))[^\>]*\>)/isU'; |
||
| 223 | |||
| 224 | // escape brackets inside of <textarea></textarea> |
||
| 225 | $patterns[] = '/(\<textarea\b[^>]*>[^\<]*\<\/textarea>)/isU'; |
||
| 226 | |||
| 227 | $text = \preg_replace_callback($patterns, 'static::escapeBracketMultiLang', $text); |
||
| 228 | |||
| 229 | // create the pattern between language tags |
||
| 230 | $pqhtmltags = \explode(',', preg_quote(\XLANGUAGE_TAGS_RESERVED, '/')); |
||
| 231 | $mid_pattern = '(?:(?!(' . \str_replace(',', '|', preg_quote(\XLANGUAGE_TAGS_RESERVED, '/')) . ')).)*'; |
||
| 232 | |||
| 233 | $patterns = []; |
||
| 234 | $replaces = []; |
||
| 235 | |||
| 236 | if (isset($xlanguage_langs[$xoopsConfig['language']])) { |
||
| 237 | $lang = $xlanguage_langs[$xoopsConfig['language']]; |
||
| 238 | $patterns[] = '/(\[([^\]]*\|)?' . preg_quote($lang, '~') . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($lang, '~') . '(\|[^\]]*)?\])/isU'; |
||
| 239 | $replaces[] = '$4'; |
||
| 240 | } |
||
| 241 | |||
| 242 | foreach (\array_keys($xlanguage_langs) as $_lang) { |
||
| 243 | if ($_lang == @$xoopsConfig['language']) { |
||
| 244 | continue; |
||
| 245 | } |
||
| 246 | $name = $xlanguage_langs[$_lang]; |
||
| 247 | $patterns[] = '/(\[([^\]]*\|)?' . preg_quote($name, '~') . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($name, '~') . '(\|[^\]]*)?(\]\<br[\s]?[\/]?\>|\]))/isU'; |
||
| 248 | $replaces[] = ''; |
||
| 249 | } |
||
| 250 | if (!empty($xoopsConfig['language'])) { |
||
| 251 | $text = \preg_replace('/\[[\/]?[\|]?' . preg_quote($xoopsConfig['language'], '~') . '[\|]?\](\<br \/\>)?/i', '', $text); |
||
| 252 | } |
||
| 253 | if (\count($replaces) > 0) { |
||
| 254 | $text = \preg_replace($patterns, $replaces, $text); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $text; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $matches |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | public static function escapeBracketMultiLang($matches) |
||
| 265 | { |
||
| 266 | global $xlanguage_langs; |
||
| 267 | $ret = $matches[1]; |
||
| 268 | if (!empty($xlanguage_langs)) { |
||
| 269 | $pattern = '/(\[([\/])?(' . \implode('|', \array_map('\preg_quote', \array_values($xlanguage_langs))) . ')([\|\]]))/isU'; |
||
| 270 | $ret = \preg_replace($pattern, '[\\2\\3\\4', $ret); |
||
| 271 | } |
||
| 272 | |||
| 273 | return $ret; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param null|array $options |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public static function showSelectedLanguage($options = null) |
||
| 281 | { |
||
| 282 | require_once XOOPS_ROOT_PATH . '/modules/xlanguage/blocks/xlanguage_blocks.php'; |
||
| 283 | if (empty($options)) { |
||
| 284 | $options[0] = 'images'; // display style: image, text, select |
||
| 285 | $options[1] = ' '; // delimitor |
||
| 286 | $options[2] = 5; // items per line |
||
| 287 | } |
||
| 288 | |||
| 289 | $block = \b_xlanguage_select_show($options); |
||
| 290 | $block['tag'] = 'xlanguage'; |
||
| 291 | |||
| 292 | $content = ''; |
||
| 293 | $i = 1; |
||
| 294 | if (!empty($block['display'])) { //mb |
||
| 295 | if (\in_array($block['display'], ['images', 'text'])) { |
||
| 296 | foreach ($block['languages'] as $name => $lang) { |
||
| 297 | $content .= '<a href="' . $block['url'] . $lang['name'] . '" title="' . $lang['desc'] . '">'; |
||
| 298 | if ('images' === $block['display']) { |
||
| 299 | $content .= '<img src="' . $lang['image'] . '" alt="' . $lang['desc'] . '"'; |
||
| 300 | if ($block['selected'] != $lang['name']) { |
||
| 301 | $content .= ' style="MozOpacity: .8; opacity: .8; filter:Alpha(opacity=80);"'; |
||
| 302 | } |
||
| 303 | $content .= '>'; |
||
| 304 | } else { |
||
| 305 | $content .= $lang['desc']; |
||
| 306 | } |
||
| 307 | $content .= '</a>'; |
||
| 308 | if (0 == (++$i % $block['number'])) { |
||
| 309 | $content .= '<br>'; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } else { |
||
| 313 | $content .= '<select name="' . $block['tag'] . '" |
||
| 314 | onChange="if (this.options[this.selectedIndex].value.length >0) { window.document.location=this.options[this.selectedIndex].value;}" |
||
| 315 | >'; |
||
| 316 | if (!empty($block['languages'])) { //mb |
||
| 317 | foreach ($block['languages'] as $name => $lang) { |
||
| 318 | $content .= '<option value="' . $block['url'] . $lang['name'] . '"'; |
||
| 319 | if ($block['selected'] == $lang['name']) { |
||
| 320 | $content .= ' selected '; |
||
| 321 | } |
||
| 322 | $content .= '>' . $lang['desc'] . '</option>'; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | $content .= '</select>'; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | \define('XLANGUAGE_SWITCH_CODE', $content); |
||
| 330 | |||
| 331 | return true; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return int|string |
||
| 336 | */ |
||
| 337 | public static function getPreferredLanguage() |
||
| 370 | } |
||
| 371 | } |
||
| 372 |