336 lines
6.3 KiB
C++
Executable File
336 lines
6.3 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2011-2012, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "core/log.h"
|
|
#include "exportinfo.h"
|
|
|
|
|
|
|
|
namespace Export
|
|
{
|
|
|
|
|
|
ExportInfo::ExportInfo()
|
|
{
|
|
use_rsa = false;
|
|
}
|
|
|
|
|
|
|
|
void ExportInfo::SetSystem(System * psystem)
|
|
{
|
|
system = psystem;
|
|
}
|
|
|
|
|
|
void ExportInfo::SetConfig(Config * pconfig)
|
|
{
|
|
config = pconfig;
|
|
}
|
|
|
|
|
|
void ExportInfo::SetEDb(EDb * pdb)
|
|
{
|
|
edb = pdb;
|
|
}
|
|
|
|
|
|
void ExportInfo::SetDb(Db * pdb)
|
|
{
|
|
db = pdb;
|
|
}
|
|
|
|
|
|
void ExportInfo::SetExportThread(ExportThread * pexport_thread)
|
|
{
|
|
export_thread = pexport_thread;
|
|
}
|
|
|
|
|
|
|
|
void ExportInfo::ReadExportDirs()
|
|
{
|
|
edb->GetExportDirs(export_dirs);
|
|
}
|
|
|
|
|
|
void ExportInfo::ReadConfigVars()
|
|
{
|
|
use_rsa = config->Bool(L"export_rsa", false);
|
|
rsa_key = config->Text(L"export_rsa_key");
|
|
}
|
|
|
|
|
|
ExportDir * ExportInfo::FindDir(long dir_id)
|
|
{
|
|
for(size_t i=0 ; i<export_dirs.size() ; ++i)
|
|
{
|
|
if( dir_id == export_dirs[i].dir_id || system->dirs.HasParent(dir_id, export_dirs[i].dir_id) )
|
|
return &export_dirs[i];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExportInfo::DecodePass(Export & exp)
|
|
{
|
|
if( exp.ftp_pass_type == 0 )
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
/*
|
|
if( system->crypt.RSA(false, rsa_key, exp.ftp_pass_bin, pass_decrypted) )
|
|
{
|
|
PT::UTF8ToWide(pass_decrypted, exp.ftp_pass);
|
|
system->crypt.ClearString(pass_decrypted);
|
|
}
|
|
else
|
|
{
|
|
log << log1 << "Export: I cannot decrypt a password (RSA failed)" << logend;
|
|
return false;
|
|
}
|
|
*/
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
bool ExportInfo::SkipDir(long dir_id, std::wstring & dir)
|
|
{
|
|
if( system->dirs.MakePath(dir_id, tmp_dir) )
|
|
{
|
|
if( !tmp_dir.empty() && IsSubString(tmp_dir, dir) )
|
|
{
|
|
// tmp_dir has a slash at the end
|
|
// we want the slash at the beginning
|
|
dir.erase(0, tmp_dir.size()-1);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
void ExportInfo::ResetRecurrenceCheck()
|
|
{
|
|
recurrence_check.clear();
|
|
}
|
|
|
|
|
|
bool ExportInfo::HasRecurrenceId(long id)
|
|
{
|
|
return (std::find(recurrence_check.begin(), recurrence_check.end(), id) != recurrence_check.end());
|
|
}
|
|
|
|
|
|
void ExportInfo::SendFile(const Item & item, bool thumb)
|
|
{
|
|
ExportDir * exp_dir = FindDir(item.parent_id);
|
|
|
|
if( !exp_dir )
|
|
return;
|
|
|
|
if( HasRecurrenceId(item.id) )
|
|
{
|
|
log << log1 << "Export: recurrence found on item.id: " << item.id << ", url: " << item.url << " (skipping)" << logend;
|
|
return;
|
|
}
|
|
|
|
recurrence_check.push_back(item.id);
|
|
|
|
msg.Clear();
|
|
system->dirs.MakePath(item.parent_id, msg.path);
|
|
msg.path += item.url;
|
|
|
|
if( item.file_type != WINIX_ITEM_FILETYPE_NONE )
|
|
{
|
|
msg.type = WINIX_PL_EXPORT_TYPE_CREATE_FILE_STATIC;
|
|
|
|
if( !system->MakeFilePath(item, msg.url, thumb) )
|
|
{
|
|
log << log1 << "Export: I cannot create a path to a static file, item id: " << item.id << logend;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
msg.type = WINIX_PL_EXPORT_TYPE_CREATE_FILE;
|
|
msg.url = config->url_proto;
|
|
msg.url += config->base_url;// !! IMPROVE ME what about subdomains?
|
|
system->dirs.MakePath(item.parent_id, msg.url, false);
|
|
msg.url += item.url;
|
|
msg.path += L".php"; // !! do konfiga
|
|
}
|
|
|
|
msg.errors = 0;
|
|
|
|
if( SkipDir(exp_dir->dir_id, msg.path) && edb->GetExport(exp_dir->id, exp) )
|
|
{
|
|
if( DecodePass(exp) )
|
|
{
|
|
msg.ftp_login = exp.ftp_login;
|
|
msg.ftp_pass = exp.ftp_pass;
|
|
msg.ftp_server = exp.ftp_server;
|
|
msg.http_server = exp.http_server;
|
|
system->dirs.MakePath(exp_dir->dir_id, msg.src_dir);
|
|
|
|
if( !item.file_path.empty() && thumb ) // !! uzyc file_type
|
|
msg.path.insert(0, L"/download"); // !! do konfiga
|
|
|
|
if( !exp.ftp_dir.empty() )
|
|
msg.path.insert(0, exp.ftp_dir);
|
|
|
|
if( !msg.path.empty() && msg.path[0] != '/' )
|
|
msg.path.insert(0, L"/");
|
|
|
|
export_thread->AddMessage(msg);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ExportInfo::SendDir(const Item & item)
|
|
{
|
|
ExportDir * exp_dir = FindDir(item.id);
|
|
|
|
if( !exp_dir )
|
|
return;
|
|
|
|
if( HasRecurrenceId(item.id) )
|
|
{
|
|
log << log1 << "Export: recurrence found on item.id: " << item.id << ", url: " << item.url << " (skipping)" << logend;
|
|
return;
|
|
}
|
|
|
|
recurrence_check.push_back(item.id);
|
|
|
|
msg.Clear();
|
|
system->dirs.MakePath(item.id, msg.path);
|
|
|
|
msg.type = WINIX_PL_EXPORT_TYPE_CREATE_FILE;
|
|
msg.url = config->url_proto;
|
|
msg.url += config->base_url;// !! IMPROVE ME what about subdomains?
|
|
system->dirs.MakePath(item.id, msg.url, false);
|
|
msg.path += L"index.html"; // !! do konfiga
|
|
|
|
msg.errors = 0;
|
|
|
|
if( SkipDir(exp_dir->dir_id, msg.path) && edb->GetExport(exp_dir->id, exp) )
|
|
{
|
|
if( DecodePass(exp) )
|
|
{
|
|
msg.ftp_login = exp.ftp_login;
|
|
msg.ftp_pass = exp.ftp_pass;
|
|
msg.ftp_server = exp.ftp_server;
|
|
msg.http_server = exp.http_server;
|
|
system->dirs.MakePath(exp_dir->dir_id, msg.src_dir);
|
|
|
|
if( !exp.ftp_dir.empty() )
|
|
msg.path.insert(0, exp.ftp_dir);
|
|
|
|
if( !msg.path.empty() && msg.path[0] != '/' )
|
|
msg.path.insert(0, L"/");
|
|
|
|
export_thread->AddMessage(msg);
|
|
AdditionalExport(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void ExportInfo::SendDir(long dir_id)
|
|
{
|
|
Item * dir = system->dirs.GetDir(dir_id);
|
|
|
|
if( !dir )
|
|
return;
|
|
|
|
SendDir(*dir);
|
|
}
|
|
|
|
|
|
// send all non-static files from a directory
|
|
void ExportInfo::SendAllFilesFromDir(long dir_id)
|
|
{
|
|
iq_dir.SetAll(false, false);
|
|
iq_dir.sel_parent_id = true;
|
|
iq_dir.sel_url = true;
|
|
iq_dir.sel_file = true;
|
|
iq_dir.WhereParentId(dir_id);
|
|
iq_dir.WhereType(Item::file);
|
|
iq_dir.WhereFileType(WINIX_ITEM_FILETYPE_NONE);
|
|
|
|
db->GetItems(dir_items, iq_dir);
|
|
|
|
for(size_t i=0 ; i<dir_items.size() ; ++i)
|
|
{
|
|
SendFile(dir_items[i]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ExportInfo::AdditionalExport(const Item & item)
|
|
{
|
|
for(size_t i=0 ; i<item.meta.spaces.size() ; ++i)
|
|
{
|
|
if( item.meta.spaces[i]->name == L"export" )
|
|
AdditionalExport(item, *item.meta.spaces[i]);
|
|
}
|
|
}
|
|
|
|
|
|
void ExportInfo::AdditionalExport(const Item & item, PT::Space & meta)
|
|
{
|
|
if( meta.ListText(L"additional_export", additional_export) )
|
|
{
|
|
for(size_t i=0 ; i<additional_export.size() ; ++i)
|
|
{
|
|
log << log1 << "exportuje dodatkowo takiego swiniaka: " << additional_export[i] << logend;
|
|
AdditionalExport(additional_export[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void ExportInfo::AdditionalExport(const std::wstring & path)
|
|
{
|
|
int res = system->FollowAllLinks(path, additional_export_dir_tab, additional_export_item);
|
|
|
|
if( res == 0 )
|
|
{
|
|
// directory
|
|
SendDir(*additional_export_dir_tab.back());
|
|
}
|
|
else
|
|
if( res == 1 )
|
|
{
|
|
// file
|
|
// !! uwaga jesli to bedzie plik statyczny (moze dwa razy uzyc raz false i raz true?)
|
|
SendFile(additional_export_item);
|
|
}
|
|
else
|
|
{
|
|
log << log1 << "Export: incorrect path for additional export: " << path << logend;
|
|
}
|
|
}
|
|
|
|
|
|
} // namespace
|
|
|