Subversion Repositories RepoView

Rev

Rev 39 | Rev 43 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: modern.c 42 2024-08-22 05:25:09Z nishi $ */
2
 
3
#include "rv_query.h"
4
 
5
#include "rv_util.h"
6
#include "rv_version.h"
3 nishi 7
#include "rv_auth.h"
1 nishi 8
#include "rv_db.h"
11 nishi 9
#include "rv_repo.h"
42 nishi 10
#include "rv_multipart.h"
1 nishi 11
 
12
#include "../../config.h"
13
 
13 nishi 14
#ifdef USE_ENSCRIPT
15
#include "rv_enscript.h"
16
#endif
17
 
39 nishi 18
#ifdef USE_AVATAR
19
#include "rv_avatar.h"
20
#endif
21
 
42 nishi 22
#ifdef USE_GRAPHICSMAGICK
23
#include "rv_magick.h"
24
#endif
25
 
7 nishi 26
#include <stdio.h>
1 nishi 27
#include <stdlib.h>
28
#include <string.h>
39 nishi 29
#include <unistd.h>
1 nishi 30
 
42 nishi 31
extern char* nocache;
1 nishi 32
extern char* buffer;
33
void add_data(char** data, const char* txt);
34
void render_stuff();
35
 
36
char* title = NULL;
37
char* desc = NULL;
38
char* page = NULL;
10 nishi 39
char* nav = NULL;
11 nishi 40
char* grepouser;
6 nishi 41
extern char* user;
1 nishi 42
 
11 nishi 43
char* url_escape(const char* input) {
44
	const char hex[] = "0123456789ABCDEF";
45
	char* r = malloc(1);
46
	r[0] = 0;
47
	char cbuf[2];
48
	cbuf[1] = 0;
49
	int i;
50
	for(i = 0; input[i] != 0; i++) {
51
		if(input[i] == 0x20 || input[i] == 0x22 || input[i] == 0x25 || input[i] == 0x2d || input[i] == 0x2e || input[i] == 0x3c || input[i] == 0x3e || input[i] == 0x5c || input[i] == 0x5e || input[i] == 0x5f || input[i] == 0x60 || input[i] == 0x7b || input[i] == 0x7c || input[i] == 0x7d || input[i] == 0x7e || input[i] == 0x21 || input[i] == 0x23 || input[i] == 0x24 || input[i] == 0x26 || input[i] == 0x27 || input[i] == 0x28 || input[i] == 0x29 || input[i] == 0x2a || input[i] == 0x2b || input[i] == 0x2c || input[i] == 0x2f || input[i] == 0x3a || input[i] == 0x3b || input[i] == 0x3d || input[i] == 0x3f || input[i] == 0x40 || input[i] == 0x5b || input[i] == 0x5d) {
52
			add_data(&r, "%");
53
			cbuf[0] = hex[(input[i] >> 4) & 0xf];
54
			add_data(&r, cbuf);
55
			cbuf[0] = hex[input[i] & 0xf];
56
			add_data(&r, cbuf);
57
		} else {
58
			cbuf[0] = input[i];
59
			add_data(&r, cbuf);
60
		}
61
	}
62
	return r;
63
}
64
 
65
char* html_escape(const char* input) {
66
	char* r = malloc(1);
67
	r[0] = 0;
68
	char cbuf[2];
69
	cbuf[1] = 0;
70
	int i;
71
	for(i = 0; input[i] != 0; i++) {
72
		if(input[i] == '<') {
73
			add_data(&r, "&lt;");
74
		} else if(input[i] == '>') {
75
			add_data(&r, "&gt;");
76
		} else {
77
			cbuf[0] = input[i];
78
			add_data(&r, cbuf);
79
		}
80
	}
81
	return r;
82
}
83
 
84
char* html_escape_nl_to_br(const char* input) {
85
	char* r = malloc(1);
86
	r[0] = 0;
87
	char cbuf[2];
88
	cbuf[1] = 0;
89
	int i;
90
	for(i = 0; input[i] != 0; i++) {
91
		if(input[i] == '<') {
92
			add_data(&r, "&lt;");
93
		} else if(input[i] == '>') {
94
			add_data(&r, "&gt;");
95
		} else if(input[i] == '\n') {
96
			add_data(&r, "<br>");
97
		} else {
98
			cbuf[0] = input[i];
99
			add_data(&r, cbuf);
100
		}
101
	}
102
	return r;
103
}
104
 
105
void list_repo(const char* name, const char* rev) {
106
	char* showname = html_escape(name);
107
	char* urluser = url_escape(user);
108
	char* urlrepo = url_escape(name);
109
	add_data(&page, "<tr>");
110
	add_data(&page, "<td><a href=\"");
111
	add_data(&page, INSTANCE_ROOT);
112
	add_data(&page, "/?page=repo&reponame=");
113
	add_data(&page, urlrepo);
114
	add_data(&page, "&username=");
115
	add_data(&page, urluser);
116
	add_data(&page, "\">");
117
	add_data(&page, showname);
118
	add_data(&page, "</a></td>");
119
	add_data(&page, "<td>");
120
	add_data(&page, rev);
121
	add_data(&page, "</td>");
122
	add_data(&page, "</tr>");
123
	free(showname);
124
	free(urluser);
125
	free(urlrepo);
126
}
127
 
