Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*******************************************************************************
Copyright (c) 2021, Jan Koester jan.koester@gmx.net
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#include <iostream>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <netplus/eventapi.h>
#include <netplus/exception.h>
#include <cryptplus/cryptplus.h>
#include <httppp/exception.h>
#include <httppp/http.h>
#include <httppp/httpd.h>
#include <htmlpp/html.h>
#include <htmlpp/exception.h>
#include <cmdplus/cmdplus.h>
#include "session.h"
#include "auth.h"
#include "database.h"
#include "conf.h"
#include "blogi.h"
#include "pgsql.cpp"
blogi::Blogi::Blogi(netplus::socket *serversocket) : event(serversocket){
PlgArgs = new PluginArgs;
PlgArgs->config=Config::getInstance();
PlgArgs->database= new Postgresql(PlgArgs->config->getdbopts());
PlgArgs->session= new Session();
PlgArgs->auth=new Auth(PlgArgs->database,PlgArgs->session);
TemplateConfig tplcfg;
tplcfg.config=Config::getInstance();
tplcfg.Theme=tplcfg.config->gettemplate();
tplcfg.TDatabase=PlgArgs->database;
BlogiPlg = new Plugin();
for(int i=0; i<PlgArgs->config->getplgdirs(); ++i){
BlogiPlg->loadPlugins(PlgArgs->config->getplgdir(i),PlgArgs);
}
}
blogi::Blogi::~Blogi(){
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
delete PlgArgs->auth;
delete PlgArgs->session;
delete PlgArgs->database;
delete BlogiPlg;
delete PlgArgs;
}
void blogi::Blogi::loginPage(netplus::con*curcon,libhttppp::HttpRequest *curreq){
char url[512];
libhttppp::HTTPException excep;
std::string sessid;
if(PlgArgs->auth->isLoggedIn(curreq,sessid)){
libhttppp::HTTPException err;
err[libhttppp::HTTPException::Error] << "you already authenticated please logoff before you login again!";
throw err;
}
libhttppp::HttpForm curform;
curform.parse(curreq);
const char *username=nullptr;
const char *password=nullptr;
if (curform.getUrlcodedFormData()) {
for (libhttppp::HttpForm::UrlcodedFormData* cururlform = curform.getUrlcodedFormData(); cururlform;
cururlform = cururlform->nextUrlcodedFormData()) {
if (strcmp(cururlform->getKey(), "username") == 0) {
username = cururlform->getValue();
}
else if (strcmp(cururlform->getKey(), "password") == 0) {
password = cururlform->getValue();
}
libhtmlpp::HtmlString condat;
condat << "<div id=\"content\">"
<< "<span>Login</span>"
<< "<form action=\""<< PlgArgs->config->buildurl("login",url,512) << "\" method=\"post\">"
<< "username:<br> <input type=\"text\" name=\"username\" value=\"\"><br>"
<< "password:<br> <input type=\"password\" name=\"password\" value=\"\"><br>"
<< "<button type=\"submit\">Submit</button>"
<< "</form>"
<< "</div>";
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
}
PlgArgs->theme->printSite(out,index,curreq->getRequestURL(),false);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
libhttppp::HttpResponse curres;
curres.setState(HTTP200);
curres.setVersion(HTTPVERSION(1.1));
curres.setContentType("text/html");
curres.send(curcon,out.c_str(),out.length());
return;
}
std::string sid;
if(PlgArgs->auth->login(username,password,sid)){
const char *sessid = PlgArgs->session->createSession(sid.c_str());
PlgArgs->session->addSessionData(sessid,"sid",sid.c_str(),sid.length());
PlgArgs->session->addSessionData(sessid,"username",username, strlen(username));
libhttppp::HttpResponse curres;
libhttppp::HttpCookie cookie;
cookie.setcookie(&curres, "sessionid", sessid);
curres.setState(HTTP307);
curres.setVersion(HTTPVERSION(1.1));
*curres.setData("Location") << PlgArgs->config->getstartpage();
curres.setContentType("text/html");
curres.send(curcon, nullptr, 0);
}else{
libhttppp::HTTPException err;
err[libhttppp::HTTPException::Error] << "Login Failed!";
throw err;
}
}
void blogi::Blogi::logoutPage(netplus::con *curcon,libhttppp::HttpRequest *curreq){
const char *host;
for(libhttppp::HttpHeader::HeaderData *preq = curreq->getfirstHeaderData(); preq; preq=curreq->nextHeaderData(preq)){
if(strncmp(curreq->getKey(preq),"Host",4)==0)
host=curreq->getValue(preq);
}
libhttppp::HttpResponse curres;
libhttppp::HttpCookie cookie;
cookie.setcookie(&curres,"sessionid","empty");
curres.setState(HTTP307);
curres.setVersion(HTTPVERSION(1.1));
*curres.setData("Location") << PlgArgs->config->getstartpage();
curres.setContentType("text/html");
curres.setContentLength(0);
curres.send(curcon,nullptr,0);
}
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
void blogi::Blogi::settingsPage(netplus::con* curcon, libhttppp::HttpRequest* curreq){
libhttppp::HttpCookie cookie;
cookie.parse(curreq);
std::string sessid;
if(!PlgArgs->auth->isLoggedIn(curreq,sessid)){
libhttppp::HTTPException err;
err[libhttppp::HTTPException::Error] << "you are not logged in permission denied";
throw err;
}
std::string out;
libhtmlpp::HtmlString setgui;
char url[512];
setgui << "<div id=\"settings\"><table><tr><td id=\"setnav\" ><ul>";
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
if(curplg->getInstace()->haveSettings())
setgui << "<li><a href=\"" << PlgArgs->config->buildurl("settings/",url,512) << curplg->getInstace()->getName() << "\">" << curplg->getInstace()->getName() << "</a></li>";
}
setgui << "</ul></td><td id=\"setcontent\">";
int relen=strlen(PlgArgs->config->buildurl("settings/",url,512))-strlen(curreq->getRequestURL());
if( relen < 0){
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
if(strncmp(curreq->getRequestURL()+strlen(PlgArgs->config->buildurl("settings/",url,512)), curplg->getInstace()->getName(),
strlen(curplg->getInstace()->getName()))==0)
curplg->getInstace()->Settings(curreq,setgui);
}
}
setgui << "</td></tr></table></div>";
index.getElementbyID("main")->appendChild(setgui.parse());
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
}
PlgArgs->theme->printSite(out,index,curreq->getRequestURL(),false);
libhttppp::HttpResponse curres;
curres.setState(HTTP200);
curres.setVersion(HTTPVERSION(1.1));
curres.setContentType("text/html");
curres.send(curcon,out.c_str(),out.length());
}
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
void blogi::Blogi::RequestEvent(netplus::con *curcon){
libhttppp::HttpRequest req;
char url[512];
try{
req.parse(curcon);
libhttppp::HttpHeader::HeaderData *hip=req.getData("x-real-ip");
libhttppp::HttpHeader::HeaderData *useragent=req.getData("user-agent");
if(hip)
std::cout <<"Request from: " << req.getData(hip) << " url: " << req.getRequestURL();
if(useragent)
std::cout << " agent: " << req.getData(useragent);
std::cout << std::endl;
/*blogi internal pages and redirections*/
if(strcmp(req.getRequestURL(),"/")==0 || strcmp(req.getRequestURL(),PlgArgs->config->getprefix())==0){
libhttppp::HttpResponse curres;
curres.setState(HTTP307);
curres.setVersion(HTTPVERSION(1.1));
*curres.setData("Location") << PlgArgs->config->buildurl("content/tag",url,512);
curres.setContentType("text/html");
curres.send(curcon, nullptr, 0);
return;
}else if(strncmp(req.getRequestURL(),PlgArgs->config->buildurl("logout",url,512),strlen(PlgArgs->config->buildurl("logout",url,512)))==0){
logoutPage(curcon,&req);
return;
}else if(strncmp(req.getRequestURL(),PlgArgs->config->buildurl("login",url,512),strlen(PlgArgs->config->buildurl("login",url,512)))==0){
loginPage(curcon,&req);
return;
}else if(strncmp(req.getRequestURL(),PlgArgs->config->buildurl("settings",url,512),strlen(PlgArgs->config->buildurl("settings",url,512)))==0){
settingsPage(curcon,&req);
return;
}else if(strncmp(req.getRequestURL(),PlgArgs->config->buildurl("editor",url,512),strlen(PlgArgs->config->buildurl("editor",url,512)))==0){
PlgArgs->edit->Controller(curcon,&req);
return;
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
PluginApi *api=curplg->getInstace();
std::string url=PlgArgs->config->getprefix();
url+="/";
url+=api->getName();
if(strncmp(req.getRequestURL(),url.c_str(),url.length())==0){
return;
}
}
std::string output;
libhtmlpp::HtmlString err;
err << "<!DOCTYPE html><html><body style=\"color:rgb(238, 238, 238); background:rgb(35, 38, 39);\"><span>"
<< "Seite oder Inhalt nicht gefudnen"
<< "</span><br/><a style=\"text-decoration: none; color: rgb(58,212, 58);\" href=\""
<< PlgArgs->config->getstartpage()
<< "\" >Zurück zur Startseite</a></body></html>";
libhtmlpp::print(err.parse(),nullptr,output);
libhttppp::HttpResponse resp;
resp.setVersion(HTTPVERSION(1.1));
resp.setState(HTTP404);
resp.setContentType("text/html");
resp.send(curcon,output.c_str(),output.length());
}
}catch(libhttppp::HTTPException &e){
std::string output;
libhtmlpp::HtmlString err,hreason;
libhtmlpp::HtmlEncode(e.what(),hreason);
err << "<!DOCTYPE html><html><body style=\"color:rgb(238, 238, 238); background:rgb(35, 38, 39);\"><span>"
<< "</span><br/><a style=\"text-decoration: none; color: rgb(58,212, 58);\" href=\""
<< PlgArgs->config->getstartpage()
<< "\" >Zurück zur Startseite</a></body></html>";
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
libhtmlpp::print(err.parse(),nullptr,output);
libhttppp::HttpResponse resp;
resp.setVersion(HTTPVERSION(1.1));
resp.setState(HTTP500);
resp.setContentType("text/html");
resp.send(curcon,output.c_str(),output.length());
}
}
class HttpConD : public libhttppp::HttpD {
public:
HttpConD(const char *httpaddr, int port,int maxconnections,const char *sslcertpath,const char *sslkeypath)
: HttpD(httpaddr,port,maxconnections,sslcertpath,sslkeypath){
libhttppp::HTTPException httpexception;
try {
blogi::Session session;
blogi::Blogi blg(getServerSocket());
blg.runEventloop();
}catch(netplus::NetException &e){
std::cout << e.what() << std::endl;
}
};
private:
};
int main(int argc, char** argv){
signal(SIGPIPE, SIG_IGN);
cmdplus::CmdController *BlogiCmdCtl;
BlogiCmdCtl = &cmdplus::CmdController::getInstance();
BlogiCmdCtl->registerCmd("help", 'h', false, (const char*) nullptr, "Helpmenu");
if (BlogiCmdCtl->getCmdbyKey("help") && BlogiCmdCtl->getCmdbyKey("help")->getFound()) {
BlogiCmdCtl->printHelp();
return 0;
}
BlogiCmdCtl->registerCmd("config",'c', true,(const char*) nullptr,"Blogi Config File");
BlogiCmdCtl->parseCmd(argc,argv);
const char *config = BlogiCmdCtl->getCmdbyKey("config")->getValue();
blogi::Config *cins;
cins=blogi::Config::getInstance();
cins->loadconfig(config);
HttpConD(cins->gethttpaddr(),
cins->gethttpport(),
cins->gethttpmaxcon(),
nullptr,
nullptr
);
}