SuiteCRM-Core icon indicating copy to clipboard operation
SuiteCRM-Core copied to clipboard

Cannot Add images to products or change the site logo - Can add images to Users! Possible Fix!

Open antonical opened this issue 1 year ago • 1 comments

Your Environment Ubuntu 22.04 Virtualmin Server 12 core Intel Silver 48Gb Ram SuiteCRM Version used: 8.5.1 Browser name and version tested various browsers this is a backend issue as we can see in the logs above Environment name and version (e.g. MySQL, PHP 7): Apache 2.4.52php 8.1.27 MariaDB 10.6.16 Operating System and version (e.g Ubuntu 16.04): Ubuntu 22.04 See all software versions: Apache version 2.4.52 PHP versions 7.4.33, 8.1.27, 8.3.2 MariaDB version 10.6.16

Context We cannot do basic things like add an image to a product or change the site logo. We are still testing but I suspect this bug(s) are impacting the use of the platform in unexpected ways. We would consider this high priority as the product does not function on php 8.1 or above.

maybe there is a glimmer of hope, but I have no idea how to fix it. See original post https://community.suitecrm.com/t/suite-crm-8-5-1-cannot-upload-pictures-or-see-them-once-uploaded/91877/13

In the studio if I examine the data types associated with the product image field it is defined as a text field.

In users where I can upload a photo of a user the data type is defined as image.

So maybe it is a datatype field definition bug. Changing the vardef for the LBL_PRODUCT_IMAGE to image and adding the same properties as the user image field made no difference though.

Damn, I really thought I was onto something there for a minute! Screenshot from 2024-02-18 11-42-54 Screenshot from 2024-02-18 11-42-54 368×661 23 KB When searching the entire installation for occurences of both LBL_PRODUCT_IMAGE and product_image this is what I find.


# find . -type f -exec grep -l "LBL_PRODUCT_IMAGE" {} \;
./cache/prod/pools/OmsCTTxpXV/9/S/mU5fLf8dl6J5gAPo5ynA
./cache/prod/pools/OmsCTTxpXV/S/J/cxjyLThttm0qH2gD6nBA
./public/legacy/cache/themes/suite8/modules/AOS_Products/EditView.tpl
./public/legacy/cache/themes/suite8/modules/AOS_Products/DetailView.tpl
./public/legacy/cache/jsLanguage/AOS_Products/en_us.js
./public/legacy/cache/modules/AOS_Products/language/en_us.lang.php
./public/legacy/cache/modules/AOS_Products/AOS_Productsvardefs.php
./public/legacy/cache/smarty/templates_c/a141623ca6b6ce702279074f1b607e7a6c681cd4_0.file.DetailView.tpl.php
./public/legacy/cache/smarty/templates_c/281e0649af9e4d432e317a4af6be8054b77312ab_0.file.EditView.tpl.php
./public/legacy/modules/AOS_Products/language/en_us.lang.php
./public/legacy/modules/AOS_Products/vardefs.php
./public/legacy/modules/AOS_Products/metadata/detailviewdefs.php
./public/legacy/custom/modules/AOS_Products/Ext/Language/en_us.lang.ext.php
./public/legacy/custom/Extension/modules/AOS_Products/Ext/Language/_override_en_us.lang.php

# find . -type f -exec grep -l "product_image" {} \;
./cache/prod/pools/OmsCTTxpXV/9/S/mU5fLf8dl6J5gAPo5ynA
./public/legacy/cache/themes/suite8/modules/AOS_Products/EditView.tpl
./public/legacy/cache/themes/suite8/modules/AOS_Products/DetailView.tpl
./public/legacy/cache/modules/AOS_Products/AOS_Productsvardefs.php
./public/legacy/cache/smarty/templates_c/a141623ca6b6ce702279074f1b607e7a6c681cd4_0.file.DetailView.tpl.php
./public/legacy/cache/smarty/templates_c/281e0649af9e4d432e317a4af6be8054b77312ab_0.file.EditView.tpl.php
./public/legacy/modules/AOS_Products/vardefs.php
./public/legacy/modules/AOS_Products/metadata/editviewdefs.php
./public/legacy/modules/AOS_Products/metadata/quickcreatedefs.php
./public/legacy/modules/AOS_Products/metadata/detailviewdefs.php
./public/legacy/modules/AOS_Products/AOS_Products.php
./public/legacy/modules/AOS_Products/views/view.edit.php
./public/legacy/modules/AOS_PDF_Templates/templateParser.php
./public/legacy/custom/modules/AOS_Products/Ext/Vardefs/vardefs.ext.php
./public/legacy/custom/Extension/modules/AOS_Products/Ext/Vardefs/_override_sugarfield_product_image.php

Multiple vardef files does not fill me with confidence!

