summaryrefslogtreecommitdiffstats
path: root/source/WebAdmin.cpp
diff options
context:
space:
mode:
authorFakeTruth <faketruth@gmail.com>2013-07-29 02:37:59 +0200
committerFakeTruth <faketruth@gmail.com>2013-07-29 02:37:59 +0200
commit5a9d4f89c2212c83f05f910b6f2a176f21048ba6 (patch)
tree63414b5adfde3ff229612628a05ac1fbecb11a7b /source/WebAdmin.cpp
parentMerge branch 'master' of github.com:mc-server/MCServer (diff)
downloadcuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar.gz
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar.bz2
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar.lz
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar.xz
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.tar.zst
cuberite-5a9d4f89c2212c83f05f910b6f2a176f21048ba6.zip
Diffstat (limited to 'source/WebAdmin.cpp')
-rw-r--r--source/WebAdmin.cpp302
1 files changed, 195 insertions, 107 deletions
diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp
index dd1a695ee..11a5bd379 100644
--- a/source/WebAdmin.cpp
+++ b/source/WebAdmin.cpp
@@ -13,6 +13,7 @@
#include "Player.h"
#include "Server.h"
#include "Root.h"
+#include "LuaScript.h"
#include "../iniFile/iniFile.h"
@@ -59,6 +60,7 @@ cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
{
WebAdmin = this;
m_Event = new cEvent();
+ m_pTemplate = new cLuaScript();
Init( m_Port );
}
@@ -68,10 +70,12 @@ cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
cWebAdmin::~cWebAdmin()
{
+
WebAdmin = 0;
m_WebServer->Stop();
delete m_WebServer;
+ delete m_pTemplate;
delete m_IniFile;
m_Event->Wait();
@@ -146,40 +150,17 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
bDontShowTemplate = true;
}
- std::string UserPassword = WebAdmin->m_IniFile->GetValue( "User:"+r->username_, "Password", "");
+ AString UserPassword = WebAdmin->m_IniFile->GetValue( "User:"+r->username_, "Password", "");
if ((UserPassword != "") && (r->password_ == UserPassword))
{
- std::string BaseURL = "./";
- if (Split.size() > 1)
- {
- for (unsigned int i = 0; i < Split.size(); i++)
- {
- BaseURL += "../";
- }
- BaseURL += "webadmin/";
- }
+ AString Template;
- std::string Menu;
- std::string Content;
- std::string Template = bDontShowTemplate ? "{CONTENT}" : WebAdmin->GetTemplate();
- std::string FoundPlugin;
-
- for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
- {
- cWebPlugin* WebPlugin = *itr;
- std::list< std::pair<std::string, std::string> > NameList = WebPlugin->GetTabNames();
- for( std::list< std::pair<std::string, std::string> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names )
- {
- Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
- }
- }
-
- HTTPRequest Request;
- Request.Username = r->username_;
- Request.Method = r->method_;
- Request.Params = r->params_;
- Request.PostParams = r->params_post_;
- Request.Path = r->path_.substr(1);
+ HTTPTemplateRequest TemplateRequest;
+ TemplateRequest.Request.Username = r->username_;
+ TemplateRequest.Request.Method = r->method_;
+ TemplateRequest.Request.Params = r->params_;
+ TemplateRequest.Request.PostParams = r->params_post_;
+ TemplateRequest.Request.Path = r->path_.substr(1);
for( unsigned int i = 0; i < r->multipart_formdata_.size(); ++i )
{
@@ -190,101 +171,113 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
HTTPfd.Type = fd.content_type_;
HTTPfd.Name = fd.name_;
LOGINFO("Form data name: %s", fd.name_.c_str() );
- Request.FormData[ fd.name_ ] = HTTPfd;
+ TemplateRequest.Request.FormData[ fd.name_ ] = HTTPfd;
}
- if (Split.size() > 1)
+ bool bLuaTemplateSuccessful = false;
+ if (!bDontShowTemplate)
+ {
+ // New Lua web template
+ bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallFunction("ShowPage", sLuaUsertype(WebAdmin, "cWebAdmin"), sLuaUsertype(&TemplateRequest, "HTTPTemplateRequest"), Template);
+ }
+
+ if (!bLuaTemplateSuccessful)
{
+ AString BaseURL = WebAdmin->GetBaseURL(Split);
+ AString Menu;
+ Template = bDontShowTemplate ? "{CONTENT}" : WebAdmin->GetTemplate();
+ AString FoundPlugin;
+
for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
{
- if ((*itr)->GetWebTitle() == Split[1])
+ cWebPlugin* WebPlugin = *itr;
+ std::list< std::pair<AString, AString> > NameList = WebPlugin->GetTabNames();
+ for( std::list< std::pair<AString, AString> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names )
{
- Content = (*itr)->HandleWebRequest(&Request);
- cWebPlugin * WebPlugin = *itr;
- FoundPlugin = WebPlugin->GetWebTitle();
- AString TabName = WebPlugin->GetTabNameForRequest(&Request).first;
- if (!TabName.empty())
- {
- FoundPlugin += " - " + TabName;
- }
- break;
+ Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
}
}
- }
- if( FoundPlugin.empty() ) // Default page
- {
- Content.clear();
- FoundPlugin = "Current Game";
- Content += "<h4>Server Name:</h4>";
- Content += "<p>" + std::string( cRoot::Get()->GetServer()->GetServerID() ) + "</p>";
-
- Content += "<h4>Plugins:</h4><ul>";
- cPluginManager* PM = cRoot::Get()->GetPluginManager();
- if( PM )
+ sWebAdminPage Page = WebAdmin->GetPage(TemplateRequest.Request);
+ AString Content = Page.Content;
+ FoundPlugin = Page.PluginName;
+ if (!Page.TabName.empty())
+ FoundPlugin += " - " + Page.TabName;
+
+ if( FoundPlugin.empty() ) // Default page
{
- const cPluginManager::PluginMap & List = PM->GetAllPlugins();
- for( cPluginManager::PluginMap::const_iterator itr = List.begin(); itr != List.end(); ++itr )
+ Content.clear();
+ FoundPlugin = "Current Game";
+ Content += "<h4>Server Name:</h4>";
+ Content += "<p>" + AString( cRoot::Get()->GetServer()->GetServerID() ) + "</p>";
+
+ Content += "<h4>Plugins:</h4><ul>";
+ cPluginManager* PM = cRoot::Get()->GetPluginManager();
+ if( PM )
{
- if( itr->second == NULL ) continue;
- AString VersionNum;
- AppendPrintf(Content, "<li>%s V.%i</li>", itr->second->GetName().c_str(), itr->second->GetVersion());
+ const cPluginManager::PluginMap & List = PM->GetAllPlugins();
+ for( cPluginManager::PluginMap::const_iterator itr = List.begin(); itr != List.end(); ++itr )
+ {
+ if( itr->second == NULL ) continue;
+ AString VersionNum;
+ AppendPrintf(Content, "<li>%s V.%i</li>", itr->second->GetName().c_str(), itr->second->GetVersion());
+ }
}
- }
- Content += "</ul>";
- Content += "<h4>Players:</h4><ul>";
+ Content += "</ul>";
+ Content += "<h4>Players:</h4><ul>";
- cPlayerAccum PlayerAccum;
- cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
- if( World != NULL )
- {
- World->ForEachPlayer(PlayerAccum);
- Content.append(PlayerAccum.m_Contents);
+ cPlayerAccum PlayerAccum;
+ cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
+ if( World != NULL )
+ {
+ World->ForEachPlayer(PlayerAccum);
+ Content.append(PlayerAccum.m_Contents);
+ }
+ Content += "</ul><br>";
}
- Content += "</ul><br>";
- }
-
+
- if (!bDontShowTemplate && (Split.size() > 1))
- {
- Content += "\n<p><a href='" + BaseURL + "'>Go back</a></p>";
- }
+ if (!bDontShowTemplate && (Split.size() > 1))
+ {
+ Content += "\n<p><a href='" + BaseURL + "'>Go back</a></p>";
+ }
- // mem usage
+ // mem usage
#ifndef _WIN32
- rusage resource_usage;
- if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
- {
- ReplaceString( Template, std::string("{MEM}"), "Error :(" );
- }
- else
- {
- AString MemUsage;
- Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
- ReplaceString(Template, std::string("{MEM}"), MemUsage);
- }
+ rusage resource_usage;
+ if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
+ {
+ ReplaceString( Template, AString("{MEM}"), "Error :(" );
+ }
+ else
+ {
+ AString MemUsage;
+ Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
+ ReplaceString(Template, AString("{MEM}"), MemUsage);
+ }
#else
- HANDLE hProcess = GetCurrentProcess();
- PROCESS_MEMORY_COUNTERS pmc;
- if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
- {
- AString MemUsage;
- Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
- ReplaceString( Template, "{MEM}", MemUsage );
- }
+ HANDLE hProcess = GetCurrentProcess();
+ PROCESS_MEMORY_COUNTERS pmc;
+ if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
+ {
+ AString MemUsage;
+ Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
+ ReplaceString( Template, "{MEM}", MemUsage );
+ }
#endif
- // end mem usage
+ // end mem usage
- ReplaceString( Template, "{USERNAME}", r->username_ );
- ReplaceString( Template, "{MENU}", Menu );
- ReplaceString( Template, "{PLUGIN_NAME}", FoundPlugin );
- ReplaceString( Template, "{CONTENT}", Content );
- ReplaceString( Template, "{TITLE}", "MCServer" );
+ ReplaceString( Template, "{USERNAME}", r->username_ );
+ ReplaceString( Template, "{MENU}", Menu );
+ ReplaceString( Template, "{PLUGIN_NAME}", FoundPlugin );
+ ReplaceString( Template, "{CONTENT}", Content );
+ ReplaceString( Template, "{TITLE}", "MCServer" );
- AString NumChunks;
- Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
- ReplaceString(Template, "{NUMCHUNKS}", NumChunks);
+ AString NumChunks;
+ Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
+ ReplaceString(Template, "{NUMCHUNKS}", NumChunks);
+ }
r->answer_ = Template;
}
@@ -309,6 +302,14 @@ bool cWebAdmin::Init( int a_Port )
m_Port = m_IniFile->GetValueI("WebAdmin", "Port", 8080 );
}
+ // Initialize the WebAdmin template script and load the file
+ m_pTemplate->Initialize();
+ if (!m_pTemplate->LoadFile( FILE_IO_PREFIX "webadmin/template.lua") || !m_pTemplate->Execute())
+ {
+ LOGWARN("Could not load WebAdmin template.");
+ }
+
+
LOG("Starting WebAdmin on port %i", m_Port);
#ifdef _WIN32
@@ -354,9 +355,9 @@ void *cWebAdmin::ListenThread( void *lpParam )
-std::string cWebAdmin::GetTemplate()
+AString cWebAdmin::GetTemplate()
{
- std::string retVal = "";
+ AString retVal = "";
char SourceFile[] = "webadmin/template.html";
@@ -370,4 +371,91 @@ std::string cWebAdmin::GetTemplate()
f.ReadRestOfFile(retVal);
return retVal;
-} \ No newline at end of file
+}
+
+
+
+
+
+sWebAdminPage cWebAdmin::GetPage(const HTTPRequest& a_Request)
+{
+ sWebAdminPage Page;
+ AStringVector Split = StringSplit(a_Request.Path, "/");
+
+ // Find the plugin that corresponds to the requested path
+ AString FoundPlugin;
+ if (Split.size() > 1)
+ {
+ for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
+ {
+ if ((*itr)->GetWebTitle() == Split[1])
+ {
+ Page.Content = (*itr)->HandleWebRequest(&a_Request);
+ cWebPlugin * WebPlugin = *itr;
+ FoundPlugin = WebPlugin->GetWebTitle();
+ AString TabName = WebPlugin->GetTabNameForRequest(&a_Request).first;
+ Page.PluginName = FoundPlugin;
+ Page.TabName = TabName;
+ break;
+ }
+ }
+ }
+
+ // Return the page contents
+ return Page;
+}
+
+
+
+
+
+AString cWebAdmin::GetBaseURL( const AString& a_URL )
+{
+ return GetBaseURL(StringSplit(a_URL, "/"));
+}
+
+
+
+
+
+AString cWebAdmin::GetBaseURL( const AStringVector& a_URLSplit )
+{
+ AString BaseURL = "./";
+ if (a_URLSplit.size() > 1)
+ {
+ for (unsigned int i = 0; i < a_URLSplit.size(); i++)
+ {
+ BaseURL += "../";
+ }
+ BaseURL += "webadmin/";
+ }
+ return BaseURL;
+}
+
+
+
+
+
+AString cWebAdmin::GetMemoryUsage() const
+{
+ AString MemUsage;
+#ifndef _WIN32
+ rusage resource_usage;
+ if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
+ {
+ MemUsage = "Error :(";
+ }
+ else
+ {
+ Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
+ }
+#else
+ HANDLE hProcess = GetCurrentProcess();
+ PROCESS_MEMORY_COUNTERS pmc;
+ if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
+ {
+ Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
+ }
+#endif
+ return MemUsage;
+}