Subversion Repositories RepoView

Rev

Rev 6 | Rev 9 | 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 7 2024-08-21 01:12:44Z 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"
9
 
10
#include "../../config.h"
11
 
7 nishi 12
#include <stdio.h>
1 nishi 13
#include <stdlib.h>
14
#include <string.h>
15
 
16
extern char* buffer;
17
void add_data(char** data, const char* txt);
18
void render_stuff();
19
 
20
char* title = NULL;
21
char* desc = NULL;
22
char* page = NULL;
6 nishi 23
extern char* user;
1 nishi 24
 
3 nishi 25
void render_page(void) {
1 nishi 26
	rv_load_query('Q');
27
	char* query = rv_get_query("page");
28
	if(query == NULL) query = "welcome";
29
 
3 nishi 30
	if(strcmp(query, "welcome") == 0) {
1 nishi 31
		title = rv_strdup("Welcome");
32
		desc = rv_strdup("Welcome to " INSTANCE_NAME ".");
33
		page = rv_strcat3("Welcome to " INSTANCE_NAME ".<br>This instance is running RepoView version ", rv_get_version(), ".");
3 nishi 34
	} else if(strcmp(query, "login") == 0) {
1 nishi 35
		title = rv_strdup("Login");
36
		desc = rv_strdup("You can log in to your account here.");
37
		page = rv_strdup("");
38
 
39
		add_data(&page, "<form action=\"");
40
		add_data(&page, INSTANCE_ROOT);
41
		add_data(&page, "/?page=sendlogin\" method=\"POST\">\n");
42
		add_data(&page, "	<table border=\"0\">\n");
43
		add_data(&page, "		<tr>\n");
44
		add_data(&page, "			<th>Username</th>\n");
45
		add_data(&page, "			<td>\n");
46
		add_data(&page, "				<input name=\"username\">\n");
47
		add_data(&page, "			</td>\n");
48
		add_data(&page, "		</tr>\n");
49
		add_data(&page, "		<tr>\n");
50
		add_data(&page, "			<th>Password</th>\n");
51
		add_data(&page, "			<td>\n");
52
		add_data(&page, "				<input name=\"password\" type=\"password\">\n");
53
		add_data(&page, "			</td>\n");
54
		add_data(&page, "		</tr>\n");
55
		add_data(&page, "	</table>\n");
56
		add_data(&page, "	<input type=\"submit\" value=\"Login\">\n");
57
		add_data(&page, "</form>\n");
3 nishi 58
	} else if(strcmp(query, "sendlogin") == 0) {
1 nishi 59
		title = rv_strdup("Login Result");
60
		page = rv_strdup("");
61
 
62
		rv_load_query('P');
3 nishi 63
		if(rv_get_query("username") == NULL || rv_get_query("password") == NULL) {
5 nishi 64
			add_data(&page, "Invalid form.\n");
3 nishi 65
		} else {
66
			if(rv_has_user(rv_get_query("username"))) {
5 nishi 67
				if(rv_check_password(rv_get_query("username"), rv_get_query("password"))) {
6 nishi 68
					if(user != NULL) free(user);
69
					user = rv_strdup(rv_get_query("username"));
5 nishi 70
					add_data(&page, "Welcome back.\n");
71
					rv_save_login(rv_get_query("username"));
72
				} else {
73
					add_data(&page, "Invalid password.");
74
				}
3 nishi 75
			} else {
5 nishi 76
				add_data(&page, "User does not exist.");
1 nishi 77
			}
78
		}
7 nishi 79
	} else if(strcmp(query, "sendlogout") == 0) {
80
		title = rv_strdup("Logout Result");
81
		page = rv_strdup("");
82
		if(user == NULL) {
83
			add_data(&page, "You were not logged in.\n");
84
		} else {
85
			rv_logout();
86
			add_data(&page, "Goodbye.\n");
87
			free(user);
88
			user = NULL;
89
		}
6 nishi 90
	} else if(strcmp(query, "mypage") == 0) {
91
		title = rv_strdup("My Page");
92
		desc = rv_strdup("You manage your information here.");
93
		if(user == NULL) {
94
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
95
			add_data(&page, INSTANCE_ROOT);
96
			add_data(&page, "/?page=login\">log in</a>?\n");
97
		}
7 nishi 98
	} else if(strcmp(query, "logout") == 0) {
99
		title = rv_strdup("Logout");
100
		desc = rv_strdup("You can log out from your account here.");
101
		if(user == NULL) {
102
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
103
			add_data(&page, INSTANCE_ROOT);
104
			add_data(&page, "/?page=login\">log in</a>?\n");
105
		} else {
106
			page = rv_strdup("");
107
			add_data(&page, "Are you sure you want to log out?\n");
108
			add_data(&page, "<form method=\"POST\" action=\"");
109
			add_data(&page, INSTANCE_ROOT);
110
			add_data(&page, "/?page=sendlogout\">\n");
111
			add_data(&page, "	<input type=\"submit\" value=\"Yes\">\n");
112
			add_data(&page, "</form>\n");
113
		}
1 nishi 114
	}
115
 
116
	if(title == NULL) title = rv_strdup("");
117
	if(desc == NULL) desc = rv_strdup("");
118
	if(page == NULL) page = rv_strdup("");
119
	render_stuff();
120
	free(page);
121
	free(desc);
122
	free(title);
123
}
124
 
