sendcomment.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. require_once('include/config.php');
  3. require_once('include/db.php');
  4. require_once('include/comment.php');
  5. switch (@$_POST['mode']) {
  6. case 'json':
  7. $mode = 'json';
  8. break;
  9. default:
  10. $mode = 'html';
  11. break;
  12. }
  13. function store_comment() {
  14. $nick = @$_POST['comment-nick'];
  15. $email = @$_POST['comment-email'];
  16. $type = @$_POST['comment-type'];
  17. $id = @$_POST['comment-id'];
  18. $content = @$_POST['comment-content'];
  19. if (empty($nick) || strlen($nick) === 0) {
  20. throw new Exception('Nie podano nicka');
  21. }
  22. if (empty($type) || strlen($type) === 0 || empty($id) || strlen($id) === 0) {
  23. throw new Exception('Niepoprawne żądanie');
  24. }
  25. if (empty($content) || strlen($content) === 0) {
  26. throw new Exception('Nie wpisano treści');
  27. }
  28. db_store_comment($nick, $email, $type, $id, $content);
  29. return 'OK';
  30. }
  31. try {
  32. $res = store_comment();
  33. if ($mode == 'html') {
  34. $output = $res;
  35. } else {
  36. $output = array('result' => $res);
  37. }
  38. } catch (Exception $e) {
  39. if ($mode == 'html') {
  40. $output = 'Błąd: ' . $e->getMessage();
  41. } else {
  42. $output = array('result' => 'error', 'error' => $e->getMessage());
  43. }
  44. }
  45. if ($mode == 'json') {
  46. header('Content-Type: application/json; charset=utf-8');
  47. echo json_encode($output);
  48. die();
  49. }
  50. ?><!DOCTYPE html>
  51. <html>
  52. <head>
  53. <title>Obrazki: komentarz</title>
  54. <link rel="stylesheet" href="style.css?<?=$time?>" type="text/css">
  55. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  56. </head>
  57. <body>
  58. <?=$output?><br>
  59. <a href="javascript:history.back()">Powrót</a>
  60. </body>
  61. </html>