PageRenderTime 44ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 237 lines | 84 code | 28 blank | 125 comment | 16 complexity | dda774b234b3a5288150596ee795436b 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: mail.php 1336 2008-03-31 17:06:23Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2008 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. /**
  19. * $Header$
  20. *
  21. * @version $ Revision: 1.23 $
  22. * @package Log
  23. */
  24. /**
  25. * The vmLog_mail class is a concrete implementation of the Log:: abstract class
  26. * which sends log messages to a mailbox.
  27. * The mail is actually sent when you close() the logger, or when the destructor
  28. * is called (when the script is terminated).
  29. *
  30. * PLEASE NOTE that you must create a vmLog_mail object using =&, like this :
  31. * $logger =& vmLog::factory("mail", "recipient@example.com", ...)
  32. *
  33. * This is a PEAR requirement for destructors to work properly.
  34. * See http://pear.php.net/manual/en/class.pear.php
  35. *
  36. * @author Ronnie Garcia <ronnie@mk2.net>
  37. * @author Jon Parise <jon@php.net>
  38. * @since Log 1.3
  39. * @package Log
  40. *
  41. * @example mail.php Using the mail handler.
  42. */
  43. class vmLog_mail extends vmLog
  44. {
  45. /**
  46. * String holding the recipient's email address.
  47. * @var string
  48. * @access private
  49. */
  50. var $_recipient = '';
  51. /**
  52. * String holding the sender's email address.
  53. * @var string
  54. * @access private
  55. */
  56. var $_from = '';
  57. /**
  58. * String holding the email's subject.
  59. * @var string
  60. * @access private
  61. */
  62. var $_subject = '[vmLog_mail] Log message';
  63. /**
  64. * String holding an optional preamble for the log messages.
  65. * @var string
  66. * @access private
  67. */
  68. var $_preamble = '';
  69. /**
  70. * String holding the mail message body.
  71. * @var string
  72. * @access private
  73. */
  74. var $_message = '';
  75. /**
  76. * Constructs a new vmLog_mail object.
  77. *
  78. * Here is how you can customize the mail driver with the conf[] hash :
  79. * $conf['from'] : the mail's "From" header line,
  80. * $conf['subject'] : the mail's "Subject" line.
  81. *
  82. * @param string $name The filename of the logfile.
  83. * @param string $ident The identity string.
  84. * @param array $conf The configuration array.
  85. * @param int $level Log messages up to and including this level.
  86. * @access public
  87. */
  88. function vmLog_mail($name, $ident = '', $conf = array(),
  89. $level = PEAR_LOG_DEBUG)
  90. {
  91. $this->_id = md5(microtime());
  92. $this->_recipient = $name;
  93. $this->_ident = $ident;
  94. $this->_mask = vmLog::UPTO($level);
  95. if (!empty($conf['from'])) {
  96. $this->_from = $conf['from'];
  97. } else {
  98. $this->_from = ini_get('sendmail_from');
  99. }
  100. if (!empty($conf['subject'])) {
  101. $this->_subject = $conf['subject'];
  102. }
  103. if (!empty($conf['preamble'])) {
  104. $this->_preamble = $conf['preamble'];
  105. }
  106. /* register the destructor */
  107. register_shutdown_function(array(&$this, '_vmLog_mail'));
  108. }
  109. /**
  110. * Destructor. Calls close().
  111. *
  112. * @access private
  113. */
  114. function _vmLog_mail()
  115. {
  116. $this->close();
  117. }
  118. /**
  119. * Starts a new mail message.
  120. * This is implicitly called by log(), if necessary.
  121. *
  122. * @access public
  123. */
  124. function open()
  125. {
  126. if (!$this->_opened) {
  127. if (!empty($this->_preamble)) {
  128. $this->_message = $this->_preamble . "\r\n\r\n";
  129. }
  130. $this->_opened = true;
  131. }
  132. return $this->_opened;
  133. }
  134. /**
  135. * Closes the message, if it is open, and sends the mail.
  136. * This is implicitly called by the destructor, if necessary.
  137. *
  138. * @access public
  139. */
  140. function close()
  141. {
  142. if ($this->_opened) {
  143. if (!empty($this->_message)) {
  144. $headers = "From: $this->_from\r\n";
  145. $headers .= "User-Agent: vmLog_mail";
  146. if (mail($this->_recipient, $this->_subject, $this->_message,
  147. $headers) == false) {
  148. error_log("vmLog_mail: Failure executing mail()", 0);
  149. return false;
  150. }
  151. /* Clear the message string now that the email has been sent. */
  152. $this->_message = '';
  153. }
  154. $this->_opened = false;
  155. }
  156. return ($this->_opened === false);
  157. }
  158. /**
  159. * Flushes the log output by forcing the email message to be sent now.
  160. * Events that are logged after flush() is called will be appended to a
  161. * new email message.
  162. *
  163. * @access public
  164. * @since Log 1.8.2
  165. */
  166. function flush()
  167. {
  168. /*
  169. * It's sufficient to simply call close() to flush the output.
  170. * The next call to log() will cause the handler to be reopened.
  171. */
  172. return $this->close();
  173. }
  174. /**
  175. * Writes $message to the currently open mail message.
  176. * Calls open(), if necessary.
  177. *
  178. * @param mixed $message String or object containing the message to log.
  179. * @param string $priority The priority of the message. Valid
  180. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  181. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  182. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  183. * @return boolean True on success or false on failure.
  184. * @access public
  185. */
  186. function log($message, $priority = null)
  187. {
  188. /* If a priority hasn't been specified, use the default value. */
  189. if ($priority === null) {
  190. $priority = $this->_priority;
  191. }
  192. /* Abort early if the priority is above the maximum logging level. */
  193. if (!$this->_isMasked($priority)) {
  194. return false;
  195. }
  196. /* If the message isn't open and can't be opened, return failure. */
  197. if (!$this->_opened && !$this->open()) {
  198. return false;
  199. }
  200. /* Extract the string representation of the message. */
  201. $message = $this->_extractMessage($message);
  202. $entry = sprintf("%s %s [%s] %s\r\n", strftime('%b %d %H:%M:%S'),
  203. $this->_ident, vmLog::priorityToString($priority),
  204. $message);
  205. $this->_message .= $entry;
  206. $this->_announce(array('priority' => $priority, 'message' => $message));
  207. return true;
  208. }
  209. }