-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
239 lines (192 loc) · 5.65 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
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
<?php
session_set_cookie_params(2592000,"/");
session_start([
'cookie_lifetime' => 2592000,
]);
$baseurl = "https://fatastreaming2.altervista.org/";
// connect to database
$db = mysqli_connect('localhost', 'fatastreaming2', '', 'my_fatastreaming2');
// variable declaration
$username = "";
$email = "";
$errors = array();
//declaring global vars
$time = date("d/m/Y - H:i:s");
$year = date("Y");
$month = date("m");
// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
register();
}
// call the login() function if register_btn is clicked
if (isset($_POST['login_btn'])) {
login();
}
// call the removeserie() function if removeserie_btn is clicked
if (isset($_POST['removeserie_btn'])) {
removeserie();
}
// call the login() function if register_btn is clicked
if (isset($_POST['removeepisode_btn'])) {
removeepisode();
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['user']);
header("location: login");
}
// REMOVE SERIE
function removeserie(){
global $db;
$serietodel = e($_POST['serietodel']);
$requestinguser = e(($_POST['requestinguser']));
if($requestinguser == $_SESSION['user']['id']) {
$query = "DELETE FROM `epseen` WHERE `serie` = $serietodel AND user = $requestinguser";
mysqli_query($db, $query);
} else {
echo "Not allowed.";
}
}
// REMOVE EPI
function removeepisode(){
global $db;
$eptodel = e($_POST['eptodel']);
$requestinguser = e(($_POST['requestinguser']));
if($requestinguser == $_SESSION['user']['id']) {
$query = "DELETE FROM `epseen` WHERE `epid` = $eptodel AND user = $requestinguser";
mysqli_query($db, $query);
} else {
echo "Not allowed.";
}
}
// REGISTER USER
function register(){
global $db, $errors;
// receive all input values from the form
$username = e($_POST['username']);
$email = e($_POST['email']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
$goto = e($_POST['goto']);
// form validation: ensure that the form is correctly filled
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
if (isset($_POST['user_type'])) {
$user_type = e($_POST['user_type']);
$query = "INSERT INTO users (username, email, user_type, password)
VALUES('$username', '$email', '$user_type', '$password')";
mysqli_query($db, $query);
$_SESSION['success'] = "New user successfully created!!";
header('location: home.php');
}else{
$query = "INSERT INTO users (username, email, user_type, password)
VALUES('$username', '$email', 'user', '$password')";
mysqli_query($db, $query);
// get id of the created user
$logged_in_user_id = mysqli_insert_id($db);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
$_SESSION['success'] = "You are now logged in";
// view.php specific redirect if there's a goto link
if(empty($goto)){
header('location: /');
} else {
header('location: view?' . $goto . '');
}
header('location: /');
}
}
}
// return user array from their id
function getUserById($id){
global $db;
$query = "SELECT * FROM users WHERE id=" . $id;
$result = mysqli_query($db, $query);
$user = mysqli_fetch_assoc($result);
return $user;
}
// LOGIN USER
function login(){
global $db, $username, $goto, $errors;
// grap form values
$username = e($_POST['username']);
$password = e($_POST['password']);
$goto = e($_POST['goto']);
// make sure form is filled properly
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// attempt login if no errors on form
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
if ($logged_in_user['user_type'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: admin/index.php');
}else{
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
// view.php specific redirect if there's a goto link
if(empty($goto)){
header('location: index.php');
} else {
header('location: view?' . $goto . '');
}
}
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
function isLoggedIn()
{
if (isset($_SESSION['user'])) {
return true;
}else{
return false;
}
}
function isAdmin()
{
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' ) {
return true;
}else{
return false;
}
}
// escape string
function e($val){
global $db;
return mysqli_real_escape_string($db, trim($val));
}
function display_error() {
global $errors;
if (count($errors) > 0){
echo '<div class="error">';
foreach ($errors as $error){
echo $error .'<br>';
}
echo '</div>';
}
}
?>