Sfoglia il codice sorgente

added uncommited changes

main
Bram Dieudonne 2 anni fa
parent
commit
66c598d484
1 ha cambiato i file con 64 aggiunte e 13 eliminazioni
  1. 64
    13
      public/index.php

+ 64
- 13
public/index.php Vedi File

@@ -8,6 +8,16 @@ use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../private/test.db');
}
}

$db = new MyDB();


function addNavbar($response)
{
@@ -28,16 +38,26 @@ function addFooter($response)
return;
}

$app->get('/', function (Request $request, Response $response, array $args) {
function init()
{
$art1 = ["slug" => "artikel-1", "title" => "Dit is titel 1", "content" => "Lorem ipsum 1"];
$art2 = ["slug" => "artikel-2", "title" => "Dit is titel 2", "content" => "Lorem ipsum 2"];
$art3 = ["slug" => "artikel-3", "title" => "Dit is titel 3", "content" => "Lorem ipsum 3"];
$art4 = ["slug" => "artikel-4", "title" => "Dit is titel 4", "content" => "Lorem ipsum 4"];
$_SESSION['blogs'] = [$art1, $art2, $art3, $art4];
}

addNavbar($response);

$app->get('/', function (Request $request, Response $response, array $args) {
if (!isset($_SESSION['blogs'])) {
init();
}
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>');
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);
@@ -46,12 +66,38 @@ $app->get('/', function (Request $request, Response $response, array $args) {
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);
$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('<input type="submit"/>');
addFooter($response);
} else {
$data = $request->getParsedBody();
$_SESSION['blogs'][] = ["slug" => $data['slug'], "title" => $data['title'], "content" => "Lorem ipsum 4"];
return $response->withHeader('Location', '/')->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);
$title = $args['slug'];
$response->getBody()->write("<h1>$title</h1>");
foreach ($_SESSION['blogs'] as $art) {
if ($art['slug'] == $args['slug']) {
$response->getBody()->write("<h1>" . $art['title'] . "</h1>");
}
}

addFooter($response);
return $response;
@@ -78,7 +124,15 @@ $app->map(['GET', 'POST'], '/login', function (Request $request, Response $respo
addFooter($response);
} else {
$postdata = $request->getParsedBody();
if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") {


global $db;
$sql = "SELECT count(*) as count FROM users WHERE username = '" . $postdata['username'] . "' AND password = '" . $postdata['password'] . "';";
$ret = $db->query($sql);
$rows = $ret->fetchArray(SQLITE3_ASSOC);
$rowcount = $rows['count'];
if ($rowcount == 1) {
//if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") {
$_SESSION["username"] = $postdata['username'];
addNavbar($response);
$response->getBody()->write('Logged in');
@@ -96,9 +150,6 @@ $app->post('/postcomment', function (Request $request, Response $response, array
return $response;
});

$app->post('/blog/create', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Blog create");
return $response;
});


$app->run();

Loading…
Annulla
Salva