-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-comparison.php
195 lines (147 loc) · 3.72 KB
/
new-comparison.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
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
<?php
require 'fb/facebook.php';
$email = 0;
session_start();
$facebook = new Facebook(array(
'appId' => '411664055513252',
'secret' => '7e8b0e70e147953329f73de98a9d45b6',
));
// Get User ID
$fb_user = $facebook->getUser();
if ($fb_user) {
try {
//Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$email = $user_profile['email'];
} catch (FacebookApiException $e) {
error_log($e);
$fb_user = 0;
}
}
// is cURL installed
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$api_key = "lYfF8kAmPyRZ0Wr0G_9R";
$api_url = "http://$api_key:[email protected]/api/v1";
//$email = isset($_GET['email']) ? trim($_GET['email']) : '';
function get_user($email)
{
global $api_url;
// create a new cURL resource handle
$ch = curl_init();
$url = $api_url."/user/get";
// Set URL
curl_setopt($ch, CURLOPT_URL, $url);
$data = array('email' => $email);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Include header in result
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 10 second timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Fetch resource
$output = curl_exec($ch);
// Close the cURL
curl_close($ch);
$obj = json_decode($output, true);
$user_id = $obj['user'];
$_SESSION['email'] = $email;
if ($user_id)
return $user_id;
else
return $output;
}
function new_user($email)
{
global $api_url;
// create a new cURL resource handle
$ch = curl_init();
$url = $api_url."/user/new";
// Set URL
curl_setopt($ch, CURLOPT_URL, $url);
if ($email)
{
$data = array('email' => $email);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// Include header in result
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 10 second timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Fetch resource
$output = curl_exec($ch);
// Close the cURL
curl_close($ch);
$obj = json_decode($output, true);
$user_id = $obj['user'];
if ($email)
{
$_SESSION['email'] = $email;
}
if ($user_id)
return $user_id;
else
return false;
}
$user_id = "";
if ($email && !isset($_SESSION['email'])) {
$user_id = new_user($email);
if(!$user_id)
{
$user_id = get_user($email);
}
$_SESSION['user'] = $user_id;
$_SESSION['fb_login'] = true;
}
elseif ($email || (isset($_SESSION['user']) && !isset($_SESSION['fb_login'])))
{
$user_id = $_SESSION['user'];
}
else
{
$user_id = new_user();
$_SESSION['user'] = $user_id;
if (isset($_SESSION['fb_login']))
{
unset($_SESSION['fb_login']);
}
if (isset($_SESSION['email']))
{
unset($_SESSION['email']);
}
if (isset($_SESSION['picks']))
{
unset($_SESSION['picks']);
}
}
//echo "User id: ".$user_id;
// create a new cURL resource handle
$ch = curl_init();
$url = $api_url."/discovery_game/comparison/new";
// Set URL
curl_setopt($ch, CURLOPT_URL, $url);
// Set POST parameters
$data = array('user' => $user_id);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Include header in result
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 10 second timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Fetch resource
$output = curl_exec($ch);
// Close the cURL
curl_close($ch);
if (preg_match("/bad/i", $output))
echo json_encode(array("email" => $_SESSION['email'], "user" => $_SESSION['user']));
else
echo $output;
?>