ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
mail.php
Go to the documentation of this file.
1 <?php
28 class 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 }