From d8d28d63504a926007f880ae2973874f7f8af770 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Wed, 29 Aug 2012 17:43:20 +0200
Subject: [PATCH] New: Add ChromePHP output into syslog module.

---
 ChangeLog                         |  1 +
 htdocs/admin/syslog.php           | 42 ++++++++++++++++++++++++++++---
 htdocs/core/lib/functions.lib.php | 26 +++++++++++++++++--
 3 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5c31ad7e3b5..aec1ba17023 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -62,6 +62,7 @@ For developers:
 - New: Basic implementation of hooks and triggers for a lot (most) of core modules: 
   action/calendar, trips and expenses, dons, vat payment, contact/society, contract, product lines, 
   expedition, order supplier and order invoice (lines included), intervention card, project, tasks.
+- New: Add ChromePHP output into syslog module.
 
 For translators:
 - New: Update language files (de, tr, pt, ca, es, en, fr).
diff --git a/htdocs/admin/syslog.php b/htdocs/admin/syslog.php
index 312a99649c2..d1abf15f345 100644
--- a/htdocs/admin/syslog.php
+++ b/htdocs/admin/syslog.php
@@ -35,7 +35,8 @@ $error=0; $mesg='';
 $action = GETPOST("action");
 $syslog_file_on=(defined('SYSLOG_FILE_ON') && constant('SYSLOG_FILE_ON'))?1:0;
 $syslog_syslog_on=(defined('SYSLOG_SYSLOG_ON') && constant('SYSLOG_SYSLOG_ON'))?1:0;
