";
$keys = array();
foreach($values as $field) {
$keys = array_unique($keys + array_keys($field));
}
$cols = count($keys);
$contents .= "
$header
";
foreach($keys as $key) {
$contents .= "$key | ";
}
$contents .= "
";
foreach($values as $field) {
$contents .= "";
foreach($field as $item) {
if(is_array($item)) {
$item = join(",", $item);
}
$contents .= "$item | ";
}
$contents .= "
";
}
$contents .= "";
return $contents;
}
// expects a string containing the name of the table you would like to get the dump of
// expects there to already be a connection to the db and the 'use database_name' to be done
// returns a string containing (in html) the dump of all rows
function getFullTableDump($tableName){
global $db;
$cols = $db->get_columns($tableName);
$indexes = $db->get_indices($tableName);
$returnString = "";
//setting up table header for each file
$returnString .= array_as_table("{$db->dbName} $tableName Definitions:", $cols);
$returnString .= array_as_table("{$db->dbName} $tableName Keys:", $indexes);
$returnString .= "
";
$def_count = count($cols);
$td_result = $db->query("select * from ".$tableName);
if(!$td_result) {
return $db->lastError();
}
$returnString .= "# | ";
$fields = $db->getFieldsArray($td_result);
foreach($fields as $field) {
$returnString .= "$field | ";
}
$returnString .= "
";
$row_counter = 1;
while($row = $db->fetchByAssoc($td_result))
{
$row = array_values($row);
$returnString .= "";
$returnString .= "".$row_counter." | ";
for($counter = 0; $counter < $def_count; $counter++) {
$replace_val = false;
//perform this check when counter is set to two, which means it is on the 'value' column
if($counter == 2) {
//if the previous "name" column value was set to smtppass, set replace_val to true
if(strcmp($row[$counter - 1], "smtppass") == 0 )
$replace_val = true;
//if the previous "name" column value was set to smtppass,
//and the "category" value set to ldap, set replace_val to true
if (strcmp($row[$counter - 2], "ldap") == 0 && strcmp($row[$counter - 1], "admin_password") == 0)
$replace_val = true;
//if the previous "name" column value was set to password,
//and the "category" value set to proxy, set replace_val to true
if(strcmp($row[$counter - 2], "proxy") == 0 && strcmp($row[$counter - 1], "password") == 0 )
$replace_val = true;
}
if($replace_val) {
$returnString .= "******** | ";
} else {
$returnString .= "".($row[$counter] == "" ? " " : $row[$counter])." | ";
}
}
$row_counter++;
$returnString .= "
";
}
$returnString .= "
";
return $returnString;
}
// Deletes the directory recursively
function deleteDir($dir)
{
if (substr($dir, strlen($dir)-1, 1) != '/')
$dir .= '/';
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!deleteDir($dir.$obj))
return false;
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
return false;
}
}
}
closedir($handle);
if (!@rmdir($dir))
return false;
return true;
}
return false;
}
function prepareDiag()
{
global $getDumpsFrom;
global $cacheDir;
global $curdatetime;
global $progress_bar_percent;
global $skip_md5_diff;
global $sod_guid;
global $mod_strings;
echo getClassicModuleTitle(
"Administration",
array(
"{$mod_strings['LBL_MODULE_NAME']}",
translate('LBL_DIAGNOSTIC_TITLE')
),
false
);
echo "
";
echo $mod_strings['LBL_DIAGNOSTIC_EXECUTING'];
echo "
";
//determine if files.md5 exists or not
if(file_exists('files.md5'))
$skip_md5_diff = false;
else
$skip_md5_diff = true;
// array of all tables that we need to pull rows from below
$getDumpsFrom = array('config' => 'config',
'fields_meta_data' => 'fields_meta_data',
'upgrade_history' => 'upgrade_history',
'versions' => 'versions',
);
//Creates the diagnostic directory in the cache directory
$cacheDir = create_cache_directory("diagnostic/");
$cacheDir = create_cache_directory("diagnostic/".$sod_guid);
$cacheDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/");
display_progress_bar("diagnostic", $progress_bar_percent, 100);
ob_flush();
}
function executesugarlog()
{
//BEGIN COPY SUGARCRM.LOG
//Copies the Sugarcrm log to our diagnostic directory
global $cacheDir;
require_once('include/SugarLogger/SugarLogger.php');
$logger = new SugarLogger();
if(!copy($logger->getLogFileNameWithPath(), $cacheDir.'/'.$logger->getLogFileName())) {
echo "Couldn't copy sugarcrm.log to cacheDir.
";
}
//END COPY SUGARCRM.LOG
//UPDATING PROGRESS BAR
sodUpdateProgressBar(SUGARLOG_WEIGHT);
}
function executephpinfo()
{
//BEGIN GETPHPINFO
//This gets phpinfo, writes to a buffer, then I write to phpinfo.html
global $cacheDir;
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
ob_clean();
$handle = sugar_fopen($cacheDir."phpinfo.html", "w");
if(fwrite($handle, $phpinfo) === FALSE){
echo "Cannot write to file ".$cacheDir."phpinfo.html
";
}
fclose($handle);
//END GETPHPINFO
//UPDATING PROGRESS BAR
sodUpdateProgressBar(PHPINFO_WEIGHT);
}
function executeconfigphp()
{
//BEGIN COPY CONFIG.PHP
//store db_password in temp var so we can get config.php w/o making anyone angry
global $cacheDir; global $sugar_config;
$tempPass = $sugar_config['dbconfig']['db_password'];
$sugar_config['dbconfig']['db_password'] = '********';
//write config.php to a file
write_array_to_file("Diagnostic", $sugar_config, $cacheDir."config.php");
//restore db_password so everything still works
$sugar_config['dbconfig']['db_password'] = $tempPass;
//END COPY CONFIG.PHP
//UPDATING PROGRESS BAR
sodUpdateProgressBar(CONFIG_WEIGHT);
}
function execute_sql($getinfo, $getdumps, $getschema)
{
//BEGIN GET DB INFO
global $getDumpsFrom;
global $curdatetime;
global $sod_guid;
global $db;
$sqlInfoDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/{$db->dbName}/");
//create directory for table definitions
if($getschema)
$tablesSchemaDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/{$db->dbName}/TableSchema/");
//make sure they checked the box to get basic info
if($getinfo)
{
$info = $db->getDbInfo();
$content = '';
if(!empty($info)) {
foreach($info as $name => $value) {
$content .= "$name: $value
";
}
}
if(!empty($content)) {
file_put_contents($sqlInfoDir."{$db->dbName}-General-info.html", $content);
sodUpdateProgressBar(SQL_INFO_WEIGHT);
}
}
$style = '';
if($getschema)
{
//BEGIN GET ALL TABLES SCHEMAS
$all_tables = $db->getTablesArray();
$contents = $style;
foreach($all_tables as $tablename){
$cols = $db->get_columns($tablename);
$indexes = $db->get_indices($tablename);
//setting up table header for each file
$contents .= array_as_table("{$db->dbName} $tablename Definitions:", $cols);
$contents .= array_as_table("{$db->dbName} $tablename Keys:", $indexes);
$contents .= "
";
}
file_put_contents($tablesSchemaDir."{$db->dbName}TablesSchema.html", $contents);
//END GET ALL TABLES SCHEMAS
//BEGIN UPDATING PROGRESS BAR
sodUpdateProgressBar(SQL_SCHEMA_WEIGHT);
//END UPDATING PROGRESS BAR
}
if($getdumps)
{
//BEGIN GET TABLEDUMPS
$tableDumpsDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/{$db->dbName}/TableDumps/");
foreach ($getDumpsFrom as $table)
{
//calling function defined above to get the string for dump
$contents = $style .getFullTableDump($table);
file_put_contents($tableDumpsDir.$table.".html", $contents);
}
//END GET TABLEDUMPS
//BEGIN UPDATING PROGRESS BAR
sodUpdateProgressBar(SQL_DUMPS_WEIGHT);
//END UPDATING PROGRESS BAR
}
//END GET DB INFO
}
function executebeanlistbeanfiles()
{
//BEGIN CHECK BEANLIST FILES ARE AVAILABLE
global $cacheDir;
global $beanList;
global $beanFiles;
global $mod_strings;
ob_start();
echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_DESC'];
echo "
";
echo "";
echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_GREEN'];
echo "";
echo "
";
echo "";
echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_ORANGE'];
echo "";
echo "
";
echo "";
echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_RED'];
echo "";
echo "
";
foreach ($beanList as $beanz)
{
if(!isset($beanFiles[$beanz]))
{
echo "NO! --- ".$beanz." is not an index in \$beanFiles
";
}
else
{
if(file_exists($beanFiles[$beanz]))
echo "YES --- ".$beanz." file \"".$beanFiles[$beanz]."\" exists
";
else
echo "NO! --- ".$beanz." file \"".$beanFiles[$beanz]."\" does NOT exist
";
}
}
$content = ob_get_contents();
ob_clean();
$handle = sugar_fopen($cacheDir."beanFiles.html", "w");
if(fwrite($handle, $content) === FALSE){
echo "Cannot write to file ".$cacheDir."beanFiles.html
";
}
fclose($handle);
//END CHECK BEANLIST FILES ARE AVAILABLE
//BEGIN UPDATING PROGRESS BAR
sodUpdateProgressBar(BEANLISTBEANFILES_WEIGHT);
//END UPDATING PROGRESS BAR
}
function executecustom_dir()
{
//BEGIN ZIP AND SAVE CUSTOM DIRECTORY
global $cacheDir;
zip_dir("custom", $cacheDir."custom_directory.zip");
//END ZIP AND SAVE CUSTOM DIRECTORY
//BEGIN UPDATING PROGRESS BAR
sodUpdateProgressBar(CUSTOM_DIR_WEIGHT);
//END UPDATING PROGRESS BAR
}
function executemd5($filesmd5, $md5calculated)
{
//BEGIN ALL MD5 CHECKS
global $curdatetime;
global $skip_md5_diff;
global $sod_guid;
if(file_exists('files.md5'))
include( 'files.md5');
//create dir for md5s
$md5_directory = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/md5/");
//skip this if the files.md5 didn't exist
if(!$skip_md5_diff)
{
//make sure the files.md5
if($filesmd5)
if(!copy('files.md5', $md5_directory."files.md5"))
echo "Couldn't copy files.md5 to ".$md5_directory."
Skipping md5 checks.
";
}
$md5_string_calculated = generateMD5array('./');
if($md5calculated)
write_array_to_file('md5_string_calculated', $md5_string_calculated, $md5_directory."md5_array_calculated.php");
//if the files.md5 didn't exist, we can't do this
if(!$skip_md5_diff)
{
$md5_string_diff = array_diff($md5_string_calculated, $md5_string);
write_array_to_file('md5_string_diff', $md5_string_diff, $md5_directory."md5_array_diff.php");
}
//END ALL MD5 CHECKS
//BEGIN UPDATING PROGRESS BAR
sodUpdateProgressBar(MD5_WEIGHT);
//END UPDATING PROGRESS BAR
}
function executevardefs()
{
//BEGIN DUMP OF SUGAR SCHEMA (VARDEFS)
//END DUMP OF SUGAR SCHEMA (VARDEFS)
//BEGIN UPDATING PROGRESS BAR
//This gets the vardefs, writes to a buffer, then I write to vardefschema.html
global $cacheDir;
global $beanList;
global $beanFiles;
global $dictionary;
global $sugar_version;
global $sugar_db_version;
global $sugar_flavor;
ob_start();
foreach ( $beanList as $beanz ) {
// echo "Module: ".$beanz."
";
$path_parts = pathinfo( $beanFiles[ $beanz ] );
$vardefFileName = $path_parts[ 'dirname' ]."/vardefs.php";
if( file_exists( $vardefFileName )) {
// echo "
".$vardefFileName."
";
}
include_once( $vardefFileName );
}
echo "";
echo "";
echo "Schema listing based on vardefs
";
echo "Sugar version: ".$sugar_version." / Sugar DB version: ".$sugar_db_version." / Sugar flavor: ".$sugar_flavor;
echo "
";
echo "";
$tables = array();
foreach($dictionary as $vardef) {
$tables[] = $vardef['table'];
$fields[$vardef['table']] = $vardef['fields'];
$comments[$vardef['table']] = $vardef['comment'];
}
asort($tables);
foreach($tables as $t) {
$name = $t;
if ( $name == "does_not_exist" )
continue;
$comment = $comments[$t];
echo "Table: $t
{$comment}
";
echo "";
echo '
Column |
Type |
Length |
Required |
Comment |
';
ksort( $fields[ $t ] );
foreach($fields[$t] as $k => $v) {
// we only care about physical tables ('source' can be 'non-db' or 'nondb' or 'function' )
if ( isset( $v[ 'source' ] ))
continue;
$columnname = $v[ 'name' ];
$columntype = $v[ 'type' ];
$columndbtype = $v[ 'dbType' ];
$columnlen = $v[ 'len' ];
$columncomment = $v[ 'comment' ];
$columnrequired = $v[ 'required' ];
if ( empty( $columnlen ) ) $columnlen = 'n/a';
if ( empty( $columncomment ) ) $columncomment = '(none)';
if ( !empty( $columndbtype ) ) $columntype = $columndbtype;
if ( empty( $columnrequired ) || ( $columnrequired == false ))
$columndisplayrequired = 'no';
else
$columndisplayrequired = 'yes';
echo '
'.$columnname.' |
'.$columntype.' |
'.$columnlen.' |
'.$columndisplayrequired.' |
'.$columncomment.' |
';
}
echo "
";
}
echo "";
$vardefFormattedOutput = ob_get_contents();
ob_clean();
$handle = sugar_fopen($cacheDir."vardefschema.html", "w");
if(fwrite($handle, $vardefFormattedOutput) === FALSE){
echo "Cannot write to file ".$cacheDir."vardefschema.html
";
}
fclose($handle);
sodUpdateProgressBar(VARDEFS_WEIGHT);
//END UPDATING PROGRESS BAR
}
function finishDiag(){
//BEGIN ZIP ALL FILES AND EXTRACT IN CACHE ROOT
global $cacheDir;
global $curdatetime;
global $sod_guid;
global $mod_strings;
chdir($cacheDir);
zip_dir(".", "../diagnostic".$curdatetime.".zip");
//END ZIP ALL FILES AND EXTRACT IN CACHE ROOT
chdir(RETURN_FROM_DIAG_DIR);
deleteDir($cacheDir);
print "".$mod_strings['LBL_DIAGNOSTIC_DOWNLOADLINK']."
";
print "".$mod_strings['LBL_DIAGNOSTIC_DELETELINK']."
";
}
//BEGIN check for what we are executing
$doconfigphp = ((empty($_POST['configphp']) || $_POST['configphp'] == 'off') ? false : true);
$docustom_dir = ((empty($_POST['custom_dir']) || $_POST['custom_dir'] == 'off') ? false : true);
$dophpinfo = ((empty($_POST['phpinfo']) || $_POST['phpinfo'] == 'off') ? false : true);
$domysql_dumps = ((empty($_POST['mysql_dumps']) || $_POST['mysql_dumps'] == 'off') ? false : true);
$domysql_schema = ((empty($_POST['mysql_schema']) || $_POST['mysql_schema'] == 'off') ? false : true);
$domysql_info = ((empty($_POST['mysql_info']) || $_POST['mysql_info'] == 'off') ? false : true);
$domd5 = ((empty($_POST['md5']) || $_POST['md5'] == 'off') ? false : true);
$domd5filesmd5 = ((empty($_POST['md5filesmd5']) || $_POST['md5filesmd5'] == 'off') ? false : true);
$domd5calculated = ((empty($_POST['md5calculated']) || $_POST['md5calculated'] == 'off') ? false : true);
$dobeanlistbeanfiles = ((empty($_POST['beanlistbeanfiles']) || $_POST['beanlistbeanfiles'] == 'off') ? false : true);
$dosugarlog = ((empty($_POST['sugarlog']) || $_POST['sugarlog'] == 'off') ? false : true);
$dovardefs = ((empty($_POST['vardefs']) || $_POST['vardefs'] == 'off') ? false : true);
//END check for what we are executing
//BEGIN items to calculate progress bar
$totalitems = 0;
$totalweight = 0;
if($doconfigphp) {$totalweight += CONFIG_WEIGHT; $totalitems++;}
if($docustom_dir) {$totalweight += CUSTOM_DIR_WEIGHT; $totalitems++;}
if($dophpinfo) {$totalweight += PHPINFO_WEIGHT; $totalitems++;}
if($domysql_dumps) {$totalweight += SQL_DUMPS_WEIGHT; $totalitems++;}
if($domysql_schema) {$totalweight += SQL_SCHEMA_WEIGHT; $totalitems++;}
if($domysql_info) {$totalweight += SQL_INFO_WEIGHT; $totalitems++;}
if($domd5) {$totalweight += MD5_WEIGHT; $totalitems++;}
if($dobeanlistbeanfiles) {$totalweight += BEANLISTBEANFILES_WEIGHT; $totalitems++;}
if($dosugarlog) {$totalweight += SUGARLOG_WEIGHT; $totalitems++;}
if($dovardefs) {$totalweight += VARDEFS_WEIGHT; $totalitems++;}
//END items to calculate progress bar
//prepare initial steps
prepareDiag();
if($doconfigphp)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETCONFPHP']."
";
executeconfigphp();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($docustom_dir)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETCUSTDIR']."
";
executecustom_dir();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($dophpinfo)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETPHPINFO']."
";
executephpinfo();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($domysql_info || $domysql_dumps || $domysql_schema)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETTING'].
($domysql_info ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLINFO'] : " ").
($domysql_dumps ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLTD'] : " ").
($domysql_schema ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLTS'] : "...").
"
";
execute_sql($domysql_info, $domysql_dumps, $domysql_schema);
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($domd5)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETMD5INFO']."
";
executemd5($domd5filesmd5, $domd5calculated);
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($dobeanlistbeanfiles)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETBEANFILES']."
";
executebeanlistbeanfiles();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($dosugarlog)
{
echo $mod_strings['LBL_DIAGNOSTIC_GETSUGARLOG']."
";
executesugarlog();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
if($dovardefs)
{
echo $mod_strings['LBL_DIAGNOSTIC_VARDEFS']."
";
executevardefs();
echo $mod_strings['LBL_DIAGNOSTIC_DONE']."
";
}
//finish up the last steps
finishDiag();
?>