This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·157 lines (131 loc) · 4.8 KB
/
functions.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
<?php //functions.php
// if URL is functions.php redirect to index
if ( $_SERVER['PHP_SELF'] == "/login/functions.php" )
{
header ("Location: index.php");
die;
}
function dbfetch($db,$query,$query_params=[])
{
//try
//{
$stmt = $db->prepare($query);
$stmt->execute($query_params);
//}
//catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); }
return $stmt->fetch();
}
function dbfetchColumn($db,$query,$query_params=[])
{
//try
//{
$stmt = $db->prepare($query);
$stmt->execute($query_params);
//}
//catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); }
return $stmt->fetchColumn();
}
function dbfetchAll($db,$query,$query_params=[])
{
//try
//{
$stmt = $db->prepare($query);
$stmt->execute($query_params);
//}
//catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); }
return $stmt->fetchAll();
}
function dbinsert($db,$query,$query_params=[])
{
//try
//{
$stmt = $db->prepare($query)->execute($query_params);
//$stmt = $db->prepare($query);
//$stmt->execute($query_params);
return $stmt;
//}
//catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); }
//$stmt->fetchAll();
}
function generateToken($length = 20)
{
return bin2hex(random_bytes($length));
}
function createnewloginCookie($userid, $db)
{
$email = $userid; // = $_SESSION['user']['email'];
//$currentdomain = $_SERVER['HTTP_HOST'];
$Selector = md5(date('Ydmhis', time()).$email);
$newToken = generateToken();
$cookiedata = $Selector.":".$newToken;
setcookie('RememberMeCookie',$cookiedata,time()+(3600*24*15),"/","example.com",true,true);
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$tokenhash = hash('sha256', $newToken . $salt);
for($round = 0; $round < 65536; $round++)
{
$tokenhash = hash('sha256', $tokenhash . $salt);
}
$query = " INSERT INTO RememberMe(email,Selector,RememberHash,RememberSalt) VALUES (:email, :Selector, :RememberHash, :RememberSalt) ";
$query_params = array ( ':email' => $email ,':Selector' => $Selector , ':RememberHash' => $tokenhash, ':RememberSalt' => $salt );
dbinsert($db,$query,$query_params);
}
// Working to here
function checkcookietokenandupdate($cookieraw, $db)
{
$CookieExtracted = explode(":", $cookieraw);
$Selector = $CookieExtracted[0];
$Validator = $CookieExtracted[1];
$query = " SELECT email, Selector, RememberHash, RememberSalt FROM RememberMe WHERE Selector = :Selector ";
$query_params = array( 'Selector' => $Selector );
$row = dbfetch($db,$query,$query_params);
if ($row)
{
$check_token = hash('sha256', $Validator . $row['RememberSalt']);
for($round = 0; $round < 65536; $round++)
{
$check_token = hash('sha256', $check_token . $row['RememberSalt']);
}
if ($check_token === $row['RememberHash'])
{
$_SESSION['user']['email'] = $row['email'];
$newToken = generateToken();
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$tokenhash = hash('sha256', $newToken . $salt);
for($round = 0; $round < 65536; $round++)
{
$tokenhash = hash('sha256', $tokenhash . $salt);
}
$query = " UPDATE RememberMe SET RememberHash = :RememberHash, RememberSalt = :RememberSalt WHERE Selector = :Selector ";
$query_params = array ( ':RememberHash' => $tokenhash,':RememberSalt' => $salt, ':Selector' => $Selector );
$UpdateToken = dbinsert($db,$query,$query_params);
if ($UpdateToken)
{
$cookiedata = $Selector.":".$newToken;
setcookie('RememberMeCookie',$cookiedata,time()+(3600*24*15),"/","example.com",true,true);
}
if ( $_SERVER['PHP_SELF'] == "/login/index.php" )
{
header("Location: private");
die;
}
}
else // If token's don't match
{
if ( $_SERVER['PHP_SELF'] != "/login/index.php" )
{
setcookie('RememberMeCookie',"",1,"/","example.com",true,true);
header("Location: index.php");
die;
}
}
}
else
{
if ( $_SERVER['PHP_SELF'] != "/login/index.php" )
{
setcookie('RememberMeCookie',"",1,"/","example.com",true,true);
header("Location: index.php");
die;
}
}
}