Initial Commit

This commit is contained in:
Kirigaya Kazuto 2017-11-16 22:23:25 +08:00
commit d2f158c614
2 changed files with 164 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
bin/
obj/
*.dll
*.exe
*.cbp
*.depend
*.layout

157
installer.cpp Normal file
View File

@ -0,0 +1,157 @@
/** bm-do-install.cgi
* Work with bm-install.html --Ajax/Post--> bm-do-install.cgi --Json--> bm-install.html --> Jump to...
*
* Book Manager Quick Installer
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <vector>
#include "Request.h"
#include "Response.h"
#include "Util.h"
#include "MySQLWrapper.h"
#include "json.hpp"
using namespace std;
using json=nlohmann::json;
int doInstall(const string& dbaddr,const string& dbuser,const string& dbpass,const string& dbname,int dbport,const vector<string>& sql,
int& errcode,string& errmsg)
{
MySQLConn conn;
int ret=conn.connect(dbaddr.c_str(),dbuser.c_str(),dbpass.c_str(),dbname.c_str(),dbport);
if(ret==-1)
{
errcode=conn.getErrCode();
errmsg=conn.getError();
return -1;
}
conn.exec("set names utf8",nullptr);
int sz=sql.size();
for(int i=0;i<sz;i++)
{
ret=conn.exec(sql[i],nullptr);
if(ret<0)
{
errcode=conn.getErrCode();
errmsg=conn.getError();
return -2;
}
}
return 0;
}
static vector<string> splitMultiSQL(const string& multisql)
{
vector<string> vec;
int len=multisql.size();
string sql;
for(int i=0;i<len;i++)
{
if(multisql[i]==';')
{
vec.push_back(sql);
sql.clear();
}
else
{
sql.push_back(multisql[i]);
}
}
if(!sql.empty())
{
vec.push_back(sql);
}
return vec;
}
template<typename T>
void _do_make_str(ostringstream& ostr,T&& x)
{
ostr<<x;
}
void _do_make_str(ostringstream& ostr)
{
}
template<typename T,typename... Args>
void _do_make_str(ostringstream& ostr,T&& x,Args&&... args)
{
ostr<<x;
_do_make_str(ostr,args...);
}
template<typename... Args>
string make_str(Args&&... args)
{
ostringstream ostr;
_do_make_str(ostr,args...);
return ostr.str();
}
int main()
{
Request req;
Response res;
res.contentType="text/json;charset=utf-8";
json j;
if(req.requestMethod=="GET")
{
j["success"]=0;
j["errcode"]=1;
j["errmsg"]=make_str("GET Method Not Supported.");
}
else if(req.requestMethod=="POST")
{
if(!req.post["dbaddr"].empty() &&
!req.post["dbport"].empty() &&
!req.post["dbuser"].empty() &&
!req.post["dbpass"].empty() &&
!req.post["dbname"].empty() &&
!req.post["tbprefix"].empty() &&
!req.post["supass"].empty())
{
string s=getFileContent("setup.sql");
replaceFlag(s,"{prefix}",req.post["tbprefix"]);
vector<string> vec=splitMultiSQL(s);
vec.push_back(make_str("insert into ",req.post["tbprefix"],"_user values ('root','",req.post["supass"],"','SuperUser',0,3) "));
int ecode;
string emsg;
int ret=doInstall(req.post["dbaddr"],req.post["dbuser"],req.post["dbpass"],req.post["dbname"],ParseInt(req.post["dbport"]),vec,
ecode,emsg);
if(ret<0)
{
/// Error.
j["success"]=0;
j["errcode"]=ecode;
j["errmsg"]=make_str("SQL Error: ",emsg);
}
else
{
j["success"]=1;
}
}
else
{
j["success"]=0;
j["errcode"]=2;
j["errmsg"]=make_str("Failed to parse parameters. Please retry.");
}
}
res.content.append(j.dump());
res.show();
return 0;
}