123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace Blog;
-
- session_start();
-
- 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;
- use Blog\DB\Bloglist;
- use Blog\DB\DB;
-
- require __DIR__ . '/../vendor/autoload.php';
-
- $app = AppFactory::create();
-
- $loader = new \Twig\Loader\FilesystemLoader('../templates');
- $twig = new \Twig\Environment($loader);
-
- ini_set('display_errors', 'Off');
-
- 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;
- }
-
- function init()
- {
- $bloglist = new Bloglist;
- $articles = $bloglist->countBlogs();
- $i = 0;
- $blogArray = [];
- while ($articles[$i]) {
- array_push($blogArray, $articles[$i]);
- $i++;
- }
- $_SESSION['blogs'] = $blogArray;
- }
-
-
- $app->get('/', function (Request $request, Response $response, array $args) {
-
-
- init();
-
- global $twig;
- $template = $twig->load('index.html.twig');
- $a = $template->render(["key" => "value", "key2" => ["val1", "val2", "val3"], "name" => "John", "lastname" => "Doe"]);
- $response->getBody()->write($a);
-
- // addNavbar($response);
- // $response->getBody()->write('<hr/><h1>Onze blog</h1>');
- // $response->getBody()->write('<ul>');
-
- // foreach ($_SESSION['blogs'] as $art) {
- // $response->getBody()->write('<li><a href="/blog/' . $art['slug'] . '">' . $art['title'] . '</a></li>');
- // }
- // $response->getBody()->write('</ul>');
- // $response->getBody()->write('<hr/>');
- // addFooter($response);
-
-
- 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 ($_SESSION['error']) {
- $response->getBody()->write($_SESSION['error']);
- $response->getBody()->write('<hr/>');
- unset($_SESSION['error']);
- }
- // toon reeds ingevoerde ggevens 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">Content:</label>');
- $response->getBody()->write('<textarea name="content" rows="4" columns="50"></textarea>');
- $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>");
- $response->getBody()->write('<p>' . $art['content'] . '</p>');
- }
- }
-
- 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) {
- $_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();
|