Module Loader Restriction Alternatives



Overview

This article provides workarounds for commonly used functions that are blacklisted by Sugar for the On-Demand environment.

Blacklisted Functions

$variable()

$variable() is 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, newBean and getBean have been implemented. You can find out more information about these functions in ourDeveloper Blog. Below is the recommended workaround:
$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);

file_exists()

file_exists() is commonly used by developers to determine if a file exists in the custom directory. An alternitive to using this is get_custom_file_if_exists.
Restricted use:
$check_path = "custom/modules/Accounts/logic_hooks.php";

if (file_exists($check_path))
{
    //file exists
}
Below is the recommended workaround to determine if a file exists in the custom directory:
//require utils
require_once("include/utils.php");
$check_path = "modules/Accounts/logic_hooks.php";

//if the $check_path exists in custom, $path will be returned as "custom/{$check_path}", otherwise $check_path will be returned
$path = get_custom_file_if_exists("modules/Accounts/logic_hooks.php");

if ($check_path != $path)
{
    //file exists in custom
}

file_get_contents()

file_get_contents() 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 method get_file_contents which can be found in theUploadFile class.
require_once('include/upload_file.php');

$file = new UploadFile();

//get the file location
$file->temp_file_location = "relative/path/to/file.txt";

//alternatively you can do the following if you know the upload file id
//$file->temp_file_location = UploadFile::get_upload_path($file_id);
$file_contents = $file->get_file_contents();

fwrite()

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

Adding/Removing Logic Hooks

Sometimes a developer will want to append or remove a custom logic hook to:
./custom/modules/<module>/logic_hooks.php
Adding a logic hook can be done by using check_logic_hook_file:
//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()

getimagesize() is a function used to retrieve information about an image file.
Restricted use:
$img_size = getimagesize($path);
As there isn’t currently a direct workaround for this function, you may verify_uploaded_image() a good alternative to what you are trying to achieve. This function will verify if the image is a valid .png or .jpeg file.
require_once('include/utils.php');

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