-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataManipulation.pm
492 lines (408 loc) · 12.6 KB
/
DataManipulation.pm
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package DataManipulation;
use CGI qw/:all/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($users_dir $suspend_dir returnUserNameWithZid returnUserImgWithZid searchForNameAndPostHtml logoutHtml displayPostWithDir unmateTwoUser checkUsrsMateRelationship sendEmail sendMateRequest makeMateTwoUser createDefaultNPStxt sendNotificationWhenZidMentioned getEmailByZid mateSuggestion); # Symbols to be exported by default
our @EXPORT_OK = qw($users_dir $suspend_dir returnUserNameWithZid returnUserImgWithZid searchForNameAndPostHtml logoutHtml displayPostWithDir unmateTwoUser checkUsrsMateRelationship sendEmail sendMateRequest makeMateTwoUser createDefaultNPStxt sendNotificationWhenZidMentioned getEmailByZid mateSuggestion); # Symbols to be exported on request
our $VERSION = 1.00; # Version number
#use Image::Thumbnail;
$users_dir = "dataset-medium";
$suspend_dir = "suspend";
sub returnUserNameWithZid{
my $user_zid = $_[0];
open F,"<$users_dir/$user_zid/user.txt";
#print "$user_zid";
while(my $line = <F>){
#print "$line";
if($line =~ /full_name/){
chomp $line;
my $name = $line;
$name =~ s/\s*full_name\s*=\s*//;
return $name;
}
}
close F;
return undef;
}
##if jpg file exist and be thumbnailed, return file name , else return 0, stands for do not exist
sub returnUserImgWithZid{
my $user_zid = $_[0];
foreach my $file (glob "$users_dir/$user_zid/*"){
#i must match profile\.jpg instead of \.jpg, since there could be two images, profile.jpg and background.jpg
if($file =~ /profile\.jpg/){
#my $fileName = $1;
#my $t = new Image::Thumbnail;
#$t -> thumbnail(
# input => "$file",
# output => "$fileName_Thumbnail\.jpg",
# size => 128,
#) or die $t -> error;
#$t ->create or die "Could not create thumbnail.\n";
return $file;
}
}
#means do not exist the corresponding jpg file, you don't need to print out
return 0;
}
#return the html code for searching for name and post
#so I can add it to the webpage I wish to have search function
sub searchForNameAndPostHtml{
return "<strong>Search:</strong>", "\n",
"<form action=\"search.cgi\">","\n",
textfield(-name=>'SearchKey',-size=>100,-maxlength=>100),"\n",
"<INPUT type=\"submit\" value=\"Search Name\" name=\"SearchName\" method=\"POST\">","\n",
"<INPUT type=\"submit\" value=\"Search Post\" name=\"SearchPost\" method=\"POST\">","\n",
"</form>";
}
#return logout html code
sub logoutHtml{
return "<form action=\"matelook.cgi\">
<input type=\"submit\" name=\"logout\"value=\"logout\" />
</form>";
}
##display a post with given directory
##assuem dir = $user_dir/$zid/posts/0 , no / in the end.
##integrate comment/reply and delete post functions
##return content with hyper link embeded
sub displayPostWithDir{
my $dir = $_[0];
my $zid_For_Delete = $_[1];
my @tempArrayForAnchorTag;
my $outputString="";
###this used in javascript, element id
my $jsId1 = $dir;
my $jsId2 = $dir;
$jsId1 =~ s/.*?([0-9]+)$/$1/;
$jsId2 =~ s/.*?(z[0-9]{7}).*/$1/;
#print "\$jsId2 :$jsId2","\n";
$jsId2 =returnUserNameWithZid($jsId2);
$jsId2 =~ s/\W//g;
my $jsId = "$jsId1$jsId2";
#add post content into outputString
open F,'<',"$dir/post.txt";
while(my $line = <F>){
if($line =~ /message/){
chomp $line;
$line =~ s/\s*message\s*=\s*//;
$outputString.="<b>post message:</b><span>$line\n</span>"."<a href=\"#\" onclick=\"document.getElementById(\'hiddenForm$jsId\').style.display = 'block'; return false;\">Make a comment</a><br/>
<form action=\"makeCommentAndReply.cgi\" id=\"hiddenForm$jsId\" style=\"display: none;\">
<textarea cols=\"50\" rows=\"10\" name=\"content\"></textarea><br />
<input type=\"hidden\" name=\"currentDir\" value='\"$dir\"'/>
<input type=\"hidden\" name=\"makeComment\" value=\"1\" />
<input type=\"submit\" value=\"submit Comment\" />
</form>";
#last;
}
if($line =~ /from=(z[0-9]{7})/i){
if($zid_For_Delete eq $1){
push @tempArrayForAnchorTag,"<span><a href=\"deletePost.cgi?directory=$dir\">Delete</a></span><br/>";
}
}
}
if(@tempArrayForAnchorTag){
$outputString.= shift @tempArrayForAnchorTag;
}
close F;
#check comments directory exists
if(-d "$dir/comments"){
foreach my $underComments (glob "$dir/comments/*"){
open F,'<',"$underComments/comment.txt";
##this used in javascript, element id, plus comment id to distinguish from each other
my $jsCommentId = $underComments;
$jsCommentId =~ s/.*?([0-9]+)$/$1/;
#print "\$jsCommentId:$jsCommentId","\n";
while(my $line = <F>){
if($line =~ /message/){
chomp $line;
$line =~ s/\s*message\s*=\s*//;
$outputString.="<b> comment message:</b><span>$line\n</span>"."<a href=\"#\" onclick=\"document.getElementById(\'hiddenFormComment$jsId$jsCommentId\').style.display = 'block'; return false;\">Make a Reply</a><br/>
<form action=\"makeCommentAndReply.cgi\" id=\"hiddenFormComment$jsId$jsCommentId\" style=\"display: none;\">
<textarea cols=\"50\" rows=\"10\" name=\"content\"></textarea><br />
<input type=\"hidden\" name=\"currentDir\" value='\"$underComments\"'/>
<input type=\"hidden\" name=\"makeReply\" value=\"1\" />
<input type=\"submit\" value=\"submit Reply\" />
</form>";
#last;
}
if($line =~ /from=(z[0-9]{7})/i){
if($zid_For_Delete eq $1){
push @tempArrayForAnchorTag,"<span><a href=\"deletePost.cgi?directory=$underComments\">Delete</a></span><br/>";
}
}
}
if(@tempArrayForAnchorTag){
$outputString.= shift @tempArrayForAnchorTag;
}
close F;
if(-d "$underComments/replies"){
foreach my $underReplies (glob "$underComments/replies/*"){
open F,'<',"$underReplies/reply.txt";
while(my $line = <F>){
if($line =~ /message/){
chomp $line;
$line =~ s/\s*message\s*=\s*//;
$outputString.="<b >reply message:</b><span>$line\n</span><br/>";
#last;
}
if($line =~ /from=(z[0-9]{7})/i){
if($zid_For_Delete eq $1){
#$outputString.="<a href=\"deletePost.cgi?directory=$underReplies\"> Delete </a>";
push @tempArrayForAnchorTag,"<a href=\"deletePost.cgi?directory=$underReplies\">Delete</a><br/>";
}
}
}
if(@tempArrayForAnchorTag){
$outputString.= shift @tempArrayForAnchorTag;
}
close F;
}
}
}
}
return $outputString;
}
sub makeMateTwoUser{
my ($user1,$user2)=@_;
open F,'<',"$users_dir/$user1/user.txt";
my @lines = <F>;
close F;
open F,'>',"$users_dir/$user1/user.txt";
foreach my $line (@lines){
if($line =~ /^\s*mates\s*=/){
chomp $line;
$line =~ s/\]//;
$line .= ",$user2]\n";
print F $line;
}else{
print F $line;
}
}
##empty the @lines array
@lines=();
open F,'<',"$users_dir/$user2/user.txt";
@lines = <F>;
close F;
open F,'>',"$users_dir/$user2/user.txt";
foreach my $line (@lines){
if($line =~ /^\s*mates\s*=/){
chomp $line;
$line =~ s/\]//;
$line .= ",$user1]\n";
print F $line;
}else{
print F $line;
}
}
return 1;
}
sub unmateTwoUser{
my ($user1,$user2)=@_;
open F,'<',"$users_dir/$user1/user.txt";
my @lines = <F>;
close F;
open F,'>',"$users_dir/$user1/user.txt";
foreach my $line (@lines){
if($line =~ /^\s*mates\s*=/){
#if there is a preceding comma(,), should remove the comma as well
$line =~ s/(,\Q$user2\E)|(\Q$user2\E)//;
print F $line;
}else{
print F $line;
}
}
##empty the @lines array
@lines=();
open F,'<',"$users_dir/$user2/user.txt";
@lines = <F>;
close F;
open F,'>',"$users_dir/$user2/user.txt";
foreach my $line (@lines){
if($line =~ /^\s*mates\s*=/){
$line =~ s/(,\Q$user1\E)|(\Q$user1\E)//;
print F $line;
}else{
print F $line;
}
}
return 1;
}
sub checkUsrsMateRelationship{
my ($user1, $user2)=@_;
open F,'<',"$users_dir/$user1/user.txt";
while(my $line = <F>){
if($line =~ /^\s*mates\s*=/ && $line =~ /\Q$user2\E/){
return 1;
}
}
return 0;
}
sub sendEmail{
my ($to,$from,$subject,$message)=@_;
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Content-type: text/html; charset=UTF-8\n\n";
print MAIL "
";
# Email Body
print MAIL "\n".$message;
close(MAIL);
}
sub sendMateRequest{
#$to and $from are zid.
my ($to,$from,$url) = @_;
$url =~ s/^(.*)\/.*$/$1/;
#http://www.cse.unsw.edu.au/~z5089812/ass2/matelook.cgi
my $sendMessageHtml .= "<html><title><Mate Request from $from</title><body>";
$sendMessageHtml .= "<p>Mate Request from $from</p>";
$sendMessageHtml .= "<a href=\"$url/matelook.cgi?acceptMateRequest=1&to=$to&from=$from\">accept</a>";
$sendMessageHtml .= "<a href=\"$url/matelook.cgi?declineMateRequest=1&to=$to&from=$from\">decline</a>";
$sendMessageHtml .= "</body></html>";
my $toEmail="";
open F,'<',"$users_dir/$to/user.txt";
while(my $line = <F>){
if($line =~ /^\s*email\s*=(.*)\n*/){
$toEmail = $1;
last;
}
}
close F;
sendEmail($toEmail,'z5089812@ad.unsw.edu.au',"Mate Request from $from",$sendMessageHtml);
open 'F','>',"$users_dir/$zid_cookie_value/notificationAndPrivacySetting.txt";
while(my $line = <F>){
if($line =~ /notified when get a mate request=(\d)/){
if($1){
sendEmail($toEmail,'z5089812@ad.unsw.edu.au',"Mate Request from $from","It's a nofication, you can choose to turn it off in nofication and privacy toggle");
}else{
next;
}
last;
}
}
close F;
}
##create default notificationAndPrivacy.txt
sub createDefaultNPStxt{
my $zid=$_[0];
open F,'>',"$users_dir/$zid/notificationAndPrivacySetting.txt";
## 1 means get notifcation, 0 means no
## default is get all the notications
print F "notified when zid is mentioned=1\n";
print F "notified when get a mate request=1\n";
## 1 means open to public, 0 means open only to mates
## default set to 1
## doesn't cover all the field, only fields bellow.
print F "profile=1\n";
print F "home_suburb=1\n";
print F "courses=1\n";
print F "program=1\n";
print F "birthday=1\n";
print F "email=1\n";
close F;
}
sub getEmailByZid{
my $zid = $_[0];
my $toEmail="";
open F,'<',"$users_dir/$zid/user.txt";
while(my $line = <F>){
if($line =~ /^\s*email\s*=(.*)\n*/){
$toEmail = $1;
return $toEmail;
last;
}
}
close F;
return undef;
}
sub sendNotificationWhenZidMentioned{
my $zid = $_[0];
my $dir = $_[1];
##does not exist then create a default one
if(! -e "$users_dir/$zid/notificationAndPrivacySetting.txt"){
createDefaultNPStxt($zid);
}
open F,'<',"$users_dir/$zid/notificationAndPrivacySetting.txt";
while(my $line = <F>){
if($line=~ /notified when zid is mentioned=(\d)/){
if($1){
sendEmail(getEmailByZid($zid),'z5089812@ad.unsw.edu.au',"your zid is mentiond","you are mentioned in $dir");
}
}
}
close F;
}
sub mateSuggestion{
my $zid = $_[0];
my %mateSuggestionMark=();
##find his mates' mates
##give higher weights to friend's friend when doing suggestion
##increase by 3 each time
open F,"<$users_dir/$zid/user.txt";
my @matesZid;
while(my $line = <F>){
if($line =~ /^\s*mates/){
@matesZid = ($line =~ /z[0-9]{7}/g);
foreach my $mateZid (@matesZid){
open F2,"<","$users_dir/$mateZid/user.txt";
while(my $tempLine = <F2>){
if($tempLine =~ /^\s*mates/){
@matesmatesZid = ($tempLine =~ /z[0-9]{7}/g);
foreach my $matemateZid (@matesmatesZid){
my @tempCheckZidIn = grep {$_ =~ /$matemateZid/} @matesZid;
if(! @tempCheckZidIn && $matemateZid ne $zid){
##they not friend and not himself, it could be someone may be suggested
if($mateSuggestionMark{$matemateZid}){
$mateSuggestionMark{$matemateZid}+=3;
}else{
$mateSuggestionMark{$matemateZid}=3;
}
}
}
last;
}
}
close F2;
}
last;
}
}
close F;
##check people in %mateSuggestionMark, the courses they have.
open F,"<$users_dir/$zid/user.txt";
my %courses;
while(my $line = <F>){
if($line =~ /^\s*courses/){
my @coursesTaken = ($line =~ /[0-9]{4} \w\d [\w]{4}[\d]{4}/g);
foreach my $course (@coursesTaken){
$course = lc $course;
$courses{$course}=1;
}
last;
}
}
close F;
for my $mate (keys %mateSuggestionMark){
open F,"<$users_dir/$mate/user.txt";
while(my $line = <F>){
if($line =~ /^\s*courses/){
my @coursesTaken = ($line =~ /[0-9]{4} \w\d [\w]{4}[\d]{4}/g);
foreach my $course (@coursesTaken){
$course = lc $course;
if($courses{$course}){
$mateSuggestionMark{$mate}+=2;
}
}
last;
}
}
close F;
}
my @outputZid;
foreach my $zid (sort { $mateSuggestionMark{$a} <=> $mateSuggestionMark{$b} } keys %mateSuggestionMark) {
push @outputZid,$zid;
}
return \@outputZid;
}