diff --git a/content/content.cpp b/content/content.cpp index d37d312..a57ebaa 100755 --- a/content/content.cpp +++ b/content/content.cpp @@ -110,7 +110,7 @@ void Content::MakeStandardFunction() if( !request.pfunction ) SetDefaultFunction(); - if( request.result == Request::redirect ) + if( !request.redirect_to.empty() ) return; @@ -239,7 +239,7 @@ void Content::Make() if( request.method == Request::post ) MakePost(); - if( request.result == Request::redirect ) + if( !request.redirect_to.empty() ) return; if( request.status == Error::ok ) @@ -249,7 +249,7 @@ void Content::Make() request.status = Error::permision_denied; } - if( request.result == Request::redirect ) + if( !request.redirect_to.empty() ) return; if( request.dir_table.empty() ) @@ -275,23 +275,22 @@ void Content::RedirectTo(const Item & item) { std::string path; - request.result = Request::redirect; - request.str = data.base_url; + request.redirect_to = data.base_url; if( item.type == Item::dir ) { // item_id is pointing to a directory data.dirs.MakePath(item.id, path); - request.str += path; + request.redirect_to += path; } else { if( !data.dirs.MakePath(item.parent_id, path) ) log << log1 << "Content: Can't redirect: no dirs for item id: " << item.id << logend; - request.str += path; - request.str += item.url; + request.redirect_to += path; + request.redirect_to += item.url; } } @@ -302,16 +301,15 @@ void Content::RedirectTo(long item_id) std::string path; Item * pdir; - request.result = Request::redirect; - request.str = data.base_url; - pdir = data.dirs.GetDir(item_id); + request.redirect_to = data.base_url; + pdir = data.dirs.GetDir(item_id); if( pdir ) { // item_id is pointing to a directory data.dirs.MakePath(pdir->id, path); - request.str += path; + request.redirect_to += path; } else { @@ -324,7 +322,7 @@ Item * pdir; if( !data.dirs.MakePath(request.item_table[0].parent_id, path) ) log << log1 << "Content: Can't redirect: no dirs for item id: " << request.item_table[0].id << ", requested directory id: " << request.item_table[0].parent_id << logend; - request.str += path + request.item_table[0].url; + request.redirect_to += path + request.item_table[0].url; } else { @@ -386,9 +384,9 @@ bool Content::CheckRebus() try { - std::string & answer = request.PostVar("rebus"); + std::string * answer = request.PostVar("rebus"); - if( data.rebus.IsAnswerOk(request.session->rebus_item, answer) ) + if( answer && data.rebus.IsAnswerOk(request.session->rebus_item, *answer) ) return true; } catch(const Error &) @@ -401,23 +399,16 @@ return false; } -void Content::SetUser() +void Content::SetUser(Item & item) { if( request.session->puser ) { - request.item.user_id = request.session->puser->id; - request.item.guest_name.clear(); + item.user_id = request.session->puser->id; + item.guest_name.clear(); } else { - request.item.user_id = -1; - - try - { - request.item.guest_name = request.PostVar("guestname"); - } - catch(const Error &) - { - } + item.user_id = -1; + request.PostVar("guestname", item.guest_name); } } diff --git a/content/content.h b/content/content.h index a54d2f8..af4a0aa 100755 --- a/content/content.h +++ b/content/content.h @@ -36,7 +36,7 @@ class Content void MakeStandardFunction(); - void SetUser(); + void SetUser(Item & item); bool CheckRebus(); void MakePost(); diff --git a/content/createthread.cpp b/content/createthread.cpp index e65a704..6b3dca6 100755 --- a/content/createthread.cpp +++ b/content/createthread.cpp @@ -37,29 +37,10 @@ void Content::PostFunCreateThread() if( !CheckRebus() ) { request.status = Error::rebus_bad_answer; - - try - { - try - { - request.item.url = request.PostVar("url"); - } - catch(...) - { - } - - request.item.subject = request.PostVar("subject"); - - - request.item.content = request.PostVar("content"); - - - SetUser(); - - } - catch(...) - { - } + request.PostVar("url", request.item.url); + request.PostVar("subject", request.item.subject); + request.PostVar("content", request.item.content); + SetUser(request.item); return; } diff --git a/content/default.cpp b/content/default.cpp index 2e1fe97..b44a4ce 100755 --- a/content/default.cpp +++ b/content/default.cpp @@ -22,13 +22,13 @@ long Content::PostFunDefaultParsePath() Item * pdir, * pdir2; long defaultid = -1; - std::string & path = request.PostVar("defaultitem"); + std::string * path = request.PostVar("defaultitem"); - if( !path.empty() ) + if( path && !path->empty() ) { std::string dir, file; - Dirs::SplitPath(path, dir, file); + Dirs::SplitPath(*path, dir, file); pdir = data.dirs.GetDir(dir); diff --git a/content/emacs.cpp b/content/emacs.cpp index 2454af2..f30b00d 100755 --- a/content/emacs.cpp +++ b/content/emacs.cpp @@ -43,7 +43,7 @@ void Content::PostFunEmacsAdd() request.is_item = true; - SetUser(); + SetUser(request.item); request.item.group_id = -1; request.item.privileges = 0644; // !! tymczasowo, bedzie uzyte umask @@ -100,14 +100,8 @@ bool adding = true; { // these old values are ignored (if exists) - //!! tymczasowo, zabezpieczenie przed pustym url-em - try { - request.item.url = request.PostVar("url"); - request.item.subject = request.PostVar("subject"); - } - catch(...) - { - } + request.PostVar("url", request.item.url); + request.PostVar("subject", request.item.subject); if( request.item.subject.empty() ) request.item.subject = request.dir_table.back()->subject; @@ -115,11 +109,12 @@ bool adding = true; // !! nie bedzie obciazany modul wyszukujacy bardzo //////// - request.item.content = request.PostVar("content"); + request.PostVar("content", request.item.content); bool with_url = false; - - if( !request.is_item || request.PostVar("old_url") != request.item.url ) + std::string * old_url = request.PostVar("old_url"); + + if( !request.is_item || (old_url && *old_url!=request.item.url) ) with_url = true; PrepareUrl(request.item); @@ -127,7 +122,7 @@ bool adding = true; if( !CheckRebus() ) { request.status = Error::rebus_bad_answer; - SetUser(); + SetUser(request.item); return; } diff --git a/content/login.cpp b/content/login.cpp index cd2fb93..2df0ea9 100755 --- a/content/login.cpp +++ b/content/login.cpp @@ -33,11 +33,11 @@ void Content::PostFunLogin() { try { - std::string & login = request.PostVar("login"); - std::string & pass = request.PostVar("password"); + std::string * login = request.PostVar("login"); + std::string * pass = request.PostVar("password"); long user_id; - if( db.CheckUser(login, pass, user_id) ) + if( login && pass && db.CheckUser(*login, *pass, user_id) ) { request.session->puser = data.users.GetUser(user_id); @@ -47,7 +47,7 @@ void Content::PostFunLogin() return; } - data.last.UserLogin(user_id, login, inet_addr(request.env_remote_addr), request.session->id); + data.last.UserLogin(user_id, *login, inet_addr(request.env_remote_addr), request.session->id); log << log2 << "User " << login << " (id: " << user_id << ") logged" << logend; } diff --git a/content/logout.cpp b/content/logout.cpp index bc8ab57..b8ea1f7 100755 --- a/content/logout.cpp +++ b/content/logout.cpp @@ -33,15 +33,12 @@ void Content::FunLogout() request.session->puser = 0; } - request.result = Request::redirect; std::string path; - data.dirs.MakePath(request.dir_table.back()->id, path); - - request.str = data.base_url + path; + request.redirect_to = data.base_url + path; if( request.is_item ) - request.str += request.item.url; + request.redirect_to += request.item.url; request.session->done = Done::loggedout; request.session->done_timer = 2; diff --git a/content/mkdir.cpp b/content/mkdir.cpp index 930eaf4..5a43c51 100755 --- a/content/mkdir.cpp +++ b/content/mkdir.cpp @@ -28,22 +28,14 @@ void Content::PostFunMkdir(bool add_to_dir_table, int mask) Item item; request.session->done = Done::added_dir; - try - { +// try +// { item.type = Item::dir; - item.subject = request.PostVar("subject"); - item.url = request.PostVar("url"); + request.PostVar("subject", item.subject); + request.PostVar("url", item.url); item.parent_id = request.dir_table.back()->id; - if( request.session->puser ) - { - item.user_id = request.session->puser->id; - } - else - { - item.user_id = -1; - item.guest_name = request.PostVar("guestname"); - } + SetUser(item); item.group_id = -1; item.privileges = mask; @@ -55,12 +47,12 @@ void Content::PostFunMkdir(bool add_to_dir_table, int mask) request.session->done_status = db.AddItem(item); - } +/* } catch(const Error & e) { request.session->done_status = e; } - +*/ if( request.session->done_status == Error::ok ) { diff --git a/content/priv.cpp b/content/priv.cpp index 27eaab9..2418f16 100755 --- a/content/priv.cpp +++ b/content/priv.cpp @@ -18,9 +18,21 @@ void Content::PostFunPriv(Item & item) { try { - long user_id = data.users.GetUserId( request.PostVar("user") ); - long group_id = data.groups.GetGroupId( request.PostVar("group") ); - int privileges = strtol( request.PostVar("privileges").c_str() , 0, 8); + std::string * user = request.PostVar("user"); + std::string * group = request.PostVar("group"); + std::string * priv = request.PostVar("privileges"); + + if( !user || !group || !priv ) + { + log << log3 << "Content: PostFunPriv: there is no some post variables" << logend; + // some kind of error to report? + // !! wstawic nowy kod bledu i rzucic wyjatek jego (usunac powyzszy wpis log takze) + return; + } + + long user_id = data.users.GetUserId( *user ); + long group_id = data.groups.GetGroupId( *group ); + int privileges = strtol( priv->c_str() , 0, 8); if( user_id==item.user_id && group_id==item.group_id && privileges==item.privileges ) { diff --git a/core/dirs.cpp b/core/dirs.cpp index 8fe1ba8..8f84461 100755 --- a/core/dirs.cpp +++ b/core/dirs.cpp @@ -60,34 +60,6 @@ void Dirs::ReadDirs() } -bool Dirs::GetRootDir(Item ** item) -{ - DirContainer::Iterator root = dir_table.GetRoot(); - - if( root == dir_table.End() ) - return false; - - *item = &(*root); - -return true; -} - - -bool Dirs::GetDir(const std::string & name, long parent, Item ** item) -{ - DirContainer::ParentIterator i = dir_table.FindFirstParent(parent); - - for( ; i!=dir_table.ParentEnd() ; i = dir_table.NextParent(i) ) - if( i->second->url == name ) - { - *item = &(*i->second); - return true; - } - -return false; -} - - bool Dirs::ExtractName(const char * & s, std::string & name) { name.clear(); @@ -103,62 +75,6 @@ return !name.empty(); -// !! moze lepiej zwracac wskaznik do Item i kiedy nie ma katalogu to zwracac 0 ? -bool Dirs::GetDir(const std::string & path, Item ** item) -{ - DirContainer::Iterator root = dir_table.GetRoot(); - - if( root == dir_table.End() ) - // ops, we do not have a root dir - return false; - - Item * pitem = &(*root); - - std::string name; - const char * s = path.c_str(); - - while( ExtractName(s, name) ) - { - if( !GetDir(name, pitem->id, &pitem) ) - return false; - } - - - - *item = pitem; - -return true; -} - - - -// !! ten interfejs jes bylejaki -// !! moze lepiej zwracac id i kiedy nie ma katalogu to -1 (przeciez to jest wartosc ktora nie moze pojawic sie w indeksie) -bool Dirs::GetDirId(const std::string & path, long * id) -{ -Item * pitem; - - if( !GetDir(path, &pitem) ) - return false; - - *id = pitem->id; - -return true; -} - - -bool Dirs::GetDirId(const std::string & name, long parent, long * id) -{ -Item * pitem; - - if( !GetDir(name, parent, &pitem) ) - return false; - - *id = pitem->id; - -return true; -} - bool Dirs::IsDir(long id) @@ -174,20 +90,6 @@ return true; -bool Dirs::GetDirChilds(long parent, std::vector & childs_table) -{ - if( parent != -1 && !IsDir(parent) ) - return false; - - DirContainer::ParentIterator i = dir_table.FindFirstParent(parent); - - for( ; i != dir_table.ParentEnd() ; i = dir_table.NextParent(i) ) - childs_table.push_back( *i->second ); - -return true; -} - - bool Dirs::GetDirChilds(long parent, std::vector & childs_table) { if( parent != -1 && !IsDir(parent) ) @@ -232,55 +134,6 @@ DirContainer::Iterator i; -// with exceptions - -Item * Dirs::GetDirT(const std::string & path) -{ -Item * pitem; - - if( !GetDir(path, &pitem) ) - throw Error(Error::incorrect_dir); - -return pitem; -} - - -Item * Dirs::GetDirT(const std::string & name, long parent) -{ -Item * pitem; - - if( !GetDir(name, parent, &pitem) ) - throw Error(Error::incorrect_dir); - -return pitem; - -} - - -long Dirs::GetDirIdT(const std::string & path) -{ -long id; - - if( !GetDirId(path, &id) ) - throw Error(Error::incorrect_dir); - -return id; -} - - -long Dirs::GetDirIdT(const std::string & name, long parent) -{ -long id; - - if( !GetDirId(name, parent, &id) ) - throw Error(Error::incorrect_dir); - -return id; -} - - - -// !! nowy interfejs @@ -417,8 +270,7 @@ void Dirs::DeleteDir(long id) if( db.DelDirById(id) == Error::ok ) dir_table.DelById(id); - if( data.mounts.CurrentMountType() == Mount::thread ) - db.RemoveThread(id); + db.RemoveThread(id); } diff --git a/core/dirs.h b/core/dirs.h index f3325e7..355f96d 100755 --- a/core/dirs.h +++ b/core/dirs.h @@ -19,6 +19,8 @@ #include "dircontainer.h" +// we do not support '..' in a path (for simplicity and security reasons) + class Dirs { @@ -26,41 +28,17 @@ public: void Clear(); void ReadDirs(); - - // without any exceptions - // these methods return false in a case the path or name (with a specific parent) are invalid - // we do not support '..' in a path (for security reason) - bool IsDir(long id); - - bool GetRootDir(Item ** item); - - bool GetDir(const std::string & path, Item ** item); - bool GetDir(const std::string & name, long parent, Item ** item); - - bool GetDirId(const std::string & path, long * id); - bool GetDirId(const std::string & name, long parent, long * id); - //!! ta nie bedzie chyba potrzebna - bool GetDirChilds(long parent, std::vector & childs_table); // only returns dir-children + // these methods return false if there is no such a dir + bool IsDir(long dir_id); + bool GetDirChilds(long parent_id, std::vector & childs_table); + bool MakePath(long dir_id, std::string & path); - bool GetDirChilds(long parent, std::vector & childs_table); // only returns dir-children - - bool MakePath(long id, std::string & path); + static void SplitPath(const std::string & path, std::string & dir, std::string & file); - // with an Error exception - // if the path or name are invalid these methods throw an exception - Item * GetDirT(const std::string & path); - Item * GetDirT(const std::string & name, long parent); - long GetDirIdT(const std::string & path); - long GetDirIdT(const std::string & name, long parent); - - - - // !! nowy interfejs - - // returns null if there is no a root dir + // these methods return null if there is no such a dir Item * GetRootDir(); Item * GetEtcDir(); Item * GetDir(const std::string & name, long parent); @@ -68,13 +46,7 @@ public: Item * GetDir(long id); Item * AddDir(const Item & item); - - static void SplitPath(const std::string & path, std::string & dir, std::string & file); - - void DeleteDir(long id); - - void CheckRootDir(); diff --git a/core/log.cpp b/core/log.cpp index a6ff5a2..3788298 100755 --- a/core/log.cpp +++ b/core/log.cpp @@ -64,6 +64,14 @@ Log & Log::operator<<(const std::string & s) } +Log & Log::operator<<(const std::string * s) +{ + buffer << *s; + + return *this; +} + + Log & Log::operator<<(int s) { buffer << s; diff --git a/core/log.h b/core/log.h index a8e6e13..bb8ede7 100755 --- a/core/log.h +++ b/core/log.h @@ -38,6 +38,7 @@ public: void PutDate(Manipulators m); Log & operator<<(const char * s); + Log & operator<<(const std::string * s); Log & operator<<(const std::string & s); Log & operator<<(int s); Log & operator<<(long s); diff --git a/core/mounts.cpp b/core/mounts.cpp index 614769a..8370090 100755 --- a/core/mounts.cpp +++ b/core/mounts.cpp @@ -75,7 +75,7 @@ Mount Mounts::GetCurrentMountPoint() Mount::Type Mounts::CurrentMountType() { - return current_dir.type; + return current_dir.type; } bool Mounts::CurrentMountIsParam(Mount::Param p) diff --git a/core/request.cpp b/core/request.cpp index 425cb97..7603e5c 100755 --- a/core/request.cpp +++ b/core/request.cpp @@ -59,17 +59,9 @@ void Request::Clear() env_http_accept_encoding = &char_empty; session = 0; - - result = err404; // !! tutaj moze cos lepszego, cos w stylu 'not implemented' -// dir = -1; -// cur_dir_table.clear(); item_table.clear(); -// dir_table2.clear(); - item.Clear(); - str.clear(); - dir_table.clear(); is_item = false; pfunction = 0; @@ -79,35 +71,16 @@ void Request::Clear() is_thread = false; thread.Clear(); - thread_tab.clear(); - notify_code = 0; - browser_msie = false; - - // should be always empty - // this is only for safety - //string_empty.clear(); + redirect_to.clear(); + plugin.Call(CMSLU_REQUEST_CLEAR); } -void Request::CopyFirstItem() -{ - if( !request.item_table.empty() ) - request.item = request.item_table[0]; - else - { - request.item.Clear(); - - log << log1 << "Request::CopyFirstItem: item_table is empty" << logend; - request.result = err_internal; - } -} - - // value can be null @@ -148,27 +121,21 @@ return true; -std::string & Request::PostVar(const char * var) +std::string * Request::PostVar(const char * var) { - PostTable::iterator p; - - p = post_table.find(var); +PostTable::iterator p = post_table.find(var); if( p == post_table.end() ) - { - throw Error(Error::no_postvar); - } + return 0; -return p->second; +return &(p->second); } -/* + bool Request::PostVar(const char * var, std::string & result) { - PostTable::iterator p; - - p = post_table.find(var); +PostTable::iterator p = post_table.find(var); if( p == post_table.end() ) { @@ -180,7 +147,7 @@ bool Request::PostVar(const char * var, std::string & result) return true; } -*/ + @@ -333,12 +300,12 @@ void Request::Read() void Request::SendAll() { - if( result == redirect ) + if( !redirect_to.empty() ) { FCGX_PutS("Status: 301 Moved Permanently\r\n", out); - FCGX_FPrintF(out, "Location: %s\r\n", str.c_str()); + FCGX_FPrintF(out, "Location: %s\r\n", redirect_to.c_str()); - log << log2 << "Redirect to: " << str << logend; + log << log2 << "Redirect to: " << redirect_to << logend; } else { @@ -355,7 +322,7 @@ void Request::SendAll() FCGX_PutS("\r\n", out); - if( result == redirect ) + if( !redirect_to.empty() ) // if there is a redirect we do not send a content return; // diff --git a/core/request.h b/core/request.h index e967a03..c7f6971 100755 --- a/core/request.h +++ b/core/request.h @@ -51,7 +51,8 @@ struct Request CookieTable cookie_table; // environment variables - // they are not null -- when the server doesn't have such a variable it will be pointing into 'char_empty' which is default '\0' + // they are not null -- when the server doesn't have such a variable + // it will be pointing into 'char_empty' which is default '\0' const char * env_request_method; const char * env_request_uri; const char * env_http_cookie; @@ -68,22 +69,7 @@ struct Request // is set after calling session_manager.SetSession() Session * session; - - // 'done_status' is set if 'done' is different than 'nothing' - Error done_status; // !! wywalic - jest przeciez w session - - // what to do - // !! wywalic? - enum Result { err_internal, err404, err_per_denied, show_dir, show_item, show_item_by_id, add_item, edit_item, del_item, del_item_confirm, confirm, redirect, logout } result; // zamienic na to_do - - - - // !! nowe skladowe - // current directory - // !! zapewnic aby byl minimum jeden katalog (root) - - // !! moze nazwac to poprostu dir? std::vector dir_table; bool is_item; @@ -102,33 +88,17 @@ struct Request // last notify int notify_code; - - // ------------------ - - // !! stare skladowe - // current directory e.g. /foo/bar - //std::vector cur_dir_table; - // id of the last directory (bar) or -1 - //long dir; - - // ------------------ - // items in the current directory // maybe without contents? std::vector item_table; - - // directories in the current directory - //std::vector dir_table2; // current thread (if exists) - bool is_thread; Thread thread; - std::vector thread_tab; - // this string is used for many purposes such as redirecting - std::string str; + // if not empty means an address for redirecting to + std::string redirect_to; // for debugging void PrintGetTable(); @@ -146,12 +116,8 @@ struct Request void SetCookie(const char * name, long value); bool IsPostVar(const char * var); - std::string & PostVar(const char * var); // with a throw !!! wywalic te wyjatki ztad, niech zwraca pusty string jak nie znajdzie nic, albo referencje na jakis statyczny pusty string - //bool PostVar(const char * var, std::string & result); - - // item_table[0] -> item - // !! to tez nie bedzie potrzebne - void CopyFirstItem(); + std::string * PostVar(const char * var); // it can return null when there is no such a post variable + bool PostVar(const char * var, std::string & result); void ReadEnvVariables(); @@ -187,8 +153,6 @@ private: // it contains '\0' const char char_empty; - // used when returning a reference to std::string (when the string doesn't exist) - const std::string string_empty; const char * SetEnvVar(const char * var); diff --git a/core/requestcontroller.cpp b/core/requestcontroller.cpp index 72c6fae..5a5c228 100755 --- a/core/requestcontroller.cpp +++ b/core/requestcontroller.cpp @@ -130,8 +130,7 @@ bool RequestController::BaseUrlRedirect() if( data.base_url_http_host == request.env_http_host ) return false; - request.result = Request::redirect; - request.str = data.base_url + request.env_request_uri; + request.redirect_to = data.base_url + request.env_request_uri; log << log3 << "RC: BaseUrlRedirect from: " << request.env_http_host << logend;