-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
144 lines (126 loc) · 3.66 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <lib/init.hpp>
#include <lib/fcgi/params.hpp>
#include <lib/fcgi/base.hpp>
void Throw (string a)
{
cout << a << "\n";
}
Process proc;
int main ()
{
MySQL::client_t Infocourse ("Infocourse");
std::vector < Resources::resource_key > all;
//std::cout << "News" << std::endl;
for (auto& x : Infocourse.result ("SELECT `Num` FROM `News`", "", ""))
{
all.push_back (Resources::resource_key ("0", x [0]));
}
//std::cout << "News" << std::endl;
for (auto& x : Infocourse.result ("SELECT `UniqueID`,`ExamLink` FROM `Videos`", "", ""))
{
all.push_back (Resources::resource_key ("1", x [0]));
}
Resources::States.init (all);
Resources::States.update ();
int count = 0;
long pid = getpid();
streambuf * cin_streambuf = cin.rdbuf ();
streambuf * cout_streambuf = cout.rdbuf ();
streambuf * cerr_streambuf = cerr.rdbuf ();
FCGX_Request request;
FCGX_Init ();
FCGX_InitRequest (&request, 0, 0);
while (FCGX_Accept_r (&request) == 0)
{
fcgi_streambuf cin_fcgi_streambuf (request.in);
fcgi_streambuf cout_fcgi_streambuf (request.out);
fcgi_streambuf cerr_fcgi_streambuf (request.err);
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = &cin_fcgi_streambuf;
cout = &cout_fcgi_streambuf;
cerr = &cerr_fcgi_streambuf;
#else
cin.rdbuf (&cin_fcgi_streambuf);
cout.rdbuf (&cout_fcgi_streambuf);
cerr.rdbuf (&cerr_fcgi_streambuf);
#endif
char * content;
unsigned long clen = gstdin(&request, &content);
count ++;
proc.set_args (request.envp);
cout << "Content-type: text/html\r\n";
if (proc.params ["QUERY_STRING"] == "")
Throw (HTML::invalid_page);
else
{
cout << HTML::viewport << "\n";
if (proc.query_string ["type"] == "update")
{
try
{
Resources::States.update ();
}
catch (exception& e)
{
Throw (e.what ());
}
}
else if (proc.query_string ["type"] == "get")
{
try
{
person test (proc.query_string ["person"]);
auto x = Resources::States.next (Resources::States.transform (test.get_visitations ()));
size_t n = std::min (atoi (proc.query_string ["n"].c_str ()), (int)x.size ());
std::cout << "[";
for (int i = 0 ; i < n - 1 ; i ++)
std::cout << "[" << x [i].key.id << "," << x [i].key.type << "," << x [i].goody << "],";
std::cout << "[" << x [n - 1].key.id << "," << x [n - 1].key.type << "," << x [n - 1].goody << "]]";
}
catch (exception& e)
{
Throw (e.what ());
}
}
else if (proc.query_string ["type"] == "receive")
{
try
{
auto x = Infocourse.result ("SELECT `ID` FROM `Users` WHERE `Email`='", proc.query_string ["person"], "'");
if (x.size () == 0)
throw "User not found";
std::string user_id = x [0][0];
x = Infocourse.result ("SELECT `UniqueID` FROM `Videos` WHERE `ExamLink`='", proc.query_string ["link"], "'");
if (x.size () == 0)
throw "User not found";
std::string resource_id = x [0][0];
MySQL::client.result ("INSERT INTO `Visits` (`IdUser`,`Type`,`IdResource`) VALUE ('" + user_id + "', '2', '" + resource_id, "", "')");
}
catch (exception& e)
{
Throw (e.what ());
}
}
}
if (content) delete []content;
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = cin_streambuf;
cout = cout_streambuf;
cerr = cerr_streambuf;
#else
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
#endif
}
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = cin_streambuf;
cout = cout_streambuf;
cerr = cerr_streambuf;
#else
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
#endif
}