Subversion Repositories RepoView

Rev

Rev 42 | Rev 44 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42 Rev 43
Line 1... Line 1...
1
/* $Id: modern.c 42 2024-08-22 05:25:09Z nishi $ */
1
/* $Id: modern.c 43 2024-08-22 05:51:35Z nishi $ */
2
 
2
 
3
#include "rv_query.h"
3
#include "rv_query.h"
4
 
4
 
5
#include "rv_util.h"
5
#include "rv_util.h"
6
#include "rv_version.h"
6
#include "rv_version.h"
Line 21... Line 21...
21
 
21
 
22
#ifdef USE_GRAPHICSMAGICK
22
#ifdef USE_GRAPHICSMAGICK
23
#include "rv_magick.h"
23
#include "rv_magick.h"
24
#endif
24
#endif
25
 
25
 
-
 
26
#include <sys/stat.h>
26
#include <stdio.h>
27
#include <stdio.h>
27
#include <stdlib.h>
28
#include <stdlib.h>
28
#include <string.h>
29
#include <string.h>
29
#include <unistd.h>
30
#include <unistd.h>
30
 
31
 
Line 35... Line 36...
35
 
36
 
36
char* title = NULL;
37
char* title = NULL;
37
char* desc = NULL;
38
char* desc = NULL;
38
char* page = NULL;
39
char* page = NULL;
39
char* nav = NULL;
40
char* nav = NULL;
-
 
41
char* logo = NULL;
40
char* grepouser;
42
char* grepouser;
41
extern char* user;
43
extern char* user;
42
 
44
 
-
 
45
bool invalid_char(char c) {
-
 
46
	if(c >= '0' && c <= '9') return false;
-
 
47
	if(c >= 'a' && c <= 'z') return false;
-
 
48
	if(c >= 'A' && c <= 'Z') return false;
-
 
49
	if(c == '_' && c == '-' && c == '.') return false;
-
 
50
}
-
 
51
 
