home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Log / mail.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.8 KB  |  295 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/mail.php,v 1.27 2008/02/10 00:15:04 jon Exp $
  4.  *
  5.  * @version $Revision: 1.27 $
  6.  * @package Log
  7.  */
  8.  
  9. /**
  10.  * The Log_mail class is a concrete implementation of the Log:: abstract class
  11.  * which sends log messages to a mailbox.
  12.  * The mail is actually sent when you close() the logger, or when the destructor
  13.  * is called (when the script is terminated).
  14.  *
  15.  * PLEASE NOTE that you must create a Log_mail object using =&, like this :
  16.  *  $logger =& Log::factory("mail", "recipient@example.com", ...)
  17.  *
  18.  * This is a PEAR requirement for destructors to work properly.
  19.  * See http://pear.php.net/manual/en/class.pear.php
  20.  *
  21.  * @author  Ronnie Garcia <ronnie@mk2.net>
  22.  * @author  Jon Parise <jon@php.net>
  23.  * @since   Log 1.3
  24.  * @package Log
  25.  *
  26.  * @example mail.php    Using the mail handler.
  27.  */
  28. class Log_mail extends Log
  29. {
  30.     /**
  31.      * String holding the recipients' email addresses.  Multiple addresses
  32.      * should be separated with commas.
  33.      * @var string
  34.      * @access private
  35.      */
  36.     var $_recipients = '';
  37.  
  38.     /**
  39.      * String holding the sender's email address.
  40.      * @var string
  41.      * @access private
  42.      */
  43.     var $_from = '';
  44.  
  45.     /**
  46.      * String holding the email's subject.
  47.      * @var string
  48.      * @access private
  49.      */
  50.     var $_subject = '[Log_mail] Log message';
  51.  
  52.     /**
  53.      * String holding an optional preamble for the log messages.
  54.      * @var string
  55.      * @access private
  56.      */
  57.     var $_preamble = '';
  58.  
  59.     /**
  60.      * String containing the format of a log line.
  61.      * @var string
  62.      * @access private
  63.      */
  64.     var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  65.  
  66.     /**
  67.      * String containing the timestamp format.  It will be passed directly to
  68.      * strftime().  Note that the timestamp string will generated using the
  69.      * current locale.
  70.      * @var string
  71.      * @access private
  72.      */
  73.     var $_timeFormat = '%b %d %H:%M:%S';
  74.  
  75.     /**
  76.      * String holding the mail message body.
  77.      * @var string
  78.      * @access private
  79.      */
  80.     var $_message = '';
  81.  
  82.     /**
  83.      * Flag used to indicated that log lines have been written to the message
  84.      * body and the message should be sent on close().
  85.      * @var boolean
  86.      * @access private
  87.      */
  88.     var $_shouldSend = false;
  89.  
  90.     /**
  91.      * String holding the backend name of PEAR::Mail
  92.      * @var string
  93.      * @access private
  94.      */
  95.     var $_mailBackend = '';
  96.  
  97.     /**
  98.      * Array holding the params for PEAR::Mail
  99.      * @var array
  100.      * @access private
  101.      */
  102.     var $_mailParams = array();
  103.  
  104.     /**
  105.      * Constructs a new Log_mail object.
  106.      *
  107.      * Here is how you can customize the mail driver with the conf[] hash :
  108.      *   $conf['from']:        the mail's "From" header line,
  109.      *   $conf['subject']:     the mail's "Subject" line.
  110.      *   $conf['mailBackend']: backend name of PEAR::Mail
  111.      *   $conf['mailParams']:  parameters for the PEAR::Mail backend
  112.      *
  113.      * @param string $name      The message's recipients.
  114.      * @param string $ident     The identity string.
  115.      * @param array  $conf      The configuration array.
  116.      * @param int    $level     Log messages up to and including this level.
  117.      * @access public
  118.      */
  119.     function Log_mail($name, $ident = '', $conf = array(),
  120.                       $level = PEAR_LOG_DEBUG)
  121.     {
  122.         $this->_id = md5(microtime());
  123.         $this->_recipients = $name;
  124.         $this->_ident = $ident;
  125.         $this->_mask = Log::UPTO($level);
  126.  
  127.         if (!empty($conf['from'])) {
  128.             $this->_from = $conf['from'];
  129.         } else {
  130.             $this->_from = ini_get('sendmail_from');
  131.         }
  132.  
  133.         if (!empty($conf['subject'])) {
  134.             $this->_subject = $conf['subject'];
  135.         }
  136.  
  137.         if (!empty($conf['preamble'])) {
  138.             $this->_preamble = $conf['preamble'];
  139.         }
  140.  
  141.         if (!empty($conf['lineFormat'])) {
  142.             $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  143.                                              array_values($this->_formatMap),
  144.                                              $conf['lineFormat']);
  145.         }
  146.  
  147.         if (!empty($conf['timeFormat'])) {
  148.             $this->_timeFormat = $conf['timeFormat'];
  149.         }
  150.  
  151.         if (!empty($conf['mailBackend'])) {
  152.             $this->_mailBackend = $conf['mailBackend'];
  153.         }
  154.  
  155.         if (!empty($conf['mailParams'])) {
  156.             $this->_mailParams = $conf['mailParams'];
  157.         }
  158.  
  159.         /* register the destructor */
  160.         register_shutdown_function(array(&$this, '_Log_mail'));
  161.     }
  162.  
  163.     /**
  164.      * Destructor. Calls close().
  165.      *
  166.      * @access private
  167.      */
  168.     function _Log_mail()
  169.     {
  170.         $this->close();
  171.     }
  172.  
  173.     /**
  174.      * Starts a new mail message.
  175.      * This is implicitly called by log(), if necessary.
  176.      *
  177.      * @access public
  178.      */
  179.     function open()
  180.     {
  181.         if (!$this->_opened) {
  182.             if (!empty($this->_preamble)) {
  183.                 $this->_message = $this->_preamble . "\r\n\r\n";
  184.             }
  185.             $this->_opened = true;
  186.             $_shouldSend = false;
  187.         }
  188.  
  189.         return $this->_opened;
  190.     }
  191.  
  192.     /**
  193.      * Closes the message, if it is open, and sends the mail.
  194.      * This is implicitly called by the destructor, if necessary.
  195.      *
  196.      * @access public
  197.      */
  198.     function close()
  199.     {
  200.         if ($this->_opened) {
  201.             if ($this->_shouldSend && !empty($this->_message)) {
  202.                 if ($this->_mailBackend === '') {  // use mail()
  203.                     $headers = "From: $this->_from\r\n";
  204.                     $headers .= "User-Agent: Log_mail";
  205.                     if (mail($this->_recipients, $this->_subject,
  206.                              $this->_message, $headers) == false) {
  207.                         return false;
  208.                     }
  209.                 } else {  // use PEAR::Mail
  210.                     include_once 'Mail.php';
  211.                     $headers = array('From' => $this->_from,
  212.                                      'To' => $this->_recipients,
  213.                                      'User-Agent' => 'Log_mail',
  214.                                      'Subject' => $this->_subject);
  215.                     $mailer = &Mail::factory($this->_mailBackend,
  216.                                              $this->_mailParams);
  217.                     $res = $mailer->send($this->_recipients, $headers,
  218.                                          $this->_message);
  219.                     if (PEAR::isError($res)) {
  220.                         return false;
  221.                     }
  222.                 }
  223.  
  224.                 /* Clear the message string now that the email has been sent. */
  225.                 $this->_message = '';
  226.                 $this->_shouldSend = false;
  227.             }
  228.             $this->_opened = false;
  229.         }
  230.  
  231.         return ($this->_opened === false);
  232.     }
  233.  
  234.     /**
  235.      * Flushes the log output by forcing the email message to be sent now.
  236.      * Events that are logged after flush() is called will be appended to a
  237.      * new email message.
  238.      *
  239.      * @access public
  240.      * @since Log 1.8.2
  241.      */
  242.     function flush()
  243.     {
  244.         /*
  245.          * It's sufficient to simply call close() to flush the output.
  246.          * The next call to log() will cause the handler to be reopened.
  247.          */
  248.         return $this->close();
  249.     }
  250.  
  251.     /**
  252.      * Writes $message to the currently open mail message.
  253.      * Calls open(), if necessary.
  254.      *
  255.      * @param mixed  $message  String or object containing the message to log.
  256.      * @param string $priority The priority of the message.  Valid
  257.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  258.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  259.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  260.      * @return boolean  True on success or false on failure.
  261.      * @access public
  262.      */
  263.     function log($message, $priority = null)
  264.     {
  265.         /* If a priority hasn't been specified, use the default value. */
  266.         if ($priority === null) {
  267.             $priority = $this->_priority;
  268.         }
  269.  
  270.         /* Abort early if the priority is above the maximum logging level. */
  271.         if (!$this->_isMasked($priority)) {
  272.             return false;
  273.         }
  274.  
  275.         /* If the message isn't open and can't be opened, return failure. */
  276.         if (!$this->_opened && !$this->open()) {
  277.             return false;
  278.         }
  279.  
  280.         /* Extract the string representation of the message. */
  281.         $message = $this->_extractMessage($message);
  282.  
  283.         /* Append the string containing the complete log line. */
  284.         $this->_message .= $this->_format($this->_lineFormat,
  285.                                           strftime($this->_timeFormat),
  286.                                           $priority, $message) . "\r\n";
  287.         $this->_shouldSend = true;
  288.  
  289.         /* Notify observers about this log message. */
  290.         $this->_announce(array('priority' => $priority, 'message' => $message));
  291.  
  292.         return true;
  293.     }
  294. }
  295.