Subversion Repositories RepoView

Rev

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