-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathindex.php
More file actions
411 lines (361 loc) · 17.5 KB
/
index.php
File metadata and controls
411 lines (361 loc) · 17.5 KB
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
<?php
require_once dirname(__FILE__) . '/php/vendor/autoload.php';
require_once dirname(__FILE__) . "/php/webApp.php";
require_once dirname(__FILE__) . '/php/util.php';
require_once dirname(__FILE__) . "/api.php";
require_once dirname(__FILE__) . "/php/digitalhigh/widget/src/widget.php";
use digitalhigh\widget\widget;
write_log("-------NEW REQUEST RECEIVED-------", "ALERT");
scriptDefaults();
buildSWCache();
$defaults = checkDefaults();
if ($defaults['migrated'] ?? false) header("Refresh:0");
$forceSSL = $defaults['forceSSL'] ?? false;
write_log("ForceSSL is ".($forceSSL ? "Enabled" : "Disabled"));
if ((empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off") && $forceSSL) {
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
write_log("Force is on, redirecting to: $redirect","ERROR");
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}
header('Cache-Control: max-age=86400');
header("Access-Control-Allow-Origin: *");
if (!session_started()) {
session_start();
}
writeSessionArray($defaults);
$GLOBALS['time'] = microtime(true);
if (substr_count($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") && hasGzip()) ob_start("ob_gzhandler"); else ob_start();
$messages = checkFiles();
if (isset($_GET['logout'])) {
clearSession();
$url = fetchUrl();
echo "<script language='javascript'>
document.location.href='$url';
</script>";
}
checkUpdate();
$code = false;
foreach ($_GET as $key => $value) {
//write_log("hey, got a key named $key with a value of $value.");
if ($key == "pinID") {
write_log("We have a PIN: $value");
$code = $value;
}
}
$user = $token = false;
$checkToken = $_SESSION['apiToken'] ?? $_GET['apiToken'] ?? false;
$bodyData = "";
if ($code || $checkToken) {
$GLOBALS['login'] = false;
if ($code) $user = plexSignIn($code);
if ($checkToken) {
$user = verifyApiToken($checkToken);
}
if ($user) $apiToken = $user['apiToken'] ?? false;
if ($apiToken) {
write_log("USER: ".json_encode($user));
define('LOGGED_IN', true);
require_once dirname(__FILE__) . '/php/body.php';
$dt = $user['darkTheme'];
write_log($dt ? "DARK" : "Light");
$darkTheme = ($user['darkTheme'] ?? false) ? '<link href="css/darkTheme.css" rel="stylesheet">' .PHP_EOL : "";
write_log("Making body!");
$defaults['token'] = $token;
$body = mainBody($defaults);
$bodyData = $body;
}
} else {
$bodyData = showLogin();
}
$execution_time = (microtime(true) - $GLOBALS['time']);
function showLogin() {
$GLOBALS['login'] = true;
$loginString = '
<div class="loginBox">
<div class="login-box">
<div class="card loginCard">
<div class="card-body">
<h4 class="card-title loginLabel">Welcome to Flex TV!</h4>
<img class="loginLogo" src="./img/phlex-med.png" alt="Card image">
<div class="card-subtitle">Please log in below to begin.</div>
<div id="loginForm">
<button class="btn btn-primary btn-wd" id="plexAuth">DO IT!</button>
<br><br>
<a href="http://flextv.us/Privacy.html" class="card-link">Privacy Policy</a>
</div>
</div>
</div>
</div>
' .
headerhtml();
return $loginString;
}
function showError() {
write_log("A new user tried to sign in, but new users are not allowed!","ERROR");
$GLOBALS['login'] = true;
$errorString = '
<div class="loginBox">
<div class="login-box">
<div class="card loginCard">
<div class="card-block">
<b><h3 class="loginLabel card-title">NOT ALLOWED!</h3></b>
<img class="loginLogo" src="./img/phlex-med.png" alt="Card image">
<h6 class="loginLabel card-subtitle text-muted">Sorry, the administrator has disabled new logins.</h6>
</div>
</div>
</div>' .
headerhtml();
return $errorString;
}
?>
<!doctype html>
<html>
<head>
<title>Flex TV</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Plex Voice Automation for Google Assistant">
<meta name="msapplication-config" content="./img/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="manifest" href="./manifest.json">
<link rel="apple-touch-icon" sizes="180x180" href="./img/apple-icon.png">
<link rel="icon" type="image/png" href="./img/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="./img/favicon-16x16.png" sizes="16x16">
<link rel="mask-icon" href="./img/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="./img/favicon.ico">
<link rel="stylesheet" href="css/loadingAnimation.css">
<!-- Material Kit/Bootstrap4 CSS, Material Icons-->
<!-- <link href="css/material-kit_custom.css?v=2.0.4" rel="stylesheet" /> -->
<link rel="stylesheet" href="https://unpkg.com/bootstrap-material-design@4.1.1/dist/css/bootstrap-material-design.min.css" integrity="sha384-wXznGJNEXNG1NFsbm0ugrLFMQPWswR3lds2VeinahP8N0zJw9VWSopbjv2x7WCvX" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Flex TV CSS Files-->
<link href="./css/main.css" rel="stylesheet">
<?php echo $darkTheme?>
<link rel="stylesheet" media="(max-width: 576px)" href="css/main_max_576.css">
<link rel="stylesheet" media="(max-width: 768px)" href="css/main_max_768.css">
<link rel="stylesheet" media="(min-width: 768px)" href="css/main_min_768.css">
<link rel="stylesheet" media="(min-width: 992px)" href="css/main_min_992.css">
<link rel="stylesheet" media="(max-width: 992px)" href="css/main_max_992.css">
<link rel="stylesheet" media="(min-width: 1200px)" href="css/main_min_1200.css">
<link rel="stylesheet" href="./css/homeBase.css" id="deferred">
<script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js" integrity="sha256-EPrkNjGEmCWyazb3A/Epj+W7Qm2pB9vnfXw+X6LImPM=" crossorigin="anonymous"></script>
<style type="text/css">
.pace {
-webkit-pointer-events: none;
pointer-events: none;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.pace-inactive {
display: none;
}
.pace .pace-progress {
background: var(--theme-accent);
position: fixed;
z-index: 2000;
top: 0;
right: 100%;
width: 100%;
height: 3px;
}
</style>
<style>
<?php echo widget::getMarkup('CSS') ?>
</style>
</head>
<body style="background-color:black">
<noscript id="deferred-styles">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" integrity="sha256-rByPlHULObEjJ6XQxW/flG2r+22R5dKiAoef+aXWfik=" crossorigin="anonymous" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.min.css" />
<link rel="stylesheet" href="css/font/font-muximux.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">
<link href="css/lib/bootstrap-iconpicker.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.3/css/bootstrap-select.min.css" integrity="sha256-q+3CS1i6GuPNz/JQk7dapdWXvii85xf7vecZx2QqsFI=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simplebar/2.6.1/simplebar.min.css" integrity="sha256-K165qkCFzUrAZsklS/OSoifAwNqxWxJ/PbYEWYUSh0I=" crossorigin="anonymous" />
</noscript>
<div class="backgrounds">
<div id="bgwrap">
</div>
<div id="weatherDiv">
<div id="tempDiv" class="meta"></div>
<div id="weatherStatus" class="row justify-content-end meta">
<div id="city" class="meta col"></div>
<div id="weatherIcon" class="meta col-1"> </div>
</div>
<div id="timeDiv" class="meta"></div>
<div id="revision" class="meta"><?php echo checkRevision(true) ?></div>
</div>
</div>
<?php
// Body data goes here
echo $bodyData;
?>
<div class="modals">
<div class="modal fade" id="alertModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="alertTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="alertBody">
<p>Modal body text goes here.</p>
</div>
</div>
</div>
</div>
</div>
<div id="loader-wrapper">
<div id="loader"></div>
<div class="loader-section section-left"></div>
<div class="loader-section section-right"></div>
</div>
<meta id="messages" data-array="<?php if (count($messages)) echo urlencode(json_encode($messages)); ?>"/>
<!-- The root of all evil-->
<script type="text/javascript" src="js/lib/jquery-3.3.1.min.js"></script>
<!-- material kit stuff -->
<script src="js/lib/popper.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/bootstrap-material-design@4.1.1/dist/js/bootstrap-material-design.js" integrity="sha384-CauSuKpEqAFajSpkdjv3z9t8E7RlpJ1UP0lKM/+NdtSarroVKu069AlsRPKkFBz9" crossorigin="anonymous"></script>
<script src="js/lib/moment.min.js"></script>
<script src="js/lib/nouislider.min.js" type="text/javascript"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script src="js/widgets.js" type="text/javascript"></script>
<!-- This is disabled until all the code works, then will be re-enabled
<script type="text/javascript">
<?php //echo widget::getMarkup('JS'); ?>
</script>
-->
<!-- Utility scripts -->
<script type="text/javascript" src="js/lib/lazyload.min.js"></script>
<script defer type="text/javascript" src="js/lib/run_prettify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha256-KM512VNnjElC30ehFwehXjx1YCHPiQkOPmqnrWtpccM=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-ui-touch-punch@0.2.3/jquery.ui.touch-punch.min.js" integrity="sha256-AAhU14J4Gv8bFupUUcHaPQfvrdNauRHMt+S4UVcaJb0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js" integrity="sha256-7/yoZS3548fXSRXqc/xYzjsmuW3sFKzuvOCHd06Pmps=" crossorigin="anonymous"></script>
<script type="text/javascript" src='//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.min.js'></script>
<script type="text/javascript" src='//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.jQueryUI.min.js'></script>
<script defer type="text/javascript" src="js/lib/jquery.simpleWeather.min.js"></script>
<!-- Icon picker -->
<script defer type="text/javascript" src="js/lib/iconset_muximux.js"></script>
<script defer type="text/javascript" src="js/lib/bootstrap-iconpicker.min.js"></script>
<!-- Snakbar, sort table, swipe to close, cache pfill for service worker -->
<script defer type="text/javascript" src="js/lib/snackbar.min.js"></script>
<script defer type="text/javascript" src="js/lib/swiped.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.8.0-rc1/Sortable.min.js" integrity="sha256-38Rua9juy4KAOXEOQyMytSVNJZQa2rL0MqaVt8yGXHE=" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/lib/cache-polyfill.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.3/js/bootstrap-select.min.js" integrity="sha256-APwNt/Yju1ZD+yBddFqLhJsk21FXdiNGUuISL4EkUs4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplebar/2.6.1/simplebar.min.js" integrity="sha256-bpLJbsEBcYeYFpadgZfuNyDGy9/IWoOEFzuVgM9QrtE=" crossorigin="anonymous"></script>
<script defer src="https://use.fontawesome.com/releases/v5.1.0/js/all.js" integrity="sha384-3LK/3kTpDE/Pkp8gTNp2gR/2gOiwQ6QaO7Td0zV76UFJVhqLl4Vl3KL1We6q6wR9" crossorigin="anonymous"></script>
<script defer src="https://code.highcharts.com/highcharts.js"></script>
<script defer src="https://code.highcharts.com/highcharts-more.js"></script>
<script defer src="https://code.highcharts.com/modules/solid-gauge.js"></script>
<script defer src="https://code.highcharts.com/modules/data.js"></script>
<!-- These are some early-stage functions that we want available right away,
even if the DOM doesn't fully load for some reason. -->
<script type="text/javascript">
var messageBox = [];
// We call this inside the login window if necessary, or main.js. Ignore lint warnings.
function loopMessages(messages) {
console.log("Function fired.");
var messageArray = messages;
messageBox = messages;
$.each(messageArray, function () {
if (messageArray[0] === undefined) return false;
var keepLooping = showMessage(messageArray[0].title, messageArray[0].message, messageArray[0].url);
messageBox.splice(0, 1);
if (!keepLooping) return false;
})
}
function showMessage(title, message, url) {
if (Notification.permission === 'granted') {
var notification = new Notification(title, {
icon: './img/avatar.png',
body: message
});
if (url) {
notification.onclick = function () {
window.open(url);
};
}
return true;
} else {
if (url !== "") {
message = "<a href='" + url + "'>"+message+"</a>";
} else {
message = "<p>" + message + "</p>";
}
if (Notification.permission !== 'denied') {
Notification.requestPermission().then(function (result) {
if ((result === 'denied') || (result === 'default')) {
$('#alertTitle').text(title);
$('#alertBody').html(message);
$('#alertModal').modal('show');
}
});
} else {
$('#alertTitle').text(title);
$('#alertBody').html(message);
$('#alertModal').modal('show');
}
return false;
}
}
// Ignore the IDE when it says this is unused, it's lying
function setBackground(last) {
var cv="";
$('#bgwrap').append("<div class='bg hidden'></div>");
var imgUrl = "https://img.flextv.media?new=true"+(last ? "&last" : "")+"&height=" + $(window).height() + "&width=" + $(window).width() + "&v=" + (Math.floor(Math.random() * (1084))) + cv;
var newDiv = $('.bg').last();
var img = $('<img src="'+imgUrl+'" class="bgImg"/>').on('load', function(){
$.when(newDiv.fadeIn(500)).done(function() {
newDiv.removeClass('hidden');
$("#bgwrap div:not(:last-child)").remove();
})
});
newDiv.append(img);
}
</script>
<?php
if ($GLOBALS['login']) {
echo '<script type="text/javascript" src="./js/login.js" async></script>';
} else {
echo '<script src="js/utilities.js"></script>';
echo '<script src="./js/main.js"></script>';
}
?>
<script>
var noWorker = true;
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js').then(function (registration) {
console.log("Service worker registered.");
noWorker = false;
}).catch(function (err) {
console.log('ServiceWorker registration failed: ', err);
});
}
if (typeof window.history.pushState === 'function') {
window.history.pushState({}, "Hide", '<?php echo $_SERVER['PHP_SELF'];?>');
}
// Load CSS deferred, because we went down the Lighthouse Audit rabbithole again...
var loadDeferredStyles = function() {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
var referenceNode = document.getElementById('deferred');
referenceNode.parentNode.insertBefore(replacement, referenceNode.nextSibling);
document.getElementById('deferred').appendChild(replacement);
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
if (raf) raf(function() { window.setTimeout(loadDeferredStyles, 0); });
else window.addEventListener('load', loadDeferredStyles);
</script>
</body>
</html>