Item class has been moved to a new directory 'models', a new class has been added: ItemContent

and same fields from Item were moved to ItemContent

Item
- id
- parent_id
- type (file, dir, symlink)
- url
- subject
- template (html template)
- sort_index
- content_id

ItemContent
- id
- ref -> references (renamed)
- user_id
- modification_user_id
- group_id
- privileges
- date_creation
- date_modification
- guest_name
- link_to
- link_redirect
- file_path
- file_fs
- file_type
- file_size
- has_thumb -> file_has_thumb (renamed)
- hash -> file_hash (renamed)
- hash_type -> file_hash_type (renamed)
- content -> content_raw (renamed)
- content_type -> content_raw_type (renamed)
- content_parsed
- content_parsed_type
- meta
- ameta -> meta_admin (renamed)
- modify_index (removed)

WIP: #4
This commit is contained in:
2021-02-24 01:19:47 +01:00
parent 3d7ece15f8
commit 32e93a04c5
118 changed files with 5795 additions and 4514 deletions

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2018, Tomasz Sowa
* Copyright (c) 2010-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -337,14 +337,23 @@ void System::RedirectTo(long item_id, const wchar_t * postfix, bool use_reqtype)
else
{
// item_id is pointing to a file
DbItemQuery iq;
// DbItemQuery iq;
iq.SetAllSel(false);
iq.WhereId(item_id);
iq.sel_parent_id = true;
iq.sel_url = true;
// iq.SetAllSel(false);
// iq.WhereId(item_id);
// iq.sel_parent_id = true;
// iq.sel_url = true;
if( db->GetItem(item_temp, iq) == WINIX_ERR_OK )
morm::Finder<Item> finder(model_connector);
item_temp = finder.
select().
where().
eq(L"id", item_id).
get();
//if( db->GetItem(item_temp, iq) == WINIX_ERR_OK )
if( item_temp.found() )
{
if( dirs.MakePath(item_temp.parent_id, cur->request->redirect_to, false) )
cur->request->redirect_to += item_temp.url;
@@ -528,7 +537,7 @@ bool System::CanChangeUser(const Item & item, long new_user_id)
// super user is allowed everything
return true;
if( item.user_id == -1 || new_user_id == -1 || item.user_id != new_user_id )
if( item.item_content.user_id == -1 || new_user_id == -1 || item.item_content.user_id != new_user_id )
// only super user can change the owner of an item
return false;
@@ -548,7 +557,7 @@ bool System::CanChangeGroup(const Item & item, long new_group_id)
// super user is allowed everything
return true;
if( item.group_id != new_group_id )
if( item.item_content.group_id != new_group_id )
{
// user is allowed to change the group only if he is an owner of the item
// he can change only into a group in which he is a member of, or into a 'no_group'
@@ -556,7 +565,7 @@ bool System::CanChangeGroup(const Item & item, long new_group_id)
if( !cur->session->puser || cur->session->puser->id == -1 )
return false;
if( item.user_id == -1 || cur->session->puser->id != item.user_id )
if( item.item_content.user_id == -1 || cur->session->puser->id != item.item_content.user_id )
return false;
if( new_group_id == -1 )
@@ -582,14 +591,14 @@ bool System::CanChangePrivileges(const Item & item, int new_priv)
// super user is allowed everything
return true;
if( item.privileges != new_priv )
if( item.item_content.privileges != new_priv )
{
// the owner of an item is allowed to change the privileges
if( !cur->session->puser || cur->session->puser->id == -1 )
return false;
if( item.user_id == -1 || cur->session->puser->id != item.user_id )
if( item.item_content.user_id == -1 || cur->session->puser->id != item.item_content.user_id )
return false;
}
@@ -608,27 +617,27 @@ bool System::HasAccess(const Item & item, int mask)
// super user is allowed everything
return true;
if( cur->session->puser && item.user_id != -1 && cur->session->puser->id == item.user_id )
if( cur->session->puser && item.item_content.user_id != -1 && cur->session->puser->id == item.item_content.user_id )
{
// the owner
return ((item.privileges >> 9) & mask) == mask;
return ((item.item_content.privileges >> 9) & mask) == mask;
}
if( cur->session->puser && item.group_id != -1 && cur->session->puser->IsMemberOf(item.group_id) )
if( cur->session->puser && item.item_content.group_id != -1 && cur->session->puser->IsMemberOf(item.item_content.group_id) )
{
// group
return ((item.privileges >> 6) & mask) == mask;
return ((item.item_content.privileges >> 6) & mask) == mask;
}
if( cur->session->puser )
{
// others -- others logged people
return ((item.privileges >> 3) & mask) == mask;
return ((item.item_content.privileges >> 3) & mask) == mask;
}
// guests -- not logged people
return (item.privileges & mask) == mask;
return (item.item_content.privileges & mask) == mask;
}
@@ -656,7 +665,7 @@ bool System::HasReadExecAccess(const Item & item)
{
// there must be at least one 'x' (for the root)
// !! CHECK ME: is it applicable to directories too?
return (item.privileges & 01111) != 0;
return (item.item_content.privileges & 01111) != 0;
}
return HasAccess(item, 5); // r+x
@@ -774,7 +783,7 @@ bool System::CanRemoveRenameChild(const Item & dir, long child_item_user_id)
if( !HasWriteAccess(dir) )
return false;
if( (dir.privileges & 010000) == 0 )
if( (dir.item_content.privileges & 010000) == 0 )
// there is no a sticky bit set to this directory
return true;
@@ -783,10 +792,10 @@ bool System::CanRemoveRenameChild(const Item & dir, long child_item_user_id)
if( cur->session->puser->super_user )
return true;
if( dir.user_id != -1 && cur->session->puser->id != -1 && child_item_user_id != -1 )
if( dir.item_content.user_id != -1 && cur->session->puser->id != -1 && child_item_user_id != -1 )
{
if( cur->session->puser->id == child_item_user_id ||
cur->session->puser->id == dir.user_id )
cur->session->puser->id == dir.item_content.user_id )
return true;
}
}
@@ -867,12 +876,12 @@ return puser->IsMemberOf(group);
// the path depends on parent_id
bool System::CreateNewFileSimpleFs(Item & item)
{
bool res = dirs.MakePath(item.parent_id, item.file_path);
bool res = dirs.MakePath(item.parent_id, item.item_content.file_path);
if( res )
{
if( !item.file_path.empty() && item.file_path[0] == '/' )
item.file_path.erase(0, 1);
if( !item.item_content.file_path.empty() && item.item_content.file_path[0] == '/' )
item.item_content.file_path.erase(0, 1);
}
else
{
@@ -897,7 +906,7 @@ size_t buffer_len = sizeof(buffer)/sizeof(wchar_t);
buffer[0] = '0';
swprintf(buffer+1, buffer_len, L"%lx", (unsigned long)item.id);
item.file_path.clear();
item.item_content.file_path.clear();
// make sure that the length is even
if( (wcslen(hash) & 1) != 0 )
@@ -905,16 +914,16 @@ size_t buffer_len = sizeof(buffer)/sizeof(wchar_t);
for(size_t i=0 ; hash[i] != 0 ; i+=2)
{
item.file_path += hash[i];
item.file_path += hash[i+1];
item.item_content.file_path += hash[i];
item.item_content.file_path += hash[i+1];
if( hash[i+2] != 0 )
item.file_path += '/';
item.item_content.file_path += '/';
}
// one character more to make sure the path is unique
// (we can have a directory without the character)
item.file_path += '_';
item.item_content.file_path += '_';
return true;
}
@@ -939,18 +948,18 @@ bool res;
if( !pmount || pmount->fs != mounts.MountFsHashfs() )
{
res = CreateNewFileSimpleFs(item);
item.file_fs = mounts.MountFsSimplefs();
item.item_content.file_fs = mounts.MountFsSimplefs();
}
else
{
res = CreateNewFileHashFs(item);
item.file_fs = mounts.MountFsHashfs();
item.item_content.file_fs = mounts.MountFsHashfs();
}
if( res )
item.file_path += item.url;
item.item_content.file_path += item.url;
else
item.file_path.clear();
item.item_content.file_path.clear();
return res;
}
@@ -968,7 +977,7 @@ bool System::MakeFilePath(const Item & item, std::wstring & path, bool thumb, bo
return false;
}
if( item.file_path.empty() || item.file_type == WINIX_ITEM_FILETYPE_NONE )
if( item.item_content.file_path.empty() || item.item_content.file_type == WINIX_ITEM_FILETYPE_NONE )
{
log << log1 << "System: MakePath: this item has not a static file" << logend;
return false;
@@ -976,7 +985,7 @@ bool System::MakeFilePath(const Item & item, std::wstring & path, bool thumb, bo
path = config->upload_dir;
if( item.file_fs == mounts.MountFsHashfs() )
if( item.item_content.file_fs == mounts.MountFsHashfs() )
path += L"/hashfs";
else
path += L"/simplefs";
@@ -988,11 +997,11 @@ bool System::MakeFilePath(const Item & item, std::wstring & path, bool thumb, bo
path += L"/normal";
if( create_dir && !CreateDirs(path, item.file_path, chmod, group, true) )
if( create_dir && !CreateDirs(path, item.item_content.file_path, chmod, group, true) )
return false;
path += '/';
path += item.file_path;
path += item.item_content.file_path;
return true;
}
@@ -1025,14 +1034,16 @@ return res;
Error System::AddFile(Item & item, int notify_code, bool call_plugins)
bool System::AddFile(Item & item, int notify_code, bool call_plugins)
{
if( item.type != Item::file )
return WINIX_ERR_FILE_EXPECTED;
Error status = db->AddItem(item);
//Error status = db->AddItem(item);
bool status = item.insert();
if( status == WINIX_ERR_OK )
if( status )
{
log << log2 << "System: added a new file, url: " << item.url << ", id: " << item.id
<< ", parent_id: " << item.parent_id << logend;
@@ -1050,20 +1061,22 @@ return status;
Error System::EditFile(Item & item, bool with_url, int notify_code, bool call_plugins)
bool System::EditFile(Item & item, bool with_url, int notify_code, bool call_plugins)
{
if( item.type != Item::file )
return WINIX_ERR_FILE_EXPECTED;
if( cur->session && cur->session->puser )
cur->request->item.modification_user_id = cur->session->puser->id;
cur->request->item.item_content.modification_user_id = cur->session->puser->id;
else
cur->request->item.modification_user_id = -1;
cur->request->item.item_content.modification_user_id = -1;
item.SetDateModifyToNow();
Error status = db->EditItemById(item, with_url);
item.item_content.SetDateModifyToNow();
bool status = item.update(with_url, true);
//Error status = db->EditItemById(item, with_url);
if( status == WINIX_ERR_OK )
if( status )
{
TemplatesFunctions::pattern_cacher.UpdatePattern(item);
@@ -1184,7 +1197,17 @@ int System::FollowLink(const std::vector<Item*> & current_dir_tab, const std::ws
if( res == 1 )
{
if( db->GetItem(out_dir_tab.back()->id, name_temp, out_item) == WINIX_ERR_OK )
morm::Finder<Item> finder(model_connector);
bool status = finder.
select().
where().
eq(L"parent_id", out_dir_tab.back()->id).
eq(L"url", name_temp).
get(out_item);
//if( db->GetItem(out_dir_tab.back()->id, name_temp, out_item) == WINIX_ERR_OK )
if( status )
return 1;
else
return 2;
@@ -1213,12 +1236,12 @@ bool System::FollowAllLinksDirFound(std::vector<Item*> & out_dir_tab,
return false;
}
if( !out_dir_tab.back()->link_to.empty() )
if( !out_dir_tab.back()->item_content.link_to.empty() )
{
if( follow_dir_default )
{
if( !(stop_on_link_redirect && out_dir_tab.back()->link_redirect==1) )
link_to_temp = out_dir_tab.back()->link_to;
if( !(stop_on_link_redirect && out_dir_tab.back()->item_content.link_redirect==1) )
link_to_temp = out_dir_tab.back()->item_content.link_to;
}
}
@@ -1252,15 +1275,15 @@ bool System::FollowAllLinksFileOrSymlinkFound(std::vector<Item*> & out_dir_tab,
if( out_item.type == Item::symlink )
{
if( out_item.link_to.empty() )
if( out_item.item_content.link_to.empty() )
{
log << log1 << "System: symlink empty" << logend;
return false;
}
else
{
if( !(stop_on_link_redirect && out_item.link_redirect==1) )
link_to_temp = out_item.link_to;
if( !(stop_on_link_redirect && out_item.item_content.link_redirect==1) )
link_to_temp = out_item.item_content.link_to;
}
}
@@ -1392,8 +1415,8 @@ bool System::FollowAllLinks(const std::wstring & link_to,
cur->request->item.Clear();
cur->request->last_item = cur->request->dir_tab.back();
if( !cur->request->dir_tab.back()->link_to.empty() )
RedirectTo(cur->request->dir_tab.back()->link_to);
if( !cur->request->dir_tab.back()->item_content.link_to.empty() )
RedirectTo(cur->request->dir_tab.back()->item_content.link_to);
log << log3 << "System: current directory changed" << logend;
}
@@ -1404,7 +1427,7 @@ bool System::FollowAllLinks(const std::wstring & link_to,
cur->request->last_item = &cur->request->item;
if( cur->request->item.type == Item::symlink )
RedirectTo(cur->request->item.link_to); // cur->request->item.link_to is not empty
RedirectTo(cur->request->item.item_content.link_to); // cur->request->item.item_content.link_to is not empty
log << log3 << "System: current directory changed and the new file loaded" << logend;
}
@@ -1459,25 +1482,35 @@ bool System::AddCommonFileToVar(const wchar_t * file_path, const wchar_t * url,
return false;
}
if( db->GetItem(var->id, url, file_content_item) == WINIX_ERR_OK )
morm::Finder<Item> finder(model_connector);
bool status = finder.select().where().eq(L"parent_id", var->id).eq(L"url", url).get(file_content_item);
//if( db->GetItem(var->id, url, file_content_item) == WINIX_ERR_OK )
if( status )
{
if( overwrite_existing )
db->DelItem(file_content_item);
{
file_content_item.remove();
//db->DelItem(file_content_item);
}
else
{
return true;
}
}
file_content_item.Clear();
file_content_item.parent_id = var->id;
file_content_item.user_id = var->user_id;
file_content_item.group_id = var->group_id;
file_content_item.privileges = 07555; // !! IMPROVE ME: may it should be added as a parameter to this function?
file_content_item.item_content.user_id = var->item_content.user_id;
file_content_item.item_content.group_id = var->item_content.group_id;
file_content_item.item_content.privileges = 07555; // !! IMPROVE ME: may it should be added as a parameter to this function?
file_content_item.subject = url;
file_content_item.url = url;
file_content_item.type = Item::file;
file_content_item.html_template = config->templates_index_raw;
file_content_item.content = file_content;
file_content_item.content_type = Item::ct_other;
file_content_item.item_content.content_raw = file_content;
file_content_item.item_content.content_raw_type = ItemContent::ct_other;
return AddFile(file_content_item, false) == WINIX_ERR_OK;
}