205 lines
4.1 KiB
C++
Executable File
205 lines
4.1 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 <cstdio>
|
|
#include <errno.h>
|
|
#include "content.h"
|
|
#include "../core/request.h"
|
|
#include "../core/error.h"
|
|
#include "../core/data.h"
|
|
|
|
|
|
|
|
bool Content::RemoveCheckAccess()
|
|
{
|
|
if( !request.is_item )
|
|
{
|
|
if( !request.CanRemove(*request.dir_table.back()) )
|
|
{
|
|
request.status = WINIX_ERR_PERMISSION_DENIED;
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
if( !request.CanRemove(request.item) )
|
|
{
|
|
request.status = WINIX_ERR_PERMISSION_DENIED;
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
void Content::RemoveAuthPrepareQuery()
|
|
{
|
|
rm_auth_iq.SetAll(true, false);
|
|
|
|
rm_auth_iq.sel_parent_id = true;
|
|
rm_auth_iq.sel_type = true;
|
|
rm_auth_iq.sel_auth = true;
|
|
|
|
rm_auth_iq.WhereType(Item::file);
|
|
rm_auth_iq.WhereAuth(Item::auth_none, false);
|
|
}
|
|
|
|
|
|
|
|
void Content::RemoveAllDirs(long dir_id)
|
|
{
|
|
DirContainer::ParentIterator pnext, p = data.dirs.FindFirstParent(dir_id);
|
|
|
|
for( ; p != data.dirs.ParentEnd() ; p = pnext )
|
|
{
|
|
// this iterator p will be deleted by the next DeleteDir(p->second->id)
|
|
// (the next iterator we must calculate beforehand)
|
|
pnext = data.dirs.NextParent(p);
|
|
RemoveAllDirs(p->second->id);
|
|
}
|
|
|
|
rm_auth_iq.WhereParentId(dir_id);
|
|
db.GetItems(request.item_table, rm_auth_iq);
|
|
|
|
for(size_t i=0 ; i<request.item_table.size() ; ++i)
|
|
RemoveAuth(request.item_table[i]);
|
|
|
|
if( db.DelDirById(dir_id) == WINIX_ERR_OK )
|
|
{
|
|
data.dirs.DelDir(dir_id);
|
|
|
|
db.RemoveThread(dir_id);
|
|
db.RemoveTicket(dir_id);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Content::RemoveAllDirs()
|
|
{
|
|
RemoveAuthPrepareQuery();
|
|
|
|
// this method deletes recursively all directories
|
|
RemoveAllDirs(request.dir_table.back()->id);
|
|
request.dir_table.erase(--request.dir_table.end());
|
|
|
|
if( request.dir_table.empty() )
|
|
{
|
|
// we have deleted the root directory
|
|
data.dirs.CheckRootDir(); // adding a new root dir
|
|
Item * proot = data.dirs.GetRootDir();
|
|
|
|
if( proot )
|
|
request.dir_table.push_back(proot);
|
|
else
|
|
// there is no a root dir
|
|
// CheckRootDir() didn't add the root dir (probably problem with the database)
|
|
// make sure that Content::Make() will check that the dir_table is empty and returns
|
|
return;
|
|
}
|
|
|
|
RedirectToLastDir();
|
|
}
|
|
|
|
|
|
void Content::RemoveDir()
|
|
{
|
|
if( request.param_table.empty() )
|
|
request.status = WINIX_ERR_PERMISSION_DENIED; // use parameter "r" for removing a directory
|
|
else
|
|
if( request.IsParam("r") )
|
|
RemoveAllDirs();
|
|
else
|
|
request.status = WINIX_ERR_UNKNOWN_PARAM;
|
|
}
|
|
|
|
|
|
|
|
void Content::RemoveAuth(Item & item)
|
|
{
|
|
if( item.auth_path.empty() )
|
|
{
|
|
log << log1 << "Content: can't remove a static file: auth_path is empty" << logend;
|
|
return;
|
|
}
|
|
|
|
if( remove(item.auth_path.c_str()) == 0 )
|
|
{
|
|
log << log1 << "Content: removed static file: " << item.auth_path << logend;
|
|
item.auth_path.clear();
|
|
item.auth = Item::auth_none;
|
|
// we don't store it to db (will be removed or is removed already)
|
|
}
|
|
else
|
|
{
|
|
int err = errno;
|
|
|
|
log << log1 << "Content: can't remove a file: " << item.auth_path;
|
|
log.SystemErr(err);
|
|
log << logend;
|
|
|
|
request.status = WINIX_ERR_PERMISSION_DENIED;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Content::RemoveFile()
|
|
{
|
|
// for safety we check if param_table is empty
|
|
// a user can use "confirm" but can make a mistake when typing
|
|
if( !request.param_table.empty() )
|
|
{
|
|
request.status = WINIX_ERR_UNKNOWN_PARAM;
|
|
return;
|
|
}
|
|
|
|
|
|
if( db.DelItem( request.item ) )
|
|
{
|
|
log << log2 << "Content: deleted item: subject: " << request.item.subject << ", id: " << request.item.id << logend;
|
|
TemplatesFunctions::pattern_cacher.DeletePattern(request.item);
|
|
|
|
if( data.mounts.pmount->type == Mount::thread )
|
|
db.EditThreadRemoveItem(request.item.parent_id);
|
|
else
|
|
if( data.mounts.pmount->type == Mount::ticket )
|
|
db.EditTicketRemoveItem(request.item.id);
|
|
|
|
if( request.item.auth != Item::auth_none )
|
|
RemoveAuth(request.item);
|
|
}
|
|
else
|
|
{
|
|
request.status = WINIX_ERR_NO_ITEM;
|
|
}
|
|
|
|
RedirectToLastDir();
|
|
}
|
|
|
|
|
|
|
|
void Content::FunRm()
|
|
{
|
|
if( !RemoveCheckAccess() )
|
|
return;
|
|
|
|
if( request.IsParam("confirm") )
|
|
return; // show confirmation dialog
|
|
|
|
if( request.is_item )
|
|
RemoveFile();
|
|
else
|
|
RemoveDir();
|
|
}
|
|
|
|
|
|
|