3 nishi 125
char* escape(const char* str) {
1 nishi 126
	char* r = malloc(1);
127
	r[0] = 0;
128
	char cbuf[2];
129
	cbuf[1] = 0;
130
	int i;
3 nishi 131
	for(i = 0; str[i] != 0; i++) {
132
		if(str[i] == '<') {
1 nishi 133
			char* tmp = r;
134
			r = rv_strcat(tmp, "&lt;");
135
			free(tmp);
3 nishi 136
		} else if(str[i] == '>') {
1 nishi 137
			char* tmp = r;
138
			r = rv_strcat(tmp, "&gt;");
139
			free(tmp);
3 nishi 140
		} else {
1 nishi 141
			cbuf[0] = str[i];
142
			char* tmp = r;
143
			r = rv_strcat(tmp, cbuf);
144
			free(tmp);
145
		}
146
	}
147
	return r;
148
}
149
 
3 nishi 150
void render_stuff(void) {
1 nishi 151
	char* escaped;
152
	add_data(&buffer, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
153
	add_data(&buffer, "<html>\n");
154
	add_data(&buffer, "	<head>\n");
155
	add_data(&buffer, "		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");
156
	add_data(&buffer, "		<title>");
157
	add_data(&buffer, INSTANCE_NAME);
158
	add_data(&buffer, " - ");
159
	add_data(&buffer, title);
160
	add_data(&buffer, "</title>\n");
161
	add_data(&buffer, "		<style type=\"text/css\">\n");
162
	add_data(&buffer, "* {\n");
163
	add_data(&buffer, "	padding: 0;\n");
164
	add_data(&buffer, "	margin: 0;\n");
165
	add_data(&buffer, "}\n");
166
	add_data(&buffer, "li {\n");
167
	add_data(&buffer, "	list-style: outside;\n");
168
	add_data(&buffer, "	margin-left: 1.25em;\n");
169
	add_data(&buffer, "}\n");
170
	add_data(&buffer, "a {\n");
171
	add_data(&buffer, "	text-decoration: none;\n");
172
	add_data(&buffer, "}\n");
173
	add_data(&buffer, "#nav div {\n");
174
	add_data(&buffer, "	float: left;\n");
175
	add_data(&buffer, "	margin: 0 0;\n");
176
	add_data(&buffer, "	padding-left: 0;\n");
177
	add_data(&buffer, "	padding-right: 20px;\n");
178
	add_data(&buffer, "	padding-top: 7px;\n");
179
	add_data(&buffer, "}\n");
180
	add_data(&buffer, "body {\n");
181
	add_data(&buffer, "	background-color: #1F4677;\n");
182
	add_data(&buffer, "	width: 940px;\n");
183
	add_data(&buffer, "	margin: 5px auto;\n");
184
	add_data(&buffer, "	font-family: sans-serif;\n");
185
	add_data(&buffer, "}\n");
186
	add_data(&buffer, "#nav {\n");
187
	add_data(&buffer, "	background-color: white;\n");
6 nishi 188
	add_data(&buffer, "	background-image: url('");
189
	add_data(&buffer, INSTANCE_NAVBAR);
190
	add_data(&buffer, "');\n");
1 nishi 191
	add_data(&buffer, "	height: 44px;\n");
192
	add_data(&buffer, "	padding: 8px;\n");
193
	add_data(&buffer, "	padding-left: 32px;\n");
194
	add_data(&buffer, "	font-size: 22px;\n");
195
	add_data(&buffer, "	font-weight: bold;\n");
196
	add_data(&buffer, "}\n");
197
	add_data(&buffer, "#index {\n");
198
	add_data(&buffer, "	list-style: none;\n");
199
	add_data(&buffer, "	line-height: normal;\n");
200
	add_data(&buffer, "	margin: auto 0;\n");
201
	add_data(&buffer, "	padding-left: 0;\n");
202
	add_data(&buffer, "}\n");
203
	add_data(&buffer, "#desc {\n");
204
	add_data(&buffer, "	background-color: #D2E1F6;\n");
205
	add_data(&buffer, "	margin: 9px auto;\n");
206
	add_data(&buffer, "	height: 128px;\n");
207
	add_data(&buffer, "	padding: 24px;\n");
208
	add_data(&buffer, "}\n");
209
	add_data(&buffer, "#descinside {\n");
210
	add_data(&buffer, "	float: left;\n");
211
	add_data(&buffer, "}\n");
212
	add_data(&buffer, "#logo {\n");
213
	add_data(&buffer, "	float: right;\n");
214
	add_data(&buffer, "}\n");
215
	add_data(&buffer, "#content {\n");
216
	add_data(&buffer, "	background-color: #FFFFFF;\n");
217
	add_data(&buffer, "	margin: -10px auto;\n");
218
	add_data(&buffer, "	padding: 8px 24px 24px;\n");
219
	add_data(&buffer, "	min-height: 128px;\n");
220
	add_data(&buffer, "}\n");
221
	add_data(&buffer, "#pageindex {\n");
222
	add_data(&buffer, "	background-color: #FFFFFF;\n");
223
	add_data(&buffer, "	padding-right: 24px;\n");
224
	add_data(&buffer, "	padding-bottom: 24px;\n");
225
	add_data(&buffer, "	float: left;\n");
226
	add_data(&buffer, "	border-right: 4px #1F4677 solid;\n");
227
	add_data(&buffer, "	width: 150px;\n");
228
	add_data(&buffer, "}\n");
229
	add_data(&buffer, "#pagecontent {\n");
230
	add_data(&buffer, "	background-color: #FFFFFF;\n");
231
	add_data(&buffer, "	width: 670px;\n");
232
	add_data(&buffer, "	float: right;\n");
233
	add_data(&buffer, "	padding-left: 24px;\n");
234
	add_data(&buffer, "}\n");
235
	add_data(&buffer, "#footer {\n");
236
	add_data(&buffer, "	background-color: #D2E1F6;\n");
237
	add_data(&buffer, "	padding: 8px 8px 48px;\n");
238
	add_data(&buffer, "	margin: 8px auto;\n");
239
	add_data(&buffer, "	font-size: 15px;\n");
240
	add_data(&buffer, "	height: 32px;\n");
241
	add_data(&buffer, "}\n");
242
	add_data(&buffer, ".fixfloat {\n");
243
	add_data(&buffer, "	clear: both;\n");
244
	add_data(&buffer, "}\n");
245
	add_data(&buffer, "#copyright {\n");
246
	add_data(&buffer, "	float: right;\n");
247
	add_data(&buffer, "	font-size: 10px;\n");
248
	add_data(&buffer, "	margin-top: 16px;\n");
249
	add_data(&buffer, "}\n");
250
	add_data(&buffer, "#gotop {\n");
251
	add_data(&buffer, "	position: absolute;\n");
252
	add_data(&buffer, "}\n");
253
	add_data(&buffer, "h2, h3 {\n");
254
	add_data(&buffer, "	padding-top: 8px;\n");
255
	add_data(&buffer, "	padding-bottom: 8px;\n");
256
	add_data(&buffer, "}\n");
257
	add_data(&buffer, "img {\n");
258
	add_data(&buffer, "	border: none;\n");
259
	add_data(&buffer, "}\n");
260
	add_data(&buffer, "		</style>\n");
261
	add_data(&buffer, "	</head>\n");
262
	add_data(&buffer, "	<body>\n");
263
	add_data(&buffer, "		<div id=\"nav\">\n");
264
	add_data(&buffer, "			<div>\n");
265
	add_data(&buffer, "				<a href=\"");
266
	add_data(&buffer, INSTANCE_ROOT);
267
	add_data(&buffer, "/\">Home</a>\n");
268
	add_data(&buffer, "			</div>\n");
5 nishi 269
	if(user == NULL) {
270
		add_data(&buffer, "			<div>\n");
271
		add_data(&buffer, "				<a href=\"");
272
		add_data(&buffer, INSTANCE_ROOT);
273
		add_data(&buffer, "/?page=login\">Login</a>\n");
274
		add_data(&buffer, "			</div>\n");
7 nishi 275
	} else {
276
		add_data(&buffer, "			<div>\n");
277
		add_data(&buffer, "				<a href=\"");
278
		add_data(&buffer, INSTANCE_ROOT);
279
		add_data(&buffer, "/?page=logout\">Logout</a>\n");
280
		add_data(&buffer, "			</div>\n");
5 nishi 281
	}
6 nishi 282
	if(user != NULL) {
283
		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=\"");
284
		add_data(&buffer, INSTANCE_ROOT);
285
		add_data(&buffer, "/?page=mypage\">");
286
		add_data(&buffer, user);
287
		add_data(&buffer, "</a></div>");
288
	}
1 nishi 289
	add_data(&buffer, "		</div>\n");
290
	add_data(&buffer, "		<div id=\"desc\">\n");
291
	add_data(&buffer, "			<div id=\"descinside\">\n");
292
	add_data(&buffer, "				<h1>");
293
	add_data(&buffer, title);
294
	add_data(&buffer, "</h1>\n");
295
	add_data(&buffer, "				<p>\n");
296
	add_data(&buffer, desc);
297
	add_data(&buffer, "				</p>\n");
298
	add_data(&buffer, "			</div>\n");
299
	add_data(&buffer, "			<img id=\"logo\" src=\"");
300
	add_data(&buffer, INSTANCE_LOGO);
301
	add_data(&buffer, "\" height=\"128px\" alt=\"logo\">\n");
302
	add_data(&buffer, "		</div>\n");
303
	add_data(&buffer, "		<div id=\"content\">\n");
304
	add_data(&buffer, "			<div id=\"pageindex\">\n");
305
	add_data(&buffer, "				<h3>Page Menu</h3>\n");
306
	add_data(&buffer, "				<ul>\n");
307
	add_data(&buffer, "				</ul>\n");
308
	add_data(&buffer, "			</div>\n");
309
	add_data(&buffer, "			<div id=\"pagecontent\">\n");
310
	add_data(&buffer, page);
311
	add_data(&buffer, "				<div class=\"fixfloat\"></div>\n");
312
	add_data(&buffer, "			</div>\n");
313
	add_data(&buffer, "		</div>\n");
314
	add_data(&buffer, "		<div id=\"footer\">\n");
315
	add_data(&buffer, "			<div id=\"gotop\">\n");
316
	add_data(&buffer, "				<a href=\"#top\">Top</a>\n");
317
	add_data(&buffer, "			</div>\n");
318
	add_data(&buffer, "			<div id=\"copyright\">\n");
319
	add_data(&buffer, "				");
320
	escaped = escape(INSTANCE_ADMIN);
321
	add_data(&buffer, escaped);
322
	free(escaped);
323
	add_data(&buffer, "\n");
324
	add_data(&buffer, "			</div>\n");
325
	add_data(&buffer, "			<div class=\"fixfloat\"></div>\n");
326
	add_data(&buffer, "		</div>\n");
327
	add_data(&buffer, "	</body>\n");
328
	add_data(&buffer, "</html>\n");
5 nishi 329
	if(user != NULL) free(user);
1 nishi 330
}