/**
 * This function reads a Chamilo language file and transforms it into XML,
 * then returns the XML string to the caller.
 */
function get_language_file_as_xml($language = 'english')
{
    $path = api_get_path(SYS_LANG_PATH) . $language . '/';
    if (!is_dir($path) or !is_readable($path)) {
        if ($language != 'english') {
            return get_language_file_as_xml('english');
        } else {
            return '';
        }
    }
    //error_log('Analysing path '.$path);
    $file = $path . 'videoconf.inc.php';
    if (!is_file($file) or !is_readable($file)) {
        if ($language != 'english') {
            return get_language_file_as_xml('english');
        } else {
            return '';
        }
    }
    /*
    $convert = true;
    if(substr($language,-7,7) == 'unicode')
    {//do not convert if the language ends with 'unicode', which means it's in UTF-8
    	$convert=false;
    }
    $list = file($file);
    $xml = '';
    foreach ( $list as $line )
    {
    	if(substr($line,0,1)=='$')
    	{
    		$items = array();
    		$match = preg_match('/^\$([^\s]*)\s*=\s*"(.*)";$/',$line,$items);
    		if($match)
    		{
    			//todo: The following conversion should only happen for old language files (encoded in ISO-8859-1).
    			if($convert)
    			{
    				$string = api_convert_encoding($items[2],'UTF-8','ISO-8859-1');
    			}
    			else
    			{
    				$string = $items[2];
    			}
    			$xml .= '<labelfield><labelid>'.$items[1].'</labelid><labelvalue>'.stripslashes($string).'</labelvalue></labelfield>'."\n";
    		}
    	}
    }
    */
    //---------
    $non_utf8_encoding = api_get_non_utf8_encoding($language);
    $list = file($file);
    $xml = '';
    foreach ($list as $line) {
        if (substr($line, 0, 1) == '$') {
            $items = array();
            $match = preg_match('/^\\$([^\\s]*)\\s*=\\s*"(.*)";$/', $line, $items);
            if ($match) {
                $string = $items[2];
                if (!api_is_valid_utf8($string)) {
                    $string = api_html_entity_decode(api_utf8_encode($string, $non_utf8_encoding), ENT_QUOTES, 'UTF-8');
                }
                $xml .= '<labelfield><labelid>' . $items[1] . '</labelid><labelvalue>' . stripslashes($string) . '</labelvalue></labelfield>' . "\n";
            }
        }
    }
    //---------
    if (empty($xml) && $language != 'english') {
        return get_language_file_as_xml('english');
    }
    return $xml;
}
/**
 * Converts a given string into the system ecoding (or platform character set).
 * When $from encoding is omited on UTF-8 platforms then language dependent encoding
 * is guessed/assumed. On non-UTF-8 platforms omited $from encoding is assumed as UTF-8.
 * When the parameter $check_utf8_validity is true the function checks string's
 * UTF-8 validity and decides whether to try to convert it or not.
 * This function is useful for problem detection or making workarounds.
 * @param string $string						The string being converted.
 * @param string $from_encoding (optional)		The encoding that $string is being converted from. It is guessed when it is omited.
 * @param bool $check_utf8_validity (optional)	A flag for UTF-8 validity check as condition for making conversion.
 * @return string								Returns the converted string.
 */
function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false)
{
    $system_encoding = api_get_system_encoding();
    if (empty($from_encoding)) {
        if (api_is_utf8($system_encoding)) {
            $from_encoding = api_get_non_utf8_encoding();
        } else {
            $from_encoding = 'UTF-8';
        }
    }
    if (api_equal_encodings($system_encoding, $from_encoding)) {
        return $string;
    }
    if ($check_utf8_validity) {
        if (api_is_utf8($system_encoding)) {
            if (api_is_valid_utf8($string)) {
                return $string;
            }
        } elseif (api_is_utf8($from_encoding)) {
            if (!api_is_valid_utf8($string)) {
                return $string;
            }
        }
    }
    return api_convert_encoding($string, $system_encoding, $from_encoding);
}