PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/Log/daemon.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 247 lines | 104 code | 29 blank | 114 comment | 19 complexity | 172bb4aea103862b9bdc658f92848e8f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: daemon.php 1336 2008-03-31 17:06:23Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2007 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. // $Id: daemon.php 1336 2008-03-31 17:06:23Z soeren_nb $
  19. /**
  20. * The vmLog_daemon class is a concrete implementation of the Log::
  21. * abstract class which sends messages to syslog daemon on UNIX-like machines.
  22. * This class uses the syslog protocol: http://www.ietf.org/rfc/rfc3164.txt
  23. *
  24. * @author Bart van der Schans <schans@dds.nl>
  25. * @version $ Revision: 1.2 $
  26. * @package Log
  27. */
  28. class vmLog_daemon extends vmLog
  29. {
  30. /**
  31. * Integer holding the log facility to use.
  32. * @var string
  33. */
  34. var $_name = vmLog_daemon;
  35. /**
  36. * Var holding the resource pointer to the socket
  37. * @var resource
  38. */
  39. var $_socket;
  40. /**
  41. * The ip address or servername
  42. * @see http://www.php.net/manual/en/transports.php
  43. * @var string
  44. */
  45. var $_ip = '127.0.0.1';
  46. /**
  47. * Protocol to use (tcp, udp, etc.)
  48. * @see http://www.php.net/manual/en/transports.php
  49. * @var string
  50. */
  51. var $_proto = 'udp';
  52. /**
  53. * Port to connect to
  54. * @var int
  55. */
  56. var $_port = 514;
  57. /**
  58. * Maximum message length in bytes
  59. * @var int
  60. */
  61. var $_maxsize = 4096;
  62. /**
  63. * Socket timeout in seconds
  64. * @var int
  65. */
  66. var $_timeout = 1;
  67. /**
  68. * Constructs a new syslog object.
  69. *
  70. * @param string $name The syslog facility.
  71. * @param string $ident The identity string.
  72. * @param array $conf The configuration array.
  73. * @param int $maxLevel Maximum level at which to log.
  74. * @access public
  75. */
  76. function vmLog_daemon($name, $ident = '', $conf = array(),
  77. $level = PEAR_LOG_DEBUG)
  78. {
  79. /* Ensure we have a valid integer value for $name. */
  80. if (empty($name) || !is_int($name)) {
  81. $name = LOG_SYSLOG;
  82. }
  83. $this->_id = md5(microtime());
  84. $this->_name = $name;
  85. $this->_ident = $ident;
  86. $this->_mask = vmLog::UPTO($level);
  87. if (isset($conf['ip'])) {
  88. $this->_ip = $conf['ip'];
  89. }
  90. if (isset($conf['proto'])) {
  91. $this->_proto = $conf['proto'];
  92. }
  93. if (isset($conf['port'])) {
  94. $this->_port = $conf['port'];
  95. }
  96. if (isset($conf['maxsize'])) {
  97. $this->_maxsize = $conf['maxsize'];
  98. }
  99. if (isset($conf['timeout'])) {
  100. $this->_timeout = $conf['timeout'];
  101. }
  102. $this->_proto = $this->_proto . '://';
  103. register_shutdown_function(array(&$this, '_vmLog_daemon'));
  104. }
  105. /**
  106. * Destructor.
  107. *
  108. * @access private
  109. */
  110. function _vmLog_daemon()
  111. {
  112. $this->close();
  113. }
  114. /**
  115. * Opens a connection to the system logger, if it has not already
  116. * been opened. This is implicitly called by log(), if necessary.
  117. * @access public
  118. */
  119. function open()
  120. {
  121. if (!$this->_opened) {
  122. $this->_opened = (bool)($this->_socket = @fsockopen(
  123. $this->_proto . $this->_ip,
  124. $this->_port,
  125. $errno,
  126. $errstr,
  127. $this->_timeout));
  128. }
  129. return $this->_opened;
  130. }
  131. /**
  132. * Closes the connection to the system logger, if it is open.
  133. * @access public
  134. */
  135. function close()
  136. {
  137. if ($this->_opened) {
  138. $this->_opened = false;
  139. return fclose($this->_socket);
  140. }
  141. return true;
  142. }
  143. /**
  144. * Sends $message to the currently open syslog connection. Calls
  145. * open() if necessary. Also passes the message along to any Log_observer
  146. * instances that are observing this Log.
  147. *
  148. * @param string $message The textual message to be logged.
  149. * @param int $priority (optional) The priority of the message. Valid
  150. * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT,
  151. * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO,
  152. * and LOG_DEBUG. The default is LOG_INFO.
  153. * @access public
  154. */
  155. function log($message, $priority = null)
  156. {
  157. /* If a priority hasn't been specified, use the default value. */
  158. if ($priority === null) {
  159. $priority = $this->_priority;
  160. }
  161. /* Abort early if the priority is above the maximum logging level. */
  162. if (!$this->_isMasked($priority)) {
  163. return false;
  164. }
  165. /* If the connection isn't open and can't be opened, return failure. */
  166. if (!$this->_opened && !$this->open()) {
  167. return false;
  168. }
  169. /* Extract the string representation of the message. */
  170. $message = $this->_extractMessage($message);
  171. /* Set the facility level. */
  172. $facility_level = intval($this->_name) +
  173. intval($this->_toSyslog($priority));
  174. /* Prepend ident info. */
  175. if (!empty($this->_ident)) {
  176. $message = $this->_ident . ' ' . $message;
  177. }
  178. /* Check for message length. */
  179. if (strlen($message) > $this->_maxsize) {
  180. $message = substr($message, 0, ($this->_maxsize) - 10) . ' [...]';
  181. }
  182. /* Write to socket. */
  183. fwrite($this->_socket, '<' . $facility_level . '>' . $message . "\n");
  184. $this->_announce(array('priority' => $priority, 'message' => $message));
  185. }
  186. /**
  187. * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  188. *
  189. * This function exists because, under Windows, not all of the LOG_*
  190. * constants have unique values. Instead, the PEAR_LOG_* were introduced
  191. * for global use, with the conversion to the LOG_* constants kept local to
  192. * to the syslog driver.
  193. *
  194. * @param int $priority PEAR_LOG_* value to convert to LOG_* value.
  195. *
  196. * @return The LOG_* representation of $priority.
  197. *
  198. * @access private
  199. */
  200. function _toSyslog($priority)
  201. {
  202. static $priorities = array(
  203. PEAR_LOG_EMERG => LOG_EMERG,
  204. PEAR_LOG_ALERT => LOG_ALERT,
  205. PEAR_LOG_CRIT => LOG_CRIT,
  206. PEAR_LOG_ERR => LOG_ERR,
  207. PEAR_LOG_WARNING => LOG_WARNING,
  208. PEAR_LOG_NOTICE => LOG_NOTICE,
  209. PEAR_LOG_INFO => LOG_INFO,
  210. PEAR_LOG_DEBUG => LOG_DEBUG
  211. );
  212. /* If we're passed an unknown priority, default to LOG_INFO. */
  213. if (!is_int($priority) || !in_array($priority, $priorities)) {
  214. return LOG_INFO;
  215. }
  216. return $priorities[$priority];
  217. }
  218. }