create_form.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. require_once('../controller/validator.php');
  3. require_once('../controller/formparser.php');
  4. require_once('../controller/forms.php');
  5. require_once('../templates/templates.php');
  6. require_once('../controller/generateform.php');
  7. $KEY_SIZE = 15; // max. 32
  8. $FOLDER_SIZE = 10; // max. 32
  9. function generateKeys ($email, $title) {
  10. global $KEY_SIZE;
  11. global $FOLDER_SIZE;
  12. $date = new DateTime();
  13. $input = $date->format("c").$email.$title;
  14. $sha = hash("sha256", $input);
  15. $key = array();
  16. $folder = array();
  17. $i = 0;
  18. for (; $i < $KEY_SIZE; $i++) {
  19. $key[] = $sha[$i];
  20. }
  21. for ($j = 0;$j < $FOLDER_SIZE; $j++ ) {
  22. $folder[] = $sha[$i+$j];
  23. }
  24. return [implode("", $key), implode("", $folder)];
  25. }
  26. if (empty($_POST)) {
  27. generateCreateForm();
  28. } else {
  29. proccessRequest($_POST);
  30. }
  31. function generateCreateForm () {
  32. $template = getTemplate('create_form.html');
  33. $context = array('post_url' => htmlspecialchars($_SERVER["PHP_SELF"]));
  34. $template = parseTemplate($template, $context);
  35. echo $template;
  36. }
  37. function proccessRequest ($data) {
  38. try {
  39. Validator::check(['email', 'title', 'description','thanks'], $data);
  40. Validator::check(['formSource'], $_FILES);
  41. $from = array();
  42. $form['email'] = Validator::email($data['email']);
  43. $form['title'] = Validator::str($data['title']);
  44. $form['description'] = Validator::str($data['description']);
  45. $form['thanks'] = Validator::str($data['thanks']);
  46. $upload = Validator::file($_FILES['formSource'], 'txt');
  47. $formSource = file_get_contents($upload['tmp_name']);
  48. $reader = new SourceReader($formSource);
  49. $parser = new Parser($reader);
  50. $form['questions'] = $parser->parse();
  51. $keys = generateKeys($form['email'], $form['title']);
  52. $form['id'] = $keys[1];
  53. $html = generateFormHTML($form);
  54. storeNewForm($form, $keys, $formSource);
  55. $path = saveHTML($form['title'], $keys[1], $html);
  56. echo "Questionário gerado com sucesso. Acesse o <a href='../forms/$path'>link</a> para visualizar seu questionario!";
  57. } catch (Exception $e) {
  58. echo 'Os dados do formulários são inválidos! Certifique-se que todos os dados foram enviados e estão no formato correto: '.$e->getMessage().'<br/>';
  59. return;
  60. }
  61. }
  62. function cleanTitle ($title) {
  63. $clearstring = filter_var($title, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  64. $clearstring = trim(preg_replace("/[^a-zA-Z0-9 ]/",'',$clearstring));
  65. $clearstring = trim(preg_replace("/[[:space:]]+/",'_',$clearstring));
  66. return strtolower($clearstring);
  67. }
  68. function saveHTML ($title, $folder,$html) {
  69. $date = new DateTime();
  70. $cleanTitle = cleanTitle($title);
  71. $datePart = $date->format("Y-m-j");
  72. $filePath = "$datePart-$cleanTitle-$folder.html";
  73. file_put_contents(__DIR__.'/../forms/'.$filePath, $html);
  74. return $filePath;
  75. }