My instincts tell me the issue is in here somewhere. This seems to be the code responsible for uploading the product image in public/legacy/modules/AOS_Products/AOS_Products.php.


    public function save($check_notify = false)
    {
        global $sugar_config, $mod_strings;

        if (isset($_POST['deleteAttachment']) && $_POST['deleteAttachment'] == '1') {
            $this->product_image = '';
        }

        require_once('include/upload_file.php');
        $GLOBALS['log']->debug('UPLOADING PRODUCT IMAGE');

        if(!empty($_FILES['uploadimage']['name'])){
            $imageFileName = $_FILES['uploadimage']['name'] ?? '';
            if (!has_valid_image_extension('AOS_Products Uploaded image file: ' . $imageFileName , $imageFileName)) {
                LoggerManager::getLogger()->fatal("AOS_Products save - Invalid image file ext : '$imageFileName'.");
                throw new RuntimeException('Invalid request');
            }
        }

        if (!empty($_FILES['uploadimage']['tmp_name']) && verify_uploaded_image($_FILES['uploadimage']['tmp_name'])) {
            if ($_FILES['uploadimage']['size'] > $sugar_config['upload_maxsize']) {
                die($mod_strings['LBL_IMAGE_UPLOAD_FAIL'] . $sugar_config['upload_maxsize']);
            }
            $prefix_image = $this->getGUID() . '_';
            $this->product_image = $sugar_config['site_url'] . '/' . $sugar_config['upload_dir'] . $prefix_image . $_FILES['uploadimage']['name'];
            move_uploaded_file($_FILES['uploadimage']['tmp_name'], $sugar_config['upload_dir'] . $prefix_image . $_FILES['uploadimage']['name']);
        }

        require_once('modules/AOS_Products_Quotes/AOS_Utils.php');

        perform_aos_save($this);

        return parent::save($check_notify);
    }

Anyone?

Cheers Tony Screenshot from 2024-02-18 11-32-02 Screenshot from 2024-02-18 11-32-02 368×661 26.4 KB

Screenshot from 2024-02-18 11-31-40 Screenshot from 2024-02-18 11-31-40 342×658 19.6 KB

antonical avatar Feb 18 '24 12:02 antonical

Working through this and adding some error handling seems to have resolved the issue. With the code below in public/legacy/modules/AOS_Products/AOS_Products.php and the vardef changed to image with the same properties as the user image. width, height etc. Ran a repair and rebuild from the admin section.

I can now add images to products! Eureka. Obviously this is a cludge and maybe I have got lucky. I cannot replace the site image though so maybe more of the same in that code. One step at a time.

No additional php errors show up in my Apache or php logs.

Here is my replacement code. I provide no guarantees or warranties and these changes will no doubt be overwritten in any upgrade!

public function save($check_notify = false)
{
    global $sugar_config, $mod_strings;

    if (isset($_POST['deleteAttachment']) && $_POST['deleteAttachment'] == '1') {
        $this->product_image = '';
    }

    require_once('include/upload_file.php');
    $GLOBALS['log']->debug('UPLOADING PRODUCT IMAGE');

    if (!empty($_FILES['uploadimage']['name'])) {
        $imageFileName = $_FILES['uploadimage']['name'] ?? '';
        if (!has_valid_image_extension('AOS_Products Uploaded image file: ' . $imageFileName, $imageFileName)) {
            LoggerManager::getLogger()->fatal("AOS_Products save - Invalid image file ext : '$imageFileName'.");
            throw new RuntimeException('Invalid request');
        }
    }

    if (!empty($_FILES['uploadimage']['tmp_name'])) {
        $uploadErrors = [
            UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.',
            UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
            UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
            UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
            UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
            UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder. Introduced in PHP 5.0.3.',
            UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Introduced in PHP 5.1.0.',
            UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
        ];

        $errorCode = $_FILES['uploadimage']['error'];

        if ($errorCode !== UPLOAD_ERR_OK) {
            die("Upload failed with error: $uploadErrors[$errorCode]");
        }

        if ($_FILES['uploadimage']['size'] > $sugar_config['upload_maxsize']) {
            die($mod_strings['LBL_IMAGE_UPLOAD_FAIL'] . $sugar_config['upload_maxsize']);
        }

        $prefix_image = $this->getGUID() . '_';
        $uploadDir = rtrim($sugar_config['upload_dir'], '/') . '/';
        $uploadedFilePath = $uploadDir . $prefix_image . $_FILES['uploadimage']['name'];
        
        if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $uploadedFilePath)) {
            $this->product_image = $sugar_config['site_url'] . '/' . $uploadDir . $prefix_image . $_FILES['uploadimage']['name'];
        } else {
            die("Failed to move uploaded file to destination.");
        }
    }

    require_once('modules/AOS_Products_Quotes/AOS_Utils.php');

    perform_aos_save($this);

    return parent::save($check_notify);
}

Maybe someone can take something from the above and fix the issue permanently. I have not looked at why I cannot change the site logo yet.

Tested with mulitple user accounts and works every time to both initially upload an image and also change it.

I hope this demonstrates I am willing to help in any way I can as we really want to start using SuiteCRM but cannot if basic functionality does not work!

I also still have many popups saying undefined or buttons saying undefined and have to guess the function!

Cheers Tony

antonical avatar Feb 18 '24 13:02 antonical