Bläddra i källkod

Implement new property to store server uri to avoid problems when using
proxies
Implement uri generation function to creates links to forms

Lucas de Souza 3 år sedan
förälder
incheckning
9030bf5f54
5 ändrade filer med 26 tillägg och 20 borttagningar
  1. 3 4
      app/create_form.php
  2. 7 6
      config/linequest.php
  3. 0 7
      controller/forms.php
  4. 14 0
      controller/util.php
  5. 2 3
      explorer/index.php

+ 3 - 4
app/create_form.php

@@ -2,6 +2,7 @@
 require_once('../controller/validator.php');
 require_once('../controller/formparser.php');
 require_once('../controller/forms.php');
+require_once('../controller/util.php');
 require_once('../templates/templates.php');
 require_once('../controller/generateform.php');
 
@@ -60,10 +61,8 @@ function proccessRequest ($data) {
         $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']];
+        $link = generateURI('/forms/'.$path);
+        $context = ['link'=> htmlspecialchars($link), 'secret'=>$keys[0], 'title'=>$form['title']];
         $success = getTemplate('created_successful.html');
         echo parseTemplate($success, $context);
 

+ 7 - 6
config/linequest.php

@@ -3,24 +3,25 @@
 /**
  * Este arquivo é parte do software linequest
  * Ambiente de questionários para a coleta de dados
- * 
+ *
  * Laboratório de Informática na Educação - LInE
  * https://www.usp.br/line/
- * 
+ *
  * Utilize os atributos definidos abaixo para
  * configurar o ambiente de questionários.
- * 
+ *
  * @author Lucas Calion
  * @author Igor Félix
  */
     global $CFG;
     $CFG = new stdClass();
-    
-    $CFG->dbhost    = 'localhost'; # Endereço do hospedeiro a ser acessado
+
+    $CFG->dbhost    = ''; # Endereço do hospedeiro a ser acessado
     $CFG->dbname    = ''; # Nome da base criada no MySQL ou MariaDB
     $CFG->dbuser    = '';  # Usuário com permissão de leitura/escrita na base
     $CFG->dbpass    = ''; # Senha do usuário
     $CFG->viewpass  = ''; # Senha para visualizar os dados no navegador
+    $CFG->server_uri = ''; # URL base do site para evitar problemas nos links quando por trás de um proxy
 
     # SMTP:
     $CFG->smtp = new stdClass();
@@ -31,4 +32,4 @@
     $CFG->smtp->from     = '';
 
 
-?>
+?>

+ 0 - 7
controller/forms.php

@@ -16,13 +16,6 @@
 
  require_once ('../config/linequest.php');
 
- function cleanTitle ($title) {
-    $clearstring = filter_var($title, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
-    $clearstring = trim(preg_replace("/[^a-zA-Z0-9 ]/",'',$clearstring));
-    $clearstring = trim(preg_replace("/[[:space:]]+/",'_',$clearstring));
-    return strtolower($clearstring);
- }
-
  function connect () {
     global $CFG, $DB;
     $DB  = new mysqli($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname);

+ 14 - 0
controller/util.php

@@ -0,0 +1,14 @@
+<?php
+require_once('../config/linequest.php');
+
+function cleanTitle ($title) {
+    $clearstring = filter_var($title, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
+    $clearstring = trim(preg_replace("/[^a-zA-Z0-9 ]/",'',$clearstring));
+    $clearstring = trim(preg_replace("/[[:space:]]+/",'_',$clearstring));
+    return strtolower($clearstring);
+ }
+
+function generateURI ($path) {
+   global $CFG;
+   return $CFG->server_uri.$path;
+}

+ 2 - 3
explorer/index.php

@@ -86,6 +86,7 @@
 
     <?php
     require_once('../controller/forms.php');
+    require_once('../controller/util.php');
     $secret = $_SESSION['hash_user'];
     $form_id = get_user_form($secret);
     $info = getQuestionaireInfoBySecret($secret);
@@ -95,9 +96,7 @@
     $date = new DateTime($info['date']);
     $datePart = $date->format("Y-m-j");
     $filePath = "$datePart-$cleanTitle-$folder.html";
-    $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ?
-                "https" : "http") . "://" . $_SERVER['HTTP_HOST'] .
-                '/forms/'.$filePath;
+    $link = generateURI('/forms/'.$filePath);
     echo "<p>Link para seu questionário: <a target='_blank' href='$link'>$link</a></p>"
     ?>