1 |
joko |
1.1 |
<?php |
2 |
|
|
// $Id: mail.php,v 1.5 2002/12/02 05:23:00 jon Exp $ |
3 |
|
|
|
4 |
|
|
/** |
5 |
|
|
* The Log_mail class is a concrete implementation of the Log:: abstract class |
6 |
|
|
* which sends log messages to a mailbox. |
7 |
|
|
* The mail is actually sent when you close() the logger, or when the destructor |
8 |
|
|
* is called (when the script is terminated). |
9 |
|
|
* |
10 |
|
|
* PLEASE NOTE that you must create a Log_mail object using =&, like this : |
11 |
|
|
* $logger =& Log::factory("mail", "recipient@example.com", ...) |
12 |
|
|
* |
13 |
|
|
* This is a PEAR requirement for destructors to work properly. |
14 |
|
|
* See http://pear.php.net/manual/en/class.pear.php |
15 |
|
|
* |
16 |
|
|
* @author Ronnie Garcia <ronnie@mk2.net> |
17 |
|
|
* @author Jon Parise <jon@php.net> |
18 |
|
|
* @version $Revision: 1.5 $ |
19 |
|
|
* @package Log |
20 |
|
|
*/ |
21 |
|
|
class Log_mail extends Log { |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* String holding the recipient's email address. |
25 |
|
|
* @var string |
26 |
|
|
*/ |
27 |
|
|
var $_recipient = ''; |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* String holding the sender's email address. |
31 |
|
|
* @var string |
32 |
|
|
*/ |
33 |
|
|
var $_from = ''; |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* String holding the email's subject. |
37 |
|
|
* @var string |
38 |
|
|
*/ |
39 |
|
|
var $_subject = '[Log_mail] Log message'; |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* String holding the mail message body. |
43 |
|
|
* @var string |
44 |
|
|
*/ |
45 |
|
|
var $_message = ''; |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Constructs a new Log_mail object. |
50 |
|
|
* |
51 |
|
|
* Here is how you can customize the mail driver with the conf[] hash : |
52 |
|
|
* $conf['from'] : the mail's "From" header line, |
53 |
|
|
* $conf['subject'] : the mail's "Subject" line. |
54 |
|
|
* |
55 |
|
|
* @param string $name The filename of the logfile. |
56 |
|
|
* @param string $ident The identity string. |
57 |
|
|
* @param array $conf The configuration array. |
58 |
|
|
* @param int $maxLevel Maximum level at which to log. |
59 |
|
|
* @access public |
60 |
|
|
*/ |
61 |
|
|
function Log_mail($name, $ident = '', $conf = array(), |
62 |
|
|
$maxLevel = PEAR_LOG_DEBUG) |
63 |
|
|
{ |
64 |
|
|
$this->_recipient = $name; |
65 |
|
|
$this->_ident = $ident; |
66 |
|
|
$this->_maxLevel = $maxLevel; |
67 |
|
|
|
68 |
|
|
if (!empty($conf['from'])) { |
69 |
|
|
$this->_from = $conf['from']; |
70 |
|
|
} else { |
71 |
|
|
$this->_from = ini_get('sendmail_from'); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
if (!empty($conf['subject'])) { |
75 |
|
|
$this->_subject = $conf['subject']; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
/* register the destructor */ |
79 |
|
|
$this->PEAR(); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Destructor. Calls close(). |
84 |
|
|
* |
85 |
|
|
* @access private |
86 |
|
|
*/ |
87 |
|
|
function _Log_mail() |
88 |
|
|
{ |
89 |
|
|
$this->close(); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* Starts a new mail message. |
94 |
|
|
* This is implicitly called by log(), if necessary. |
95 |
|
|
* |
96 |
|
|
* @access public |
97 |
|
|
*/ |
98 |
|
|
function open() |
99 |
|
|
{ |
100 |
|
|
if (!$this->_opened) { |
101 |
|
|
$this->_message = "Log messages:\n\n"; |
102 |
|
|
$this->_opened = true; |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* Closes the message, if it is open, and sends the mail. |
108 |
|
|
* This is implicitly called by the destructor, if necessary. |
109 |
|
|
* |
110 |
|
|
* @access public |
111 |
|
|
*/ |
112 |
|
|
function close() |
113 |
|
|
{ |
114 |
|
|
if ($this->_opened) { |
115 |
|
|
if (!empty($this->_message)) { |
116 |
|
|
$headers = "From: $this->_from\r\n"; |
117 |
|
|
$headers .= "User-Agent: Log_mail\r\n"; |
118 |
|
|
|
119 |
|
|
if (mail($this->_recipient, $this->_subject, $this->_message, |
120 |
|
|
$headers) == false) { |
121 |
|
|
error_log("Log_mail: Failure executing mail()", 0); |
122 |
|
|
return false; |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
$this->_opened = false; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
return true; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Writes $message to the currently open mail message. |
133 |
|
|
* Calls open(), if necessary. |
134 |
|
|
* |
135 |
|
|
* @param string $message The textual message to be logged. |
136 |
|
|
* @param string $priority The priority of the message. Valid |
137 |
|
|
* values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, |
138 |
|
|
* PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, |
139 |
|
|
* PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. |
140 |
|
|
* The default is PEAR_LOG_INFO. |
141 |
|
|
* @return boolean True on success or false on failure. |
142 |
|
|
* @access public |
143 |
|
|
*/ |
144 |
|
|
function log($message, $priority = PEAR_LOG_INFO) |
145 |
|
|
{ |
146 |
|
|
/* Abort early if the priority is above the maximum logging level. */ |
147 |
|
|
if ($priority > $this->_maxLevel) { |
148 |
|
|
return false; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
if (!$this->_opened) { |
152 |
|
|
$this->open(); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
$entry = sprintf("%s %s [%s] %s\n", strftime('%b %d %H:%M:%S'), |
156 |
|
|
$this->_ident, Log::priorityToString($priority), $message); |
157 |
|
|
|
158 |
|
|
$this->_message .= $entry; |
159 |
|
|
|
160 |
|
|
$this->notifyAll(array('priority' => $priority, 'message' => $message)); |
161 |
|
|
|
162 |
|
|
return true; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
?> |