<commit_before>/*************************************************************************
*    UrBackup - Client/Server backup system
*    Copyright (C) 2011  <NAME>
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#ifndef CLIENT_ONLY

#include "helper.h"
#include "../../stringtools.h"
#include "../database.h"

#include <stdlib.h>

Helper::Helper(THREAD_ID pTID, str_map *pGET, str_nmap *pPARAMS)
{
	session=NULL;
	update(pTID,pGET,pPARAMS);
}

void Helper::update(THREAD_ID pTID, str_map *pGET, str_nmap *pPARAMS)
{
	tid=pTID;
	GET=pGET;
	PARAMS=pPARAMS;

	if( session==NULL )
	{	
		session=Server->getSessionMgr()->getUser( (*GET)[L"ses"], widen((*PARAMS)["REMOTE_ADDR"]+(*PARAMS)["HTTP_USER_AGENT"]) );
	}

	/*for(str_nmap::iterator it=PARAMS->begin();it!=PARAMS->end();++it)
	{
		Server->Log(it->first+"="+it->second);
	}*/

	//Get language from ACCEPT_LANGUAGE
	str_map::iterator lit=GET->find(L"lang");
	if(lit!=pGET->end() && lit->second!=L"-")
	{
		language=wnarrow(lit->second);
	}
	else
	{
		std::wstring langs=(*GET)[L"langs"];
		std::vector<std::wstring> clangs;
		Tokenize(langs, clangs, L",");
		str_nmap::iterator al=PARAMS->find("ACCEPT_LANGUAGE");
		if(al==PARAMS->end())
			al=PARAMS->find("HTTP_ACCEPT_LANGUAGE");
		
		if(al!=PARAMS->end())
		{
			std::vector<std::string> toks;
			Tokenize(al->second, toks, ",");
			for(size_t i=0;i<toks.size();++i)
			{
				std::string lstr=getuntil(";", toks[i]);
				if(lstr.empty())
					lstr=toks[i];

				std::string prefix=getuntil("-", lstr);
				if(prefix.empty())
					prefix=lstr;

				if(language.empty())
				{
					bool found=false;
					for(size_t j=0;j<clangs.size();++j)
					{
						if(strlower(clangs[j])==strlower(widen(prefix)))
						{
							found=true;
							break;
						}
					}
					if(found)
					{
						language=strlower(prefix);
						break;
					}
				}
			}

			if(language.empty())
			{
				language="en";
			}
		}
		else
		{
			language="en";
		}
	}

	if( session==NULL)
		invalid_session=true;
	else
		invalid_session=false;
}

SUser *Helper::getSession(void)
{
	return session;
}

void Helper::OverwriteLanguage( std::string pLanguage)
{<commit_msg>Properly select interface language<commit_after>/*************************************************************************
*    UrBackup - Client/Server backup system
*    Copyright (C) 2011  <NAME>
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#ifndef CLIENT_ONLY

#include "helper.h"
#include "../../stringtools.h"
#include "../database.h"

#include <stdlib.h>
#include <algorithm>

Helper::Helper(THREAD_ID pTID, str_map *pGET, str_nmap *pPARAMS)
{
	session=NULL;
	update(pTID,pGET,pPARAMS);
}

void Helper::update(THREAD_ID pTID, str_map *pGET, str_nmap *pPARAMS)
{
	tid=pTID;
	GET=pGET;
	PARAMS=pPARAMS;

	if( session==NULL )
	{	
		session=Server->getSessionMgr()->getUser( (*GET)[L"ses"], widen((*PARAMS)["REMOTE_ADDR"]+(*PARAMS)["HTTP_USER_AGENT"]) );
	}

	//Get language from ACCEPT_LANGUAGE
	str_map::iterator lit=GET->find(L"lang");
	if(lit!=pGET->end() && lit->second!=L"-")
	{
		language=wnarrow(lit->second);
	}
	else
	{
		std::wstring langs=(*GET)[L"langs"];
		std::vector<std::wstring> clangs;
		Tokenize(langs, clangs, L",");
		for(size_t j=0;j<clangs.size();++j)
		{
			clangs[j]=strlower(clangs[j]);
		}
		str_nmap::iterator al=PARAMS->find("ACCEPT_LANGUAGE");
		if(al==PARAMS->end())
			al=PARAMS->find("HTTP_ACCEPT_LANGUAGE");
		
		if(al!=PARAMS->end())
		{
			std::vector<std::string> toks;
			Tokenize(al->second, toks, ",");
			for(size_t i=0;i<toks.size();++i)
			{
				std::string lstr=getuntil(";", toks[i]);
				if(lstr.empty())
					lstr=toks[i];

				std::string prefix=getuntil("-", lstr);
				if(prefix.empty())
					prefix=lstr;

				std::string sub=getafter("-", lstr);

				if(language.empty())
				{
					if(std::find(clangs.begin(), clangs.end(), strlower(widen(prefix+"_"+sub)))!=clangs.end())
					{
						language=strlower(prefix+"_"+sub);
						break;
					}

					if(std::find(clangs.begin(), clangs.end(), strlower(widen(prefix)))!=clangs.end())
					{
						language=strlower(prefix);
						break;
					}
				}
			}

			if(language.empty())
			{
				language="en";
			}
		}
		else
		{
			language="en";
		}
	}

	if( session==NULL)
		invalid_session=true;
	else
		invalid_session=false;
}

SUser *Helper::getSession(void)
{
	return session;
}

void Helper::OverwriteLanguage( std::string pLanguage)
{
{'fraction_non_alphanumeric': 0.2087588713865328, 'fraction_numerical': 0.0024234031504240957, 'mean_word_length': 4.019982623805387, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 41, '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': '4429008', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2013, 'n_words': 428}