128
int fcounter = 0;
129
void list_files(const char* pathname) {
130
	if(fcounter == 0) {
131
		add_data(&nav, "<li><a href=\"#filelist\">File List</a></li>\n");
132
		add_data(&page, "<h2 id=\"filelist\">File List</h2>\n");
133
		add_data(&page, "<tr style=\"background-color: #D2E1F6;\"><th>Name</th><th>Size</th></tr>\n");
12 nishi 134
		char* path = rv_get_query("path");
135
		if(path == NULL) path = "/";
136
		if(strcmp(path, "/") != 0) {
137
			char* query = rv_strdup("?page=repo&reponame=");
138
			char* esc;
139
			esc = url_escape(rv_get_query("reponame"));
140
			add_data(&query, esc);
141
			free(esc);
142
			add_data(&query, "&username=");
15 nishi 143
			esc = url_escape(rv_get_query("username"));
12 nishi 144
			add_data(&query, esc);
145
			free(esc);
146
			add_data(&query, "&path=");
147
 
148
			char* urlpath = rv_strdup(path);
149
			int i;
150
			int counter = 0;
151
			int rep = urlpath[strlen(urlpath) - 1] == '/' ? 2 : 1;
152
			for(i = strlen(urlpath) - 1; i >= 0; i--) {
153
				char oldc = urlpath[i];
154
				urlpath[i] = 0;
155
				if(oldc == '/') {
156
					counter++;
157
					if(counter == 2) {
158
						break;
159
					}
160
				}
161
			}
162
 
163
			if(strlen(urlpath) == 0) {
164
				free(urlpath);
165
				urlpath = rv_strdup("/");
166
			}
167
 
168
			esc = url_escape(urlpath);
169
			add_data(&query, esc);
170
			free(esc);
171
 
172
			add_data(&page, "<tr><td><a href=\"");
173
			add_data(&page, query);
174
			add_data(&page, "\">../</a></td><td>&lt;DIR&gt;</td></tr>\n");
175
			fcounter++;
176
			free(query);
177
		}
11 nishi 178
	}
179
	fcounter++;
180
	add_data(&page, "<tr style=\"background-color: #");
181
	if((fcounter % 2) == 0) {
182
		add_data(&page, "D2E1C0");
183
	} else {
184
		add_data(&page, "FFFFFF");
185
	}
186
	char* path = rv_get_query("path");
187
	if(path == NULL) path = "/";
188
	char* query = rv_strdup("?page=repo&reponame=");
189
	char* esc;
190
	esc = url_escape(rv_get_query("reponame"));
191
	add_data(&query, esc);
192
	free(esc);
193
	add_data(&query, "&username=");
15 nishi 194
	esc = url_escape(rv_get_query("username"));
11 nishi 195
	add_data(&query, esc);
196
	free(esc);
197
	add_data(&query, "&path=");
13 nishi 198
	char* urlpath = rv_strcat3(path, "/", pathname);
11 nishi 199
	esc = url_escape(urlpath);
200
	add_data(&query, esc);
201
	free(esc);
202
	char* sz = malloc(128);
203
	sprintf(sz, "%lld", rv_get_filesize(grepouser, urlpath));
204
	add_data(&page, "\"><td><a href=\"");
205
	add_data(&page, query);
206
	add_data(&page, "\">");
207
	add_data(&page, pathname);
208
	add_data(&page, "</a></td>\n");
209
	add_data(&page, "<td>\n");
210
	if(strcmp(sz, "-1") != 0) {
211
		add_data(&page, sz);
212
	} else {
213
		add_data(&page, "&lt;DIR&gt;");
214
	}
215
	add_data(&page, "</td>\n");
216
	free(sz);
217
	add_data(&page, "</tr>\n");
218
	free(query);
219
	free(urlpath);
220
}
221
 
39 nishi 222
void generate_avatar(void) {
223
	if(user != NULL) {
224
		char* tmp = rv_strcat3(AVATAR_ROOT, "/", user);
225
		char* path = rv_strcat(tmp, ".png");
226
		free(tmp);
227
		if(access(path, F_OK) != 0) {
228
			rv_avatar_generate(path, user);
229
		}
230
		free(path);
231
	}
232
}
233
 
