This repository has been archived on 2025-12-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
adashima.fun/API/collection.cpp
T

99 lines
4.2 KiB
C++

#include <mysql.hpp>
#include <database.hpp>
#include <html5.hpp>
#include <string>
#include <cgicc/Cgicc.h>
#include <iostream>
bool login=false;
bool member=false;
int main(){
mysqlpp::Connection database;
userdata user;
cgicc::Cgicc cgi;
std::string s_id = get_cookie(cgi,"id");
std::string s_loginkey = get_cookie(cgi,"loginkey");
if(s_id==""||s_id.find_first_not_of("0123456789")!=std::string::npos||std::stoul(s_id)>UINT64_MAX){
s_id="";
}
if(s_loginkey==""||s_loginkey.find_first_not_of("0123456789")!=std::string::npos||std::stoul(s_loginkey)>UINT64_MAX){
s_loginkey="";
}
if(!connect_mysql(&database)){
std::cout << "Content-type:text/html\r\n\r\n"
<< "<html>\n"
<< "<head>\n"
<< "<meta charset=\"utf-8\">\n"
<< "<title>出现错误!</title>\n"
<< "</head>\n"
<< "<body>\n"
<< "数据库连接失败,请检查服务器状态!\n"
<< "</body>\n"
<< "</html>\n";
return 0;
}
if(s_id!=""&&query_userdata(database,&user,"id = "+s_id)){
if(s_loginkey!=""&&user.loginkey==std::stoul(s_loginkey)){
login=true;
}
if(user.status=='m'){
member=true;
}
if(!login&&member){
member=false;
}
}
if(login&&member){
std::string action = cgi.getElement("action")->getValue();
if(action=="create"){
std::string title = cgi.getElement("title")->getValue();
if(title.size()>255||title.find_first_of("'\\")!=std::string::npos){
std::cout << "Status: 502 Bad request\r\n" << std::endl;
return 0;
}
change_mysql(database,"INSERT INTO collectiondata (title,uploader) VALUE ('"+title+"',"+s_id+")");
change_mysql(database,"CREATE TABLE collection_"+std::to_string(latest_cid(database))+" ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, vid BIGINT UNSIGNED NOT NULL ) CHARSET=utf8mb4 ENGINE=InnoDB;");
std::cout << "Status: 200 OK\r\n" << std::endl;
return 0;
}else if(action=="add_video"){
std::string cid = cgi.getElement("collection_id")->getValue();
std::string vid = cgi.getElement("video_id")->getValue();
if(cid.find_first_not_of("0123456789")!=std::string::npos||vid.find_first_not_of("0123456789")!=std::string::npos){
std::cout << "Status: 502 Bad request\r\n" << std::endl;
return 0;
}
change_mysql(database,"UPDATE collectiondata SET videocount = videocount + 1 WHERE id = "+cid);
change_mysql(database,"UPDATE videodata SET collection = "+cid+" WHERE id = "+vid);
change_mysql(database,"INSERT INTO collection_"+cid+" (vid) VALUE ("+vid+")");
std::cout << "Status: 200 OK\r\n" << std::endl;
return 0;
}else if(action=="update"){
std::string title = cgi.getElement("title")->getValue();
std::string cid = cgi.getElement("collection_id")->getValue();
if(title.size()>255||title.find_first_of("'\\")!=std::string::npos||cid.find_first_not_of("0123456789")!=std::string::npos){
std::cout << "Status: 502 Bad request\r\n" << std::endl;
return 0;
}
change_mysql(database,"UPDATE collectiondata SET title = '"+title+"' WHERE id = "+cid);
std::cout << "Status: 200 OK\r\n" << std::endl;
return 0;
}else if(action=="delete"){
std::string cid = cgi.getElement("collection_id")->getValue();
if(cid.find_first_not_of("0123456789")!=std::string::npos){
std::cout << "Status: 502 Bad request\r\n" << std::endl;
return 0;
}
change_mysql(database,"DROP TABLE collection_"+cid);
change_mysql(database,"DELETE FROM collectiondata WHERE id = "+cid);
std::cout << "Status: 200 OK\r\n" << std::endl;
return 0;
}else{
std::cout << "Status: 502 Bad request\r\n" << std::endl;
return 0;
}
}else{
std::cout << "Status: 302 Found\r\n"
<< "Location: /\r\n" << std::endl;
}
}