2018-11-03 17:12:20 +01:00
|
|
|
/* Copyright (c) 2018 Albert S.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "handlerpageview.h"
|
|
|
|
#include "../database/exceptions.h"
|
|
|
|
#include "../logger.h"
|
|
|
|
#include "../parser.h"
|
|
|
|
#include "../htmllink.h"
|
2022-03-27 08:37:55 +02:00
|
|
|
#include "../dynamic/dynamiccontentpostlist.h"
|
2022-03-27 21:36:53 +02:00
|
|
|
#include "../dynamic/dynamiccontentincludepage.h"
|
2022-03-29 22:36:03 +02:00
|
|
|
#include "../dynamic/dynamiccontentsetvar.h"
|
|
|
|
#include "../dynamic/dynamiccontentgetvar.h"
|
2022-08-20 11:23:27 +02:00
|
|
|
#include "../revisionrenderer.h"
|
2022-03-29 22:36:03 +02:00
|
|
|
|
2018-11-03 17:12:20 +01:00
|
|
|
bool HandlerPageView::canAccess(std::string page)
|
|
|
|
{
|
2019-05-03 15:59:29 +02:00
|
|
|
return effectivePermissions(page).canRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HandlerPageView::accessErrorMessage()
|
|
|
|
{
|
|
|
|
return "You don't have permission to view this page";
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string HandlerPageView::createIndexContent(IParser &parser, std::string content)
|
|
|
|
{
|
|
|
|
std::vector<Headline> headlines = parser.extractHeadlines(content);
|
|
|
|
std::string indexcontent = "";
|
2019-08-26 20:51:59 +02:00
|
|
|
unsigned int previous = 0;
|
2018-11-03 17:12:20 +01:00
|
|
|
for(const Headline &h : headlines)
|
|
|
|
{
|
2019-08-26 20:51:59 +02:00
|
|
|
if(h.level > previous)
|
2018-11-03 17:12:20 +01:00
|
|
|
{
|
|
|
|
indexcontent += "<ul>";
|
|
|
|
}
|
2019-08-26 20:51:59 +02:00
|
|
|
else if(h.level < previous)
|
2018-11-03 17:12:20 +01:00
|
|
|
{
|
2019-08-26 20:51:59 +02:00
|
|
|
unsigned int diff = previous - h.level;
|
|
|
|
for(unsigned int i = 0; i < diff; i++)
|
|
|
|
{
|
|
|
|
indexcontent += "</ul>";
|
|
|
|
}
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
2019-08-26 20:51:59 +02:00
|
|
|
previous = h.level;
|
2018-11-03 17:12:20 +01:00
|
|
|
HtmlLink link;
|
|
|
|
link.href = "#" + h.title;
|
|
|
|
link.innervalue = h.title;
|
|
|
|
link.cssclass = "indexlink";
|
|
|
|
indexcontent += "<li>" + link.render() + "</li>";
|
|
|
|
}
|
2019-08-26 20:51:59 +02:00
|
|
|
for(unsigned int i = 0; i < previous; i++)
|
|
|
|
{
|
|
|
|
indexcontent += "</ul>";
|
|
|
|
}
|
2018-11-03 17:12:20 +01:00
|
|
|
return indexcontent;
|
|
|
|
}
|
|
|
|
|
|
|
|
Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, const Request &r)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string revisionparam = r.get("revision");
|
|
|
|
unsigned int revisionid = 0;
|
|
|
|
if(!revisionparam.empty())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
revisionid = utils::toUInt(revisionparam);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
return errorResponse("Error", "Supplied revisionid is misformated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Revision> revision;
|
|
|
|
std::string templatepartname;
|
2022-08-17 19:35:52 +02:00
|
|
|
auto revisionDao = this->database->createRevisionDao();
|
2018-11-03 17:12:20 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if(revisionid > 0)
|
|
|
|
{
|
2020-09-21 21:44:26 +02:00
|
|
|
if(!effectivePermissions(pagename).canSeePageHistory())
|
|
|
|
{
|
2022-08-17 19:35:52 +02:00
|
|
|
auto current = revisionDao->getCurrentForPage(pagename);
|
2021-03-16 21:05:59 +01:00
|
|
|
if(current && current->revision > revisionid)
|
|
|
|
{
|
|
|
|
return errorResponse("Error", "You are not allowed to view older revisions of this page");
|
|
|
|
}
|
2020-09-21 21:44:26 +02:00
|
|
|
}
|
2022-08-17 19:35:52 +02:00
|
|
|
revision = revisionDao->getRevisionForPage(pagename, revisionid);
|
2018-11-03 17:12:20 +01:00
|
|
|
if(!revision)
|
|
|
|
{
|
|
|
|
return errorResponse("Revision not found", "No such revision found");
|
|
|
|
}
|
|
|
|
templatepartname = "page_view_revision";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!this->userSession->loggedIn)
|
|
|
|
{
|
|
|
|
auto content = this->cache->get("page:foranon:" + pagename);
|
|
|
|
if(content)
|
|
|
|
{
|
|
|
|
Response r;
|
|
|
|
r.setBody(*content);
|
|
|
|
// TODO: etag?
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 19:35:52 +02:00
|
|
|
revision = revisionDao->getCurrentForPage(pagename);
|
2018-11-03 17:12:20 +01:00
|
|
|
templatepartname = "page_view";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const DatabaseException &e)
|
|
|
|
{
|
|
|
|
Logger::error() << "DatabaseException in handlerpageview: " << e.what();
|
|
|
|
return errorResponse("Database error", "While trying to fetch revision, a database error occured");
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser parser;
|
|
|
|
Response result;
|
|
|
|
result.setStatus(200);
|
|
|
|
|
2022-08-20 11:23:27 +02:00
|
|
|
RevisionRenderer revisionRenderer { *this->templ, *this->database, *this->urlProvider };
|
2022-08-17 19:35:52 +02:00
|
|
|
|
|
|
|
|
2022-08-17 22:06:09 +02:00
|
|
|
std::string customtitle = parser.extractCommand("pagetitle", revision->content);
|
2022-08-20 11:23:27 +02:00
|
|
|
std::string parsedcontent = revisionRenderer.renderContent(revision.value(), customtitle);
|
|
|
|
/* TODO: Dynamic includes not considered, probably fine in practise */
|
|
|
|
std::string indexcontent = createIndexContent(parser, revision->content);
|
2022-08-17 22:06:09 +02:00
|
|
|
|
2021-01-29 16:45:04 +01:00
|
|
|
std::string revisionstr = std::to_string(revision->revision);
|
2022-08-20 11:23:27 +02:00
|
|
|
TemplatePage page = this->templ->getPage(templatepartname);
|
2018-11-03 17:12:20 +01:00
|
|
|
page.setVar("content", parsedcontent);
|
|
|
|
page.setVar("index", indexcontent);
|
|
|
|
page.setVar("editedby", revision->author);
|
2022-03-27 08:37:55 +02:00
|
|
|
page.setVar("editedon", utils::toISODateTime(revision->timestamp));
|
2018-11-03 17:12:20 +01:00
|
|
|
page.setVar("historyurl", this->urlProvider->pageHistory(pagename));
|
2021-01-29 16:45:04 +01:00
|
|
|
page.setVar("revision", revisionstr);
|
2018-11-03 17:12:20 +01:00
|
|
|
setPageVars(page, pagename);
|
2022-01-23 10:02:46 +01:00
|
|
|
if(!customtitle.empty())
|
|
|
|
{
|
|
|
|
page.setVar("title", createPageTitle(customtitle));
|
|
|
|
}
|
2018-11-03 17:12:20 +01:00
|
|
|
std::string body = page.render();
|
|
|
|
if(revisionid == 0 && !this->userSession->loggedIn)
|
|
|
|
{
|
|
|
|
this->cache->put("page:foranon:" + pagename, body);
|
|
|
|
}
|
2021-01-29 16:45:04 +01:00
|
|
|
result.addHeader("ETAG", revisionstr + "+" + std::to_string(this->userSession->loggedIn));
|
2018-11-03 17:12:20 +01:00
|
|
|
result.setBody(body);
|
|
|
|
return result;
|
|
|
|
}
|