-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseefriends.c
More file actions
executable file
·283 lines (218 loc) · 8.72 KB
/
seefriends.c
File metadata and controls
executable file
·283 lines (218 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Helper macro to convert two-character hex strings to character value
#define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)
// will capture information recieved from dashboard.py
char InputData[4096];
int main(void)
{
// find out who the current user is
char username[100] = "";
getAllParams();
getParam("username", username);
/* this username variable is for testing purposes
const char* testname = "brady";
*/
/* A bunch of printf's to print out html headers and css */
printf("Content-Type:text/html\r\n\r\n<html>\n<head>\n<title>See Friends</title>\n</head>\n");
printf("<style type=\"text/css\">\nhtml, body {\nmargin: 0px;\nheight: 100%;\nbackground-color: #111111;\ncolor: #dddddd;\n}\n");
printf("#header {\nposition: absolute;\ntop: 20px;\nleft: 5.6%;\nheight: 10%;\nwidth: 85%;\ncolor: #33ccff;\nfont-size: 40px;\n}\n");
printf("#main-container {\nposition: relative;\ntop: 100px;\nleft: 80px;\n};\n");
printf(".line {\ndisplay: block;\nmargin-top: 0.5em;\nmargin-bottom: 0.5em;\nmargin-left: auto;\nmargin-right: auto;\nborder-style: inset;\nborder-width: 1px;\n}\n");
printf("#topline {\nposition: relative;\nmargin-top: 0.1em;\nmargin-left: -4.6%;\nmargin-right: 8.5%;\n}\n");
printf(".profiles { display: none; }\n");
printf("</style>\n");
printf("<body>\n");
printf("<div id=\"header\">\n");
// pagetitle refuses to work :(
// printf("<div id=\"pagetitle\">\n");
printf("See a Friend");
// pagetitle div closer tag
// printf("</div>\n");
printf("<hr class=\"line\" id=\"topline\"></div>\n");
printf("</div>\n");
printf("<div id=\"main-container\">\n");
//strlen for website usage, ! operand for terminal usage
if(!username || strlen(username) == 0) {
// username not usable. get outta here!
printf("You're not logged in!\n");
printf("<p><form action=\"../../~ascott40/cgi-bin/login.html\">\n");
printf("<button name=\"goback\">Register/Login</button>");
printf("</form></p>");
printf("</div>\n</body>\n</html>\n");
// else case for when there is a usable string in variable "username"
} else {
printf("<p>Your username is %s. The following is a list of your friends (if any) - click a username to expand a friend's profile!</p>\n", username);
// read friends.txt, parse each line, and use the line of the current user to populate the webpage.
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("../friends.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
char ** parsearray = NULL;
char * p = strtok (line, " ");
int n_spaces = 0, i;
/* split line and append its tokens to 'parsearray' */
while (p) {
parsearray = realloc (parsearray, sizeof (char*) * ++n_spaces);
if (parsearray == NULL)
exit (-1); /* exit if memory allocation failed */
parsearray[n_spaces-1] = p;
p = strtok (NULL, " ");
}
/* use tokens (friends) if first token (username) matches current user */
if ((strcmp(parsearray[0], username)) == 0) {
for (i = 1; i < (n_spaces); ++i) {
char *fullname;
char *profession;
int lines_allocated = 128;
int max_line_len = 100;
/* Allocate lines of text to parse users.txt into a array of char arrays 'words' */
char **words = (char **)malloc(sizeof(char*)*lines_allocated);
if (words==NULL)
{
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *fp = fopen("../../../ascott40/public_html/users.txt", "r");
if (fp == NULL)
{
fprintf(stderr,"Error opening file.\n");
exit(2);
}
/* max index of words is words[l] */
int l;
for (l=0;1;l++)
{
int j;
/* Have we gone over our line allocation? */
if (l >= lines_allocated)
{
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated*2;
words = (char **)realloc(words,sizeof(char*)*new_size);
if (words==NULL)
{
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
words[l] = malloc(max_line_len);
if (words[l]==NULL)
{
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(words[l],max_line_len-1,fp)==NULL)
break;
/* Get rid of return or newline at end of line */
for (j=strlen(words[l])-1;j>=0 && (words[l][j]=='\n' || words[l][j]=='\r');j--)
;
words[l][j+1]='\0';
}
/* Close users.txt. 'words' now contains all user info */
fclose(fp);
int j;
/* gather user info for profile iteration */
for(j = 0; j < l; j++) {
if ((strcmp(parsearray[i], words[j])) == 0) {
fullname = words[j+2];
profession = words[j+3];
}
}
/* profile div's are hidden on load and are shown only when clicked */
printf("<p><a onclick=\"toggle_visibility('profile%d');\">%s</a></p>\n", i, parsearray[i]);
printf("\n<div class=\"profiles\" id=\"profile%d\">\n", i);
/* finally, populate full name and profession */
printf("<b>Full Name</b>: %s. <b>Profession</b>: %s.\n", fullname, profession);
printf("</div>\n");
/* we're done populating user profiles, so let's free up memory */
for (;l>=0;l--)
free(words[l]);
free(words);
}
}
/* free memory before parsing next line of friends.txt */
free (parsearray);
}
/* close friends.txt, we're done with it */
fclose(fp);
/* free line memory */
if (line) {
free(line);
}
/* A button to return to the correct dashboard */
printf("<p><form action=\"../../~jinsco/cgi-bin/dashboard.py\" method=\"post\">\n");
printf("<button name=\"username\" value=\"%s\">Go Back to Dashboard</button>", username);
printf("</form></p>\n");
/* A little log out helper */
printf("<p>P.S. You're currently logged in as \"%s\". If that's not you,\n", username);
printf("<a href=\"http://cs.mcgill.ca/~ascott40\">click here</a> to logout.</p>\n</div>\n");
/* A bunch of printf's to print out the script that toggles profile visibility upon username click */
printf("<script type=\"text/javascript\">\n");
printf("function toggle_visibility(id) {\n");
printf("var e = document.getElementById(id);\n");
printf("if(e.style.display == 'block')\n");
printf("e.style.display = 'none';\n");
printf("else\n");
printf("e.style.display = 'block';\n");
printf("}\n");
printf("</script>\n");
/* goodbye HTML. this was fun */
printf("</body>\n</html>\n");
}
/* it's a C program, what can I say? */
return(0);
}
/* parse all recieved information */
void getAllParams() {
// Determines if it is a POST or GET method
if( getenv( "REQUEST_METHOD" ) == 0 ) {
printf("No REQUEST_METHOD, must be running in DOS mode\n");
return;
} else if (strcmp( getenv("REQUEST_METHOD"), "POST") == 0) {
// If POST
char *endptr; // quite useless, but required
char *len1 = getenv("CONTENT_LENGTH");
int contentlength = strtol(len1, &endptr, 10);
fread(InputData , contentlength, 1, stdin);
} else {
// If GET
strcpy(InputData, getenv("QUERY_STRING"));
}
}
/* parse specific key into a variable name for later use */
void getParam(const char *Name, char *Value) {
char *pos1 = strstr(InputData, Name);
if (pos1) {
pos1 += strlen(Name);
if (*pos1 == '=') { // Make sure there is an '=' where we expect it
pos1++;
while (*pos1 && *pos1 != '&') { // '&' signifies end of this parameter and the beginning of another
if (*pos1 == '%') {
// Convert it to a single ASCII character and store
*Value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
pos1 += 3;
} else if( *pos1=='+' ) {
// If it's a '+', store a space
*Value++ = ' ';
pos1++;
} else {
*Value++ = *pos1++; // Otherwise, just store the character
}
}
*Value++ = '\0';
return;
}
}
strcpy(Value, "undefined"); // If parameter not found, then use default parameter. Great for debugging.
return;
}