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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\DB\Blog as BlogDB;
  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', ['urls' => $menu]);
  33. }
  34. function addFooter($response)
  35. {
  36. $response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>');
  37. $response->getBody()->write("</body></html>");
  38. return;
  39. }
  40. $app->get('/', function (Request $request, Response $response, array $args) {
  41. //TODO: load the blog data
  42. $vars = [
  43. "content" => "Dit is de main body met info"
  44. ];
  45. $twig = $this->get('twig');
  46. addNavbar($twig);
  47. $a = $twig->render('index.html.twig', $vars);
  48. $response->getBody()->write($a);
  49. return $response;
  50. });
  51. $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response $response, array $args) {
  52. if (isset($_SESSION['username'])) {
  53. if ($request->getMethod() == 'GET') {
  54. addNavbar($response);
  55. if (isset($_SESSION['error'])) {
  56. $response->getBody()->write($_SESSION['error']);
  57. $response->getBody()->write('<hr/>');
  58. unset($_SESSION['error']);
  59. }
  60. // Opdracht : Toon reeds ingevoerde gegevens in geval van foutmelding
  61. $response->getBody()->write('<form action="/blog/create" method="POST">');
  62. $response->getBody()->write('<label for="slug">slug:</label>');
  63. $response->getBody()->write('<input type="text" name="slug"/><br/>');
  64. $response->getBody()->write('<label for="title">Titel:</label>');
  65. $response->getBody()->write('<input type="text" name="title"/><br/>');
  66. $response->getBody()->write('<label for="content">Inhoud:</label>');
  67. $response->getBody()->write('<textarea type="textarea" name="content"></textarea><br/>');
  68. $response->getBody()->write('<input type="submit"/>');
  69. addFooter($response);
  70. } else {
  71. $data = $request->getParsedBody();
  72. $blog = new Blog();
  73. $res = $blog->createBlog($data);
  74. if ($res) {
  75. return $response->withHeader('Location', '/')->withStatus(302);
  76. } else {
  77. $err = $blog->lastErrorMsg();
  78. $_SESSION['error'] = $err;
  79. return $response->withHeader('Location', '/blog/create')->withStatus(302);
  80. }
  81. }
  82. } else {
  83. addNavbar($response);
  84. $response->getBody()->write(('Please login'));
  85. addFooter($response);
  86. }
  87. return $response;
  88. });
  89. $app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
  90. addNavbar($response);
  91. foreach ($_SESSION['blogs'] as $art) {
  92. if ($art['slug'] == $args['slug']) {
  93. $response->getBody()->write("<h1>" . $art['title'] . "</h1>");
  94. }
  95. }
  96. addFooter($response);
  97. return $response;
  98. });
  99. $app->get('/logout', function (Request $request, Response $response, array $args) {
  100. unset($_SESSION['username']);
  101. addNavbar($response);
  102. $response->getBody()->write('Logged out');
  103. addFooter($response);
  104. return $response->withHeader('Location', '/')->withStatus(302);
  105. });
  106. $app->map(['GET', 'POST'], '/login', function (Request $request, Response $response, array $args) {
  107. if ($request->getMethod() == 'GET') {
  108. addNavbar($response);
  109. $response->getBody()->write('<form action="/login" method="POST">');
  110. $response->getBody()->write('<label for="username">Username:</label>');
  111. $response->getBody()->write('<input type="text" name="username"/><br/>');
  112. $response->getBody()->write('<label for="password">Password:</label>');
  113. $response->getBody()->write('<input type="password" name="password"/><br/>');
  114. $response->getBody()->write('<input type="submit"/>');
  115. addFooter($response);
  116. } else {
  117. $postdata = $request->getParsedBody();
  118. $user = new User();
  119. $logged_in = $user->checkUserPass($postdata['username'], $postdata['password']);
  120. if ($logged_in) {
  121. //if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") {
  122. $_SESSION["username"] = $postdata['username'];
  123. addNavbar($response);
  124. $response->getBody()->write('Logged in');
  125. addFooter($response);
  126. return $response->withHeader('Location', '/')->withStatus(302);
  127. } else {
  128. $response->getBody()->write('Username and/or password incorrect');
  129. }
  130. }
  131. return $response;
  132. });
  133. $app->post('/postcomment', function (Request $request, Response $response, array $args) {
  134. $response->getBody()->write("Postcomment");
  135. return $response;
  136. });
  137. $app->run();