FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Mail.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Log\Handler;
4 
5 class Mail extends Html{
6 
7  public $threshold = \Rsi\Fred\Log::ERROR;
8  public $interval = 60; //!< Minimal mail interval (minutes).
9  public $deadline = 24 * 60; //!< Maximum mail interval (minutes).
10  public $from = null; //!< Sender (defaults to default From address).
11  public $to = null; //!< Send mail to this address (or array for multiple; defaults to default From address).
12  public $subject = 'Log *'; //!< Subject for the mail (an asterisk is replaced with the host name).
13  public $templateSkipped = '
14  <tr><td colspan="2"><hr></td></tr>
15  <tr><td class="skipped" colspan="2">Skipped * bytes</td><tr>';
16  //!< Template when skipping (an asterisk is replaced with the number of bytes skipped).
17  public $templateFile = __DIR__ . '/mail.html'; //!< File with mail template ([time], [host], [ip] of server, [index] and
18  // [log] are replaced; empty or non-existent defaults to log only).
19 
20  protected function format($prio,$context){
21  if(strlen($result = parent::format($prio,$context)) > $this->maxSize){
22  $result = substr($result,0,$this->maxSize);
23  $result = substr($result,0,strrpos($result,"</tr>\n<tr>") + 5);
24  }
25  return $result;
26  }
27 
28  public function send(){
29  $result = null;
30  if($size = filesize($this->filename)) try{
31  \Rsi\File::write($this->timeFilename,date('c'),0666);
32  //index
33  fseek($f = fopen($this->indexFilename,'r'),$skipped = max(0,filesize($this->indexFilename) - $this->maxSize));
34  $index = fread($f,$this->maxSize);
35  fclose($f);
36  file_put_contents($this->indexFilename,null);
37  if($skipped) $index = substr($index,strpos($index,"\n<tr"));
38  //log
39  fseek($f = fopen($this->filename,'r'),$skipped = max(0,$size - $this->maxSize));
40  $log = fread($f,$this->maxSize);
41  fclose($f);
42  file_put_contents($this->filename,null);
43  if($skipped){
44  $log = str_replace('*',$skipped,$this->templateSkipped) . substr($log,$extra = strpos($log,"\n<tr"));
45  $skipped += $extra;
46  }
47  if($log) $result = $this->_log->component('mail')->send(
48  $this->from,
49  $this->to ?: ini_get('sendmail_from'),
50  str_replace('*',$host = \Rsi\Http::host(),$this->subject),
51  strtr(\Rsi\File::read($this->templateFile),[
52  '[time]' => date('Y-m-d H:i:s'),
53  '[host]' => $host,
54  '[ip]' => $_SERVER['SERVER_ADDR'] ?? gethostbyname($host),
55  '[index]' => $index,
56  '[log]' => $log
57  ]) ?: $log,
58  true
59  );
60  }
61  catch(\Rsi\Fred\Exception $e){
62  if($this->_log->fred->debug) throw $e;
63  $result = false;
64  }
65  return $result;
66  }
67 
68  public function add($prio,$message,$context){
69  parent::add($prio,$message,$context);
70  $age = time() - $this->_log->filemtime($this->timeFilename);
71  if(
72  (($prio >= $this->threshold) && ($prio < \Rsi\Fred\Log::CUSTOM) && ($age >= $this->interval * 60)) ||
73  ($age > $this->deadline * 60)
74  ) $this->send();
75  }
76 
77  protected function getFilename(){
78  if(!parent::getFilename()) $this->_filename = \Rsi\File::tempDir() . "maillog-{$this->name}.tmp";
79  return $this->_filename;
80  }
81 
82  protected function getTimeFilename(){
83  return $this->filename . '.time';
84  }
85 
86 }
$interval
Minimal mail interval (minutes).
Definition: Mail.php:8
const CUSTOM
Everything above this is only logged when exactly matched.
Definition: Log.php:7
$templateSkipped
Template when skipping (an asterisk is replaced with the number of bytes skipped).
Definition: Mail.php:13
add($prio, $message, $context)
Definition: Mail.php:68
$from
Sender (defaults to default From address).
Definition: Mail.php:10
format($prio, $context)
Definition: Mail.php:20
Framework for Rapid and Easy Development.
Definition: Fred.php:18
$to
Send mail to this address (or array for multiple; defaults to default From address).
Definition: Mail.php:11
$deadline
Maximum mail interval (minutes).
Definition: Mail.php:9
$subject
Subject for the mail (an asterisk is replaced with the host name).
Definition: Mail.php:12
$templateFile
File with mail template ([time], [host], [ip] of server, [index] and.
Definition: Mail.php:17