mod_syslog_firephp.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* Copyright (C) 2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
  19. /**
  20. * Class to manage logging to a FirePHP
  21. */
  22. class mod_syslog_firephp extends LogHandler implements LogHandlerInterface
  23. {
  24. public $code = 'firephp';
  25. private static $firephp_include_path = '/includes/firephp/firephp-core/lib/';
  26. private static $firephp_class_path = 'FirePHPCore/FirePHP.class.php';
  27. /**
  28. * Return name of logger
  29. *
  30. * @return string Name of logger
  31. */
  32. public function getName()
  33. {
  34. return 'FirePHP';
  35. }
  36. /**
  37. * Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
  38. *
  39. * @return string
  40. */
  41. public function getVersion()
  42. {
  43. return 'dolibarr';
  44. }
  45. /**
  46. * Content of the info tooltip.
  47. *
  48. * @return false|string
  49. */
  50. public function getInfo()
  51. {
  52. global $langs;
  53. return ($this->isActive() == 1)?'':$langs->trans('ClassNotFoundIntoPathWarning', self::$firephp_class_path);
  54. }
  55. /**
  56. * Is the module active ?
  57. *
  58. * @return int
  59. */
  60. public function isActive()
  61. {
  62. global $conf;
  63. try
  64. {
  65. if (empty($conf->global->SYSLOG_FIREPHP_INCLUDEPATH)) {
  66. $conf->global->SYSLOG_FIREPHP_INCLUDEPATH = DOL_DOCUMENT_ROOT . self::$firephp_include_path;
  67. }
  68. set_include_path($conf->global->SYSLOG_FIREPHP_INCLUDEPATH);
  69. $res = @include_once self::$firephp_class_path;
  70. restore_include_path();
  71. if ($res) {
  72. return empty($conf->global->SYSLOG_DISABLE_LOGHANDLER_FIREPHP)?1:0; // Set SYSLOG_DISABLE_LOGHANDLER_FIREPHP to 1 to disable this loghandler
  73. } else {
  74. return 0;
  75. }
  76. }
  77. catch(Exception $e)
  78. {
  79. print '<!-- FirePHP not available into PHP -->'."\n";
  80. }
  81. return -1;
  82. }
  83. /**
  84. * Return array of configuration data
  85. *
  86. * @return array Return array of configuration data
  87. */
  88. public function configure()
  89. {
  90. global $langs;
  91. return array(
  92. array(
  93. 'name' => $langs->trans('IncludePath', 'SYSLOG_FIREPHP_INCLUDEPATH'),
  94. 'constant' => 'SYSLOG_FIREPHP_INCLUDEPATH',
  95. 'default' => DOL_DOCUMENT_ROOT . self::$firephp_include_path,
  96. 'attr' => 'size="60"',
  97. 'example' => '/usr/share/php, ' . DOL_DOCUMENT_ROOT . self::$firephp_include_path
  98. )
  99. );
  100. }
  101. /**
  102. * Return if configuration is valid
  103. *
  104. * @return array Array of errors. Empty array if ok.
  105. */
  106. public function checkConfiguration()
  107. {
  108. global $conf, $langs;
  109. $errors = array();
  110. if (!file_exists($conf->global->SYSLOG_FIREPHP_INCLUDEPATH . self::$firephp_class_path))
  111. {
  112. $conf->global->MAIN_SYSLOG_DISABLE_FIREPHP = 1; // avoid infinite loop
  113. if (is_object($langs)) // $langs may not be defined yet.
  114. {
  115. $errors[] = $langs->trans("ErrorFailedToOpenFile", self::$firephp_class_path);
  116. }
  117. else
  118. {
  119. $errors[] = "ErrorFailedToOpenFile " . self::$firephp_class_path;
  120. }
  121. }
  122. return $errors;
  123. }
  124. /**
  125. * Output log content
  126. *
  127. * @param array $content Content to log
  128. * @return null|false
  129. */
  130. public function export($content)
  131. {
  132. global $conf;
  133. if (! empty($conf->global->MAIN_SYSLOG_DISABLE_FIREPHP)) return; // Global option to disable output of this handler
  134. //We check the configuration to avoid showing PHP warnings
  135. if (count($this->checkConfiguration())) return false;
  136. try
  137. {
  138. // Warning FirePHPCore must be into PHP include path. It is not possible to use into require_once a constant from
  139. // database or config file because we must be able to log data before database or config file read.
  140. $oldinclude=get_include_path();
  141. set_include_path($conf->global->SYSLOG_FIREPHP_INCLUDEPATH);
  142. include_once self::$firephp_class_path;
  143. set_include_path($oldinclude);
  144. ob_start(); // To be sure headers are not flushed until all page is completely processed
  145. $firephp = FirePHP::getInstance(true);
  146. if ($content['level'] == LOG_ERR) $firephp->error($content['message']);
  147. elseif ($content['level'] == LOG_WARNING) $firephp->warn($content['message']);
  148. elseif ($content['level'] == LOG_INFO) $firephp->log($content['message']);
  149. else $firephp->log($content['message']);
  150. }
  151. catch (Exception $e)
  152. {
  153. // Do not use dol_syslog here to avoid infinite loop
  154. return false;
  155. }
  156. }
  157. }