3 nishi 234
void render_page(void) {
1 nishi 235
	rv_load_query('Q');
236
	char* query = rv_get_query("page");
237
	if(query == NULL) query = "welcome";
238
 
39 nishi 239
#ifdef USE_AVATAR
240
	generate_avatar();
241
#endif
242
 
3 nishi 243
	if(strcmp(query, "welcome") == 0) {
1 nishi 244
		title = rv_strdup("Welcome");
245
		desc = rv_strdup("Welcome to " INSTANCE_NAME ".");
246
		page = rv_strcat3("Welcome to " INSTANCE_NAME ".<br>This instance is running RepoView version ", rv_get_version(), ".");
13 nishi 247
#ifdef ALLOW_SIGNUP
248
	} else if(strcmp(query, "signup") == 0) {
249
		title = rv_strdup("Signup");
250
		desc = rv_strdup("You can create your account here.");
251
		page = rv_strdup("");
252
 
253
		add_data(&page, "<form action=\"");
254
		add_data(&page, INSTANCE_ROOT);
255
		add_data(&page, "/?page=sendsignup\" method=\"POST\">\n");
256
		add_data(&page, "	<table border=\"0\">\n");
257
		add_data(&page, "		<tr>\n");
258
		add_data(&page, "			<th>Username</th>\n");
259
		add_data(&page, "			<td>\n");
260
		add_data(&page, "				<input name=\"username\">\n");
261
		add_data(&page, "			</td>\n");
262
		add_data(&page, "		</tr>\n");
263
		add_data(&page, "		<tr>\n");
264
		add_data(&page, "			<th>Password</th>\n");
265
		add_data(&page, "			<td>\n");
266
		add_data(&page, "				<input name=\"password\" type=\"password\">\n");
267
		add_data(&page, "			</td>\n");
268
		add_data(&page, "		</tr>\n");
269
		add_data(&page, "	</table>\n");
270
		char cbuf[2];
271
		cbuf[0] = REPO_USER_DELIM;
272
		cbuf[1] = 0;
273
		add_data(&page, "Username cannot contain '<code>");
274
		add_data(&page, cbuf);
22 nishi 275
		add_data(&page, "</code>', '<code>#</code>', '<code>\\</code>', and '<code>/</code>'.<br>");
13 nishi 276
		add_data(&page, "	<input type=\"submit\" value=\"Signup\">\n");
277
		add_data(&page, "</form>\n");
278
	} else if(strcmp(query, "sendsignup") == 0) {
279
		title = rv_strdup("Signup Result");
280
		page = rv_strdup("");
281
 
282
		rv_load_query('P');
283
		if(user != NULL) {
284
			page = rv_strdup("It looks like you are already logged in.<br>Want to <a href=\"");
285
			add_data(&page, INSTANCE_ROOT);
286
			add_data(&page, "/?page=logout\">log out</a>?\n");
287
		} else if(rv_get_query("username") == NULL || rv_get_query("password") == NULL) {
288
			add_data(&page, "Invalid form.\n");
289
		} else {
290
			if(rv_has_user(rv_get_query("username"))) {
291
				add_data(&page, "User already exists.");
292
			} else {
293
				if(user != NULL) free(user);
294
				int i;
295
				bool reject = false;
296
				char* name = rv_get_query("username");
297
				for(i = 0; name[i] != 0; i++) {
42 nishi 298
					if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
13 nishi 299
						char cbuf[2];
300
						cbuf[0] = REPO_USER_DELIM;
301
						cbuf[1] = 0;
302
						add_data(&page, "Username cannot contain '<code>");
303
						add_data(&page, cbuf);
304
						add_data(&page, "</code>'.");
305
						reject = true;
306
						break;
307
					}
308
				}
309
				if(!reject) {
310
					rv_create_user(rv_get_query("username"), rv_get_query("password"));
311
					user = rv_strdup(rv_get_query("username"));
312
					add_data(&page, "Welcome.\n");
313
					rv_save_login(rv_get_query("username"));
314
				}
315
			}
316
		}
317
#endif
3 nishi 318
	} else if(strcmp(query, "login") == 0) {
1 nishi 319
		title = rv_strdup("Login");
320
		desc = rv_strdup("You can log in to your account here.");
321
		page = rv_strdup("");
322
 
323
		add_data(&page, "<form action=\"");
324
		add_data(&page, INSTANCE_ROOT);
325
		add_data(&page, "/?page=sendlogin\" method=\"POST\">\n");
326
		add_data(&page, "	<table border=\"0\">\n");
327
		add_data(&page, "		<tr>\n");
328
		add_data(&page, "			<th>Username</th>\n");
329
		add_data(&page, "			<td>\n");
330
		add_data(&page, "				<input name=\"username\">\n");
331
		add_data(&page, "			</td>\n");
332
		add_data(&page, "		</tr>\n");
333
		add_data(&page, "		<tr>\n");
334
		add_data(&page, "			<th>Password</th>\n");
335
		add_data(&page, "			<td>\n");
336
		add_data(&page, "				<input name=\"password\" type=\"password\">\n");
337
		add_data(&page, "			</td>\n");
338
		add_data(&page, "		</tr>\n");
339
		add_data(&page, "	</table>\n");
340
		add_data(&page, "	<input type=\"submit\" value=\"Login\">\n");
341
		add_data(&page, "</form>\n");
3 nishi 342
	} else if(strcmp(query, "sendlogin") == 0) {
1 nishi 343
		title = rv_strdup("Login Result");
344
		page = rv_strdup("");
345
 
346
		rv_load_query('P');
11 nishi 347
		if(user != NULL) {
348
			page = rv_strdup("It looks like you are already logged in.<br>Want to <a href=\"");
349
			add_data(&page, INSTANCE_ROOT);
13 nishi 350
			add_data(&page, "/?page=logout\">log out</a>?\n");
11 nishi 351
		} else if(rv_get_query("username") == NULL || rv_get_query("password") == NULL) {
5 nishi 352
			add_data(&page, "Invalid form.\n");
3 nishi 353
		} else {
354
			if(rv_has_user(rv_get_query("username"))) {
5 nishi 355
				if(rv_check_password(rv_get_query("username"), rv_get_query("password"))) {
6 nishi 356
					if(user != NULL) free(user);
357
					user = rv_strdup(rv_get_query("username"));
5 nishi 358
					add_data(&page, "Welcome back.\n");
359
					rv_save_login(rv_get_query("username"));
360
				} else {
361
					add_data(&page, "Invalid password.");
362
				}
3 nishi 363
			} else {
5 nishi 364
				add_data(&page, "User does not exist.");
1 nishi 365
			}
366
		}
7 nishi 367
	} else if(strcmp(query, "sendlogout") == 0) {
368
		title = rv_strdup("Logout Result");
369
		page = rv_strdup("");
370
		if(user == NULL) {
371
			add_data(&page, "You were not logged in.\n");
372
		} else {
373
			rv_logout();
374
			add_data(&page, "Goodbye.\n");
375
			free(user);
376
			user = NULL;
377
		}
27 nishi 378
#ifdef USE_MYPAGE
6 nishi 379
	} else if(strcmp(query, "mypage") == 0) {
380
		title = rv_strdup("My Page");
381
		desc = rv_strdup("You manage your information here.");
382
		if(user == NULL) {
383
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
384
			add_data(&page, INSTANCE_ROOT);
385
			add_data(&page, "/?page=login\">log in</a>?\n");
39 nishi 386
		} else {
387
			page = rv_strdup("");
388
			add_data(&page, "<h2 id=\"youricon\">Your Icon</h2>\n");
389
			add_data(&page, "<a href=\"");
390
			add_data(&page, WWW_AVATAR_ROOT);
391
			add_data(&page, "/");
392
			add_data(&page, user);
393
			add_data(&page, ".png\"><img src=\"");
394
			add_data(&page, WWW_AVATAR_ROOT);
395
			add_data(&page, "/");
396
			add_data(&page, user);
42 nishi 397
			add_data(&page, ".png");
398
			add_data(&page, nocache);
399
			add_data(&page, "\" alt=\"Your Icon\" width=\"50%\"></a>");
39 nishi 400
			add_data(&page, "<form action=\"");
401
			add_data(&page, INSTANCE_ROOT);
402
			add_data(&page, "/?page=uploadpfp\" method=\"POST\" enctype=\"multipart/form-data\">\n");
403
			add_data(&page, "	<input type=\"file\" name=\"pfp\">\n");
404
			add_data(&page, "	<input type=\"submit\" value=\"Upload\">\n");
405
			add_data(&page, "</form>\n");
6 nishi 406
		}
27 nishi 407
#endif
42 nishi 408
#ifdef USE_AVATAR
409
	} else if(strcmp(query, "uploadpfp") == 0) {
410
		title = rv_strdup("Uploading Profile Picture Result");
411
		page = rv_strdup("");
412
		if(user == NULL) {
413
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
414
			add_data(&page, INSTANCE_ROOT);
415
			add_data(&page, "/?page=login\">log in</a>?\n");
416
		} else if(rv_get_multipart("pfp") == NULL) {
417
			add_data(&page, "Invalid Form.");
418
		} else {
419
			struct multipart_entry* entry = rv_get_multipart("pfp");
420
			char* tmp = rv_strcat3(AVATAR_ROOT, "/", user);
421
			char* path = rv_strcat(tmp, ".tmp");
422
			char* outpath = rv_strcat(tmp, ".png");
423
			free(tmp);
424
			FILE* f = fopen(path, "wb");
425
			fwrite(entry->data, 1, entry->length, f);
426
			fclose(f);
427
			char* reason;
428
			if(rv_resize_picture(path, outpath, &reason)) {
429
				add_data(&page, "Uploaded the profile picture successfully.\n");
430
			} else {
431
				add_data(&page, "Failed to upload the profile picture.<br><code>\n");
432
				char* esc = html_escape(reason);
433
				add_data(&page, esc);
434
				free(esc);
435
				add_data(&page, "</code>\n");
436
				free(reason);
437
			}
438
			free(path);
439
			free(outpath);
440
		}
441
#endif
9 nishi 442
	} else if(strcmp(query, "myrepo") == 0) {
443
		title = rv_strdup("My Repositories");
444
		desc = rv_strdup("You manage your repositories here.");
445
		if(user == NULL) {
446
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
447
			add_data(&page, INSTANCE_ROOT);
448
			add_data(&page, "/?page=login\">log in</a>?\n");
10 nishi 449
		} else {
11 nishi 450
			char cbuf[2];
451
			cbuf[0] = REPO_USER_DELIM;
452
			cbuf[1] = 0;
10 nishi 453
			nav = rv_strdup("");
454
			add_data(&nav, "<li><a href=\"#createrepo\">Create a repository</a></li>\n");
455
			add_data(&nav, "<li><a href=\"#repolist\">Repository List</a></li>\n");
456
			page = rv_strdup("");
457
			add_data(&page, "<h2 id=\"createrepo\">Create a repository</h2>\n");
458
			add_data(&page, "<form action=\"");
459
			add_data(&page, INSTANCE_ROOT);
460
			add_data(&page, "/?page=createrepo\" method=\"POST\">\n");
461
			add_data(&page, "	<table border=\"0\">\n");
462
			add_data(&page, "		<tr>\n");
463
			add_data(&page, "			<th>Repository name</th>\n");
464
			add_data(&page, "			<td>\n");
465
			add_data(&page, "				<input name=\"name\">\n");
466
			add_data(&page, "			</td>\n");
467
			add_data(&page, "			<td><input type=\"submit\" value=\"Create\"></td>\n");
468
			add_data(&page, "		</tr>\n");
469
			add_data(&page, "	</table>\n");
11 nishi 470
			add_data(&page, "Repository name cannot contain '<code>");
471
			add_data(&page, cbuf);
22 nishi 472
			add_data(&page, "</code>', '<code>#</code>', '<code>\\</code>', and '<code>/</code>'.");
10 nishi 473
			add_data(&page, "</form>\n");
474
			add_data(&page, "<h2 id=\"repolist\">Repository List</h2>\n");
11 nishi 475
			add_data(&page, "<table border=\"0\">\n");
476
			add_data(&page, "<tr><th>Repository name</th><th>Revision</th></tr>\n");
477
			rv_repo_list(user, list_repo);
478
			add_data(&page, "</table>\n");
9 nishi 479
		}
11 nishi 480
	} else if(strcmp(query, "createrepo") == 0) {
481
		title = rv_strdup("Creating Repository Result");
482
		page = rv_strdup("");
483
 
484
		rv_load_query('P');
485
		if(user == NULL) {
486
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
487
			add_data(&page, INSTANCE_ROOT);
488
			add_data(&page, "/?page=login\">log in</a>?\n");
489
		} else if(rv_get_query("name") == NULL) {
490
			add_data(&page, "Invalid form.\n");
491
		} else {
492
			int i;
493
			bool reject = false;
494
			char* name = rv_get_query("name");
495
			for(i = 0; name[i] != 0; i++) {
42 nishi 496
				if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
11 nishi 497
					char cbuf[2];
498
					cbuf[0] = REPO_USER_DELIM;
499
					cbuf[1] = 0;
500
					add_data(&page, "Repository name cannot contain '<code>");
501
					add_data(&page, cbuf);
502
					add_data(&page, "</code>'.");
503
					reject = true;
504
					break;
505
				}
506
			}
507
			if(!reject) {
508
				char* ru = rv_construct_repouser(name, user);
509
				if(rv_repo_exists(ru)) {
510
					add_data(&page, "Repository already exists.");
511
				} else {
512
					char* esc;
513
					rv_create_repo(ru);
514
					add_data(&page, "Repository has been created.<br>\n");
515
					add_data(&page, "<a href=\"");
516
					add_data(&page, INSTANCE_ROOT);
517
					esc = url_escape(name);
518
					add_data(&page, "/?page=repo&reponame=");
519
					add_data(&page, esc);
520
					free(esc);
521
					esc = url_escape(user);
522
					add_data(&page, "&username=");
523
					add_data(&page, esc);
524
					free(esc);
525
					add_data(&page, "\">Go to the repository</a>.\n");
526
				}
527
				free(ru);
528
			}
529
		}
