]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/DiagnosticRun.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Administration / DiagnosticRun.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41 require_once( 'include/utils/progress_bar_utils.php' );
42 require_once( 'include/utils/zip_utils.php' );
43
44
45
46 global $skip_md5_diff;
47 $skip_md5_diff = false;
48
49 set_time_limit(3600);
50 // get all needed globals
51 global $app_strings;
52 global $app_list_strings;
53 global $mod_strings;
54
55 global $theme;
56
57
58 global $db;
59 if(empty($db)) {
60
61         $db = DBManagerFactory::getInstance();
62 }
63
64 global $current_user;
65 if(!is_admin($current_user)){
66         die($mod_strings['LBL_DIAGNOSTIC_ACCESS']);
67 }
68 global $sugar_config;
69 global $beanFiles;
70
71
72
73 //get sugar version and flavor
74 global $sugar_version;
75 global $sugar_flavor;
76
77
78 //guid used for directory path
79 global $sod_guid;
80 $sod_guid = create_guid();
81
82 //GET CURRENT DATETIME STAMP TO USE IN FILENAME
83 global $curdatetime;
84 $curdatetime = date("Ymd-His");
85
86
87 global $progress_bar_percent;
88 $progress_bar_percent = 0;
89 global $totalweight;
90 $totalweight = 0;
91 global $totalitems;
92 $totalitems = 0;
93 global $currentitems;
94 $currentitems = 0;
95 define("CONFIG_WEIGHT", 1);
96 define("CUSTOM_DIR_WEIGHT", 1);
97 define("PHPINFO_WEIGHT", 1);
98 define("MYSQL_DUMPS_WEIGHT", 2);
99 define("MYSQL_SCHEMA_WEIGHT", 3);
100 define("MYSQL_INFO_WEIGHT", 1);
101 define("MD5_WEIGHT", 5);
102 define("BEANLISTBEANFILES_WEIGHT", 1);
103 define("SUGARLOG_WEIGHT", 2);
104 define("VARDEFS_WEIGHT", 2);
105
106 //THIS MUST CHANGE IF THE NUMBER OF DIRECTORIES TRAVERSED TO GET TO
107 //   THE DIAGNOSTIC CACHE DIR CHANGES
108 define("RETURN_FROM_DIAG_DIR", "../../../..");
109
110 global $getDumpsFrom;
111 $getDumpsFrom = Array();
112
113 global $cacheDir;
114 $cacheDir = "";
115
116 function sodUpdateProgressBar($itemweight){
117     global $progress_bar_percent;
118     global $totalweight;
119     global $totalitems;
120     global $currentitems;
121
122     $currentitems++;
123     if($currentitems == $totalitems)
124       update_progress_bar("diagnostic", 100, 100);
125     else
126     {
127       $progress_bar_percent += ($itemweight / $GLOBALS['totalweight'] * 100);
128       update_progress_bar("diagnostic", $progress_bar_percent, 100);
129     }
130 }
131
132
133 // expects a string containing the name of the table you would like to get the dump of
134 // expects there to already be a connection to mysql and the 'use database_name' to be done
135 // returns a string containing (in html) the dump of all rows
136 function getFullTableDump($tableName){
137
138         global $db;
139
140         $returnString = "<table border=\"1\">";
141         $returnString .= "<tr><b><center>Table ".$tableName."</center></b></tr>";
142         //get table field definitions
143         $definitions = array();
144         $def_result = $db->query("describe ".$tableName);
145         if(!$def_result)
146         {
147                 return mysql_error();
148         }
149         else
150         {
151                 $returnString .= "<tr><td><b>Row Num</b></td>";
152                 $def_count = 0;
153                 while($row = $db->fetchByAssoc($def_result))
154                 {
155                         $row = array_values($row);
156                         $definitions[$def_count] = $row[0];
157                         $def_count++;
158                         $returnString .= "<td><b>".$row[0]."</b></td>";
159                 }
160                 $returnString .= "</tr>";
161         }
162
163         $td_result = $db->query("select * from ".$tableName);
164         if(!$td_result)
165         {
166                 return mysql_error();
167         }
168         else
169         {
170                 $row_counter = 1;
171                 while($row = $db->fetchByAssoc($td_result))
172                 {
173                         $row = array_values($row);
174                         $returnString .= "<tr>";
175                         $returnString .= "<td>".$row_counter."</td>";
176                         for($counter = 0; $counter < $def_count; $counter++){
177
178                         $replace_val = false;
179                         //perform this check when counter is set to two, which means it is on the 'value' column
180                         if($counter == 2){
181                                 //if the previous "name" column value was set to smtppass, set replace_val to true 
182                                 if(strcmp($row[$counter - 1], "smtppass") == 0  )
183                                         $replace_val = true;
184                                 
185                                 //if the previous "name" column value was set to smtppass, 
186                                 //and the "category" value set to ldap, set replace_val to true
187                                 if (strcmp($row[$counter - 2], "ldap") == 0 && strcmp($row[$counter - 1], "admin_password") == 0)
188                                         $replace_val = true;
189                                         
190                                 //if the previous "name" column value was set to password, 
191                                 //and the "category" value set to proxy, set replace_val to true
192                                 if(strcmp($row[$counter - 2], "proxy") == 0 && strcmp($row[$counter - 1], "password") == 0 )
193                                         $replace_val = true;                            
194                         }
195
196                         if($replace_val)                                
197                                         $returnString .= "<td>********</td>";
198                                 else
199                                         $returnString .= "<td>".($row[$counter] == "" ? "&nbsp;" : $row[$counter])."</td>";
200                         }
201                         $row_counter++;
202                         $returnString .= "</tr>";
203                 }
204         }
205         $returnString .= "</table>";
206
207         return $returnString;
208
209 }
210
211 // Deletes the directory recursively
212 function deleteDir($dir)
213 {
214    if (substr($dir, strlen($dir)-1, 1) != '/')
215        $dir .= '/';
216
217    if ($handle = opendir($dir))
218    {
219        while ($obj = readdir($handle))
220        {
221            if ($obj != '.' && $obj != '..')
222            {
223                if (is_dir($dir.$obj))
224                {
225                    if (!deleteDir($dir.$obj))
226                        return false;
227                }
228                elseif (is_file($dir.$obj))
229                {
230                    if (!unlink($dir.$obj))
231                        return false;
232                }
233            }
234        }
235
236        closedir($handle);
237
238        if (!@rmdir($dir))
239            return false;
240        return true;
241    }
242    return false;
243 }
244
245
246 function prepareDiag()
247 {
248         global $getDumpsFrom;
249         global $cacheDir;
250         global $curdatetime;
251         global $progress_bar_percent;
252         global $skip_md5_diff;
253         global $sod_guid;
254         global $mod_strings;
255
256         echo getClassicModuleTitle(
257         "Administration", 
258         array(
259             "<a href='index.php?module=Administration&action=index'>{$mod_strings['LBL_MODULE_NAME']}</a>",
260            translate('LBL_DIAGNOSTIC_TITLE')
261            ), 
262         true
263         );
264         echo "<BR>";
265         echo $mod_strings['LBL_DIAGNOSTIC_EXECUTING'];
266         echo "<BR>";
267
268
269         //determine if files.md5 exists or not
270         if(file_exists('files.md5'))
271                 $skip_md5_diff = false;
272         else
273                 $skip_md5_diff = true;
274
275         // array of all tables that we need to pull rows from below
276         $getDumpsFrom = array('config' => 'config',
277                               'fields_meta_data' => 'fields_meta_data',
278                               'upgrade_history' => 'upgrade_history',
279                               'versions' => 'versions',
280                               );
281
282
283         //Creates the diagnostic directory in the cache directory
284     $cacheDir = create_cache_directory("diagnostic/");
285     $cacheDir = create_cache_directory("diagnostic/".$sod_guid);
286     $cacheDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/");
287
288         display_progress_bar("diagnostic", $progress_bar_percent, 100);
289
290         ob_flush();
291 }
292
293 function executesugarlog()
294 {
295     //BEGIN COPY SUGARCRM.LOG
296     //Copies the Sugarcrm log to our diagnostic directory
297     global $cacheDir;
298         require_once('include/SugarLogger/SugarLogger.php');
299         $logger = new SugarLogger();
300     if(!copy($logger->getLogFileNameWithPath(), $cacheDir.'/'.$logger->getLogFileName())) {
301       echo "Couldn't copy sugarcrm.log to cacheDir.<br>";
302     }
303     //END COPY SUGARCRM.LOG
304
305     //UPDATING PROGRESS BAR
306     sodUpdateProgressBar(SUGARLOG_WEIGHT);
307 }
308
309 function executephpinfo()
310 {
311     //BEGIN GETPHPINFO
312     //This gets phpinfo, writes to a buffer, then I write to phpinfo.html
313     global $cacheDir;
314
315     ob_start();
316     phpinfo();
317     $phpinfo = ob_get_contents();
318     ob_clean();
319
320     $handle = sugar_fopen($cacheDir."phpinfo.html", "w");
321     if(fwrite($handle, $phpinfo) === FALSE){
322       echo "Cannot write to file ".$cacheDir."phpinfo.html<br>";
323     }
324     fclose($handle);
325     //END GETPHPINFO
326
327     //UPDATING PROGRESS BAR
328     sodUpdateProgressBar(PHPINFO_WEIGHT);
329 }
330
331 function executeconfigphp()
332 {
333     //BEGIN COPY CONFIG.PHP
334     //store db_password in temp var so we can get config.php w/o making anyone angry
335     global $cacheDir;    global $sugar_config;
336
337     $tempPass = $sugar_config['dbconfig']['db_password'];
338     $sugar_config['dbconfig']['db_password'] = '********';
339     //write config.php to a file
340     write_array_to_file("Diagnostic", $sugar_config, $cacheDir."config.php");
341     //restore db_password so everything still works
342     $sugar_config['dbconfig']['db_password'] = $tempPass;
343     //END COPY CONFIG.PHP
344
345     //UPDATING PROGRESS BAR
346     sodUpdateProgressBar(CONFIG_WEIGHT);
347 }
348
349 function executemysql($getinfo, $getdumps, $getschema)
350 {
351     //BEGIN GET DB INFO
352     global $getDumpsFrom;
353     global $curdatetime;
354     global $sugar_config;
355     global $progress_bar_percent;
356     global $sod_guid;
357     global $db;
358
359     if($db->dbType != "mysql") {
360         if($getinfo) sodUpdateProgressBar(MYSQL_INFO_WEIGHT);
361         if($getschema) sodUpdateProgressBar(MYSQL_SCHEMA_WEIGHT);
362         if($getdumps) sodUpdateProgressBar(MYSQL_DUMPS_WEIGHT);
363         return;
364     }
365     
366     $mysqlInfoDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/MySQL/");
367
368
369     //create directory for table definitions
370     if($getschema)
371       $tablesSchemaDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/MySQL/TableSchema/");
372
373     //BEGIN GET MYSQL INFO
374     //make sure they checked the box to get basic info
375     if($getinfo)
376     {
377       ob_start();
378       echo "MySQL Version: ".(function_exists('mysqli_get_client_info') ? @mysqli_get_client_info() : @mysql_get_client_info())."<BR>";
379       echo "MySQL Host Info: ".(function_exists('mysqli_get_host_info') ? @mysqli_get_host_info($db->getDatabase()) : @mysql_get_host_info())."<BR>";
380       echo "MySQL Server Info: ".(function_exists('mysqli_get_client_info') ? @mysqli_get_client_info() : @mysql_get_client_info())."<BR>";
381       echo "MySQL Client Encoding: ".(function_exists('mysqli_character_set_name') ? @mysqli_character_set_name($db->getDatabase()) : @mysql_client_encoding())."<BR>";
382
383       /* Uncomment to get current processes as well
384       echo "<BR>MySQL Processes<BR>";
385       $res = $db->query('SHOW PROCESSLIST');
386       echo "<table border=\"1\"><tr><th>Id</th><th>Host</th><th>db</th><th>Command</th><th>Time</th></tr>";
387       if($db->getRowCount($res) > 0)
388       {
389         while($row = $db->fetchByAssoc($res))
390         {
391           printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
392                  $row['Id'], $row['Host'], $row['db'], $row['Command'], $row['Time']
393                  );
394         }
395         echo "</table><br>";
396       }
397       else
398       {
399         echo "</table>";
400         echo "No processes running<br>";
401       }
402       */
403
404       echo "<BR>MySQL Character Set Settings<BR>";
405       $res = $db->query("show variables like 'character\_set\_%'");
406       echo "<table border=\"1\"><tr><th>Variable Name</th><th>Value</th></tr>";
407       while($row = $db->fetchByAssoc($res))
408       {
409         printf("<tr><td>%s</td><td>%s</td></tr>",
410                $row['Variable_name'], $row['Value']
411                );
412       }
413       echo "</table>";
414
415       $content = ob_get_contents();
416       ob_clean();
417
418       $handle = sugar_fopen($mysqlInfoDir."MySQL-General-info.html", "w");
419       if(fwrite($handle, $content) === FALSE){
420         echo "Cannot write to file ".$mysqlInfoDir."_MySQL-General-info.html<br>";
421       }
422       fclose($handle);
423       //BEGIN UPDATING PROGRESS BAR
424       sodUpdateProgressBar(MYSQL_INFO_WEIGHT);
425       //END UPDATING PROGRESS BAR
426     }
427     //END GET MYSQL INFO
428
429
430     if($getschema)
431     {
432         //BEGIN GET ALL TABLES SCHEMAS
433         $all_tables = $db->getTablesArray();
434
435         global $theme_path;
436
437                 ob_start();
438                 echo "<style>";
439                 echo file_get_contents($theme_path."style.css");
440                 echo "</style>";
441         foreach($all_tables as $tablename){
442                         //setting up table header for each file
443                         echo "<table border=\"0\" cellpadding=\"0\" class=\"tabDetailView\">";
444                         echo "<tr>MySQL ".$tablename." Definitions:</tr>".
445                                 "<tr><td class=\"tabDetailViewDL\"><b>Field</b></td>".
446                                         "<td class=\"tabDetailViewDL\">Type</td>".
447                                         "<td class=\"tabDetailViewDL\">Null</td>".
448                                         "<td class=\"tabDetailViewDL\">Key</td>".
449                                         "<td class=\"tabDetailViewDL\">Default</td>".
450                                         "<td class=\"tabDetailViewDL\">Extra</td></tr>";
451                         $describe = $db->query("describe ".$tablename);
452                         while($inner_row = $db->fetchByAssoc($describe)){
453                                 $inner_row = array_values($inner_row);
454                                 echo "<tr><td class=\"tabDetailViewDF\"><b>".$inner_row[0]."</b></td>";
455                                 echo     "<td class=\"tabDetailViewDF\">".$inner_row[1]."</td>";
456                                 echo     "<td class=\"tabDetailViewDF\">".$inner_row[2]."</td>";
457                                 echo     "<td class=\"tabDetailViewDF\">".$inner_row[3]."</td>";
458                                 echo     "<td class=\"tabDetailViewDF\">".$inner_row[4]."</td>";
459                                 echo     "<td class=\"tabDetailViewDF\">".$inner_row[5]."</td></tr>";
460                         }
461                         echo "</table>";
462                         echo "<BR><BR>";
463                 }
464
465                 $content = ob_get_contents();
466                 ob_clean();
467
468                 $handle = sugar_fopen($tablesSchemaDir."MySQLTablesSchema.html", "w");
469                 if(fwrite($handle, $content) === FALSE){
470                   echo "Cannot write to file ".$tablesSchemaDir."MySQLTablesSchema.html<br>";
471                 }
472                 fclose($handle);
473
474                 //END GET ALL TABLES SCHEMAS
475                 //BEGIN UPDATING PROGRESS BAR
476                 sodUpdateProgressBar(MYSQL_SCHEMA_WEIGHT);
477                 //END UPDATING PROGRESS BAR
478     }
479
480     if($getdumps)
481     {
482                 //BEGIN GET TABLEDUMPS
483                 $tableDumpsDir = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/MySQL/TableDumps/");
484
485
486                 foreach ($getDumpsFrom as $table)
487                 {
488                         ob_start();
489                         //calling function defined above to get the string for dump
490                         echo getFullTableDump($table);
491                         $content = ob_get_contents();
492                         ob_clean();
493                         $handle = sugar_fopen($tableDumpsDir.$table.".html", "w");
494                         if(fwrite($handle, $content) === FALSE){
495                           echo "Cannot write to file ".$tableDumpsDir.$table."html<br>";
496                         }
497                         fclose($handle);
498                 }
499                 //END GET TABLEDUMPS
500                 //BEGIN UPDATING PROGRESS BAR
501                 sodUpdateProgressBar(MYSQL_DUMPS_WEIGHT);
502                 //END UPDATING PROGRESS BAR
503         }
504
505
506     //END GET DB INFO
507 }
508
509
510 function executebeanlistbeanfiles()
511 {
512     //BEGIN CHECK BEANLIST FILES ARE AVAILABLE
513     global $cacheDir;
514     global $beanList;
515     global $beanFiles;
516     global $mod_strings;
517
518     ob_start();
519
520     echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_DESC'];
521     echo "<BR>";
522     echo "<font color=green>";
523     echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_GREEN'];
524     echo "</font>";
525     echo "<BR>";
526     echo "<font color=orange>";
527     echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_ORANGE'];
528     echo "</font>";
529     echo "<BR>";
530     echo "<font color=red>";
531     echo $mod_strings['LBL_DIAGNOSTIC_BEANLIST_RED'];
532     echo "</font>";
533     echo "<BR><BR>";
534
535         foreach ($beanList as $beanz)
536         {
537                 if(!isset($beanFiles[$beanz]))
538                 {
539                         echo "<font color=orange>NO! --- ".$beanz." is not an index in \$beanFiles</font><br>";
540                 }
541                 else
542                 {
543                         if(file_exists($beanFiles[$beanz]))
544                                 echo "<font color=green>YES --- ".$beanz." file \"".$beanFiles[$beanz]."\" exists</font><br>";
545                         else
546                                 echo "<font color=red>NO! --- ".$beanz." file \"".$beanFiles[$beanz]."\" does NOT exist</font><br>";
547                 }
548         }
549
550         $content = ob_get_contents();
551         ob_clean();
552
553         $handle = sugar_fopen($cacheDir."beanFiles.html", "w");
554         if(fwrite($handle, $content) === FALSE){
555         echo "Cannot write to file ".$cacheDir."beanFiles.html<br>";
556     }
557     fclose($handle);
558     //END CHECK BEANLIST FILES ARE AVAILABLE
559     //BEGIN UPDATING PROGRESS BAR
560     sodUpdateProgressBar(BEANLISTBEANFILES_WEIGHT);
561     //END UPDATING PROGRESS BAR
562 }
563
564 function executecustom_dir()
565 {
566     //BEGIN ZIP AND SAVE CUSTOM DIRECTORY
567     global $cacheDir;
568
569     zip_dir("custom", $cacheDir."custom_directory.zip");
570     //END ZIP AND SAVE CUSTOM DIRECTORY
571     //BEGIN UPDATING PROGRESS BAR
572     sodUpdateProgressBar(CUSTOM_DIR_WEIGHT);
573     //END UPDATING PROGRESS BAR
574 }
575
576 function executemd5($filesmd5, $md5calculated)
577 {
578         //BEGIN ALL MD5 CHECKS
579         global $curdatetime;
580         global $skip_md5_diff;
581         global $sod_guid;
582         if(file_exists('files.md5'))
583         include( 'files.md5');
584         //create dir for md5s
585         $md5_directory = create_cache_directory("diagnostic/".$sod_guid."/diagnostic".$curdatetime."/md5/");
586
587         //skip this if the files.md5 didn't exist
588         if(!$skip_md5_diff)
589         {
590                 //make sure the files.md5
591                 if($filesmd5)
592                         if(!copy('files.md5', $md5_directory."files.md5"))
593                                 echo "Couldn't copy files.md5 to ".$md5_directory."<br>Skipping md5 checks.<br>";
594         }
595
596         $md5_string_calculated = generateMD5array('./');
597
598         if($md5calculated)
599                 write_array_to_file('md5_string_calculated', $md5_string_calculated, $md5_directory."md5_array_calculated.php");
600
601
602         //if the files.md5 didn't exist, we can't do this
603         if(!$skip_md5_diff)
604         {
605                 $md5_string_diff = array_diff($md5_string_calculated, $md5_string);
606
607                 write_array_to_file('md5_string_diff', $md5_string_diff, $md5_directory."md5_array_diff.php");
608         }
609         //END ALL MD5 CHECKS
610     //BEGIN UPDATING PROGRESS BAR
611     sodUpdateProgressBar(MD5_WEIGHT);
612     //END UPDATING PROGRESS BAR
613 }
614
615 function executevardefs()
616 {
617     //BEGIN DUMP OF SUGAR SCHEMA (VARDEFS)
618
619     //END DUMP OF SUGAR SCHEMA (VARDEFS)
620     //BEGIN UPDATING PROGRESS BAR
621     //This gets the vardefs, writes to a buffer, then I write to vardefschema.html
622     global $cacheDir;
623     global $beanList;
624     global $beanFiles;
625     global $dictionary;
626     global $sugar_version;
627     global $sugar_db_version;
628     global $sugar_flavor;
629
630     ob_start();
631     foreach ( $beanList as $beanz ) {
632       // echo "Module: ".$beanz."<br>";
633
634         $path_parts = pathinfo( $beanFiles[ $beanz ] );
635         $vardefFileName = $path_parts[ 'dirname' ]."/vardefs.php";
636           if( file_exists( $vardefFileName )) {
637             // echo "<br>".$vardefFileName."<br>";
638             include_once( $vardefFileName );
639           }
640     }
641
642     echo "<html>";
643     echo "<BODY>";
644     echo "<H1>Schema listing based on vardefs</H1>";
645     echo "<P>Sugar version:  ".$sugar_version." / Sugar DB version:  ".$sugar_db_version." / Sugar flavor:  ".$sugar_flavor;
646     echo "</P>";
647
648     echo "<style> th { text-align: left; } </style>";
649
650     $tables = array();
651     foreach($dictionary as $vardef) {
652         $tables[] = $vardef['table'];
653         $fields[$vardef['table']] = $vardef['fields'];
654         $comments[$vardef['table']] = $vardef['comment'];
655     }
656
657     asort($tables);
658
659     foreach($tables as $t) {
660         $name = $t;
661         if ( $name == "does_not_exist" )
662           continue;
663         $comment = $comments[$t];
664         echo "<h2>Table: $t</h2>
665                 <p><i>{$comment}</i></p>";
666         echo "<table border=\"0\" cellpadding=\"3\" class=\"tabDetailView\">";
667         echo '<TR BGCOLOR="#DFDFDF">
668                 <TD NOWRAP ALIGN=left class=\"tabDetailViewDL\">Column</TD>
669                 <TD NOWRAP class=\"tabDetailViewDL\">Type</TD>
670                 <TD NOWRAP class=\"tabDetailViewDL\">Length</TD>
671                 <TD NOWRAP class=\"tabDetailViewDL\">Required</TD>
672                 <TD NOWRAP class=\"tabDetailViewDL\">Comment</TD>
673         </TR>';
674
675         ksort( $fields[ $t ] );
676
677         foreach($fields[$t] as $k => $v) {
678           // we only care about physical tables ('source' can be 'non-db' or 'nondb' or 'function' )
679           if ( isset( $v[ 'source' ] ))
680             continue;
681           $columnname = $v[ 'name' ];
682           $columntype = $v[ 'type' ];
683           $columndbtype = $v[ 'dbType' ];
684           $columnlen = $v[ 'len' ];
685           $columncomment = $v[ 'comment' ];
686           $columnrequired = $v[ 'required' ];
687
688           if ( empty( $columnlen ) ) $columnlen = '<i>n/a</i>';
689           if ( empty( $columncomment ) ) $columncomment = '<i>(none)</i>';
690           if ( !empty( $columndbtype ) ) $columntype = $columndbtype;
691           if ( empty( $columnrequired ) || ( $columnrequired == false ))
692             $columndisplayrequired = 'no';
693           else
694             $columndisplayrequired = 'yes';
695
696           echo '<TR BGCOLOR="#FFFFFF" ALIGN=left>
697                         <TD ALIGN=left class=\"tabDetailViewDF\">'.$columnname.'</TD>
698                         <TD NOWRAP class=\"tabDetailViewDF\">'.$columntype.'</TD>
699                         <TD NOWRAP class=\"tabDetailViewDF\">'.$columnlen.'</TD>
700                         <TD NOWRAP class=\"tabDetailViewDF"\">'.$columndisplayrequired.'</TD>
701                         <TD WRAP class=\"tabDetailViewDF\">'.$columncomment.'</TD></TR>';
702         }
703
704         echo "</table></p>";
705     }
706
707     echo "</body></html>";
708
709     $vardefFormattedOutput = ob_get_contents();
710     ob_clean();
711
712     $handle = sugar_fopen($cacheDir."vardefschema.html", "w");
713     if(fwrite($handle, $vardefFormattedOutput) === FALSE){
714       echo "Cannot write to file ".$cacheDir."vardefschema.html<br>";
715     }
716     fclose($handle);
717     sodUpdateProgressBar(VARDEFS_WEIGHT);
718     //END UPDATING PROGRESS BAR
719 }
720
721 function finishDiag(){
722         //BEGIN ZIP ALL FILES AND EXTRACT IN CACHE ROOT
723         global $cacheDir;
724         global $curdatetime;
725         global $sod_guid;
726         global $mod_strings;
727
728         chdir($cacheDir);
729         zip_dir(".", "../diagnostic".$curdatetime.".zip");
730         //END ZIP ALL FILES AND EXTRACT IN CACHE ROOT
731         chdir(RETURN_FROM_DIAG_DIR);
732
733         deleteDir($cacheDir);
734
735         print "<a href=\"{$GLOBALS['sugar_config']['cache_dir']}diagnostic/".$sod_guid."/diagnostic".$curdatetime.".zip\">".$mod_strings['LBL_DIAGNOSTIC_DOWNLOADLINK']."</a><BR>";
736
737         print "<a href=\"index.php?module=Administration&action=DiagnosticDelete&file=diagnostic".$curdatetime."&guid=".$sod_guid."\">".$mod_strings['LBL_DIAGNOSTIC_DELETELINK']."</a><br>";
738
739 }
740
741 //BEGIN check for what we are executing
742 $doconfigphp = ((empty($_POST['configphp']) || $_POST['configphp'] == 'off') ? false : true);
743 $docustom_dir = ((empty($_POST['custom_dir']) || $_POST['custom_dir'] == 'off') ? false : true);
744 $dophpinfo = ((empty($_POST['phpinfo']) || $_POST['phpinfo'] == 'off') ? false : true);
745 $domysql_dumps = ((empty($_POST['mysql_dumps']) || $_POST['mysql_dumps'] == 'off') ? false : true);
746 $domysql_schema = ((empty($_POST['mysql_schema']) || $_POST['mysql_schema'] == 'off') ? false : true);
747 $domysql_info = ((empty($_POST['mysql_info']) || $_POST['mysql_info'] == 'off') ? false : true);
748 $domd5 = ((empty($_POST['md5']) || $_POST['md5'] == 'off') ? false : true);
749 $domd5filesmd5 = ((empty($_POST['md5filesmd5']) || $_POST['md5filesmd5'] == 'off') ? false : true);
750 $domd5calculated = ((empty($_POST['md5calculated']) || $_POST['md5calculated'] == 'off') ? false : true);
751 $dobeanlistbeanfiles = ((empty($_POST['beanlistbeanfiles']) || $_POST['beanlistbeanfiles'] == 'off') ? false : true);
752 $dosugarlog = ((empty($_POST['sugarlog']) || $_POST['sugarlog'] == 'off') ? false : true);
753 $dovardefs = ((empty($_POST['vardefs']) || $_POST['vardefs'] == 'off') ? false : true);
754 //END check for what we are executing
755
756
757 //BEGIN items to calculate progress bar
758 $totalitems = 0;
759 $totalweight = 0;
760 if($doconfigphp) {$totalweight += CONFIG_WEIGHT; $totalitems++;}
761 if($docustom_dir) {$totalweight += CUSTOM_DIR_WEIGHT; $totalitems++;}
762 if($dophpinfo) {$totalweight += PHPINFO_WEIGHT; $totalitems++;}
763 if($domysql_dumps) {$totalweight += MYSQL_DUMPS_WEIGHT; $totalitems++;}
764 if($domysql_schema) {$totalweight += MYSQL_SCHEMA_WEIGHT; $totalitems++;}
765 if($domysql_info) {$totalweight += MYSQL_INFO_WEIGHT; $totalitems++;}
766 if($domd5) {$totalweight += MD5_WEIGHT; $totalitems++;}
767 if($dobeanlistbeanfiles) {$totalweight += BEANLISTBEANFILES_WEIGHT; $totalitems++;}
768 if($dosugarlog) {$totalweight += SUGARLOG_WEIGHT; $totalitems++;}
769 if($dovardefs) {$totalweight += VARDEFS_WEIGHT; $totalitems++;}
770 //END items to calculate progress bar
771
772 //prepare initial steps
773 prepareDiag();
774
775
776 if($doconfigphp)
777 {
778   echo $mod_strings['LBL_DIAGNOSTIC_GETCONFPHP']."<BR>";
779   executeconfigphp();
780   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
781 }
782 if($docustom_dir)
783 {
784   echo $mod_strings['LBL_DIAGNOSTIC_GETCUSTDIR']."<BR>";
785   executecustom_dir();
786   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
787 }
788 if($dophpinfo)
789 {
790   echo $mod_strings['LBL_DIAGNOSTIC_GETPHPINFO']."<BR>";
791   executephpinfo();
792   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
793 }
794 if($domysql_info || $domysql_dumps || $domysql_schema)
795 {
796   echo $mod_strings['LBL_DIAGNOSTIC_GETTING'].
797                  ($domysql_info ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLINFO'] : " ").
798                  ($domysql_dumps ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLTD'] : " ").
799                  ($domysql_schema ? "... ".$mod_strings['LBL_DIAGNOSTIC_GETMYSQLTS'] : "...").
800                  "<BR>";
801   executemysql($domysql_info, $domysql_dumps, $domysql_schema);
802   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
803 }
804 if($domd5)
805 {
806   echo $mod_strings['LBL_DIAGNOSTIC_GETMD5INFO']."<BR>";
807   executemd5($domd5filesmd5, $domd5calculated);
808   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
809 }
810 if($dobeanlistbeanfiles)
811 {
812   echo $mod_strings['LBL_DIAGNOSTIC_GETBEANFILES']."<BR>";
813   executebeanlistbeanfiles();
814   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
815 }
816 if($dosugarlog)
817 {
818   echo $mod_strings['LBL_DIAGNOSTIC_GETSUGARLOG']."<BR>";
819   executesugarlog();
820   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
821 }
822 if($dovardefs)
823 {
824   echo $mod_strings['LBL_DIAGNOSTIC_VARDEFS']."<BR>";
825   executevardefs();
826   echo $mod_strings['LBL_DIAGNOSTIC_DONE']."<BR><BR>";
827 }
828
829 //finish up the last steps
830 finishDiag();
831
832 ?>