-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
339 lines (226 loc) · 8.84 KB
/
index.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
session_start();
// define("HOME", 'http://'.$_SERVER['HTTP_HOST'].pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME).'/');
define("HOME", 'http://'.$_SERVER['HTTP_HOST'].pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME).'/');
define("ASSETS", HOME.'assets/');
define("CSS", HOME.'css/');
define("JS", HOME.'js/');
define("IMAGES", HOME.'images/');
define("FONTS", HOME.'fonts/');
$page = isset($_GET['page'])?$_GET['page']:'';
$page = rtrim($page, '/');
require_once 'include/functions.php';
require_once '../include/database.php';
// if(check_notification()){
// notify();
// }
function set_status($type, $message){
$_SESSION['status']['type'] = $type;
$_SESSION['status']['message'] = $message;
}
function display_status(){
if(isset($_SESSION['status'])){
$status = $_SESSION['status'];
if($status['type'] == 'success'){
echo "<div style='text-align: center; font-type: tahoma; color: green;'>".$status['message']."</div>";
}else{
echo "<div style='text-align: center; font-type: tahoma; color: red;'>".$status['message']."</div>";
}
unset($_SESSION['status']);
}
}
if($page == ''){
$query = $conn->prepare("SELECT * FROM cuberfluxadmin");
$query->execute();
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['contents'])){
$add_comment = $conn->prepare("INSERT INTO cuberfluxadmin(contents) VALUES(:contents)");
$add_comment->bindParam(':contents', $_POST['contents']);
$add_comment->execute();
if($add_comment->rowCount() > 0){
//set_status('success', 'The comment "'.$add_comment['contents'].'" by '.$insert['contents'].' was deleted successfully.');
display_status("The comment '".$_POST['contents']."' has been successfully added.", 'success');
}else{
display_status("The comment '".$_POST['contents']."' was not added.", 'error');
}
}else{
display_status("Please fill all fields", 'error');
}
}
require 'home.php';
//IF PAGE IS THE CONTACT PAGE
}elseif($page == 'add_posts'){
redirect_no_login();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['name']) && !empty($_POST['topic']) && !empty($_POST['mess'])){
$insert = $conn->prepare("INSERT INTO cuberfluxadmin(name, topic, mess) VALUES(:name, :topic, :mess)");
$insert->bindParam(':name', $_POST['name']);
$insert->bindParam(':topic', $_POST['topic']);
$insert->bindParam(':mess', $_POST['mess']);
$insert->execute();
if($insert->rowCount() > 0){
display_status("The Note '".$_POST['name']."' has been successfully added.", 'success');
}else{
display_status("The Note '".$_POST['topic']."' was not added.", 'error');
}
}else{
display_status("Please fill all fields", 'error');
}
}
require 'views/add_posts.php';
// die();
}elseif($page == 'view_all_posts'){
$query = $conn->prepare("SELECT * FROM cuberfluxadmin");
$query->execute();
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
require 'views/view_all_posts.php';
}elseif ($page =='add_comment') {
require 'views/add_comment.php';
}elseif ($page == 'post') {
$query = $conn->prepare("SELECT * FROM cuberfluxadmin");
$query->execute();
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
require 'post.php';
}elseif($page == 'delete_post'){
////redirect_no_login();
$id = @$_GET['id'];
if ($id != '') {
//CHECK THAT THE NOTE EXISTS
$insert = $conn->prepare("SELECT * FROM cuberfluxadmin WHERE id = :id");
$insert->bindParam(':id', $id);
$insert->execute();
$insert = $insert->fetch(PDO::FETCH_ASSOC);
//IF NOTE DOES NOT EXITS, REDIRECT TO APP HOME
if($insert == false){
set_status('error', 'The note you wanted to delete does not exist.');
header('location:'.HOME.'view_all_posts');
die();
}
$delete = $conn->prepare("DELETE FROM cuberfluxadmin WHERE id = :id");
$delete->bindParam(':id', $id);
$delete->execute();
set_status('success', 'The note "'.$insert['topic'].'" by '.$insert['name'].' was deleted successfully.');
}
header('location:'.HOME.'view_all_posts');
}elseif ($page == 'edit_post') {
// redirect_no_login();
$id = @$_GET['id'];
if ($id != '') {
//CHECK IF FORM IS SUBMITTED
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//CHECK THAT THE FIELDS ARE NOT EMPTY
// if(@$_POST['name'] !='' ){
if(!empty($_POST['name']) && !empty($_POST['topic']) && !empty($_POST['mess'])){
$edit_post = $conn->prepare("UPDATE cuberfluxadmin SET name = :name, topic = :topic, mess = :mess WHERE id = :id");
$edit_post->bindParam(':name', $_POST['name']);
$edit_post->bindParam(':topic', $_POST['topic']);
$edit_post->bindParam(':mess', $_POST['mess']);
$edit_post->bindParam(':id', $id);
if ($edit_post->execute()) {
$posts = $_POST;
$posts['id'] = $id;
}else{
set_status('success', 'The note "'.$posts['topic'].'" by '.$posts['name'].' was deleted successfully.');
}
}else{
set_status('error', 'Please fill all fields.');
}
}
//CHECK THAT THE NOTE EXISTS
$posts = $conn->prepare("SELECT * FROM cuberfluxadmin WHERE id = :id");
$posts->bindParam(':id', $id);
$posts->execute();
$posts = $posts->fetch(PDO::FETCH_ASSOC);
//IF NOTE DOES NOT EXITS, REDIRECT TO APP HOME
if($posts == false){
set_status('error', 'The note you wanted to edit does not exist.');
header('location:'.HOME.'view_all_posts');
die();
}
}
require 'views/edit_post.php';
}elseif ($page =='about') {
require_once 'about.php';
}elseif ($page == 'services') {
require_once 'services.php';
}elseif ($page =='contact') {
$query = $conn->prepare("SELECT * FROM user");
$query->execute();
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['username']) && !empty($_POST['lastname']) && !empty($_POST['username']) && !empty($_POST['password'])){
$new_user = $conn->prepare("INSERT INTO user(username, lastname, useremail, password) VALUES(:username, :lastname, :useremail, :password)");
$new_user->bindParam(':username', $_POST['username']);
$new_user->bindParam(':lastname', $_POST['lastname']);
$new_user->bindParam(':useremail', $_POST['useremail']);
$new_user->bindParam(':password', $_POST['password']);
$new_user->execute();
if($new_user->rowCount() > 0){
display_status("The Note '".$_POST['name']."' has been successfully added.", 'success');
}else{
display_status("The Note '".$_POST['topic']."' was not added.", 'error');
}
}else{
display_status("Please fill all fields", 'error');
}
}
require_once 'contact.php';
}elseif ($page =='posts_tables') {
$notes = $conn->prepare("SELECT * FROM cyberfluxadmin");
$notes->execute();
$notes = $notes->fetchAll(PDO::FETCH_ASSOC);
require 'views/posts_tables.php';
}elseif ($page =='services_table') {
require 'views/services_table.php';
}elseif($page == 'login'){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['username']) && $_POST['username'] != '' && isset($_POST['password']) && $_POST['password'] != ''){
$users = $conn->prepare("SELECT * FROM cyberfluxadmin WHERE username = :username");
$users->bindParam(':username', $_POST['username']);
$users->execute();
$users = $users->fetch(PDO::FETCH_ASSOC);
if($users != false){
if(password_verify($_POST['pas'], $users['pas'])){
$_SESSION['username'] = $_POST['username'];
header('location:'.HOME.'add_posts');
}else{
display_status("Password is incorrect", 'error');
}
}else{
display_status("Username does not exist", 'error');
}
}else{
display_status("Please fill all fields", 'error');
}
}
require 'about.php';
// die();
}elseif ($page == 'register') {
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['username']) && !empty($_POST['pas'])){
if($users == false){
$_POST['pas'] = password_hash($_POST['pas'], PASSWORD_DEFAULT);
$insert = $conn->prepare("INSERT INTO cuberfluxadmin(username, pas) VALUES(:username, :pas)");
$insert->bindParam(':username', $_POST['username']);
$insert->bindParam(':pas', $_POST['pas']);
$insert->execute();
if($insert->rowCount() > 0){
display_status("Welcome '".$_POST['username']."' has been successfully added.", 'success');
$_SESSION['username'] = $_POST['username'];
header('location:'.HOME. 'add_posts');
}else{
display_status("The Note '".$_POST['pas']."' was not added.", 'error');
}
}else{
display_status("Please fill all fields", 'error');
}
}
}
require 'views/register.php';
}elseif ($page =='logout') {
logout();
require 'views/logout.php';
}else{
require 'views/404.php';
}