/**
 * Returns returns person name convention for a given language.
 * @param string $language	The input language.
 * @param string $type		The type of the requested convention. It may be 'format' for name order convention or 'sort_by' for name sorting convention.
 * @return mixed			Depending of the requested type, the returned result may be string or boolean; null is returned on error;
 */
function _api_get_person_name_convention($language, $type)
{
    global $app;
    $conventions = $app['name_order_conventions'];
    $language = api_purify_language_id($language);
    switch ($type) {
        case 'format':
            return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
        case 'sort_by':
            return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
    }
    return null;
}
 /**
  * Chooses the default MySQL-specific collation from given encoding and language.
  * @param string $encoding				A conventional encoding id, i.e. 'UTF-8'
  * @param string $language (optional)	A conventional for the system language id, i.e. 'bulgarian'. If it is empty, the chosen collation is the default server value corresponding to the given encoding.
  * @return string						Returns a suitable default collation, for example 'utf8_general_ci', or NULL if collation was not found.
  * @author Ivan Tcholakov
  */
 public static function to_db_collation($encoding, $language = null)
 {
     static $result = array();
     if (!isset($result[$encoding][$language])) {
         $result[$encoding][$language] = null;
         if (self::is_encoding_supported($encoding)) {
             $db_encoding = self::to_db_encoding($encoding);
             if (!empty($language)) {
                 $lang = api_purify_language_id($language);
                 $res = self::check_db_collation($db_encoding, $lang);
                 if (empty($res)) {
                     $db_collation_map =& self::get_db_collation_map();
                     if (isset($db_collation_map[$lang])) {
                         $res = self::check_db_collation($db_encoding, $db_collation_map[$lang]);
                     }
                 }
                 if (empty($res)) {
                     $res = self::check_db_collation($db_encoding, null);
                 }
                 $result[$encoding][$language] = $res;
             } else {
                 $result[$encoding][$language] = self::check_db_collation($db_encoding, null);
             }
         }
     }
     return $result[$encoding][$language];
 }
/**
 * Returns returns person name convention for a given language.
 * @param string $language	The input language.
 * @param string $type		The type of the requested convention.
 * It may be 'format' for name order convention or 'sort_by' for name sorting convention.
 * @return mixed Depending of the requested type,
 * the returned result may be string or boolean; null is returned on error;
 */
function _api_get_person_name_convention($language, $type)
{
    static $conventions;
    $language = api_purify_language_id($language);
    if (!isset($conventions)) {
        $file = dirname(__FILE__) . '/internationalization_database/name_order_conventions.php';
        if (file_exists($file)) {
            $conventions = (include $file);
        } else {
            $conventions = array('english' => array('format' => 'title first_name last_name', 'sort_by' => 'first_name'));
        }
        // Overwrite classic conventions
        $customConventions = api_get_configuration_value('name_order_conventions');
        if (!empty($customConventions)) {
            foreach ($customConventions as $key => $data) {
                $conventions[$key] = $data;
            }
        }
        $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
        $replacement1 = array('%F', '%L', '%T');
        $search2 = array('first_name', 'last_name', 'title');
        $replacement2 = array('%f', '%l', '%t');
        foreach (array_keys($conventions) as $key) {
            $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
            $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
            $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
        }
    }
    switch ($type) {
        case 'format':
            return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
        case 'sort_by':
            return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
    }
    return null;
}
/**
 * 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;
}