Page 1 of 1

code not PHP5 compatable fix required please!!!!

Posted: Thu Mar 13, 2008 11:32 am
by malcolmboston

Code: Select all

 
<?php
 
error_reporting ('E_ALL');
 
class cart
{
    function cart () {
        @session_start (); // aut session start with error supressions
        $this->load (); // look for an existing cart to load up
        $this->save (); // save the cart back to the database (this extra call is to update the last active
        if (isset($_POST['cartAction'])) {
            $this->doAction (); // do the requested method as requested via $_POST
            $this->save (); // save the cart back to the database
            #$this->debug (); // show debug settings
            header('Location: ../showBasket.php');
        }
    }
    
    function doAction () {
        if ($_POST['cartAction'] == 'add') {
            $this->add ($_POST['itemStockCode'], $_POST['itemSize'], 1);
        } elseif ($_POST['cartAction'] == 'del') {
            $this->remove ();
        } else {
            //
        }
    }
    
    function buildPayPalCart ($cart, $deliveryPrice) {
        // static function only
        require_once ('cms/classes/coupon/coupon.class.php');
        $content = '';
        for ($i = 0; $i < count ($cart->cartContents); $i++) {
            if (!isset($displayI)) {
                $displayI = 1;
            }
            $content .= '<input type="hidden" name="item_name_'.$displayI.'" value="'.$cart->cartContents[$i]['productStockCode'].' - '.$cart->cartContents[$i]['productBrandName'].' '.product::buildProductDisplayName ($cart->cartContents[$i]['productName'], $cart->cartContents[$i]['productCategoryType']).' - '.$cart->cartContents[$i]['productSize'].'">';
            if (isset($cart->discount)) {
                $productPrice = coupon::getNewPrice ($cart->discount['type'], $cart->discount['amount'], $cart->cartContents[$i]['productOnlinePrice']);
            } else {
                $productPrice = $cart->cartContents[$i]['productOnlinePrice'];
            }
            $content .= '<input type="hidden" name="amount_'.$displayI.'" value="'.$productPrice.'">';          
            $displayI++;
        }
        // also add a delivery charge
        $content .= '<input type="hidden" name="handling_cart" value="'.$deliveryPrice.'">';
        return $content;        
    }
    
    function load () {
        // load existing cart
        $query = 'SELECT *
                    FROM `cart`
                   WHERE `sessionID` = "'.session_id().'"
                   LIMIT 1';
        $result = mysql_query ($query) or die (mysql_error());
        if (mysql_num_rows ($result) == 1) {
            // theres an existing cart
            while ($a = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $cart = stripslashes ($a['cartContents']);
                $cart = unserialize ($cart);
                #$cart = stripslashes(unserialize($a['cartContents']));
            }
            $this = $cart;
        }
        // becase of an unforeseen problem with the way the cart can sometimes be empty but delivery details be present
        // if theres no cart items, auto remove the cart
        $this->initialiseReferrer (); // grab referrer and first time visit info
        if (empty ($cart->cartContents)) {
            // the cart must be empty if [1] is not set
            $this->delete ();
        }
    }
    
    function initialiseReferrer () {
        /*if (!isset($this->$referrer)) {
            $this->$referrer = $_SERVER['HTTP_REFERER'];
            $this->$visitTime = time ();
        }*/
    }
    
    function findMissingCart ($ip) {
        $query = "SELECT *
                    FROM `cart`";
        $result = mysql_query ($query) or die (mysql_error());
        while ($a = mysql_fetch_array($result, MYSQL_ASSOC)) {
            if (!isset($i)) {
                $i = 1;
            }
            $cart[$i]['cartID'] = $a['sessionID'];
            $cart[$i]['cartContents'] = (array) unserialize($a['cartContents']);
            $i++;
        }
        for ($i = 1; $i <= count ($cart); $i++) {
            if ($cart[$i]['cartContents']['ip'] == $_SERVER['REMOTE_ADDR']) {
                $cart = cart::loadCartByID ($cart[$i]['cartID']);
                return $cart;
            }
        }
    }
    
    function add ($stockCode, $itemSize, $itemQuantity) {
        $this->cartContents[] = product::getProductSimple ($stockCode, FALSE);
    }
    
    function remove () {
        for ($i = 0; $i<=count ($this->cartContents); $i++) {
            if ($i == $_GET['item']) {
                unset($this->cartContents[$i]);
            }
        }
        $this->cartContents = array_values($this->cartContents);
    }
    
    function update () {
        //
    }
    
    function fixIncompleteTransaction ($cart, $paymentMethod) {
        // this is a bug some people seem to be able to reproduct, however as of yet i dont know how its done
        // therefore for thwe time being i willdo a workaround where you can menually
        // the only step we will not do is actually send the emails as we may be doing this hours after the order actually took lpace
        $cart = transaction::prepareCart ($cart);
        transaction::storeTransaction ($cart, $paymentMethod); // this seems to never be done
        #transaction::writeStockFile ($cart); // this seems to never be done
        #transaction::amendStockLevels ($cart); // same
    }
    
