diff --git a/content/Makefile b/content/Makefile index e1b0940..393d884 100755 --- a/content/Makefile +++ b/content/Makefile @@ -1,4 +1,4 @@ -o = content.o privileges.o emacs.o login.o rm.o cat.o logout.o ls.o node.o mkdir.o +o = content.o privileges.o emacs.o login.o rm.o cat.o logout.o ls.o node.o mkdir.o defaultitem.o all: content.a diff --git a/content/Makefile.dep b/content/Makefile.dep index a657ac2..d7fab05 100755 --- a/content/Makefile.dep +++ b/content/Makefile.dep @@ -23,6 +23,19 @@ content.o: ../core/cookieparser.h ../core/item.h ../core/db.h ../core/error.h content.o: ../core/user.h ../core/group.h ../core/users.h ../core/groups.h content.o: ../core/log.h ../core/misc.h ../core/function.h content.o: ../core/functionparser.h ../core/request.h ../core/data.h +defaultitem.o: content.h ../templates/templates.h ../../ezc/src/ezc.h +defaultitem.o: ../core/data.h ../core/misc.h ../core/log.h ../core/item.h +defaultitem.o: ../core/error.h ../core/dirs.h ../core/db.h +defaultitem.o: ../core/dircontainer.h ../core/user.h ../core/group.h +defaultitem.o: ../core/ugcontainer.h ../core/users.h ../core/groups.h +defaultitem.o: ../core/functions.h ../core/function.h ../core/request.h +defaultitem.o: ../core/requesttypes.h ../core/session.h ../core/done.h +defaultitem.o: ../core/getparser.h ../core/httpsimpleparser.h +defaultitem.o: ../core/postparser.h ../core/cookieparser.h ../core/item.h +defaultitem.o: ../core/db.h ../core/error.h ../core/user.h ../core/group.h +defaultitem.o: ../core/users.h ../core/groups.h ../core/log.h ../core/misc.h +defaultitem.o: ../core/function.h ../core/functionparser.h ../core/request.h +defaultitem.o: ../core/data.h emacs.o: content.h ../templates/templates.h ../../ezc/src/ezc.h emacs.o: ../core/data.h ../core/misc.h ../core/log.h ../core/item.h emacs.o: ../core/error.h ../core/dirs.h ../core/db.h ../core/dircontainer.h diff --git a/content/content.cpp b/content/content.cpp index 9b394cf..7817ad8 100755 --- a/content/content.cpp +++ b/content/content.cpp @@ -102,6 +102,9 @@ void Content::MakeStandardFunction() if( request.pfunction->code == Function::mkdir ) FunMkdir(); else + if( request.pfunction->code == Function::default_item ) + FunDefaultItem(); + else if( request.pfunction->code == Function::privileges ) FunPrivileges(); else @@ -136,6 +139,10 @@ void Content::MakePost() PostFunMkdir(); break; + case Function::default_item: + PostFunDefaultItem(); + break; + case Function::privileges: PostFunPrivileges(); break; diff --git a/content/content.h b/content/content.h index 2ba751b..e7e87ae 100755 --- a/content/content.h +++ b/content/content.h @@ -51,12 +51,15 @@ class Content void FunRm(); void FunNode(); void FunMkdir(); + void FunDefaultItem(); + void PostFunLogin(); void PostFunEmacsAdd(); void PostFunEmacsEdit(bool with_url); void PostFunEmacs(); void PostFunMkdir(); + void PostFunDefaultItem(); void PostFunPrivileges(); void RedirectTo(const Item & item); diff --git a/content/defaultitem.cpp b/content/defaultitem.cpp new file mode 100755 index 0000000..34c3ce9 --- /dev/null +++ b/content/defaultitem.cpp @@ -0,0 +1,81 @@ +/* + * This file is a part of CMSLU -- Content Management System like Unix + * and is not publicly distributed + * + * Copyright (c) 2008, Tomasz Sowa + * All rights reserved. + * + */ + +#include "content.h" + + + + + + +void Content::PostFunDefaultItem() +{ +Item * pdir; +long fileid = -1; + + request.session->done = Done::defaulted_dir; + + + if( !request.HasWriteAccess(*request.dir_table.back()) ) + { + request.status = Error::permision_denied; + return; + } + + try + { + std::string & path = request.PostVar("defaultitem"); + + if( !path.empty() ) + { + std::string dir, file; + Dirs::SplitPath(path, dir, file); + + pdir = data.dirs.GetDir(dir); + + if( !pdir ) + throw Error(Error::incorrect_dir); + + fileid = db.GetFileId(pdir->id, file); + + if( fileid == -1 ) + throw Error(Error::db_no_item); + } + + request.session->done_status = db.EditDefaultItem(request.dir_table.back()->id, fileid); + + if( request.session->done_status == Error::ok ) + request.dir_table.back()->default_item = fileid; + } + catch(const Error & e) + { + request.session->done_status = e; + } + + + if( request.session->done_status == Error::ok ) + RedirectTo(*request.dir_table.back()); + else + log << log1 << "Content: PostFunDefaultItem: Error: " << request.session->done_status << logend; +} + + + + +void Content::FunDefaultItem() +{ + if( !request.HasWriteAccess(*request.dir_table.back()) ) + { + request.status = Error::permision_denied; + return; + } + +} + + diff --git a/core/db.cpp b/core/db.cpp index bee1805..446b550 100755 --- a/core/db.cpp +++ b/core/db.cpp @@ -502,7 +502,7 @@ Error Db::EditItemInItem(Item & item, bool with_subject) { AssertConnection(); std::ostringstream query; - query << "update core.item set (user_id, group_id, privileges, type, parent_id"; + query << "update core.item set (user_id, group_id, privileges, type, default_item, parent_id"; if( with_subject ) query << ", url"; @@ -512,6 +512,7 @@ Error Db::EditItemInItem(Item & item, bool with_subject) query << '\'' << item.group_id << "', "; query << '\'' << item.privileges << "', "; query << '\'' << static_cast(item.type) << "', "; + query << '\'' << item.default_item << "', "; query << '\'' << item.parent_id << "' "; if( with_subject ) @@ -617,6 +618,7 @@ Error Db::EditItemGetContentId(Item & item) { AssertConnection(); std::ostringstream query; + // !! tutaj chyba nie ma potrzeby robic left join z core.content (nie uzywamy nic z tamtej tabeli) query << "select content_id from core.item left join core.content on item.content_id = content.id where item.id='"; query << item.id << "';"; @@ -644,11 +646,17 @@ return result; // !! moze nazwa poprostu EditItem (nie trzeba tego ById) ? (sprawdzic czy nie koliduje z inna nazwa) Error Db::EditItemById(Item & item, bool with_subject) { - Error result = EditItemGetContentId(item); + Error result = Error::ok; + + // !! dla katalogow nie testowane jeszcze + + if( item.type == Item::file ) + result = EditItemGetContentId(item); if( result == Error::ok ) { - result = EditItemInContent(item); + if( item.type == Item::file ) + result = EditItemInContent(item); if( result == Error::ok ) result = EditItemInItem(item, with_subject); @@ -660,6 +668,7 @@ return result; // item.url and item.parent_id must be set +// doesn't work with directiories Error Db::EditItemByUrl(Item & item, bool with_subject) { Error result = EditItemGetId(item); @@ -677,6 +686,43 @@ return result; +Error Db::EditDefaultItem(long id, long new_default_item) +{ + PGresult * r = 0; + Error result = Error::ok; + + try + { + AssertConnection(); + std::ostringstream query; + query << "update core.item set (default_item) = ('" << new_default_item << "') where id='" << id << "';"; + + r = AssertQuery(query.str()); + AssertResultStatus(r, PGRES_COMMAND_OK); + + char * rows_str = PQcmdTuples(r); + long rows = 0; + + if( rows_str ) + rows = atol(rows_str); + + if( rows == 0 ) + { + result = Error::db_no_item; + log << log1 << "Db: EditDefaultItem: no such item, id: " << id << logend; + } + } + catch(const Error & e) + { + result = e; + } + + ClearResult(r); + +return result; +} + + PGresult * Db::GetItemsQuery(Item & item_ref) @@ -806,6 +852,55 @@ return result; } +long Db::GetItemId(long parent_id, const std::string & url, Item::Type type) +{ + PGresult * r = 0; + long result = -1; + + try + { + AssertConnection(); + + std::ostringstream query; + query << "select id from core.item where type='" << static_cast(type) << "' and item.parent_id='"; + query << parent_id << "' and item.url='" << Escape(url) << "';"; + + r = AssertQuery( query.str() ); + AssertResultStatus(r, PGRES_TUPLES_OK); + + int rows = PQntuples(r); + + if( rows == 1 ) + result = atol( AssertValue(r, 0, 0) ); + } + catch(const Error & e) + { + result = e; + } + + ClearResult(r); + + +return result; +} + + + +long Db::GetFileId(long parent_id, const std::string & url) +{ + return GetItemId(parent_id, url, Item::file); +} + + +long Db::GetDirId(long parent_id, const std::string & url) +{ + return GetItemId(parent_id, url, Item::dir); +} + + + + + bool Db::GetPriv(Item & item, long id) { bool result = false; diff --git a/core/db.h b/core/db.h index 9d3c07e..26270a0 100755 --- a/core/db.h +++ b/core/db.h @@ -35,6 +35,11 @@ public: Db(); ~Db(); + + // !! przerobic tak aby GetItem zwracalo wszystkie pozycja + // !! GetFile tylko dla plikow + // !! GetDir tylko dla katalogow + // !! GetFile i GetDir beda uzywac GetItem void Init(const std::string & database, const std::string & user, const std::string & pass); bool CheckUser(std::string & login, std::string & password, long & user_id); @@ -54,7 +59,11 @@ public: // !! nowy interfejs Error GetItem(long parent_id, const std::string & url, Item & item); - + Error EditDefaultItem(long id, long new_default_item); + + long GetItemId(long parent_id, const std::string & url, Item::Type type); + long GetFileId(long parent_id, const std::string & url); + long GetDirId(long parent_id, const std::string & url); protected: diff --git a/core/dirs.cpp b/core/dirs.cpp index d38c845..e3369c7 100755 --- a/core/dirs.cpp +++ b/core/dirs.cpp @@ -343,3 +343,32 @@ void Dirs::AddDir(const Item & item) dir_table.PushBack(item); } + + + + +void Dirs::SplitPath(const std::string & path, std::string & dir, std::string & file) +{ + std::string::size_type i; + + dir.clear(); + file.clear(); + + if( path.empty() ) + // !! moze dir ustawic na '/' ? + return; + + for( i=path.size()-1 ; i>0 && path[i]!='/' ; --i); + + if( path[i] != '/' ) + { + // we do not have any slashes '/' + file = path; + return; + } + + dir.assign(path, 0, i + 1); // +1 means with a slash at the end + + if( i < path.size() - 1 ) + file.assign(path, i+1, path.size() - i - 1); +} diff --git a/core/dirs.h b/core/dirs.h index 8275565..98038bb 100755 --- a/core/dirs.h +++ b/core/dirs.h @@ -82,6 +82,9 @@ public: Item * GetDir(long id); void AddDir(const Item & item); + + + static void SplitPath(const std::string & path, std::string & dir, std::string & file); }; diff --git a/core/done.h b/core/done.h index 8653e0a..8cbae1c 100755 --- a/core/done.h +++ b/core/done.h @@ -25,6 +25,7 @@ public: deleted_item, privileged_item, added_dir, + defaulted_dir, loggedout }; diff --git a/core/function.h b/core/function.h index 1ab6155..1f85b72 100755 --- a/core/function.h +++ b/core/function.h @@ -30,6 +30,7 @@ public: node, emacs, mkdir, + default_item, privileges, rm, login, diff --git a/core/functions.cpp b/core/functions.cpp index cdc5a52..cf2f22a 100755 --- a/core/functions.cpp +++ b/core/functions.cpp @@ -55,6 +55,10 @@ void Functions::ReadFunctions() f.item.url = "mkdir"; table.insert( std::make_pair(f.item.url, f) ); + f.code = Function::default_item; + f.item.url = "default"; + table.insert( std::make_pair(f.item.url, f) ); + f.code = Function::privileges; f.item.url = "uprawnienia"; table.insert( std::make_pair(f.item.url, f) ); diff --git a/templates/dir.cpp b/templates/dir.cpp index 6bc14e7..5d8a6c4 100755 --- a/templates/dir.cpp +++ b/templates/dir.cpp @@ -96,6 +96,60 @@ void dir_tab_link(Info & i) +static Item dir_last_default_item; +static size_t dir_last_default_item_reqid = 0; + + + +void dir_last_default_item_init() +{ + if( dir_last_default_item_reqid == request.id ) + return; + + dir_last_default_item_reqid = request.id; + + dir_last_default_item.Clear(); + log << "inicjalizacja dir_last_default_item" << logend; + + if( request.dir_table.empty() ) + return; + + long default_item = request.dir_table.back()->default_item; + + if( default_item == -1 ) + return; + + std::vector item_table; // !! tymczasowo, nie bedzie tego po zmianie interfejsu dla Db + db.GetItem(item_table, default_item); + + if( item_table.empty() ) + return; + + dir_last_default_item = item_table[0]; +} + + + +void dir_last_default_item_dir(Info & i) +{ + dir_last_default_item_init(); + + std::string path; + + if( dir_last_default_item.parent_id != -1 ) + if( data.dirs.MakePath(dir_last_default_item.parent_id, path) ) + HtmlEscape(i.out, path); +} + + +void dir_last_default_item_url(Info & i) +{ + dir_last_default_item_init(); + + HtmlEscape(i.out, dir_last_default_item.url); +} + + } // namespace TemplatesFunctions diff --git a/templates/done.cpp b/templates/done.cpp index 121bf70..d9ad0ce 100755 --- a/templates/done.cpp +++ b/templates/done.cpp @@ -27,6 +27,12 @@ void done_status(Info & i) } +void done_status_no_item(Info & i) +{ + i.result = request.session->done_status == Error::db_no_item; +} + + void done_status_incorrect_dir(Info & i) { i.result = request.session->done_status == Error::incorrect_dir; @@ -75,6 +81,12 @@ void done_added_dir(Info & i) +void done_defaulted_dir(Info & i) +{ + i.result = request.session->done == Done::defaulted_dir; +} + + } // namespace TemplatesFunctions diff --git a/templates/templates.cpp b/templates/templates.cpp index 0822dce..550be26 100755 --- a/templates/templates.cpp +++ b/templates/templates.cpp @@ -19,6 +19,7 @@ Ezc::Pattern pat_fun_cat; Ezc::Pattern pat_fun_ls; Ezc::Pattern pat_fun_emacs; Ezc::Pattern pat_fun_mkdir; +Ezc::Pattern pat_fun_default_item; Ezc::Pattern pat_fun_privileges; Ezc::Pattern pat_fun_rm; Ezc::Pattern pat_err_item_required; @@ -80,6 +81,10 @@ Ezc::Pattern * p = 0; p = &pat_fun_mkdir; break; + case Function::default_item: + p = &pat_fun_default_item; + break; + case Function::privileges: p = &pat_fun_privileges; break; @@ -209,15 +214,17 @@ void Templates::CreateFunctions() /* dir */ - functions.Insert("dir", dir); + functions.Insert("dir", dir); - functions.Insert("dir_childs_tab", dir_childs_tab); - functions.Insert("dir_childs_tab_url", dir_childs_tab_url); + functions.Insert("dir_childs_tab", dir_childs_tab); + functions.Insert("dir_childs_tab_url", dir_childs_tab_url); - functions.Insert("dir_tab", dir_tab); - functions.Insert("dir_tab_url", dir_tab_url); - functions.Insert("dir_tab_link", dir_tab_link); + functions.Insert("dir_tab", dir_tab); + functions.Insert("dir_tab_url", dir_tab_url); + functions.Insert("dir_tab_link", dir_tab_link); + functions.Insert("dir_last_default_item_dir", dir_last_default_item_dir); + functions.Insert("dir_last_default_item_url", dir_last_default_item_url); /* user @@ -244,6 +251,7 @@ void Templates::CreateFunctions() */ functions.Insert("done_errors", done_errors); functions.Insert("done_status", done_status); + functions.Insert("done_status_no_item", done_status_no_item); functions.Insert("done_status_incorrect_dir", done_status_incorrect_dir); functions.Insert("done_added_item", done_added_item); functions.Insert("done_edited_item", done_edited_item); @@ -251,6 +259,7 @@ void Templates::CreateFunctions() functions.Insert("done_privileged_item", done_privileged_item); functions.Insert("done_loggedout", done_loggedout); functions.Insert("done_added_dir", done_added_dir); + functions.Insert("done_defaulted_dir", done_defaulted_dir); /* @@ -298,6 +307,9 @@ void Templates::Read() pat_fun_mkdir.Directory(data.templates); pat_fun_mkdir.ParseFile("fun_mkdir.html"); + pat_fun_default_item.Directory(data.templates); + pat_fun_default_item.ParseFile("fun_default_item.html"); + pat_fun_privileges.Directory(data.templates); pat_fun_privileges.ParseFile("fun_privileges.html"); diff --git a/templates/templates.h b/templates/templates.h index b8dc444..176ea76 100755 --- a/templates/templates.h +++ b/templates/templates.h @@ -100,6 +100,8 @@ namespace TemplatesFunctions void dir_tab_url(Info & i); void dir_tab_link(Info & i); + void dir_last_default_item_dir(Info &); + void dir_last_default_item_url(Info & i); /* user @@ -127,6 +129,7 @@ namespace TemplatesFunctions */ void done_errors(Info & i); void done_status(Info & i); + void done_status_no_item(Info & i); void done_status_incorrect_dir(Info & i); void done_added_item(Info & i); @@ -135,6 +138,7 @@ namespace TemplatesFunctions void done_privileged_item(Info & i); void done_added_dir(Info & i); + void done_defaulted_dir(Info & i); void done_loggedout(Info & i);