ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
mail.php
Go to the documentation of this file.
1<?php
28class Log_mail extends Log
29{
36 var $_recipients = '';
37
43 var $_from = '';
44
50 var $_subject = '[Log_mail] Log message';
51
57 var $_preamble = '';
58
64 var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
65
73 var $_timeFormat = '%b %d %H:%M:%S';
74
80 var $_message = '';
81
88 var $_shouldSend = false;
89
95 var $_mailBackend = '';
96
102 var $_mailParams = array();
103
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
168 function _Log_mail()
169 {
170 $this->close();
171 }
172
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
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: PEAR Log Package';
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' => 'PEAR Log Package',
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
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
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}
const PEAR_LOG_DEBUG
Definition: Log.php:17
_Log_mail()
Destructor.
Definition: mail.php:168
$_recipients
Definition: mail.php:36
$_from
Definition: mail.php:43
$_shouldSend
Definition: mail.php:88
$_subject
Definition: mail.php:50
$_lineFormat
Definition: mail.php:64
flush()
Flushes the log output by forcing the email message to be sent now.
Definition: mail.php:242
close()
Closes the message, if it is open, and sends the mail.
Definition: mail.php:198
$_message
Definition: mail.php:80
$_mailBackend
Definition: mail.php:95
$_preamble
Definition: mail.php:57
$_mailParams
Definition: mail.php:102
open()
Starts a new mail message.
Definition: mail.php:179
Log_mail($name, $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
Constructs a new Log_mail object.
Definition: mail.php:119
log($message, $priority=null)
Writes $message to the currently open mail message.
Definition: mail.php:263
$_timeFormat
Definition: mail.php:73
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
$_priority
Definition: Log.php:69
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
$_opened
Definition: Log.php:45
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
_format($format, $timestamp, $priority, $message)
Produces a formatted log line based on a format string and a set of variables representing the curren...
Definition: Log.php:530
& factory($driver, $params=array())
Provides an interface for generating Mail:: objects of various types.
Definition: Mail.php:74
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
The Log:: class implements both an abstraction for various logging mechanisms and the Subject end of ...