post.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Este arquivo é parte do software linequest
  4. * Ambiente de questionários para a coleta de dados
  5. *
  6. * Laboratório de Informática na Educação - LInE
  7. * https://www.usp.br/line/
  8. *
  9. * Utilize os atributos definidos abaixo para
  10. * configurar o ambiente de questionários.
  11. *
  12. * @author Lucas Calion
  13. * @author Igor Félix
  14. */
  15. # Os dados podem ser submetidos para o linequest por
  16. # meio de requisições POST ou GET
  17. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  18. execute($_POST);
  19. } elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
  20. execute($_GET);
  21. }
  22. /**
  23. * Extrai todos os parâmetros enviados na requisição
  24. * @param $data - representa o conteúdo de $_POST ou $_GET
  25. */
  26. function execute ($data) {
  27. if (count($data) < 1) {
  28. echo print_error('empty_data');
  29. exit;
  30. }
  31. if (!isset($data['form'])) {
  32. echo print_error('key_missing');
  33. exit;
  34. }
  35. $data["ip"] = get_user_ip();
  36. $data["timestamp"] = time();
  37. require_once('../controller/forms.php');
  38. try {
  39. store(json_encode($data, JSON_UNESCAPED_UNICODE));
  40. } catch (Exception $e) {
  41. echo print_error($e->getMessage());
  42. exit;
  43. }
  44. header('Location: thanks.html');
  45. }
  46. /**
  47. * Imprime as mensagens de erro padrão:
  48. * @param $code - identifica o erro a ser retornado
  49. */
  50. function print_error ($code) {
  51. switch($code) {
  52. case 'key_missing':
  53. return "Error: The data could not be stored. <br>Reason: The 'form' parameter was not sent in the request. This parameter is required, it identifies which form this data is associated.";
  54. case 'empty_data':
  55. return "Error: The data could not be stored. <br>Reason: None parameter was sent in the request.<br>";
  56. }
  57. }
  58. function get_user_ip () {
  59. $ip = "";
  60. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  61. $ip = $_SERVER['HTTP_CLIENT_IP'];
  62. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  63. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  64. } else {
  65. $ip = $_SERVER['REMOTE_ADDR'];
  66. }
  67. return $ip;
  68. }
  69. ?>