Added Email Logging for Emailnotification class

This commit is contained in:
Frank Schubert
2025-08-22 17:49:54 +02:00
parent 866c3b2ae1
commit 3a86160b38
26 changed files with 1636 additions and 46 deletions

View File

@@ -0,0 +1,243 @@
<?php
class EmailLog extends mfBaseModel
{
protected $forcestr = ["object_type", "object_data", "from", "to", "subject"];
private $attachments;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "attachments") {
$att = EmailLogAttachment::search(["emaillog_id" => $this->id]);
if($att) {
$this->attachments = $att;
}
return $this->attachments;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->getMfObject($classname, $this->$idfield);
if(!$this->$name) {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new EmailLog();
$table_fields = [
"object_type", "object_id", "object_data", "from", "to", "headers", "subject", "body_text", "body_html",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("EmailLog", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new EmailLog($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM EmailLog
WHERE $where
ORDER BY id LIMIT 1";
//var_dump($sql);exit;
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new EmailLog($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter, $order = false) {
$db = FronkDB::singleton();
if(!$order) {
$order = "`create` DESC";
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM EmailLog
WHERE $where
ORDER BY $order LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new EmailLog($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM EmailLog
WHERE $where";
//var_dump($filter);exit;
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`create` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM EmailLog
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new EmailLog($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("object_type", $filter)) {
$object_type = FronkDB::singleton()->escape($filter["object_type"]);
if($object_type) {
$where .= " AND object_type='$object_type'";
}
}
if(array_key_exists("object_id", $filter)) {
$object_id = $filter["object_id"];
if(is_numeric($object_id)) {
$where .= " AND object_id=$object_id";
}
}
if(array_key_exists("object_data", $filter)) {
$object_data = FronkDB::singleton()->escape($filter["object_data"]);
if($object_data) {
$where .= " AND object_data LIKE '$object_data'";
}
}
if(array_key_exists("from", $filter)) {
$from = FronkDB::singleton()->escape($filter["from"]);
if($from) {
$where .= " AND from LIKE '$from'";
}
}
if(array_key_exists("to", $filter)) {
$to = FronkDB::singleton()->escape($filter["to"]);
if($to) {
$where .= " AND to LIKE '$to'";
}
}
if(array_key_exists("subject", $filter)) {
$subject = FronkDB::singleton()->escape($filter["subject"]);
if($subject) {
$where .= " AND subject LIKE '$subject'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,48 @@
<?php
class EmailLogController extends mfBaseController {
protected function init() {
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
}
protected function downloadContentAction() {
$hash = $this->request->hash;
$aid = $this->request->aid;
$att = EmailLogAttachment::getOne($aid);
if(!$att) {
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
echo "File not found";
}
//var_dump($att);exit;
$content = EmailLogAttachmentContent::getFirst(["sha256" => $hash]);
if(!$content) {
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
echo "File not found";
}
if($att->content_id != $content->id) {
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
echo "File not found";
}
set_time_limit(36000);
if($content->mimetype) {
header("Content-Type: $content->mimetype");
} else {
header("Content-Type: application/octet-stream");
}
header('Content-disposition: attachment; filename="' . $att->filename . '"');
header("Content-Length: $content->filesize");
echo $content->content;
exit;
}
}