/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2016 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
**************************************************************************/
#include "vld.h"
#include "Template.h"
#include "stringtools.h"
#include "Table.h"
#include "utf8/utf8.h"
#include "Server.h"
#include "Interface/Database.h"
CTemplate::CTemplate(std::string pFile)
{
file=pFile;
data=getFile(pFile);
mCurrValues=new CTable();
mValuesRoot=mCurrValues;
AddDefaultReplacements();
}
CTemplate::~CTemplate()
{
delete mValuesRoot;
for(size_t i=0;idestroyQuery( mTables[i].second );
}
}
void CTemplate::AddDefaultReplacements(void)
{
#define ADD_REPLACEMENT(key,value) mReplacements.push_back( std::pair(key,value) )
#define ADD_REPLACEMENT_CODE(key,value) {unsigned char ch=key; std::string tmp;tmp+=ch; mReplacements.push_back( std::pair(tmp,value) );}
/*ADD_REPLACEMENT(L"ä",L"\\xE4");
ADD_REPLACEMENT(L"ö",L"\\xF6");
ADD_REPLACEMENT(L"ü",L"\\xFC");
ADD_REPLACEMENT(L"Ä",L"\\xC4");
ADD_REPLACEMENT(L"Ö",L"\\xFC");
ADD_REPLACEMENT(L"Ü",L"\\xD6");
ADD_REPLACEMENT(L"ß",L"\\xDF");
ADD_REPLACEMENT_CODE(246, "\\xF6");
ADD_REPLACEMENT_CODE(228, "\\xE4");
ADD_REPLACEMENT_CODE(252, "\\xFC");
ADD_REPLACEMENT_CODE(196, "\\xC4");
ADD_REPLACEMENT_CODE(214, "\\xD6");
ADD_REPLACEMENT_CODE(220, "\\xDC");
ADD_REPLACEMENT_CODE(223, "\\xDF");*/
}
ITable* CTemplate::createTableRecursive(std::string key)
{
std::vector toks;
Tokenize(key, toks, ".");
ITable *ct=mCurrValues;
for(size_t i=0;igetObject(toks[i]);
if( nt==NULL )
{
nt=new CRATable();
ct->addObject(toks[i], nt);
}
ct=nt;
}
return ct;
}
ITable* CTemplate::getTable(std::string key)
{
ITable *ret;
if( (ret=findTable(key))!=NULL )
return ret;
else
return createTableRecursive(key);
}
void CTemplate::setValue(std::string key, std::string value)
{
std::vector toks;
Tokenize(key, toks, ".");
ITable *ct=mCurrValues;
if( toks.size()==0 )
return;
if( toks.size()>1 )
{
for(size_t i=0;igetObject(toks[i]);
if( ct==NULL )
break;
}
}
if( ct!=NULL )
ct->addString(toks[toks.size()-1], value);
}
ITable *CTemplate::findTable(std::string key)
{
std::vector toks;
Tokenize(key, toks, ".");
ITable *ct;
if( toks.size()>0 )
{
ct=mCurrValues->getObject(toks[0]);
if( ct==NULL && mValuesRoot!=mCurrValues )
{
ct=mValuesRoot->getObject(toks[0]);
}
for(size_t i=1;igetObject(toks[i]);
}
return ct;
}
return NULL;
}
bool CTemplate::FindValue(const std::string &key, std::string &value, bool dbs)
{
ITable *val=findTable(key);
if( val!=NULL )
{
value=val->getValue();
return true;
}
else
{
if( dbs==false )
return false;
else
{
for(size_t i=0;iBind(key);
db_results res=mTables[i].second->Read();
mTables[i].second->Reset();
if( res.size() >0 )
{
db_single_result result=res[0];
db_single_result::iterator iter=result.find("value");
if( iter!= result.end() )
{
value=iter->second;
return true;
}
}
}
return false;
}
}
}
std::string CTemplate::getData(void)
{
std::string output=data;
transform(output);
return output;
}
void CTemplate::transform(std::string &output)
{
for(size_t i=0;igetSize();++k)
{
std::string tmp=mid;
mCurrValues=tab->getObject(k);
transform(tmp);
output+=tmp;
}
mCurrValues=old_values;
i=output.size()-1;
output+=strend;
}
}
}
bool ifndef1=false,ifndef2=false;
bool next2=false;
if( ((i==0||i==1) && next(output, i, "#ifdef ")==true) || (next2=next(output, i, "\r\n#ifdef "))==true
||((i==0||i==1) && (ifndef1=next(output, i, "#ifndef "))==true) || ((ifndef2=next(output, i, "\r\n#ifndef "))==true)
)
{
bool ifndef=false;
if( ifndef1 || ifndef2 )
ifndef=true;
if( next2==true||ifndef2==true )
i+=2;
std::string var;
int addi=7;
if( ifndef==true )
++addi;
for(size_t j=i+addi;j0 )--i;
output.erase(j, 8);
if( todel!=std::string::npos )
output.erase(todel, j-todel);
}
else
{
size_t todel=0;
size_t enterelseif=std::string::npos;
size_t stopelseif=std::string::npos;
size_t proc=0;
for(size_t j=i;j0 )
todel--;
}
else if( next(output, j, "\r\n#endif" ) )
{
if( proc!=0 )
--proc;
else
{
if( enterelseif!=std::string::npos )
if( stopelseif==std::string::npos )
stopelseif=j;
break;
}
}
++todel;
}
std::string data;
if( enterelseif!=std::string::npos )
data=output.substr(enterelseif, stopelseif-enterelseif);
if( i==0 && data.size()>1 )
data.erase(0,2);
if( i!=0 )
{
i-=2;
todel+=2;
}
output.erase(i, todel+8);
if( data.size()>0 )
{
output.insert(i, data);
}
if( i>0 )
--i;
else
{
retry=true;
break;
}
}
}
if( next(output, i, "#include \"")==true )
{
std::string fn;
bool inside=false;
for(size_t j=i;j(db, db->Prepare("SELECT * FROM "+table+" WHERE key=?",false) ) );
}