12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- require_once('../controller/validator.php');
- require_once('../controller/formparser.php');
- require_once('../controller/forms.php');
- require_once('../templates/templates.php');
- require_once('../controller/generateform.php');
- $KEY_SIZE = 15; // max. 32
- $FOLDER_SIZE = 10; // max. 32
- function generateKeys ($email, $title) {
- global $KEY_SIZE;
- global $FOLDER_SIZE;
- $date = new DateTime();
- $input = $date->format("c").$email.$title;
- $sha = hash("sha256", $input);
- $key = array();
- $folder = array();
- $i = 0;
- for (; $i < $KEY_SIZE; $i++) {
- $key[] = $sha[$i];
- }
- for ($j = 0;$j < $FOLDER_SIZE; $j++ ) {
- $folder[] = $sha[$i+$j];
- }
- return [implode("", $key), implode("", $folder)];
- }
- if (empty($_POST)) {
- generateCreateForm();
- } else {
- proccessRequest($_POST);
- }
- function generateCreateForm () {
- $template = getTemplate('create_form.html');
- $context = array('post_url' => htmlspecialchars($_SERVER["PHP_SELF"]));
- $template = parseTemplate($template, $context);
- echo $template;
- }
- function proccessRequest ($data) {
- try {
- Validator::check(['email', 'title', 'description','thanks'], $data);
- Validator::check(['formSource'], $_FILES);
- $from = array();
- $form['email'] = Validator::email($data['email']);
- $form['title'] = Validator::str($data['title']);
- $form['description'] = Validator::str($data['description']);
- $form['thanks'] = Validator::str($data['thanks']);
- $upload = Validator::file($_FILES['formSource'], 'txt');
- $formSource = file_get_contents($upload['tmp_name']);
- $reader = new SourceReader($formSource);
- $parser = new Parser($reader);
- $form['questions'] = $parser->parse();
- $keys = generateKeys($form['email'], $form['title']);
- $form['id'] = $keys[1];
- $html = generateFormHTML($form);
- storeNewForm($form, $keys, $formSource);
- $path = saveHTML($form['title'], $keys[1], $html);
- $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ?
- "https" : "http") . "://" . $_SERVER['HTTP_HOST'] .
- '/forms/'.$path;
- $context = ['link'=>$link, 'secret'=>$keys[0], 'title'=>$form['title']];
- $success = getTemplate('created_successful.html');
- echo parseTemplate($success, $context);
- } catch (Exception $e) {
- 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/>';
- return;
- }
- }
- function saveHTML ($title, $folder,$html) {
- $date = new DateTime();
- $cleanTitle = cleanTitle($title);
- $datePart = $date->format("Y-m-j");
- $filePath = "$datePart-$cleanTitle-$folder.html";
- file_put_contents(__DIR__.'/../forms/'.$filePath, $html);
- return $filePath;
- }
|