home » zplus/freepost.git
ID: 03447cc3a2563ce67ca7c245388ec5ab342fcc0a
45 lines — 887B — View raw


<?php

require_once 'session.php';
require_once 'database.php';
require_once 'twig.php';

// Must be logged in
if (!Session::is_valid())
{
    header ('Location: ./login');
    exit ();
}

$db = new Database();
$db->connect();

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    // Make sure we have a title
    if (!isset ($_POST['title']))
    {
        header ('Location: ./');
        exit ();
    }
    
    // Trim title
    $title = trim ($_POST['title']);
    
    // Title empty
    if (0 == strlen ($title))
    {
        header ('Location: ./submit');
        exit ();
    }
    
    // Title is ok, add the new post
    $post_hash_id = $db->new_post ($title, $_POST['link'], $_POST['text'], Session::get_userid());
    
    // Redirect to the new post page
    header ('Location: ./post/' . $post_hash_id);
    exit();
}

// Render template
echo $twig->render ('submit.twig');