7 nishi 530
	} else if(strcmp(query, "logout") == 0) {
531
		title = rv_strdup("Logout");
532
		desc = rv_strdup("You can log out from your account here.");
533
		if(user == NULL) {
534
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
535
			add_data(&page, INSTANCE_ROOT);
536
			add_data(&page, "/?page=login\">log in</a>?\n");
537
		} else {
538
			page = rv_strdup("");
539
			add_data(&page, "Are you sure you want to log out?\n");
540
			add_data(&page, "<form method=\"POST\" action=\"");
541
			add_data(&page, INSTANCE_ROOT);
542
			add_data(&page, "/?page=sendlogout\">\n");
543
			add_data(&page, "	<input type=\"submit\" value=\"Yes\">\n");
544
			add_data(&page, "</form>\n");
545
		}
11 nishi 546
	} else if(strcmp(query, "repo") == 0) {
547
		title = rv_strdup("Repository");
548
		desc = rv_strdup("");
549
		page = rv_strdup("");
550
		nav = rv_strdup("");
15 nishi 551
		rv_load_query('Q');
11 nishi 552
		if(rv_get_query("username") == NULL || rv_get_query("reponame") == NULL) {
553
			add_data(&page, "Required parameters not set.");
554
		} else {
15 nishi 555
			char* ruser = rv_get_query("username");
11 nishi 556
			char* repo = rv_get_query("reponame");
15 nishi 557
			char* repouser = rv_construct_repouser(repo, ruser);
11 nishi 558
			grepouser = repouser;
559
			if(rv_repo_exists(repouser)) {
15 nishi 560
				char* showuser = html_escape(ruser);
11 nishi 561
				char* showrepo = html_escape(repo);
562
				char* showreadme = rv_get_readme(repouser);
563
				desc = html_escape_nl_to_br(showreadme);
564
				add_data(&title, " - ");
32 nishi 565
				add_data(&title, showuser);
566
				add_data(&title, "/");
11 nishi 567
				add_data(&title, showrepo);
568
				free(showuser);
569
				free(showrepo);
570
				free(showreadme);
15 nishi 571
#ifdef WWW_SVN_ROOT
16 nishi 572
				add_data(&nav, "<li><a href=\"#repoinfo\">Info</a></li>\n");
15 nishi 573
				add_data(&page, "<h2 id=\"repoinfo\">Info</h2>\n");
574
				add_data(&page, "<a href=\"");
575
				add_data(&page, WWW_SVN_ROOT);
576
				add_data(&page, "/");
577
				char* escru = url_escape(repouser);
578
				add_data(&page, escru);
579
				free(escru);
580
				add_data(&page, "\">Raw repository</a>");
581
#endif
11 nishi 582
 
583
				int isdir;
584
				char* path = rv_get_query("path");
585
				if(path == NULL) path = "/";
586
				fcounter = 0;
587
				add_data(&page, "<table border=\"0\" style=\"width: 100%;\">");
15 nishi 588
				bool rej = false;
11 nishi 589
				if(!rv_get_list(repouser, path, list_files, &isdir)) {
590
					add_data(&page, "<tr><td>Path not found.</td></tr>\n");
15 nishi 591
					rej = true;
11 nishi 592
				}
593
				add_data(&page, "</table>");
15 nishi 594
				if(isdir == 0 && !rej) {
11 nishi 595
					add_data(&nav, "<li><a href=\"#filecontent\">Content</a></li>");
596
					add_data(&page, "<h2 id=\"filecontent\">Content</h2>\n");
597
					add_data(&page, "<pre class=\"codeblock\"><code>");
13 nishi 598
#ifdef USE_ENSCRIPT
599
					int i;
600
					char* ext = NULL;
601
					for(i = strlen(path) - 1; i >= 0; i--) {
602
						if(path[i] == '.') {
603
							ext = path + i + 1;
604
							break;
605
						}
606
					}
607
					char* data = rv_enscript(repouser, path, ext);
608
					if(data != NULL) {
609
						add_data(&page, data);
610
						free(data);
611
					} else {
612
						data = rv_read_file(repouser, path);
613
						if(data != NULL) {
614
							char* esc = html_escape_nl_to_br(data);
615
							add_data(&page, esc);
616
							free(esc);
617
							free(data);
618
						} else {
619
							add_data(&page, "Cannot open the file.\n");
620
						}
621
					}
622
#else
11 nishi 623
					char* data = rv_read_file(repouser, path);
12 nishi 624
					if(data != NULL) {
625
						char* esc = html_escape_nl_to_br(data);
626
						add_data(&page, esc);
627
						free(esc);
628
						free(data);
629
					} else {
630
						add_data(&page, "Cannot open the file.\n");
631
					}
13 nishi 632
#endif
11 nishi 633
					add_data(&page, "</code></pre>");
634
				}
15 nishi 635
				if(user != NULL && strcmp(user, ruser) == 0) {
636
					char* esc;
637
					add_data(&nav, "<li><a href=\"#managerepo\">Manage The Repository</a></li>\n");
638
					add_data(&page, "<h2 id=\"managerepo\">Manage The Repository</h2>\n");
639
					add_data(&page, "<form action=\"");
640
					add_data(&page, INSTANCE_ROOT);
641
					add_data(&page, "/?page=sendmanrepo&username=");
642
					esc = url_escape(ruser);
643
					add_data(&page, esc);
644
					free(esc);
645
					add_data(&page, "&reponame=");
21 nishi 646
					esc = url_escape(repo);
15 nishi 647
					add_data(&page, esc);
648
					free(esc);
649
					add_data(&page, "\" method=\"POST\">\n");
650
					add_data(&page, "<table border=\"0\" style=\"width: 100%;\">\n");
651
					add_data(&page, "	<tr>\n");
652
					add_data(&page, "		<th>README</th>\n");
653
					add_data(&page, "		<td>\n");
654
					add_data(&page, "			<textarea name=\"readme\" style=\"width: 100%;resize: none;height: 128px;\">\n");
655
					char* readme = rv_get_readme(repouser);
656
					esc = html_escape(readme);
657
					add_data(&page, esc);
658
					free(esc);
659
					free(readme);
660
					add_data(&page, "			</textarea>\n");
661
					add_data(&page, "		</td>\n");
662
					add_data(&page, "	</tr>\n");
663
					add_data(&page, "</table>\n");
664
					add_data(&page, "<input type=\"submit\" value=\"Send\">\n");
665
					add_data(&page, "</form>\n");
666
					add_data(&page, "<a href=\"");
667
					add_data(&page, INSTANCE_ROOT);
668
					add_data(&page, "/?page=deleterepo&username=");
669
					esc = url_escape(ruser);
670
					add_data(&page, esc);
671
					free(esc);
672
					add_data(&page, "&reponame=");
673
					esc = url_escape(ruser);
674
					add_data(&page, esc);
675
					free(esc);
676
					add_data(&page, "\">\n");
677
					add_data(&page, "Delete repository\n");
678
					add_data(&page, "</a>\n");
679
				}
11 nishi 680
			} else {
681
				add_data(&page, "Repository does not exist.\n");
682
			}
683
			free(repouser);
684
		}
15 nishi 685
	} else if(strcmp(query, "deleterepo") == 0) {
686
		title = rv_strdup("Delete The Repository");
687
		page = rv_strdup("");
688
 
689
		rv_load_query('Q');
690
		if(user == NULL) {
691
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
692
			add_data(&page, INSTANCE_ROOT);
693
			add_data(&page, "/?page=login\">log in</a>?\n");
694
		} else if(rv_get_query("username") == NULL || rv_get_query("reponame") == NULL) {
695
			add_data(&page, "Invalid Form.\n");
696
		} else {
697
			char* esc;
698
			add_data(&page, "Are you sure you want to delete the repository?\n");
699
			add_data(&page, "<form method=\"POST\" action=\"");
700
			add_data(&page, INSTANCE_ROOT);
701
			add_data(&page, "/?page=senddeleterepo&username=");
702
			esc = url_escape(rv_get_query("username"));
703
			add_data(&page, esc);
704
			free(esc);
705
			add_data(&page, "&reponame=");
706
			esc = url_escape(rv_get_query("reponame"));
707
			add_data(&page, esc);
708
			free(esc);
709
			add_data(&page, "\">");
710
			add_data(&page, "	<input type=\"submit\" value=\"Yes\">\n");
711
			add_data(&page, "</form>\n");
712
		}
713
	} else if(strcmp(query, "senddeleterepo") == 0) {
714
		title = rv_strdup("Deleting Repository Result");
715
		page = rv_strdup("");
716
 
717
		rv_load_query('Q');
718
		if(user == NULL) {
719
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
720
			add_data(&page, INSTANCE_ROOT);
721
			add_data(&page, "/?page=login\">log in</a>?\n");
722
		} else if(rv_get_query("username") == NULL || rv_get_query("reponame") == NULL) {
723
			add_data(&page, "Invalid Form.\n");
724
		} else if(strcmp(rv_get_query("username"), user) != 0) {
725
			add_data(&page, "You are not the owner of the repository.\n");
726
		} else {
727
			char* repouser = rv_construct_repouser(rv_get_query("reponame"), rv_get_query("username"));
728
			if(rv_repo_exists(repouser)) {
729
				rv_remove_repo(repouser);
730
				add_data(&page, "Deleted the repository successfully.<br>\n");
731
			} else {
732
				add_data(&page, "Repository does not exist.<br>\n");
733
			}
734
		}
735
	} else if(strcmp(query, "sendmanrepo") == 0) {
736
		title = rv_strdup("Modifying Repository Result");
737
		page = rv_strdup("");
738
 
739
		rv_load_query('Q');
740
		if(user == NULL) {
741
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
742
			add_data(&page, INSTANCE_ROOT);
743
			add_data(&page, "/?page=login\">log in</a>?\n");
744
		} else if(rv_get_query("username") == NULL || rv_get_query("reponame") == NULL) {
745
			add_data(&page, "Invalid Form.\n");
746
		} else if(strcmp(rv_get_query("username"), user) != 0) {
747
			add_data(&page, "You are not the owner of the repository.\n");
748
		} else {
749
			char* esc;
750
			rv_load_query('P');
751
			char* readme = rv_get_query("readme");
752
			if(readme != NULL) {
753
				rv_load_query('Q');
754
				char* name = rv_construct_repouser(rv_get_query("reponame"), rv_get_query("username"));
755
				rv_set_readme(name, readme);
756
				free(name);
757
			}
758
			rv_load_query('Q');
759
			add_data(&page, "Modified the repository successfully.<br>\n");
760
			add_data(&page, "<a href=\"");
761
			add_data(&page, INSTANCE_ROOT);
762
			add_data(&page, "?page=repo&username=");
763
			esc = url_escape(rv_get_query("username"));
764
			add_data(&page, esc);
765
			free(esc);
766
			add_data(&page, "&reponame=");
767
			esc = url_escape(rv_get_query("reponame"));
768
			add_data(&page, esc);
769
			free(esc);
770
			add_data(&page, "\">Go back to the repository</a>.\n");
771
		}
1 nishi 772
	}