-$syslog_firephp_on=(defined('SYSLOG_FIREPHP_ON') && constant('SYSLOG_FIREPHP_ON'))?1:0;
+$syslog_firephp_on=(defined('SYSLOG_FIREPHP_ON') && constant('SYSLOG_FIREPHP_ON'))?1:0;
+$syslog_chromephp_on=(defined('SYSLOG_CHROMEPHP_ON') && constant('SYSLOG_CHROMEPHP_ON'))?1:0;
 
 
 /*
@@ -54,6 +55,7 @@ if ($action == 'set')
     $syslog_file_on=0;
     $syslog_syslog_on=0;
     $syslog_firephp_on=0;
+    $syslog_chromephp_on=0;
 
 	if (! $error && GETPOST("filename"))
 	{
@@ -99,13 +101,19 @@ if ($action == 'set')
 		}
 	}
 
-	if (! $error && isset($_POST['SYSLOG_FIREPHP_ON']))    // If firephp no available, post is not present
+	if (! $error && isset($_POST['SYSLOG_FIREPHP_ON']))    // If firephp no available, post is not present. We must keep isset here.
 	{
         $syslog_firephp_on=GETPOST('SYSLOG_FIREPHP_ON');
 		if (! $error) $res = dolibarr_set_const($db,"SYSLOG_FIREPHP_ON",$syslog_firephp_on,'chaine',0,'',0);
     }
 
-	if (! $error)
+	if (! $error && isset($_POST['SYSLOG_CHROMEPHP_ON']))  // If chromephp no available, post is not present. We must keep isset here.
+	{
+        $syslog_chromephp_on=GETPOST('SYSLOG_CHROMEPHP_ON');
+		if (! $error) $res = dolibarr_set_const($db,"SYSLOG_CHROMEPHP_ON",$syslog_chromephp_on,'chaine',0,'',0);
+    }
+
+    if (! $error)
 	{
 		$db->commit();
 		$mesg = "<font class=\"ok\">".$langs->trans("SetupSaved")."</font>";
@@ -176,6 +184,7 @@ print '<td align="right" colspan="2"><input type="submit" class="button" '.$opti
 print "</tr>\n";
 $var=true;
 
+// Output to file
 $var=!$var;
 print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_FILE_ON" '.$option.' value="1" '.($syslog_file_on?' checked="checked"':'').'> '.$langs->trans("SyslogSimpleFile").'</td>';
 print '<td nowrap="nowrap">'.$langs->trans("SyslogFilename").': <input type="text" class="flat" name="filename" '.$option.' size="60" value="'.$defaultsyslogfile.'">';
@@ -183,6 +192,7 @@ print '</td>';
 print "<td align=\"left\">".$form->textwithpicto('',$langs->trans("YouCanUseDOL_DATA_ROOT"));
 print '</td></tr>';
 
+// Output to syslog
 $var=!$var;
 print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_SYSLOG_ON" '.$option.' value="1" '.($syslog_syslog_on?' checked="checked"':'').'> '.$langs->trans("SyslogSyslog").'</td>';
 print '<td nowrap="nowrap">'.$langs->trans("SyslogFacility").': <input type="text" class="flat" name="facility" '.$option.' value="'.$defaultsyslogfacility.'">';
@@ -190,6 +200,7 @@ print '</td>';
 print "<td align=\"left\">".$form->textwithpicto('', $langs->trans('OnlyWindowsLOG_USER'));
 print '</td></tr>';
 
+// Output to Firebug
 try
 {
     set_include_path('/usr/share/php/');
@@ -214,6 +225,31 @@ catch(Exception $e)
     print '<!-- FirePHP no available into PHP -->'."\n";
 }
 
+// Output to Chrome
+try
+{
+	set_include_path('/usr/share/php/');
+	$res=@include_once 'ChromePhp.php';
+	restore_include_path();
+	if ($res)
+	{
+		$var=!$var;
+		print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_CHROMEPHP_ON" '.$option.' value="1" ';
+		if (! class_exists('ChromePHP')) print ' disabled="disabled"';
+		else print ($syslog_chromephp_on?' checked="checked"':"");
+		print '> '.$langs->trans("ChromePHP").'</td>';
+		print '<td nowrap="nowrap">';
+		print '</td>';
+		print "<td align=\"left\">".$form->textwithpicto('','ChromePHP must be installed onto PHP path and ChromePHP plugin for Chrome must also be installed');
+		print '</td></tr>';
+	}
+}
+catch(Exception $e)
+{
+	// Do nothing
+	print '<!-- ChromePHP no available into PHP -->'."\n";
+}
+
 print "</table>\n";
 print "</form>\n";
 
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 790d8580b93..cccc2429817 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -570,7 +570,7 @@ function dol_syslog($message, $level=LOG_INFO)
 				set_include_path('/usr/share/php/');
 				include_once 'FirePHPCore/FirePHP.class.php';
 				set_include_path($oldinclude);
-				ob_start();
+				ob_start();	// To be sure headers are not flushed until all page is completely processed
 				$firephp = FirePHP::getInstance(true);
 				if ($level == LOG_ERR) $firephp->error($message);
 				elseif ($level == LOG_WARNING) $firephp->warn($message);
@@ -579,7 +579,29 @@ function dol_syslog($message, $level=LOG_INFO)
 			}
 			catch(Exception $e)
 			{
-				// Do not use dol_syslog to avoid infinite loop
+				// Do not use dol_syslog here to avoid infinite loop
+			}
+		}
+			// Check if log is to syslog (SYSLOG_FIREPHP_ON defined)
+		if (defined("SYSLOG_CHROMEPHP_ON") && constant("SYSLOG_CHROMEPHP_ON") && ! empty($_SERVER["SERVER_NAME"]))     //! empty($_SERVER["SERVER_NAME"]) to be sure to enable this in Web mode only
+		{
+			try
+			{
+				// Warning ChromePHP must be into PHP include path. It is not possible to use into require_once() a constant from
+				// database or config file because we must be able to log data before database or config file read.
+				$oldinclude=get_include_path();
+				set_include_path('/usr/share/php/');
+				include_once 'ChromePhp.php';
+				set_include_path($oldinclude);
+				ob_start();	// To be sure headers are not flushed until all page is completely processed
+				if ($level == LOG_ERR) ChromePhp::error($message);
+				elseif ($level == LOG_WARNING) ChromePhp::warn($message);
+				elseif ($level == LOG_INFO) ChromePhp::log($message);
+				else ChromePhp::log($message);
+			}
+			catch(Exception $e)
+			{
+				// Do not use dol_syslog here to avoid infinite loop
 			}
 		}
 	}
-- 
GitLab