123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
-
- namespace Blog;
-
-
-
- session_start();
-
- use DI\Container;
- use Psr\Http\Message\ResponseInterface as Response;
- use Psr\Http\Message\ServerRequestInterface as Request;
- use Slim\Factory\AppFactory;
- use Blog\DB\User;
- use Blog\DB\Blog as BlogDB;
- use Blog\View\Twig;
- use Blog\DB\DB;
- use Blog\Model\Menu;
-
- use Blog\Model\Blog;
-
-
- require __DIR__ . '/../vendor/autoload.php';
-
-
-
-
-
- $container = new Container();
- AppFactory::setContainer($container);
- $app = AppFactory::create();
-
-
-
- $container->set('twig', function () {
- $twig = new Twig('../templates');
- return $twig;
- });
-
- $container->set(
- 'db',
- function () {
- $db = new DB();
- return $db;
- }
- );
-
-
- function addNavbar($twig)
- {
- $menu = new Menu('primary');
- $twig->addBlockVariable('navbar', ['urls' => $menu]);
- }
-
-
-
- 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) {
-
-
-
- //TODO: load the blog data
-
- $vars = [
- "content" => "Dit is de main body met info"
- ];
-
- $twig = $this->get('twig');
- addNavbar($twig);
- $a = $twig->render('index.html.twig', $vars);
- $response->getBody()->write($a);
- return $response;
- });
-
- $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response $response, array $args) {
- if (isset($_SESSION['username'])) {
- if ($request->getMethod() == 'GET') {
- addNavbar($response);
- if (isset($_SESSION['error'])) {
- $response->getBody()->write($_SESSION['error']);
- $response->getBody()->write('<hr/>');
- unset($_SESSION['error']);
- }
-
- // Opdracht : Toon reeds ingevoerde gegevens in geval van foutmelding
- $response->getBody()->write('<form action="/blog/create" method="POST">');
- $response->getBody()->write('<label for="slug">slug:</label>');
- $response->getBody()->write('<input type="text" name="slug"/><br/>');
- $response->getBody()->write('<label for="title">Titel:</label>');
- $response->getBody()->write('<input type="text" name="title"/><br/>');
- $response->getBody()->write('<label for="content">Inhoud:</label>');
- $response->getBody()->write('<textarea type="textarea" name="content"></textarea><br/>');
- $response->getBody()->write('<input type="submit"/>');
- addFooter($response);
- } else {
- $data = $request->getParsedBody();
- $blog = new Blog();
-
- $res = $blog->createBlog($data);
- if ($res) {
- return $response->withHeader('Location', '/')->withStatus(302);
- } else {
- $err = $blog->lastErrorMsg();
- $_SESSION['error'] = $err;
- return $response->withHeader('Location', '/blog/create')->withStatus(302);
- }
- }
- } else {
- addNavbar($response);
- $response->getBody()->write(('Please login'));
- addFooter($response);
- }
- return $response;
- });
-
- $app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
-
- addNavbar($response);
- foreach ($_SESSION['blogs'] as $art) {
- if ($art['slug'] == $args['slug']) {
- $response->getBody()->write("<h1>" . $art['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();
-
- $user = new User();
- $logged_in = $user->checkUserPass($postdata['username'], $postdata['password']);
- if ($logged_in) {
- //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->run();
|