You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Blog;
  3. session_start();
  4. use DI\Container;
  5. use Psr\Http\Message\ResponseInterface as Response;
  6. use Psr\Http\Message\ServerRequestInterface as Request;
  7. use Slim\Factory\AppFactory;
  8. use Blog\DB\User;
  9. use Blog\Model\Blogs;
  10. use Blog\View\Twig;
  11. use Blog\DB\DB;
  12. use Blog\Model\Menu;
  13. use Blog\Model\Blog;
  14. require __DIR__ . '/../vendor/autoload.php';
  15. $container = new Container();
  16. AppFactory::setContainer($container);
  17. $app = AppFactory::create();
  18. $container->set('twig', function () {
  19. $twig = new Twig('../templates');
  20. return $twig;
  21. });
  22. $container->set(
  23. 'db',
  24. function () {
  25. $db = new DB();
  26. return $db;
  27. }
  28. );
  29. function addNavbar($twig)
  30. {
  31. $menu = new Menu('primary');
  32. $twig->addBlockVariable('navbar', $menu->getLinks());
  33. }
  34. function addNavbar2($twig)
  35. {
  36. $menu = new Menu('secondary');
  37. $twig->addBlockVariable('navbar', $menu->getLinks());
  38. }
  39. function addFooter($response)
  40. {
  41. $response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>');
  42. $response->getBody()->write("</body></html>");
  43. return;
  44. }
  45. $app->get('/', function (Request $request, Response $response, array $args) {
  46. $blogs = new Blogs();
  47. $blogs = $blogs->getBlogs();
  48. $vars = [
  49. "content" => $blogs
  50. ];
  51. $twig = $this->get('twig');
  52. addNavbar($twig);
  53. addNavbar2($twig);
  54. $a = $twig->render('index.html.twig', $vars);
  55. $response->getBody()->write($a);
  56. return $response;
  57. });
  58. $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response $response, array $args) {
  59. if (isset($_SESSION['username'])) {
  60. if ($request->getMethod() == 'GET') {
  61. addNavbar($response);
  62. if (isset($_SESSION['error'])) {
  63. $response->getBody()->write($_SESSION['error']);
  64. $response->getBody()->write('<hr/>');
  65. unset($_SESSION['error']);
  66. }
  67. // Opdracht : Toon reeds ingevoerde gegevens in geval van foutmelding
  68. $response->getBody()->write('<form action="/blog/create" method="POST">');
  69. $response->getBody()->write('<label for="slug">slug:</label>');
  70. $response->getBody()->write('<input type="text" name="slug"/><br/>');
  71. $response->getBody()->write('<label for="title">Titel:</label>');
  72. $response->getBody()->write('<input type="text" name="title"/><br/>');
  73. $response->getBody()->write('<label for="content">Inhoud:</label>');
  74. $response->getBody()->write('<textarea type="textarea" name="content"></textarea><br/>');
  75. $response->getBody()->write('<input type="submit"/>');
  76. addFooter($response);
  77. } else {
  78. $data = $request->getParsedBody();
  79. $blog = new Blog();
  80. $res = $blog->createBlog($data);
  81. if ($res) {
  82. return $response->withHeader('Location', '/')->withStatus(302);
  83. } else {
  84. $err = $blog->lastErrorMsg();
  85. $_SESSION['error'] = $err;
  86. return $response->withHeader('Location', '/blog/create')->withStatus(302);
  87. }
  88. }
  89. } else {
  90. addNavbar($response);
  91. $response->getBody()->write(('Please login'));
  92. addFooter($response);
  93. }
  94. return $response;
  95. });
  96. $app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
  97. $blogs = new Blogs();
  98. $blogs = $blogs->getBlog($args['slug']);
  99. $vars = [
  100. "content" => $blogs
  101. ];
  102. $twig = $this->get('twig');
  103. addNavbar($twig);
  104. $a = $twig->render('blog.html.twig', $vars);
  105. $response->getBody()->write($a);
  106. return $response;
  107. });
  108. $app->get('/logout', function (Request $request, Response $response, array $args) {
  109. unset($_SESSION['username']);
  110. addNavbar($response);
  111. $response->getBody()->write('Logged out');
  112. addFooter($response);
  113. return $response->withHeader('Location', '/')->withStatus(302);
  114. });
  115. $app->map(['GET', 'POST'], '/login', function (Request $request, Response $response, array $args) {
  116. if ($request->getMethod() == 'GET') {
  117. addNavbar($response);
  118. $response->getBody()->write('<form action="/login" method="POST">');
  119. $response->getBody()->write('<label for="username">Username:</label>');
  120. $response->getBody()->write('<input type="text" name="username"/><br/>');
  121. $response->getBody()->write('<label for="password">Password:</label>');
  122. $response->getBody()->write('<input type="password" name="password"/><br/>');
  123. $response->getBody()->write('<input type="submit"/>');
  124. addFooter($response);
  125. } else {
  126. $postdata = $request->getParsedBody();
  127. $user = new User();
  128. $logged_in = $user->checkUserPass($postdata['username'], $postdata['password']);
  129. if ($logged_in) {
  130. //if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") {
  131. $_SESSION["username"] = $postdata['username'];
  132. addNavbar($response);
  133. $response->getBody()->write('Logged in');
  134. addFooter($response);
  135. return $response->withHeader('Location', '/')->withStatus(302);
  136. } else {
  137. $response->getBody()->write('Username and/or password incorrect');
  138. }
  139. }
  140. return $response;
  141. });
  142. $app->post('/postcomment', function (Request $request, Response $response, array $args) {
  143. $response->getBody()->write("Postcomment");
  144. return $response;
  145. });
  146. $app->run();