From ea00b5f5dc36464af3462fe1bb6f5533a8b3f02f Mon Sep 17 00:00:00 2001
From: Tim Repke <timmothey@gmx.de>
Date: Sun, 16 Aug 2015 21:22:24 +0200
Subject: [PATCH] in between states, started restructuring, added signup method
 classes [WIP]

---
 registration-system/index.php                 |   1 -
 registration-system/view/js/api.js            |  13 +-
 registration-system/view/js/soft_protect.js   |  12 -
 .../view/signups/abstract_signup_class.php    | 209 ++++++++++++++++++
 .../view/signups/form/index.php               |  36 +++
 .../view/signups/form/style.css               |  61 +++++
 registration-system/view/signups/index.php    |  79 +++++++
 .../story => signups/story/graphics}/age.png  | Bin
 .../story/graphics}/begin.png                 | Bin
 .../story => signups/story/graphics}/bell.png | Bin
 .../story => signups/story/graphics}/eat.png  | Bin
 .../story/graphics}/ticket.png                | Bin
 .../story/graphics}/travelBegin.png           | Bin
 .../story/graphics}/travelEnd.png             | Bin
 .../story/graphics}/umleitung.png             | Bin
 .../story => signups/story/graphics}/warn.png | Bin
 .../view/{js => signups/story}/story.js       |   0
 17 files changed, 397 insertions(+), 14 deletions(-)
 delete mode 100644 registration-system/view/js/soft_protect.js
 create mode 100644 registration-system/view/signups/abstract_signup_class.php
 create mode 100644 registration-system/view/signups/form/index.php
 create mode 100644 registration-system/view/signups/form/style.css
 create mode 100644 registration-system/view/signups/index.php
 rename registration-system/view/{graphics/story => signups/story/graphics}/age.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/begin.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/bell.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/eat.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/ticket.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/travelBegin.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/travelEnd.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/umleitung.png (100%)
 rename registration-system/view/{graphics/story => signups/story/graphics}/warn.png (100%)
 rename registration-system/view/{js => signups/story}/story.js (100%)

diff --git a/registration-system/index.php b/registration-system/index.php
index 451525e..9ef255f 100644
--- a/registration-system/index.php
+++ b/registration-system/index.php
@@ -199,7 +199,6 @@ function index_check_form(){
         return $data;
 
     }
