341 lines
7.6 KiB
C++
Executable File
341 lines
7.6 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2012, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <cstdio>
|
|
#include <errno.h>
|
|
#include "upload.h"
|
|
#include "core/misc.h"
|
|
#include "core/plugin.h"
|
|
#include "functions/functions.h"
|
|
#include "templates/templates.h"
|
|
#include "utf8/utf8.h"
|
|
|
|
|
|
|
|
namespace Fun
|
|
{
|
|
|
|
Upload::Upload()
|
|
{
|
|
fun.url = L"upload";
|
|
}
|
|
|
|
|
|
|
|
bool Upload::HasAccess(const Item & item)
|
|
{
|
|
// you can use 'upload' only in a directory
|
|
if( item.type != Item::dir )
|
|
return false;
|
|
|
|
if( config->upload_dir.empty() )
|
|
{
|
|
log << log1 << "Request: can't use upload function, upload_dir must be set in the config file" << logend;
|
|
return false;
|
|
}
|
|
|
|
if( cur->session->puser && cur->session->puser->super_user )
|
|
// super user can use upload everywhere
|
|
return true;
|
|
|
|
if( !system->HasWriteAccess(item) )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Upload::HasAccess()
|
|
{
|
|
if( cur->request->is_item || !HasAccess(*cur->request->dir_tab.back()) )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Upload::UploadSaveStaticFile(const Item & item, const std::wstring & tmp_filename)
|
|
{
|
|
if( !system->MakeFilePath(item, path, false, true, config->upload_dirs_chmod) )
|
|
{
|
|
cur->request->status = WINIX_ERR_PERMISSION_DENIED;
|
|
return false;
|
|
}
|
|
|
|
if( RenameFile(tmp_filename, path) )
|
|
{
|
|
log << log1 << "Upload: uploaded a new file: " << path << logend;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
log << log1 << "Upload: can't move the tmp file from: " << tmp_filename << ", to: " << path << logend;
|
|
cur->request->status = WINIX_ERR_PERMISSION_DENIED;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Upload::ResizeImage(Item & item)
|
|
{
|
|
::Mount * m = system->mounts.CalcMount(item.parent_id);
|
|
size_t cx = config->image_cx;
|
|
size_t cy = config->image_cy;
|
|
int mode = config->image_mode;
|
|
int quality = config->image_quality;
|
|
|
|
// reading width and height from the mount point (if exists)
|
|
int index = system->mounts.MountParImageSize();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 2 )
|
|
{
|
|
cx = Tol(m->param[index].arg[0]);
|
|
cy = Tol(m->param[index].arg[1]);
|
|
}
|
|
|
|
// reading image mode from the mount point (if exists)
|
|
index = system->mounts.MountParImageMode();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
|
mode = Toi(m->param[index].arg[0]);
|
|
|
|
// reading image quality from the mount point (if exists)
|
|
index = system->mounts.MountParImageQuality();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
|
quality = Toi(m->param[index].arg[0]);
|
|
|
|
system->image.Resize(item, cx, cy, mode, quality);
|
|
}
|
|
|
|
|
|
|
|
void Upload::CreateThumb(Item & item)
|
|
{
|
|
::Mount * m = system->mounts.CalcMount(item.parent_id);
|
|
size_t cx = config->thumb_cx;
|
|
size_t cy = config->thumb_cy;
|
|
int mode = config->thumb_mode;
|
|
int quality = config->thumb_quality;
|
|
|
|
// reading width and height from the mount point (if exists)
|
|
int index = system->mounts.MountParThumbSize();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 2 )
|
|
{
|
|
cx = Tol(m->param[index].arg[0]);
|
|
cy = Tol(m->param[index].arg[1]);
|
|
}
|
|
|
|
// reading thumb mode from the mount point (if exists)
|
|
index = system->mounts.MountParThumbMode();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
|
mode = Toi(m->param[index].arg[0]);
|
|
|
|
// reading image quality from the mount point (if exists)
|
|
index = system->mounts.MountParThumbQuality();
|
|
|
|
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
|
quality = Toi(m->param[index].arg[0]);
|
|
|
|
system->image.CreateThumb(item, cx, cy, mode, quality);
|
|
}
|
|
|
|
|
|
|
|
void Upload::UploadFile(Item & item, const std::wstring & tmp_filename)
|
|
{
|
|
// we should add the file beforehand to get the proper item.id
|
|
cur->request->status = system->AddFile(item, 0, false);
|
|
|
|
if( cur->request->status == WINIX_ERR_OK )
|
|
{
|
|
if( system->CreateNewFile(item) )
|
|
{
|
|
if( UploadSaveStaticFile(item, tmp_filename) )
|
|
{
|
|
cur->request->status = db->EditFileById(item, item.id);
|
|
|
|
plugin.Call(WINIX_FILE_ADDED, &item);
|
|
|
|
if( item.file_type == WINIX_ITEM_FILETYPE_IMAGE )
|
|
{
|
|
if( config->image_resize )
|
|
ResizeImage(item);
|
|
|
|
if( config->create_thumb )
|
|
CreateThumb(item);
|
|
}
|
|
|
|
if( is_jquery_upload )
|
|
cur->request->item_tab.push_back(item);
|
|
}
|
|
else
|
|
{
|
|
db->DelItem(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool Upload::FunUploadCheckAbuse()
|
|
{
|
|
if( !system->rebus.CheckRebus() )
|
|
{
|
|
cur->request->status = WINIX_ERR_INCORRECT_REBUS;
|
|
return false;
|
|
}
|
|
|
|
functions->CheckGetPostTimes(4);
|
|
|
|
if( cur->session->spam_score > 0 )
|
|
{
|
|
cur->request->status = WINIX_ERR_SPAM;
|
|
log << log1 << "Content: ignoring due to suspected spamming" << logend;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Upload::UploadMulti()
|
|
{
|
|
cur->request->item.Clear(); // clearing and setting date
|
|
cur->request->item.parent_id = cur->request->dir_tab.back()->id;
|
|
cur->request->item.type = Item::file;
|
|
cur->request->item.privileges = system->NewFilePrivileges();
|
|
functions->SetUser(cur->request->item);
|
|
|
|
PostFileTab::iterator i = cur->request->post_file_tab.begin();
|
|
|
|
for( ; i != cur->request->post_file_tab.end() ; ++i)
|
|
{
|
|
const wchar_t * file_name = i->second.filename.c_str();
|
|
|
|
cur->request->item.subject = file_name;
|
|
cur->request->item.url = file_name;
|
|
cur->request->item.file_type = SelectFileType(file_name);
|
|
cur->request->item.file_size = i->second.file_size;
|
|
|
|
functions->PrepareUrl(cur->request->item);
|
|
UploadFile(cur->request->item, i->second.tmp_filename);
|
|
i->second.tmp_filename.clear();
|
|
}
|
|
|
|
if( is_jquery_upload )
|
|
CreateJSON();
|
|
else
|
|
system->RedirectToLastDir();
|
|
}
|
|
|
|
|
|
void Upload::UploadSingle()
|
|
{
|
|
const std::wstring & new_subject = cur->request->PostVar(L"subject");
|
|
const std::wstring & new_url = cur->request->PostVar(L"url");
|
|
bool has_subject = !new_subject.empty();
|
|
bool has_url = !new_url.empty();
|
|
|
|
functions->ReadItem(cur->request->item, Item::file); // ReadItem() changes the url if it is empty
|
|
functions->SetUser(cur->request->item);
|
|
cur->request->item.privileges = system->NewFilePrivileges();
|
|
|
|
PostFile & post_file = cur->request->post_file_tab.begin()->second;
|
|
|
|
const wchar_t * file_name = post_file.filename.c_str();
|
|
cur->request->item.file_type = SelectFileType(file_name);
|
|
cur->request->item.file_size = post_file.file_size;
|
|
|
|
if( !has_subject )
|
|
cur->request->item.subject = file_name;
|
|
|
|
if( !has_url )
|
|
{
|
|
cur->request->item.url = file_name;
|
|
functions->PrepareUrl(cur->request->item);
|
|
}
|
|
|
|
UploadFile(cur->request->item, post_file.tmp_filename);
|
|
post_file.tmp_filename.clear();
|
|
|
|
if( is_jquery_upload )
|
|
CreateJSON();
|
|
else
|
|
if( cur->request->status == WINIX_ERR_OK )
|
|
system->RedirectTo(cur->request->item, L"/cat");
|
|
}
|
|
|
|
|
|
|
|
|
|
void Upload::MakePost()
|
|
{
|
|
cur->request->item_tab.clear();
|
|
is_jquery_upload = cur->request->IsParam(L"jquery_upload");
|
|
|
|
if( cur->request->post_file_tab.empty() )
|
|
{
|
|
cur->request->status = WINIX_ERR_PERMISSION_DENIED;
|
|
return;
|
|
}
|
|
|
|
if( !FunUploadCheckAbuse() )
|
|
return;
|
|
|
|
if( cur->request->post_file_tab.size() > 1 )
|
|
UploadMulti();
|
|
else
|
|
UploadSingle();
|
|
}
|
|
|
|
|
|
|
|
void Upload::CreateJSON()
|
|
{
|
|
size_t loc = TemplatesFunctions::locale.GetCurLang();
|
|
Ezc::Pattern * pat = TemplatesFunctions::patterns.Get(template_index, loc);
|
|
|
|
if( pat )
|
|
{
|
|
templates->Generate(*pat);
|
|
cur->request->page_generated = true;
|
|
cur->request->use_html_filter = false;
|
|
}
|
|
}
|
|
|
|
|
|
void Upload::MakeGet()
|
|
{
|
|
if( cur->request->IsParam(L"jquery_upload") )
|
|
{
|
|
query.Clear();
|
|
query.WhereParentId(cur->request->dir_tab.back()->id);
|
|
query.WhereType(Item::file);
|
|
query.WhereFileType(WINIX_ITEM_FILETYPE_NONE, false);
|
|
|
|
db->GetItems(cur->request->item_tab, query);
|
|
|
|
CreateJSON();
|
|
}
|
|
}
|
|
|
|
|
|
} // namespace
|