-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
704 lines (435 loc) · 31.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
# ══════════════════════════════════════════════════════════════════════════════════════════════════
#╔═════════════════════════════════════════════════╗
#║ ║
#║ ---| PAGE CONFIGURATION |--- ║
#║ ║
#╚═════════════════════════════════════════════════╝
require_once('./include/config.inc.php');
require_once('./include/form.inc.php');
require_once('./include/db.inc.php');
require_once('./include/dateTime.inc.php');
# ==================================================================================================
#╔═════════════════════════════════════════════════╗
#║ ║
#║ ---| SECURE PAGE ACCESS |--- ║
#║ ║
#╚═════════════════════════════════════════════════╝
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Session start...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> PREPARE SESSSION <----═════════
session_name('wwwblogprojektmahboubede');
//══════════----> START SESSION <----═════════
session_start();
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$_SESSION <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($_SESSION);
// if(DEBUG_A) echo "</pre>";
//══════════----> CHECK FOR VALID LOGIN <----═════════
if( isset($_SESSION['ID']) === false OR $_SESSION['IPAddress'] !== $_SERVER['REMOTE_ADDR'] ) {
// if(DEBUG) echo "<p class='debug auth err'><b>Line " . __LINE__ . "</b>: Error: Invalid Session! <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> DENY PAGE ACCESS <----═════════
session_destroy();
} else {
if(DEBUG) echo "<p class='debug auth ok'><b>Line " . __LINE__ . "</b>: Identification of the session was successful. <i>(" . basename(__FILE__) . ")</i></p>\n";
//----> Generate a new session ID on every page refresh to enhance security
session_regenerate_id(true);
}
# ==================================================================================================
#╔═══════════════════════════════════════════════════════╗
#║ ║
#║ ---| VARIABLEN INITIALISIEREN |--- ║
#║ ║
#╚═══════════════════════════════════════════════════════╝
//══════════----> VARIABLEN INITIALISIEREN <----═════════
//----> Login form section
$userEmail = NULL;
$password = NULL;
$errorUserEmail = NULL; // validateInputString output
$errorPassword = NULL; // validateInputString output
$loginError = NULL; // Login errors
$errorLoadBlog = NULL; // Error by load blogs
# ==================================================================================================
#╔═══════════════════════════════════════════════════════╗
#║ ║
#║ ---| LOGIN FORM PROCESS |--- ║
#║ ║
#╚═══════════════════════════════════════════════════════╝
//══════════----> POST ARRAY PREVIEW <----═════════
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$_POST <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($_POST);
// if(DEBUG_A) echo "</pre>";
//══════════----> FORM-STEP-1 : Check if the form has been submitted <----═════════
if(isset($_POST['formType']) && $_POST['formType'] === 'loginForm') {
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Login form ist submitted...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> FORM-STEP-2 : Reading, defusing and debugging the passed form values <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Values are reading, sanitizing...<i>(" . basename(__FILE__) . ")</i></p>\n";
$userEmail = sanitizeString($_POST['f1']);
$password = sanitizeString($_POST['f2']);
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$userEmail: $userEmail <i>(" . basename(__FILE__) . ")</i></p>\n";
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$password: $password <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> FORM-STEP-3 : Validating the form values <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>:Field values are being validated...<i>(" . basename(__FILE__) . ")</i></p>\n";
//---->[x] FORM-STEP-3-a : Formally validate field values
//---->[x] FORM-STEP-3-b : Display detailed error messages in deug and a general error message to the user.
//---->[-] FORM-STEP-3-c : Pre-assignment of form fields (Not for Login form)
$errorUserEmail = validateEmail($userEmail);
$errorPassword = validateInputString($password, minLength:4);
if(DEBUG_V) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: \$errorUserEmail: $errorUserEmail <i>(" . basename(__FILE__) . ")</i></p>\n";
if(DEBUG_V) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: \$errorPassword: $errorPassword <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> FORM-STEP-3-d : FINAL FORM VALIDATION <----═════════
//---->If successful, proceed to FORM-STEP 4; if not, the processing is aborted
if( $errorUserEmail !== NULL OR $errorPassword !== NULL) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: There are still errors in the form! <i>(" . basename(__FILE__) . ")</i></p>\n";
$loginError= "The email or password you entered is incorrect. Please try again."; // a neutral error for user
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: The form is formally error-free. <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> FORM-STEP-4 : Form values processing <----═════════
//══════════----> FETCH USER DATA FROM DB <----═════════
#╔═════════════════════════════════════════════════════╗
#║ ---| DATABASE OPERATIONS |---- ║
#╚═════════════════════════════════════════════════════╝
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Database operations start...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> DB-Step-1 : Connet to DB <----═════════
$PDO = dbConnect('blogprojekt');
//══════════----> DB-Step-2 : Create SQL-Statement and Placeholder-Array <----═════════
$sql = 'SELECT userID, userPassword FROM users
WHERE userEmail = :userEmail';
$placeholders = array( 'userEmail' => $userEmail );
//══════════----> DB-Step-3 : Prepared Statements <----═════════
try {
// Prepare: SQL-Statement vorbereiten
$PDOStatement = $PDO->prepare($sql);
// Execute: SQL-Statement ausführen und ggf. Platzhalter füllen
$PDOStatement->execute($placeholders);
// showQuery($PDOStatement);
} catch(PDOException $error) {
if(DEBUG) echo "<p class='debug db err'><b>Line " . __LINE__ . "</b>: ERROR: " . $error->GetMessage() . "<i>(" . basename(__FILE__) . ")</i></p>\n";
}
//══════════----> DB-Step-4 : Daten proccess <----═════════
// Get user data from DB
$userInfos = $PDOStatement->fetch(PDO::FETCH_ASSOC);
//══════════----> CLOSE DB CONNECTION <----═════════
dbClose($PDO, $PDOStatement);
//══════════----> DISPLAY USER DATA ARRAY <----═════════
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$userInfos <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($userInfos);
// if(DEBUG_A) echo "</pre>";
//══════════----> 1.EMAIL VALIDATE <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Email validation start...<i>(" . basename(__FILE__) . ")</i></p>\n";
// if userInfos array empty is , returns false
if( $userInfos === false ) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: Email '$userEmail' not found in database.! <i>(" . basename(__FILE__) . ")</i></p>\n";
$loginError= "The email or password you entered is incorrect. Please try again.";
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: Email '$userEmail' found in database. <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> 2.PASSWORD VALIDATE <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Password validation start...<i>(" . basename(__FILE__) . ")</i></p>\n";
// ----> Password verify
if (password_verify( $password, $userInfos['userPassword']) === false ) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: Incorrect password for '$userEmail'! <i>(" . basename(__FILE__) . ")</i></p>\n";
$loginError= "The email or password you entered is incorrect. Please try again.";
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: Password for '$userEmail' verified successfully. <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> LOGIN PROCESS START... <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Starting login process after validation...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> PREPARE SESSSION <----═════════
// session_name('wwwblogprojektmahboubede'); // is already started
if( session_start() === false) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: Login is currently not possible! Please check if cookies are enabled in your browser! <i>(" . basename(__FILE__) . ")</i></p>\n";
$loginError = 'Login is not possible! Please check if in your browser are cookies activ!';
// TODO: save it in ErrorLog file
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: Session has been successfully started. <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> SAVE USER DATA INTO SESSION FILE <----═════════
$_SESSION['ID'] = $userInfos['userID'];
$_SESSION['IPAddress'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['isUserLoggedIn'] = true;
// TODO: We can save userFirstName and userLastName in Session instead of in Dashboard page, a db process haben to get userdate mit userID from DB
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$_SESSION <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($_SESSION);
// if(DEBUG_A) echo "</pre>";
//══════════----> REDIRECT TO DASHBOARD PAGE <----═════════
header('LOCATION: dashboard.php');
// exit();
} // LOGIN PROCESS AND PREPARE SESSSION END
} // PASSWORD VALIDATE END
} // EMAIL VALIDATE END
} // FINAL FORM VALIDATION END
} // LOGIN FORM PROCESS END
# ==================================================================================================
#╔═══════════════════════════════════════════════════════╗
#║ ║
#║ ---| FETCH CATEGORY LABELS FORM DB |--- ║
#║ ║
#╚═══════════════════════════════════════════════════════╝
//══════════----> DATABASE OPERATIONS <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Database operations start...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> DB-Step-1 : Connet to DB <----═════════
$PDO = dbConnect('blogprojekt');
//══════════----> DB-Step-2 : Create SQL-Statement and Placeholder-Array <----═════════
// $sql = 'SELECT * FROM categories';
$sql = 'SELECT c.catID, c.catLabel, COUNT(b.blogID) AS numberOfPosts
FROM categories c
LEFT JOIN blogs b ON c.catID = b.catID
GROUP BY c.catID, c.catLabel;';
$placeholders = array();
//══════════----> DB-Step-3 : Prepared Statements <----═════════
try {
// Prepare: SQL-Statement vorbereiten
$PDOStatement = $PDO->prepare($sql);
// Execute: SQL-Statement ausführen und ggf. Platzhalter füllen
$PDOStatement->execute($placeholders);
// showQuery($PDOStatement);
} catch(PDOException $error) {
if(DEBUG) echo "<p class='debug db err'><b>Line " . __LINE__ . "</b>: ERROR: " . $error->GetMessage() . "<i>(" . basename(__FILE__) . ")</i></p>\n";
}
//══════════----> CHECK IF ANY CATEGORY HAS BIN LOADED FROM DB<----═════════
$rowCount = $PDOStatement->rowCount();
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$rowCount: $rowCount <i>(" . basename(__FILE__) . ")</i></p>\n";
if( $rowCount === 0 ) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: No Category was loaded! <i>(" . basename(__FILE__) . ")</i></p>\n";
$errorLoadCategory = " No Category Was Uploaded! ";
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: $rowCount categories are successfully loaded. <i>(" . basename(__FILE__) . ")</i></p>\n";
$categoriesArray= $PDOStatement->fetchALL(PDO::FETCH_ASSOC);
} // CHECK IF ANY CATEGORY HAS BIN LOADED FROM DB END
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$categoriesArray <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($categoriesArray);
// if(DEBUG_A) echo "</pre>";
# ==================================================================================================
#╔═══════════════════════════════════════════════════════╗
#║ ║
#║ ---| URL PARAMETER PROCESSES |--- ║
#║ ║
#╚═══════════════════════════════════════════════════════╝
//══════════----> GET ARRAY PREVIEW <----═════════
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$_GET <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($_GET);
// if(DEBUG_A) echo "</pre>";
//══════════----> URL-STEP-1 : Check if the url prameters is passed <----═════════
if(isset($_GET['action']) === true ) {
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: URL parameter 'action' is passed...<i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> URL-STEP-2 : Reading, defusing and debugging the passed URL-parameters <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Parameters are reading, sanitizing...<i>(" . basename(__FILE__) . ")</i></p>\n";
$action = sanitizeString($_GET['action']);
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$action: $action <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> URL-STEP-3 : Branch based on the permitted value of the URL parameter <----═════════
#╔═══════════════════════════════════════════════════════╗
#║ ---|LOGOUT PROCESS|--- ║
#╚═══════════════════════════════════════════════════════╝
if( $action === 'logout') {
//══════════----> URL-STEP-4 : Data Processing <----═════════
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Logout process start...<i>(" . basename(__FILE__) . ")</i></p>\n";
// ----> 1. Delete session data
session_destroy();
// ----> 2. Redirect user to index page
header('LOCATION: ./');
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: User is Logged out...<i>(" . basename(__FILE__) . ")</i></p>\n";
exit();
} // LOGOUT PROCESS END
#╔═══════════════════════════════════════════════════════╗
#║ ---|BLOGS FILTERING PROCESS|--- ║
#╚═══════════════════════════════════════════════════════╝
if( $action === 'showCategory') {
if(DEBUG) echo "<p class='debug'><b>Line " . __LINE__ . "</b>: Show filterd category process start...<i>(" . basename(__FILE__) . ")</i></p>\n";
//---> Check if Category Id ist passed in url
if(!isset ($_GET['id']) ) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: An error occurred while reading Category ID ...<i>(" . basename(__FILE__) . ")</i></p>\n";
$errorLoadBlog = " No Filtering possible!";
$sql='SELECT userFirstName, userLastName, userCity, blogHeadline, blogImagePath, blogImageAlignment, blogContent, blogDate, catLabel
FROM blogs
INNER JOIN users USING (userID)
INNER JOIN categories USING (catID)
ORDER BY blogDate DESC';
$placeholders = array();
} else {
$selectedCategory = sanitizeString($_GET['id']);
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$selectedCategory: $selectedCategory <i>(" . basename(__FILE__) . ")</i></p>\n";
// TODO: check if format Id is integer
// TODO: Check if ID is actually existing in DB
$sql = 'SELECT userFirstName, userLastName, userCity, blogHeadline, blogImagePath, blogImageAlignment, blogContent, blogDate, catLabel
FROM blogs
INNER JOIN users USING (userID)
INNER JOIN categories USING (catID)
WHERE catID = :catID
ORDER BY blogDate DESC';
$placeholders = array( 'catID' => $selectedCategory );
} // Check if Category Id ist passed in url END
} // BLOGS FILTERING PROCESS
} else {
//----> IF NO GET URL PARAMETER IS PASSED MUSS DISPLAY ALL POSTS
$sql = 'SELECT userFirstName, userLastName, userCity, blogHeadline, blogImagePath, blogImageAlignment, blogContent, blogDate, catLabel
FROM blogs
INNER JOIN users USING (userID)
INNER JOIN categories USING (catID)
ORDER BY blogDate DESC';
$placeholders = array();
}
#╔═════════════════════════════════════════════════════╗
#║ ║
#║ ---|FETCH BLOG-POSTS FROM DB |---- ║
#║ ║
#╚═════════════════════════════════════════════════════╝
//══════════----> DB-Step-1 : Connet to DB <----═════════
// $PDO = dbConnect('blogprojekt'); //---> is already connected
//══════════----> DB-Step-2 : Create SQL-Statement and Placeholder-Array <----═════════
//----> IS IN LINE 469
// $sql = 'SELECT userFirstName, userLastName, userCity, blogHeadline, blogImagePath, blogImageAlignment, blogContent, blogDate, catLabel
// FROM blogs
// INNER JOIN users USING (userID)
// INNER JOIN categories USING (catID)
// ORDER BY blogDate DESC';
// $placeholders = array();
//══════════----> DB-Step-3 : Prepared Statements <----═════════
try {
// Prepare: SQL-Statement vorbereiten
$PDOStatement = $PDO->prepare($sql);
// Execute: SQL-Statement ausführen und ggf. Platzhalter füllen
$PDOStatement->execute($placeholders);
// showQuery($PDOStatement);
} catch(PDOException $error) {
if(DEBUG) echo "<p class='debug db err'><b>Line " . __LINE__ . "</b>: ERROR: " . $error->GetMessage() . "<i>(" . basename(__FILE__) . ")</i></p>\n";
}
//══════════----> CHECK IF ANY BLOGS HAS BIN LOADED FROM DB<----═════════
$rowCount = $PDOStatement->rowCount();
if(DEBUG_V) echo "<p class='debug value'><b>Line " . __LINE__ . "</b>: \$rowCount: $rowCount <i>(" . basename(__FILE__) . ")</i></p>\n";
if( $rowCount === 0 ) {
if(DEBUG) echo "<p class='debug err'><b>Line " . __LINE__ . "</b>: Error: No blog post was loaded! <i>(" . basename(__FILE__) . ")</i></p>\n";
$errorLoadBlog = " No Blog Post Was Uploaded ";
} else {
if(DEBUG) echo "<p class='debug ok'><b>Line " . __LINE__ . "</b>: $rowCount blogs are successfully loaded. <i>(" . basename(__FILE__) . ")</i></p>\n";
//══════════----> DB-Step-4 : Daten proccess <----═════════
//----> Save blogs in an array
$allBlogsArray= $PDOStatement->fetchALL(PDO::FETCH_ASSOC);
// if(DEBUG_A) echo "<pre class='debug value'><b>Line " . __LINE__ . "</b>: \$allBlogsArray <i>(" . basename(__FILE__) . ")</i>:<br>\n";
// if(DEBUG_A) print_r($allBlogsArray);
// if(DEBUG_A) echo "</pre>";
} // CHECK IF ANY BLOGS HAS BIN LOADED END
//══════════----> CLOSE DB CONNECTION <----═════════
dbClose($PDO, $PDOStatement);
# ══════════════════════════════════════════════════════════════════════════════════════════════════
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Blog Projekt</title>
<link rel="stylesheet" href="./css/debug.css">
<link rel="stylesheet" href="./css/main.css">
</head>
<body>
<!-- ========== HEADER START ========== -->
<header class="container">
<!-- ========== LOGIN FORM START ========== -->
<!-- If user is loggedin is Login form hidden -->
<?php if( !isset($_SESSION['isUserLoggedIn']) ) : ?>
<div class="login">
<form class="form" action="" method="POST">
<!-- Hidden input to differentiate from -->
<input type="hidden" name="formType" value="loginForm">
<!-- Email input box -->
<div class="textbox">
<input type="text" name="f1" id="email" placeholder="" class="login-input" >
<label for="email">Enter your email</label>
</div>
<!-- Password input box -->
<div class="textbox">
<input type="password" name="f2" id="password" placeholder="" class="login-input" >
<label for="password">Enter your password</label>
</div>
<!-- Submit button for the form -->
<button type="submit" class="login-submit" >LOGIN</button>
</form>
<!-- Placeholder for error messages -->
<?php if( isset($loginError)): ?><span class="login-error"><?= $loginError ?></span> <?php endif ?>
</div>
<!-- ---------- LOGIN FORM END ----------- -->
<?php else : ?>
<!-- ========== MENU START ========== -->
<div class="header-menu" >
<ul class="nav">
<li class="nav-item"><a class="nav-link" href="?action=logout">Logout</a></li>
<li class="nav-item"><a class="nav-link" href="dashboard.php">Dashboard</a></li>
</ul>
</div>
<!-- ---------- MENU END ----------- -->
<?php endif ?>
</header>
<!-- ---------- HEADER END ----------- -->
<!-- ========== CONTENT START ========== -->
<main class="container">
<h1 class="intro-title">______ PHP PROJECT BLOG ______</h1>
<section class="row">
<!-- ========== BLOG POSTS START ========== -->
<div class="col-lg-8">
<div class="blogs-wrapper-header">
<div class="blog-header-title">
<h5>Blogs</h5>
</div>
<div class="blog-show-all" >
<a class="show-all-blogs-link" href="index.php">SHOW ALL</a>
</div>
</div>
<h3 style="color: #C40D43;"><?= $errorLoadBlog ?></h3>
<?php foreach( $allBlogsArray as $blog) : ?>
<article class="blog-post-container">
<!-- Category -->
<h3 class="post-category">Category: <b><?= $blog['catLabel'] ?></b></h3>
<!-- Post headline -->
<h2 class="post-title"><?= $blog['blogHeadline'] ?></h2>
<!-- Author Infos -->
<div class="post-infos">
<p>BY <b><?= $blog['userFirstName'] ?> <?= $blog['userLastName'] ?></b> (<?= $blog['userCity'] ?>) am <?= isoToEuDateTime($blog['blogDate'])['date'] ?> um <?= isoToEuDateTime($blog['blogDate'])['time']?> Uhr</p>
</div>
<div class="post-content">
<!-- Image Upload -->
<figure class="post-img <?= $blog['blogImageAlignment'] ?>">
<img src="<?= $blog['blogImagePath'] ?>" >
</figure>
<!-- Post Content -->
<p class="post-paragraph"><?= nl2br( $blog['blogContent'] , false ) ?></p>
</div>
</article>
<?php endforeach ?>
</div>
<!-- ---------- BLOGS POSTS END ----------- -->
<!-- ========== CATEGORIES START ========== -->
<div class="col-lg-4 ">
<div class="blog-sidebar-category">
<div class="category-title">
<h5>Categories</h5>
</div>
<!-- Show error if no Category loaded is -->
<?php if(isset($errorLoadCategory)) : ?>
<h3><?= $errorLoadCategory ?></h3>
<?php else : ?>
<!-- if categories was loaded from db -->
<ul class="cat-list">
<?php
// Identify the currently selected category
$currentCatID = isset($_GET['id']) ? $_GET['id'] : NULL;
foreach( $categoriesArray as $category ) :
$isActive = ($category['catID'] == $currentCatID ) ? 'cat-active' : ''; // Highlight the selected category
?>
<li>
<a class="category <?= $isActive ?>" href="?action=showCategory&id=<?= $category['catID'] ?>">
<h5 class="cat-title"><?= $category['catLabel'] ?></h5>
<h5 class="cat-quantity"><?= $category['numberOfPosts'] ?></h5>
</a>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
</div>
<!-- ---------- CATEGORIES END ----------- -->
</section>
</main>
<!-- ---------- CONTENT END ----------- -->
<!-- ========== FOOTER START ========== -->
<footer>
<p>© <?php echo date("Y"); ?> | Developed by <a href="https://github.com/mahboube89" target="_blank">Mahbobubeh Ranjbar</a></p>
</footer>
<!-- ---------- FOOTER END ----------- -->
</body>
</html>