-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriends.php
71 lines (43 loc) · 1.45 KB
/
friends.php
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
<?php
/**
* I Have Plans -- Friend controller
* @author Steven Dufresne <[email protected]>
* @link http://github.com/StevenDufresne/web-app
* @copyright 2012 Steven Dufresne
* @license BSD-3-Clause <https://github.com/stevendufresne/web-app/BSD-3-CLAUSE-LICENSE.txt>
*/
require_once 'includes/db.inc.php';
require_once 'includes/users.inc.php';
require_once 'includes/requests.inc.php';
require_once 'includes/functions.inc.php';
if ( !user_is_signed_in() ) {
header('Location: index.php');
exit;
}
$raw_email= filter_input(INPUT_POST, 'addEmail', FILTER_SANITIZE_STRING);
$user_id = ($_SESSION['user-id']);
$user_info = get_username ($db, $user_id);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
if($raw_email !== ''){
preg_match('/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i', $raw_email, $matches);
$email = $matches[0];
$friend_id = friend_email_check($db, $email);
}
if( isset($friend_id) ) {
//check to see if they are already friends
$friendAlready = check_friend_id ($db, $user_id, $friend_id);
if( !$friendAlready && $friend_id !== $user_id ){
// if they are not friends, add them
add_friend_id ($db, $user_id, $friend_id, 0);
//set the session to alert user that request has been sent.
$_SESSION['friend-request'] = true;
header('Location: friends.php');
exit;
}
} else {
$errors['no-user'] = true;
}
}
include "views/friends.html.php";
?>