-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.php
230 lines (222 loc) · 8.3 KB
/
profile.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
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
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
session_start();
require_once ("./hybridauth/Hybrid/Auth.php");
require_once ("./classes/Database.class.php");
require_once ("./classes/User.class.php");
require_once ("./classes/Auth.class.php");
$db = new Database();
$user = new User();
$auth = new Authentication();
$config_file_path = './hybridauth/config.php';
$userDataInDatabase = array();
$availableProviders = array("twitter", "facebook");
function lower(&$string) {
$string = strtolower($string);
}
try {
$hybridauth = new Hybrid_Auth($config_file_path);
if (isset($_GET['callback'])) {
$callbackProvider = $_GET['callback'];
$connectedProviders = $hybridauth -> getConnectedProviders();
if (!empty($connectedProviders)) {
foreach ($connectedProviders as $connectedProvider) {
$adapter = $hybridauth -> authenticate($connectedProvider);
$user_data = $adapter -> getUserProfile();
$userInfo = $auth -> find_by_provider_uid($connectedProvider, $user_data -> identifier);
if (!empty($userInfo)) {
$userDataInDatabase = $user -> find_by_id($userInfo['user_id']);
}
}
} else {
header("Location: index.php");
}
} elseif (isset($_GET['reg'])) {
if (isset($_POST['register'])) {
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$birthdate = $_POST['birthDate'];
$email = $_POST['email'];
$location = $_POST['location'];
$userID = $user -> create($firstname, $lastname, $birthdate, $email, $location);
$connectedProviders = $hybridauth -> getConnectedProviders();
foreach ($connectedProviders as $connectedProvider) {
$adapter = $hybridauth -> getAdapter($connectedProvider);
$user_data = $adapter -> getUserProfile();
$auth -> create($userID, $connectedProvider, $user_data -> identifier, $user_data -> displayName, $user_data -> profileURL);
}
header("Location: profile.php");
}elseif(isset($_POST['update'])){
$connectedProviders = $hybridauth -> getConnectedProviders();
$selectedProviders = array();
$userID = NULL;
foreach ($connectedProviders as $connectedProvider) {
$adapter = $hybridauth -> getAdapter($connectedProvider);
$user_data = $adapter -> getUserProfile();
$authUser = $auth->find_by_provider_uid($connectedProvider, $user_data->identifier);
if($authUser != NULL){
array_push($selectedProviders, $authUser['provider']);
$userID = $authUser['user_id'];
}
}
array_walk($selectedProviders, "lower");
array_walk($connectedProviders, "lower");
$notLinkedProviders = array_diff($connectedProviders, $selectedProviders);
foreach($notLinkedProviders as $notLinkedProvider){
$adapter = $hybridauth -> getAdapter($notLinkedProvider);
$user_data = $adapter -> getUserProfile();
$auth -> create($userID, $notLinkedProvider, $user_data -> identifier, $user_data -> displayName, $user_data -> profileURL);
}
header("Location: profile.php");
}
} else {
$connectedProviders = $hybridauth -> getConnectedProviders();
if (!empty($connectedProviders)) {
foreach ($connectedProviders as $connectedProvider) {
$adapter = $hybridauth -> authenticate($connectedProvider);
$user_data = $adapter -> getUserProfile();
$userInfo = $auth -> find_by_provider_uid($connectedProvider, $user_data -> identifier);
if (!empty($userInfo)) {
$userDataInDatabase = $user -> find_by_id($userInfo['user_id']);
}
}
} else {
header("Location: index.php");
}
}
} catch( Exception $e ) {
echo "Ooophs, we got an error: " . $e -> getMessage();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<link rel="stylesheet" href="application/public/css.css" type="text/css" >
<style type="text/css">
.userInfo {
width: 400px;
}
.userInfo label {
float: left;
width: 150px;
}
.userInfo input {
float: right;
width: 150px
}
</style>
</head>
<body>
<h1>Welcome back</h1>
<br />
<a href="index.php">Home</a>
<form method="POST" action="profile.php?reg">
<div class="userInfo">
<?php
if(!empty($userDataInDatabase)){
?>
<h2>Gegevens in de database</h2>
<label for="firstName">Voornaam</label>
<input type="text" disabled name='firstName' value='<?php echo $userDataInDatabase['first_name'];?>' />
<label for="lastName">Achternaam</label>
<input type="text" disabled name='lastName' value='<?php echo $userDataInDatabase['last_name'] ?>' />
<label for="birthDate">Verjaardag</label>
<input type="text" disabled name='birthDate' value='<?php echo $userDataInDatabase['birthdate'];?>' />
<label for="email">Email</label>
<input type="text" name='email' value='<?php echo $userDataInDatabase['email'];?>' />
<label for="location">Huidige locatie</label>
<input type="text" name='location' value='<?php echo $userDataInDatabase['location'];?>' />
<?php
}elseif(empty($userDataInDatabase)){
?>
<h2>Gegevens van <?php echo $connectedProvider;?></h2>
<label for="firstName">Voornaam</label>
<input type="text" name='firstName' value='<?php echo $user_data -> firstName;?>' />
<label for="lastName">Achternaam</label>
<input type="text" name='lastName' value='<?php echo $user_data -> lastName;?>' />
<label for="birthDate">Verjaardag</label>
<input type="text" name='birthDate' value='<?php echo $user_data -> birthYear . "-" . $user_data -> birthMonth . "-" . $user_data -> birthDay;?>' />
<label for="email">Email</label>
<input type="text" name='email' value='<?php echo $user_data -> email;?>' />
<label for="location">Huidige locatie</label>
<input type="text" name='location' value='' />
<?php
}
//User exists in Database, we can now list the associated providers for this account.
if(!empty($userDataInDatabase)){
?>
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<?php
// Looping through all the connected providers
// And show if they are stored in the database
foreach($connectedProviders as $connectedProvider){
$adapter = $hybridauth->getAdapter($connectedProvider);
$user_data = $adapter -> getUserProfile();
$accountLinkedToThisUserInDatabase = $auth->find_by_provider_uid($connectedProvider, $user_data -> identifier);
?>
<tr>
<td valign="top">
<fieldset>
<legend>
Associated authentications
</legend>
<table width="100%" cellspacing="0" cellpadding="3" border="0">
<tbody>
<tr>
<td width="35%"><b>Provider</b></td>
<td width="65%"> <?php echo $connectedProvider;?></td>
</tr>
<tr>
<td><b>Provider UID</b></td>
<td> <?php echo $accountLinkedToThisUserInDatabase['provider_uid'];?></td>
</tr>
<tr>
<td><b>Display name</b></td>
<td> <?php echo $accountLinkedToThisUserInDatabase['display_name'];?></td>
</tr>
<tr>
<td><b>User profile URL</b></td>
<td> <?php echo $accountLinkedToThisUserInDatabase['profile_url'];?></td>
</tr>
<tr>
<td><b>Link this account to your Lift22-account</b></td>
<td> <input type="checkbox" name="providerSelect[]"
<?php
if (in_array(ucfirst($accountLinkedToThisUserInDatabase['provider']), $connectedProviders)) {
echo "checked ";
}
?>
value=<?php echo $connectedProvider;?>"/></td>
</tr>
</tbody>
</table>
</fieldset>
</td>
</tr>
<?php
}
}
?>
</table>
<?php
array_walk($availableProviders, "lower");
array_walk($connectedProviders, "lower");
$notLinkedProviders = array_diff($availableProviders, $connectedProviders);
foreach ($notLinkedProviders as $notLinkedProvider) {
echo "<a href='auth.php?auth=" . $notLinkedProvider . "'>Link {$notLinkedProvider}";
}
if(!empty($userDataInDatabase)){
?>
<input type="submit" value="Update" name="update"/>
<?php
}else{
?>
<input type="submit" value="Registreer" name="register"/>
<?php
}
?>
</div>
</form>
</body>
</html>