43
char* url_escape(const char* input) {
52
char* url_escape(const char* input) {
44
	const char hex[] = "0123456789ABCDEF";
53
	const char hex[] = "0123456789ABCDEF";
45
	char* r = malloc(1);
54
	char* r = malloc(1);
46
	r[0] = 0;
55
	r[0] = 0;
47
	char cbuf[2];
56
	char cbuf[2];
Line 270... Line 279...
270
		char cbuf[2];
279
		char cbuf[2];
271
		cbuf[0] = REPO_USER_DELIM;
280
		cbuf[0] = REPO_USER_DELIM;
272
		cbuf[1] = 0;
281
		cbuf[1] = 0;
273
		add_data(&page, "Username cannot contain '<code>");
282
		add_data(&page, "Username cannot contain '<code>");
274
		add_data(&page, cbuf);
283
		add_data(&page, cbuf);
275
		add_data(&page, "</code>', '<code>#</code>', '<code>\\</code>', and '<code>/</code>'.<br>");
284
		add_data(&page, "</code>'.<br>");
276
		add_data(&page, "	<input type=\"submit\" value=\"Signup\">\n");
285
		add_data(&page, "	<input type=\"submit\" value=\"Signup\">\n");
277
		add_data(&page, "</form>\n");
286
		add_data(&page, "</form>\n");
278
	} else if(strcmp(query, "sendsignup") == 0) {
287
	} else if(strcmp(query, "sendsignup") == 0) {
279
		title = rv_strdup("Signup Result");
288
		title = rv_strdup("Signup Result");
280
		page = rv_strdup("");
289
		page = rv_strdup("");
Line 293... Line 302...
293
				if(user != NULL) free(user);
302
				if(user != NULL) free(user);
294
				int i;
303
				int i;
295
				bool reject = false;
304
				bool reject = false;
296
				char* name = rv_get_query("username");
305
				char* name = rv_get_query("username");
297
				for(i = 0; name[i] != 0; i++) {
306
				for(i = 0; name[i] != 0; i++) {
298
					if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
307
					if(name[i] == REPO_USER_DELIM || invalid_char(name[i])) {
299
						char cbuf[2];
308
						char cbuf[2];
300
						cbuf[0] = REPO_USER_DELIM;
309
						cbuf[0] = REPO_USER_DELIM;
301
						cbuf[1] = 0;
310
						cbuf[1] = 0;
302
						add_data(&page, "Username cannot contain '<code>");
311
						add_data(&page, "Username cannot contain '<code>");
303
						add_data(&page, cbuf);
312
						add_data(&page, cbuf);
Line 383... Line 392...
383
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
392
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
384
			add_data(&page, INSTANCE_ROOT);
393
			add_data(&page, INSTANCE_ROOT);
385
			add_data(&page, "/?page=login\">log in</a>?\n");
394
			add_data(&page, "/?page=login\">log in</a>?\n");
386
		} else {
395
		} else {
387
			page = rv_strdup("");
396
			page = rv_strdup("");
-
 
397
			nav = rv_strdup("");
-
 
398
			add_data(&nav, "<li><a href=\"#youricon\">Your Icon</a></li>\n");
-
 
399
			add_data(&nav, "<li><a href=\"#bio\">Bio</a></li>\n");
-
 
400
			add_data(&page, "<form action=\"");
-
 
401
			add_data(&page, INSTANCE_ROOT);
-
 
402
			add_data(&page, "/?page=upload\" method=\"POST\" enctype=\"multipart/form-data\">\n");
388
			add_data(&page, "<h2 id=\"youricon\">Your Icon</h2>\n");
403
			add_data(&page, "	<h2 id=\"youricon\">Your Icon</h2>\n");
389
			add_data(&page, "<a href=\"");
404
			add_data(&page, "	<a href=\"");
390
			add_data(&page, WWW_AVATAR_ROOT);
405
			add_data(&page, WWW_AVATAR_ROOT);
391
			add_data(&page, "/");
406
			add_data(&page, "/");
392
			add_data(&page, user);
407
			add_data(&page, user);
393
			add_data(&page, ".png\"><img src=\"");
408
			add_data(&page, ".png\"><img src=\"");
394
			add_data(&page, WWW_AVATAR_ROOT);
409
			add_data(&page, WWW_AVATAR_ROOT);
395
			add_data(&page, "/");
410
			add_data(&page, "/");
396
			add_data(&page, user);
411
			add_data(&page, user);
397
			add_data(&page, ".png");
412
			add_data(&page, ".png");
398
			add_data(&page, nocache);
413
			add_data(&page, nocache);
399
			add_data(&page, "\" alt=\"Your Icon\" width=\"50%\"></a>");
414
			add_data(&page, "\" alt=\"Your Icon\" width=\"50%\"></a><br>");
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");
415
			add_data(&page, "	<input type=\"file\" name=\"pfp\">\n");
-
 
416
			add_data(&page, "	<h2 id=\"bio\">Bio</h2>\n");
-
 
417
			add_data(&page, "	<textarea name=\"bio\" style=\"width: 100%;resize: none;height: 128px;\">\n");
-
 
418
			char* path = rv_strcat3(BIO_ROOT, "/", user);
-
 
419
			FILE* f = fopen(path, "r");
-
 
420
			if(f != NULL) {
-
 
421
				struct stat s;
-
 
422
				stat(path, &s);
-
 
423
				char* biobuf = malloc(s.st_size + 1);
-
 
424
				fread(biobuf, 1, s.st_size, f);
-
 
425
				biobuf[s.st_size] = 0;
-
 
426
 
-
 
427
				char* esc = html_escape(biobuf);
-
 
428
				add_data(&page, esc);
-
 
429
				free(esc);
-
 
430
 
-
 
431
				free(biobuf);
-
 
432
				fclose(f);
-
 
433
			}
-
 
434
			free(path);
-
 
435
			add_data(&page, "</textarea>\n");
404
			add_data(&page, "	<input type=\"submit\" value=\"Upload\">\n");
436
			add_data(&page, "	<input type=\"submit\" value=\"Send\">\n");
405
			add_data(&page, "</form>\n");
437
			add_data(&page, "</form>\n");
406
		}
438
		}
407
#endif
439
#endif
408
#ifdef USE_AVATAR
440
#ifdef USE_AVATAR
409
	} else if(strcmp(query, "uploadpfp") == 0) {
441
	} else if(strcmp(query, "upload") == 0) {
410
		title = rv_strdup("Uploading Profile Picture Result");
442
		title = rv_strdup("Uploading My Page Result");
411
		page = rv_strdup("");
443
		page = rv_strdup("");
412
		if(user == NULL) {
444
		if(user == NULL) {
413
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
445
			add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
414
			add_data(&page, INSTANCE_ROOT);
446
			add_data(&page, INSTANCE_ROOT);
415
			add_data(&page, "/?page=login\">log in</a>?\n");
447
			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 {
448
		} else {
419
			struct multipart_entry* entry = rv_get_multipart("pfp");
449
			struct multipart_entry* entry = rv_get_multipart("pfp");
-
 
450
			if(entry != NULL && entry->length > 0) {
420
			char* tmp = rv_strcat3(AVATAR_ROOT, "/", user);
451
				char* tmp = rv_strcat3(AVATAR_ROOT, "/", user);
421
			char* path = rv_strcat(tmp, ".tmp");
452
				char* path = rv_strcat(tmp, ".tmp");
422
			char* outpath = rv_strcat(tmp, ".png");
453
				char* outpath = rv_strcat(tmp, ".png");
423
			free(tmp);
454
				free(tmp);
424
			FILE* f = fopen(path, "wb");
455
				FILE* f = fopen(path, "wb");
425
			fwrite(entry->data, 1, entry->length, f);
456
				fwrite(entry->data, 1, entry->length, f);
426
			fclose(f);
457
				fclose(f);
427
			char* reason;
458
				char* reason;
428
			if(rv_resize_picture(path, outpath, &reason)) {
459
				if(rv_resize_picture(path, outpath, &reason)) {
429
				add_data(&page, "Uploaded the profile picture successfully.\n");
460
					add_data(&page, "Uploaded the profile picture successfully.\n");
430
			} else {
461
				} else {
431
				add_data(&page, "Failed to upload the profile picture.<br><code>\n");
462
					add_data(&page, "Failed to upload the profile picture.<br><code>\n");
432
				char* esc = html_escape(reason);
463
					char* esc = html_escape(reason);
433
				add_data(&page, esc);
464
					add_data(&page, esc);
434
				free(esc);
465
					free(esc);
435
				add_data(&page, "</code>\n");
466
					add_data(&page, "</code>\n");
436
				free(reason);
467
					free(reason);
-
 
468
				}
-
 
469
				free(path);
-
 
470
				free(outpath);
-
 
471
			}
-
 
472
			entry = rv_get_multipart("bio");
-
 
473
			if(entry != NULL) {
-
 
474
				char* path = rv_strcat3(BIO_ROOT, "/", user);
-
 
475
				FILE* f = fopen(path, "w");
-
 
476
				fwrite(entry->data, 1, entry->length, f);
-
 
477
				fclose(f);
-
 
478
				free(path);
-
 
479
				add_data(&page, "Uploaded the bio successfully.\n");
437
			}
480
			}
438
			free(path);
-
 
439
			free(outpath);
-
 
440
		}
481
		}
441
#endif
482
#endif
442
	} else if(strcmp(query, "myrepo") == 0) {
483
	} else if(strcmp(query, "myrepo") == 0) {
443
		title = rv_strdup("My Repositories");
484
		title = rv_strdup("My Repositories");
444
		desc = rv_strdup("You manage your repositories here.");
485
		desc = rv_strdup("You manage your repositories here.");
Line 467... Line 508...
467
			add_data(&page, "			<td><input type=\"submit\" value=\"Create\"></td>\n");
508
			add_data(&page, "			<td><input type=\"submit\" value=\"Create\"></td>\n");
468
			add_data(&page, "		</tr>\n");
509
			add_data(&page, "		</tr>\n");
469
			add_data(&page, "	</table>\n");
510
			add_data(&page, "	</table>\n");
470
			add_data(&page, "Repository name cannot contain '<code>");
511
			add_data(&page, "Repository name cannot contain '<code>");
471
			add_data(&page, cbuf);
512
			add_data(&page, cbuf);
472
			add_data(&page, "</code>', '<code>#</code>', '<code>\\</code>', and '<code>/</code>'.");
513
			add_data(&page, "</code>'.");
473
			add_data(&page, "</form>\n");
514
			add_data(&page, "</form>\n");
474
			add_data(&page, "<h2 id=\"repolist\">Repository List</h2>\n");
515
			add_data(&page, "<h2 id=\"repolist\">Repository List</h2>\n");
475
			add_data(&page, "<table border=\"0\">\n");
516
			add_data(&page, "<table border=\"0\">\n");
476
			add_data(&page, "<tr><th>Repository name</th><th>Revision</th></tr>\n");
517
			add_data(&page, "<tr><th>Repository name</th><th>Revision</th></tr>\n");
477
			rv_repo_list(user, list_repo);
518
			rv_repo_list(user, list_repo);
Line 491... Line 532...
491
		} else {
532
		} else {
492
			int i;
533
			int i;
493
			bool reject = false;
534
			bool reject = false;
494
			char* name = rv_get_query("name");
535
			char* name = rv_get_query("name");
495
			for(i = 0; name[i] != 0; i++) {
536
			for(i = 0; name[i] != 0; i++) {
496
				if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
537
				if(name[i] == REPO_USER_DELIM || invalid_char(name[i])) {
497
					char cbuf[2];
538
					char cbuf[2];
498
					cbuf[0] = REPO_USER_DELIM;
539
					cbuf[0] = REPO_USER_DELIM;
499
					cbuf[1] = 0;
540
					cbuf[1] = 0;
500
					add_data(&page, "Repository name cannot contain '<code>");
541
					add_data(&page, "Repository name cannot contain '<code>");
501
					add_data(&page, cbuf);
542
					add_data(&page, cbuf);
Line 655... Line 696...
655
					char* readme = rv_get_readme(repouser);
696
					char* readme = rv_get_readme(repouser);
656
					esc = html_escape(readme);
697
					esc = html_escape(readme);
657
					add_data(&page, esc);
698
					add_data(&page, esc);
658
					free(esc);
699
					free(esc);
659
					free(readme);
700
					free(readme);
660
					add_data(&page, "			</textarea>\n");
701
					add_data(&page, "</textarea>\n");
661
					add_data(&page, "		</td>\n");
702
					add_data(&page, "		</td>\n");
662
					add_data(&page, "	</tr>\n");
703
					add_data(&page, "	</tr>\n");
663
					add_data(&page, "</table>\n");
704
					add_data(&page, "</table>\n");
664
					add_data(&page, "<input type=\"submit\" value=\"Send\">\n");
705
					add_data(&page, "<input type=\"submit\" value=\"Send\">\n");
665
					add_data(&page, "</form>\n");
706
					add_data(&page, "</form>\n");
Line 730... Line 771...
730
				add_data(&page, "Deleted the repository successfully.<br>\n");
771
				add_data(&page, "Deleted the repository successfully.<br>\n");
731
			} else {
772
			} else {
732
				add_data(&page, "Repository does not exist.<br>\n");
773
				add_data(&page, "Repository does not exist.<br>\n");
733
			}
774
			}
734
		}
775
		}
-
 
776
	} else if(strcmp(query, "person") == 0) {
-
 
777
		title = rv_strdup("Person");
-
 
778
		page = rv_strdup("");
-
 
779
 
-
 
780
		rv_load_query('Q');
-
 
781
		if(rv_get_query("username") == NULL) {
-
 
782
			add_data(&page, "Invalid Form.\n");
-
 
783
		} else {
-
 
784
			if(rv_has_user(rv_get_query("username"))) {
-
 
785
				add_data(&title, " - ");
-
 
786
				add_data(&title, rv_get_query("username"));
-
 
787
				char* path = rv_strcat3(BIO_ROOT, "/", rv_get_query("username"));
-
 
788
				FILE* f = fopen(path, "r");
-
 
789
				if(f != NULL) {
-
 
790
					struct stat s;
-
 
791
					stat(path, &s);
-
 
792
					char* buf = malloc(s.st_size + 1);
-
 
793
					fread(buf, 1, s.st_size, f);
-
 
794
					buf[s.st_size] = 0;
-
 
795
 
-
 
796
					desc = html_escape_nl_to_br(buf);
-
 
797
 
-
 
798
					char* tmp = rv_strcat3(WWW_AVATAR_ROOT, "/", rv_get_query("username"));
-
 
799
					logo = rv_strcat(tmp, ".png");
-
 
800
					free(tmp);
-
 
801
 
-
 
802
					fclose(f);
-
 
803
				}
-
 
804
				free(path);
-
 
805
			} else {
-
 
806
				add_data(&page, "User does not exist.\n");
-
 
807
			}
-
 
808
		}
735
	} else if(strcmp(query, "sendmanrepo") == 0) {
809
	} else if(strcmp(query, "sendmanrepo") == 0) {
736
		title = rv_strdup("Modifying Repository Result");
810
		title = rv_strdup("Modifying Repository Result");
737
		page = rv_strdup("");
811
		page = rv_strdup("");
738
 
812
 
739
		rv_load_query('Q');
813
		rv_load_query('Q');
Line 784... Line 858...
784
freeall:
858
freeall:
785
	free(page);
859
	free(page);
786
	free(desc);
860
	free(desc);
787
	free(title);
861
	free(title);
788
	free(nav);
862
	free(nav);
-
 
863
	if(logo != NULL) free(logo);
789
}
864
}
790
 
865
 
791
char* escape(const char* str) {
866
char* escape(const char* str) {
792
	char* r = malloc(1);
867
	char* r = malloc(1);
793
	r[0] = 0;
868
	r[0] = 0;
Line 976... Line 1051...
976
		add_data(&buffer, INSTANCE_ROOT);
1051
		add_data(&buffer, INSTANCE_ROOT);
977
		add_data(&buffer, "/?page=logout\">Logout</a>\n");
1052
		add_data(&buffer, "/?page=logout\">Logout</a>\n");
978
		add_data(&buffer, "			</div>\n");
1053
		add_data(&buffer, "			</div>\n");
979
	}
1054
	}
980
	if(user != NULL) {
1055
	if(user != NULL) {
-
 
1056
#ifdef USE_AVATAR
-
 
1057
#ifdef USE_MYPAGE
-
 
1058
		add_data(&buffer, "<a href=\"");
-
 
1059
		add_data(&buffer, INSTANCE_ROOT);
-
 
1060
		add_data(&buffer, "/?page=person&username=");
-
 
1061
		add_data(&buffer, user);
-
 
1062
		add_data(&buffer, "\">");
-
 
1063
#endif
-
 
1064
		add_data(&buffer, "<img src=\"");
-
 
1065
		add_data(&buffer, WWW_AVATAR_ROOT);
-
 
1066
		add_data(&buffer, "/");
-
 
1067
		add_data(&buffer, user);
-
 
1068
		add_data(&buffer, ".png\" alt=\"Your Icon\" style=\"float: right;height: 32px;\">");
-
 
1069
#ifdef USE_MYPAGE
-
 
1070
		add_data(&buffer, "</a>");
-
 
1071
#endif
-
 
1072
#endif
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=\"");
1073
		add_data(&buffer, "<div style=\"clear: both;float: right;font-size: 10px;padding-right: 0;font-style: italic;\">You have logged in as <a href=\"");
982
		add_data(&buffer, INSTANCE_ROOT);
1074
		add_data(&buffer, INSTANCE_ROOT);
983
		add_data(&buffer, "/?page=mypage\">");
1075
		add_data(&buffer, "/?page=mypage\">");
984
		add_data(&buffer, user);
1076
		add_data(&buffer, user);
985
		add_data(&buffer, "</a></div>");
1077
		add_data(&buffer, "</a></div>");
986
	}
1078
	}
