Menu

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

Download this file

267 lines (216 with data), 5.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "main_thread.h"
#include "global_config.h"
#include "templates/look.h"
#include "templates.h"
#include "data.h"
#include "boost/format.hpp"
#include "text_tool.h"
void Main_Thread::init()
{
url.init(this);
url.add("^/?$", BIND(&Main_Thread::show_main_page,this,"end"));
url.add("^/from/(\\d+)$",BIND(&Main_Thread::show_main_page,this,$1));
url.add("^/login$", BIND(&Main_Thread::show_login,this));
url.add("^/logout$", BIND(&Main_Thread::show_logout,this));
url.add("^/newpost$", BIND(&Main_Thread::show_post_form,this));
url.add("^/post$", BIND(&Main_Thread::get_post_message,this));
url.add("^/dologin$", BIND(&Main_Thread::do_login,this));
url.add("^/edit/(\\d+)$",BIND(&Main_Thread::edit_message,this,$1));
}
void Main_Thread::edit_message(string msg)
{
// TODO
}
void Main_Thread::load_cookies()
{
const vector<HTTPCookie> &cookies = env->getCookieList();
unsigned int i;
username.clear();
visitor.clear();
email.clear();
vurl.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") {
vurl=cookies[i].getValue();
}
}
}
void Main_Thread::check_athentication_by_name(string name,string password)
{
Users::username_c cur(users->username);
if(cur==name){
user_t const &user=cur;
if(user.password==password.c_str()) {
authenticated=true;
user_id=user.id;
}
username=name;
}
else {
username.clear();
authenticated=false;
}
password.clear();
}
void Main_Thread::check_athentication()
{
load_cookies();
check_athentication_by_name(username,password);
}
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=="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()
{
Content c(T_VAR_NUM);
c[TV_title]="Login";
c[TV_show_content]=TT_dologin;
Renderer r(templates,TT_master,c);
while(r.render(out)!=0);
}
void Main_Thread::do_login()
{
load_inputs();
check_athentication_by_name(new_username,new_password);
if(authenticated) {
set_header(new HTTPRedirectHeader("/site/"));
HTTPCookie cookie_u("username",username,"","",7*24*3600,"/",false);
response_header->setCookie(cookie_u);
HTTPCookie cookie_p("password",new_password,"","",7*24*3600,"/",false);
response_header->setCookie(cookie_p);
}
else {
username="";
password="";
set_header(new HTTPRedirectHeader("/site/login"));
}
};
void Main_Thread::show_logout()
{
set_header(new HTTPRedirectHeader("/site/"));
HTTPCookie cookie("username","","","",0,"/",false);
response_header->setCookie(cookie);
cookie.setName("password");
cookie.setValue("");
response_header->setCookie(cookie);
}
void Main_Thread::show_main_page(string from)
{
check_athentication();
Content c(T_VAR_NUM);
Renderer t(templates,TT_master,c);
c[TV_title]="Main page";
c[TV_show_content]=TT_main;
if(authenticated) {
c[TV_username]=username;
}
Messages::id_c cur(all_messages->id);
if(from=="end") {
cur.end();
}
else {
int from_id=atoi(from.c_str());
cur<from_id;
}
int id;
string content;
int counter=0;
while((id=t.render(out))!=0) {
if(id==TV_get_message){
if(cur && counter<10){
message_t const &message=cur;
c[TV_new_message]=1;
char buf[20];
snprintf(buf,20,"%d",message.id);
c[TV_message_id]=buf;
string intext;
texts->get(message.text_id,intext);
Text_Tool conv;
conv.markdown2html(intext,content);
c[TV_message_body]=content.c_str();
Users::id_c ucur(users->id);
ucur==message.user_id;
user_t const &user=ucur;
c[TV_author]=user.username.c_str();
cur.next();
counter++;
}
else {
c[TV_new_message]=0;
}
}
}
}
void Main_Thread::show_post_form()
{
check_athentication();
if(authenticated) {
Content c(T_VAR_NUM);
c[TV_title]="New message";
c[TV_show_content]=TT_post;
Renderer t(templates,TT_master,c);
while(t.render(out));
}
else {
set_header(new HTTPRedirectHeader("/site/login"));
}
}
void Main_Thread::get_post_message()
{
check_athentication();
load_inputs();
if(!authenticated){
set_header(new HTTPRedirectHeader("/site/login"));
return;
}
message_t msg;
msg.text_id=texts->add(message);
msg.user_id=user_id;
all_messages->id.add(msg);
set_header(new HTTPRedirectHeader("/site/"));
}
void Main_Thread::show_page()
{
url.parse();
}
void Main_Thread::main()
{
try {
show_page();
}
catch (DbException &err) {
throw HTTP_Error(err.what());
}
catch(char const *s) {
throw HTTP_Error(s);
}
}
MongoDB Logo MongoDB