-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchResult.php
414 lines (348 loc) · 14 KB
/
searchResult.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>LeagueLights</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/shop-homepage.css" rel="stylesheet">
<link href="css/leaguelights.css" rel="stylesheet">
<!-- videojs style sheet -->
<link href="http://vjs.zencdn.net/6.2.8/video-js.css" rel="stylesheet">
<!-- videojs js file -->
<script src="http://vjs.zencdn.net/6.2.8/video.js"></script>
<style>
img {
max-width: 200px;
max-height: 200px;
}
video {
max-width: 200px;
max-height: 160px;
}
</style>
</head>
<body>
<?php
//boolean to decide wheather to display link to logged in user's profile "My Lights" or not
$profileDisplay = false;
if(isset($_SESSION['currentId'])){
//if user is logged in, display sign out option and display "My Lights" button
$loggedInAs = $_SESSION['currentId'];
$signStatus = "Sign out";
//sign out link will pop up a new tab to confirm sign out
$href = "javascript:window.open('signOut.php','mywindowtitle','width=1000,height=400')";
$profileDisplay = true;
}else{
//if user is not logged in, display sign in
$signStatus = "Sign in";
$href = "login.php";
}
//$_GET will get value of corresponding variable from URL
$serverStr = $_GET['server'];
$searchKey = $_GET['keyword'];
$userKey = str_replace(' ', '', $searchKey);
$userExist = false;
$searchKey = "%".$searchKey."%";
$videoSrc = array();
$video = array();
$title = array();
$uploader = array();
$uploaderName = array();
$likes = array();
$liked = array();
$numVideos = "";
$summonerLevel = "";
$summonerId = "";
$summonerIcon = "";
$summonerName = "";
//getenv will retrive enviornmental variables
$apiKey = getenv('RIOT_API');
$version = getenv('CLIENT_VERSION');
/*
curl is used to check if summoner name with exact searchKey is registered in leaguelights
and to access user database, input has to convert into summonerId
*/
$serverInfo = "https://" . $serverStr;
$userInfo = $serverInfo . ".api.riotgames.com/lol/summoner/v3/summoners/by-name/" .$userKey. "?api_key=" .$apiKey;
//curl initialize with request url
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $userInfo);
$userResult = curl_exec($ch);
curl_close($ch);
$obj = json_decode($userResult);
//if output object from the curl has property 'id' that means LeagueLights has user with same name as searchKey
if (property_exists($obj, 'id')){
$summonerId = $obj->{'id'};
$summonerName = $obj->{'name'};
}
$dbHost = getenv('RDS_HOSTNAME');
$dbUser = getenv('RDS_USERNAME');
$dbPass = getenv('RDS_PASSWORD');
$dbConn = 'mysql:host='.$dbHost.';dbname='.$serverStr.';charset=utf8mb4';
//create new php database object with credential information
$conn = new \PDO( $dbConn,
$dbUser,
$dbPass,
array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_PERSISTENT => false
)
);
if (!empty($summonerId)){
//if output object from the curl has property 'id' display the user's profile
$idHandle = $conn->prepare("SELECT SummonerId from user where SummonerId=?");
$idHandle->bindParam(1, $summonerId, PDO::PARAM_INT);
$idHandle->execute();
if($idHandle->rowCount()>0){
//user's profile display contains their summoner icon, summoner level, and number of videos they uploaded to LeagueLights
$userExist = true;
$summonerIcon = $obj->{'profileIconId'};
$summonerLevel = $obj->{'summonerLevel'};
$numVideos = $conn->prepare("select Id from video where Uploader=?");
$numVideos->bindParam(1, $summonerId, PDO::PARAM_INT);
$numVideos->execute();
$numVideos = $numVideos->rowCount();
}
}
//display all videos with title that contains searchKey
$srcHandle = $conn->prepare("SELECT Id, Uploader from video where Title LIKE ?");
$srcHandle->bindParam(1, $searchKey, PDO::PARAM_STR);
$srcHandle->execute();
$likedHandle = $conn->prepare("SELECT * from likes where likedBy = ? AND videoId = ?");
//for each videos with title that contains searchKey, store video link to $videoSrc array, uploader name to $uploader array, and video id to $video array
foreach ($srcHandle as $value){
//files in aws buckets can be easily accessed with just https request
$src = "https://s3-ca-central-1.amazonaws.com/".$serverStr."-vid/".$value['Uploader']."/".$value['Id'];
array_push($videoSrc, $src);
array_push($uploader, $value['Uploader']);
array_push($video, $value['Id']);
$likedHandle->bindParam(1, $loggedInAs, PDO::PARAM_INT);
$likedHandle->bindParam(2, $value['Id'], PDO::PARAM_STR);
$likedHandle->execute();
if ($likedHandle->rowCount() < 1){
array_push($liked, false);
} else {
array_push($liked, true);
}
}
//if there is uploaders, need to convert summonerId from database to user's current in game name
if (count($uploader)>0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach ($uploader as $value){
$idToNameRequest = $serverInfo . ".api.riotgames.com/lol/summoner/v3/summoners/".$value."?api_key=".$apiKey;
curl_setopt($ch, CURLOPT_URL, $idToNameRequest);
$nameResult = curl_exec($ch);
$name = json_decode($nameResult);
array_push($uploaderName, $name->{'name'});
}
curl_close($ch);
}
$srcJson = json_encode($videoSrc);
$likedJson = json_encode($liked);
$videoJson = json_encode($video);
$uploaderJson = json_encode($uploaderName);
$titleHandle = $conn->prepare("SELECT Id,Title from video where Title Like ?");
$titleHandle->bindParam(1, $searchKey, PDO::PARAM_STR);
$titleHandle->execute();
$likeHandle = $conn->prepare("SELECT * from likes where videoId = ?");
foreach ($titleHandle as $value){
array_push($title, $value['Title']);
$likeHandle->bindParam(1, $value['Id'], PDO::PARAM_STR);
$likeHandle->execute();
array_push($likes, $likeHandle->rowCount());
}
$titleJson = json_encode($title);
$likesJson = json_encode($likes);
?>
<script>
var videos = JSON.parse('<?php echo $srcJson; ?>');
var titles = JSON.parse('<?php echo $titleJson; ?>');
var uploaderName = JSON.parse('<?php echo $uploaderJson; ?>');
var likes = JSON.parse('<?php echo $likesJson; ?>');
var videoId = JSON.parse('<?php echo $videoJson; ?>');
var likedBy = JSON.parse('<?php echo $likedJson; ?>');
var counter = videos.length;
//function to addUser profile if it exist
function addUserElement(){
var userName = "<?php echo $summonerName; ?>";
var numVideos = "<?php echo $numVideos; ?>";
var userLevel = "<?php echo $summonerLevel; ?>";
var userId = "<?php echo $summonerId; ?>";
var profileIconUrl = "http://ddragon.leagueoflegends.com/cdn/" + "<?php echo $version; ?>" + "/img/profileicon/" + "<?php echo $summonerIcon; ?>" + ".png";
var profileUrl = "publicProfile.php?userId=";
profileUrl += userId;
var doubleSpacer = document.createElement("div");
doubleSpacer.setAttribute("class", "col-lg-2");
var singleSpacer = document.createElement("div");
singleSpacer.setAttribute("class", "col-lg-1");
var summonerIcon = document.createElement("img");
summonerIcon.setAttribute("src", profileIconUrl);
var summonerInfo = document.createElement("div");
summonerInfo.setAttribute("class", "col-lg-4");
//Summoner info section begin
var summonerName = document.createElement("h5");
summonerName.setAttribute("class", "row text-center");
summonerName.innerHTML = userName;
var summonerLevel = document.createElement("h5");
summonerLevel.setAttribute("class", "row text-center");
summonerLevel.innerHTML = "Level " + userLevel;
var videoInfo = document.createElement("h5");
videoInfo.setAttribute("class", "row text-center");
videoInfo.innerHTML = numVideos + " uploads";
summonerInfo.appendChild(summonerName);
summonerInfo.appendChild(summonerLevel);
summonerInfo.appendChild(videoInfo);
//Summoner info section end
var aTag = document.createElement("a");
aTag.setAttribute("href", profileUrl);
aTag.setAttribute("class", "row");
aTag.appendChild(summonerIcon);
aTag.appendChild(singleSpacer);
aTag.appendChild(summonerInfo);
var user = document.getElementById("userProfile");
user.appendChild(aTag);
}
//add videos if they exist
function addVidElement(vid, vidTitle, name, like, videoId, liked){
var divider = document.createElement("form");
divider.setAttribute("class", "row mb-4");
divider.setAttribute("action", "likes.php");
divider.setAttribute("method", "post");
var singleSpacer = document.createElement("div");
singleSpacer.setAttribute("class", "col-lg-1");
var title = document.createElement("h2");
title.setAttribute("class", "row text-center");
title.setAttribute("style", "white-space: nowrap; text-overflow: ellipsis");
title.innerHTML = vidTitle;
var uploader = document.createElement("h4");
uploader.setAttribute("class", "row text-center");
uploader.setAttribute("style", "white-space: nowrap; text-overflow: ellipsis");
uploader.innerHTML = name;
var likes = document.createElement("h4");
likes.setAttribute("class", "row text-center");
likes.innerHTML = like + " likes";
var videoInfo = document.createElement("div");
videoInfo.setAttribute("class", "col-lg-4");
var likeBtn = document.createElement("button");
likeBtn.setAttribute("type", "submit");
likeBtn.setAttribute("name", "submit");
likeBtn.setAttribute("value", "submit");
likeBtn.setAttribute("class", "row text-center");
if(liked){
likeBtn.setAttribute("style", "background-color:#ADD8E6");
likeBtn.innerHTML = "liked";
}else{
likeBtn.innerHTML = "like";
}
videoInfo.appendChild(title);
videoInfo.appendChild(uploader);
videoInfo.appendChild(likes);
videoInfo.appendChild(likeBtn);
var video = document.createElement("video");
video.setAttribute("class", "col-lg-6 video-js vjs-icon-play-circle");
video.setAttribute("preload", "auto");
video.setAttribute("controls", true);
video.src = vid;
var vidId = document.createElement("input");
vidId.setAttribute("type", "hidden");
vidId.setAttribute("name", "videoId");
vidId.setAttribute("value", videoId);
vidId.setAttribute("style", "display:none");
divider.appendChild(vidId);
divider.appendChild(video);
divider.appendChild(singleSpacer);
divider.appendChild(videoInfo);
var container = document.getElementById("videoContainer");
container.appendChild(divider);
}
window.onload = function() {
// sign in/out display and its corresponding link
var sign = "<?php echo $signStatus; ?>";
var signInfo = document.getElementById("sign");
var link = document.createElement("a");
link.setAttribute("href", "<?php echo $href; ?>");
link.setAttribute("class", "nav-link");
link.innerHTML = sign;
signInfo.appendChild(link);
//display "My Lights" and its link
if ("<?php echo $profileDisplay; ?>"){
var showProfile = document.getElementById("profile");
var profileAnchor = document.createElement("a");
profileAnchor.setAttribute("href", "profile.php");
profileAnchor.setAttribute("class", "nav-link");
profileAnchor.innerHTML = "My Lights";
showProfile.appendChild(profileAnchor);
}
//if there's a user with the corresponding searchKey, display the user profile
if ("<?php echo $userExist; ?>"){
addUserElement();
}
//if there's videos with the corresponding searchKey, display videos
if (counter>0){
for(var i=0;i<counter;i++){
addVidElement(videos[i], titles[i], uploaderName[i], likes[i], videoId[i], likedBy[i]);
}
}
//if there's no videos with the corresponding searchKey and no user with the searchKey, display result not found message
if (counter == 0 && !("<?php echo $userExist; ?>")){
var text = document.createElement('h1');
text.setAttribute("class", "text-center");
text.innerHTML = "The summoner you are looking for is not registered on LeagueLights, or the video with the title does not exist";
var result = document.getElementById("userProfile");
result.appendChild(text);
}
}
</script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="/">LeagueLights</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<div class="navbar-nav ml-auto">
<li id="profile" class="nav-item ml-auto">
</li>
<li id="sign" class="navbar-nav ml-auto">
</li>
</div>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-6 my-2 card-body" id="userProfile"></div>
<div class="col-lg-3"></div>
</div>
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-6 my-2 card-body" id="videoContainer"></div>
<div class="col-lg-3"></div>
</div>
</div>
<!-- Footer -->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright © LeagueLights 2018</p>
</div>
<!-- /.container -->
</footer>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/popper/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>