-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
847 lines (670 loc) · 18.4 KB
/
functions.php
File metadata and controls
847 lines (670 loc) · 18.4 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
<?php
// functions file
include("resources/db.php");
include("facebook_checks.php");
record_visit();
$db = new db_functions();
// checks to see if the given account is already in use
function check_email($email)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Users`
WHERE `Email` LIKE '$email'
LIMIT 0 , 30";
$result = $db->db_query($query);
if($row = $db->db_fetch_row($result))
return true;
else
return false;
}
// checks to see if the user is logged out of facebook
function is_logged_out_fb($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Users`
WHERE `Crypt` LIKE '$crypt'
AND `Force Logout` LIKE 'true'
LIMIT 0 , 30";
$result = $db->db_query($query);
if($row = $db->db_fetch_row($result))
return true;
else
return false;
}
// changes the logout status of this facebook user
function change_log_out_status_fb($crypt, $status)
{
$db = new db_functions();
$db->db_connect();
$query = "UPDATE `ididit`.`Users` SET `Force Logout` = '$status' WHERE `Users`.`Crypt` = '$crypt';";
$db->db_query($query);
}
// checks whether the given crypt value of the given type is alreayd in use
function check_crypt($crypt, $type)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `$type`
WHERE `Crypt` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
if($db->db_num_rows($result) != 0)
{
return false;
}
return true;
}
// generates random hexidecimal number
function gen_rand_hex()
{
$number = substr(md5(rand()), 0, 10);
return $number;
}
// adds a new user with some optional parameters
function add_user($fname, $lname, $pass, $email, $picture = "", $id = "", $access_token = "")
{
$db = new db_functions();
$db->db_connect();
$crypt = gen_rand_hex();
while(!check_crypt($crypt, 'Users')) {
$crypt = gen_rand_hex();
}
$query = "INSERT INTO `ididit`.`Users` (
`First Name` ,
`Last Name` ,
`Email` ,
`Password`,
`Crypt`,
`Picture`,
`Facebook ID`,
`Access`
)
VALUES (
'$fname', '$lname', '$email', '$pass', '$crypt', '$picture', '$id', '$access_token'
);";
$db->db_query($query);
return $crypt;
}
// updates users facebook info on login
function update_user_facebook_info($fname, $lname, $email, $picture = "", $id = "", $crypt, $access_token)
{
$db = new db_functions();
$db->db_connect();
$query = "UPDATE `ididit`.`Users` SET `First Name` = '$fname',
`Last Name` = '$lname',
`Email` = '$email',
`Picture` = '$picture',
`Access` = '$access_token',
`Facebook ID` = '$id' WHERE `Users`.`Crypt` = '$crypt' LIMIT 1 ;";
$db->db_query($query);
}
// returns all user attributes
function fetch_user_info($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Users`
WHERE `Crypt` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
$row = $db->db_fetch_row($result);
$info = array();
foreach($row as $row_item)
{
array_push($info, $row_item);
}
return $info;
}
// returns all user attributes based on fb token
function fetch_user_info_token($token)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Users`
WHERE `Facebook ID` LIKE '$token'
LIMIT 0 , 30";
$result = $db->db_query($query);
$info = array();
if($row = $db->db_fetch_row($result))
{
foreach($row as $row_item)
{
array_push($info, $row_item);
}
}
return $info;
}
// returns all users goals
function fetch_user_goals($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Goal`
WHERE `Crypt of User` LIKE '$crypt'
ORDER BY `Date Posted` DESC
LIMIT 0 , 30";
$result = $db->db_query($query);
$goals = array();
while($row = $db->db_fetch_row($result))
{
array_push($goals, $row);
}
return $goals;
}
// finds a specific achievement/goal
function fetch_user_goal($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Goal`
WHERE `Crypt` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
$row = $db->db_fetch_row($result);
return $row;
}
// checks if the given goal is of the given users
function is_user_goal($crypt, $user)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Goal`
WHERE `Crypt` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
$goals = array();
$row = $db->db_fetch_row($result);
return $row[5] == $user;
}
// adds a new achievemnt to the users profile
function add_new_goal($title, $date_s, $date_e, $pic, $desc, $crypt_of_user, $category, $witness, $youtube, $org = '', $prof = '', $school = '')
{
$db = new db_functions();
$db->db_connect();
$crypt = gen_rand_hex();
while(!check_crypt($crypt, 'Goal')) {
$crypt = gen_rand_hex();
}
$query = "INSERT INTO `ididit`.`Goal` (
`Title` ,
`Date Started` ,
`Date Achieved` ,
`Description` ,
`Picture` ,
`Crypt of User`,
`Category`,
`Crypt`,
`Witness`,
`Youtube`,
`School`,
`Orginization`,
`Company`
)
VALUES (
'$title', '$date_s', '$date_e', '$desc', '$pic', '$crypt_of_user','$category', '$crypt', '$witness', '$youtube', '$school', '$org', '$prof'
);";
$db->db_query($query);
}
// genereates a random picture name
function gen_pic_name($original)
{
$components = explode(".", $original);
$end = $components[(sizeof($components)-1)];
$name = gen_rand_hex();
if(file_exists("uploads/".$name.".".$end)){
return gen_pic_name($original);
}
else{
return $name.".".$end;
}
}
// changes the users profile picture
function change_user_pic($crypt, $path)
{
$db = new db_functions();
$db->db_connect();
$query = "UPDATE `ididit`.`Users` SET
`Picture` = '$path'
WHERE `Users`.`Crypt` = '$crypt'
LIMIT 1 ;";
$db->db_query($query);
}
// updates this users information, for the account page
function update_user_info($fname, $lname, $email, $password, $crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "UPDATE `ididit`.`Users` SET `First Name` = '$fname',
`Last Name` = '$lname',
`Email` = '$email',
`Password` = '$password' WHERE `Users`.`Crypt` = '$crypt' LIMIT 1 ;";
$db->db_query($query);
}
// records a visit from any user, guest or member
function record_visit()
{
$db = new db_functions();
$db->db_connect();
$ip_new = VisitorIP();
$page_name = find_full_page_name();
$query = "SELECT *
FROM `Visits`
WHERE `IP` LIKE '$ip_new'
AND `Page Name` LIKE '$page_name'
LIMIT 0 , 30";
$result = $db->db_query($query);
$visited = false;
if($row = $db->db_fetch_row($result))
{
$visited = true;
$ip = $row[0];
$times = $row[2];
$user = $row[3];
$page_name = $row[4];
$new_times = $times + 1;
if(strlen($user) == 0)
{
$new_user = $_SESSION['email'];
}
else
{
$new_user = $user;
}
}
if($visited)
{
$date = date('Y-m-d g-i-s', time()+(60*60*3));
$query = "UPDATE `ididit`.`Visits` SET `Most Recent` = '$date',
`Times` = '$new_times',
`User` = '$new_user' WHERE
`Visits`.`IP` = '$ip' AND
`Visits`.`Times` =$times AND
`Visits`.`User` = '$user' AND
`Visits`.`Page Name` = '$page_name' LIMIT 1 ;";
}
else
{
if(isset($_COOKIE['user']))
$user = $_COOKIE['user'];
else
$user = '';
if(strlen($user) == 0)
{
$user = "Guest";
}
$date = date('Y-m-d g-i-s', time()+(60*60*3));
$location_info = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?format=json&key=b49cd6eb6da4c0e429300fd010f02061db560d5b38c2526481abe89bc73f8b3b&ip=".$ip_new));
@$country = @$location_info->{'countryName'};
@$city = @$location_info->{'cityName'};
@$zip = @$location_info->{'zipCode'};
@$state = @$location_info->{'regionName'};
$query = "INSERT INTO `ididit`.`Visits` (
`IP` ,
`Most Recent` ,
`Times` ,
`User`,
`Page Name`,
`Country`,
`City`,
`Zip`,
`State`
)
VALUES (
'$ip_new',
'$date', '1', '$user', '$page_name', '$country', '$city','$zip', '$state'
);";
}
$db->db_query($query);
}
// method to find users IP address
function VisitorIP()
{
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
else $TheIp=$_SERVER['REMOTE_ADDR'];
return trim($TheIp);
}
// returns the whole page name(this means includes arguments) but minus the domain name
function find_full_page_name()
{
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$array = explode("/", $url);
$full_name = "";
$length = sizeof($array);
$start = 1;
while($start < $length)
{
$full_name = $full_name."/".$array[$start];
$start += 1;
}
return $full_name;
}
// adds a comment to the achievement
function add_comment($comment, $crypt_user, $crypt_goal)
{
$db = new db_functions();
$db->db_connect();
$comment = strip_tags($comment);
$date = date('Y-m-d g-i-s', time()+(60*60*3));
$crypt = gen_rand_hex();
while(!check_crypt($crypt, 'Comments')) {
$crypt = gen_rand_hex();
}
$query = "INSERT INTO `ididit`.`Comments` (
`Comment` ,
`Date Posted` ,
`Crypt of User` ,
`Crypt of Goal` ,
`Crypt`
)
VALUES (
'$comment',
'$date', '$crypt_user', '$crypt_goal', '$crypt'
);";
$db->db_query($query);
}
// gets the comments that are on this goal
function get_comments($goal)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Comments`
WHERE `Crypt of Goal` LIKE '$goal'
ORDER BY `Date Posted` DESC
LIMIT 0 , 30";
$result = $db->db_query($query);
$comments = array();
while($row = $db->db_fetch_row($result))
{
array_push($comments, $row);
}
return $comments;
}
// generates small goal HTML
function gen_small_goal($row, $float)
{
if($float)
echo '<div class="stream-goal-small" style="float: right;">';
else
echo '<div class="stream-goal-small" style="float: left;">';
echo '<a href="goal.php?id='.$row[8].'"><h2>'.html_entity_decode($row[0])."</h2></a>";
echo '<img src="'.$row[4].'">';
echo '<div class="small-space"></div>';
echo '</div>';
}
// generates large goal HTML
function gen_large_goal($row)
{
echo '<div class="stream-goal">';
echo '<a href="goal.php?id='.$row[8].'"><h2>'.html_entity_decode($row[0])."</h2></a>";
echo '<img src="'.$row[4].'">';
echo '<div class="small-space"></div>';
echo '</div>';
echo '<div class="space"></div>';
}
// genreates large detailed goal HTML
function gen_large_detailed_goal($goal)
{
echo '<div class="goal">';
echo '<div class="goal_column">';
echo '<a href="goal.php?id='.$goal[8].'"><h1>'.html_entity_decode($goal[0]).'</h1></a><p>'.html_entity_decode($goal[3]).'</p>';
echo '<div class="witness">';
echo '<h2>Witnesses</h2>';
if($goal[6] != '')
{
$witnesses = explode(',', $goal[6]);
foreach ($witnesses as $witness)
{
$info = fetch_user_info_token($witness);
if(sizeof($info) > 0)
echo "<a href='profile.php?id=".$info[4]."'><img src='http://graph.facebook.com/".$witness."/picture?type=square' alt=''></a>";
else
echo "<img src='http://graph.facebook.com/".$witness."/picture?type=square' alt=''>";
}
}
else
{
echo "No Witness Mentioned";
}
echo "</div>";
echo '<div class="witness">';
echo '<h2>Congratulators</h2>';
if($goal[9] != '')
{
$congradulator = explode(',', $goal[9]);
foreach ($congradulator as $congrats) {
$info = fetch_user_info($congrats);
echo "<a href='profile.php?id=".$info[4]."'><img src='".$info[5]."' alt=''></a>";
}
}
else
{
echo "No Congratulators Yet";
}
echo "</div>";
echo '</div>';
echo '<div class="goal-column-wrapper">';
if($goal[4] != '')
{
echo '<div class="goal_column_double">';
echo '<img src="'.$goal[4].'">';
echo '</div>';
echo '<div class="goal_column_double">';
echo '<img src="'.$goal[4].'">';
echo '</div>';
}
echo '<div class="info">';
echo '<h3>Stats</h3>';
if($goal[9] != '')
echo 'Nods: <span id="'.$goal[8].'">'.sizeof(explode(",",$goal[9]))."</span>";
else
echo 'Nods: <span id="'.$goal[8].'">0'."</span>";
if($goal[6] != '')
echo 'Validators: '.sizeof(explode(",",$goal[6]));
else
echo 'Validators: 0';
echo '</div>';
echo '</div>';
if(isset($_COOKIE['user']) && (isset($_GET['id'])) && (strpos($goal[9], $_COOKIE['user']) === false) && ($_GET['id'] != $_COOKIE['user']))
{
$user = "'".$_COOKIE['user']."'";
$goal_crypt = "'".$goal[8]."'";
echo '<div class="congrats-button"><a onclick="congrats('.$user.', '.$goal_crypt.', this); return false;" href="">Congratulate</a></div>';
}
$album_info = fetch_album($goal[8]);
if(isset($_COOKIE['user']) && ((!isset($_GET['id'])) || $_GET['id'] == $_COOKIE['user']))
{
if(!isset($album_info[1]))
echo '<a href="add_album.php?g='.$goal[8].'">Add Album</a>';
else
echo '<a href="add_album.php?g='.$goal[8].'&id='.$album_info[6].'&mode=edit">Edit Album</a>';
}
else if(isset($album_info[1]))
echo '<a href="view_album.php?g='.$goal[8].'">View Album</a>';
echo '</div>';
echo '<div class="space"></div>';
}
// decides what to output for the given notification
function get_notifications_html($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Notifications`
WHERE `Crypt of User` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
while($row = $db->db_fetch_row($result))
{
if($row[3] == 'false')
{
switch ($row[1])
{
case 'Congrats':
$user_info = fetch_user_info($row[5]);
echo '<tr class="notification">';
echo '<td><a href="goal.php?id='.$row[4].'&n=true">'.$user_info[0].' Congradulated You!</a></td>';
echo '</tr>';
break;
case 'Comment':
$user_info = fetch_user_info($row[5]);
echo '<tr class="notification">';
echo '<td><a href="goal.php?id='.$row[4].'&n=true">'.$user_info[0].' Commented on your Achievement</a></td>';
echo '</tr>';
case 'Reply':
$user_info = fetch_user_info($row[5]);
$other_user_info = fetch_user_info($row[2]);
echo '<tr class="notification">';
echo '<td><a href="goal.php?id='.$row[4].'&n=true">'.$user_info[0].' also commented on '.$other_user_info[0]."'s Achievement</a></td>";
echo '</tr>';
default:
break;
}
}
}
}
// mark a notification as read
function kill_notifications($object)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Notifications`
WHERE `Crypt of Object` LIKE '$object'";
$result = $db->db_query($query);
while($row = $db->db_fetch_row($result))
{
$crypt = $row[6];
$query = "UPDATE `ididit`.`Notifications` SET `Seen` = 'true'
WHERE `Notifications`.`Crypt` = '$crypt' LIMIT 1 ;";
$db->db_query($query);
}
}
// creates a new notification
function new_notification($user, $interact, $type, $object)
{
$db = new db_functions();
$db->db_connect();
$crypt = gen_rand_hex();
while(!check_crypt($crypt, 'Notifications')) {
$crypt = gen_rand_hex();
}
$query = "INSERT INTO `ididit`.`Notifications` (
`Date` ,
`Type` ,
`Crypt of User` ,
`Seen` ,
`Crypt of Object` ,
`Crypt of Issuing User`,
`Crypt`
)
VALUES (
CURRENT_TIMESTAMP , '$type', '$user', 'false', '$object', '$interact', '$crypt'
);";
$db->db_query($query);
}
// notifies all commenters of a new comment
function notify_commenters($goal, $user)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Comments`
WHERE `Crypt of Goal` LIKE '$goal'
LIMIT 0 , 30";
$result = $db->db_query($query);
$users = array();
while($row = $db->db_fetch_row($result))
{
if(!in_array($row[2], $users) && $row[2] != $user)
array_push($users, $row[2]);
}
foreach($users as $not_user)
{
new_notification($not_user, $user, 'Reply', $goal);
}
}
// deletes an album
function delete_album($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "DELETE FROM `ididit`.`Album` WHERE `Album`.`Crypt` = '$crypt' LIMIT 1";
$db->db_query($query);
}
// adds an album
function add_album($locals, $fbs, $fb_albums, $user, $goal, $crypt)
{
$temp = fetch_album($goal);
if(isset($temp[0]))
{
$crypt = $temp[0];
$date = $temp[1];
delete_album($crypt);
}
$db = new db_functions();
$db->db_connect();
if(!isset($date))
$date = date('Y-m-d g-i-s', time()+(60*60*3));
$query = "INSERT INTO `ididit`.`Album` (
`Crypt` ,
`Date Added` ,
`Local Pictures` ,
`Crypt of Goal` ,
`Crypt of User`,
`FB Pictures`,
`FB Album ID's`
)
VALUES (
'$crypt',
'$date', '$locals', '$goal', '$user', '$fbs', '$fb_albums'
);";
$db->db_query($query);
}
// returns the album
function fetch_album($crypt)
{
$db = new db_functions();
$db->db_connect();
$query = "SELECT *
FROM `Album`
WHERE `Crypt of Goal` LIKE '$crypt'
LIMIT 0 , 30";
$result = $db->db_query($query);
$info = array();
if($row = $db->db_fetch_row($result))
{
foreach($row as $row_item)
{
array_push($info, $row_item);
}
}
return $info;
}
// makes a thumbnail image from a bigger image
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
?>