-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.php
executable file
·135 lines (111 loc) · 4.37 KB
/
home.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
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
<!DOCTYPE html>
<html>
<head>
<link href="/public/css/styles.css" rel="stylesheet">
<link href="/public/css/bootstrap.min.css" rel="stylesheet">
<link href="/georgianletter.ico" rel="icon">
<title>Welcome</title>
</head>
<body>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
//Log in to facebook
require_once __DIR__ . '/vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '512533675528202',
'app_secret' => '629e3db8c0a822f695e98b7e679976c2',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getRedirectLoginHelper();
if(! isset($_SESSION['fb_access_token'])){
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
//If login attempt fails
if (! isset($accessToken)) {
if ($helper->getError()) {
header('Location: "http://localhost/index.php"');
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
$_SESSION['fb_access_token'] = (string) $accessToken;
}
//Get the user facebook ID and Name
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', $_SESSION['fb_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
//Save the facebook ID in the session
$_SESSION['facebook_id'] = (string) $user["id"];
//Print username
print('<body style="background-image:url(/img/pirosmani_stgeorge.jpg)"><div id="top" class="centeredelement"><h2>Hello, ' . $user["name"] . '!</h2></div>');
//Spacing
print('<div><br></div>');
//Tests if user is in the database
$username = NULL;
$password = NULL;
$mysqli = new mysqli("localhost", $username, $password, "yhack2015");
$query = 'SELECT * FROM Users WHERE FacebookID like ' . $user["id"];
$result = $mysqli->query($query);
$_SESSION["user_name"] = $user["name"];
if (!$result)
{
//Ask them if they'd like to create a new profile
print('<div class="centeredelement"><div><p>Would you like to create a new profile?</p></div>');
print('<div class="col-md-2"></div><div class="col-md-2"></div><div class="col-md-2"><a href="http://localhost/user_songs.php" class="btn btn-primary">Yes</a></div>
<div class="col-md-2"><a href="http://localhost/index.php" class="btn btn-default">No</a></div><div class="col-md-2"></div><div class="col-md-2"></div></div></body>');
}
else //Otherwise the user is in our database
{
$rows = $result->fetch_array(MYSQLI_NUM);
$_SESSION["user_id"] = $rows[0];
//Grab, decode, and print a table of events
try {
// Returns a `Facebook\FacebookResponse` object
$page_events = $fb->get('/onefourfiveseattle/events', $_SESSION['fb_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$events_data = $page_events->getDecodedBody();
//Prints link to profile
print('<div class="profilebutton"><p><a class="profilebutton" href="user_songs.php">My Profile</a></div>');
//Prints table
echo '<div class="centeredelement"><table class="table table-hover"><thead>
<tr>
<th><h4>Events</h4></th>
</tr>
</thead><tbody>';
foreach($events_data['data'] as $i){
print("<tr>");
print("<td><div font-weight='bold'><a href='/songlist.php?event=" . $i["id"] . "&id=" . $user["id"] . "&eventname=" . $i["name"] . "'>" . $i["name"] . "</a></div></td>");
print("</tr>");
}
echo '</tbody></table></div></body>';
}
}//End page creation
?>
</html>