PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/external_lib/Log/mail.php

https://github.com/OwlManAtt/kittokittokitto
PHP | 257 lines | 96 code | 30 blank | 131 comment | 17 complexity | 9ecfe8706ca192d48d1eef9a78ab6a41 MD5 | raw file
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/mail.php,v 1.26 2006/12/18 00:53:02 jon Exp $
  4. *
  5. * @version $Revision: 1.26 $
  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 recipients' email addresses. Multiple addresses
  31. * should be separated with commas.
  32. * @var string
  33. * @access private
  34. */
  35. var $_recipients = '';
  36. /**
  37. * String holding the sender's email address.
  38. * @var string
  39. * @access private
  40. */
  41. var $_from = '';
  42. /**
  43. * String holding the email's subject.
  44. * @var string
  45. * @access private
  46. */
  47. var $_subject = '[Log_mail] Log message';
  48. /**
  49. * String holding an optional preamble for the log messages.
  50. * @var string
  51. * @access private
  52. */
  53. var $_preamble = '';
  54. /**
  55. * String containing the format of a log line.
  56. * @var string
  57. * @access private
  58. */
  59. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  60. /**
  61. * String containing the timestamp format. It will be passed directly to
  62. * strftime(). Note that the timestamp string will generated using the
  63. * current locale.
  64. * @var string
  65. * @access private
  66. */
  67. var $_timeFormat = '%b %d %H:%M:%S';
  68. /**
  69. * String holding the mail message body.
  70. * @var string
  71. * @access private
  72. */
  73. var $_message = '';
  74. /**
  75. * Flag used to indicated that log lines have been written to the message
  76. * body and the message should be sent on close().
  77. * @var boolean
  78. * @access private
  79. */
  80. var $_shouldSend = false;
  81. /**
  82. * Constructs a new Log_mail object.
  83. *
  84. * Here is how you can customize the mail driver with the conf[] hash :
  85. * $conf['from'] : the mail's "From" header line,
  86. * $conf['subject'] : the mail's "Subject" line.
  87. *
  88. * @param string $name The message's recipients.
  89. * @param string $ident The identity string.
  90. * @param array $conf The configuration array.
  91. * @param int $level Log messages up to and including this level.
  92. * @access public
  93. */
  94. function Log_mail($name, $ident = '', $conf = array(),
  95. $level = PEAR_LOG_DEBUG)
  96. {
  97. $this->_id = md5(microtime());
  98. $this->_recipients = $name;
  99. $this->_ident = $ident;
  100. $this->_mask = Log::UPTO($level);
  101. if (!empty($conf['from'])) {
  102. $this->_from = $conf['from'];
  103. } else {
  104. $this->_from = ini_get('sendmail_from');
  105. }
  106. if (!empty($conf['subject'])) {
  107. $this->_subject = $conf['subject'];
  108. }
  109. if (!empty($conf['preamble'])) {
  110. $this->_preamble = $conf['preamble'];
  111. }
  112. if (!empty($conf['lineFormat'])) {
  113. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  114. array_values($this->_formatMap),
  115. $conf['lineFormat']);
  116. }
  117. if (!empty($conf['timeFormat'])) {
  118. $this->_timeFormat = $conf['timeFormat'];
  119. }
  120. /* register the destructor */
  121. register_shutdown_function(array(&$this, '_Log_mail'));
  122. }
  123. /**
  124. * Destructor. Calls close().
  125. *
  126. * @access private
  127. */
  128. function _Log_mail()
  129. {
  130. $this->close();
  131. }
  132. /**
  133. * Starts a new mail message.
  134. * This is implicitly called by log(), if necessary.
  135. *
  136. * @access public
  137. */
  138. function open()
  139. {
  140. if (!$this->_opened) {
  141. if (!empty($this->_preamble)) {
  142. $this->_message = $this->_preamble . "\r\n\r\n";
  143. }
  144. $this->_opened = true;
  145. $_shouldSend = false;
  146. }
  147. return $this->_opened;
  148. }
  149. /**
  150. * Closes the message, if it is open, and sends the mail.
  151. * This is implicitly called by the destructor, if necessary.
  152. *
  153. * @access public
  154. */
  155. function close()
  156. {
  157. if ($this->_opened) {
  158. if ($this->_shouldSend && !empty($this->_message)) {
  159. $headers = "From: $this->_from\r\n";
  160. $headers .= "User-Agent: Log_mail";
  161. if (mail($this->_recipients, $this->_subject, $this->_message,
  162. $headers) == false) {
  163. error_log("Log_mail: Failure executing mail()", 0);
  164. return false;
  165. }
  166. /* Clear the message string now that the email has been sent. */
  167. $this->_message = '';
  168. $this->_shouldSend = false;
  169. }
  170. $this->_opened = false;
  171. }
  172. return ($this->_opened === false);
  173. }
  174. /**
  175. * Flushes the log output by forcing the email message to be sent now.
  176. * Events that are logged after flush() is called will be appended to a
  177. * new email message.
  178. *
  179. * @access public
  180. * @since Log 1.8.2
  181. */
  182. function flush()
  183. {
  184. /*
  185. * It's sufficient to simply call close() to flush the output.
  186. * The next call to log() will cause the handler to be reopened.
  187. */
  188. return $this->close();
  189. }
  190. /**
  191. * Writes $message to the currently open mail message.
  192. * Calls open(), if necessary.
  193. *
  194. * @param mixed $message String or object containing the message to log.
  195. * @param string $priority The priority of the message. Valid
  196. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  197. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  198. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  199. * @return boolean True on success or false on failure.
  200. * @access public
  201. */
  202. function log($message, $priority = null)
  203. {
  204. /* If a priority hasn't been specified, use the default value. */
  205. if ($priority === null) {
  206. $priority = $this->_priority;
  207. }
  208. /* Abort early if the priority is above the maximum logging level. */
  209. if (!$this->_isMasked($priority)) {
  210. return false;
  211. }
  212. /* If the message isn't open and can't be opened, return failure. */
  213. if (!$this->_opened && !$this->open()) {
  214. return false;
  215. }
  216. /* Extract the string representation of the message. */
  217. $message = $this->_extractMessage($message);
  218. /* Append the string containing the complete log line. */
  219. $this->_message .= $this->_format($this->_lineFormat,
  220. strftime($this->_timeFormat),
  221. $priority, $message) . "\r\n";
  222. $this->_shouldSend = true;
  223. /* Notify observers about this log message. */
  224. $this->_announce(array('priority' => $priority, 'message' => $message));
  225. return true;
  226. }
  227. }