create_form.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. require_once('../controller/validator.php');
  3. require_once('../controller/formparser.php');
  4. require_once('../controller/forms.php');
  5. require_once('../controller/util.php');
  6. require_once('../templates/templates.php');
  7. require_once('../controller/generateform.php');
  8. $KEY_SIZE = 15; // max. 32
  9. $FOLDER_SIZE = 10; // max. 32
  10. function generateKeys ($email, $title) {
  11. global $KEY_SIZE;
  12. global $FOLDER_SIZE;
  13. $date = new DateTime();
  14. $input = $date->format("c").$email.$title;
  15. $sha = hash("sha256", $input);
  16. $key = array();
  17. $folder = array();
  18. $i = 0;
  19. for (; $i < $KEY_SIZE; $i++) {
  20. $key[] = $sha[$i];
  21. }
  22. for ($j = 0;$j < $FOLDER_SIZE; $j++ ) {
  23. $folder[] = $sha[$i+$j];
  24. }
  25. return [implode("", $key), implode("", $folder)];
  26. }
  27. if (empty($_POST)) {
  28. generateCreateForm();
  29. } else {
  30. proccessRequest($_POST);
  31. }
  32. function generateCreateForm () {
  33. $template = getTemplate('create_form.html');
  34. $context = array('post_url' => '../app/create_form.php');
  35. $template = parseTemplate($template, $context);
  36. echo $template;
  37. }
  38. function proccessRequest ($data) {
  39. try {
  40. Validator::check(['email', 'title', 'description','thanks'], $data);
  41. Validator::check(['formSource'], $_FILES);
  42. $from = array();
  43. $form['email'] = Validator::email($data['email']);
  44. $form['title'] = Validator::str($data['title']);
  45. $form['description'] = Validator::str($data['description']);
  46. $form['thanks'] = Validator::str($data['thanks']);
  47. $upload = Validator::file($_FILES['formSource'], 'txt');
  48. $formSource = file_get_contents($upload['tmp_name']);
  49. $reader = new SourceReader($formSource);
  50. $parser = new Parser($reader);
  51. $form['questions'] = $parser->parse();
  52. $keys = generateKeys($form['email'], $form['title']);
  53. $form['id'] = $keys[1];
  54. $html = generateFormHTML($form);
  55. storeNewForm($form, $keys, $formSource);
  56. $id = $keys[1];
  57. $link = generateURI("/forms/viewer.php?id=$id");
  58. $context = ['link'=> htmlspecialchars($link), 'secret'=>$keys[0], 'title'=>$form['title']];
  59. $success = getTemplate('created_successful.html');
  60. echo parseTemplate($success, $context);
  61. } catch (Exception $e) {
  62. 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/>';
  63. return;
  64. }
  65. }
  66. /*function saveHTML ($title, $folder,$html) {
  67. $date = new DateTime();
  68. $cleanTitle = cleanTitle($title);
  69. $datePart = $date->format("Y-m-j");
  70. $filePath = "$dateart-$cleanTitle-$folder.html";
  71. file_put_contents('../forms/'.$filePath, $html);
  72. return $filePath;
  73. }*/