1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace bedezign\yii2\audit\panels; |
4
|
|
|
|
5
|
|
|
use bedezign\yii2\audit\Audit; |
6
|
|
|
use bedezign\yii2\audit\components\panels\Panel; |
7
|
|
|
use bedezign\yii2\audit\models\AuditMail; |
8
|
|
|
use bedezign\yii2\audit\models\AuditMailSearch; |
9
|
|
|
use Swift_Message; |
10
|
|
|
use Swift_Mime_Attachment; |
11
|
|
|
use Swift_Mime_MimePart; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\base\Event; |
14
|
|
|
use yii\grid\GridViewAsset; |
15
|
|
|
use yii\mail\BaseMailer; |
16
|
|
|
use yii\mail\MessageInterface; |
17
|
|
|
use yii\swiftmailer\Message; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MailPanel |
21
|
|
|
* @package bedezign\yii2\audit\panels |
22
|
|
|
*/ |
23
|
|
|
class MailPanel extends Panel |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Store full email daata |
27
|
|
|
* |
28
|
|
|
* /!\ Set this to true will increase database size /!\ |
29
|
|
|
*/ |
30
|
|
|
public $storeData = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
54 |
|
public function init() |
36
|
|
|
{ |
37
|
52 |
|
parent::init(); |
38
|
54 |
|
Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) { |
39
|
2 |
|
$this->record($event); |
40
|
54 |
|
}); |
41
|
52 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Event $event |
45
|
|
|
* @return null|static |
46
|
|
|
*/ |
47
|
4 |
|
public function record($event) |
48
|
|
|
{ |
49
|
|
|
/* @var $message MessageInterface */ |
50
|
2 |
|
$message = $event->message; |
51
|
2 |
|
$entry = Audit::getInstance()->getEntry(true); |
52
|
|
|
|
53
|
2 |
|
$mail = new AuditMail(); |
54
|
2 |
|
$mail->entry_id = $entry->id; |
55
|
2 |
|
$mail->successful = $event->isSuccessful; |
56
|
2 |
|
$mail->from = self::convertParams($message->getFrom()); |
57
|
4 |
|
$mail->to = self::convertParams($message->getTo()); |
58
|
4 |
|
$mail->reply = self::convertParams($message->getReplyTo()); |
59
|
4 |
|
$mail->cc = self::convertParams($message->getCc()); |
60
|
4 |
|
$mail->bcc = self::convertParams($message->getBcc()); |
61
|
4 |
|
$mail->subject = $message->getSubject(); |
62
|
|
|
|
63
|
|
|
// add more information when message is a SwiftMailer message |
64
|
4 |
|
if ($message instanceof Message) { |
65
|
|
|
/* @var $swiftMessage Swift_Message */ |
66
|
4 |
|
$swiftMessage = $message->getSwiftMessage(); |
67
|
2 |
|
if ($swiftMessage->getContentType() == 'text/html') { |
68
|
|
|
$mail->html = $swiftMessage->getBody(); |
69
|
|
|
} else { |
70
|
2 |
|
$mail->text = $swiftMessage->getBody(); |
71
|
|
|
} |
72
|
2 |
|
foreach ($swiftMessage->getChildren() as $part) { |
73
|
|
|
/* @var $part Swift_Mime_MimePart */ |
74
|
2 |
|
if ($part instanceof Swift_Mime_Attachment) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
2 |
|
$contentType = $part->getContentType(); |
78
|
2 |
|
if ($contentType == 'text/plain') { |
79
|
2 |
|
$mail->text = $part->getBody(); |
80
|
2 |
|
} elseif ($contentType == 'text/html') { |
81
|
2 |
|
$mail->html = $part->getBody(); |
82
|
2 |
|
} |
83
|
2 |
|
} |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
// makes full email available for download |
87
|
2 |
|
if ($this->storeData) { |
88
|
2 |
|
$mail->data = $message->toString(); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
2 |
|
return $mail->save(false) ? $mail : null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $attr |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
2 |
|
private static function convertParams($attr) |
99
|
|
|
{ |
100
|
2 |
|
if (is_array($attr)) { |
101
|
2 |
|
$attr = implode(', ', array_keys($attr)); |
102
|
2 |
|
} |
103
|
2 |
|
return $attr; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
2 |
|
public function hasEntryData($entry) |
110
|
|
|
{ |
111
|
2 |
|
return count($entry->mails) > 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
22 |
|
public function getName() |
118
|
|
|
{ |
119
|
22 |
|
return Yii::t('audit', 'Mails'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
2 |
|
public function getLabel() |
126
|
|
|
{ |
127
|
2 |
|
return $this->getName() . ' <small>(' . count($this->_model->mails) . ')</small>'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
2 |
|
public function getDetail() |
134
|
|
|
{ |
135
|
2 |
|
$searchModel = new AuditMailSearch(); |
136
|
2 |
|
$params = \Yii::$app->request->getQueryParams(); |
137
|
2 |
|
$params['AuditMailSearch']['entry_id'] = $params['id']; |
138
|
2 |
|
$dataProvider = $searchModel->search($params); |
139
|
|
|
|
140
|
2 |
|
return \Yii::$app->view->render('panels/mail/detail', [ |
141
|
2 |
|
'panel' => $this, |
142
|
2 |
|
'dataProvider' => $dataProvider, |
143
|
2 |
|
'searchModel' => $searchModel, |
144
|
2 |
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
150
|
22 |
|
public function getIndexUrl() |
151
|
|
|
{ |
152
|
22 |
|
return ['mail/index']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
158
|
2 |
|
public function getChart() |
159
|
|
|
{ |
160
|
2 |
|
return \Yii::$app->view->render('panels/mail/chart', [ |
161
|
2 |
|
'panel' => $this, |
162
|
2 |
|
]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
2 |
|
public function registerAssets($view) |
169
|
|
|
{ |
170
|
2 |
|
GridViewAsset::register($view); |
171
|
2 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
2 |
|
public function cleanup($maxAge = null) |
177
|
|
|
{ |
178
|
2 |
|
$maxAge = $maxAge !== null ? $maxAge : $this->maxAge; |
179
|
2 |
|
if ($maxAge === null) |
180
|
2 |
|
return false; |
181
|
2 |
|
return AuditMail::deleteAll([ |
182
|
2 |
|
'<=', 'created', date('Y-m-d 23:59:59', strtotime("-$maxAge days")) |
183
|
2 |
|
]); |
184
|
|
|
} |
185
|
|
|
} |