submit.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. require_once 'session.php';
  3. require_once 'database.php';
  4. require_once 'twig.php';
  5. // Must be logged in
  6. if (!Session::is_valid ())
  7. {
  8. header ('Location: ./login');
  9. exit ();
  10. }
  11. $db = new Database();
  12. $db->connect();
  13. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  14. {
  15. // Make sure we have a title
  16. if (!isset ($_POST['title']))
  17. {
  18. header ('Location: ./');
  19. exit ();
  20. }
  21. // Trim title
  22. $title = trim ($_POST['title']);
  23. // Title empty
  24. if (0 == strlen ($title))
  25. {
  26. header ('Location: ./submit');
  27. exit ();
  28. }
  29. // Normalize Link
  30. $link = trim ($_POST['link']);
  31. // If no link given, keep an empty string
  32. if (strlen ($link) > 0)
  33. {
  34. $link_components = parse_url ($link);
  35. // Make sure there's a "scheme"
  36. if (!isset ($link_components['scheme']))
  37. $link = 'http://' . $link;
  38. }
  39. // Add the new post
  40. $post_hash_id = $db->new_post ($title, $link, $_POST['text'], Session::get_userid());
  41. // Redirect to the new post page
  42. header ('Location: ./post/' . $post_hash_id);
  43. exit();
  44. }
  45. // Render template
  46. echo $twig->render (
  47. 'submit.twig',
  48. array ('title' => 'Submit'));