-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic-link.php
289 lines (216 loc) · 8.72 KB
/
magic-link.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
<?php
require_once 'app/init/init-login.php';
require_once 'landing/tracking.php';
session_start();
// If the session is set, is redirects the user to the app
if (isset($_SESSION["suite"])) {
header('Location: app/index.php');
}
// Checks if the magic link feature is enabled.
if($magiclinken=='false'){
header('Location: error.php?err=magiclink');
}
if($_GET['pg']=='linkclicked'){
if(isset($_GET['otk'])){
$otk = $_GET['otk'];
$usersget = $db->prepare("SELECT * FROM `passwordlogin` WHERE `otk` = :otk");
$usersget->execute([
'otk' => $otk,
]);
$users = $usersget->rowCount() ? $usersget : [];
foreach($users as $item){
// Sets to null
$nullquery = $db->prepare("
UPDATE passwordlogin SET otk = NULL WHERE username = :username
");
$nullquery->execute([
':username' => $item['username'],
]);
// Assigned the session as the username/account email
$_SESSION["suite"] = $item['username'];
// Gets the cookie and encrypts it
$token_cookie = base64_encode(openssl_encrypt($item["token"], $method, $key, OPENSSL_RAW_DATA, $iv));
setcookie('token',$token_cookie,time()+604800); // 86400 = 1 day 604800
// Regenerates the session code
session_regenerate_id(true);
// Sends the user to the app
header('Location: app/index.php');
// Makes log quert
$logQuery = $db->prepare("
INSERT INTO tasks_log (account, content, loginip, logindevice, loginos, loginbrowser, date)
VALUES (:account, :content, :loginip, :logindevice, :loginos, :loginbrowser, :date)
");
$content = 'Successful login - via magic link';
$logQuery->execute([
':account' => $item["accountid"],
':content' => $content,
':date' => $logdate,
':loginip' => $loginip,
':logindevice' => $logindevice,
':loginos' => $loginos,
':loginbrowser' => $loginbrowser,
]);
}
}
}
require 'app/plugins/mailjet/vendor/autoload.php';
use \Mailjet\Resources;
// Handles everything for the user to login
if (isset($_POST["login"])) {
if (empty($_POST["username"])) {
// What happens if none or just one of the fields are entered.
echo '<script>alert("Please type your email")</script>';
} else {
$formemail = $_POST['username'];
$usersget = $db->prepare("SELECT * FROM `passwordlogin` WHERE `username` = :account");
$usersget->execute([
'account' => $formemail,
]);
$users = $usersget->rowCount() ? $usersget : [];
foreach($users as $item){
// Magic link key
$str = rand();
$key = hash("sha256", $str);
// Set the magic key
$logQuery = $db->prepare("
UPDATE passwordlogin SET otk = :otk WHERE username = :username
");
$logQuery->execute([
':username' => $formemail,
':otk' => $key
]);
// Sends the email
$mj = new \Mailjet\Client(''.$apipublic.'',''.$apiprivate.'',true,['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => "".$fromemail."",
'Name' => "".$fromname.""
],
'To' => [
[
'Email' => "".$formemail."",
'Name' => ""
]
],
'Subject' => "Login - Magic Link",
'TextPart' => "Login - Magic Link",
'HTMLPart' => '<p>Wasn\'t you? Maybe change your password but don\'t click the link.</p><br><a href="https://'.$rootwebsite.'/magic-link.php?pg=linkclicked&otk='.$key.'">Log me in!</a>',
'CustomID' => "Login - Magic Link"
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());
// Makes log query
$logQuery = $db->prepare("
INSERT INTO tasks_log (account, content, loginip, logindevice, loginos, loginbrowser, date)
VALUES (:account, :content, :loginip, :logindevice, :loginos, :loginbrowser, :date)
");
$content = 'Magic link sent';
$logQuery->execute([
':account' => 'SYSTEM',
':content' => $content,
':date' => $logdate,
':loginip' => $loginip,
':logindevice' => $logindevice,
':loginos' => $loginos,
':loginbrowser' => $loginbrowser,
]);
header('Location: ?pg=sent');
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>-->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<meta content="text/html; charset=UTF-8; X-Content-Type-Options=nosniff" http-equiv="Content-Type" />
<link rel="shortcut icon" type="image/png" href="app/icons/favicon.png"/>
<!--Scripts-->
<link href="app/plugins/fa/css/all.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://use.typekit.net/rdi8jbs.css">
<!--iOS PWA Compatability-->
<link rel="apple-touch-icon" href="icons/favicon.png">
<meta name="apple-mobile-web-app-title" content="Tasks">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta name="apple-mobile-web-app-capable" content="yes">
</head>
<body>
<div class="main">
<div class="overlay-color">
<div class="container">
<div class="row">
<div class="column left">
<div class="left-content">
<img src="app/icons/Landing-Light.svg" class="logo">
</div>
</div>
<div class="column right">
<div class="login-container">
<div class="login-form">
<?php if($_GET["pg"] == "send"): ?>
<div class="left-force">
<img src="app/icons/Landing-Dark.svg" class="panel-logo">
<h2 class="main-txt">Magic Link</h2>
<p>Type your email below and we'll email you a magic link to login with. <a href="login.php" class="link">Login with my password instead.</a></p>
</div>
<br>
<form method="post">
<div class="input-section">
<label for="username" class="form-label">Email</label>
<input type="email" name="username" required class="form-control-new" placeholder="[email protected]"/>
</div>
<br>
<div class="align-right-container">
<div class="align-right">
<input type="submit" name="login" value="Send Link" class="btn btn-info" />
</div>
</div>
<br><br><br>
<div class="center">
<a href="privacy-terms.html" class="link">Privacy Policy and Terms of Service</a>
</div>
</form>
<?php endif; ?>
<?php if($_GET["pg"] == "sent"): ?>
<div class="left-force">
<img src="app/icons/Landing-Dark.svg" class="panel-logo">
<h2 class="main-txt">Welcome to Tasks 👋</h2>
<p>We sent the email. Go check your inbox.</p>
</div>
<?php endif; ?>
<?php if($_GET["pg"] == "linkclicked"): ?>
<div class="left-force">
<img src="app/icons/Landing-Dark.svg" class="panel-logo">
<h2 class="main-txt">Welcome to Tasks 👋</h2>
<p>We're working to sign you in right now!</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="notice">
<p><i class="fab fa-unsplash"></i> Backgrounds from Unsplash</p>
</div>
</div>
</div>
</div>
</body>
</html>
<style>
<?php include('landing/login-v2.css'); ?>
</style>