Config Tutorial
Config is a 'background' module providing developers and extensions an easy way of saving configuration settings in a existing SugarCRM database tabls. Storing variables in a database has some advantages over storing in configuration files, such as difficulty in handling existing code in config_override.php and SugarCRM's tendency to remove all unexpected code upon saving.
Activating Config
Config can be activated as easily as pasting this line of code at the top of your script:
<?php
require_once 'custom/include/Config/Config.php';
?>Retrieving a Variable
In order to retrieve a previously stored variable from Sugar, just use config_get() as follows:
<?php
config_get("DownForMaintenance", 0);
?>The function config_get is straightforward and has the following syntax:
config_get(variable name, default)
So in order to check whether SugarCRM is down for maintenance, as is done in SugarDev.net Tools, this code is executed:
<?php
if (config_get("DownForMaintenance", 0) != 1) {
return;
}
?>Storing a Variable
Storing a variable is easy with config_set():
<?php
config_set("DownForMaintenance", $_REQUEST['down']);
?>config_set(variable name, value)
That's it!
Specials
Storing HTML code
For storing HTML code, you may use html_entity_decode():
<?php
$customMsg = html_entity_decode(config_get("DownForMaintenanceMessage", null))
?>Storing sensitive information
You may store passwords and other sensitive information using SugarCRM's Blowfish system:
<?php
//Storing
config_set('secretPassword', blowfishEncode(blowfishGetKey('MY_KEY'), $_POST['DrupalPass']));
//Retrieving
blowfishDecode(blowfishGetKey('MY_KEY'), config_get('DrupalPass', null));
?>When doing this, make sure that "MY_KEY" is exactly the same in the two lines. There is no need to do anything else, both SugarCRM and Config will create what's needed on the first run. Note: The security of this feature is limited. As is true with Sugar's e-mail accounts passwords, the system (database + application code) need to be able to reverse the Blowfish encryption because they need to original value. This is fundamentally different from hashing, which is used for storing User passwords. In short: If you have the code or the database, you cannot retrieve the encrypted information. But if you have both, you can.
Some Details
- Since all variables are serialized, any data format (strings, integers, booleans, etc.) can be stored using Config
- Sugar's caching system is used in order to minimize Config's impact on performance, making Config very scalable.