-
 }
 
 /**
diff --git a/registration-system/view/js/api.js b/registration-system/view/js/api.js
index 4ea0968..7f2bd3a 100644
--- a/registration-system/view/js/api.js
+++ b/registration-system/view/js/api.js
@@ -21,4 +21,15 @@ function api_send_signup(params, callback) {
         messages: 'Successful signup',
         errors: null
     })
-}
\ No newline at end of file
+}
+
+function api_soft_protect(elementIds, regex) {
+    for(var i = 0; i < elementIds.length; ++i) {
+        $('#'+elementIds[i]).keyup(function(event) {
+            if (!event.target.value.match(regex))
+                event.target.style.backgroundColor="#f00";
+            else
+                event.target.style.backgroundColor="#fff";
+        });
+    }
+}
diff --git a/registration-system/view/js/soft_protect.js b/registration-system/view/js/soft_protect.js
deleted file mode 100644
index 0de6a28..0000000
--- a/registration-system/view/js/soft_protect.js
+++ /dev/null
@@ -1,12 +0,0 @@
-function soft_protect(elementIds, regex)
-{
-	for(var i = 0; i < elementIds.length; ++i)
-	{
-		$('#'+elementIds[i]).keyup(function(event) {
-			if (!event.target.value.match(regex))
-				event.target.style.backgroundColor="#f00";
-			else
-				event.target.style.backgroundColor="#fff";
-		});
-	}
-}
diff --git a/registration-system/view/signups/abstract_signup_class.php b/registration-system/view/signups/abstract_signup_class.php
new file mode 100644
index 0000000..c45a855
--- /dev/null
+++ b/registration-system/view/signups/abstract_signup_class.php
@@ -0,0 +1,209 @@
+<?php
+
+abstract class SignupMethod {
+
+    // =================================================================================================================
+    // Abstract functions
+    // to be implemented by each method
+    // =================================================================================================================
+
+    /**
+     * @return string with humanly readable name of this method
+     */
+    abstract public static function getName();
+
+    /**
+     * @return string with a short description
+     */
+    abstract public static function getAltText();
+
+    /**
+     * This method will return some meta info about that method. It should return an associative array of that form:
+     *
+     * [
+     *   version      => '1.0',
+     *   date         => '20.08.2014',
+     *   contributors => ['MH <mail>', 'TR <mail>']
+     * ]
+     *
+     * @return object containing meta info (see description)
+     */
+    abstract public static function getMetaInfo();
+
+    /**
+     * @return array of (relative to '/view/signups/<folder>/') scripts to include in frontend
+     */
+    abstract public function getJSDependencies();
+
+    /**
+     * @return array of (relative to '/view/signups/<folder>/') css files to include in frontend
+     */
+    abstract public function getCSSDependencies();
+
+    /**
+     * @return string to be added to to the HTML page header
+     */
+    abstract public function getAdditionalHeader();
+
+    /**
+     * @return string containing the necessary HTML code to bind the stuff needed into
+     * the placeholder on the signup page
+     */
+    abstract public function getInlineHTML();
+
+
+
+
+
+    // =================================================================================================================
+    // Shared functions
+    // to be implemented by each method
+    // =================================================================================================================
+
+    /**
+     * This function takes submitted form data from $_REQUEST and validates the input.
+     *
+     * returns assoc array looking like this:
+     * [
+     *   "valid" => true|false,
+     *   "message" => 'a message',
+     *   "errors"  => ['array of', 'errors'],
+     *   "data"    => [<validated data as assoc array>]
+     * ]
+     *
+     * @return array (see above)
+     */
+    private function validateSubmission() {
+        global $config_studitypen, $config_essen, $config_reisearten, $index_db, $invalidCharsRegEx;
+        $errors = [];
+        $data   = [];
+
+        $fid  = $_REQUEST['fid'];
+        $data['fahrt_id'] = $fid;
+        if(comm_isopen_fid_helper($index_db, $fid)>1){
+            $errors = ["Ungültige Fahrt!"];
+        } else {
+            $possible_dates = comm_get_possible_dates($index_db, $fid);
+
+            $this->validateField('forname', $invalidCharsRegEx, $data, $errors, "Fehlerhafter oder fehlender Vorname!");
+            $this->validateField('sirname', $invalidCharsRegEx, $data, $errors, "Fehlerhafter oder fehlender Nachname!");
+            $this->validateField('pseudo', $invalidCharsRegEx, $data, $errors, "Fehlerhafter oder fehlender Anzeigename!");
+            $this->validateField('mehl', 'mail', $data, $errors, "Fehlerhafte oder fehlende E-Mail-Adresse!");
+            $this->validateField('anday', array_slice($possible_dates,0, -1), $data, $errors, 'Hilfe beim Ausfüllen: <a href="https://www.hu-berlin.de/studium/bewerbung/imma/exma">hier klicken!</a>');
+            $this->validateField('antyp', $config_reisearten, $data, $errors, 'Trolle hier lang: <a href="https://www.hu-berlin.de/studium/bewerbung/imma/exma">hier klicken!</a>');
+            $this->validateField('abday', array_slice($possible_dates,1), $data, $errors, 'Ruth hat mitgedacht: <a href="https://www.hu-berlin.de/studium/bewerbung/imma/exma">hier klicken!</a>');
+            $this->validateField('abtyp', $config_reisearten, $data, $errors, 'Entwickler Bier geben und: <a href="https://www.hu-berlin.de/studium/bewerbung/imma/exma">hier klicken!</a>');
+            $this->validateField('essen', $config_essen, $data, $errors, 'Hat das wirklich nicht gereicht??'); // ggf trollable machen mit /^[a-zA-Z]{2,50}$/
+            $this->validateField('studityp', $config_studitypen, $data, $errors, 'Neue Chance, diesmal FS-Ini wählen!');
+            $this->validateField('public', 'public', $data, $errors, 'Trollololol');
+            $this->validateField('virgin', array("Ja","Nein"), $data, $errors, 'Bitte Altersbereich wählen!');
+            $this->validateField('comment', 'comment', $data, $errors, 'Trollololol');
+            $this->validateField('captcha', 'captcha', $data, $errors, 'Captcha falsch eingegeben.');
+
+            if($data['anday'] == $data['abday'])
+                array_push($errors, "Anreisetag = Abreisetag -> Bitte prüfen!");
+        }
+
+        if(count($errors)>0){
+            return [
+                "valid"   => false,
+                "errors"  => $errors,
+                "message" => "Fehlerhafte Angaben!",
+                "data"    => $data
+            ];
+        } else {
+            return [
+                "valid"   => true,
+                "errors"  => null,
+                "message" => "Angaben gültig!",
+                "data"    => $data
+            ];
+        }
+    }
+
+    /**
+     * checks for correctness of a given field ($index) by trying $check.
+     * pushes $errmess into $errarr, if $check fails
+     * pushes empty data on fail or correct data on success into $data
+     *
+     * check can be regex or array or special (public, mail, comment).
+     * if array, than check only succeeds if sent data is inside that array
+     *
+     * @param $index
+     * @param $check
+     * @param $datarr
+     * @param $errarr
+     * @param $errmess
+     */
+    function validateField($index, $check, &$datarr, &$errarr, $errmess){
+        $pushdat = "";
+        comm_verbose(3,"checking ".$index);
+
+        // check that first because if unchecked it doesnt exist
+        if($check == "public"){
+            if(isset($_REQUEST[$index])) $datarr[$index] = 0;
+            else  $datarr[$index] = 1;
+        }
+
+        // if index is missing -> error!
+        elseif(!isset($_REQUEST[$index])){
+            array_push($errarr, $errmess);
+
+            // set it in every case so corrections are possible
+            $datarr[$index] = "";
+        }
+
+        // index is there -> check if value is allowed
+        else {
+            $tmp = trim($_REQUEST[$index]);
+
+            // do specific check if a set of variables is allowed
+            if(is_array($check)){
+                if(!in_array($tmp,$check))
+                    array_push($errarr, $errmess);
+                else
+                    $datarr[$index] = $tmp;
+            }
+
+            // check captcha
+            elseif($check == "captcha"){
+                if(isset($_SESSION['captcha']) && strtolower($tmp) == strtolower($_SESSION['captcha'])){
+                    unset($_SESSION['captcha']);
+                } else{
+                    array_push($errarr, $errmess);
+                    $datarr[$index] = "";
+                }
+            }
+
+            // check mail address
+            elseif($check == "mail"){
+                if(!filter_var($tmp, FILTER_VALIDATE_EMAIL))
+                    array_push($errarr, $errmess);
+
+                // set it in every case so corrections are possible
+                $datarr[$index] = $tmp;
+            }
+
+            // check comment field
+            elseif($check == "comment"){
+                $datarr[$index] = htmlspecialchars($tmp, ENT_QUOTES);
+            }
+
+            // check virgin field
+            elseif($index == "virgin"){
+                if($_REQUEST[$index]=="Ja") $datarr[$index] = 0; // NOTE: for consistency: virgin = 0 means > 18
+                else  $datarr[$index] = 1;
+            }
+
+            //everything else
+            else {
+                // check with regex
+                if(!(preg_match($check, $tmp)==1))
+                    array_push($errarr, $errmess);
+
+                // set it in every case so corrections are possible
+                $datarr[$index] = $tmp;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/registration-system/view/signups/form/index.php b/registration-system/view/signups/form/index.php
new file mode 100644
index 0000000..c7eeec8
--- /dev/null
+++ b/registration-system/view/signups/form/index.php
@@ -0,0 +1,36 @@
+<?php
+
+class FormSignupMethod extends SignupMethod {
+
+    public static function getName() {
+        return "Langweiliges Formular";
+    }
+
+    public static function getAltText() {
+        return "Seite zu bunt? Kein JavaScript? Oder einfach nur Langweiler?";
+    }
+
+    public static function getMetaInfo() {
+        return [
+            "version"      => '1.1',
+            "date"         => '20.09.2014',
+            "contributors" => ['Tim Repke <tim@repke.eu>']
+        ];
+    }
+
+    public function getJSDependencies() {
+        return [];
+    }
+
+    public function getCSSDependencies() {
+        return ['style.css'];
+    }
+
+    public function getAdditionalHeader() {
+        return '';
+    }
+
+    public function getInlineHTML() {
+
+    }
+}
\ No newline at end of file
diff --git a/registration-system/view/signups/form/style.css b/registration-system/view/signups/form/style.css
new file mode 100644
index 0000000..6936027
--- /dev/null
+++ b/registration-system/view/signups/form/style.css
@@ -0,0 +1,61 @@
+/* ----------- My Form ----------- */
+.myform{
+    margin:0 auto;
+    width:400px;
+    padding:14px;
+}
+
+/* ----------- stylized ----------- */
+#stylized{
+    border:solid 2px #b7ddf2;
+    background:#ebf4fb;
+}
+#stylized h1 {
+    font-size:1.5em;
+
+    color:black;
+    font-weight:bold;
+    margin-bottom:8px;
+}
+#stylized p{
+    font-size:11px;
+    color:#666666;
+    margin-bottom:20px;
+    border-bottom:solid 1px #b7ddf2;
+    padding-bottom:10px;
+}
+#stylized label{
+    display:block;
+    font-weight:bold;
+    text-align:right;
+    width:140px;
+    float:left;
+}
+#stylized .small{
+    color:#666666;
+    display:block;
+    font-size:11px;
+    font-weight:normal;
+    text-align:right;
+    width:140px;
+}
+#stylized input, #stylized select, #stylized textarea{
+    float:left;
+    font-size:12px;
+    padding:4px 2px;
+    border:solid 1px #aacfe4;
+    width:200px;
+    margin:2px 0 20px 10px;
+}
+#stylized button{
+    clear:both;
+    margin-left:150px;
+    width:125px;
+    height:31px;
+    background:#666666 url(img/button.png) no-repeat;
+    text-align:center;
+    line-height:31px;
+    color:#FFFFFF;
+    font-size:11px;
+    font-weight:bold;
+}
\ No newline at end of file
diff --git a/registration-system/view/signups/index.php b/registration-system/view/signups/index.php
new file mode 100644
index 0000000..3f34a9b
--- /dev/null
+++ b/registration-system/view/signups/index.php
@@ -0,0 +1,79 @@
+<?php
+
+require_once "abstract_signup_class.php";
+
+class SignupMethods {
+
+    private $signup_methods = [];
+
+    function __construct() {
+        $this->signup_methods = $this->loadSignupMethods();
+    }
+
+    public function getSignupMethods() {
+        return $this->signup_methods;
+    }
+
+    /**
+     * @return array of assoc_arrays with name, desc and contribs
+     */
+    public function getSignupMethodsBaseInfo() {
+        $tmp = [];
+        foreach($this->signup_methods as $method) {
+            array_push($tmp, [
+                "name"         => $method["class"]::getName(),
+                "description"  => $method["class"]::getAltText(),
+                "contributors" => $method["class"]::getMetaInfo()
+            ]);
+        }
+        return $tmp;
+    }
+
+    /**
+     * @return class (instantiated) of the active signup method
+     * @throws ErrorException when $_GET["method"] is missing or not available in the list
+     */
+    public function getActiveMethod() {
+        if(!isset($_GET["method"])) throw new ErrorException("No signup-method selected!");
+        $mode = $_GET["method"];
+        if(!isset($this->signup_methods[$mode])) throw new ErrorException("Signup-method does not exist!");
+
+        return new $this->signup_methods->$mode->class();
+    }
+
+    private function getMethodDirs() {
+        return glob(__DIR__ . '/*' , GLOB_ONLYDIR);
+    }
+
+    private function loadSignupMethod($folder_name) {
+        $tmp_file_name = __DIR__ . '/' . $folder_name . '/index.php';
+
+        try {
+            if (file_exists($tmp_file_name)) {
+                require_once $tmp_file_name;
+
+                $tmp_class_name = ucfirst($folder_name . 'SignupMethod');
+                if (class_exists($tmp_class_name)) {
+                    return [
+                        'class' => $tmp_class_name,
+                        'classFile' => $tmp_file_name
+                    ];
+                }
+            }
+        } catch (Exception $e) { /* do nothing */  }
+
+        return false;
+    }
+
+    private function loadSignupMethods() {
+        $tmp_method_dirs = $this->getMethodDirs();
+        $tmp_methods     = [];
+
+        foreach ($tmp_method_dirs as $method_dir) {
+            $tmp_method = $this->loadSignupMethod($method_dir);
+            if ($tmp_method) $tmp_methods[$method_dir] =  $tmp_method;
+        }
+
+        return $tmp_methods;
+    }
+}
\ No newline at end of file
diff --git a/registration-system/view/graphics/story/age.png b/registration-system/view/signups/story/graphics/age.png
similarity index 100%
rename from registration-system/view/graphics/story/age.png
rename to registration-system/view/signups/story/graphics/age.png
diff --git a/registration-system/view/graphics/story/begin.png b/registration-system/view/signups/story/graphics/begin.png
similarity index 100%
rename from registration-system/view/graphics/story/begin.png
rename to registration-system/view/signups/story/graphics/begin.png
diff --git a/registration-system/view/graphics/story/bell.png b/registration-system/view/signups/story/graphics/bell.png
similarity index 100%
rename from registration-system/view/graphics/story/bell.png
rename to registration-system/view/signups/story/graphics/bell.png
diff --git a/registration-system/view/graphics/story/eat.png b/registration-system/view/signups/story/graphics/eat.png
similarity index 100%
rename from registration-system/view/graphics/story/eat.png
rename to registration-system/view/signups/story/graphics/eat.png
diff --git a/registration-system/view/graphics/story/ticket.png b/registration-system/view/signups/story/graphics/ticket.png
similarity index 100%
rename from registration-system/view/graphics/story/ticket.png
rename to registration-system/view/signups/story/graphics/ticket.png
diff --git a/registration-system/view/graphics/story/travelBegin.png b/registration-system/view/signups/story/graphics/travelBegin.png
similarity index 100%
rename from registration-system/view/graphics/story/travelBegin.png
rename to registration-system/view/signups/story/graphics/travelBegin.png
diff --git a/registration-system/view/graphics/story/travelEnd.png b/registration-system/view/signups/story/graphics/travelEnd.png
similarity index 100%
rename from registration-system/view/graphics/story/travelEnd.png
rename to registration-system/view/signups/story/graphics/travelEnd.png
diff --git a/registration-system/view/graphics/story/umleitung.png b/registration-system/view/signups/story/graphics/umleitung.png
similarity index 100%
rename from registration-system/view/graphics/story/umleitung.png
rename to registration-system/view/signups/story/graphics/umleitung.png
diff --git a/registration-system/view/graphics/story/warn.png b/registration-system/view/signups/story/graphics/warn.png
similarity index 100%
rename from registration-system/view/graphics/story/warn.png
rename to registration-system/view/signups/story/graphics/warn.png
diff --git a/registration-system/view/js/story.js b/registration-system/view/signups/story/story.js
similarity index 100%
rename from registration-system/view/js/story.js
rename to registration-system/view/signups/story/story.js
-- 
GitLab