-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
executable file
·154 lines (120 loc) · 3.63 KB
/
admin.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
<?php
require("config.php");
session_start();
$action = isset($_GET['action']) ? $_GET['action'] : "";
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";
?>
<div class="container">
<?php
if ($action != "login" && $action != "logout" && !$username){
login();
exit;
}
switch ($action) {
case 'login':
login();
break;
case 'logout':
logout();
break;
case 'newArticle':
newArticle();
break;
case 'editArticle':
editArticle();
break;
case 'deleteArticle':
deleteArticle();
break;
default:
listArticles();
}
function login() {
$results = array();
$results ['pageTitle'] = "Admin login";
if(isset($_POST['login'])) {
//user has posted the login foarm: attmep to log the user in
if($_POST['username'] == ADMIN_USERNAME && $_POST['password'] == ADMIN_PASSWORD){
//log in successful: Create a Session and redirect to the admin homepage
$_SESSION['username'] = ADMIN_USERNAME;
header("location: admin.php");
} else {
//login failed: display an error message to the user
$results['errorMessage'] = "Incorrect username or password. Please try again.";
require("templates/admin/loginForm.php");
}
}else{
//user has not posted the login from yet: display the form
require("templates/admin/loginForm.php");
}
}
function logout(){
unset($_SESSION['username']);
header("Location: admin.php");
}
function newArticle() {
$results = array();
$results['pageTitle'] = "New Article";
$results['formAction'] = "newArticle";
if(isset($_POST['saveChanges'])){
//user has posted the article edit form: save the new article
$article = new Article;
$article->storeFormValues($_POST);
$article->insert();
header("Location: admin.php?status=changesSaved");
}elseif(isset($_POST['cancel'])){
//user has cancelled their edit: return to the article lists
header("Location: admin.php");
}else{
//user has not posted the article edit form yet: display the form
$results['article'] = new Article;
require("templates/admin/editArticle.php");
}
}
function editArticle() {
$results = array();
$results['pageTitle'] = "Edit Article";
$results['formAction'] = "editArticle";
if(isset($_POST['saveChanges'])) {
//user has posted the article edit form; save the article changes
if(!$article = Article::getById((int)$_POST['articleId'])) {
header("Location: admin.php?error=articleNotFound");
retun;
}
$article->storeFormValues($_POST);
$article->update();
header("Location: admin.php?status=changesSaved");
}elseif (isset($_POST['cancel'])) {
//user has cancelled theri edits: return to thte article list
header("Location: admin.php");
}else{
//user has not posted the article edit from yet: display the form
$results['article'] = Article::getById((int)$_GET['articleId']);
require("templates/admin/editArticle.php");
}
}
function deleteArticle(){
if(!$article = Article::getById((int)$_GET['articleId'])){
header("Location: admin.php?error=articleNotFound");
return;
}
$article->delete();
header("Location: admin.php?status=articleDeleted");
}
function listArticles(){
$results = array();
$data = Article::getList();
$results['articles'] = $data["results"];
$results['totalRows'] = $data["totalRows"];
$results['pageTitle'] = "All Articles";
if(isset($_GET['error'])){
if($_GET['error'] == "articleNotFound") $results['errorMessage'] = "Error: Article not found.";
}
if(isset($_GET['status'])){
if($_GET['status'] == "changesSaved") $results['statusMessage'] = "Your changes have been saved.";
if($_GET['status'] == "articleDeleted") $results['statusMessage'] = "Article deleted'";
}
require("templates/admin/listArticles.php");
}
?>
</div>