Move Files. Update Submodules. Update Web Pages
This commit is contained in:
parent
688160b139
commit
ec42d7db08
|
@ -1 +1 @@
|
|||
Subproject commit 7c5558ea3fe2240b61b18849ce68535a89de95c2
|
||||
Subproject commit af77d51fd8071cd598c94ab8a49a632f5ccdca8d
|
|
@ -1,58 +0,0 @@
|
|||
#include "MySQLTransaction.h"
|
||||
|
||||
Transaction::Transaction(MySQLConn& MySQLConnection) : conn(MySQLConnection)
|
||||
{
|
||||
_isReady=false;
|
||||
_operated=false;
|
||||
if(conn.exec("start transaction",nullptr)<0)
|
||||
{
|
||||
/// SQL execute error.
|
||||
}
|
||||
else
|
||||
{
|
||||
_isReady=true;
|
||||
}
|
||||
}
|
||||
|
||||
Transaction::~Transaction()
|
||||
{
|
||||
if(!_operated)
|
||||
{
|
||||
rollback();
|
||||
}
|
||||
}
|
||||
|
||||
int Transaction::commit()
|
||||
{
|
||||
if(_operated) return -2;
|
||||
|
||||
if(conn.exec("commit",nullptr)<0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_operated=true;
|
||||
return conn.getAffectedRows();
|
||||
}
|
||||
}
|
||||
|
||||
int Transaction::rollback()
|
||||
{
|
||||
if(_operated) return -2;
|
||||
|
||||
if(conn.exec("rollback",nullptr)<0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_operated=true;
|
||||
return conn.getAffectedRows();
|
||||
}
|
||||
}
|
||||
|
||||
bool Transaction::isReady() const
|
||||
{
|
||||
return _isReady;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
#include "MySQLWrapper.h"
|
||||
|
||||
class Transaction
|
||||
{
|
||||
public:
|
||||
/// Start Transaction on construct.
|
||||
Transaction(MySQLConn& MySQLConnection);
|
||||
|
||||
/// Rollback transaction on destruct. If commit() or rollback() is called, this function will not execute any sql.
|
||||
~Transaction();
|
||||
|
||||
/// NOTE: A Transaction can only call commit() or rollback() once, until it calls start commit again.
|
||||
/// Commit transaction. Equals to SQL: COMMIT
|
||||
/// ReturnValue: -1 SQL Error. -2 Operated >=0 Affected Rows
|
||||
int commit();
|
||||
/// Rollback transaction. Equals to SQL: ROLLBACK
|
||||
/// ReturnValue: -1 SQL Error. -2 Operated >=0 Affected Rows
|
||||
int rollback();
|
||||
|
||||
/// Check if a Transaction object is ready. If it is ready, then a transaction has started.
|
||||
bool isReady() const;
|
||||
private:
|
||||
MySQLConn& conn;
|
||||
bool _isReady;
|
||||
bool _operated;
|
||||
};
|
|
@ -1 +1 @@
|
|||
Subproject commit 5de3e0c2954ad4051e5b794453015457b7f0b459
|
||||
Subproject commit 621ef36565bd0d262437e879a833ae3a5c59cd0d
|
83
bs_util.cpp
Normal file
83
bs_util.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
#include "bs_util.h"
|
||||
using namespace std;
|
||||
|
||||
void jsonfail_func(json& j,int errcode,const std::string& detail)
|
||||
{
|
||||
j["success"]=0;
|
||||
j["errcode"]=errcode;
|
||||
j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail;
|
||||
}
|
||||
|
||||
int getPermissionLevel(const std::string& Username,Response& res)
|
||||
{
|
||||
DBInfo db;
|
||||
MySQLConn conn;
|
||||
json j;
|
||||
bool failed=false;
|
||||
auto jsonfail=[&](int errcode,const std::string& detail="")
|
||||
{
|
||||
jsonfail_func(j,errcode,detail);
|
||||
failed=true;
|
||||
};
|
||||
|
||||
if( db.readConfig()<0 )
|
||||
{
|
||||
jsonfail(err_config);
|
||||
}
|
||||
else if(db.connectProxy(conn)<0)
|
||||
{
|
||||
jsonfail(err_connect);
|
||||
}
|
||||
else
|
||||
{
|
||||
int permission_level;
|
||||
int ret=conn.exec(make_str("select permission_level from bs_user where username='",
|
||||
Username,
|
||||
"'"),
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
permission_level=ParseInt(val[0]);
|
||||
});
|
||||
});
|
||||
if(ret==1)
|
||||
{
|
||||
jsonfail(err_data,"User Not Found");
|
||||
}
|
||||
else if(ret<0)
|
||||
{
|
||||
jsonfail(err_sql,"Step 1");
|
||||
}
|
||||
else
|
||||
{
|
||||
return permission_level;
|
||||
}
|
||||
}
|
||||
|
||||
if(failed)
|
||||
{
|
||||
res.content=j.dump();
|
||||
exit(0);
|
||||
|
||||
/// Never reaches here.
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::function<void(MySQLResult&)> SQLParseInt(int& nval)
|
||||
{
|
||||
return
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
nval=ParseInt(val[0]);
|
||||
});
|
||||
};
|
||||
}
|
22
bs_util.h
Normal file
22
bs_util.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
#include "Request.h"
|
||||
#include "Response.h"
|
||||
#include "Session.h"
|
||||
#include "json.hpp"
|
||||
#include "jsonfail.h"
|
||||
#include "Util.h"
|
||||
using json=nlohmann::json;
|
||||
|
||||
/// General Json Failure Function.
|
||||
void jsonfail_func(json& j,int errcode,const std::string& detail="");
|
||||
|
||||
/// Get Permission of User
|
||||
/// If failed to get level, error message will be written to Response in JSON style. Then exit(0) is called.
|
||||
/// If success, returns permission_level.
|
||||
/// Returns -1 if control flow reaches unknown branch.
|
||||
int getPermissionLevel(const std::string& Username,Response& res);
|
||||
|
||||
std::function<void(MySQLResult&)> SQLParseInt(int& nval);
|
||||
|
||||
#define postval(NAME) if(req.post[#NAME].empty()){jsonfail(err_missing_parameter);break;}string NAME=req.post[#NAME]
|
||||
#define startdb() DBInfo db;if(db.readConfig()<0){jsonfail(err_config);break;} MySQLConn conn;if(db.connectProxy(conn)<0){jsonfail(err_connect);break;}
|
|
@ -2,6 +2,7 @@
|
|||
#include "Util.h"
|
||||
#include "jsonfail.h"
|
||||
#include "json.hpp"
|
||||
#include "MySQLTransaction.h"
|
||||
using namespace std;
|
||||
using json = nlohmann::json;
|
||||
|
|
@ -1,11 +1,7 @@
|
|||
#include "Session.h"
|
||||
#include "Util.h"
|
||||
#include "json.hpp"
|
||||
#include "jsonfail.h"
|
||||
#include "bs_util.h"
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
using namespace std;
|
||||
using json=nlohmann::json;
|
||||
|
||||
#define postval(NAME) string NAME=req.post[#NAME]
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -14,11 +10,9 @@ int main()
|
|||
Response res;
|
||||
json j;
|
||||
|
||||
auto jsonfail=[&](int errcode,const string& detail="")
|
||||
auto jsonfail=[&](int errcode,const std::string& dtl="")
|
||||
{
|
||||
j["success"]=0;
|
||||
j["errcode"]=errcode;
|
||||
j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail;
|
||||
jsonfail_func(j,errcode,dtl);
|
||||
};
|
||||
|
||||
do
|
||||
|
@ -29,7 +23,7 @@ int main()
|
|||
break;
|
||||
}
|
||||
|
||||
if(!se.isNew()||!se.getUser().empty())
|
||||
if(!se.getUser().empty())
|
||||
{
|
||||
/// Logged in...
|
||||
j["success"]=2;
|
||||
|
@ -42,53 +36,26 @@ int main()
|
|||
jsonfail(err_method_not_supported);
|
||||
break;
|
||||
}
|
||||
if(req.post["username"].empty() ||
|
||||
req.post["password"].empty() )
|
||||
{
|
||||
jsonfail(err_missing_parameter);
|
||||
break;
|
||||
}
|
||||
|
||||
postval(username);
|
||||
postval(password);
|
||||
|
||||
DBInfo db;
|
||||
MySQLConn conn;
|
||||
if(db.readConfig()<0)
|
||||
{
|
||||
jsonfail(err_config);
|
||||
break;
|
||||
}
|
||||
startdb();
|
||||
|
||||
if(db.connectProxy(conn)<0)
|
||||
{
|
||||
jsonfail(err_connect);
|
||||
break;
|
||||
}
|
||||
|
||||
/// Check if password is correct
|
||||
bool authpass=false;
|
||||
int count_val;
|
||||
if(conn.exec(make_str("select count(username) from bs_user where username='",
|
||||
username,
|
||||
"' and password='",
|
||||
password,
|
||||
"'"),
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
if(string(val[0])=="1")
|
||||
{
|
||||
authpass=true;
|
||||
}
|
||||
});
|
||||
})<0)
|
||||
SQLParseInt(count_val)
|
||||
)<0)
|
||||
{
|
||||
jsonfail(err_sql,"Step 1");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!authpass)
|
||||
if(count_val!=1)
|
||||
{
|
||||
jsonfail(err_data,"Auth Failed");
|
||||
break;
|
||||
|
@ -99,13 +66,7 @@ int main()
|
|||
if(conn.exec(make_str("select account_status from bs_user where username='",
|
||||
username,
|
||||
"'"),
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
account_status=ParseInt(val[0]);
|
||||
});
|
||||
})<0)
|
||||
SQLParseInt(account_status))<0)
|
||||
{
|
||||
jsonfail(err_sql,"Step 2");
|
||||
break;
|
||||
|
@ -123,12 +84,6 @@ int main()
|
|||
jsonfail(err_data,"Your account is banned.");
|
||||
break;
|
||||
}
|
||||
else if(account_status==2)
|
||||
{
|
||||
/// Need verify, redirect to account enable.
|
||||
jsonfail(err_data,"Need verify first.");
|
||||
j["next_url"]="/booksys/enable.html";
|
||||
}
|
||||
|
||||
/// Try to mark session logged in
|
||||
int ret;
|
||||
|
@ -138,12 +93,24 @@ int main()
|
|||
break;
|
||||
}
|
||||
|
||||
if(account_status==2)
|
||||
{
|
||||
/// Need verify, redirect to account enable.
|
||||
j["success"]=3;
|
||||
j["next_url"]="/booksys/enable.html";
|
||||
}
|
||||
else
|
||||
{
|
||||
j["success"]=1;
|
||||
j["next_url"]="/booksys/dashboard.html";
|
||||
}
|
||||
}
|
||||
while(0);
|
||||
|
||||
se.writeToResponse(res);
|
||||
res.content.append(j.dump());
|
||||
res.show();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
BIN
web/booksys/img/booksys_icon.png
Normal file
BIN
web/booksys/img/booksys_icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
BIN
web/booksys/img/default_user.png
Normal file
BIN
web/booksys/img/default_user.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
BIN
web/booksys/img/notice_icon.png
Normal file
BIN
web/booksys/img/notice_icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 830 B |
|
@ -1,14 +0,0 @@
|
|||
<!DOCTYPE>
|
||||
<html>
|
||||
<head>
|
||||
<title>安装失败</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>出现了一些问题导致安装无法进行:</p>
|
||||
<p>错误代码: {ErrCode}</p>
|
||||
<p>错误信息: {ErrMsg}</p>
|
||||
<p><a href="install.html">返回</a></p>
|
||||
</body>
|
||||
</html>
|
|
@ -3,11 +3,24 @@
|
|||
<head>
|
||||
<title>安装成功</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link rel="stylesheet" href="jquery-ui.css">
|
||||
<link rel="stylesheet" href="jquery-ui.structure.css">
|
||||
<link rel="stylesheet" href="jquery-ui.theme.css">
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
<script type="text/javascript" src="jquery-ui.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>你已经安装好了BookSystem.</p>
|
||||
<p>现在开始使用吧!</p>
|
||||
<p><a href="index.html">前往主页</a></p>
|
||||
<button id="submit">前往主页</button>
|
||||
|
||||
<script>
|
||||
$("button").button();
|
||||
|
||||
$("#submit").click(function(){
|
||||
location.href="login.html";
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -57,14 +57,11 @@
|
|||
obj.html('<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right:.3em;"></span>'+obj.html()+'</p>');
|
||||
}
|
||||
|
||||
$(".error").hide();
|
||||
$(".error").text("No Error");
|
||||
$(".error").fadeTo(0,0).text("No Error");
|
||||
createError($(".error"));
|
||||
$(".error").fadeTo(0,0);
|
||||
$(".error").show();
|
||||
|
||||
$("#submit").click(function(){
|
||||
$("#submit").hide();
|
||||
$("#submit").hide("");
|
||||
var bar=$("#progressbar").progressbar({
|
||||
value:false
|
||||
});
|
||||
|
@ -88,17 +85,20 @@
|
|||
console.log("Install Failure");
|
||||
$(".error").text("Install Failure.");
|
||||
createError($(".error"));
|
||||
$(".error").fadeTo(0,1).show().delay(1500).fadeTo(3000,0);
|
||||
$(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){
|
||||
$("#submit").show("");
|
||||
});
|
||||
}
|
||||
},"json")
|
||||
.fail(function(err){
|
||||
console.log("Failed to post");
|
||||
$(".error").text("Failed to Install");
|
||||
createError($(".error"));
|
||||
$(".error").fadeTo(0,1).show().delay(1500).fadeTo(3000,0);
|
||||
$(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){
|
||||
$("#submit").show("");
|
||||
});
|
||||
})
|
||||
.always(function(){
|
||||
$("#submit").show();
|
||||
bar.hide();
|
||||
});
|
||||
});
|
||||
|
|
105
web/booksys/login.html
Normal file
105
web/booksys/login.html
Normal file
|
@ -0,0 +1,105 @@
|
|||
<!DOCTYPE>
|
||||
<html>
|
||||
<head>
|
||||
<title>BookSystem 登录</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link rel="stylesheet" href="jquery-ui.css">
|
||||
<link rel="stylesheet" href="jquery-ui.structure.css">
|
||||
<link rel="stylesheet" href="jquery-ui.theme.css">
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
<script type="text/javascript" src="jquery-ui.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="login">
|
||||
<div class="error"></div>
|
||||
<p>BookSystem | Sign In </p>
|
||||
<div id="js_login_info_panel">
|
||||
<p id="js_login_info"></p>
|
||||
</div>
|
||||
<div id="login_panel" class="login_panel">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" name="username" />
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password" />
|
||||
<button id="submit">Sign In</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$("button").button();
|
||||
|
||||
/// Check Login
|
||||
$.post("/cgi-bin/booksys/checkLogin",{},function(data){
|
||||
console.log("CheckLogin Ajax Success");
|
||||
if(data.success==1)
|
||||
{
|
||||
console.log("logged in. redirecting...");
|
||||
location.href=data.next_url;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log("Check Failed, need login");
|
||||
}
|
||||
}).fail(function(err){
|
||||
console.log("Failed to check login. err:",err);
|
||||
});
|
||||
|
||||
//functions start
|
||||
function createHighlight(obj){
|
||||
obj.addClass('ui-state-highlight ui-corner-all');
|
||||
obj.html('<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right:.3em;"></span>'+obj.html()+'</p>');
|
||||
}
|
||||
|
||||
function createError(obj){
|
||||
obj.addClass('ui-state-error ui-corner-all');
|
||||
obj.html('<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right:.3em;"></span>'+obj.html()+'</p>');
|
||||
}
|
||||
|
||||
$(".error").fadeTo(0,0).text("No Error");
|
||||
createError($(".error"));
|
||||
|
||||
$("#submit").click(function(){
|
||||
$("#submit").hide("");
|
||||
$(".error").fadeTo(0,0).text("Submitting...");
|
||||
createHighlight($(".error"));
|
||||
$(".error").fadeTo(0,1);
|
||||
|
||||
console.log("Clicked");
|
||||
console.log("Begin Post");
|
||||
$.post("/cgi-bin/booksys/doLogin",
|
||||
{
|
||||
username:$("#username").val(),
|
||||
password:$("#password").val()
|
||||
},function(data){
|
||||
console.log("Post Done!");
|
||||
console.log(data);
|
||||
if(data.success==1||data.success==2) {
|
||||
// Success
|
||||
location.href=data.next_url;
|
||||
} else {
|
||||
// Fail
|
||||
console.log("Login Failure.");
|
||||
$(".error").text("Login Failure.");
|
||||
createError($(".error"));
|
||||
$(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){
|
||||
$("#submit").show("");
|
||||
});
|
||||
}
|
||||
},"json")
|
||||
.fail(function(err){
|
||||
console.log("Failed to post");
|
||||
$(".error").text("Failed to Login");
|
||||
createError($(".error"));
|
||||
$(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){
|
||||
$("#submit").show("");
|
||||
});
|
||||
})
|
||||
.always(function(){
|
||||
console.log("Post Finished");
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
0
web/booksys/logout.html
Normal file
0
web/booksys/logout.html
Normal file
27
web/booksys/mainpage.html
Normal file
27
web/booksys/mainpage.html
Normal file
|
@ -0,0 +1,27 @@
|
|||
<!DOCTYPE>
|
||||
<html>
|
||||
<head>
|
||||
<title>BookSystem 主页</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link rel="stylesheet" href="jquery-ui.css">
|
||||
<link rel="stylesheet" href="jquery-ui.structure.css">
|
||||
<link rel="stylesheet" href="jquery-ui.theme.css">
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
<script type="text/javascript" src="jquery-ui.js"></script>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav>
|
||||
<a href=mainpage.html><img src="img/booksys_icon.png" alt="booksys_icon"/></a>
|
||||
<input type="text" placeholder="Search BookSys" id="search_box"/>
|
||||
<a href="explore.html"><b>Explore</b></a>
|
||||
<a href="notice.html"><img src="img/notice_icon.png" alt="Notice"/></a>
|
||||
<a href="profile.html"><img src="img/default_user.png" alt="default_user"/></a>
|
||||
</nav>
|
||||
<div>
|
||||
BookSystem
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user