PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/Log/Writer/Mail.php

https://github.com/lanmediaservice/lms-tplib
PHP | 295 lines | 104 code | 30 blank | 161 comment | 11 complexity | fb768a3718044b13a05b1172fb824b3c MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. //*** require_once 'Zend/Log/Writer/Abstract.php';
  24. /** Zend_Log_Exception */
  25. //*** require_once 'Zend/Log/Exception.php';
  26. /** Zend_Log_Formatter_Simple*/
  27. //*** require_once 'Zend/Log/Formatter/Simple.php';
  28. /**
  29. * Class used for writing log messages to email via Zend_Mail.
  30. *
  31. * Allows for emailing log messages at and above a certain level via a
  32. * Zend_Mail object. Note that this class only sends the email upon
  33. * completion, so any log entries accumulated are sent in a single email.
  34. *
  35. * @category Zend
  36. * @package Zend_Log
  37. * @subpackage Writer
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $
  41. */
  42. class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
  43. {
  44. /**
  45. * Array of formatted events to include in message body.
  46. *
  47. * @var array
  48. */
  49. protected $_eventsToMail = array();
  50. /**
  51. * Array of formatted lines for use in an HTML email body; these events
  52. * are formatted with an optional formatter if the caller is using
  53. * Zend_Layout.
  54. *
  55. * @var array
  56. */
  57. protected $_layoutEventsToMail = array();
  58. /**
  59. * Zend_Mail instance to use
  60. *
  61. * @var Zend_Mail
  62. */
  63. protected $_mail;
  64. /**
  65. * Zend_Layout instance to use; optional.
  66. *
  67. * @var Zend_Layout
  68. */
  69. protected $_layout;
  70. /**
  71. * Optional formatter for use when rendering with Zend_Layout.
  72. *
  73. * @var Zend_Log_Formatter_Interface
  74. */
  75. protected $_layoutFormatter;
  76. /**
  77. * Array keeping track of the number of entries per priority level.
  78. *
  79. * @var array
  80. */
  81. protected $_numEntriesPerPriority = array();
  82. /**
  83. * Subject prepend text.
  84. *
  85. * Can only be used of the Zend_Mail object has not already had its
  86. * subject line set. Using this will cause the subject to have the entry
  87. * counts per-priority level appended to it.
  88. *
  89. * @var string|null
  90. */
  91. protected $_subjectPrependText;
  92. /**
  93. * Class constructor.
  94. *
  95. * Constructs the mail writer; requires a Zend_Mail instance, and takes an
  96. * optional Zend_Layout instance. If Zend_Layout is being used,
  97. * $this->_layout->events will be set for use in the layout template.
  98. *
  99. * @param Zend_Mail $mail Mail instance
  100. * @param Zend_Layout $layout Layout instance; optional
  101. * @return void
  102. */
  103. public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
  104. {
  105. $this->_mail = $mail;
  106. $this->_layout = $layout;
  107. $this->_formatter = new Zend_Log_Formatter_Simple();
  108. }
  109. /**
  110. * Places event line into array of lines to be used as message body.
  111. *
  112. * Handles the formatting of both plaintext entries, as well as those
  113. * rendered with Zend_Layout.
  114. *
  115. * @param array $event Event data
  116. * @return void
  117. */
  118. protected function _write($event)
  119. {
  120. // Track the number of entries per priority level.
  121. if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
  122. $this->_numEntriesPerPriority[$event['priorityName']] = 1;
  123. } else {
  124. $this->_numEntriesPerPriority[$event['priorityName']]++;
  125. }
  126. $formattedEvent = $this->_formatter->format($event);
  127. // All plaintext events are to use the standard formatter.
  128. $this->_eventsToMail[] = $formattedEvent;
  129. // If we have a Zend_Layout instance, use a specific formatter for the
  130. // layout if one exists. Otherwise, just use the event with its
  131. // default format.
  132. if ($this->_layout) {
  133. if ($this->_layoutFormatter) {
  134. $this->_layoutEventsToMail[] =
  135. $this->_layoutFormatter->format($event);
  136. } else {
  137. $this->_layoutEventsToMail[] = $formattedEvent;
  138. }
  139. }
  140. }
  141. /**
  142. * Gets instance of Zend_Log_Formatter_Instance used for formatting a
  143. * message using Zend_Layout, if applicable.
  144. *
  145. * @return Zend_Log_Formatter_Interface|null The formatter, or null.
  146. */
  147. public function getLayoutFormatter()
  148. {
  149. return $this->_layoutFormatter;
  150. }
  151. /**
  152. * Sets a specific formatter for use with Zend_Layout events.
  153. *
  154. * Allows use of a second formatter on lines that will be rendered with
  155. * Zend_Layout. In the event that Zend_Layout is not being used, this
  156. * formatter cannot be set, so an exception will be thrown.
  157. *
  158. * @param Zend_Log_Formatter_Interface $formatter
  159. * @return Zend_Log_Writer_Mail
  160. * @throws Zend_Log_Exception
  161. */
  162. public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
  163. {
  164. if (!$this->_layout) {
  165. throw new Zend_Log_Exception(
  166. 'cannot set formatter for layout; ' .
  167. 'a Zend_Layout instance is not in use');
  168. }
  169. $this->_layoutFormatter = $formatter;
  170. return $this;
  171. }
  172. /**
  173. * Allows caller to have the mail subject dynamically set to contain the
  174. * entry counts per-priority level.
  175. *
  176. * Sets the text for use in the subject, with entry counts per-priority
  177. * level appended to the end. Since a Zend_Mail subject can only be set
  178. * once, this method cannot be used if the Zend_Mail object already has a
  179. * subject set.
  180. *
  181. * @param string $subject Subject prepend text.
  182. * @return Zend_Log_Writer_Mail
  183. */
  184. public function setSubjectPrependText($subject)
  185. {
  186. if ($this->_mail->getSubject()) {
  187. throw new Zend_Log_Exception(
  188. 'subject already set on mail; ' .
  189. 'cannot set subject prepend text');
  190. }
  191. $this->_subjectPrependText = (string) $subject;
  192. return $this;
  193. }
  194. /**
  195. * Sends mail to recipient(s) if log entries are present. Note that both
  196. * plaintext and HTML portions of email are handled here.
  197. *
  198. * @return void
  199. */
  200. public function shutdown()
  201. {
  202. // If there are events to mail, use them as message body. Otherwise,
  203. // there is no mail to be sent.
  204. if (empty($this->_eventsToMail)) {
  205. return;
  206. }
  207. if ($this->_subjectPrependText !== null) {
  208. // Tack on the summary of entries per-priority to the subject
  209. // line and set it on the Zend_Mail object.
  210. $numEntries = $this->_getFormattedNumEntriesPerPriority();
  211. $this->_mail->setSubject(
  212. "{$this->_subjectPrependText} ({$numEntries})");
  213. }
  214. // Always provide events to mail as plaintext.
  215. $this->_mail->setBodyText(implode('', $this->_eventsToMail));
  216. // If a Zend_Layout instance is being used, set its "events"
  217. // value to the lines formatted for use with the layout.
  218. if ($this->_layout) {
  219. // Set the required "messages" value for the layout. Here we
  220. // are assuming that the layout is for use with HTML.
  221. $this->_layout->events =
  222. implode('', $this->_layoutEventsToMail);
  223. // If an exception occurs during rendering, convert it to a notice
  224. // so we can avoid an exception thrown without a stack frame.
  225. try {
  226. $this->_mail->setBodyHtml($this->_layout->render());
  227. } catch (Exception $e) {
  228. trigger_error(
  229. "exception occurred when rendering layout; " .
  230. "unable to set html body for message; " .
  231. "message = {$e->getMessage()}; " .
  232. "code = {$e->getCode()}; " .
  233. "exception class = " . get_class($e),
  234. E_USER_NOTICE);
  235. }
  236. }
  237. // Finally, send the mail. If an exception occurs, convert it into a
  238. // warning-level message so we can avoid an exception thrown without a
  239. // stack frame.
  240. try {
  241. $this->_mail->send();
  242. } catch (Exception $e) {
  243. trigger_error(
  244. "unable to send log entries via email; " .
  245. "message = {$e->getMessage()}; " .
  246. "code = {$e->getCode()}; " .
  247. "exception class = " . get_class($e),
  248. E_USER_WARNING);
  249. }
  250. }
  251. /**
  252. * Gets a string of number of entries per-priority level that occurred, or
  253. * an emptry string if none occurred.
  254. *
  255. * @return string
  256. */
  257. protected function _getFormattedNumEntriesPerPriority()
  258. {
  259. $strings = array();
  260. foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
  261. $strings[] = "{$priority}={$numEntries}";
  262. }
  263. return implode(', ', $strings);
  264. }
  265. }