221 lines
4.4 KiB
C++
Executable File
221 lines
4.4 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2010, 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 "functions/functions.h"
|
|
#include "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;
|
|
|
|
// we must know where to store the file
|
|
if( !system->mounts.pmount )
|
|
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( request->session->puser && request->session->puser->super_user )
|
|
// super user can use upload everywhere
|
|
return true;
|
|
|
|
if( !system->HasWriteAccess(item) )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Upload::HasAccess()
|
|
{
|
|
if( request->is_item || !HasAccess(*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) )
|
|
{
|
|
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;
|
|
request->status = WINIX_ERR_PERMISSION_DENIED;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
void Upload::UploadFile(Item & item, const std::wstring & tmp_filename)
|
|
{
|
|
// we should add the file beforehand to get the proper item.id
|
|
request->status = system->AddFile(item);
|
|
|
|
if( request->status == WINIX_ERR_OK )
|
|
{
|
|
if( system->CreateNewFile(item) )
|
|
{
|
|
if( UploadSaveStaticFile(item, tmp_filename) )
|
|
{
|
|
request->status = db->EditFileById(item, item.id);
|
|
|
|
if( item.file_type == WINIX_ITEM_FILETYPE_IMAGE && config->create_thumb )
|
|
system->thumb.CreateThumb(item, config->thumb_cx, config->thumb_cy, config->thumb_mode);
|
|
}
|
|
else
|
|
{
|
|
db->DelItem(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool Upload::FunUploadCheckAbuse()
|
|
{
|
|
if( !system->rebus.CheckRebus() )
|
|
{
|
|
request->status = WINIX_ERR_INCORRECT_REBUS;
|
|
return false;
|
|
}
|
|
|
|
functions->CheckGetPostTimes(4);
|
|
|
|
if( request->session->spam_score > 0 )
|
|
{
|
|
request->status = WINIX_ERR_SPAM;
|
|
log << log1 << "Content: ignoring due to suspected spamming" << logend;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Upload::UploadMulti()
|
|
{
|
|
request->item.Clear(); // clearing and setting date
|
|
request->item.parent_id = request->dir_tab.back()->id;
|
|
request->item.type = Item::file;
|
|
request->item.privileges = 0644; // !! tymczasowo
|
|
functions->SetUser(request->item);
|
|
|
|
PostFileTab::iterator i = request->post_file_tab.begin();
|
|
|
|
for( ; i != request->post_file_tab.end() ; ++i)
|
|
{
|
|
const wchar_t * file_name = i->second.filename.c_str();
|
|
|
|
request->item.subject = file_name;
|
|
request->item.url = file_name;
|
|
request->item.file_type = SelectFileType(file_name);
|
|
|
|
functions->PrepareUrl(request->item);
|
|
UploadFile(request->item, i->second.tmp_filename);
|
|
}
|
|
|
|
system->RedirectToLastDir();
|
|
}
|
|
|
|
|
|
void Upload::UploadSingle()
|
|
{
|
|
const std::wstring & new_subject = request->PostVar(L"subject");
|
|
const std::wstring & new_url = request->PostVar(L"url");
|
|
bool has_subject = !new_subject.empty();
|
|
bool has_url = !new_url.empty();
|
|
|
|
functions->ReadItem(request->item, Item::file); // ReadItem() changes the url if it is empty
|
|
functions->SetUser(request->item);
|
|
request->item.privileges = 0644; // !! tymczasowo
|
|
|
|
const wchar_t * file_name = request->post_file_tab.begin()->second.filename.c_str();
|
|
request->item.file_type = SelectFileType(file_name);
|
|
|
|
if( !has_subject )
|
|
request->item.subject = file_name;
|
|
|
|
if( !has_url )
|
|
{
|
|
request->item.url = file_name;
|
|
functions->PrepareUrl(request->item);
|
|
}
|
|
|
|
UploadFile(request->item, request->post_file_tab.begin()->second.tmp_filename);
|
|
|
|
if( request->status == WINIX_ERR_OK )
|
|
system->RedirectTo(request->item, L"/cat");
|
|
}
|
|
|
|
|
|
|
|
// !! dodac usuwanie plikow statycznych przez rm
|
|
void Upload::MakePost()
|
|
{
|
|
if( request->post_file_tab.empty() )
|
|
{
|
|
request->status = WINIX_ERR_PERMISSION_DENIED;
|
|
return;
|
|
}
|
|
|
|
if( !FunUploadCheckAbuse() )
|
|
return;
|
|
|
|
if( request->post_file_tab.size() > 1 )
|
|
UploadMulti();
|
|
else
|
|
UploadSingle();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|