From 439c93d833b651a04810d8c80d893f7d3a7a680f Mon Sep 17 00:00:00 2001
From: Tim Repke <repketim@informatik.hu-berlin.de>
Date: Fri, 8 Aug 2014 17:20:20 +0200
Subject: [PATCH] now registrationform is working, some bugfixes needed, no
 proper functionality test yet

---
 registration-system/config.inc.php         |  2 +-
 registration-system/frameworks/commons.php | 20 ++++++++++++++
 registration-system/index.php              | 31 +++++++++++++++++-----
 3 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/registration-system/config.inc.php b/registration-system/config.inc.php
index 0a036f7..ecfb021 100644
--- a/registration-system/config.inc.php
+++ b/registration-system/config.inc.php
@@ -2,7 +2,7 @@
 // https://repke.eu:8443
 // passwort manu:kuzerPenis666!
 
-$config_verbose_level = 2; // 0 = nothing, 1 = important, 2 = somewhat important, 3 = detailed verbose
+$config_verbose_level = 3; // 0 = nothing, 1 = important, 2 = somewhat important, 3 = detailed verbose
 
 $config_db = array(
     "name" => "fsfahrt",
diff --git a/registration-system/frameworks/commons.php b/registration-system/frameworks/commons.php
index aebab42..98ea7ea 100644
--- a/registration-system/frameworks/commons.php
+++ b/registration-system/frameworks/commons.php
@@ -20,4 +20,24 @@ function comm_format_date($date){
 
 function comm_get_possible_dates($fid){
     return array("12.03.2014","13.03.2014","14.03.2014");
+}
+
+function comm_isopen_fid($db_handle, $fid){
+    comm_verbose(3,"checking if fid ". $fid . " is open");
+    return $db_handle->has("fahrten", array(
+                                            "AND" => array(
+                                                "fahrt_id"=>$fid,
+                                                "regopen"=>1)));
+}
+
+function comm_generate_key($db_handle, $table, $col, $conditions){
+    again:
+    $bytes = openssl_random_pseudo_bytes(8);
+    $hex   = bin2hex($bytes);
+    comm_verbose(3,"generated hex for test: ".$hex);
+    $conditions[$col] = $hex;
+
+    if($db_handle->has($table, array("AND"=>$conditions))) goto again;
+    comm_verbose(2,"generated hex: ".$hex);
+    return $hex;
 }
\ No newline at end of file
diff --git a/registration-system/index.php b/registration-system/index.php
index 7aa868e..463505b 100644
--- a/registration-system/index.php
+++ b/registration-system/index.php
@@ -43,10 +43,12 @@ function index_show_content(){
         // --- Formular
         if(isset($_REQUEST['submit'])){ // Formular auswerten
             comm_verbose(1,"Formular bekommen");
-            index_check_form();
-        } elseif(isset($_REQUEST['bid'])){ // Änderungsformular anzeigen TODO: Anmeldung noch offen?
+            $data = index_check_form();
+            if(!is_null($data))
+                index_form_to_db($data);
+        } /*elseif(isset($_REQUEST['bid'])){ // Änderungsformular anzeigen, Anmeldung noch offen?
             index_show_formular($fid, $_REQUEST['bid']);
-        } else {                       // leeres Formular anzeigen
+        } */ else {                       // leeres Formular anzeigen
             index_show_formular($fid);
         }
 
@@ -61,6 +63,13 @@ function index_show_content(){
 
 }
 
+function index_form_to_db($data){
+    global $index_db;
+    $data['version'] = 1;
+    $data['bachelor_id'] = comm_generate_key($index_db, "bachelor", "bachelor_id", array('fahrt_id'=>$data['fahrt_id']));
+    $index_db->insert("bachelor", $data);
+}
+
 /**
  * validates the sent form
  * on failure: repost form with prefilled data and errors
@@ -68,10 +77,17 @@ function index_show_content(){
  *
  */
 function index_check_form(){
-    global $config_studitypen, $config_essen, $config_reisearten;
+    global $config_studitypen, $config_essen, $config_reisearten, $index_db;
     $errors = array();
+    $data   = array();
+
     $fid  = $_REQUEST['fid'];
-    $data = array();
+    $data['fahrt_id'] = $fid;
+    if(!comm_isopen_fid($index_db, $fid)){
+        $errors = array("Ungültige Fahrt!");
+        goto index_check_form_skip;
+    }
+
     $possible_dates = comm_get_possible_dates($fid);
 
     index_check_field('forname', '/^[a-zA-Z]{2,50}$/', $data, $errors, "Fehlerhafter oder fehlender Vorname!");
@@ -88,12 +104,13 @@ function index_check_form(){
     index_check_field('virgin', array("Ja","Nein"), $data, $errors, 'Bitte Altersbereich wählen!');
     index_check_field('comment', "comment", $data, $errors, 'Trollololol');
 
+    index_check_form_skip:
     if(count($errors)>0){
         index_show_errors($errors);
         index_show_formular($fid, NULL, $data);
+        return NULL;
     } else {
-
-        // put in DB
+        return $data;
 
     }
 
-- 
GitLab