forked from kxopa/rapid-slim-php-blog-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
140 lines (107 loc) · 3.56 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
<?php
// Auth Details.
define('USERNAME', 'admin');
define('PASSWORD', 'password');
// Slim PHP
require 'Slim/Slim.php';
require 'Views/TwigView.php';
// Paris and Idiorm
require 'Paris/idiorm.php';
require 'Paris/paris.php';
// Models
require 'models/Article.php';
// Configuration
TwigView::$twigDirectory = __DIR__ . '/Twig/lib/Twig/';
ORM::configure('mysql:host=localhost;dbname=blog');
ORM::configure('username', 'root');
ORM::configure('password', '');
// Start Slim.
$app = new Slim(array(
'view' => new TwigView
));
// Auth Check.
$authCheck = function() use ($app) {
$authRequest = isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
$authUser = $authRequest && $_SERVER['PHP_AUTH_USER'] === USERNAME;
$authPass = $authRequest && $_SERVER['PHP_AUTH_PW'] === PASSWORD;
if (! $authUser || ! $authPass) {
$app->response()->header('WWW-Authenticate: Basic realm="My Blog Administration"', '');
$app->response()->header('HTTP/1.1 401 Unauthorized', '');
$app->response()->body('<h1>Please enter valid administration credentials</h1>');
$app->response()->send();
exit;
}
};
// Blog Homepage.
$app->get('/', function() use ($app) {
$articles = Model::factory('Article')
->order_by_desc('timestamp')
->find_many();
return $app->render('blog_home.html', array('articles' => $articles));
});
// Blog View.
$app->get('/view/(:id)', function($id) use ($app) {
$article = Model::factory('Article')->find_one($id);
if (! $article instanceof Article) {
$app->notFound();
}
return $app->render('blog_detail.html', array('article' => $article));
});
// Admin Home.
$app->get('/admin', $authCheck, function() use ($app) {
$articles = Model::factory('Article')
->order_by_desc('timestamp')
->find_many();
return $app->render('admin_home.html', array('articles' => $articles));
});
// Admin Add.
$app->get('/admin/add', $authCheck, function() use ($app) {
return $app->render('admin_input.html', array('action_name' => 'Add', 'action_url' => '/admin/add'));
});
// Admin Add - POST.
$app->post('/admin/add', $authCheck, function() use ($app) {
$article = Model::factory('Article')->create();
$article->title = $app->request()->post('title');
$article->author = $app->request()->post('author');
$article->summary = $app->request()->post('summary');
$article->content = $app->request()->post('content');
$article->timestamp = date('Y-m-d H:i:s');
$article->save();
$app->redirect('/admin');
});
// Admin Edit.
$app->get('/admin/edit/(:id)', $authCheck, function($id) use ($app) {
$article = Model::factory('Article')->find_one($id);
if (! $article instanceof Article) {
$app->notFound();
}
return $app->render('admin_input.html', array(
'action_name' => 'Edit',
'action_url' => '/admin/edit/' . $id,
'article' => $article
));
});
// Admin Edit - POST.
$app->post('/admin/edit/(:id)', $authCheck, function($id) use ($app) {
$article = Model::factory('Article')->find_one($id);
if (! $article instanceof Article) {
$app->notFound();
}
$article->title = $app->request()->post('title');
$article->author = $app->request()->post('author');
$article->summary = $app->request()->post('summary');
$article->content = $app->request()->post('content');
$article->timestamp = date('Y-m-d H:i:s');
$article->save();
$app->redirect('/admin');
});
// Admin Delete.
$app->get('/admin/delete/(:id)', $authCheck, function($id) use ($app) {
$article = Model::factory('Article')->find_one($id);
if ($article instanceof Article) {
$article->delete();
}
$app->redirect('/admin');
});
// Slim Run.
$app->run();