Cgiapp2
[ class tree: Cgiapp2 ] [ index: Cgiapp2 ] [ all elements ]

Source for file Mail.class.php

Documentation is available at Mail.class.php

  1. <?php
  2. /**
  3. * Cgiapp2 - Framework for building reusable web-applications
  4. *
  5. * A PHP5 port of perl's CGI::Application, a framework for building reusable web
  6. * applications.
  7. *
  8. * @package Cgiapp2
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>; based on
  10. * CGI::Application, by Jesse Erlbaum <jesse@erlbaum.net>, et. al.
  11. * @copyright (c) 2004 - present, Matthew Weier O'Phinney
  12. * @license BSD License (http://www.opensource.org/licenses/bsd-license.php)
  13. * @category Tools and Utilities
  14. * @tutorial Cgiapp2/Cgiapp2.cls
  15. * @version $Id:$
  16. */
  17.  
  18. /**
  19. * Observes Cgiapp2_Error
  20. */
  21. require_once 'Cgiapp2/Error.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Error_Observer_Interface
  25. */
  26. require_once 'Cgiapp2/Error/Observer/Interface.class.php';
  27.  
  28. /**
  29. * Cgiapp2_Error_Observer_Mail
  30. *
  31. * {@link Cgiapp2_Error} observer. Mails PHP error information to a user.
  32. *
  33. * Sample usage:
  34. * <code>
  35. * require_once 'Cgiapp2/Error.class.php';
  36. * require_once 'Cgiapp2/Error/Observer/Mail.class.php';
  37. *
  38. * // Set the mail recipient to 'mweierophinney@gmail.com'
  39. * Cgiapp2_Error_Observer_Mail:setRecipient('mweierophinney@gmail.com');
  40. *
  41. * // Set the mail subject to 'Error in application code'
  42. * Cgiapp2_Error_Observer_Mail:setSubject('Error in application code');
  43. *
  44. * set_error_handler(array('Cgiapp2_Error', 'handler'));
  45. * trigger_error('Mail this...', E_USER_NOTICE);
  46. * </code>
  47. *
  48. * @package Cgiapp2
  49. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  50. * @copyright (c) 2006 - Present, Matthew Weier O'Phinney
  51. * <mweierophinney@gmail.com>
  52. * @version @release-version@
  53. */
  54. class Cgiapp2_Error_Observer_Mail implements Cgiapp2_Error_Observer_Interface
  55. {
  56. /**
  57. * Email recipient
  58. * @var string
  59. * @access public
  60. */
  61. public $to;
  62.  
  63. /**
  64. * Email subject
  65. * @var string
  66. * @access public
  67. */
  68. public $subject;
  69.  
  70. /**
  71. * Singleton instance
  72. * @var bool|Cgiapp2_Error_Observer_Mail
  73. * @static
  74. * @access private
  75. */
  76. private static $instance = false;
  77.  
  78. /**
  79. * Constructor
  80. *
  81. * @param mixed $to
  82. * @param mixed $subject
  83. * @access private
  84. * @return void
  85. */
  86. private function __construct($subject, $to = null)
  87. {
  88. $this->to = $to;
  89. $this->subject = $subject;
  90. }
  91.  
  92. /**
  93. * Singleton
  94. *
  95. * Accepts recipient address and subject; uses sane default for subject
  96. * ('Error occurred in application').
  97. *
  98. * @static
  99. * @access public
  100. * @param string $to
  101. * @param string $subject
  102. * @return Cgiapp2_Error_Observer_Mail
  103. */
  104. public static function getInstance($to = null, $subject = '')
  105. {
  106. if (self::$instance) {
  107. return self::$instance;
  108. }
  109.  
  110. if (empty($subject)) {
  111. $subject = 'Error in application occurred';
  112. }
  113.  
  114. return new Cgiapp2_Error_Observer_Mail($subject, $to);
  115. }
  116.  
  117. /**
  118. * Set mail recipient
  119. *
  120. * @static
  121. * @access public
  122. * @param string $to
  123. * @return void
  124. */
  125. public static function setRecipient($to)
  126. {
  127. self::getInstance()->to = $to;
  128. }
  129.  
  130. /**
  131. * Set mail subject
  132. *
  133. * @static
  134. * @access public
  135. * @param string $subject
  136. * @return void
  137. */
  138. public static function setSubject($subject)
  139. {
  140. self::getInstance()->subject = $subject;
  141. }
  142.  
  143. /**
  144. * Mail a report
  145. *
  146. * If no {@link $to} address is defined in the singleton instance, nothing
  147. * is done. Otherwise, an email is sent with details of the PHP error.
  148. *
  149. * @access public
  150. * @param Cgiapp2_Error $e
  151. * @return void
  152. */
  153. public static function event(Cgiapp2_Error $e)
  154. {
  155. $handler = self::getInstance();
  156. if (empty($handler->to)) {
  157. return;
  158. }
  159.  
  160. $code = $e->errno;
  161. $msg = $e->errmsg;
  162. $file = '';
  163. $line = '';
  164. $context = '';
  165.  
  166. if (!empty($e->errfile)) {
  167. $file = 'File: ' . $e->errfile . "\n";
  168. }
  169.  
  170. if (!empty($e->errline)) {
  171. $line = 'Line: ' . $e->errline . "\n";
  172. }
  173.  
  174. if (!empty($e->errcontext) && is_array($e->errcontext)) {
  175. $context = "Error Message:\n";
  176. $context = implode("\n", $e->errcontext);
  177. }
  178.  
  179. $body=<<<EOT
  180. An error occurred in your application:
  181.  
  182. $file
  183. $line
  184. Error Number: $code
  185. Error Message:
  186. $msg
  187.  
  188. $context
  189. EOT;
  190. @mail($handler->to, $handler->subject, $body);
  191. }
  192. }
  193.  
  194. /**
  195. * Observe Cgiapp2_Error
  196. */
  197. Cgiapp2_Error::attach('Cgiapp2_Error_Observer_Mail');

Documentation generated on Sat, 03 Jun 2006 10:48:51 -0400 by phpDocumentor 1.3.0RC5