773
 
774
	if(title == NULL) title = rv_strdup("");
775
	if(desc == NULL) desc = rv_strdup("");
776
	if(page == NULL) page = rv_strdup("");
10 nishi 777
	if(nav == NULL) nav = rv_strdup("");
39 nishi 778
 
779
#ifdef USE_AVATAR
780
	generate_avatar();
781
#endif
782
 
1 nishi 783
	render_stuff();
15 nishi 784
freeall:
1 nishi 785
	free(page);
786
	free(desc);
787
	free(title);
10 nishi 788
	free(nav);
1 nishi 789
}
790
 
3 nishi 791
char* escape(const char* str) {
1 nishi 792
	char* r = malloc(1);
793
	r[0] = 0;
794
	char cbuf[2];
795
	cbuf[1] = 0;
796
	int i;
3 nishi 797
	for(i = 0; str[i] != 0; i++) {
798
		if(str[i] == '<') {
1 nishi 799
			char* tmp = r;
800
			r = rv_strcat(tmp, "&lt;");
801
			free(tmp);
3 nishi 802
		} else if(str[i] == '>') {
1 nishi 803
			char* tmp = r;
804
			r = rv_strcat(tmp, "&gt;");
805
			free(tmp);
3 nishi 806
		} else {
1 nishi 807
			cbuf[0] = str[i];
808
			char* tmp = r;
809
			r = rv_strcat(tmp, cbuf);
810
			free(tmp);
811
		}
812
	}
813
	return r;
814
}
815
 
