Jump to content

function called with false parameter


Recommended Posts

I've got a very strange behavior in Symfony / Prestashop.

This is the code:

 $row['attachments'] = [];
 if (!isset($row['cache_has_attachments']) || $row['cache_has_attachments']) {
        var_dump($row['id_product']);
        $row['attachments'] = Product::getAttachmentsStatic((int) $id_lang, (int) $row['id_product']);
        var_dump($row['id_product']);
 }

The function Product::getAttachmentsStatic($id_lang, $id_product) basically expects two ints.

However symfony throws an Exception becuase the function is called like this Product::getAttachmentsStatic(2, false). The var_dump($row['id_product']); one row before shows that $row['id_product']='20'.

How could the variable change within one row?

I am using PHP 7.4 and Prestashop 1.7.8.8 with a plugin overwriting the product class.

The data in $row are fine. I have absolutly no clue what to do.

grafik.thumb.png.1c523541bdd43db797643804b5d303ce.png

 

Also posted on stackoverflow: https://stackoverflow.com/questions/74904973/symfony-function-called-with-false-parameter

Link to comment
Share on other sites

Il y a 2 heures, CMeier Software a dit :

No, there is only the function "Product::getAttachmentsStatic($id_lang, $id_product)" is in the override.

So the concern is at this level, because the basic function is made like this:

    /**
     * @param int $id_lang Language identifier
     * @param int $id_product Product identifier
     *
     * @return array
     */
    public static function getAttachmentsStatic($id_lang, $id_product)
    {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
        SELECT *
        FROM ' . _DB_PREFIX_ . 'product_attachment pa
        LEFT JOIN ' . _DB_PREFIX_ . 'attachment a ON a.id_attachment = pa.id_attachment
        LEFT JOIN ' . _DB_PREFIX_ . 'attachment_lang al ON (a.id_attachment = al.id_attachment AND al.id_lang = ' . (int) $id_lang . ')
        WHERE pa.id_product = ' . (int) $id_product);
    }

So what does your override look like?

Link to comment
Share on other sites

public static function getAttachmentsStatic($id_lang, $id_product)
    {
        $return_attach =  Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
        SELECT *
        FROM ' . _DB_PREFIX_ . 'product_attachment pa
        LEFT JOIN ' . _DB_PREFIX_ . 'attachment a ON a.id_attachment = pa.id_attachment
        LEFT JOIN ' . _DB_PREFIX_ . 'attachment_lang al ON (a.id_attachment = al.id_attachment AND al.id_lang = ' . (int) $id_lang . ')
        WHERE pa.id_product = ' . (int) $id_product);
        if ($return_attach) {
            $attach_id_arrys = [];
            foreach ($return_attach as $pfta_value) {
                $attach_id_arrys[] = $pfta_value['id_attachment'];
            }
        }
        else{
            return $return_attach;
        }
        $group_id = Group::getCurrent()->id;
        $id_product = Tools::getValue('id_product');
        $active_rules_datas = AddifyproductattachmentproModel::getallrulesgrouped($group_id,$id_product);
        if ($active_rules_datas) {
            $final_attachment_ids = [];
            foreach ($active_rules_datas as $active_rule_data) {
                if ($active_rule_data['accessibility'] == 0) {  
                    $attach_db_id_arrys = explode(",",$active_rule_data['Attach_Files']);
                    foreach ($attach_db_id_arrys as $attach_db_id_arry) {
                        if (in_array($attach_db_id_arry, $attach_id_arrys)) {
                          $final_attachment_ids[] = $attach_db_id_arry;
                        }
                    }
                }
                else{
                    $con =  Context::getContext();
                    $id_customer = $con->customer->id;
                    $order_list = Order::getCustomerOrders($id_customer);
                    if ($order_list) {
                       foreach ($order_list as $value) {
                            $order = new Order($value['id_order']);
                            $products = $order->getProducts();
                            foreach ($products as $products_value) {
                                if ($products_value['product_id'] == $id_product) {
                                    $attach_db_id_arrys = explode(",",$active_rule_data['Attach_Files']);
                                    foreach ($attach_db_id_arrys as $attach_db_id_arry) {
                                        if (in_array($attach_db_id_arry, $attach_id_arrys)) {
                                          $final_attachment_ids[] = $attach_db_id_arry;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $final_attachments = [];
            foreach ($return_attach as $pfta_files) {
                $attach_id_arrys[] = $pfta_files['id_attachment'];
                if (in_array($pfta_files['id_attachment'], $final_attachment_ids)) {
                   $final_attachments[] = $pfta_files;
                }
            }
            return $final_attachments;
        }
        else
        {
            $arrayattach = [];
            return $arrayattach;
        }
    }

Basically the function tries to get the data from the database with the given parameter. But if that was not succesful the function tries to get that from the context. In my situation the context is not set. Here the funtion tries to access $id_customer = $con->customer->id;where $con = null. The correct behavior would be the function call with the corret parameter.

This is code from a module, so its not mine

Link to comment
Share on other sites

Hi,

when we analyze the code of this overload, we can see that if a data is missing, as there is no test on it, it will crash the code.

So either this overload is badly coded, or you find yourself in a case that should not exist.

But looking at the code, we can see that it is not really optimized and loads data repeatedly when some could be loaded only once.

So, contact the module support to either install the latest version of the module if it exists or to improve and secure the code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...