save.php
3375a8a6
 <?php
 session_start();
 
 if (! isset($_SESSION['handle'])) {
     $_SESSION['handle'] = sprintf('%09s', ( round(microtime(true)*10) % 1000000000));
     $_SESSION['date'] = date('c');
     $_SESSION['complete'] = false;
 }
 
 $redirect = NULL;
 if (isset($_REQUEST['form'])) {
     switch ($_REQUEST['form']) {
64ed8519
         case 'start':
3375a8a6
             $number = NULL;
             if (isset($_REQUEST['phone']) && trim($_REQUEST['phone']) != '') {
9402d799
                 $number = trim($_REQUEST['phone']);
3375a8a6
             }
             if (! $number) {
                 $redirect = 'index.php?error&num=';
                 break;
             }
9402d799
             if (substr($number, 0, 1) != 0 && substr($number, 0, 3) != '+49' && substr($number, 0, 4) != '0049') {
                 $number = '07192'.$number;
             }
3375a8a6
 
             require_once('vendor/autoload.php');
 
             function format_number($number) {
                 $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
                 try {
                     $phoneNumber = $phoneNumberUtil->parse($number, 'DE');
                 } catch (Exception $e) {
                     return NULL;
                 }
                 if ($phoneNumberUtil->isValidNumber($phoneNumber)) {
                     return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::NATIONAL);
                 }
                 return NULL;
             }
             $number = format_number($number);
 
03ef9c50
             $_SESSION['phone'] = htmlspecialchars($number);
b7442cef
             if (isset($_REQUEST['save']) && $_REQUEST['save'] == 'yes') {
                 setcookie('NUMBER', $number, time() + 24*3600*500, "");
             }
3375a8a6
 
             if (! $_SESSION['phone']) {
9402d799
                 $redirect = 'index.php?error&num='.urlencode($_REQUEST['phone']);
3375a8a6
                 break;
             }   
 
d72f67d6
             $redirect = 'address.php';
3375a8a6
             break;
 
64ed8519
         case 'address':
3375a8a6
             $fields = array("fname", "lname", "address", "zip", "city");
             foreach ($fields as $f) {
                 if (isset($_REQUEST[$f])) {
03ef9c50
                     $_SESSION[$f] = trim(htmlspecialchars($_REQUEST[$f]));
3375a8a6
                 } else {
                     if (! isset($_SESSION[$f])) {
                         $_SESSION[$f] = '';
                     }
                 }
             }
9b550393
             if ($_SESSION['lname'] == '') {
                 $redirect = 'address.php?error';
                 break;
             }
64ed8519
             if (isset($_SESSION['angeliefert'])) {
                 if ($_SESSION['angeliefert']) {
                     $redirect = 'gitterbox.php';
                 } else {
                     $redirect = 'gitterbox_spaeter.php';
                 }
             } else {
                 $redirect = 'anlieferung.php';
             }
3375a8a6
             break;
 
64ed8519
         case 'anlieferung':
             if (isset($_REQUEST['schonda'])) {
                 $_SESSION['angeliefert'] = true;
                 $redirect = 'gitterbox.php';
3375a8a6
             }
64ed8519
             if (isset($_REQUEST['kommtnoch'])) {
                 $_SESSION['angeliefert'] = false;
                 $redirect = 'gitterbox_spaeter.php';
             }
             break;
         case 'gitterbox':
3375a8a6
             if (isset($_REQUEST['lieferart'])) {
                 if ($_REQUEST['lieferart'] == 'gitterbox') {
                     $_SESSION['lieferart'] = 'gitterbox';
03ef9c50
                     $_SESSION['gbcount'] = htmlspecialchars($_REQUEST['gbcount']);
3375a8a6
                 }
                 if ($_REQUEST['lieferart'] == 'anhaenger') {
                     $_SESSION['lieferart'] = 'anhaenger';
03ef9c50
                     $_SESSION['kennz'] = htmlspecialchars($_REQUEST['kennz']);
64ed8519
                     if ($_SESSION['angeliefert'] === false && ! $_SESSION['kennz']) {
                         $redirect = 'gitterbox_spaeter.php?error';
                         break;
                     }
3375a8a6
                 }
             }
d72f67d6
             $redirect = 'gebrauchte.php';
3375a8a6
             break;
 
64ed8519
          case 'gebrauchte':
3375a8a6
             if (isset($_REQUEST['kartonsja'])) {
                 $_SESSION['kartons'] = true;
             } 
             if (isset($_REQUEST['kartonsnein'])) {
                 $_SESSION['kartons'] = false;
             }
 
d72f67d6
             $redirect = 'auftrag.php';
3375a8a6
             break;
64ed8519
          case 'auftrag':
3375a8a6
             if (isset($_REQUEST['5er'])) {
                 $_SESSION['neue'] = '5er';
             }
             if (isset($_REQUEST['10er'])) {
                 $_SESSION['neue'] = '10er';
             }
             if (isset($_REQUEST['submit']) && isset($_REQUEST['sonstiges']) && $_REQUEST['sonstiges'] != '') {
                 $_SESSION['neue'] = 'sonstiges';
03ef9c50
                 $_SESSION['sonstiges'] = htmlspecialchars($_REQUEST['sonstiges']);
3375a8a6
             }
 
 
d72f67d6
             $redirect = 'frischsaft.php';
3375a8a6
             break;
 
64ed8519
          case 'frischsaft':
3375a8a6
             if (isset($_REQUEST['frischsaftnein'])) {
                 $_SESSION['frischsaft'] = 0;
             } elseif (isset($_REQUEST['frischsaft'])) {
03ef9c50
                 $_SESSION['frischsaft'] = htmlspecialchars($_REQUEST['frischsaft']);
3375a8a6
             }
 
d72f67d6
             $redirect = 'summary.php';
3375a8a6
             break;
64ed8519
         case 'summary':
03ef9c50
             if (isset($_REQUEST['anmerkungen'])) {
                 $_SESSION['anmerkungen'] = htmlspecialchars($_REQUEST['anmerkungen']);
             }
3375a8a6
             if (!isset($_REQUEST['agb'])) {
d72f67d6
                 $redirect = 'summary.php?error';
3375a8a6
                 break;
             }
 
             $_SESSION['complete'] = true;
             $redirect = 'finish.php?handle='.$_SESSION['handle'];
             break;
 
     }
 }
 
 
 $filename = '../data/'.date('Y').'/'.$_SESSION['handle'].'.json';
 if (!file_exists(dirname($filename))) {
     mkdir(dirname($filename), 0700, true);
 }
 
 $json = json_encode($_SESSION);
 file_put_contents($filename, $json);
 
 header('Location: '.$redirect);