-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
executable file
·281 lines (258 loc) · 8.99 KB
/
user.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
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
<?php
Namespace MusicTogether;
use GuzzleHttp;
/**
* This class holds information on a user object.
*
* This class initialises a User object with all of their details.
* It connects to the Spotify API and MySQL database to properly
* setup a user to use Music Together!
*/
class User {
/**
* The user's Spotify ID.
* @var string $id
*/
public $id;
/**
* The user's original name as set inside of Spotify.
* @var string $name
*/
public $name;
/**
* The user's nickname set inside of Music Together!
* Defaults to NULL when the user is first created.
* @var string|null $nickname
*/
public $nickname;
/**
* The user's email address from Spotify.
* @var string $email
*/
public $email;
/**
* A URL to the user's profile picture. Set to "defaultProfPic.png"
* if none exists from Spotify.
* @var string $prof_pic
*/
public $prof_pic;
/**
* A URL to the user's profile picture on Spotify.
* @var string $prof_pic_orig
*/
public $prof_pic_orig;
/**
* Whether the user is banned or not.
* @var boolean $banned
*/
public $banned;
/**
* Whether the user has a Spotify Premium subscription or not.
* @var boolean $premium
*/
public $premium;
/**
* Whether the user prefers to mute the dashboard preview sound or not.
* @var boolean $dashboard_muted
*/
public $dashboard_muted;
/**
* An access token to make requests to the Spotify API with. Used by
* methods part of this class.
* @var string $access_token
*/
public $access_token;
/**
* Whether it is the user's first logon or not.
* @var bool $first_login
*/
public $first_login = false;
/**
* Constructor method for User class.
*
* @param string $access_token An access token to access the
* Spotify API with.
* @param MusicTogether\DatabaseConnection $db_con A DatabaseConnection object
* with a valid connection to
* connect to the database with.
*
* @return User A User object containing information about a User.
*/
public function __construct($access_token, $db_con) {
// Get the user's data from Spotify
$this->access_token = $access_token;
$user_data = $this->getUserData();
// echo "<br><br>USER DATA<br><br><br><br>";
// var_dump($user_data);
$id = $user_data->{"id"};
if ($user_data->{"product"} == "premium") {
$this->premium = True;
$db_data = $db_con->getUser($id);
if (!($db_data)) {
// If db_con error occured
errorToHome("db_data_is_false");
} else if ($db_data == "not_found") {
// If the user is not found, attempt to insert user
if (!($db_con->insertUser($user_data))) {
errorToHome("db_con_insert_user_1");
}
$this->first_login = true;
$this->banned = false;
} else {
// If user exists in the database
// Get the database entry for if the user is banned
$this->banned = $db_data["banned"];
if ($this->banned == "1") {
// If the user is banned
$this->banned = true;
errorToHome("banned");
} else {
$this->banned = false;
}
}
// Set properties to setup the dashboard
$this->id = $id;
$this->name = $user_data->{"display_name"};
// Set the nickname and muted property depending on whether new user or not
if ($db_data == "not_found") {
$this->nickname = NULL;
$this->dashboard_muted = False;
} else {
$this->nickname = $db_data["nickname"];
// echo $db_data["dashboardMuted"];
$this->dashboard_muted = $db_data["dashboardMuted"];
}
$this->email = $user_data->{"email"};
// Set the profile picture
if ($db_data["profilePicture"] == "defaultProfilePicture.png" ||
substr($db_data["profilePicture"], 0, 20) != "https://morahman.me/") {
if (count($user_data->{"images"}) == 0) {
$userProfPic = "defaultProfilePicture.png";
} else {
$userProfPic = $user_data->{"images"}[0]->{"url"};
}
} else {
$userProfPic = $db_data["profilePicture"];
}
$this->prof_pic = $userProfPic;
$db_con->updateUser($this);
} else {
$this->premium = False;
}
}
/**
* Update a user's nickname and profile picture.
*
* Get the latest nickname and profile picture from the datbase.
*
* @param DatabaseConnection $db_con
*/
public function update($db_con) {
$db_data = $db_con->getUser($this->id);
$this->nickname = $db_data["nickname"];
$this->prof_pic = $db_data["profilePicture"];
}
/**
* Logs a login on the databse.
*
* This function connects to the database and logs a login of the current
* User using {@see DatabaseConnection::insertLogin()}.
*
* @param DatabaseConnection $db_con
* @return void
*/
public function logLogin($db_con) {
$db_con->insertLogin("user", $this->id);
}
/**
* Performa a GET request using Guzzle using the provided endpoint.
*
* @param string $url The endpoint to request. Only include the URL
* after "v1/", for example "me".
*
* @return \Psr\Http\Message\ResponseInterface The response from {@see GuzzleHttp\Client::request()}.
*/
function getRequest($url) {
// Require Guzzle
require_once 'vendor/autoload.php';
require "constants.php";
$client = new GuzzleHttp\Client();
// Do a GET request
try {
$response = $client->request(
"GET",
$api_endpoint . $url,
[
"headers" => [
"Authorization" => "Bearer " . $this->access_token
]
]
);
} catch (Exception $e) {
// If an error occured in the request
echo "<br><br>GUZZLE ERROR<br><br><br><br>";
var_dump($e);
$_SESSION["latest_error"] = $e;
errorToHome("guzzle_getRequest_caughtException");
}
return $response;
}
/**
* Get the user's data from the Spotify API
*
* @see User::getRequest() Do a GET request using Guzzle.
* @return mixed|null A JSON object representing the response from Spotify.
*/
function getUserData() {
// "functions.php";
$response = $this->getRequest("me");
// Return an object of the JSON response
return json_decode($response->getBody()->getContents());
}
/**
* Get the user's image from Spotify API
*
* @return string|null The image from the user's profile. Null if none set.
*/
public function getProfPic() {
$response = $this->getRequest("me");
$response = json_decode($response->getBody()->getContents());
if (count($response->{"images"}) == 0) {
return null;
} else {
return $response->{"images"}[0]->{"url"};
}
}
/**
* Get the user's recently played tracks
*
* @param int $limit The number of tracks to receive.
* @return mixed[] An array containing an array of Track objects of the length specified in paramter
* at index 0 and a string containing the next href at index 1.
* @see User::getRequest() Method to handle GET request.
*/
public function getRecentTracks($limit) {
require_once "track.php";
// Store an array of Track objects to be returned
$tracks = array();
$response = json_decode($this->getRequest("me/player/recently-played?limit=" . $limit)->getBody()->getContents());
foreach ($response->{"items"} as $track) {
array_push($tracks, new Track($track, $this->access_token));
}
return array($tracks, $response->{"next"});
}
/**
* Update the access token inside the database for current User.
*
* Push the access token of the current user to the the users table
* inside of the datbabase.
*
* @param DatabaseConnection $db_con The database connection to use.
* @return boolean Whether the operation was successful or not.
* @see DatabaseConnection::updateUserAccessToken()
*/
public function pushAccessTokenToDB($db_con) {
return $db_con->updateUserAccessToken($this->access_token, $this->id);
}
}
?>