-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.php
164 lines (125 loc) · 4.56 KB
/
routes.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
<?php
$app = new \Slim\Slim();
/**
* Login
*/
$app->get('/close_session', function () use ($app) {
$_SESSION = [];
$app->response->redirect('/create_session');
});
$app->get('/create_session', function () use ($app) {
render('create_session', $_SESSION['flash']);
});
$app->post('/create_session', function () use ($app) {
if ($user = user_verify($_POST, $error)) {
create_session($user['role'], $user['login']);
return $app->response->redirect('/pages');
}
$_SESSION['flash'] = array_select_keys($_POST, ['login','password']);
$_SESSION['flash']['alert'] = $error;
return $app->response->redirect('/create_session');
});
$app->get('/not_allowed', function () use ($app) {
render('not_allowed');
});
/**
* Template Dashboard
*/
$app->get('/templates', function () use ($app) {
authenticate_admin($app);
render('templates', template_list());
});
$app->get('/templates/create', function ($name) use ($app) {
authenticate_admin($app);
render('template_x', ['create'=>true]);
});
$app->get('/templates/:name', function ($name) use ($app) {
authenticate_admin($app);
$template = template_fetch($name);
render('template_x', $template);
});
$app->post('/templates(/:name)/', function ($name) use ($app) {
authenticate_admin($app);
if (!$name) $name = $_REQUEST['name'];
if (template_update($name, $_REQUEST, $_SESSION['flash']['alert'])) {
$_SESSION['flash']['success'] = ($_REQUEST['create']) ? 'Template successfully created!' : 'Template successfully updated!';
$app->response->redirect('/templates/' . urlencode($_REQUEST['name']));
} else {
$_SESSION['flash'] = $_SESSION['flash'] + array_select_keys($_REQUEST, ['name','css','view','data','edit']);
$app->response->redirect('/templates/' . urlencode($name));
}
});
$app->get('/templates/:name/delete', function ($name) use ($app) {
authenticate_admin($app);
render('confirm', ['message'=>"Are you sure you want to delete '$name'?"]);
});
$app->post('/templates/:name/delete', function ($name) use ($app) {
authenticate_admin($app);
if (template_delete($name, $error)) {
$_SESSION['flash']['success'] = "Template '$name' successfully deleted!";
$app->response->redirect('/templates');
} else {
$_SESSION['flash']['alert'] = "Something went wrong deleting '$name'";
$app->response->redirect('/templates/' . urlencode($name));
}
});
/**
* Page Dashboard
*/
$app->get('/pages/', function () use ($app) {
authenticate($app);
$opts = [];
if (!admin()) $opts['login'] = current_user();
render('pages', page_list($opts));
});
$app->get('/pages/create', function ($name) use ($app) {
authenticate($app);
render('page_x', ['create'=>true] + template_list());
});
$app->get('/pages/:name', function ($name) use ($app) {
authenticate($app);
$page = page_fetch($name);
$model = ['page'=>render_keys($page)] + compact('name');
$model['editing_template'] =
render(name_encode($page->theme)."_edit", $model, false, true);
$model['permissions'] = permissions_fetch($name);
render('page_x', $model);
});
$app->post('/pages(/:name)/', function ($name) use ($app) {
authenticate($app);
// make sure _add_keys and theme get stored
if (page_update($name, $_REQUEST, $_SESSION['flash']['alert'])) {
$_SESSION['flash']['success'] = 'Page successfully updated!';
$app->response->redirect('/pages/' . urlencode($_REQUEST['name']));
} else {
$_SESSION['flash'] = $_SESSION['flash'] + array_select_keys($_REQUEST, ['name','css','view','data','edit']);
$app->response->redirect('/pages/' . urlencode($name));
}
});
$app->get('/pages/:name/delete', function ($name) use ($app) {
authenticate($app);
render('confirm', ['message'=>"Are you sure you want to delete '$name'?"]);
});
$app->post('/pages/:name/delete', function ($name) use ($app) {
authenticate($app);
if (page_delete($name, $error)) {
$_SESSION['flash']['success'] = "Page '$name' successfully deleted!";
$app->response->redirect('/pages');
} else {
$_SESSION['flash']['alert'] = "Something went wrong deleting '$name'";
$app->response->redirect('/pages/' . urlencode($name));
}
});
/**
* Display Pages
*/
$app->get('/', function () use ($app) {
render('index', page_list());
});
$app->get('/:name', function ($name) use ($app) {
$page = page_fetch($name);
$theme = name_encode($page->theme);
render(page_template($page), compact('page','theme'));
});
$app->run();
exit;