2
* Copyright (C) 2004-2009 See the AUTHORS file for details.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 as published
6
* by the Free Software Foundation.
15
#include "FileUtils.h"
21
typedef CSmartPtr<CWebSubPage> TWebSubPage;
22
typedef vector<TWebSubPage> VWebSubPages;
24
class CZNCTagHandler : public CTemplateTagHandler {
26
CZNCTagHandler(CWebSock& pWebSock);
27
virtual ~CZNCTagHandler() {}
29
virtual bool HandleTag(CTemplate& Tmpl, const CString& sName, const CString& sArgs, CString& sOutput);
37
CWebSession(const CString& sId);
38
virtual ~CWebSession() {}
40
const CString& GetId() const { return m_sId; }
41
CUser* GetUser() const { return m_pUser; }
42
bool IsLoggedIn() const { return m_pUser != NULL; }
45
CUser* SetUser(CUser* p) { m_pUser = p; return m_pUser; }
47
void ClearMessageLoops();
48
void FillMessageLoops(CTemplate& Tmpl);
49
size_t AddError(const CString& sMessage);
50
size_t AddSuccess(const CString& sMessage);
54
VCString m_vsErrorMsgs;
55
VCString m_vsSuccessMsgs;
61
CWebSubPage(const CString& sName, const CString& sTitle = "", unsigned int uFlags = 0) : m_sName(sName), m_sTitle(sTitle) {
65
CWebSubPage(const CString& sName, const CString& sTitle, const VPair& vParams, unsigned int uFlags = 0) : m_sName(sName), m_sTitle(sTitle), m_vParams(vParams) {
69
virtual ~CWebSubPage() {}
75
void SetName(const CString& s) { m_sName = s; }
76
void SetTitle(const CString& s) { m_sTitle = s; }
77
void AddParam(const CString& sName, const CString& sValue) { m_vParams.push_back(make_pair(sName, sValue)); }
79
bool RequiresAdmin() const { return m_uFlags & F_ADMIN; }
81
const CString& GetName() const { return m_sName; }
82
const CString& GetTitle() const { return m_sTitle; }
83
const VPair& GetParams() const { return m_vParams; }
85
unsigned int m_uFlags;
91
class CWebAuth : public CAuthBase {
93
CWebAuth(CWebSock* pWebSock, const CString& sUsername, const CString& sPassword);
94
virtual ~CWebAuth() {}
96
void SetWebSock(CWebSock* pWebSock) { m_pWebSock = pWebSock; }
97
void AcceptedLogin(CUser& User);
98
void RefusedLogin(const CString& sReason);
102
CWebSock* m_pWebSock;
105
class CWebSessionMap : public TCacheMap<CString, CSmartPtr<CWebSession> > {
107
CWebSessionMap(unsigned int uTTL = 5000) : TCacheMap<CString, CSmartPtr<CWebSession> >(uTTL) {}
108
void FinishUserSessions(const CUser& User);
111
class CWebSock : public CHTTPSock {
113
enum EPageReqResult {
114
PAGE_NOTFOUND, // print 404 and Close()
115
PAGE_PRINT, // print page contents and Close()
116
PAGE_DEFERRED, // async processing, Close() will be called from a different place
117
PAGE_DONE // all stuff has been done
120
CWebSock(CModule* pModule);
121
CWebSock(CModule* pModule, const CString& sHostname, unsigned short uPort, int iTimeout = 60);
124
virtual bool ForceLogin();
125
virtual bool OnLogin(const CString& sUser, const CString& sPass);
126
virtual void OnPageRequest(const CString& sURI);
128
void ParsePath(); // This parses the path portion of the url into some member vars
129
CModule* ResolveModule();
131
//virtual bool PrintFile(const CString& sFileName, CString sContentType = "");
132
EPageReqResult PrintTemplate(const CString& sPageName, CString& sPageRet, CModule* pModule = NULL);
133
EPageReqResult PrintStaticFile(const CString& sPath, CString& sPageRet, CModule* pModule = NULL);
135
bool AddModLoop(const CString& sLoopName, CModule& Module);
136
void SetPaths(CModule* pModule, bool bIsTemplate = false);
139
void FillErrorPage(CString& sPageRet, const CString& sError) {
140
m_Template["Action"] = "error";
141
m_Template["Title"] = "Error";
142
m_Template["Error"] = sError;
144
PrintTemplate("error", sPageRet);
147
void PrintErrorPage(const CString& sMessage);
149
CSmartPtr<CWebSession> GetSession();
150
CString GetCSRFCheck();
152
virtual Csock* GetSockObj(const CString& sHost, unsigned short uPort);
153
CString GetSkinPath(const CString& sSkinName) const;
154
CModule* GetModule() const { return (CModule*) m_pModule; }
155
size_t GetAvailSkins(vector<CFile>& vRet);
156
CString GetSkinName();
158
CString GetRequestCookie(const CString& sKey) const;
159
bool SendCookie(const CString& sKey, const CString& sValue);
161
static void FinishUserSessions(const CUser& User) {
162
m_mspSessions.FinishUserSessions(User);
166
EPageReqResult OnPageRequestInternal(const CString& sURI, CString& sPageRet);
169
CTemplate m_Template;
170
CSmartPtr<CAuthBase> m_spAuth;
171
CString m_sForceUser; // Gets filled by ResolveModule()
172
CString m_sModName; // Gets filled by ResolveModule()
173
CString m_sPath; // Gets filled by ResolveModule()
174
CString m_sPage; // Gets filled by ResolveModule()
175
CSmartPtr<CWebSession> m_spSession;
177
static CWebSessionMap m_mspSessions;
180
#endif // !_WEBMODULES_H