user.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once 'session.php';
  3. require_once 'database.php';
  4. require_once 'date.php';
  5. require_once 'twig.php';
  6. $db = new Database ();
  7. $db->connect ();
  8. // Form submitted
  9. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  10. {
  11. if (!isset ($_POST['update']))
  12. {
  13. header ('Location: ./');
  14. exit ();
  15. }
  16. // Open database connection
  17. $db = new Database();
  18. $db->connect();
  19. // Update database with new user information
  20. $data = array(
  21. 'about' => (isset ($_POST['about']) ? $_POST['about'] : ''),
  22. 'email' => (isset ($_POST['email']) ? $_POST['email'] : ''),
  23. 'email_notifications' => (isset ($_POST['email_notifications']) ? $_POST['email_notifications'] : ''));
  24. $db->edit_user (
  25. $data['about'],
  26. $data['email'],
  27. $data['email_notifications'],
  28. Session::get_userid ());
  29. // Update $_SESSION
  30. Session::set_property ('about', $data['about']);
  31. Session::set_property ('email', $data['email']);
  32. Session::set_property ('email_notifications', $data['email_notifications']);
  33. header ('Location: ./user');
  34. exit ();
  35. }
  36. // Show public profile
  37. if (isset ($_GET['username']))
  38. {
  39. $user = $db->get_user ($_GET['username']);
  40. // User doesn't exist
  41. if (is_null ($user) || empty ($user))
  42. {
  43. header ('Location: ../login');
  44. exit ();
  45. }
  46. echo $twig->render (
  47. 'user.twig',
  48. array (
  49. 'title' => $user['username'],
  50. 'profile' => 'public',
  51. 'other_user' => $user));
  52. } else {
  53. // Show private page
  54. echo $twig->render (
  55. 'user.twig',
  56. array (
  57. 'title' => Session::get_username (),
  58. 'profile' => 'private'));
  59. }