version 0.3.29
[fms.git] / src / http / fmshttprequesthandlerfactory.cpp
1 #include "../../include/http/fmshttprequesthandlerfactory.h"\r
2 #include "../../include/option.h"\r
3 #include "../../include/stringfunctions.h"\r
4 #include "../../include/http/pages/homepage.h"\r
5 #include "../../include/http/pages/optionspage.h"\r
6 #include "../../include/http/pages/createidentitypage.h"\r
7 #include "../../include/http/pages/localidentitiespage.h"\r
8 #include "../../include/http/pages/confirmpage.h"\r
9 #include "../../include/http/pages/showcaptchapage.h"\r
10 #include "../../include/http/pages/announceidentitypage.h"\r
11 #include "../../include/http/pages/execquerypage.h"\r
12 #include "../../include/http/pages/boardspage.h"\r
13 #include "../../include/http/pages/insertedfilespage.h"\r
14 #include "../../include/http/pages/addpeerpage.h"\r
15 #include "../../include/http/pages/peerdetailspage.h"\r
16 #include "../../include/http/pages/controlboardpage.h"\r
17 #include "../../include/http/pages/peermaintenancepage.h"\r
18 #include "../../include/http/pages/peertrustpage.h"\r
19 #include "../../include/http/pages/versioninfopage.h"\r
20 #include "../../include/http/pages/recentlyaddedpage.h"\r
21 #include "../../include/http/pages/forummainpage.h"\r
22 #include "../../include/http/pages/showimagepage.h"\r
23 #include "../../include/http/pages/forumthreadspage.h"\r
24 #include "../../include/http/pages/forumviewthreadpage.h"\r
25 #include "../../include/http/pages/forumcreatepostpage.h"\r
26 //ROBERT CHANGE\r
27 #include "../../include/http/pages/showpendingmessagepage.h"\r
28 \r
29 #include <cstdio>\r
30 \r
31 FMSHTTPRequestHandlerFactory::FMSHTTPRequestHandlerFactory(SQLite3DB::DB *db):IDatabase(db)\r
32 {\r
33         Option option(m_db);\r
34 \r
35         // set template\r
36         std::string templatestr="<html><head></head><body><a href=\"home.htm\">Home</a><br><h1>Could not open template.htm!  Place in program directory and restart!</h1><br>[CONTENT]</body></html>";\r
37         FILE *infile=fopen("template.htm","rb");\r
38         if(infile)\r
39         {\r
40                 fseek(infile,0,SEEK_END);\r
41                 long len=ftell(infile);\r
42                 std::vector<char> data(len,0);\r
43                 fseek(infile,0,SEEK_SET);\r
44                 fread(&data[0],1,len,infile);\r
45                 fclose(infile);\r
46                 templatestr.assign(data.begin(),data.end());\r
47         }\r
48         else\r
49         {\r
50                 m_log->error("HTTPThread::HTTPThread could not open template.htm");\r
51         }\r
52 \r
53         // load forum template\r
54         std::string forumtemplate="<html><head></head><body><a href=\"home.htm\">Home</a><br><h1>Could not open forum-template.htm!  Place in program directory and restart!</h1><br>[CONTENT]</body></html>";\r
55         infile=fopen("forum-template.htm","rb");\r
56         if(infile)\r
57         {\r
58                 fseek(infile,0,SEEK_END);\r
59                 long len=ftell(infile);\r
60                 std::vector<char> data(len,0);\r
61                 fseek(infile,0,SEEK_SET);\r
62                 fread(&data[0],1,len,infile);\r
63                 fclose(infile);\r
64                 forumtemplate.assign(data.begin(),data.end());\r
65         }\r
66         else\r
67         {\r
68                 m_log->error("HTTPThread::HTTPThread could not open forum-template.htm");\r
69         }\r
70 \r
71         // push back page handlers\r
72         m_pagehandlers.push_back(new OptionsPage(m_db,templatestr));\r
73         m_pagehandlers.push_back(new CreateIdentityPage(m_db,templatestr));\r
74         m_pagehandlers.push_back(new LocalIdentitiesPage(m_db,templatestr));\r
75         m_pagehandlers.push_back(new ConfirmPage(m_db,templatestr));\r
76         m_pagehandlers.push_back(new ShowCaptchaPage(m_db));\r
77         m_pagehandlers.push_back(new AnnounceIdentityPage(m_db,templatestr));\r
78         m_pagehandlers.push_back(new ExecQueryPage(m_db,templatestr));\r
79         m_pagehandlers.push_back(new BoardsPage(m_db,templatestr));\r
80         m_pagehandlers.push_back(new InsertedFilesPage(m_db,templatestr));\r
81         m_pagehandlers.push_back(new AddPeerPage(m_db,templatestr));\r
82         m_pagehandlers.push_back(new PeerDetailsPage(m_db,templatestr));\r
83         m_pagehandlers.push_back(new ControlBoardPage(m_db,templatestr));\r
84         m_pagehandlers.push_back(new PeerMaintenancePage(m_db,templatestr));\r
85         m_pagehandlers.push_back(new PeerTrustPage(m_db,templatestr));\r
86         m_pagehandlers.push_back(new VersionInfoPage(m_db,templatestr));\r
87         m_pagehandlers.push_back(new RecentlyAddedPage(m_db,templatestr));\r
88         m_pagehandlers.push_back(new ShowImagePage(m_db));\r
89         m_pagehandlers.push_back(new ForumMainPage(m_db,forumtemplate));\r
90         m_pagehandlers.push_back(new ForumThreadsPage(m_db,forumtemplate));\r
91         m_pagehandlers.push_back(new ForumViewThreadPage(m_db,forumtemplate));\r
92         m_pagehandlers.push_back(new ForumCreatePostPage(m_db,forumtemplate));\r
93         //ROBERT CHANGE\r
94         m_pagehandlers.push_back(new ShowPendingMessagePage(m_db,templatestr));\r
95         // homepage must be last - catch all page handler\r
96         m_pagehandlers.push_back(new HomePage(m_db,templatestr));\r
97 \r
98         // initialize the access control list\r
99         std::string aclstr;\r
100         std::vector<std::string> aclparts;\r
101         option.Get("HTTPAccessControl",aclstr);\r
102         StringFunctions::Split(aclstr,",",aclparts);\r
103         for(std::vector<std::string>::iterator i=aclparts.begin(); i!=aclparts.end(); i++)\r
104         {\r
105                 m_acl.Add((*i));\r
106         }\r
107 }\r
108 \r
109 FMSHTTPRequestHandlerFactory::~FMSHTTPRequestHandlerFactory()\r
110 {\r
111 \r
112         for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
113         {\r
114                 delete (*i);\r
115         }\r
116 \r
117 }\r
118 \r
119 Poco::Net::HTTPRequestHandler *FMSHTTPRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &request)\r
120 {\r
121         if(m_acl.IsAllowed(request.clientAddress().host()))\r
122         {\r
123                 for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
124                 {\r
125                         if((*i)->WillHandleURI(request.getURI()))\r
126                         {\r
127                                 // we need to return a new object because the HTTPServer will destory it when it's done.\r
128                                 return (*i)->New();\r
129                         }\r
130                 }\r
131         }\r
132         else\r
133         {\r
134                 m_log->debug("FMSHTTPRequestHandlerFactory::createRequestHandler host denied access "+request.clientAddress().host().toString());\r
135         }\r
136         return 0;\r
137 }\r