Subversion Repositories RepoView

Rev

Rev 7 | Rev 10 | 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 9 2024-08-21 02:02:13Z 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
		}
9 nishi 98
	} else if(strcmp(query, "myrepo") == 0) {
99
		title = rv_strdup("My Repositories");
100
		desc = rv_strdup("You manage your repositories 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
		}
7 nishi 106
	} else if(strcmp(query, "logout") == 0) {
107
		title = rv_strdup("Logout");
108
		desc = rv_strdup("You can log out from your account here.");
109
		if(user == NULL) {
110
			page = rv_strdup("It looks like you are not logged in.<br>Want to <a href=\"");
111
			add_data(&page, INSTANCE_ROOT);
112
			add_data(&page, "/?page=login\">log in</a>?\n");
113
		} else {
114
			page = rv_strdup("");
115
			add_data(&page, "Are you sure you want to log out?\n");
116
			add_data(&page, "<form method=\"POST\" action=\"");
117
			add_data(&page, INSTANCE_ROOT);
118
			add_data(&page, "/?page=sendlogout\">\n");
119
			add_data(&page, "	<input type=\"submit\" value=\"Yes\">\n");
120
			add_data(&page, "</form>\n");
121
		}
1 nishi 122
	}
123
 
124
	if(title == NULL) title = rv_strdup("");
125
	if(desc == NULL) desc = rv_strdup("");
126
	if(page == NULL) page = rv_strdup("");
127
	render_stuff();
128
	free(page);
129
	free(desc);
130
	free(title);
131
}
132
 
3 nishi 133
char* escape(const char* str) {
1 nishi 134
	char* r = malloc(1);
135
	r[0] = 0;
136
	char cbuf[2];
137
	cbuf[1] = 0;
138
	int i;
3 nishi 139
	for(i = 0; str[i] != 0; i++) {
140
		if(str[i] == '<') {
1 nishi 141
			char* tmp = r;
142
			r = rv_strcat(tmp, "&lt;");
143
			free(tmp);
3 nishi 144
		} else if(str[i] == '>') {
1 nishi 145
			char* tmp = r;
146
			r = rv_strcat(tmp, "&gt;");
147
			free(tmp);
3 nishi 148
		} else {
1 nishi 149
			cbuf[0] = str[i];
150
			char* tmp = r;
151
			r = rv_strcat(tmp, cbuf);
152
			free(tmp);
153
		}
154
	}
155
	return r;
156
}
157
 