Line 993... Line 1085...
993
	add_data(&buffer, "				<p>\n");
1085
	add_data(&buffer, "				<p>\n");
994
	add_data(&buffer, desc);
1086
	add_data(&buffer, desc);
995
	add_data(&buffer, "				</p>\n");
1087
	add_data(&buffer, "				</p>\n");
996
	add_data(&buffer, "			</div>\n");
1088
	add_data(&buffer, "			</div>\n");
997
	add_data(&buffer, "			<img id=\"logo\" src=\"");
1089
	add_data(&buffer, "			<img id=\"logo\" src=\"");
-
 
1090
	if(logo != NULL) {
-
 
1091
		add_data(&buffer, logo);
-
 
1092
	} else {
998
	add_data(&buffer, INSTANCE_LOGO);
1093
		add_data(&buffer, INSTANCE_LOGO);
-
 
1094
	}
999
	add_data(&buffer, "\" height=\"128px\" alt=\"logo\">\n");
1095
	add_data(&buffer, "\" height=\"128px\" alt=\"logo\">\n");
1000
	add_data(&buffer, "		</div>\n");
1096
	add_data(&buffer, "		</div>\n");
1001
	add_data(&buffer, "		<div id=\"content\">\n");
1097
	add_data(&buffer, "		<div id=\"content\">\n");
1002
	add_data(&buffer, "			<div id=\"pageindex\">\n");
1098
	add_data(&buffer, "			<div id=\"pageindex\">\n");
1003
	add_data(&buffer, "				<h3>Page Menu</h3>\n");
1099
	add_data(&buffer, "				<h3>Page Menu</h3>\n");