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