/
index.php
170 lines (156 loc) · 7.1 KB
/
index.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
<?php
require 'includes/init.php';
$prisjakt = new Prisjakt([
'user_id' => $_SESSION['prisjakt_user_id'] ?? '',
'password_hash' => $_SESSION['prisjakt_password_hash'] ?? ''
]);
$out = [];
if (isset($_POST['submit'])) {
if (hasApiCredentials()) {
$searchUrl = $_POST['prisjakt_search_url'] ?? '';
$priceThreshold = floatval($_POST['prisjakt_price_threshold'] ?? 0);
if ($searchUrl) {
$products = $prisjakt->getProductsInSearch($searchUrl);
foreach ($products as $productId => $product) {
if ($_POST['submit'] === 'add') {
$price = ceil(floatval($product['price'] * (1 - $priceThreshold)));
$out[] = 'Adding alert for <b>' . $product['name']. '</b> (price set to ' . $price . ')...';
$prisjakt->addAlertForProduct($productId, [ 'price_threshold' => floatval(( 1 - $priceThreshold) * $product['price']) ]);
} else {
$out[] = 'Removing alert for <b>' . $product['name']. '</b>...';
$prisjakt->removeAlertForProduct($productId);
}
}
} else {
$out[] = 'No search URL provided';
}
} else {
$username = $_POST['prisjakt_username'] ?? '';
$password = $_POST['prisjakt_password'] ?? '';
$sessionData = $prisjakt->login($username, $password);
if (count($sessionData)) {
$_SESSION['prisjakt_username'] = $username;
$_SESSION['prisjakt_user_id'] = $sessionData['user_id'];
$_SESSION['prisjakt_password_hash'] = $sessionData['password_hash'];
session_write_close();
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
$out[] = 'Unable to login user <b> ' . $username . '</b>';
}
}
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
unset($_SESSION['prisjakt_username']);
unset($_SESSION['prisjakt_user_id']);
unset($_SESSION['prisjakt_password_hash']);
session_write_close();
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
$discountSteps = [5, 10, 25, 50, 75, 90];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Augmented Price Tracking on Prisjakt™</title>
<link href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" type="text/css" />
<link href="//cdn.muicss.com/mui-0.9.24/css/mui.min.css" rel="stylesheet" type="text/css" />
<script src="//cdn.muicss.com/mui-0.9.24/js/mui.min.js"></script>
<style>
body {
font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif;
background: #f0f0f0;
}
.status-message {
background: rgba(255, 64, 129, 0.25);
padding: 8px;
margin-bottom: 16px;
margin-left: 16px;
margin-right: 16px;
margin: 0 auto;
}
.toolbar {
height: 48px;
line-height: 48px;
background: #2196F3;
padding: 0 16px;
color: #fff;
}
.toolbar-actions {
float: right;
list-style-type: none;
text-align: right;
}
.toolbar-actions li {
padding: 0 8px;
}
.toolbar-actions li:hover {
background: rgba(255, 255, 255, 0.2);
}
.username-link {
color: #FFF;
display: inline-block;
text-decoration: none;
}
.username-link:hover {
text-decoration: none;
}
</style>
</head>
<body>
<div class="toolbar">
<span class="mui--text-title">Augmented Prisjakt</span>
<?php if (hasApiCredentials()) { ?>
<ul class="toolbar-actions">
<li><a class="username-link" href="<?= $_SERVER['PHP_SELF'] ?>?action=logout">Sign out <strong><?= $_SESSION['prisjakt_username'] ?></strong></a></li>
</ul>
<?php } ?>
</div>
<?php if (count($out)) { ?>
<div class="mui-container" style="margin-top: 16px">
<div class="mui-container status-message">
<div class="mui--text-accent">
<?= implode('<br>', $out) ?>
</div>
</div>
</div>
<?php } ?>
<div class="mui-container" style="margin-top: <?= count($out) ? 16 : 32 ?>px">
<div class="mui-panel">
<form class="mui-form" method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
<?php if (hasApiCredentials()) { ?>
<div class="mui-textfield">
<input name="prisjakt_search_url" type="url" placeholder="Ex. https://www.prisjakt.nu/#rparams=ss=NOCCO%20Focus" required>
<label>Prisjakt search URL</label>
</div>
<div class="mui-select" style="width: 240px">
<select name="prisjakt_price_threshold">
<option value="0" selected>Any discount</option>
<?php foreach ($discountSteps as $step) { ?>
<option value="<?= $step / 100 ?>"><?= $step ?>%</option>
<?php } ?>
</select>
<label>How much discount are you waiting for?</label>
</div>
<button name="submit" class="mui-btn mui-btn--primary mui-btn--raised" type="submit" value="add">Track products</button>
<button name="submit" class="mui-btn mui-btn--accent mui-btn--raised" type="submit" value="remove">Untrack products</button>
<?php } else { ?>
<div class="mui--text-title" style="margin-bottom: 8px">Sign in to Prisjakt</div>
<div class="mui-textfield mui-textfield--float-label">
<input name="prisjakt_username" required>
<label>Username</label>
</div>
<div class="mui-textfield mui-textfield--float-label">
<input name="prisjakt_password" type="password" required>
<label>Password</label>
</div>
<button name="submit" class="mui-btn mui-btn--accent mui-btn--raised" type="submit">Sign in</button>
<?php } ?>
</form>
</div>
</div>
</body>
</html>