Let the platform do the work

Module Loader Restriction Alternatives

Overview

This article provides workarounds for commonly used functions that are denylisted by Sugar for Sugar's cloud environment.

Denylisted Functions

$variable()

Variable functions are sometimes used when trying to dynamically call a function. This is commonly used to retrieve a new bean object.

Restricted use:

    $module = "Account";
$id = "6468238c-da75-fd9a-406b-50199fe6b5f8";

//creating a new bean
$focus = new $module()

//retrieving a specific record
$focus->retrieve($id);

As of 6.3.0, we have implemented newBean and getBean which can be found in the BeanFactory. Below is the recommended approach to create or fetch a bean:

    $module = "Accounts";
$id = "6468238c-da75-fd9a-406b-50199fe6b5f8";

//creating a new bean
$focus = BeanFactory::newBean($module);

//or creating a new bean and retrieving a specific record
$focus = BeanFactory::getBean($module, $id);

array_filter()

The array_filter filters elements of an array using a callback function. It is restricted from use on Sugar's cloud service due to its ability to call other restricted functions.

Restricted use:

    /**
 * Returns whether the input integer is odd
 * @param $var
 * @return int
 */
function odd($var) {
    return($var & 1);
}

$myArray = array(
    "a"=>1,
    "b"=>2,
    "c"=>3,
    "d"=>4,
    "e"=>5
);

$filteredArray = array_filter($myArray, "odd");

An alternative to using array_filter is to use a foreach loop.

    $filteredArray = array();

$myArray = array(
    "a"=>1,
    "b"=>2,
    "c"=>3,
    "d"=>4,
    "e"=>5
);

foreach ($myArray as $key => $value) {
    // check whether the input integer is odd
    if($value & 1) {
        $filteredArray[$key] = $value; 
    }
}

copy()

The copy method is sometimes used by developers when duplicating files in the uploads directory.

Restricted use:

    $result = copy($oldFile, $newFile);

 An alternative to using copy is the duplicate_file method found in the UploadFile class.

    require_once 'include/upload_file.php';

$uploadFile = new UploadFile();
$result = $uploadFile->duplicate_file($oldFileId, $newFileId);

file_exists()

The file_exists method is used by developers to determine if a file exists.

Restricted use:

    if(file_exists($file_path)) {
    require_once($file);
}

An alternative to using file_exists is the fileExists method found in the SugarAutoLoader class.

    $file = 'include/utils.php';
if (SugarAutoloader::fileExists($file)) {
    require_once($file);
}

file_get_contents()

The file_get_contents method is used to retrieve the contents of a file.

Restricted use:

    $file_contents = file_get_contents('file.txt');

An alternative to using file_get_contents and sugar_file_get_contents is the get_file_contents method found in the UploadFile class.

    require_once('include/upload_file.php');

$uploadFile = new UploadFile();

//get the file location
$uploadFile->temp_file_location = UploadFile::get_upload_path($file_id);
$file_contents = $uploadFile->get_file_contents();

fwrite()

The fwrite method is a function used to write content to a file. As there isn't currently a direct alternative for this function, you may find one of the following a good solution to what you are trying to achieve.

Adding/Removing Logic Hooks

When working with logic hooks, it is very common for a developer to need to modify ./custom/modules/<module>/logic_hooks.php. When creating module loadable packages, developers will sometimes use fwrite to modify this file upon installation to include their additional hooks. As of Sugar 6.3, Logic Hook Extensions were implemented to allow a developer to append custom hooks. If you would prefer to edit the logic_hooks.php file, you will need to use the check_logic_hook_file method as described below:

    //Adding a logic hook
require_once("include/utils.php");

$my_hook = Array(
    999,
    'Example Logic Hook',
    'custom/modules/<module>/my_hook.php',
    'my_hook_class',
    'my_hook_function'
);

check_logic_hook_file("Accounts", "before_save", $my_hook);

Removing a logic hook can be done by using remove_logic_hook:

    //Removing a logic hook
require_once("include/utils.php");

$my_hook = Array(
    999,
    'Example Logic Hook',
    'custom/modules/<module>/my_hook.php',
    'my_hook_class',
    'my_hook_function'
);

remove_logic_hook("Accounts", "before_save", $my_hook);

getimagesize()

The getimagesize method is used to retrieve information about an image file.

Restricted use:

    $img_size = getimagesize($path);

If you are looking to verify an image is .png or .jpeg, you can use the verify_uploaded_image method:

    require_once('include/utils.php');

if (verify_uploaded_image($path)) {
    //logic
}

If you are looking to get the mime type of an image, you can use the get_file_mime_type method:

    $mime_type = get_file_mime_type($path);