winix/plugins/ticket/ticketinfo.cpp

447 lines
9.4 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include <algorithm>
#include <ctime>
#include "ticketinfo.h"
#include "core/error.h"
#include "core/log.h"
#include "core/misc.h"
#include "sessiondata.h"
namespace Ticket
{
TicketInfo::TicketInfo()
{
Clear();
}
void TicketInfo::SetTDb(TDb * ptdb)
{
tdb = ptdb;
}
void TicketInfo::SetDb(Db * pdb)
{
db = pdb;
}
void TicketInfo::SetConfig(Config * pconfig)
{
config = pconfig;
}
void TicketInfo::SetSystem(System * psystem)
{
system = psystem;
}
void TicketInfo::SetCur(Cur * pcur)
{
cur = pcur;
}
void TicketInfo::SetFunctions(Functions * pfunctions)
{
functions = pfunctions;
}
void TicketInfo::Clear()
{
ticket = &ticket_empty;
cur_conf_wrap = &cur_conf_wrap_empty;
cur_conf = &cur_conf_empty;
item_tab.clear();
ticket_tab.clear();
item_sort_tab.clear();
}
void TicketInfo::MarkAllConfToDelete()
{
ConfTab::iterator i = conf_tab.begin();
for( ; i!=conf_tab.end() ; ++i)
i->second.to_delete = true;
}
void TicketInfo::DeleteAllMarkedConf()
{
ConfTab::iterator inext;
ConfTab::iterator i = conf_tab.begin();
while( i != conf_tab.end() )
{
inext = i;
++inext;
if( i->second.to_delete )
{
log << log3 << "Ticket: deleting ticket conf for dir id: " << i->first << logend;
conf_tab.erase(i);
}
i = inext;
}
}
bool TicketInfo::GetConfContent(const std::wstring & path)
{
int status = system->FollowAllLinks(path, config_dir_tab, config_file, false, false, false);
if( status != 1 )
{
log << log1 << "Ticket: problem with reading a config file: " << path << ", status: " << status << logend;
return false;
}
return true;
}
bool TicketInfo::ParseTicketConf(long mount_dir_id, const std::wstring & path)
{
log << log3 << "Ticket: parsing conf file: " << path << logend;
int code = ticket_parser.Parse(config_file.content, conf_tab[mount_dir_id].conf);
conf_tab[mount_dir_id].file_name = path;
return code == WINIX_TICKET_ERR_OK;
}
// if skip_existing_configs is true then only new config files will be parsed
void TicketInfo::ReadTicketConf(Mounts & mounts, bool skip_existing_configs)
{
Mounts::MountTab::const_iterator i;
const Mounts::MountTab * mtab = mounts.GetMountTab();
// loop through all mount points
for(i=mtab->begin() ; i!=mtab->end() ; ++i)
{
const Mount & mount = i->second;
if( mount.param[mount_par_ticket_conf].defined &&
mount.param[mount_par_ticket_conf].arg.size() == 1 )
{
const std::wstring & file_name = mount.param[mount_par_ticket_conf].arg[0];
ConfTab::iterator c = conf_tab.find(mount.dir_id);
bool exists = (c != conf_tab.end() && c->second.file_name == file_name);
if( exists )
c->second.to_delete = false;
if( !(skip_existing_configs && exists) )
{
if( GetConfContent(file_name) )
{
if( !ParseTicketConf(mount.dir_id, file_name) )
conf_tab[mount.dir_id].to_delete = true;
}
else
{
if( exists )
c->second.to_delete = true;
}
}
}
}
}
// if skip_existing_configs is true then only new config files will be parsed
void TicketInfo::ReadTicketConf(bool skip_existing_configs)
{
MarkAllConfToDelete();
ReadTicketConf(system->mounts, skip_existing_configs);
DeleteAllMarkedConf();
}
void TicketInfo::FindCurrentConf()
{
if( !system->mounts.pmount )
return;
long dir_id = system->mounts.pmount->dir_id;
ConfTab::iterator i = conf_tab.find(dir_id);
if( i != conf_tab.end() )
{
cur_conf_wrap = &i->second;
cur_conf = &i->second.conf;
}
else
{
log << log1 << "Ticket: there is no ticket_conf parameter in the mount point (an empty used)" << logend;
}
}
void TicketInfo::CheckMinMaxValue(const TicketConf::TicketItem & conf_item, Ticket::TicketParam & par)
{
if( conf_item.type == TicketConf::TicketItem::TypeInteger ||
conf_item.type == TicketConf::TicketItem::TypeProgress )
{
if( par.int_value < conf_item.integer_min )
par.int_value = conf_item.integer_min;
if( par.int_value > conf_item.integer_max )
par.int_value = conf_item.integer_max;
}
else
if( conf_item.type == TicketConf::TicketItem::TypeSelect )
{
for(size_t a=0 ; a<conf_item.select.size() ; ++a)
{
if( conf_item.select[a].id == par.int_value )
return;
}
if( conf_item.select_default < conf_item.select.size() )
par.int_value = conf_item.select[conf_item.select_default].id;
else
par.int_value = 0;
}
}
bool TicketInfo::ReadTicketValue(const TicketConf::TicketItem & conf_item, Ticket::TicketParam & par, const std::wstring & value)
{
if( conf_item.type == TicketConf::TicketItem::TypeInteger ||
conf_item.type == TicketConf::TicketItem::TypeProgress ||
conf_item.type == TicketConf::TicketItem::TypeSelect )
{
par.int_value = Tol(value);
par.str_value.clear();
CheckMinMaxValue(conf_item, par);
}
else
if( conf_item.type == TicketConf::TicketItem::TypeString ||
conf_item.type == TicketConf::TicketItem::TypeMultistring )
{
par.int_value = 0;
par.str_value = value;
}
else
if( conf_item.type == TicketConf::TicketItem::TypeImages ||
conf_item.type == TicketConf::TicketItem::TypeFiles )
{
if( !value.empty() )
{
log << log1 << "Ticket: images or files should be added only by a specific html tag (ignored)" << logend;
}
else
{
// an empty field from the html form
}
return false;
}
else
{
log << log1 << "Ticket: incorrect parameter, param: " << par.param << ", value: " << value << " (ignored)" << logend;
return false;
}
return true;
}
// file_map can be null
bool TicketInfo::ReadTicketValue(const TicketConf::TicketItem & conf_item,
Ticket::TicketParam & par, const PostFile & value, std::vector<long> * file_map)
{
bool add = false;
if( conf_item.type == TicketConf::TicketItem::TypeImages ||
conf_item.type == TicketConf::TicketItem::TypeFiles )
{
Item * upload_dir = system->dirs.GetDir(conf_item.upload_dir);
if( upload_dir )
{
file.Clear(); // clearing and setting date
file.parent_id = upload_dir->id;
file.type = Item::file;
file.privileges = 0644; // !! tymczasowo
file.file_type = SelectFileType(value.filename);
file.url = value.filename;
functions->PrepareUrl(file);
functions->SetUser(file);
functions->fun_upload.UploadFile(file, value.tmp_filename);
if( cur->request->status == WINIX_ERR_OK )
{
add = true;
par.int_value = file.id;
system->MakePath(file, par.str_value);
if( file_map )
file_map->push_back(file.id);
}
else
{
log << log1 << "Ticket: problem with uploading" << logend;
}
}
else
{
log << log1 << "Ticket: there is no upload dir: " << conf_item.upload_dir << logend;
}
}
if( !add )
log << log1 << "Ticket: file parameter, param: " << par.param << " ignored" << logend;
return add;
}
// if the ticket has param_id already then the parameter is changed
// if no then it is added
void TicketInfo::ReadTicketParam(Ticket & ticket, int param_id, const std::wstring & value)
{
bool exists = false;
ticket_param.Clear();
for(size_t i=0 ; i<cur_conf->tab.size() ; ++i)
{
if( param_id == cur_conf->tab[i].id )
{
for(size_t i2=0 ; i2<ticket.par_tab.size() ; ++i2)
{
if( ticket.par_tab[i2].param == param_id )
{
// parameter exists
exists = true;
ticket_param.param = param_id;
if( ReadTicketValue(cur_conf->tab[i], ticket_param, value) )
ticket.par_tab[i2] = ticket_param;
break;
}
}
if( !exists )
{
// adding a new parameter
exists = true;
ticket_param.param = param_id;
if( ReadTicketValue(cur_conf->tab[i], ticket_param, value) )
ticket.par_tab.push_back(ticket_param);
}
break;
}
}
if( !exists )
log << log1 << "Ticket: skipped incorrect parameter: " << param_id << " (not defined in the config)" << logend;
}
// always adds a new parameter
// file_map can be null
void TicketInfo::ReadTicketParam(Ticket & ticket, int param_id, const PostFile & value, std::vector<long> * file_map)
{
bool exists = false;
ticket_param.Clear();
for(size_t i=0 ; i<cur_conf->tab.size() ; ++i)
{
if( param_id == cur_conf->tab[i].id )
{
exists = true;
ticket_param.param = param_id;
if( ReadTicketValue(cur_conf->tab[i], ticket_param , value, file_map) )
ticket.par_tab.push_back(ticket_param);
break;
}
}
if( !exists )
log << log1 << "Ticket: skipped incorrect parameter: " << param_id << " (not defined in the config)" << logend;
}
void TicketInfo::ReadTicketParams(Ticket & ticket, bool clear_ticket, std::vector<long> * file_map)
{
PostTab::iterator i;
PostFileTab::iterator i2;
if( clear_ticket )
ticket.Clear();
for(i=cur->request->post_tab.begin() ; i!=cur->request->post_tab.end() ; ++i)
{
if( IsSubString(config->ticket_form_prefix, i->first) )
ReadTicketParam(ticket, Toi(i->first.c_str() + config->ticket_form_prefix.size()), i->second);
}
for(i2=cur->request->post_file_tab.begin() ; i2!=cur->request->post_file_tab.end() ; ++i2)
{
if( IsSubString(config->ticket_form_prefix, i2->first) )
ReadTicketParam(ticket, Toi(i2->first.c_str() + config->ticket_form_prefix.size()), i2->second, file_map);
}
ticket.SortParTab();
}
void TicketInfo::RemoveTicket(long file_id)
{
if( tdb->GetTicket(file_id, rm_ticket) == WINIX_ERR_OK )
{
for(size_t i=0 ; i<rm_ticket.par_tab.size(); ++i)
{
long id = rm_ticket.par_tab[i].int_value;
functions->fun_rm.RemoveItemById(id);
}
tdb->RemoveTicket(file_id);
}
}
} // namespace