-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.php
149 lines (127 loc) · 3.32 KB
/
connection.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
<?php
$login_regex = "/^[a-zA-Z0-9]{3,20}$/";
$password_regex = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/";
function connectToDatabase()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depajratbej";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
return $conn;
}
function getAccount($conn, $data)
{
$query = "SELECT * FROM users WHERE id = '$data' OR username = '$data' OR email = '$data'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// Account found, return the data
return mysqli_fetch_assoc($result);
} else {
// Account not found, return false
return false;
}
}
function getAccountByLogin($conn, $login)
{
$query = "SELECT * FROM users WHERE username = '$login'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// Account found, return the data
return mysqli_fetch_assoc($result);
} else {
// Account not found, return false
return false;
}
}
function getAccountByEmail($conn, $email)
{
$query = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// Account found, return the data
return mysqli_fetch_assoc($result);
} else {
// Account not found, return false
return false;
}
}
function loginUser($conn, $login)
{
$data = getAccount($conn, $login); // get account data
// check if data exists
if (!$data) {
return false;
}
// set session variables
$_SESSION['login'] = $login;
$_SESSION['id'] = $data['id'];
$_SESSION['email'] = $data['email'];
$_SESSION['profile_pic'] = $data['profile_pic'];
$_SESSION['logged_in'] = true;
// redirect
header('Location: account.php');
}
function getCategories($conn)
{
$query = "SELECT * FROM categories";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// Categories found, return the data
return mysqli_fetch_all($result, MYSQLI_ASSOC);
} else {
// Categories not found, return false
return false;
}
}
function logoutUser()
{
// remove all session variables
session_unset();
// destroy the session
session_destroy();
// redirect
header('Location: login.php');
}
function loginRedirect()
{
// if user arleady logged in, redirect to account.php
if (isUserLoggedIn() == true) {
header('Location: account.php');
exit();
}
}
function notLoggedInRedirect()
{
// if user is not logged in, redirect to login.php
if (isUserLoggedIn() == false) {
header('Location: login.php');
exit();
}
}
function isUserLoggedIn()
{
return (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) ? true : false;
}
function getLoginRegex()
{
return $GLOBALS['login_regex'];
}
function getPasswordRegex()
{
return $GLOBALS['password_regex'];
}
// accesirues
function printArrayToConsole($array)
{
echo '<script>';
echo 'console.log(' . json_encode($array) . ')';
echo '</script>';
}
function toNiceDate($date)
{
return date('d.m.Y', strtotime($date));
}