public function sqlConnect() { if (!$this->connection_handler) { $this->connection_handler = @mysql_connect($this->connection['server'], $this->connection['login'], $this->connection['password'], true); // The system has not been designed to use special SQL modes that were introduced since MySQL 5 @mysql_query("set session sql_mode='';", $this->connection_handler); @mysql_select_db($this->connection['base'], $this->connection_handler); // Initialization of the database connection encoding to be used. // The internationalization library should be already initialized. @mysql_query("SET SESSION character_set_server='utf8';", $this->connection_handler); @mysql_query("SET SESSION collation_server='utf8_general_ci';", $this->connection_handler); $system_encoding = api_get_system_encoding(); if (api_is_utf8($system_encoding)) { // See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language. @mysql_query("SET NAMES 'utf8';", $this->connection_handler); } else { @mysql_query("SET CHARACTER SET '" . Database::to_db_encoding($system_encoding) . "';", $this->connection_handler); } } return ($this->connection_handler) ? true : false; }
/** * Detects the encoding of a given manifest (a xml-text). * It is possible the encoding of the manifest to be wrongly declared or * not to be declared at all. The proposed method tries to resolve these problems. * @param string $xml The input xml-text. * @return string The detected value of the input xml. */ private function detect_manifest_encoding(&$xml) { if (api_is_valid_utf8($xml)) { return 'UTF-8'; } if (preg_match(_PCRE_XML_ENCODING, $xml, $matches)) { $declared_encoding = api_refine_encoding_id($matches[1]); } else { $declared_encoding = ''; } if (!empty($declared_encoding) && !api_is_utf8($declared_encoding)) { return $declared_encoding; } $test_string = ''; if (preg_match_all('/<langstring[^>]*>(.*)<\\/langstring>/m', $xml, $matches)) { $test_string = implode("\n", $matches[1]); unset($matches); } if (preg_match_all('/<title[^>]*>(.*)<\\/title>/m', $xml, $matches)) { $test_string .= "\n" . implode("\n", $matches[1]); unset($matches); } if (empty($test_string)) { $test_string = $xml; } return api_detect_encoding($test_string); }
/** * Sorts an array with maintaining index association, elements will be arranged from the lowest to the highest. * @param array $array The input array. * @param int $sort_flag (optional) Shows how elements of the array to be compared. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default. * @return bool Returns TRUE on success, FALSE on error. * Note: $sort_flag may have the following values: * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types; * SORT_NUMERIC - items will be compared as numbers; * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale; * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale. * This function is aimed at replacing the function asort() for sorting human-language strings. * @link http://php.net/manual/en/function.asort.php * @link http://php.net/manual/en/collator.asort.php */ function api_asort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) { if (INTL_INSTALLED) { if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } $collator = _api_get_collator($language); if (is_object($collator)) { if (api_is_utf8($encoding)) { $sort_flag = $sort_flag == SORT_LOCALE_STRING ? SORT_STRING : $sort_flag; return collator_asort($collator, $array, _api_get_collator_sort_flag($sort_flag)); } elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) { global $_api_collator, $_api_encoding; $_api_collator = $collator; $_api_encoding = $encoding; return uasort($array, '_api_cmp'); } } } return asort($array, $sort_flag); }
/** * Splits a string by a regular expression, UTF-8 aware when it is applicable. * @param string $pattern The pattern to search for, as a string. * @param string $subject The input string. * @param int $limit (optional) If specified, then only substrings up to $limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP. * @param int $flags (optional) $flags can be any combination of the following flags (combined with bitwise | operator): * PREG_SPLIT_NO_EMPTY - if this flag is set, only non-empty pieces will be returned; * PREG_SPLIT_DELIM_CAPTURE - if this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well; * PREG_SPLIT_OFFSET_CAPTURE - If this flag is set, for every occurring match the appendant string offset will also be returned. * Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default. * @return array Returns an array containing substrings of $subject split along boundaries matched by $pattern. * @link http://php.net/preg_split */ function api_preg_split($pattern, $subject, $limit = -1, $flags = 0, $encoding = null) { if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } return preg_split(api_is_utf8($encoding) ? $pattern . 'u' : $pattern, $subject, $limit, $flags); }
function xmddoc($strings, $charset = null, $textstring = '') { if (empty($charset)) { $charset = api_get_system_encoding(); } $this->parent = array(); $this->name = array(); $this->ns = array(); $this->attributes = array(); $this->atns = array(); $this->children = array(); $this->error = ''; $this->_nesting = array(); $this->_ns = array(); $this->_last = -1; $this->_nsp = array(); foreach (explode(',', 'eb,tn,eg,ut,as,ne,jt,ne,ah,en,er') as $pfx) { $this->_nsp[$pfx] = ''; } if (is_array($charset)) { $this->names = $strings; $this->numbers = $charset; $this->textstring = $textstring; $this->_uncache(); return; } $this->names = array(); $this->_lookup(''); // empty ns is number 0 // This is a quick workaround. // The xml-parser supports only ISO-8859-1, UTF-8 and US-ASCII. // See http://php.net/manual/en/function.xml-parser-create-ns.php //$xml_parser = xml_parser_create_ns($charset, ':'); $xml_parser = xml_parser_create_ns(api_is_utf8($charset) ? 'UTF-8' : 'ISO-8859-1', ':'); xml_set_object($xml_parser, $this); // instead of ...,&$this // See PHP manual: Passing by Reference vs. xml_set_object xml_set_element_handler($xml_parser, '_startElement', '_endElement'); xml_set_character_data_handler($xml_parser, '_cData'); xml_set_start_namespace_decl_handler($xml_parser, '_startNs'); // xml_set_end_namespace_decl_handler($xml_parser, '_endNs'); // xml_set_default_handler ($xml_parser, ''); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE); if (!is_array($strings)) { $strings = array($strings); } if (count($strings) && api_substr($strings[0], 0, 5) != '<?xml' && !xml_parse($xml_parser, '<?xml version="1.0" encoding="' . $charset . '"?>', FALSE)) { $this->error = 'Encoding ' . $charset . ': ' . xml_error_string(xml_get_error_code($xml_parser)); $strings = array(); } foreach ($strings as $s) { if (api_substr($s, -1) != "\n") { $s .= "\n"; } if (!xml_parse($xml_parser, $s, FALSE)) { $errCode = xml_get_error_code($xml_parser); $this->error = 'Line ' . xml_get_current_line_number($xml_parser) . ' (c' . xml_get_current_column_number($xml_parser) . '): error ' . $errCode . '= ' . xml_error_string($errCode); break; // the error string is English... } } xml_parse($xml_parser, '', TRUE); xml_parser_free($xml_parser); }
while ($row = @Database::fetch_array($result)) { $charset = $row[0]; } if (empty($charset)) { $charset = 'UTF-8'; } // Preserving the value of the global variable $charset. $charset_initial_value = $charset; // Initialization of the internationalization library. api_initialize_internationalization(); // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library. api_set_internationalization_default_encoding($charset); // Initialization of the database encoding to be used. Database::query("SET SESSION character_set_server='utf8';"); Database::query("SET SESSION collation_server='utf8_general_ci';"); if (api_is_utf8($charset)) { // See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language. Database::query("SET NAMES 'utf8';"); } else { Database::query("SET CHARACTER SET '" . Database::to_db_encoding($charset) . "';"); } // Start session after the internationalization library has been initialized. Chamilo::session()->start($already_installed); // Remove quotes added by PHP - get_magic_quotes_gpc() is deprecated in PHP 5 see #2970 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) { array_walk_recursive_limited($_GET, 'stripslashes', true); array_walk_recursive_limited($_POST, 'stripslashes', true); array_walk_recursive_limited($_COOKIE, 'stripslashes', true); array_walk_recursive_limited($_REQUEST, 'stripslashes', true); } // access_url == 1 is the default chamilo location
/** * Detects encoding of plain text. * @param string $string The input text. * @param string $language (optional) The language of the input text, provided if it is known. * @return string Returns the detected encoding. */ function api_detect_encoding($string, $language = null) { // Testing against valid UTF-8 first. if (api_is_valid_utf8($string)) { return 'UTF-8'; } $result = null; $delta_points_min = LANGUAGE_DETECT_MAX_DELTA; // Testing non-UTF-8 encodings. $encodings = api_get_valid_encodings(); foreach ($encodings as &$encoding) { if (api_is_encoding_supported($encoding) && !api_is_utf8($encoding)) { $stringToParse = api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding); $strintToParse2 = _api_generate_n_grams($stringToParse, $encoding); $result_array = _api_compare_n_grams($strintToParse2, $encoding); if (!empty($result_array)) { list($key, $delta_points) = each($result_array); if ($delta_points < $delta_points_min) { $pos = strpos($key, ':'); $result_encoding = api_refine_encoding_id(substr($key, $pos + 1)); if (api_equal_encodings($encoding, $result_encoding)) { if ($string == api_utf8_decode(api_utf8_encode($string, $encoding), $encoding)) { $delta_points_min = $delta_points; $result = $encoding; } } } } } } // "Broken" UTF-8 texts are to be detected as UTF-8. // This functionality is enabled when language of the text is known. $language = api_purify_language_id((string) $language); if (!empty($language)) { $encoding = 'UTF-8'; $result_array =& _api_compare_n_grams(_api_generate_n_grams(api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding), $encoding), $encoding); if (!empty($result_array)) { list($key, $delta_points) = each($result_array); if ($delta_points < $delta_points_min) { $pos = strpos($key, ':'); $result_encoding = api_refine_encoding_id(substr($key, $pos + 1)); $result_language = substr($key, 0, $pos); if ($language == $result_language && api_is_utf8($result_encoding)) { $delta_points_min = $delta_points; $result = $encoding; } } } } return $result; }