3 nishi 816
void render_stuff(void) {
1 nishi 817
	char* escaped;
818
	add_data(&buffer, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
819
	add_data(&buffer, "<html>\n");
820
	add_data(&buffer, "	<head>\n");
821
	add_data(&buffer, "		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");
822
	add_data(&buffer, "		<title>");
823
	add_data(&buffer, INSTANCE_NAME);
824
	add_data(&buffer, " - ");
825
	add_data(&buffer, title);
826
	add_data(&buffer, "</title>\n");
827
	add_data(&buffer, "		<style type=\"text/css\">\n");
828
	add_data(&buffer, "* {\n");
829
	add_data(&buffer, "	padding: 0;\n");
830
	add_data(&buffer, "	margin: 0;\n");
831
	add_data(&buffer, "}\n");
832
	add_data(&buffer, "li {\n");
833
	add_data(&buffer, "	list-style: outside;\n");
834
	add_data(&buffer, "	margin-left: 1.25em;\n");
835
	add_data(&buffer, "}\n");
836
	add_data(&buffer, "a {\n");
837
	add_data(&buffer, "	text-decoration: none;\n");
838
	add_data(&buffer, "}\n");
839
	add_data(&buffer, "#nav div {\n");
840
	add_data(&buffer, "	float: left;\n");
841
	add_data(&buffer, "	margin: 0 0;\n");
842
	add_data(&buffer, "	padding-left: 0;\n");
9 nishi 843
	add_data(&buffer, "	padding-right: 25px;\n");
1 nishi 844
	add_data(&buffer, "	padding-top: 7px;\n");
845
	add_data(&buffer, "}\n");
11 nishi 846
	add_data(&buffer, "th,td {\n");
847
	add_data(&buffer, "	padding: 2px;\n");
848
	add_data(&buffer, "}\n");
1 nishi 849
	add_data(&buffer, "body {\n");
850
	add_data(&buffer, "	background-color: #1F4677;\n");
851
	add_data(&buffer, "	width: 940px;\n");
852
	add_data(&buffer, "	margin: 5px auto;\n");
853
	add_data(&buffer, "	font-family: sans-serif;\n");
854
	add_data(&buffer, "}\n");
855
	add_data(&buffer, "#nav {\n");
856
	add_data(&buffer, "	background-color: white;\n");
6 nishi 857
	add_data(&buffer, "	background-image: url('");
858
	add_data(&buffer, INSTANCE_NAVBAR);
859
	add_data(&buffer, "');\n");
1 nishi 860
	add_data(&buffer, "	height: 44px;\n");
861
	add_data(&buffer, "	padding: 8px;\n");
862
	add_data(&buffer, "	padding-left: 32px;\n");
863
	add_data(&buffer, "	font-size: 22px;\n");
864
	add_data(&buffer, "	font-weight: bold;\n");
865
	add_data(&buffer, "}\n");
11 nishi 866
	add_data(&buffer, "pre {\n");
867
	add_data(&buffer, "	background-color: #dddddd;\n");
868
	add_data(&buffer, "	border: solid 2px #bbbbbb;\n");
12 nishi 869
	add_data(&buffer, "	padding: 8px;\n");
870
	add_data(&buffer, "	overflow: scroll;\n");
11 nishi 871
	add_data(&buffer, "}\n");
1 nishi 872
	add_data(&buffer, "#index {\n");
873
	add_data(&buffer, "	list-style: none;\n");
874
	add_data(&buffer, "	line-height: normal;\n");
875
	add_data(&buffer, "	margin: auto 0;\n");
876
	add_data(&buffer, "	padding-left: 0;\n");
877
	add_data(&buffer, "}\n");
878
	add_data(&buffer, "#desc {\n");
879
	add_data(&buffer, "	background-color: #D2E1F6;\n");
880
	add_data(&buffer, "	margin: 9px auto;\n");
881
	add_data(&buffer, "	height: 128px;\n");
882
	add_data(&buffer, "	padding: 24px;\n");
883
	add_data(&buffer, "}\n");
884
	add_data(&buffer, "#descinside {\n");
885
	add_data(&buffer, "	float: left;\n");
11 nishi 886
	add_data(&buffer, "	width: 700px;\n");
887
	add_data(&buffer, "	overflow-y: scroll;\n");
888
	add_data(&buffer, "	max-height: 128px;\n");
1 nishi 889
	add_data(&buffer, "}\n");
890
	add_data(&buffer, "#logo {\n");
891
	add_data(&buffer, "	float: right;\n");
892
	add_data(&buffer, "}\n");
893
	add_data(&buffer, "#content {\n");
894
	add_data(&buffer, "	background-color: #FFFFFF;\n");
895
	add_data(&buffer, "	margin: -10px auto;\n");
896
	add_data(&buffer, "	padding: 8px 24px 24px;\n");
897
	add_data(&buffer, "}\n");
898
	add_data(&buffer, "#pageindex {\n");
899
	add_data(&buffer, "	background-color: #FFFFFF;\n");
900
	add_data(&buffer, "	padding-right: 24px;\n");
901
	add_data(&buffer, "	padding-bottom: 24px;\n");
902
	add_data(&buffer, "	float: left;\n");
903
	add_data(&buffer, "	border-right: 4px #1F4677 solid;\n");
904
	add_data(&buffer, "	width: 150px;\n");
905
	add_data(&buffer, "}\n");
906
	add_data(&buffer, "#pagecontent {\n");
907
	add_data(&buffer, "	background-color: #FFFFFF;\n");
908
	add_data(&buffer, "	width: 670px;\n");
909
	add_data(&buffer, "	float: right;\n");
910
	add_data(&buffer, "	padding-left: 24px;\n");
911
	add_data(&buffer, "}\n");
912
	add_data(&buffer, "#footer {\n");
913
	add_data(&buffer, "	background-color: #D2E1F6;\n");
914
	add_data(&buffer, "	padding: 8px 8px 48px;\n");
915
	add_data(&buffer, "	margin: 8px auto;\n");
916
	add_data(&buffer, "	font-size: 15px;\n");
917
	add_data(&buffer, "	height: 32px;\n");
918
	add_data(&buffer, "}\n");
919
	add_data(&buffer, ".fixfloat {\n");
920
	add_data(&buffer, "	clear: both;\n");
921
	add_data(&buffer, "}\n");
922
	add_data(&buffer, "#copyright {\n");
923
	add_data(&buffer, "	float: right;\n");
924
	add_data(&buffer, "	font-size: 10px;\n");
925
	add_data(&buffer, "	margin-top: 16px;\n");
926
	add_data(&buffer, "}\n");
927
	add_data(&buffer, "#gotop {\n");
928
	add_data(&buffer, "	position: absolute;\n");
929
	add_data(&buffer, "}\n");
930
	add_data(&buffer, "h2, h3 {\n");
931
	add_data(&buffer, "	padding-top: 8px;\n");
932
	add_data(&buffer, "	padding-bottom: 8px;\n");
933
	add_data(&buffer, "}\n");
934
	add_data(&buffer, "img {\n");
935
	add_data(&buffer, "	border: none;\n");
936
	add_data(&buffer, "}\n");
937
	add_data(&buffer, "		</style>\n");
938
	add_data(&buffer, "	</head>\n");
939
	add_data(&buffer, "	<body>\n");
940
	add_data(&buffer, "		<div id=\"nav\">\n");
941
	add_data(&buffer, "			<div>\n");
942
	add_data(&buffer, "				<a href=\"");
943
	add_data(&buffer, INSTANCE_ROOT);
944
	add_data(&buffer, "/\">Home</a>\n");
945
	add_data(&buffer, "			</div>\n");
5 nishi 946
	if(user == NULL) {
947
		add_data(&buffer, "			<div>\n");
948
		add_data(&buffer, "				<a href=\"");
949
		add_data(&buffer, INSTANCE_ROOT);
950
		add_data(&buffer, "/?page=login\">Login</a>\n");
951
		add_data(&buffer, "			</div>\n");
13 nishi 952
#ifdef ALLOW_SIGNUP
953
		add_data(&buffer, "			<div>\n");
954
		add_data(&buffer, "				<a href=\"");
955
		add_data(&buffer, INSTANCE_ROOT);
956
		add_data(&buffer, "/?page=signup\">Signup</a>\n");
957
		add_data(&buffer, "			</div>\n");
958
#endif
7 nishi 959
	} else {
27 nishi 960
#ifdef USE_MYPAGE
7 nishi 961
		add_data(&buffer, "			<div>\n");
962
		add_data(&buffer, "				<a href=\"");
963
		add_data(&buffer, INSTANCE_ROOT);
9 nishi 964
		add_data(&buffer, "/?page=mypage\">My Page</a>\n");
965
		add_data(&buffer, "			</div>\n");
27 nishi 966
#endif
9 nishi 967
 
968
		add_data(&buffer, "			<div>\n");
969
		add_data(&buffer, "				<a href=\"");
970
		add_data(&buffer, INSTANCE_ROOT);
971
		add_data(&buffer, "/?page=myrepo\">My Repositories</a>\n");
972
		add_data(&buffer, "			</div>\n");
973
 
974
		add_data(&buffer, "			<div>\n");
975
		add_data(&buffer, "				<a href=\"");
976
		add_data(&buffer, INSTANCE_ROOT);
7 nishi 977
		add_data(&buffer, "/?page=logout\">Logout</a>\n");
978
		add_data(&buffer, "			</div>\n");
5 nishi 979
	}
6 nishi 980
	if(user != NULL) {
981
		add_data(&buffer, "<div style=\"float: right;font-size: 10px;padding-top: 36px;padding-right: 0;font-style: italic;\">You have logged in as <a href=\"");
982
		add_data(&buffer, INSTANCE_ROOT);
983
		add_data(&buffer, "/?page=mypage\">");
984
		add_data(&buffer, user);
985
		add_data(&buffer, "</a></div>");
986
	}
1 nishi 987
	add_data(&buffer, "		</div>\n");
988
	add_data(&buffer, "		<div id=\"desc\">\n");
989
	add_data(&buffer, "			<div id=\"descinside\">\n");
990
	add_data(&buffer, "				<h1>");
991
	add_data(&buffer, title);
992
	add_data(&buffer, "</h1>\n");
993
	add_data(&buffer, "				<p>\n");
994
	add_data(&buffer, desc);
995
	add_data(&buffer, "				</p>\n");
996
	add_data(&buffer, "			</div>\n");
997
	add_data(&buffer, "			<img id=\"logo\" src=\"");
998
	add_data(&buffer, INSTANCE_LOGO);
999
	add_data(&buffer, "\" height=\"128px\" alt=\"logo\">\n");
1000
	add_data(&buffer, "		</div>\n");
1001
	add_data(&buffer, "		<div id=\"content\">\n");
1002
	add_data(&buffer, "			<div id=\"pageindex\">\n");
1003
	add_data(&buffer, "				<h3>Page Menu</h3>\n");
1004
	add_data(&buffer, "				<ul>\n");
10 nishi 1005
	add_data(&buffer, nav);
1 nishi 1006
	add_data(&buffer, "				</ul>\n");
1007
	add_data(&buffer, "			</div>\n");
1008
	add_data(&buffer, "			<div id=\"pagecontent\">\n");
1009
	add_data(&buffer, page);
1010
	add_data(&buffer, "			</div>\n");
11 nishi 1011
	add_data(&buffer, "			<div class=\"fixfloat\"></div>\n");
1 nishi 1012
	add_data(&buffer, "		</div>\n");
1013
	add_data(&buffer, "		<div id=\"footer\">\n");
1014
	add_data(&buffer, "			<div id=\"gotop\">\n");
1015
	add_data(&buffer, "				<a href=\"#top\">Top</a>\n");
1016
	add_data(&buffer, "			</div>\n");
1017
	add_data(&buffer, "			<div id=\"copyright\">\n");
1018
	add_data(&buffer, "				");
1019
	escaped = escape(INSTANCE_ADMIN);
1020
	add_data(&buffer, escaped);
1021
	free(escaped);
1022
	add_data(&buffer, "\n");
1023
	add_data(&buffer, "			</div>\n");
1024
	add_data(&buffer, "			<div class=\"fixfloat\"></div>\n");
15 nishi 1025
#ifdef INSTANCE_BANNERS
1026
	add_data(&buffer, "			<div id=\"banners\" style=\"clear: both;\">\n");
1027
	add_data(&buffer, INSTANCE_BANNERS);
1028
	add_data(&buffer, "			</div>\n");
1029
#else
1030
	add_data(&buffer, "			<div class=\"fixfloat\"></div>\n");
1031
#endif
1 nishi 1032
	add_data(&buffer, "		</div>\n");
1033
	add_data(&buffer, "	</body>\n");
1034
	add_data(&buffer, "</html>\n");
5 nishi 1035
	if(user != NULL) free(user);
1 nishi 1036
}