mod_syslog_chromephp.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
  3. /**
  4. * Class to manage logging to ChromPHP
  5. */
  6. class mod_syslog_chromephp extends LogHandler implements LogHandlerInterface
  7. {
  8. var $code = 'chromephp';
  9. /**
  10. * Return name of logger
  11. *
  12. * @return string Name of logger
  13. */
  14. public function getName()
  15. {
  16. return 'ChromePHP';
  17. }
  18. /**
  19. * Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
  20. *
  21. * @return string
  22. */
  23. public function getVersion()
  24. {
  25. return 'dolibarr';
  26. }
  27. /**
  28. * Content of the info tooltip.
  29. *
  30. * @return string
  31. */
  32. public function getInfo()
  33. {
  34. return '';
  35. }
  36. /**
  37. * Return warning if something is wrong with logger
  38. *
  39. * @return string
  40. */
  41. public function getWarning()
  42. {
  43. global $langs;
  44. return ($this->isActive() == 1)?'':$langs->trans('ClassNotFoundIntoPathWarning','ChromePhp.class.php');
  45. }
  46. /**
  47. * Is the module active ?
  48. *
  49. * @return int -1 if not active, 0 if active but lib/path not found, 1 if OK
  50. */
  51. public function isActive()
  52. {
  53. global $conf;
  54. try
  55. {
  56. if (empty($conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH)) {
  57. $conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH = DOL_DOCUMENT_ROOT . '/includes/ccampbell/chromephp/';
  58. }
  59. set_include_path($conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH);
  60. $res = @include_once 'ChromePhp.php';
  61. if (! $res) $res=@include_once 'ChromePhp.class.php';
  62. restore_include_path();
  63. if ($res)
  64. {
  65. return empty($conf->global->SYSLOG_DISABLE_LOGHANDLER_CHROMEPHP)?1:0; // Set SYSLOG_DISABLE_LOGHANDLER_CHROMEPHP to 1 to disable this loghandler
  66. }
  67. else
  68. {
  69. return 0;
  70. }
  71. }
  72. catch(Exception $e)
  73. {
  74. print '<!-- ChromePHP not available into PHP -->'."\n";
  75. }
  76. return -1;
  77. }
  78. /**
  79. * Return array of configuration data
  80. *
  81. * @return array Return array of configuration data
  82. */
  83. public function configure()
  84. {
  85. global $langs;
  86. return array(
  87. array(
  88. 'name' => $langs->trans('IncludePath','SYSLOG_CHROMEPHP_INCLUDEPATH'),
  89. 'constant' => 'SYSLOG_CHROMEPHP_INCLUDEPATH',
  90. 'default' => DOL_DOCUMENT_ROOT . '/includes/ccampbell/chromephp/',
  91. 'attr' => 'size="60"',
  92. 'example' =>'/usr/share/php, '.DOL_DOCUMENT_ROOT . '/includes/ccampbell/chromephp/'
  93. )
  94. );
  95. }
  96. /**
  97. * Return if configuration is valid
  98. *
  99. * @return array Array of errors. Empty array if ok.
  100. */
  101. public function checkConfiguration()
  102. {
  103. global $langs,$conf;
  104. $errors = array();
  105. if (! file_exists($conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH.'/ChromePhp.php') && ! file_exists($conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH.'/ChromePhp.class.php'))
  106. {
  107. $conf->global->MAIN_SYSLOG_DISABLE_CHROMEPHP = 1; // avoid infinite loop
  108. if (is_object($langs)) // $langs may not be defined yet.
  109. {
  110. $errors[] = $langs->trans("ErrorFailedToOpenFile", 'ChromePhp.class.php or ChromePhp.php');
  111. }
  112. else
  113. {
  114. $errors[] = "ErrorFailedToOpenFile ChromePhp.class.php or ChromePhp.php";
  115. }
  116. }
  117. return $errors;
  118. }
  119. /**
  120. * Output log content. We also start output buffering at first log write.
  121. *
  122. * @param array $content Content to log
  123. * @return null|false
  124. */
  125. public function export($content)
  126. {
  127. global $conf;
  128. if (! empty($conf->global->MAIN_SYSLOG_DISABLE_CHROMEPHP)) return; // Global option to disable output of this handler
  129. //We check the configuration to avoid showing PHP warnings
  130. if (count($this->checkConfiguration()) > 0) return false;
  131. try
  132. {
  133. // Warning ChromePHP must be into PHP include path. It is not possible to use into require_once a constant from
  134. // database or config file because we must be able to log data before database or config file read.
  135. $oldinclude=get_include_path();
  136. set_include_path($conf->global->SYSLOG_CHROMEPHP_INCLUDEPATH);
  137. $res = @include_once 'ChromePhp.php';
  138. if (! $res) $res=@include_once 'ChromePhp.class.php';
  139. set_include_path($oldinclude);
  140. ob_start(); // To be sure headers are not flushed until all page is completely processed
  141. if ($content['level'] == LOG_ERR) ChromePhp::error($content['message']);
  142. elseif ($content['level'] == LOG_WARNING) ChromePhp::warn($content['message']);
  143. elseif ($content['level'] == LOG_INFO) ChromePhp::log($content['message']);
  144. else ChromePhp::log($content['message']);
  145. }
  146. catch (Exception $e)
  147. {
  148. // Do not use dol_syslog here to avoid infinite loop
  149. }
  150. }
  151. }