[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/Log/ -> mail.php (source)

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


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7