Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
6.1 KiB

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