From 79eda7abb0a6c96eb30d90fbc0cfc0ae6b49538f Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 20 Jun 2021 18:04:50 +0200 Subject: [PATCH] - added to FunctionBase: bool register_default_models (default true) if true then winix will add default models for ezc templates such as "request", "item", etc. - some methods from templates/item.cpp moved to Item --- winixd/core/app.cpp | 21 ++++++++ winixd/core/app.h | 1 + winixd/core/misc.cpp | 2 +- winixd/core/misc.h | 3 +- winixd/core/textstream.h | 71 +++++++++++++++++++++++++- winixd/core/threadmanager.cpp | 3 +- winixd/functions/cat.cpp | 6 --- winixd/functions/functionbase.cpp | 1 + winixd/functions/functionbase.h | 5 ++ winixd/functions/ls.cpp | 10 ---- winixd/models/item.cpp | 61 ++++++++++++++++++++--- winixd/models/item.h | 16 ++---- winixd/models/winixmodel.cpp | 24 +++++++++ winixd/models/winixmodel.h | 4 +- winixd/models/winixmodelconnector.cpp | 16 ++++++ winixd/models/winixmodelconnector.h | 8 ++- winixd/templates/htmltextstream.cpp | 12 +++++ winixd/templates/htmltextstream.h | 6 ++- winixd/templates/item.cpp | 72 +++++++++++++-------------- winixd/templates/templates.cpp | 6 +-- winixd/templates/templates.h | 6 +-- 21 files changed, 271 insertions(+), 83 deletions(-) diff --git a/winixd/core/app.cpp b/winixd/core/app.cpp index 3449579..f06416d 100644 --- a/winixd/core/app.cpp +++ b/winixd/core/app.cpp @@ -344,6 +344,7 @@ bool App::Init() model_connector.set_winix_locale(&TemplatesFunctions::locale); model_connector.set_winix_session_manager(&session_manager); model_connector.set_winix_time_zones(&system.time_zones); + model_connector.set_winix_pattern_cacher(&TemplatesFunctions::pattern_cacher); if( !TryToMakeDatabaseMigration() ) return false; @@ -846,6 +847,24 @@ void App::CheckPostRedirect() } +void App::AddDefaultModels() +{ + if( !cur.request->send_bin_stream && !cur.request->return_json ) + { + if( cur.request->function && cur.request->function->register_default_models ) + { + cur.request->models.Add(L"request", cur.request); + + if( cur.request->is_item ) + { + cur.request->models.Add(L"item", cur.request->item); + } + } + } +} + + + // !! IMPROVE ME change to a better name // may ProcessRequest()? but probably it is already defined... // this method needs some refactoring @@ -886,6 +905,8 @@ void App::Make() if( !cur.request->redirect_to.empty() ) return; + AddDefaultModels(); + if( cur.request->status == WINIX_ERR_OK ) functions.MakeFunction(); diff --git a/winixd/core/app.h b/winixd/core/app.h index 4067f77..5a17487 100644 --- a/winixd/core/app.h +++ b/winixd/core/app.h @@ -194,6 +194,7 @@ private: void SetLocale(); void CheckPostRedirect(); void MakePage(); + void AddDefaultModels(); void Make(); void SaveSessionsIfNeeded(); // !! IMPROVE ME wywalic do menagera sesji?? void LogAccess(); diff --git a/winixd/core/misc.cpp b/winixd/core/misc.cpp index a734375..46a9805 100644 --- a/winixd/core/misc.cpp +++ b/winixd/core/misc.cpp @@ -1389,7 +1389,7 @@ using namespace misc_private; tmp_qencode.clear(); QEncode(in, tmp_qencode); - tmp_qencode.to_string(out, clear); + tmp_qencode.to_str(out, clear); tmp_qencode.clear(); } diff --git a/winixd/core/misc.h b/winixd/core/misc.h index ab8ba9d..ea41768 100644 --- a/winixd/core/misc.h +++ b/winixd/core/misc.h @@ -771,7 +771,7 @@ void UrlEncode(char c, StringType & out, bool clear_out = true) static pt::TextStream tmp; UrlEncode(c, tmp); - tmp.to_string(out, clear_out); + tmp.to_str(out, clear_out); } @@ -882,6 +882,7 @@ void QEncode(const std::wstring & in, pt::TextStreamBase TextStream & operator<<(const pt::TextStreamBase & arg); @@ -233,10 +240,59 @@ void TextStream::Str(const StringType & str) template void TextStream::Str(const StringType && str) { - buffer = str; + buffer = std::move(str); } +template +void TextStream::to_str(std::string & str, bool clear_string) const +{ + return to_string(str, clear_string); +} + + +template +void TextStream::to_str(std::wstring & str, bool clear_string) const +{ + return to_string(str, clear_string); +} + + +template +void TextStream::to_string(std::string & str, bool clear_string) const +{ + if( clear_string ) + str.clear(); + + if constexpr (sizeof(typename StringType::value_type) == sizeof(char)) + { + str.append(buffer); + } + else + { + pt::wide_to_utf8(buffer, str, false); + } +} + + +template +void TextStream::to_string(std::wstring & str, bool clear_string) const +{ + if( clear_string ) + str.clear(); + + if constexpr (sizeof(typename StringType::value_type) == sizeof(wchar_t)) + { + str.append(buffer); + } + else + { + pt::utf8_to_wide(buffer, str, false); + } +} + + + template typename TextStream::CharType TextStream::operator[](size_t index) @@ -455,6 +511,19 @@ return *this; } + +template +TextStream & TextStream::operator<<(const TextStream & stream) +{ + buffer.append(stream.buffer); + +return *this; +} + + + + + template template TextStream & TextStream::operator<<( diff --git a/winixd/core/threadmanager.cpp b/winixd/core/threadmanager.cpp index 11a1a67..40eb1b3 100644 --- a/winixd/core/threadmanager.cpp +++ b/winixd/core/threadmanager.cpp @@ -107,7 +107,8 @@ void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name) data.model_connector.set_winix_session(nullptr); data.model_connector.set_winix_locale(nullptr); // null for a moment, may will be changed data.model_connector.set_winix_session_manager(nullptr);// null for a moment, may will be changed - data.model_connector.set_winix_time_zones(nullptr);// null for a moment, may will be changed + data.model_connector.set_winix_time_zones(nullptr); // null for a moment, may will be changed + data.model_connector.set_winix_pattern_cacher(nullptr); // null for a moment, may will be changed item.object->set_model_connector(&data.model_connector); diff --git a/winixd/functions/cat.cpp b/winixd/functions/cat.cpp index ab45377..e6f899d 100644 --- a/winixd/functions/cat.cpp +++ b/winixd/functions/cat.cpp @@ -67,12 +67,6 @@ void Cat::MakeGet() } cur->request->send_as_attachment = cur->request->IsParam(L"attachment"); - - - // IMPROVEME this will be put by a generic method from winix - // if output is html - cur->request->models.Add(L"item", cur->request->item); - //////////////////////////////////////////////////////////// } diff --git a/winixd/functions/functionbase.cpp b/winixd/functions/functionbase.cpp index 2bca752..adfafc6 100644 --- a/winixd/functions/functionbase.cpp +++ b/winixd/functions/functionbase.cpp @@ -47,6 +47,7 @@ FunctionBase::FunctionBase() template_index = size_t(-1); need_ssl = false; need_session = true; + register_default_models = true; fun.item_content.user_id = -1; fun.item_content.group_id = -1; diff --git a/winixd/functions/functionbase.h b/winixd/functions/functionbase.h index ae57663..0495604 100644 --- a/winixd/functions/functionbase.h +++ b/winixd/functions/functionbase.h @@ -86,6 +86,11 @@ public: // if false then a temporary session is used bool need_session; + // true if the function allows to register default models such as 'request', 'item', etc. + // default: true + bool register_default_models; + + virtual void Init(); virtual bool HasAccess(); virtual void MakePost(); diff --git a/winixd/functions/ls.cpp b/winixd/functions/ls.cpp index bc76e5e..5705d33 100644 --- a/winixd/functions/ls.cpp +++ b/winixd/functions/ls.cpp @@ -102,16 +102,6 @@ void Ls::MakeGet() cur->request->models.Add(L"items", item_tab); cur->request->models.Add(L"child_dirs", dir_tab); } - - // IMPROVEME this will be put by a generic method from winix - // if output is html - cur->request->models.Add(L"request", cur->request); - - if( cur->request->is_item ) - { - cur->request->models.Add(L"item", cur->request->item); - } - //////////////////////////////////////////////////////////// } diff --git a/winixd/models/item.cpp b/winixd/models/item.cpp index 1e2463e..dfe09a5 100644 --- a/winixd/models/item.cpp +++ b/winixd/models/item.cpp @@ -38,6 +38,7 @@ #include "core/request.h" #include "templates/templates.h" #include "core/session.h" +#include "core/log.h" @@ -85,7 +86,10 @@ void Item::fields() field(L"can_be_removed", &Item::can_be_removed); field(L"has_read_access", &Item::has_read_access); field(L"has_write_access", &Item::has_write_access); + field(L"has_read_write_access", &Item::has_read_write_access); + field(L"has_read_exec_access", &Item::has_read_exec_access); + field(L"execute", &Item::execute); // may we should add a method setTypeFromInt(int t)? @@ -374,17 +378,27 @@ bool Item::can_be_removed() const } -bool Item::has_read_access() +bool Item::has_read_access() const { return item_content.has_read_access(); } - -bool Item::has_write_access() +bool Item::has_write_access() const { return item_content.has_write_access(); } +bool Item::has_read_write_access() const +{ + return item_content.has_read_write_access(); +} + +bool Item::has_read_exec_access() const +{ + return item_content.has_read_exec_access(); +} + + bool Item::type_is_symlink() const { @@ -603,10 +617,43 @@ void Item::url_is(EzcEnv & env) } -//bool Item::has_read_accessxx() const -//{ -// -//} +void Item::execute(EzcEnv & env) +{ + if( has_read_access() ) + { + PatternCacher * pattern_cacher = get_pattern_cacher(); + Request * request = get_request(); + Config * config = get_config(); + + if( pattern_cacher && request && config ) + { + HtmlTextStream item_run_content; + TemplatesFunctions::EzcGen ezc_generator; + + Ezc::Pattern * p = pattern_cacher->GetPattern(*this); + + TemplatesFunctions::InitGenerator(ezc_generator, request->models); + ezc_generator.SetPattern(*p); + + if( config->allow_ezc_frames_in_executable_items ) + ezc_generator.Generate(item_run_content, request->out_streams); + else + ezc_generator.Generate(item_run_content); + + ItemContent::print_content(env.out, item_run_content.Str(), item_content.content_raw_type, config->html_filter); + } + } + else + { + Log * log = get_logger(); + + if( log ) + { + (*log) << "Item::execute: permission denied, no read_exec access to item id: " << id << ", url: " << url << ", subject" << subject; + } + } + +} diff --git a/winixd/models/item.h b/winixd/models/item.h index d7f9213..dd1a2a7 100644 --- a/winixd/models/item.h +++ b/winixd/models/item.h @@ -38,12 +38,6 @@ #include #include "winixmodel.h" #include "itemcontent.h" - - -// temporary -#include "core/log.h" - - #include "funinfo.h" #include "templates/htmltextstream.h" #include "templates/misc.h" @@ -189,8 +183,10 @@ public: bool can_remove_child(long child_user_id) const; bool can_be_removed() const; - bool has_read_access(); - bool has_write_access(); + bool has_read_access() const; + bool has_write_access() const; + bool has_read_write_access() const; + bool has_read_exec_access() const; bool type_is_symlink() const; bool type_is_file() const; @@ -202,7 +198,6 @@ public: bool is_current_dir() const; - protected: CalcItemsHelper calc_items_by_url(long parent_id, const std::wstring & url); @@ -216,9 +211,8 @@ protected: void print_dir_without_slash(EzcEnv & env); void dir_link(EzcEnv & env); void link(EzcEnv & env); - void url_is(EzcEnv & env); - + void execute(EzcEnv & env); MORM_MEMBER_FIELD(Item) diff --git a/winixd/models/winixmodel.cpp b/winixd/models/winixmodel.cpp index 0bcbd38..b873867 100644 --- a/winixd/models/winixmodel.cpp +++ b/winixd/models/winixmodel.cpp @@ -209,6 +209,17 @@ TimeZones * WinixModel::get_time_zones() } +PatternCacher * WinixModel::get_pattern_cacher() +{ + WinixModelConnector * connector = get_winix_model_connector(); + + if( connector ) + { + return connector->get_winix_pattern_cacher(); + } + + return nullptr; +} @@ -383,6 +394,19 @@ const TimeZones * WinixModel::get_time_zones() const } +const PatternCacher * WinixModel::get_pattern_cacher() const +{ + const WinixModelConnector * connector = get_winix_model_connector(); + + if( connector ) + { + return connector->get_winix_pattern_cacher(); + } + + return nullptr; +} + + diff --git a/winixd/models/winixmodel.h b/winixd/models/winixmodel.h index 248ddcf..381a6db 100644 --- a/winixd/models/winixmodel.h +++ b/winixd/models/winixmodel.h @@ -55,7 +55,7 @@ class User; class Locale; class SessionManager; class TimeZones; - +class PatternCacher; class WinixModel : public morm::Model @@ -75,6 +75,7 @@ public: Locale * get_locale(); SessionManager * get_session_manager(); TimeZones * get_time_zones(); + PatternCacher * get_pattern_cacher(); const Config * get_config() const; const Request * get_request() const; @@ -89,6 +90,7 @@ public: const Locale * get_locale() const; const SessionManager * get_session_manager() const; const TimeZones * get_time_zones() const; + const PatternCacher * get_pattern_cacher() const; diff --git a/winixd/models/winixmodelconnector.cpp b/winixd/models/winixmodelconnector.cpp index 2635938..ca31a0d 100644 --- a/winixd/models/winixmodelconnector.cpp +++ b/winixd/models/winixmodelconnector.cpp @@ -53,6 +53,7 @@ WinixModelConnector::WinixModelConnector() locale = nullptr; session_manager = nullptr; time_zones = nullptr; + pattern_cacher = nullptr; } @@ -127,6 +128,10 @@ TimeZones * WinixModelConnector::get_winix_time_zones() return time_zones; } +PatternCacher * WinixModelConnector::get_winix_pattern_cacher() +{ + return pattern_cacher; +} @@ -207,6 +212,12 @@ const TimeZones * WinixModelConnector::get_winix_time_zones() const } +const PatternCacher * WinixModelConnector::get_winix_pattern_cacher() const +{ + return pattern_cacher; +} + + void WinixModelConnector::set_winix_config(Config * config) { @@ -280,6 +291,11 @@ void WinixModelConnector::set_winix_time_zones(TimeZones * time_zones) } +void WinixModelConnector::set_winix_pattern_cacher(PatternCacher * pattern_cacher) +{ + this->pattern_cacher = pattern_cacher; +} + diff --git a/winixd/models/winixmodelconnector.h b/winixd/models/winixmodelconnector.h index f136f70..baaf606 100644 --- a/winixd/models/winixmodelconnector.h +++ b/winixd/models/winixmodelconnector.h @@ -52,6 +52,8 @@ class Session; class Locale; class SessionManager; class TimeZones; +class PatternCacher; + class WinixModelConnector : public morm::ModelConnector @@ -72,6 +74,8 @@ public: Locale * get_winix_locale(); SessionManager * get_winix_session_manager(); TimeZones * get_winix_time_zones(); + PatternCacher * get_winix_pattern_cacher(); + const Config * get_winix_config() const; const Request * get_winix_request() const; @@ -85,6 +89,7 @@ public: const Locale * get_winix_locale() const; const SessionManager * get_winix_session_manager() const; const TimeZones * get_winix_time_zones() const; + const PatternCacher * get_winix_pattern_cacher() const; void set_winix_config(Config * config); void set_winix_request(Request * request); @@ -98,6 +103,7 @@ public: void set_winix_locale(Locale * locale); void set_winix_session_manager(SessionManager * session_manager); void set_winix_time_zones(TimeZones * time_zones); + void set_winix_pattern_cacher(PatternCacher * pattern_cacher); protected: @@ -114,7 +120,7 @@ protected: Locale * locale; SessionManager * session_manager; TimeZones * time_zones; - + PatternCacher * pattern_cacher; }; diff --git a/winixd/templates/htmltextstream.cpp b/winixd/templates/htmltextstream.cpp index 5b2d13f..e309374 100644 --- a/winixd/templates/htmltextstream.cpp +++ b/winixd/templates/htmltextstream.cpp @@ -58,6 +58,13 @@ void HtmlTextStream::clear() } +void HtmlTextStream::to_str(std::wstring & str) +{ + str = TextStream::Str(); +} + + + /* without escaping @@ -253,6 +260,11 @@ HtmlTextStream & HtmlTextStream::operator<<(RawText raw) } +HtmlTextStream & HtmlTextStream::operator<<(const HtmlTextStream & stream) +{ + TextStream::operator<<(stream.Str()); + return *this; +} diff --git a/winixd/templates/htmltextstream.h b/winixd/templates/htmltextstream.h index 1e79730..147c241 100644 --- a/winixd/templates/htmltextstream.h +++ b/winixd/templates/htmltextstream.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2015, Tomasz Sowa + * Copyright (c) 2010-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -87,6 +87,7 @@ public: void Clear(); void clear(); // utf8 methods call clear(), in the future Clear() will be renamed to clear() + void to_str(std::wstring & str); /* a helper struct to select a proper operator<< @@ -151,6 +152,9 @@ public: HtmlTextStream & operator<<(RawText > raw); + // this method doesn't escape stream as there is no need for such behavior - stream should be already escaped + HtmlTextStream & operator<<(const HtmlTextStream & stream); + // 'write' don't escapes too // with these methods you can write a zero character too diff --git a/winixd/templates/item.cpp b/winixd/templates/item.cpp index bb77e1b..265badd 100644 --- a/winixd/templates/item.cpp +++ b/winixd/templates/item.cpp @@ -280,46 +280,46 @@ void item_dates_equal(Info & i) -void item_run(Info & i) -{ - if( !cur->request->is_item ) - { - i.out << ""; - return; - } - - if( system->HasReadExecAccess(*cur->request->last_item) ) - { - Ezc::Pattern * p = pattern_cacher.GetPattern(*cur->request->last_item); - - item_run_content.Clear(); - InitGenerator(ezc_generator, cur->request->models); - ezc_generator.SetPattern(*p); - - if( config->allow_ezc_frames_in_executable_items ) - ezc_generator.Generate(item_run_content, cur->request->out_streams); - else - ezc_generator.Generate(item_run_content); - - ItemContent::print_content(i.out, item_run_content.Str(), cur->request->last_item->item_content.content_raw_type, config->html_filter); - } - else - { - i.out << ""; - } -} +//void item_run(Info & i) +//{ +// if( !cur->request->is_item ) +// { +// i.out << ""; +// return; +// } +// +// if( system->HasReadExecAccess(*cur->request->last_item) ) +// { +// Ezc::Pattern * p = pattern_cacher.GetPattern(*cur->request->last_item); +// +// item_run_content.Clear(); +// InitGenerator(ezc_generator, cur->request->models); +// ezc_generator.SetPattern(*p); +// +// if( config->allow_ezc_frames_in_executable_items ) +// ezc_generator.Generate(item_run_content, cur->request->out_streams); +// else +// ezc_generator.Generate(item_run_content); +// +// ItemContent::print_content(i.out, item_run_content.Str(), cur->request->last_item->item_content.content_raw_type, config->html_filter); +// } +// else +// { +// i.out << ""; +// } +//} -void item_guest_name(Info & i) -{ - i.out << cur->request->last_item->item_content.guest_name; -} +//void item_guest_name(Info & i) +//{ +// i.out << cur->request->last_item->item_content.guest_name; +//} -void item_html_template(Info & i) -{ - i.out << cur->request->last_item->html_template; -} +//void item_html_template(Info & i) +//{ +// i.out << cur->request->last_item->html_template; +//} void item_has_html_template(Info & i) diff --git a/winixd/templates/templates.cpp b/winixd/templates/templates.cpp index e4c927f..873987b 100644 --- a/winixd/templates/templates.cpp +++ b/winixd/templates/templates.cpp @@ -463,9 +463,9 @@ void Templates::CreateFunctions() ezc_functions.Insert("item_date_creation_nice", item_date_creation_nice); ezc_functions.Insert("item_date_modification_nice",item_date_modification_nice); ezc_functions.Insert("item_dates_equal", item_dates_equal); - ezc_functions.Insert("item_run", item_run); - ezc_functions.Insert("item_guest_name", item_guest_name); - ezc_functions.Insert("item_html_template", item_html_template); +// ezc_functions.Insert("item_run", item_run); +// ezc_functions.Insert("item_guest_name", item_guest_name); +// ezc_functions.Insert("item_html_template", item_html_template); ezc_functions.Insert("item_has_html_template", item_has_html_template); // ezc_functions.Insert("item_type_is_dir", item_type_is_dir); // ezc_functions.Insert("item_type_is_file", item_type_is_file); diff --git a/winixd/templates/templates.h b/winixd/templates/templates.h index c0caf94..6d8172b 100644 --- a/winixd/templates/templates.h +++ b/winixd/templates/templates.h @@ -353,9 +353,9 @@ namespace TemplatesFunctions void item_date_creation_nice(Info & i); void item_date_modification_nice(Info & i); void item_dates_equal(Info & i); - void item_run(Info & i); - void item_guest_name(Info & i); - void item_html_template(Info & i); + //void item_run(Info & i); + //void item_guest_name(Info & i); + //void item_html_template(Info & i); void item_has_html_template(Info & i); //void item_type_is_dir(Info & i); //void item_type_is_file(Info & i);