<commit_before><?php
/*
 * This file is part of Web Instant Messenger project.
 *
 * Copyright (c) 2005-2008 Web Messenger Community
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */

session_start();

require_once(dirname(__FILE__).'/converter.php');
require_once(dirname(__FILE__).'/config.php');

$version = 'v1.4.3';

function myiconv($in_enc, $out_enc, $string) {
	global $_utf8win1251, $_win1251utf8;
	if($in_enc == $out_enc ) {
		return $string;
	}
	if( function_exists('iconv') ) {
		$converted = @iconv($in_enc, $out_enc, $string);
		if( $converted !== FALSE ) {
			return $converted;
		}
	}
	if( $in_enc == "cp1251" && $out_enc == "utf-8" )
		return strtr($string, $_win1251utf8);
	if( $in_enc == "utf-8" && $out_enc == "cp1251" )
		return strtr($string, $_utf8win1251);

	return $string; // do not know how to convert
}

function verifyparam( $name, $regexp, $default = null ) {
	if( isset( $_GET[$name] ) ) {
		$val = $_GET[$name];
		if( preg_match( $regexp, $val ) )
			return $val;

	} else if( isset( $_POST[$name] ) ) {
		$val = $_POST[$name];
		if( preg_match( $regexp, $val ) )
			return $val;

	} else {
		if( isset( $default ) )
			return $default;
	}
	echo "<html><head></head><body>Wrong parameter used or absent: ".$name."</body></html>";
	exit;
}

function debugexit_print( $var ) {
	echo "<html><body><pre>";
	print_r( $var );
	echo "</pre></body></html>";
	exit;
}

function get_user_locale() {
	global $available_locales, $default_locale;

	if( isset($_COOKIE['webim_locale']) ) {
		$requested_lang = $_COOKIE['webim_locale'];
		if( in_array($requested_lang,$available_locales) )
			return $requested_lang;
	}

	if( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
		$requested_langs = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
		foreach( $requested_langs as $requested_lang) {
			if( strlen($requested_lang) > 2 )
				$requested_lang = substr($requested_lang,0,2);

			if( in_array($requested_lang,$available_locales) )
				return $requested_lang;
		}
	}

	if( in_array($default_locale,$available_locales) )
		return $default_locale;

	return 'en';
}

function get_locale() {
	global $available_locales, $webimroot;

	$locale = verifyparam("locale", "/^[\w-]{2,5}$/", "");

	if( $locale && in_array($locale,$available_locales) ) {
		$_SESSION['locale'] = $locale;
		setcookie('webim_locale', $locale, time()+60*60*24*1000, "$webimroot/");
	} else if( isset($_SESSION['locale']) ){
		$locale = $_SESSION['locale'];
	}

	if( !$locale || !in_array($locale,$available_locales) )
		$locale = get_user_locale();
	return $locale;
}

$current_locale = get_locale();
setlocale(LC_TIME, $current_locale);
$messages = array();
$output_encoding = array();

function get_locale_links($href) {
	global $available_locales, $current_locale;
	$localeLinks = "";
	foreach($available_locales as $k) {
		if( strlen($localeLinks) > 0 )
			$localeLinks .= " &bull; ";
		if( $k == $current_locale )
			$localeLinks .= $k;
		else
			$localeLinks .= "<a href=\"$href?locale=$k\">$k</a>";
	}
	return $localeLinks;
}

function load_messages($locale) {
	global $messages, $webim_encoding, $output_encoding;
	$hash = array();
	$current_encoding = $webim_encoding;
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "r");
	while (!feof($fp)) {
		$line = fgets($fp, 4096);
		$keyval = split("=", $line, 2 );
		if( isset($keyval[1]) ) {
			if($keyval[0] == 'encoding') {
				$current_encoding = trim($keyval[1]);
			} else if($keyval[0] == 'output_encoding') {
				$output_encoding[$locale] = trim($keyval[1]);
			} else if( $current_encoding == $webim_encoding ) {
				$hash[$keyval[0]] = str_replace("\\n", "\n",trim($keyval[1]));
			} else {
				$hash[$keyval[0]] = myiconv($current_encoding, $webim_encoding, str_replace("\\n", "\n",trim($keyval[1])));
			}
		}
	}
	fclose($fp);
	$messages[$locale] = $hash;
}

function save_message($locale,$key,$value) {
	global $webim_encoding;
	$result = "";
	$added = false;
	$current_encoding = $webim_encoding;
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "r");
	while (!feof($fp)) {
		$line = fgets($fp, 4096);
		$keyval = split("=", $line, 2 );
		if( isset($keyval[1]) ) {
			if($keyval[0] == 'encoding') {
				$current_encoding = trim($keyval[1]);
			} else if(!$added && $keyval[0] == $key) {
				$line = "$key=".myiconv($webim_encoding, $current_encoding, str_replace("\n", "\\n",trim($value)))."\n";
				$added = true;
			}
		}
		$result .= $line;
	}
	fclose($fp);
	if(!$added) {
		$result .= "$key=".myiconv($webim_encoding, $current_encoding, str_replace("\n", "\\n",trim($value)))."\n";
	}
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "w");
	fwrite($fp, $result);
	fclose($fp);
}

function getoutputenc() {
	global $current_locale, $output_encoding, $webim_encoding, $messages;
	if(!isset($messages[$current_locale]))
		load_messages($current_locale);
	return isset($output_encoding[$current_locale]) ? $output_encoding[$current_locale] : $webim_encoding;
}

