-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbLogic.php
318 lines (236 loc) · 9.41 KB
/
dbLogic.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// require 'phpmailer/src/Exception.php';
// require 'phpmailer/src/PHPMailer.php';
// require 'phpmailer/src/SMTP.php';
require 'config.php';
// establishing a connection to the database
$conn = mysqli_connect($database['host'], $database['username'], $database['password'], $database['database']);
// Checking if the connection is established or not
if (!$conn) {
echo "<h3>Not able to establish Database connection</h3>";
}
// fetching all the blogs from database
$sql = "SELECT * FROM blogsdata WHERE status = 1 ORDER BY id DESC";
$query = mysqli_query($conn, $sql);
// inserting the new blog into database
if (isset($_REQUEST['new_post'])) {
$blog_image = $_FILES['blog__img'];
$filename = $blog_image['name'];
$filename_tmp = $blog_image['tmp_name'];
$profile_ext = explode('.', $filename);
$filecheck = strtolower(end($profile_ext));
$file_ext_stored = array('jpeg', 'png', 'jpg');
if (in_array($filecheck, $file_ext_stored)) {
$destinationFile = "uploads/" . $filename;
move_uploaded_file($filename_tmp, $destinationFile);
} else {
$destinationFile = null;
}
$title = $_REQUEST['title'];
$content = $_REQUEST['editor1'];
$category = $_REQUEST['blog__topic'];
$userId = $_REQUEST['userId'];
$sql_query = $conn->prepare("INSERT INTO blogsdata(title, content, user_id, blog_image, category) VALUES (?, ?, ?, ?, ?)");
$sql_query->bind_param("ssiss", $title, $content, $_SESSION['uid'], $destinationFile, $category);
$sql_query->execute();
header("Location: index.php?info=added");
exit();
}
if (isset($_REQUEST['id'])) {
$id = mysqli_real_escape_string($conn, $_REQUEST['id']);
$sql = "SELECT * FROM blogsdata WHERE id = '$id' AND status = 1";
$query = mysqli_query($conn, $sql);
}
if (isset($_REQUEST['update'])) {
$id = mysqli_real_escape_string($conn, $_REQUEST['id']);
$title = mysqli_real_escape_string($conn, $_REQUEST['title']);
$content = mysqli_real_escape_string($conn, $_REQUEST['editor1']);
// using mysql prepared statement
$stmt = $conn->prepare("UPDATE blogsdata SET title = ?, content = ?
WHERE id = ?");
$stmt->bind_param("ssi", $title, $content, $id);
$stmt->execute();
header("Location: index.php?info=updated");
exit();
}
if (isset($_REQUEST['delete'])) {
$id = mysqli_real_escape_string($conn, $_REQUEST['id']);
$stmt = $conn->prepare("SELECT blog_image FROM blogsdata WHERE id = ? AND status = 1");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$ans = $result->fetch_assoc();
$filename = $ans['blog_image'];
if (isset($filename)) {
// deletes the image from /uploads as well
unlink($filename);
}
$stmt = $conn->prepare("DELETE FROM blogsdata WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
header("Location: index.php?info=deleted");
exit();
}
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['new_user'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$pwd = $_POST['password'];
$con_pwd = $_POST['confirmPassword'];
// Use prepared statements to prevent SQL injection
$query = "SELECT email FROM userdetails WHERE email = ? AND status = 1";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$num = $result->num_rows;
if ($num >= 1) {
header("Location: register.php?info=present");
exit();
}
if ($pwd === $con_pwd) {
$hashed_password = password_hash($pwd, PASSWORD_DEFAULT);
$bio = '';
// Use prepared statements to prevent SQL injection
$sql_query = "INSERT INTO userdetails (name, email, avatar, password, bio, date) VALUES (?, ?, 'avatar/avatar.png', ?, ?, current_timestamp())";
$stmt = $conn->prepare($sql_query);
$stmt->bind_param("ssss", $name, $email, $hashed_password, $bio);
$stmt->execute();
header("Location: login.php?info=registered");
exit();
} else {
header("Location: register.php?info=error");
exit();
}
}
if (isset($_POST['profile__btn'])) {
$bio = trim($_POST['profile__bio']);
$profile_id = intval($_POST['profile__user_id']);
if (empty($bio) || empty($profile_id)) {
// Handle error here
exit;
}
// Using Mysql prepared statement
$sql = $conn->prepare("UPDATE userdetails SET bio = ? WHERE user_id = ?");
$sql->bind_param("si", $bio, $profile_id);
$sql->execute();
if ($sql->affected_rows === 1) {
header("Location: profile.php?uid=$profile_id");
exit;
} else {
// Handle error here
exit;
}
}
if (isset($_POST['new_pass'])) {
$pass_email = $_POST['pass_email'];
$sql = $conn->prepare("SELECT user_id FROM userdetails WHERE email = ?");
$sql->bind_param("s", $pass_email);
$sql->execute();
$res = $sql->get_result();
$ans = $res->fetch_assoc();
$userId = $ans['user_id'];
if (empty($userId)) {
header("Location: fpemail.php?info=invalid");
exit;
}
$mail = new PHPMailer(true);
$mail -> isSMTP();
$mail -> Host = 'smtp.gmail.com';
$mail -> SMTPAuth = true;
$mail -> Username = 'user_email';
$mail -> Password = 'user_pass';
$mail -> SMTPSecure = 'ssl';
$mail -> Port = 465;
$mail -> setFrom('user_email', 'BlogsConn');
$mail -> addAddress($pass_email);
$mail -> Subject = 'Password Reset - BlogsConn';
$body = '<div>
<h2>BlogIt - Password Reset</h2>
</div>' .
'<p style = "font-size: 1.4rem"> To reset password, <a style = "text-decoration:none" href = "http://localhost/BlogsConn/passwordReset.php?id=' . $userId . '"> Click here </a></p>';
$mail -> MsgHTML($body);
$result = $mail -> Send();
if(!$result){
echo "Mailer Error: " . $mail -> ErrorInfo;
}
else{
// Adding time interval
$release = date("H:i:s");
$endTime = strtotime("+15 minutes", strtotime($release));
$expire = date('H:i:s', $endTime);
$sql = $conn->prepare("INSERT INTO forgotpassword(user_id, release_time, expire_time) VALUES($userId, '$release', '$expire')");
$sql->execute();
// redirecting to the home page
header("Location: fpemail.php?info=sent");
echo 'Message has been sent';
exit();
}
}
// // password reset
if (isset($_POST['pass_reset'])) {
$pass = $_POST['password'];
$con_pass = $_POST['confirmPassword'];
$userId = $_POST['user-id'];
// fetching expire time
$sql = $conn->prepare("SELECT * FROM forgotpassword WHERE user_id = ?");
$sql->bind_param("i", $userId);
$sql->execute();
$res = $sql->get_result();
$ans = $res->fetch_assoc();
$current_time = date("H:i:s");
$expire_time = $ans['expire_time'];
// checking if the time interval is valid
if ($current_time > $expire_time) {
$sql = $conn->prepare("DELETE FROM forgotpassword WHERE user_id = ?");
$sql->bind_param("i", $userId);
$sql->execute();
header("Location: fpemail.php?info=expired");
exit;
}
if ($pass == $con_pass) {
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$sql = $conn->prepare("UPDATE userdetails SET password = ? WHERE user_id = ?");
$sql->bind_param("si", $hashed_password, $userId);
$sql->execute();
header("Location: login.php?info=resetdone");
exit;
}
}
// if (isset($_POST['pass_reset'])) {
// $pass = $_POST['password'];
// $con_pass = $_POST['confirmPassword'];
// $userId = $_POST['user-id'];
// // fetching expire time
// $sql = $conn->prepare("SELECT * FROM forgotpassword WHERE user_id = ?");
// $sql->bind_param("i", $userId);
// $sql->execute();
// $res = $sql->get_result();
// $ans = $res->fetch_assoc();
// $current_time = date("H:i:s");
// $expire_time = $ans['expire_time'];
// // checking if the time interval is valid
// if ($current_time > $expire_time) {
// $sql = $conn->prepare("DELETE FROM forgotpassword WHERE user_id = ?");
// $sql->bind_param("i", $userId);
// $sql->execute();
// header("Location: fpemail.php?info=expired");
// exit;
// }
// if ($pass == $con_pass) {
// $hashed_password = password_hash($pass, PASSWORD_DEFAULT);
// $sql = $conn->prepare("UPDATE userdetails SET password = ? WHERE user_id = ?");
// $sql->bind_param("si", $hashed_password, $userId);
// $sql->execute();
// header("Location: login.php?info=resetdone");
// exit;
// } else {
// header("Location: passwordReset.php?id=$userId&info=mismatch");
// exit;
// }
// }