forked from shammamah-zz/PluggedIn
/
seefriends.c
167 lines (125 loc) · 4.1 KB
/
seefriends.c
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
#include <stdio.h>
#include <stdlib.h>
char *entries[1];//the username
char *friends[5000];//the friends
int friendNumber=0;//number of friends
int i=0;
//takes input an already parsed section, eg username=bob
int parser( char *s )
/* Nested strtoks are a problem so this is our own parser*/
{
char *vname;
char *val;
//this part gets rid of the CGI vatiabble name tag
if(s == '\0') {
printf("X_ERR='Null phrase'\n");
i++;
return 0;
}
vname = s;//i we ll need the vname later on we can get it form here
while( (*s != '\0') && (*s != '=')) s++;
if(*s == '\0' )
{
printf("X_ERR='Null assignment'\n");
i++;
return 0;
}
*(s++) = '\0';
//now the pointer points to the beggining of the username/password
val =s;
while( (*s != '\0') && (*s != '=')) s++;
*(s++) = '\0';
entries[i]=val;
i++;
return 1;
}
int main()
{
char *query_string=getenv("QUERY_STRING");
char buffer[ strlen(query_string) +1];
strcpy(buffer, query_string);
char* token;
token = strtok(buffer,"&");
parser(token);
// now entries[0] is the username
FILE *validate=fopen("users.txt", "rt");
char field[5000];//each line in users.txt
fgets( field, 5000, validate);
field[strlen(field) - 1] = '\0'; //removes newline
char *usrname;
usrname=field;
strtok(usrname, " ");
while(!feof(validate) )
{
if( strcmp(entries[0], field) == 0) //if user name is found
{
char *friend;
friend = strtok(NULL," "); //get friend
while( friend != NULL)
{
*(friends+friendNumber)=friend;
friendNumber++;
friend = strtok(NULL," ");
}
*(friends+friendNumber)='\0';
break;
}
//skip 4 lines to next user name line
int c1=0;
for(c1; c1<4; c1++)
{
fgets( field, 5000, validate);
field[strlen(field) - 1] = '\0';
usrname=field;
strtok(usrname, " ");
}
}
//proceed to printing the html page
//static html top
FILE *htmlTop=fopen("seefriendsTop.html", "rt");
printf("%s%c%c\n","Content-Type:text/html",13,10);
int c;
c=fgetc(htmlTop);
while(!feof(htmlTop) )
{
fputc(c, stdout);
c=fgetc(htmlTop);
}
fclose(htmlTop);
//submit button cgi
printf("<form name=\"seeuser\" action=\"http://www.cs.mcgill.ca/~ycukra/cgi-bin/friendpage.py\" method=\"get\">");
printf("<input type=\"hidden\" name = \"username\" value=\"%s\">", entries[0]);
//the dynamic part of the html that will be changed according to friends
int index=0;
for(index; index<friendNumber; index++)
{
printf("<div class=\"check\"><input type=\"radio\" name=\"friend\" value=\"%s\"></div>", friends[index] );
printf("<div class=\"username\"><h2>%s</h2></div>", friends[index]);
}
//print static html bottom1
FILE *htmlBot1= fopen("seefriendsBottom1.html", "rt");
c=fgetc(htmlBot1);
while(!feof(htmlBot1) )
{
fputc(c, stdout);
c=fgetc(htmlBot1);
}
fclose(htmlBot1);
//print topmenu javascript and hiddenfield connections to other pages
printf("<form name=\"makefriend\" action=\"http://www.cs.mcgill.ca/~shossa15/cgi-bin/makefriends.py\" method=\"get\">");
printf("<input type=\"hidden\" name = \"username\" value=\"%s\"> </form>" , entries[0]);
printf("<form name=\"seefriend\" action=\"\" method=\"get\">");
printf("<input type=\"hidden\" name = \"username\" value=\"%s\"></form>", entries[0]);
printf("<form name=\"statusupdate\" action=\"http://www.cs.mcgill.ca/~ycukra/cgi-bin/status.py\" method=\"get\">");
printf("<input type=\"hidden\" name = \"update\" value=\"\"><input type=\"hidden\" name = \"username\" value=\"%s\"></form>", entries[0]);
//print static html bottom2
FILE *htmlBot2=fopen("seefriendsBottom2.html", "rt");
c=fgetc(htmlBot2);
while(!feof(htmlBot2) )
{
fputc(c, stdout);
c=fgetc(htmlBot2);
}
fclose(htmlBot2);
return 0;
}