Determining Environment at Runtime

When creating customizations in Customer Portal (CP3), it is important to remember that your code needs to run not only in Production, but should also be engineered to run in a "Clone" environment without modification. This is especially important with integrations to external systems. You wouldn't want non-production data being passed to an external production system.

A "Clone" is a copy of a RightNow Production site. A clone has its own set of files, database, URL, and configuration. Clones are used for many purposes. Many clients use their paid "TST" site (a clone) for development work or training. Additionally, Oracle makes internal clones for many purposes including for upgrades, customer care testing or professional services engagements. Many of these clones could exist without your knowledge.

For this reason, it is important to make your customizations "clone-proof". This often means modifying behavior or configuration at runtime in your PHP scripts. There are no build in PHP Constants that indicate if a site is a clone, but it is easy enough to create your own. I personally like to use the config/hooks.php file as a location where I set up PHP Constants and other global behaviors. The file is always executed regardless of request type (page view or ajax request), and it runs very early in the applications execution.

At the bottom of my config/hooks.php file, I like to place the following code.


<?php //config/hooks.php

//...
//...

/* Global Constants */

/* Determine if this is Prod, or a clone */
if (!defined('45NORTH_IS_PRODUCTION')) {
    $db_name = \RightNow\Utils\Config::getConfig(DB_NAME);
    if (strpos($db_name, '__') == false) {
        define('45NORTH_IS_PRODUCTION', true);
    } else {
        define('45NORTH_IS_PRODUCTION', false);
    }
}

I determine if a site is a "clone" by looking at the DB_NAME site configuration property. This value is not available through the Configuration Settings Workspace, but you can access it via code. If the DB_NAME property contains an __ (double underscore) character, the site is a clone; otherwise the site is your production environment. For example, if my production site was named '45NORTH', my TST site would automatically be named "45NORTH__TST" by the clone process. I then set a PHP Constant with true or false depending on the result. That constant can then be easily used in any subsequent PHP scripts.

For Example:


<?php

if(constant('45NORTH_IS_PRODUCTION') == true)
{
  $url = "http://myprodurl.com";
}
else
{
  $url = "http://mydevurl.com";
}

doSomething($url);

This has been the most consistent way I've found of making this determination. Other approaches I've seen rely on looking at the request URL and searching for a "--" (double hyphen) characters. This works most of the time but custom VHOSTS can be used for clone sites, and the "--" characters could not be part of your custom VHOST name. Additionally, a request URL is not available in a CPM. For these reasons, I like to go with the DB_NAME property as in all circumstances I've seen it has worked.

Zircon - This is a contributing Drupal Theme
Design by WeebPal.