3 nishi 158
void render_stuff(void) {
1 nishi 159
	char* escaped;
160
	add_data(&buffer, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
161
	add_data(&buffer, "<html>\n");
162
	add_data(&buffer, "	<head>\n");
163
	add_data(&buffer, "		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");
164
	add_data(&buffer, "		<title>");
165
	add_data(&buffer, INSTANCE_NAME);
166
	add_data(&buffer, " - ");
167
	add_data(&buffer, title);
168
	add_data(&buffer, "</title>\n");
169
	add_data(&buffer, "		<style type=\"text/css\">\n");
170
	add_data(&buffer, "* {\n");
171
	add_data(&buffer, "	padding: 0;\n");
172
	add_data(&buffer, "	margin: 0;\n");
173
	add_data(&buffer, "}\n");
174
	add_data(&buffer, "li {\n");
175
	add_data(&buffer, "	list-style: outside;\n");
176
	add_data(&buffer, "	margin-left: 1.25em;\n");
177
	add_data(&buffer, "}\n");
178
	add_data(&buffer, "a {\n");
179
	add_data(&buffer, "	text-decoration: none;\n");
180
	add_data(&buffer, "}\n");
181
	add_data(&buffer, "#nav div {\n");
182
	add_data(&buffer, "	float: left;\n");
183
	add_data(&buffer, "	margin: 0 0;\n");
184
	add_data(&buffer, "	padding-left: 0;\n");
9 nishi 185
	add_data(&buffer, "	padding-right: 25px;\n");
1 nishi 186
	add_data(&buffer, "	padding-top: 7px;\n");
187
	add_data(&buffer, "}\n");
188
	add_data(&buffer, "body {\n");
189
	add_data(&buffer, "	background-color: #1F4677;\n");
190
	add_data(&buffer, "	width: 940px;\n");
191
	add_data(&buffer, "	margin: 5px auto;\n");
192
	add_data(&buffer, "	font-family: sans-serif;\n");
193
	add_data(&buffer, "}\n");
194
	add_data(&buffer, "#nav {\n");
195
	add_data(&buffer, "	background-color: white;\n");
6 nishi 196
	add_data(&buffer, "	background-image: url('");
197
	add_data(&buffer, INSTANCE_NAVBAR);
198
	add_data(&buffer, "');\n");
1 nishi 199
	add_data(&buffer, "	height: 44px;\n");
200
	add_data(&buffer, "	padding: 8px;\n");
201
	add_data(&buffer, "	padding-left: 32px;\n");
202
	add_data(&buffer, "	font-size: 22px;\n");
203
	add_data(&buffer, "	font-weight: bold;\n");
204
	add_data(&buffer, "}\n");
205
	add_data(&buffer, "#index {\n");
206
	add_data(&buffer, "	list-style: none;\n");
207
	add_data(&buffer, "	line-height: normal;\n");
208
	add_data(&buffer, "	margin: auto 0;\n");
209
	add_data(&buffer, "	padding-left: 0;\n");
210
	add_data(&buffer, "}\n");
211
	add_data(&buffer, "#desc {\n");
212
	add_data(&buffer, "	background-color: #D2E1F6;\n");
213
	add_data(&buffer, "	margin: 9px auto;\n");
214
	add_data(&buffer, "	height: 128px;\n");
215
	add_data(&buffer, "	padding: 24px;\n");
216
	add_data(&buffer, "}\n");
217
	add_data(&buffer, "#descinside {\n");
218
	add_data(&buffer, "	float: left;\n");
219
	add_data(&buffer, "}\n");
220
	add_data(&buffer, "#logo {\n");
221
	add_data(&buffer, "	float: right;\n");
222
	add_data(&buffer, "}\n");
223
	add_data(&buffer, "#content {\n");
224
	add_data(&buffer, "	background-color: #FFFFFF;\n");
225
	add_data(&buffer, "	margin: -10px auto;\n");
226
	add_data(&buffer, "	padding: 8px 24px 24px;\n");
227
	add_data(&buffer, "	min-height: 128px;\n");
228
	add_data(&buffer, "}\n");
229
	add_data(&buffer, "#pageindex {\n");
230
	add_data(&buffer, "	background-color: #FFFFFF;\n");
231
	add_data(&buffer, "	padding-right: 24px;\n");
232
	add_data(&buffer, "	padding-bottom: 24px;\n");
233
	add_data(&buffer, "	float: left;\n");
234
	add_data(&buffer, "	border-right: 4px #1F4677 solid;\n");
235
	add_data(&buffer, "	width: 150px;\n");
236
	add_data(&buffer, "}\n");
237
	add_data(&buffer, "#pagecontent {\n");
238
	add_data(&buffer, "	background-color: #FFFFFF;\n");
239
	add_data(&buffer, "	width: 670px;\n");
240
	add_data(&buffer, "	float: right;\n");
241
	add_data(&buffer, "	padding-left: 24px;\n");
242
	add_data(&buffer, "}\n");
243
	add_data(&buffer, "#footer {\n");
244
	add_data(&buffer, "	background-color: #D2E1F6;\n");
245
	add_data(&buffer, "	padding: 8px 8px 48px;\n");
246
	add_data(&buffer, "	margin: 8px auto;\n");
247
	add_data(&buffer, "	font-size: 15px;\n");
248
	add_data(&buffer, "	height: 32px;\n");
249
	add_data(&buffer, "}\n");
250
	add_data(&buffer, ".fixfloat {\n");
251
	add_data(&buffer, "	clear: both;\n");
252
	add_data(&buffer, "}\n");
253
	add_data(&buffer, "#copyright {\n");
254
	add_data(&buffer, "	float: right;\n");
255
	add_data(&buffer, "	font-size: 10px;\n");
256
	add_data(&buffer, "	margin-top: 16px;\n");
257
	add_data(&buffer, "}\n");
258
	add_data(&buffer, "#gotop {\n");
259
	add_data(&buffer, "	position: absolute;\n");
260
	add_data(&buffer, "}\n");
261
	add_data(&buffer, "h2, h3 {\n");
262
	add_data(&buffer, "	padding-top: 8px;\n");
263
	add_data(&buffer, "	padding-bottom: 8px;\n");
264
	add_data(&buffer, "}\n");
265
	add_data(&buffer, "img {\n");
266
	add_data(&buffer, "	border: none;\n");
267
	add_data(&buffer, "}\n");
268
	add_data(&buffer, "		</style>\n");
269
	add_data(&buffer, "	</head>\n");
270
	add_data(&buffer, "	<body>\n");
271
	add_data(&buffer, "		<div id=\"nav\">\n");
272
	add_data(&buffer, "			<div>\n");
273
	add_data(&buffer, "				<a href=\"");
274
	add_data(&buffer, INSTANCE_ROOT);
275
	add_data(&buffer, "/\">Home</a>\n");
276
	add_data(&buffer, "			</div>\n");
5 nishi 277
	if(user == NULL) {
278
		add_data(&buffer, "			<div>\n");
279
		add_data(&buffer, "				<a href=\"");
280
		add_data(&buffer, INSTANCE_ROOT);
281
		add_data(&buffer, "/?page=login\">Login</a>\n");
282
		add_data(&buffer, "			</div>\n");
7 nishi 283
	} else {
284
		add_data(&buffer, "			<div>\n");
285
		add_data(&buffer, "				<a href=\"");
286
		add_data(&buffer, INSTANCE_ROOT);
9 nishi 287
		add_data(&buffer, "/?page=mypage\">My Page</a>\n");
288
		add_data(&buffer, "			</div>\n");
289
 
290
		add_data(&buffer, "			<div>\n");
291
		add_data(&buffer, "				<a href=\"");
292
		add_data(&buffer, INSTANCE_ROOT);
293
		add_data(&buffer, "/?page=myrepo\">My Repositories</a>\n");
294
		add_data(&buffer, "			</div>\n");
295
 
296
		add_data(&buffer, "			<div>\n");
297
		add_data(&buffer, "				<a href=\"");
298
		add_data(&buffer, INSTANCE_ROOT);
7 nishi 299
		add_data(&buffer, "/?page=logout\">Logout</a>\n");
300
		add_data(&buffer, "			</div>\n");
5 nishi 301
	}
6 nishi 302
	if(user != NULL) {
303
		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=\"");
304
		add_data(&buffer, INSTANCE_ROOT);
305
		add_data(&buffer, "/?page=mypage\">");
306
		add_data(&buffer, user);
307
		add_data(&buffer, "</a></div>");
308
	}
1 nishi 309
	add_data(&buffer, "		</div>\n");
310
	add_data(&buffer, "		<div id=\"desc\">\n");
311
	add_data(&buffer, "			<div id=\"descinside\">\n");
312
	add_data(&buffer, "				<h1>");
313
	add_data(&buffer, title);
314
	add_data(&buffer, "</h1>\n");
315
	add_data(&buffer, "				<p>\n");
316
	add_data(&buffer, desc);
317
	add_data(&buffer, "				</p>\n");
318
	add_data(&buffer, "			</div>\n");
319
	add_data(&buffer, "			<img id=\"logo\" src=\"");
320
	add_data(&buffer, INSTANCE_LOGO);
321
	add_data(&buffer, "\" height=\"128px\" alt=\"logo\">\n");
322
	add_data(&buffer, "		</div>\n");
323
	add_data(&buffer, "		<div id=\"content\">\n");
324
	add_data(&buffer, "			<div id=\"pageindex\">\n");
325
	add_data(&buffer, "				<h3>Page Menu</h3>\n");
326
	add_data(&buffer, "				<ul>\n");
327
	add_data(&buffer, "				</ul>\n");
328
	add_data(&buffer, "			</div>\n");
329
	add_data(&buffer, "			<div id=\"pagecontent\">\n");
330
	add_data(&buffer, page);
331
	add_data(&buffer, "				<div class=\"fixfloat\"></div>\n");
332
	add_data(&buffer, "			</div>\n");
333
	add_data(&buffer, "		</div>\n");
334
	add_data(&buffer, "		<div id=\"footer\">\n");
335
	add_data(&buffer, "			<div id=\"gotop\">\n");
336
	add_data(&buffer, "				<a href=\"#top\">Top</a>\n");
337
	add_data(&buffer, "			</div>\n");
338
	add_data(&buffer, "			<div id=\"copyright\">\n");
339
	add_data(&buffer, "				");
340
	escaped = escape(INSTANCE_ADMIN);
341
	add_data(&buffer, escaped);
342
	free(escaped);
343
	add_data(&buffer, "\n");
344
	add_data(&buffer, "			</div>\n");
345
	add_data(&buffer, "			<div class=\"fixfloat\"></div>\n");
346
	add_data(&buffer, "		</div>\n");
347
	add_data(&buffer, "	</body>\n");
348
	add_data(&buffer, "</html>\n");
5 nishi 349
	if(user != NULL) free(user);
1 nishi 350
}