-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditUserProfile.cgi
More file actions
executable file
·200 lines (173 loc) · 5.63 KB
/
Copy patheditUserProfile.cgi
File metadata and controls
executable file
·200 lines (173 loc) · 5.63 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
#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use DataManipulation;
##user zid, which is stored in cookie
$zid_cookie_value = cookie("zid");
##should not allow user to change zid,full_name,email,password here
##update values if it's input
##use ne instead of !=
if(defined param('birthday') && param("birthday") ne ''){
my $userTXTPath = "$users_dir/$zid_cookie_value/user.txt";
open F,'<',$userTXTPath;
my @lines=<F>;
close F;
open F,'>',$userTXTPath;
#write this into file, no matter if it is exist in the orignal file
print F "birthday=".param('birthday')."\n";
foreach my $line (@lines){
if($line =~ /^\s*birthday/){
next;
}else{
print F $line;
}
}
close F;
}
if(defined param('home_suburb') && param('home_suburb') ne ''){
my $userTXTPath = "$users_dir/$zid_cookie_value/user.txt";
open F,'<',$userTXTPath;
my @lines=<F>;
close F;
open F,'>',$userTXTPath or die "can't open $userTXTPath";
print F "home_suburb=".param('home_suburb')."\n";
foreach my $line (@lines){
if($line =~ /^\s*home_suburb/){
next;
}else{
print F $line;
}
}
close F;
}
if(defined param('courses') && param("courses") ne ''){
my $userTXTPath = "$users_dir/$zid_cookie_value/user.txt";
open F,'<',$userTXTPath;
my @lines=<F>;
close F;
open F,'>',$userTXTPath;
print F "courses=[".param('courses')."]"."\n";
foreach my $line (@lines){
if($line =~ /^\s*courses/){
next;
}else{
print F $line;
}
}
close F;
}
if(defined param('profile') && param("profile") ne ''){
my $userTXTPath = "$users_dir/$zid_cookie_value/user.txt";
open F,'<',$userTXTPath;
my @lines=<F>;
close F;
open F,'>',$userTXTPath;
print F "profile=".param('profile')."\n";
foreach my $line (@lines){
if($line =~ /^\s*profile/){
next;
}else{
print F $line;
}
}
close F;
}
if(defined param('program') && param("program") ne ''){
my $userTXTPath = "$users_dir/$zid_cookie_value/user.txt";
open F,'<',$userTXTPath;
my @lines=<F>;
close F;
open F,'>',$userTXTPath;
print F "program=".param('program')."\n";
foreach my $line (@lines){
if($line =~ /^\s*program/){
next;
}else{
print F $line;
}
}
close F;
}
##update image
##grab this snippet from http://www.grm.cuhk.edu.hk/~htlee/perlcourse/fileupload/fileupload2.html
$upload_filehandle = upload("image");
if($upload_filehandle){
open UPLOADFILE, ">$users_dir/$zid_cookie_value/profile.jpg";
binmode UPLOADFILE;
while ( <$upload_filehandle> ) { print UPLOADFILE; }
close UPLOADFILE;
}
##update BACKGROUND image
##grab this snippet from http://www.grm.cuhk.edu.hk/~htlee/perlcourse/fileupload/fileupload2.html
$upload_filehandle = upload("backGroundImage");
if($upload_filehandle){
open UPLOADFILE, ">$users_dir/$zid_cookie_value/background.jpg";
binmode UPLOADFILE;
while ( <$upload_filehandle> ) { print UPLOADFILE; }
close UPLOADFILE;
}
##remove profile images
if(defined param('dpi')){
if(-e "$users_dir/$zid_cookie_value/profile.jpg"){
unlink "$users_dir/$zid_cookie_value/profile.jpg";
}
}
##remove background images
if(defined param('dbi')){
#print "dbi\n";'
if(-e "$users_dir/$zid_cookie_value/background.jpg"){
unlink "$users_dir/$zid_cookie_value/background.jpg";
}
}
##print out user current information and give out the fill-in form, user could change his/her info by filling the form
print header(-charset => "utf-8"), start_html('edit user profile');
print h2("edit your profile");
### go back to user home page
### a strange technique, the submit button's name is showUserPage, which in matelook.cgi will triger to show the user's profile
print "<form action=\"matelook.cgi?showUserPage=1\">",
"<input type='submit' action = \"matelook.cgi?showUserPage=1\" value=\"Go Back To My Page\" name=\"showUserPage\">",
"</form>";
print "<br/>";
###display user infomation
print "<b>Current User Profile:</b>";
open F,"<$users_dir/$zid_cookie_value/user.txt";
while(my $line = <F>){
if($line =~ /^\s*mates/){
next;
}
print "<p>$line</p>","\n";
}
close F;
$tnjpgfile = returnUserImgWithZid($zid_cookie_value );
if($tnjpgfile){
print "<span><img src=\"$tnjpgfile\" alt=\"$zid_cookie_value image\" width=\"200\" height=\"200\"></span>","\n";
}
print h2("edit user profile");
print "<div style=\"background:#F9EECF;border:10px splash black;text-align:left;width:100%\">";
### need to specify the action, or else it would be /~z5089812/ass2/editUserProfile.cgi which may lost in some circumstance
print p("if you do not want to change a field, do not input anything in that field");
print start_form(-action=>"editUserProfile.cgi",-enctype=>'multipart/form-data');
print "birthday:",textfield(-name=>'birthday'),"\n";
print "<br/>";
print "home_suburb:",textfield(-name=>'home_suburb'),"\n";
print "<br/>";
print "courses",textfield(-name=>'courses'),"(please follow the format:year sem coursecode, and delimit with comma(,))","\n";
print "<br/>";
print "Introduce self/Profile text",textarea(-name=>'profile'),"\n";
print "<br/>";
print "program:",textfield(-name=>'program'),"\n";
print "<br/>";
print "Enter a image to upload: <input type=\"file\" name=\"image\">";
print "<br/>";
##only editUserProfile.cgi has this function, createUserAccount do not include upload background image
print "upload a background image : <input type=\"file\" name=\"backGroundImage\">";
print "<br/>";
##check box , to delete the profile image and background image
print "delete profile image?",checkbox(-name => 'dpi',-checked =>'0',-label => 'delete profile image'),"\n";
print "<br/>";
print "delete background image?",checkbox(-name => 'dbi',-checked =>'0',-label => 'delete background image'),"\n";
print "<br/>";
print submit;
print end_form;
print "</div>";
print end_html;