123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- session_start();
-
- use Psr\Http\Message\ResponseInterface as Response;
- use Psr\Http\Message\ServerRequestInterface as Request;
- use Slim\Factory\AppFactory;
-
- require __DIR__ . '/../vendor/autoload.php';
-
- $app = AppFactory::create();
-
- function addNavbar($response) {
- $response->getBody()->write("<html><head></head><body>");
- if (isset($_SESSION['username'])) {
- $response->getBody()->write("<div><a href='/'>Index</a> | <a href='/blog/create'>Create Blog</a> | <a href='/logout'>Logout</a></div>");
- } else {
- $response->getBody()->write("<div><a href='/'>Index</a> | <a href='/login'>login</a></div>");
- }
- return;
- }
-
- function addFooter($response) {
- $response->getBody()->write("<div>Privacy statement | Cookie Policy | Contact</div>");
- $response->getBody()->write("</body></html>");
- return;
- }
-
- $app->get('/', function (Request $request, Response $response, array $args) {
-
-
-
- addNavbar($response);
-
- $response->getBody()->write("<hr/><h1>Onze blog</h1>");
- $response->getBody()->write("<ul>");
- $response->getBody()->write("<li><a href='/blog/artikel-1'>Blogartikel 1</a></li>");
- $response->getBody()->write("<li><a href='/blog/artikel-2'>Blogartikel 2</a></li>");
- $response->getBody()->write("<li><a href='/blog/artikel-3'>Blogartikel 3</a></li>");
- $response->getBody()->write("<li><a href='/blog/artikel-4'>Blogartikel 4</a></li>");
- $response->getBody()->write("</ul>");
- $response->getBody()->write("<hr/>");
- addFooter($response);
-
- return $response;
- });
-
- $app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
-
- addNavbar($response);
- $title = $args['slug'];
- $response->getBody()->write("<h1>$title</h1>");
-
- addFooter($response);
- return $response;
- });
-
- $app->get('/logout', function(Request $request, Response $response, array $args) {
- unset($_SESSION['username']);
- addNavbar($response);
- $response->getBody()->write('logged out');
- addFooter($response);
- return $response->withHeader('location', '/')->withStatus(302);
- });
-
- $app->map(['GET', 'POST'],'/login', function (Request $request, Response $response, array $args) {
- if ($request->getMethod() == 'GET') {
- addNavbar($response);
- $response->getBody()->write('<form action="/login" method="POST">');
- $response->getBody()->write('<label for="username">Username</label>');
- $response->getBody()->write('<input type="text" name="username"/></br>');
- $response->getBody()->write('<label for="password">Password</label>');
- $response->getBody()->write('<input type="password" name="password"/></br>');
- $response->getBody()->write('<input type="submit">');
- addFooter($response);
- } else {
- $postdata = $request->getParsedBody();
- if ($postdata['username'] == 'gebruiker' && $postdata['password'] == 'abcd') {
- $_SESSION['username'] = $postdata['username'];
- addNavbar($response);
- $response->getBody()->write('logged in');
- addFooter($response);
- return $response->withHeader('location', '/')->withStatus(302);
- } else {
- $response->getBody()->write('username and/or password incorrect');
- }
-
- }
- return $response;
- });
-
- $app->post('/postcomment', function (Request $request, Response $response, array $args) {
-
- $response->getBody()->write("Postcomment");
- return $response;
- });
-
- $app->post('/blog/create', function (Request $request, Response $response, array $args) {
-
- $response->getBody()->write("Blog create");
- return $response;
- });
-
- $app->run();
|