Advertisement
Bozman2024

routes.php

Jul 2nd, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.79 KB | None | 0 0
  1. <?php
  2. $url = $_SERVER['REQUEST_URI'];
  3.  
  4. $indexPHPPosition = strpos($url,'index.php');
  5. $baseUrl = substr($url,0,$indexPHPPosition);
  6. $baseUrl = $url;
  7. if(false !== $indexPHPPosition){
  8.   $baseUrl = substr($baseUrl,0,$indexPHPPosition);
  9. }
  10.  
  11. if(substr($baseUrl,-1) !== '/'){
  12.   $baseUrl .='/';
  13. }
  14.  
  15. $route = null;
  16.  
  17. if(false !== $indexPHPPosition){
  18.   $route = substr($url,$indexPHPPosition);
  19.   $route = str_replace('index.php','',$route);
  20.  
  21. }
  22.  
  23.  
  24. $userId = getCurrentUserId();
  25. $countCartItems = countProductsInCart($userId);
  26.  
  27. setcookie('userId',$userId,strtotime('+30 days'),$baseUrl);
  28.  
  29. if(!$route){
  30.   $products = getAllProducts();
  31.   require __DIR__.'/templates/main.php';
  32.   exit();
  33. }
  34. if(strpos($route,'/cart/add/') !== false){
  35.   $routeParts = explode('/',$route);
  36.   $productId = (int)$routeParts[3];
  37.   addProductToCart($userId,$productId);
  38.   header("Location: ".$baseUrl."index.php");
  39.   exit();
  40. }
  41. if(strpos($route,'/cart') !== false){
  42.   $cartItems = getCartItemsForUserId($userId);
  43.   $cartSum = getCartSumForUserId($userId);
  44.   require __DIR__.'/templates/cartPage.php';
  45.   exit();
  46. }
  47. if(strpos($route,'/login') !== false){
  48.   $isPost = isPost();
  49.   $username ="";
  50.   $password= "";
  51.   $errors = [];
  52.   $hasErrors = false;
  53.   if($isPost){
  54.     $username = filter_input(INPUT_POST,'username',FILTER_SANITIZE_SPECIAL_CHARS);
  55.     $password = filter_input(INPUT_POST,'password');
  56.     if(false === (bool)$username){
  57.       $errors[]="Benutzername ist leer";
  58.     }
  59.     if(false === (bool)$password){
  60.       $errors[]="Passwort ist leer";
  61.     }
  62.     $userData = getUserDataForUsername($username);
  63.     if((bool)$username && 0 === count($userData)){
  64.       $errors[]="Benutzername exestiert nicht";
  65.     }
  66.     if((bool)$password &&
  67.     isset($userData['password']) &&
  68.     false === password_verify($password,$userData['password'])
  69.   ){
  70.     $errors[]="Passwort stimmt nicht";
  71.   }
  72.  
  73.     if(0 === count($errors)){
  74.       $_SESSION['userId'] = (int)$userData['id'];
  75.       moveCartProductsToAnotherUser($_COOKIE['userId'],(int)$userData['id']);
  76.  
  77.       setcookie('userId',(int)$userData['id'],strtotime('+30 days'),$baseUrl);
  78.       $redirectTarget = $baseUrl.'index.php';
  79.       if(isset($_SESSION['redirectTarget'])){
  80.           $redirectTarget  = $_SESSION['redirectTarget'];
  81.         $redirectTarget = $_SESSION['redirectTarget'];
  82.       }
  83.       header("Location: ".$redirectTarget);
  84.       header("Location: ". $redirectTarget);
  85.       exit();
  86.     }
  87.  
  88.   }
  89.   $hasErrors = count($errors) > 0;
  90.   require __DIR__.'/templates/login.php';
  91.   exit();
  92. }
  93.   if(strpos($route,'/checkout') !== false){
  94.     if(!isLoggedIn()){
  95.       $_SESSION['redirectTarget'] = $baseUrl.'index.php/checkout';
  96.       header("Location: ".$baseUrl."index.php/login");
  97.       exit();
  98.  
  99.  
  100.     }
  101.     $recipient = "";
  102.     $city ="";
  103.     $street = "";
  104.     $streetNumber = "";
  105.     $zipCode = "";
  106.     $recipentIsValid = true;
  107.     $cityIsValid = true;
  108.     $streetIsValid = true;
  109.     $streetNumberIsValid = true;
  110.     $zipCodeIsValid = true;
  111.     $errors = [];
  112.     $hasErrors = count($errors) >0;
  113.     require __DIR__.'/templates/selectDeliveryAddress.php';
  114.     exit();
  115.   }
  116.  
  117.   if(strpos($route,'/logout') !== false){
  118.     session_regenerate_id(true);
  119.     session_destroy();
  120.     $redirectTarget = $baseUrl.'index.php';
  121.     if(isset($_SESSION['redirectTarget'])){
  122.       $redirectTarget = $_SESSION['redirectTarget'];
  123.     }
  124.     header("Location: " .$redirectTarget);
  125.     exit();
  126. }
  127.  
  128.   if(strpos($route,'/deliveryAddress/add') !== false){
  129.     if(false === !isLoggedIn()){
  130.       $_SESSION['redirectTarget'] = $baseUrl.'index.php/deliveryAddress/add';
  131.       header("Location: ".$baseUrl."index.php/login");
  132.       exit();
  133.     }
  134.     $recipient = "";
  135.     $city = "";
  136.     $street = "";
  137.     $streetNumber = "";
  138.     $zipCode = "";
  139.     $recipientIsValid = true;
  140.     $cityIsValid = true;
  141.     $streetIsValid = true;
  142.     $streetNumberIsValid = true;
  143.     $zipCodeIsValid = true;
  144.     $isPost = isPost();
  145.     $errors = [];
  146.     if ($isPost) {
  147.         $recipient = filter_input(INPUT_POST, 'recipient', FILTER_SANITIZE_SPECIAL_CHARS);
  148.         $recipient = trim($recipient);
  149.         $city = filter_input(INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS);
  150.         $city = trim($city);
  151.         $street = filter_input(INPUT_POST, 'street', FILTER_SANITIZE_SPECIAL_CHARS);
  152.         $street = trim($street);
  153.         $streetNumber = filter_input(INPUT_POST, 'streetNumber', FILTER_SANITIZE_SPECIAL_CHARS);
  154.         $streetNumber = trim($streetNumber);
  155.         $zipCode = filter_input(INPUT_POST, 'zipCode', FILTER_SANITIZE_SPECIAL_CHARS);
  156.         $zipCode = trim($zipCode);
  157.  
  158.         if (!$recipient) {
  159.             $errors[] = "Bitte Empfänger eintragen";
  160.             $recipientIsValid = false;
  161.         }
  162.         if (!$city) {
  163.             $errors[] = "Bitte Stadt eintragen";
  164.             $cityIsValid = false;
  165.         }
  166.         if (!$street) {
  167.             $errors[] = "Bitte Stasse eintragen";
  168.             $streetIsValid = false;
  169.         }
  170.         if (!$streetNumber) {
  171.             $errors[] = "Bitte Hausnummer eintragen";
  172.             $streetNumberIsValid = false;
  173.         }
  174.         if (!$zipCode) {
  175.             $errors[] = "Bitte PLZ Eintragen";
  176.             $zipCodeIsValid = false;
  177.         }
  178.         if (count($errors) === 0) {
  179.             $deliveryAddresId = saveDeliveryAddressForUser($userId, $recipient, $city, $zipCode, $street, $streetNumber);
  180.             if ($deliveryAddresId > 0){
  181.                 $_SESSION['deliveryAddressId'] = $deliveryAddresId;
  182.                 header("Location: ". $baseUrl."index.php/selectPayment");
  183.                 exit();
  184.             }
  185.             $errors[]="Fehler beim Speicher der Lieferadresse";
  186.         }
  187.     }
  188.     $hasErrors = count($errors) > 0;
  189.  
  190.     require __DIR__.'/templates/selectDeliveryAddress.php';
  191.     exit();
  192.   }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement