HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/php/page_cart.php
<?php


/* 
	Deense Kroon
	Shopping Cart
	
	Mike Ontwerpt 2012
	www.mikeontwerpt.nl
	
	initCart(); 

*/

function initCart(){
	$output = '';

	require_once './multisafepay/MultiSafepay.class.php';
	require_once './multisafepay/MultiSafepay.config.php';
	
	if(isset($_POST['validate-cart'])){
		validateCart();
	}
	else{
		// dispatch page \\			
		$cartOutput = '';	
		if(defined('URL_SUB')){
			$subpage = URL_SUB;
			switch($subpage){
				case 'winkelwagen':
                    $_SESSION['cart']['validate']['userdata'] = '';
                    $_SESSION['cart']['validate']['payment'] = '';


					if(defined('URL_SUB2')){
						$action = URL_SUB2;
						editCart($action);
					}
					else{
						$cartOutput .= showCart();
					}
				break;
				case 'uw-gegevens':
                    require_once DOCUMENT_ROOT . 'php/one_page_checkout/check_out.class.php';
                    $CheckOut = new Check_Out();
                    $cartOutput .= $CheckOut->init();
				break;
				case 'bevestiging':
                    require_once DOCUMENT_ROOT . 'php/one_page_checkout/check_out.class.php';
                    $CheckOutClosing = new Check_Out_Closing();
                    $cartOutput .= $CheckOutClosing->showConfirmation();
				break;
				default:
					$cartOutput .= showCart();
			}			
		}	
		else{
            $cartOutput .= showCart();
        }

		// set up basic output \\
		$output .= '<div id="cart-pageheader">';
        //$output .= '<h1><span class="icon-shopping-cart"></span> Mijn winkelwagen</h1>';

        
        $output .= '</div>';
		$output .= '<div id="cart-content">';
			//process
			$output .= showCartProcess();
            $output .= '<div class="clear"></div>';
			$output .= $cartOutput;
		$output .= '</div>';
	}
		
	return $output;
}

// shows the progress bar \\
function showCartProcess(){
	
	include './lang/'.$_SESSION['lang'].'.php';	
	
	//get current subpage (for the active arrow)
	$subpage = 'winkelwagen';
	if(defined('URL_SUB')) $subpage = URL_SUB;
	
	$output = '';
	$output .= '<ul id="cart-process">';
		////////// winkelwagen \\\\\\\\\\
		$_SESSION['cart']['validate']['cart'] == 'ok' ? $validCart = true : $validCart = false;
		$_SESSION['cart']['validate']['cart'] == 'err' ? $errCart = true : $errCart = false;
		
		//check arrow and color
		$subpage == 'winkelwagen' ? $output .= '<li class="active-cart">' : $output .= '<li>';
        //check icon
        if($validCart){
            $output .= '<span class="icon-fm-ok"></span>';
        }
        else if($errCart){
            $output .= '<span class="icon-fm-err"></span>';
        }
        else{
            $output .= '<span class="icon-step1"></span>';
        }
        //if valid add link
        $validCart ? $output .= '<a href="/toont-uw-winkelwagen/winkelwagen/">'.$siteLabels['mycart'].'</a>' : $output .= $siteLabels['mycart'];
		$output .= '</li>';
		
		////////// gegevens \\\\\\\\\\
		$_SESSION['cart']['validate']['userdata'] == 'ok' ? $validUserdata = true : $validUserdata = false;
		$_SESSION['cart']['validate']['userdata'] == 'err' ? $errUserdata = true : $errUserdata = false;
		//check arrow and color
		$subpage == 'uw-gegevens' ? $output .= '<li class="active-cart">' : $output .= '<li>';
        //check icon
        if($validUserdata){
            $output .= '<span class="icon-fm-ok"></span>';
        }
        else if($errUserdata){
            $output .= '<span class="icon-fm-err"></span>';
        }
        else{
            $output .= '<span class="icon-step2"></span>';
        }
        //if valid add link
        $validUserdata ? $output .= '<a href="/toont-uw-winkelwagen/uw-gegevens/">'.$siteLabels['mydata'].'</a>' : $output .= $siteLabels['mydata'];
		$output .= '</li>';
		
		////////// betaling \\\\\\\\\\
		$_SESSION['cart']['validate']['payment'] == 'ok' ? $validPayment = true : $validPayment = false;
		$_SESSION['cart']['validate']['payment'] == 'err' ? $errPayment = true : $errPayment = false;
		//check arrow and color
		$subpage == 'betaling' ? $output .= '<li class="active-cart">' : $output .= '<li>';

        //check icon
        if($validPayment){
            $output .= '<span class="icon-fm-ok"></span>';
        }
        else if($errPayment){
            $output .= '<span class="icon-fm-err"></span>';
        }
        else if($_SESSION['cart']['validate']['payment'] == 'wait'){
            $output .= '<span class="icon-fm-wait"></span>';
        }
        else{
            $output .= '<span class="icon-step3"></span>';
        }
        //if valid add link
        $validPayment ? $output .= '<a href="/">' . $siteLabels['payment'] . '</a>' : $output .= $siteLabels['payment'];
		$output .= '</li>';
		
		////////// bevestiging \\\\\\\\\\
		$_SESSION['cart']['validate']['confirm'] == 'ok' ? $validConfirm = true : $validConfirm = false;
		$_SESSION['cart']['validate']['confirm'] == 'err' ? $errConfirm = true : $errConfirm = false;
		//check arrow and color
		$subpage == 'bevestiging' ? $output .= '<li class="active-cart">' : $output .= '<li>';
        //check icon
        if($validConfirm){
            $output .= '<span class="icon-fm-ok"></span>';
        }
        else if($errConfirm){
            $output .= '<span class="icon-fm-err"></span>';
        }
        else{
            $output .= '<span class="icon-step4"></span>';
        }
        //if valid add link
        $validConfirm ? $output .= '<a href="/toont-uw-winkelwagen/bevestiging/">'.$siteLabels['confirm'].'</a>' : $output .= $siteLabels['confirm'];
		$output .= '</li>';
	$output .= '</ul>';
	return $output;
}

function showCart(){
	global $mysqli;
	
	include './lang/'.$_SESSION['lang'].'.php';

    include_once $_SERVER['DOCUMENT_ROOT'] . '/php/discount/DiscountService.php';
    $discountService = new DiscountService();
	
	//reset cart session
	$_SESSION['cart']['validate']['cart'] = false;
	
	//reset discount sessionm
	$_SESSION['cart']['discountcode']['code'] = '';	
	$_SESSION['cart']['discountcode']['amount'] = 0;
	$_SESSION['cart']['discountcode']['message'] = '';
	$_SESSION['cart']['discountcode']['freeshipnl'] = false;
	
	$output = '';
	if(!empty($_SESSION['cart']['products'])){
		
		$i = 0;
		
		$subtotalprice = 0;
		$totalDiscount = 0;
		if(!isset($discount)) $discount = 0;
		
		foreach($_SESSION['cart']['products'] as $productId => $numProducts){

            // Check if productId has a Size
            $key = $productId;

            if(isset($size)) unset($size);
            $temp = explode('_',$productId);
            if(count($temp) > 1)
            {
                $productId = $temp[0];
                $size = $temp[1];
            }

            $productQuery =  'SELECT p.titleNL, p.titleEN, p.price, p.discountPrice, p.published, p.color, m.filename_medium
                              FROM product_products as p, media_files AS m
                              WHERE p.id = "'.$productId.'"
                              AND m.shortcode = p.thumb
                              LIMIT 1';
            if($productResult = $mysqli->query($productQuery)){
                $productRecord = $productResult->fetch_assoc();

                if($productRecord['published'] == 1){

                    $title = $productRecord['titleNL'];
                    $titleLink = linkname($title);

                    $color = $productRecord['color'];
                    $thumb = '/images/uploads/'.$productRecord['filename_medium'];

                    // price \\
                    $price = ($productRecord['price'] * $numProducts);

                    //DISCOUNT
                    $discountPrice = $productRecord['discountPrice'];

                    $oldPrice = 0;
                    if( ! empty($discountPrice)){
                        // het product is hard afgeprijsd
                        $oldPrice = $price;
                        $price = ($discountPrice * $numProducts);
                    }
                    else{
                        $discountFactor = $discountService->getDiscountFactorOnProduct($productId);
                        if($discountFactor > 0 && $discountFactor < 1)
                        {
                            $oldPrice = $price;
                            $price = round( ($price * 100) * $discountFactor) / 100;
                        }

                    }
                    $displayOldPrice = displayPrice($oldPrice);
                    $displayPrice = displayPrice($price);

                    // add tot subtotal \\
                    $subtotalprice = $subtotalprice + $price;

                    if(isset($size))
                    {
                        $available = 1; // Je kunt alleen maar kleding bestellen die op voorraad is.
                    }
                    else
                    {
                        $available = checkAvailableInStock($productId);
                    }
                    if($available > 0) $instock = '';
                    if($available <= 0) $instock = '<span class="cart-instance-notinstock">'.$siteLabels['notInStock'].'</span>';

                    $output .= '<div class="cart-instance-row';
                    if($i == 0) $output .= ' cart-instance-firstrow';
                    $output .= '">';

                        $output .= '<div class="cart-instance-image">
                                        <a href="/toont-het-artikel/'.$titleLink.'-'.$productId.'/">
                                            <img src="'.$thumb.'" alt="Deense Kroon | ' . $title . '" width="72"/>
                                        </a>
                                    </div>';

                        $output .= '<div class="cart-instance-title">';

                            // Product title
                            $output .= $title;
                            if(isset($size))
                            {
                                // Omdat de slash alles kapot maakte
                                $size = str_replace('*','/',$size);
                                $output .= ' (' . strtoupper($size) . ')';
                            }

                            // Quantity
                            $output .= '<div class="cart-instance-numproducts">';
                            $output .= '</div>';

                            // Buttons for changing quantity
                            $output .= '<div class="cart-quantity">';
                            $output .= '<span class="quantity">' . $numProducts.'x</span>';
                            $output .= '<a href="/toont-uw-winkelwagen/winkelwagen/add/'.$key.'/"><div class="icon-addone-btn"></div></a>';
                            $output .= '<a href="/toont-uw-winkelwagen/winkelwagen/remove/'.$key.'/"><div class="icon-removeone-btn"></div></a>';
                            $output .= '<a href="/toont-uw-winkelwagen/winkelwagen/removeall/'.$key.'/"><div class="icon-removeall-btn"></div></a>';
                            $output .= '</div>';

                        $output .= '</div>';

                        // Price
                        $output .= '<div class="cart-instance-price">';
                            if ( ! empty($oldPrice)) $output .= '<span class=" product-price-line-through">'.$displayOldPrice . '</span><br />';
                            $output .= $displayPrice;
                        $output .= '</div>';

                        $output .= '<div class="clear"></div>';
                    $output .= '</div>';
                }
                else{
                    $output .= '<div class="cart-instance-row';
                    if($i == 0) $output .= ' cart-instance-firstrow';
                    $output .= '">';
                        $output .= '<div class="cart-instance-title">';
                            $output .= '[this product is no longer available]';
                        $output .= '</div>';
                        $output .= '<div class="addremove-btns">';
                            $output .= '<a href="/toont-uw-winkelwagen/winkelwagen/removeall/'.$key.'/"><div class="icon-removeall-btn"></div></a>';
                        $output .= '</div>';
                    $output .= '</div>';
                }

                $i++;
            }

		}//end for each

		// Subtotal
		$totalprice = $subtotalprice;

		$_SESSION['cart']['totalprice'] = $totalprice;
			
		// Display prices
		$displayTotalPrice = displayPrice($totalprice);

        // Summary
		$output .= '<form action="" class="cart-summary" name="discount-form" method="post">';
			$output .= '<div class="cart-summary-holder">';

				$output .= '<div class="cart-summary-price-label-holder">';
					//$output .= 'subtotaal<br />';
					//$output .= 'korting<br />';
					//$output .= '<br />';
					$output .= $siteLabels['total'];
				$output .= '</div>';
				$output .= '<div class="cart-summary-price-holder">';
					//$output .= $displaySubtotalPrice.'<br />';
					//$output .= $displayDiscountPrice.'<br />';
					//$output .= '<br />';
					$output .= $displayTotalPrice;
				$output .= '</div>';
                $output .= '<div class="clear"></div>';

                // Vat info
                $output .= '<div class="cart-summary-vat-holder">';
                    $priceInfo = $siteLabels['priceIncBtw'];
                    if($vatresult = $mysqli->query('SELECT VAT FROM countries WHERE isocode = "nl" LIMIT 1')){
                        $vatrecord = $vatresult->fetch_assoc();
                        $vat = $vatrecord['VAT'];
                    }
                    $priceInfo = str_replace('[per]',$vat,$priceInfo);
                    $output .= $priceInfo;
                $output .= '</div>';

			$output .= '</div>';
            $output .= '<div class="clear"></div>';

			$output .= '<div class="cart-summary-buttons-holder">';
				$output .= '<a href="/"><span class="button-arrow-152 backwards">'.$siteLabels['continueShopping'].'</span></a>';
            /*
            $output .= '<div id="cart_order_btn" class="button-add-to-cart btnhover-extra">';
                $output .= '<input type="submit" name="validate-cart" value="" />';
                $output .= '<span class="text">'.$siteLabels['order'].'</span><span class="icon-arrow"></span>';
                $output .= '<div class="over"></div>';
                $output .= '<div class="off"></div>';
            $output .= '</div>';
            */
            $output .= '<div class="opc_button" id="cart_order_btn">
                            <input type="submit" name="validate-cart" value="'.$siteLabels['order'].'" />
                            <span class="arrow"></span>
                        </div>';
            $output .= '<div class="clear"></div>';

        $output .= '</div>';
		$output .= '</form>';	
		$output .= '<div class="bottom-space"></div>';
	}
	else{
		$_SESSION['cart']['totalprice'] = 0;
		$output .= '<div class="cart-instance-row cart-instance-firstrow">Er bevinden zich geen artikelen in uw winkelwagen.</div>';
	}

	return $output;
}

/* valideer-winkelwagen */
function validateCart(){
	global $mysqli;
	//check some things ?
	//check if products stil exists
	foreach($_SESSION['cart']['products'] as $productId => $numProducts){

        // Check for size
        if(isset($size)) unset($size);
        $temp = explode('_',$productId);
        if(count($temp) > 1)
        {
            $productId = $temp[0];
            $size = $temp[1];
        }

		//get information for each product in the session
        $productQuery =  'SELECT published FROM product_products WHERE id = "'.$productId.'" LIMIT 1';
        if($productResult = $mysqli->query($productQuery)){
            if($productResult->num_rows > 0){
                $productRecord = $productResult->fetch_assoc();
                if($productRecord['published'] == 1){
                    //avaible -> do nothing .
                }
                else{
                    //unavailable
                    unset($_SESSION['cart']['products'][$productId]);
                }

            }
            else{
                //unavailable
                unset($_SESSION['cart']['products'][$productId]);
            }
        }
	}

	//errors[] = 'error';
	if(!isset($errors)){
		$_SESSION['cart']['validate']['cart'] = 'ok';
		header('location: /toont-uw-winkelwagen/uw-gegevens/');
	}	
}

function editCart($action){
	if(defined('URL_SUB3')){
		$productId = URL_SUB3;
		if(!empty($productId)){
			switch($action){
				case 'removeall':
					$_SESSION['cart']['products'][$productId] = 0;
					unset($_SESSION['cart']['products'][$productId]);
					
				break;
				case 'remove' :
					if($_SESSION['cart']['products'][$productId] == 1){
						$_SESSION['cart']['products'][$productId] = 0;
						unset($_SESSION['cart']['products'][$productId]);
					}
					else{
						$_SESSION['cart']['products'][$productId]--;
					}
				break;
				case 'add':
					$_SESSION['cart']['products'][$productId]++;
				break;		
			}	
		}
	}

    // Reset giftcards and discount
    $_SESSION['checkout_amount']['discount'] = 0; // total discount (codes + discount)
    $_SESSION['checkout_amount']['discount_code'] = ''; // discount code
    $_SESSION['checkout_amount']['discount_code_amount'] = ''; // amount discount code
    $_SESSION['cart']['discountcode']['amount'] = 0;
    $_SESSION['cart']['discountcode']['code'] = '';
    $_SESSION['checkout_amount']['gift_card'] = array(); // gift cards

	header('location: /toont-uw-winkelwagen/winkelwagen/');
}

function removeDiscountcode(){
	unset($_SESSION['cart']['discountcode']);
	header('location: ../');
}
function removeGiftcard(){
	if(defined('URL_SUB3') && is_numeric(URL_SUB3))
	{
		$key = URL_SUB3;
		unset($_SESSION['cart']['giftcard'][$key]);
	}
	header('location: ../');
}

function checkOrderDiscount(){
	/* CHECKS IF ORDER DESERVES DISCOUNT WITHOUT A CODE */
	global $mysqli;
	//reset session
	//$_SESSION['cart']['discountcode']['message'] = '';
	//$_SESSION['cart']['discountcode']['amount'] = 0;
	//$_SESSION['cart']['discountcode']['code'] = '';	
	//$_SESSION['cart']['discountcode']['freeshipnl'] = false;
	
	//is there a valid action which uses no code
	$query = 'SELECT DISTINCT da.id AS actionId, da.type, dd.catId, dd.searchtag, dd.percentage, dd.minamount
				FROM discount_action AS da, discount_details AS dd, content_status AS cs
				WHERE (da.active = 1
				OR (da.starts < '.time().' AND da.expires > '.time().'))
				AND da.discountCode = ""
				AND cs.active = 1
				AND cs.linkname = "discount_action"
				AND cs.itemId = da.id
				AND da.id = dd.actionId';
							
	if($result = $mysqli->query($query)){
        if($result->num_rows > 0){
            $totalDiscount = 0;

			while($record = $result->fetch_assoc())
            {
                $type = $record['type'];
                $actionId = $record['actionId'];
                $temp = explode('-',$type);

                switch($temp[0]){
                    case 'allorders':
                        //deze actie geld voor alle orders
                        if($temp[1] == 'per' &&  $record['percentage'] != 0 && !empty($record['percentage'])){
                        // als we hier ook willen dat wanneer er al een product is afgeprijs de actie niet geldm moeten we dit controleren.
                        //	if(!checkProductDiscount($productId)){
                            $tempAmount = ($_SESSION['cart']['totalprice'] * $record['percentage']) / 100;
                            if($checkAmount = checkPrice($tempAmount)){
                                $discount = $checkAmount;
                                $totalDiscount = $totalDiscount + $discount;
                            }
                            $_SESSION['cart']['discountcode']['message'] = '';

                            ///// return discount
                        }
                        else if($temp[1] == 'freeshipnl'){
                            //set to true and reset
                            $_SESSION['cart']['discountcode']['freeshipnl'] = true;
                        }
                    break;
                    case 'minamount':
                    //deze actie geldt voor alle orders vanaf een bepaald bedrag
                        $minamount = $record['minamount'];
                        //als de order hoger is dan dit bedrag
                        if($_SESSION['cart']['totalprice'] > $minamount){
                            //deze actie geld voor alle orders groter dan min bedrag
                            if($temp[1] == 'per' &&  $record['percentage'] != 0 && !empty($record['percentage'])){
                            // als we hier ook willen dat wanneer er al een product is afgeprijs de actie niet geldm moeten we dit controleren.
                            //	if(!checkProductDiscount($productId)){
                                $tempAmount = ($_SESSION['cart']['totalprice'] * $record['percentage']) / 100;
                                if($checkAmount = checkPrice($tempAmount)){
                                    $discount = $checkAmount;
                                    $totalDiscount = $totalDiscount + $discount;
                                }
                                $_SESSION['cart']['discountcode']['message'] = '';

                                ///// return discount
                            }
                            else if($temp[1] == 'freeshipnl'){
                                //set to true and reset
                                $_SESSION['cart']['discountcode']['freeshipnl'] = true;
                            }
                        }
                    break;
                }
            }
            return $totalDiscount;
        }
	}
    return false;
}


/**
	
	After order, this function stores a used discountcode to the database

*/
function storeDiscountCode()
{
	global $mysqli;
	if(isset($_SESSION['cart']['discountcode']))
	{
		if( ! empty($_SESSION['cart']['discountcode']['code']))
		{
			$customerId = $_SESSION['cart']['customerid'];
			$discountCode = $_SESSION['cart']['discountcode']['code'];
			$orderCode = $_SESSION['cart']['orderCode'];
			
			$mysqli->query('INSERT INTO discount_codes(actionId, discountCode, customerId, orderId, timest)
							VALUES( '.$_SESSION['cart']['discountcode']['actionId'].',"'.$discountCode.'",'.$customerId.','.$orderCode.','.time().')');
		}
	}	
}