    function save () {
        $this->ip = $_SERVER['REMOTE_ADDR'];
        $savedCart = serialize($this);
        $sessionID = session_id ();
        $currentTime = time ();
        $savedCart = str_replace(',', ' ', $savedCart);
        $savedCart = addslashes($savedCart);
        //check to see if there is a basket
        if (isset($this->cartContents)) {
            // check to see if this sessionID is already in the database
            $query = 'SELECT *
                        FROM `cart`
                       WHERE `sessionID` = "'.session_id().'"
                       LIMIT 1';
 
            $result = mysql_query ($query) or die (mysql_error());
            if (mysql_num_rows($result) == 1) {
                // its already there, an update is required
                $query = 'UPDATE `cart`
                             SET `cartContents` = "'.mysql_real_escape_string($savedCart).'",
                                 `lastActive` = "'.time().'"
                           WHERE `sessionID` = "'.session_id().'"
                           LIMIT 1';
            } else {
                // insert
                $query = 'INSERT INTO `cart`
                      (`sessionID` , `cartContents`, `lastActive`)
                      VALUES
                      ("'.session_id().'", "'.mysql_real_escape_string($savedCart).'", '.time().')';
            }
            $result = mysql_query ($query) or die (mysql_error());
        }
    }
    
    function loadCartByID ($cartID) {
        // load existing cart
        $query = 'SELECT *
                    FROM `cart`
                   WHERE `sessionID` = "'.$cartID.'"
                   LIMIT 1';
        $result = mysql_query ($query) or die (mysql_error());
        if (mysql_num_rows ($result) == 1) {
            // theres an existing cart
            while ($a = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $b = stripslashes ($a['cartContents']);
                $cart = unserialize ($b);
            }
            return $cart;
        }       
    }
    
    function delete () {
        $query = 'DELETE FROM `cart`
                        WHERE `sessionID` = "'.session_id().'"';
        $result = mysql_query ($query) or die (mysql_error());
    }
    
    function debug () {
        print "<strong>POST</strong>";
        print "<pre>";
        print_r ($_POST);
        print "</pre>";
        print "<strong>CLASS</strong>";
        print "<pre>";
        print_r ($this);
        print "</pre>";
    }
    
    function displayCartOverview ($cartArray) {
        $total = 0.00;
        $count = count ($cartArray);
        for ($i = 0; $i < $count; $i++) {
            $total = ($total + ($cartArray[$i]['productOnlinePrice'] * $cartArray[$i]['productQuantity']));
        }
        echo '<div style="display: inline;">'.$count.' Item(s) / &pound;'.number_format($total, 2).' / </div>';
        echo '<a href="showBasket.php" style="text-decoration: none; color: #000;" title="View Basket">';
        echo '<img src="images/view_cart.jpg" title="View Cart" alt="eg" align="absmiddle" />';
        echo '</a>';
    }
    
    function getCartOverview ($cartArray) {
    
        $total = 0.00;
        if ($cartArray == FALSE) {
            $count = 0;
        } else {
            $count = count ($cartArray);
        }   
        for ($i = 0; $i < $count; $i++) {
            $total = ($total + ($cartArray[$i]['productOnlinePrice'] * $cartArray[$i]['productQuantity']));
        }
        $cartInfo['itemCount'] = $count;
        $cartInfo['itemTotal'] = $total;
        
        return $cartInfo;
    }
    
    function calculateTotal () {
        $countItems = count ($this->cartContents);
        $total = 0.00;
        for ($i = 0; $i < $countItems; $i++) {
            $total = ($total + ($this->cartContents[$i]['productOnlinePrice'] * $this->cartContents[$i]['productQuantity']));
        }
        return number_format($total, 2);
    }
    
    function getPriceMinusVAT ($productPrice) {
        $vat = ($productPrice / 100);
        $vat = ($vat * 17.5);
        $productPrice = ($productPrice - $vat);
        return $productPrice;
    }
}
?>
 
Fatal error: Cannot re-assign $this in /classes/cart/cart.class.php on line 65
now this code has always worked in PHP 4.4.x on *nix and windows, ive jumped onto a dedicated PHP5 server and now it hates my script, ive been giving myself (and mark beech!!) a major migraine for the last 6 hours trying to get this working so im coming on here in the hope that one of you PHP gods can fix this, i dont really want to have to rebuild the script as this is part of a live eCommerce site and seeing as its the cart script, its a pretty important part!!!

anyone??

Re: code not PHP5 compatable fix required please!!!!

Posted: Thu Mar 13, 2008 3:10 pm
by Christopher
Well you can't do this anymore:

Code: Select all

            $this = $cart;
And you probably never should have.

I would recommend creating a getInstance() method that returns $this->_cart; and reworking the code a little to use that to get the instance.

Re: code not PHP5 compatable fix required please!!!!

Posted: Fri Mar 14, 2008 4:45 am
by malcolmboston
Update:

Found this article here: http://bugs.php.net/bug.php?id=34358

Although that workaround no longer works on my box, seems like a class rewrite is in order.......

Cheers anyway, Mal