Subversion Repositories RepoView

Rev

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