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 $maxLines = null; //!< Maximum numbers of lines to show (hide + click-to-show when larger than this; empty = show all).
8  public $templateMaxLines = "
9  <div class='more'>
10  <input type='checkbox' id='[id]'> <label for='[id]'>show [lines] lines</label>
11  <div>[value]</div>
12  </div>";
14  public $interval = 60; //!< Minimal mail interval (minutes).
15  public $deadline = 24 * 60; //!< Maximum mail interval (minutes).
16  public $from = null; //!< Sender (defaults to default From address).
17  public $to = null; //!< Send mail to this address (or array for multiple; defaults to default From address).
18  public $subject = 'Log *'; //!< Subject for the mail (an asterisk is replaced with the host name).
19  public $templateSkipped = "
20  <tr><td colspan='2'><hr></td></tr>
21  <tr><td class='skipped' colspan='2'>Skipped * bytes</td></tr>";
22  //!< Template when skipping (an asterisk is replaced with the number of bytes skipped).
23  public $templateFile = __DIR__ . '/mail.html'; //!< File with mail template ([time], [host], [ip] of server, [index] and
24  // [log] are replaced; empty or non-existent defaults to log only).
25 
26  protected function value($value){
27  $value = parent::value($value);
28  return $this->maxLines && (($lines = substr_count($value,"\n") + 1) > $this->maxLines) ? strtr($this->templateMaxLines,[
29  '[id]' => \Rsi\Str::random(),
30  '[lines]' => $lines,
31  '[value]' => $value
32  ]) : $value;
33  }
34 
35  protected function format($prio,$context){
36  if(strlen($result = parent::format($prio,$context)) > $this->maxSize){
37  $result = substr($result,0,$this->maxSize);
38  $result = substr($result,0,strrpos($result,"</tr>\n<tr>") + 5);
39  }
40  return $result;
41  }
42 
43  public function send(){
44  $result = $body = null;
45  if($size = filesize($this->filename)) try{
46  \Rsi\File::write($this->timeFilename,date('c'),0666);
47  //index
48  fseek($f = fopen($this->indexFilename,'r'),$skipped = max(0,filesize($this->indexFilename) - $this->maxSize));
49  $index = fread($f,$this->maxSize);
50  fclose($f);
51  file_put_contents($this->indexFilename,null);
52  if($skipped) $index = substr($index,strpos($index,"<tr"));
53  if(preg_match_all('/<tr.*?(' . str_replace('*','\\w+\\b',$this->hashFormat) . ')/',$index,$hashes))
54  foreach(($hashes = array_count_values($hashes[1])) as $hash => $count){
55  $body .= "<input type='checkbox' id='$hash' class='hide-all'>";
56  $index = str_replace($hash . '-count',$count,substr_replace($index,$count == 1 ? 'single ' : 'first ',strpos($index,$hash),0));
57  }
58  //log
59  fseek($f = fopen($this->filename,'r'),$skipped = max(0,$size - $this->maxSize));
60  $log = fread($f,$this->maxSize);
61  fclose($f);
62  file_put_contents($this->filename,null);
63  if($skipped){
64  $log = str_replace('*',$skipped,$this->templateSkipped) . substr($log,$extra = strpos($log,"\n<tr"));
65  $skipped += $extra;
66  }
67  if(preg_match_all('/\\[hashes\\|(.*?)\\]/',$message = strtr(\Rsi\File::read($this->templateFile),[
68  '[time]' => date('Y-m-d H:i:s'),
69  '[host]' => $host = \Rsi\Http::host(),
70  '[ip]' => $_SERVER['SERVER_ADDR'] ?? gethostbyname($host),
71  '[body]' => $body,
72  '[index]' => $index,
73  '[log]' => $log
74  ]),$matches,PREG_SET_ORDER)) foreach($matches as $match){
75  $replace = [];
76  foreach($hashes as $hash => $count) if($count > 1) $replace[] = str_replace('*',$hash,$match[1]);
77  $message = str_replace($match[0],implode(',',$replace) ?: str_replace('*','single',$this->hashFormat),$message);
78  }
79  if($log) $result = $this->_log->component('mail')->send(
80  $this->from,
81  $this->to ?: ini_get('sendmail_from'),
82  str_replace('*',$host,$this->subject),
83  $message ?: $log,
84  true
85  );
86  }
87  catch(\Rsi\Fred\Exception $e){
88  if($this->_log->fred->debug) throw $e;
89  $result = false;
90  }
91  return $result;
92  }
93 
94  public function add($prio,$message,$context){
95  parent::add($prio,$message,$context);
96  $age = time() - $this->_log->filemtime($this->timeFilename);
97  if(
98  (($prio >= $this->threshold) && ($prio < \Rsi\Fred\Log::CUSTOM) && ($age >= $this->interval * 60)) ||
99  ($age > $this->deadline * 60)
100  ) $this->send();
101  }
102 
103  protected function getFilename(){
104  if(!parent::getFilename()) $this->_filename = \Rsi\File::tempDir() . "maillog-{$this->name}.tmp";
105  return $this->_filename;
106  }
107 
108  protected function getTimeFilename(){
109  return $this->filename . '.time';
110  }
111 
112 }
Rsi
Rsi\Fred\Log\Handler\Mail\$templateFile
$templateFile
File with mail template ([time], [host], [ip] of server, [index] and.
Definition: Mail.php:23
Rsi\Fred\Log\Handler\Mail\$interval
$interval
Minimal mail interval (minutes).
Definition: Mail.php:14
Rsi\Fred\Log\Handler\Html
Definition: Html.php:5
Rsi\Fred\Log\Handler\Mail\getFilename
getFilename()
Definition: Mail.php:103
Rsi\Fred\Log\Handler\Mail\$templateMaxLines
$templateMaxLines
Definition: Mail.php:8
Rsi\Fred\Log\Handler\Mail\$templateSkipped
$templateSkipped
Template when skipping (an asterisk is replaced with the number of bytes skipped).
Definition: Mail.php:19
Rsi\Fred\Log\Handler\Mail
Definition: Mail.php:5
Rsi\Fred\Log\Handler\Mail\add
add($prio, $message, $context)
Definition: Mail.php:94
Rsi\Fred\Log\Handler\Mail\$to
$to
Send mail to this address (or array for multiple; defaults to default From address).
Definition: Mail.php:17
Rsi\Fred\Log\CUSTOM
const CUSTOM
Everything above this is only logged when exactly matched.
Definition: Log.php:7
Rsi\Fred\Log\Handler
Definition: Console.php:3
Rsi\Fred\Log\Handler\Mail\$maxLines
$maxLines
Maximum numbers of lines to show (hide + click-to-show when larger than this; empty = show all).
Definition: Mail.php:7
Rsi\Fred\Log\ERROR
const ERROR
Error conditions.
Definition: Log.php:11
Rsi\Fred\Log\Handler\Mail\$threshold
$threshold
Definition: Mail.php:13
Rsi\Fred\Log\Handler\File\$_filename
$_filename
Definition: File.php:12
Rsi\Fred\Log\Handler\Mail\format
format($prio, $context)
Definition: Mail.php:35
Rsi\Fred\Log\Handler\Mail\$subject
$subject
Subject for the mail (an asterisk is replaced with the host name).
Definition: Mail.php:18
Rsi\Fred\Log\Handler\Mail\value
value($value)
Definition: Mail.php:26
Rsi\Fred\Log\Handler\Mail\$from
$from
Sender (defaults to default From address).
Definition: Mail.php:16
Rsi\Fred\Log\Handler\Mail\$deadline
$deadline
Maximum mail interval (minutes).
Definition: Mail.php:15
Rsi\Fred\Log\Handler\Mail\send
send()
Definition: Mail.php:43
Rsi\Fred\Log\Handler\Mail\getTimeFilename
getTimeFilename()
Definition: Mail.php:108
Rsi\Fred\Exception
Definition: Exception.php:5