2017-11-16 22:23:25 +08:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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["supass"].empty())
|
|
|
|
{
|
|
|
|
string s=getFileContent("setup.sql");
|
|
|
|
vector<string> vec=splitMultiSQL(s);
|
2017-11-17 14:32:28 +08:00
|
|
|
vec.push_back(make_str("insert into bs_user values ('root','",req.post["supass"],"','SuperUser',0,3) "));
|
2017-11-16 22:23:25 +08:00
|
|
|
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;
|
2017-11-17 14:32:28 +08:00
|
|
|
WriteDBConfig(req.post["dbaddr"],req.post["dbuser"],req.post["dbpass"],req.post["dbname"],ParseInt(req.post["dbport"]));
|
2017-11-16 22:23:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j["success"]=0;
|
|
|
|
j["errcode"]=2;
|
|
|
|
j["errmsg"]=make_str("Failed to parse parameters. Please retry.");
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 14:32:28 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
j["success"]=0;
|
|
|
|
j["errcode"]=3;
|
|
|
|
j["errmsg"]=make_str("Unknown request method.");
|
|
|
|
}
|
2017-11-16 22:23:25 +08:00
|
|
|
res.content.append(j.dump());
|
|
|
|
|
|
|
|
res.show();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|