Sugar On-Demand Code Pre-Check

Hosting Sugar on your own server has its perks. One is not confined to the standards of an on-demand environment. This often allows developers to bend the rules and stray from the restrictions set in place by Sugar, one of which are blacklisted PHP functions. A developer can leverage Sugar’s built-in Module Scanner to sift through any custom files and make sure that the code is free of these functions.

1. Create a new [SCRIPT_NAME].php in your root Sugar directory.

2. Copy and paste the code below:

<?php

     if(!defined(‘sugarEntry’))define(‘sugarEntry’, true);

     //change directories to where this file is located.

     chdir(dirname(__FILE__));

     define(‘ENTRY_POINT_TYPE’, ‘api’);

     require_once(‘include/entryPoint.php’);

     global $current_user, $log;

     require_once __DIR__.‘/ModuleInstall/ModuleScanner.php’;

     $test = new ModuleScanner();

     // Using a git ignore file to ignore all stock sugar files

     $lines = file(‘[PATH_TO_IGNORE_FILE]’,FILE_IGNORE_NEW_LINES);

     $Directory = new RecursiveDirectoryIterator(__DIR__.“/custom”);

     $Iterator = new RecursiveIteratorIterator($Directory);

     $files = new RegexIterator($Iterator, ‘/^.+\.php$/i’,

          RecursiveRegexIterator::GET_MATCH);

     foreach($files as $file) {

          $fn = $file[0];

          $ignore = 0;

          foreach($lines as $line) {

               if(strpos($line,$fn) !== false) {

                    $ignore = 1;

               }

          }

          if($ignore == 0) {

               $cont = file_get_contents($fn);

               if($test->isPHPFile($cont)) {

                    $issues = $test->scanFile($fn);

                    if(!empty($issues)) {

                         echo $fn\n;

                         var_dump($issues);

                         echo \n\n;

                    }

               }

          }

     }

     // If we have a session left over, destroy it

     if(session_id()) {

          session_destroy();

     }

3. Here we are using a file to list all of Sugar’s stock folders so that we can ignore them. A list of directories and files can be created and passed into the [PATH_TO_IGNORE_FILE]. This is what it could look like:

custom/Extension/modules/Accounts/Ext/Vardefs/rli_link_workflow.phpcustom/Extension/modules/ActivityStream/

custom/Extension/modules/Emails/Ext/Vardefs/rli_link_workflow.php

custom/Extension/modules/Forecasts/Ext/clients/

custom/Extension/modules/Manufacturers/Ext/Vardefs/rli_link_workflow.php

custom/Extension/modules/Opportunities/Ext/Dependencies/

custom/Extension/modules/Opportunities/Ext/Vardefs/rli_link_workflow.php

custom/Extension/modules/Project/Ext/Vardefs/rli_link_workflow.php

custom/Extension/modules/RevenueLineItems/Ext/Vardefs/

custom/modules/Accounts/Ext/

custom/modules/Activities/Ext/

custom/modules/ActivityStream/

custom/modules/Administration/

custom/modules/Bugs/Ext/

custom/modules/Calls/Ext/

custom/modules/CampaignTrackers/

Since we are setting the directory variable to point to the custom directory, the ignore file (above) need not include any elements of the root modules directory.

4. Once these changes are made, simply save the file, open a new terminal in your Sugar’s root directory and run the following:

php -f [SCRIPT_NAME].php

5. You’ll see the error’s echoed out on your terminal screen. If you wish to save these into a file to better examine, simply change the above command to output to a file instead:

php -f [SCRIPT_NAME].php > [OUTPUT_FILE].php