Menu

[r9]: / framework / trunk / main_thread.cpp  Maximize  Restore  History

Download this file

248 lines (222 with data), 5.2 kB

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "main_thread.h"
#include "global_config.h"
void Main_Thread::init()
{
db.open();
}
void Main_Thread::load_cookies()
{
const vector<HTTPCookie> &cookies = env->getCookieList();
unsigned int i;
username.clear();
visitor.clear();
email.clear();
url.clear();
password.clear();
for(i=0;i<cookies.size();i++) {
if(cookies[i].getName()=="username") {
username=cookies[i].getValue();
}
else if(cookies[i].getName()=="password") {
password=cookies[i].getValue();
}
else if(cookies[i].getName()=="visitor") {
visitor=cookies[i].getValue();
}
else if(cookies[i].getName()=="email") {
email=cookies[i].getValue();
}
else if(cookies[i].getName()=="url") {
url=cookies[i].getValue();
}
}
}
void Main_Thread::check_athentication()
{
MySQL_DB_Res res=db.query(
escape( "SELECT password,id from cp_users "
"WHERE username='%1%' LIMIT 1")<<username);
MySQL_DB_Row row;
if((row=res.next())!=NULL){
if(password==row[0]) {
authenticated=true;
user_id=atoi(row[1]);
}
}
else {
username.clear();
authenticated=false;
}
}
void Main_Thread::load_inputs()
{
const vector<FormEntry> &elements=cgi->getElements();
unsigned i;
page=0;
message.clear();
for(i=0;i<elements.size();i++) {
string const &name=elements[i].getName();
if(name=="id") {
string const &s=elements[i].getValue();
if(s=="login") {
page=1;
}
else if(s=="logout") {
page=2;
}
else if(s=="newpost") {
page=3;
}
else if(s=="post") {
page=4;
}
else if(s=="dologin") {
page=5;
}
else {
page=0;
}
}
else if(name=="message"){
message=elements[i].getValue();
}
else if(name=="username"){
new_username=elements[i].getValue();
}
else if(name=="password"){
new_password=elements[i].getValue();
}
}
}
void Main_Thread::show_login()
{
out.puts(
"<html><body>"
"<form name=\"input'\" action="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"test.fcgi\" method=\"post\">\n"
" Username: <input type=\"text\" name=\"username\">\n<br>"
" Password: <input type=\"password\" name=\"password\">\n"
"<input type=\"hidden\" name=\"id\" value=\"dologin\">\n"
" <input type=\"submit\" value=\"Submit\"></form>\n"
"</html></body>");
}
void Main_Thread::do_login()
{
username=new_username;
password=new_password;
check_athentication();
if(authenticated) {
set_header(new HTTPRedirectHeader("test.fcgi"));
HTTPCookie cookie("username",username);
cookie.setMaxAge(7*24*3600);
response_header->setCookie(cookie);
cookie.setName("password");
cookie.setValue(password);
response_header->setCookie(cookie);
}
else {
set_header(new HTTPRedirectHeader("test.fcgi?id=login"));
}
};
void Main_Thread::show_logout()
{
set_header(new HTTPRedirectHeader("test.fcgi"));
response_header->setCookie(HTTPCookie("username",""));
response_header->setCookie(HTTPCookie("password",""));
}
void Main_Thread::printhtml(char const *text)
{
int c;
while((c=*text++)!=0) {
switch(c) {
case '\n': out.puts("<br>\n"); break;
case '<': out.puts("&lt;"); break;
case '>': out.puts("&gt;"); break;
case '&': out.puts("&amp;"); break;
default:
out.putchar(c);
}
}
}
void Main_Thread::show_main_page()
{
if(authenticated) {
out.printf("<h1>Wellcome %s to forum</h1>\n",username.c_str());
}
else {
out.puts("<h1>Wellcome to the forum</h1>");
}
out.puts("<a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"test.fcgi?id=newpost\">New Post</a><br>");
out.puts("<a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"test.fcgi?id=logout\">Logout</a><br>");
MySQL_DB_Res res = db.query(
"SELECT cp_messages.id,message,username "
"FROM cp_messages,cp_users "
"WHERE cp_messages.user_id=cp_users.id "
"ORDER BY cp_messages.id DESC LIMIT 10");
MySQL_DB_Row row;
out.puts("<dl>\n");
while((row=res.next())!=NULL) {
out.printf("<dt>Message %s, by %s</dt>\n<dd>\n",row[0],row[2]);
printhtml(row[1]);
out.puts("</dd>\n");
}
out.puts("</dl>\n");
}
void Main_Thread::show_post_form()
{
if(authenticated) {
out.puts(
"<html><body>"
"<form name=\"input'\" action="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"test.fcgi\" method=\"post\">\n"
"<TEXTAREA NAME=\"message\" COLS=40 ROWS=6></TEXTAREA><br>\n"
"<input type=\"hidden\" name=\"id\" value=\"post\">\n"
" <input type=\"submit\" value=\"Submit\"></form>\n"
"</html></body>");
}
else {
set_header(new HTTPRedirectHeader("test.fcgi?id=login"));
}
}
void Main_Thread::get_post_message()
{
if(!authenticated){
set_header(new HTTPRedirectHeader("test.fcgi?id=login"));
return;
}
db.exec(escape("INSERT INTO cp_messages (message,user_id) values('%1%',%2%)")
<<message<<user_id);
set_header(new HTTPRedirectHeader("test.fcgi"));
}
void Main_Thread::show_page()
{
switch(page) {
case 1: show_login(); break;
case 4: if(authenticated)
get_post_message();
else
show_login();
break;
case 3: if(authenticated)
show_post_form();
else
show_login();
break;
case 2: show_logout(); break;
case 5: do_login(); break;
case 0:
default:
show_main_page();
}
}
void Main_Thread::main()
{
try {
set_header(new HTTPHTMLHeader);
load_cookies();
check_athentication();
load_inputs();
show_page();
}
catch (MySQL_DB_Err &mysql_err) {
throw HTTP_Error(mysql_err.get());
}
}
MongoDB Logo MongoDB