function getstring_($text,$locale) {<commit_msg>remove \r in property strings<commit_after><?php
/*
 * This file is part of Web Instant Messenger project.
 *
 * Copyright (c) 2005-2008 Web Messenger Community
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */

session_start();

require_once(dirname(__FILE__).'/converter.php');
require_once(dirname(__FILE__).'/config.php');

$version = 'v1.4.3';

function myiconv($in_enc, $out_enc, $string) {
	global $_utf8win1251, $_win1251utf8;
	if($in_enc == $out_enc ) {
		return $string;
	}
	if( function_exists('iconv') ) {
		$converted = @iconv($in_enc, $out_enc, $string);
		if( $converted !== FALSE ) {
			return $converted;
		}
	}
	if( $in_enc == "cp1251" && $out_enc == "utf-8" )
		return strtr($string, $_win1251utf8);
	if( $in_enc == "utf-8" && $out_enc == "cp1251" )
		return strtr($string, $_utf8win1251);

	return $string; // do not know how to convert
}

function verifyparam( $name, $regexp, $default = null ) {
	if( isset( $_GET[$name] ) ) {
		$val = $_GET[$name];
		if( preg_match( $regexp, $val ) )
			return $val;

	} else if( isset( $_POST[$name] ) ) {
		$val = $_POST[$name];
		if( preg_match( $regexp, $val ) )
			return $val;

	} else {
		if( isset( $default ) )
			return $default;
	}
	echo "<html><head></head><body>Wrong parameter used or absent: ".$name."</body></html>";
	exit;
}

function debugexit_print( $var ) {
	echo "<html><body><pre>";
	print_r( $var );
	echo "</pre></body></html>";
	exit;
}

function get_user_locale() {
	global $available_locales, $default_locale;

	if( isset($_COOKIE['webim_locale']) ) {
		$requested_lang = $_COOKIE['webim_locale'];
		if( in_array($requested_lang,$available_locales) )
			return $requested_lang;
	}

	if( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
		$requested_langs = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
		foreach( $requested_langs as $requested_lang) {
			if( strlen($requested_lang) > 2 )
				$requested_lang = substr($requested_lang,0,2);

			if( in_array($requested_lang,$available_locales) )
				return $requested_lang;
		}
	}

	if( in_array($default_locale,$available_locales) )
		return $default_locale;

	return 'en';
}

function get_locale() {
	global $available_locales, $webimroot;

	$locale = verifyparam("locale", "/^[\w-]{2,5}$/", "");

	if( $locale && in_array($locale,$available_locales) ) {
		$_SESSION['locale'] = $locale;
		setcookie('webim_locale', $locale, time()+60*60*24*1000, "$webimroot/");
	} else if( isset($_SESSION['locale']) ){
		$locale = $_SESSION['locale'];
	}

	if( !$locale || !in_array($locale,$available_locales) )
		$locale = get_user_locale();
	return $locale;
}

$current_locale = get_locale();
setlocale(LC_TIME, $current_locale);
$messages = array();
$output_encoding = array();

function get_locale_links($href) {
	global $available_locales, $current_locale;
	$localeLinks = "";
	foreach($available_locales as $k) {
		if( strlen($localeLinks) > 0 )
			$localeLinks .= " &bull; ";
		if( $k == $current_locale )
			$localeLinks .= $k;
		else
			$localeLinks .= "<a href=\"$href?locale=$k\">$k</a>";
	}
	return $localeLinks;
}

function load_messages($locale) {
	global $messages, $webim_encoding, $output_encoding;
	$hash = array();
	$current_encoding = $webim_encoding;
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "r");
	while (!feof($fp)) {
		$line = fgets($fp, 4096);
		$keyval = split("=", $line, 2 );
		if( isset($keyval[1]) ) {
			if($keyval[0] == 'encoding') {
				$current_encoding = trim($keyval[1]);
			} else if($keyval[0] == 'output_encoding') {
				$output_encoding[$locale] = trim($keyval[1]);
			} else if( $current_encoding == $webim_encoding ) {
				$hash[$keyval[0]] = str_replace("\\n", "\n",trim($keyval[1]));
			} else {
				$hash[$keyval[0]] = myiconv($current_encoding, $webim_encoding, str_replace("\\n", "\n",trim($keyval[1])));
			}
		}
	}
	fclose($fp);
	$messages[$locale] = $hash;
}

function save_message($locale,$key,$value) {
	global $webim_encoding;
	$result = "";
	$added = false;
	$current_encoding = $webim_encoding;
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "r");
	while (!feof($fp)) {
		$line = fgets($fp, 4096);
		$keyval = split("=", $line, 2 );
		if( isset($keyval[1]) ) {
			if($keyval[0] == 'encoding') {
				$current_encoding = trim($keyval[1]);
			} else if(!$added && $keyval[0] == $key) {
				$line = "$key=".myiconv($webim_encoding, $current_encoding, str_replace("\r", "",str_replace("\n", "\\n",trim($value))))."\n";
				$added = true;
			}
		}
		$result .= $line;
	}
	fclose($fp);
	if(!$added) {
		$result .= "$key=".myiconv($webim_encoding, $current_encoding, str_replace("\r", "",str_replace("\n", "\\n",trim($value))))."\n";
	}
	$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties", "w");
	fwrite($fp, $result);
	fclose($fp);
}

function getoutputenc() {
	global $current_locale, $output_encoding, $webim_encoding, $messages;
	if(!isset($messages[$current_locale]))
		load_messages($current_locale);
	return isset($output_encoding[$current_locale]) ? $output_encoding[$current_locale] : $webim_encoding;
}

function getstring_($text,$locale) {
{'fraction_non_alphanumeric': 0.20024500565397663, 'fraction_numerical': 0.0158311345646438, 'mean_word_length': 4.8895671476137625, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2746478', 'n_tokens_mistral': 4206, 'n_tokens_neox': 3911, 'n_words': 748}