1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2014 Mautic Contributors. All rights reserved |
5
|
|
|
* @author Mautic |
6
|
|
|
* |
7
|
|
|
* @link http://mautic.org |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\PageBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\EventListener\CommonSubscriber; |
15
|
|
|
use Mautic\CoreBundle\Helper\IpLookupHelper; |
16
|
|
|
use Mautic\CoreBundle\Model\AuditLogModel; |
17
|
|
|
use Mautic\CoreBundle\Templating\Helper\AssetsHelper; |
18
|
|
|
use Mautic\PageBundle\Event as Events; |
19
|
|
|
use Mautic\PageBundle\Model\PageModel; |
20
|
|
|
use Mautic\PageBundle\PageEvents; |
21
|
|
|
use Mautic\QueueBundle\Event\QueueConsumerEvent; |
22
|
|
|
use Mautic\QueueBundle\Queue\QueueConsumerResults; |
23
|
|
|
use Mautic\QueueBundle\QueueEvents; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class PageSubscriber. |
27
|
|
|
*/ |
28
|
|
|
class PageSubscriber extends CommonSubscriber |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AssetsHelper |
32
|
|
|
*/ |
33
|
|
|
protected $assetsHelper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AuditLogModel |
37
|
|
|
*/ |
38
|
|
|
protected $auditLogModel; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var IpLookupHelper |
42
|
|
|
*/ |
43
|
|
|
protected $ipLookupHelper; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var PageModel |
47
|
|
|
*/ |
48
|
|
|
protected $pageModel; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* PageSubscriber constructor. |
52
|
|
|
* |
53
|
|
|
* @param AssetsHelper $assetsHelper |
54
|
|
|
* @param IpLookupHelper $ipLookupHelper |
55
|
|
|
* @param AuditLogModel $auditLogModel |
56
|
|
|
* @param PageModel $pageModel |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
AssetsHelper $assetsHelper, |
60
|
|
|
IpLookupHelper $ipLookupHelper, |
61
|
|
|
AuditLogModel $auditLogModel, |
62
|
|
|
PageModel $pageModel |
63
|
|
|
) { |
64
|
|
|
$this->assetsHelper = $assetsHelper; |
65
|
|
|
$this->ipLookupHelper = $ipLookupHelper; |
66
|
|
|
$this->auditLogModel = $auditLogModel; |
67
|
|
|
$this->pageModel = $pageModel; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public static function getSubscribedEvents() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
PageEvents::PAGE_POST_SAVE => ['onPagePostSave', 0], |
77
|
|
|
PageEvents::PAGE_POST_DELETE => ['onPageDelete', 0], |
78
|
|
|
PageEvents::PAGE_ON_DISPLAY => ['onPageDisplay', -255], // We want this to run last |
79
|
|
|
QueueEvents::PAGE_HIT => ['onPageHit', 0], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add an entry to the audit log. |
85
|
|
|
* |
86
|
|
|
* @param Events\PageEvent $event |
87
|
|
|
*/ |
88
|
|
|
public function onPagePostSave(Events\PageEvent $event) |
89
|
|
|
{ |
90
|
|
|
$page = $event->getPage(); |
91
|
|
|
if ($details = $event->getChanges()) { |
92
|
|
|
$log = [ |
93
|
|
|
'bundle' => 'page', |
94
|
|
|
'object' => 'page', |
95
|
|
|
'objectId' => $page->getId(), |
96
|
|
|
'action' => ($event->isNew()) ? 'create' : 'update', |
97
|
|
|
'details' => $details, |
98
|
|
|
'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(), |
99
|
|
|
]; |
100
|
|
|
$this->auditLogModel->writeToLog($log); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add a delete entry to the audit log. |
106
|
|
|
* |
107
|
|
|
* @param Events\PageEvent $event |
108
|
|
|
*/ |
109
|
|
|
public function onPageDelete(Events\PageEvent $event) |
110
|
|
|
{ |
111
|
|
|
$page = $event->getPage(); |
112
|
|
|
$log = [ |
113
|
|
|
'bundle' => 'page', |
114
|
|
|
'object' => 'page', |
115
|
|
|
'objectId' => $page->deletedId, |
116
|
|
|
'action' => 'delete', |
117
|
|
|
'details' => ['name' => $page->getTitle()], |
118
|
|
|
'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(), |
119
|
|
|
]; |
120
|
|
|
$this->auditLogModel->writeToLog($log); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Allow event listeners to add scripts to |
125
|
|
|
* - </head> : onPageDisplay_headClose |
126
|
|
|
* - <body> : onPageDisplay_bodyOpen |
127
|
|
|
* - </body> : onPageDisplay_bodyClose. |
128
|
|
|
* |
129
|
|
|
* @param Events\PageDisplayEvent $event |
130
|
|
|
*/ |
131
|
|
|
public function onPageDisplay(Events\PageDisplayEvent $event) |
132
|
|
|
{ |
133
|
|
|
$content = $event->getContent(); |
134
|
|
|
|
135
|
|
|
// Get scripts to insert before </head> |
136
|
|
|
ob_start(); |
137
|
|
|
$this->assetsHelper->outputScripts('onPageDisplay_headClose'); |
138
|
|
|
$headCloseScripts = ob_get_clean(); |
139
|
|
|
|
140
|
|
|
if ($headCloseScripts) { |
141
|
|
|
$content = str_ireplace('</head>', $headCloseScripts."\n</head>", $content); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Get scripts to insert after <body> |
145
|
|
|
ob_start(); |
146
|
|
|
$this->assetsHelper->outputScripts('onPageDisplay_bodyOpen'); |
147
|
|
|
$bodyOpenScripts = ob_get_clean(); |
148
|
|
|
|
149
|
|
|
if ($bodyOpenScripts) { |
150
|
|
|
preg_match('/(<body[a-z=\s\-_:"\']*>)/i', $content, $matches); |
151
|
|
|
|
152
|
|
|
$content = str_ireplace($matches[0], $matches[0]."\n".$bodyOpenScripts, $content); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Get scripts to insert before </body> |
156
|
|
|
ob_start(); |
157
|
|
|
$this->assetsHelper->outputScripts('onPageDisplay_bodyClose'); |
158
|
|
|
$bodyCloseScripts = ob_get_clean(); |
159
|
|
|
|
160
|
|
|
if ($bodyCloseScripts) { |
161
|
|
|
$content = str_ireplace('</body>', $bodyCloseScripts."\n</body>", $content); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Get scripts to insert before a custom tag |
165
|
|
|
$params = $event->getParams(); |
166
|
|
|
if (count($params) > 0) { |
167
|
|
|
if (isset($params['custom_tag']) && $customTag = $params['custom_tag']) { |
168
|
|
|
ob_start(); |
169
|
|
|
$this->assetsHelper->outputScripts('customTag'); |
170
|
|
|
$bodyCustomTag = ob_get_clean(); |
171
|
|
|
|
172
|
|
|
if ($bodyCustomTag) { |
173
|
|
|
$content = str_ireplace($customTag, $bodyCustomTag."\n".$customTag, $content); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$event->setContent($content); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param QueueConsumerEvent $event |
183
|
|
|
*/ |
184
|
|
|
public function onPageHit(QueueConsumerEvent $event) |
185
|
|
|
{ |
186
|
|
|
$payload = $event->getPayload(); |
187
|
|
|
$request = $payload['request']; |
188
|
|
|
$trackingNewlyGenerated = $payload['isNew']; |
189
|
|
|
$hitId = $payload['hitId']; |
190
|
|
|
$pageId = $payload['pageId']; |
191
|
|
|
$leadId = $payload['leadId']; |
192
|
|
|
$isRedirect = !empty($payload['isRedirect']); |
193
|
|
|
$hitRepo = $this->em->getRepository('MauticPageBundle:Hit'); |
194
|
|
|
$pageRepo = $this->em->getRepository('MauticPageBundle:Page'); |
195
|
|
|
$redirectRepo = $this->em->getRepository('MauticPageBundle:Redirect'); |
196
|
|
|
$leadRepo = $this->em->getRepository('MauticLeadBundle:Lead'); |
197
|
|
|
$hit = $hitId ? $hitRepo->find((int) $hitId) : null; |
198
|
|
|
$lead = $leadId ? $leadRepo->find((int) $leadId) : null; |
199
|
|
|
|
200
|
|
|
// On the off chance that the queue contains a message which does not |
201
|
|
|
// reference a valid Hit or Lead, discard it to avoid clogging the queue. |
202
|
|
|
if (null === $hit || null === $lead) { |
203
|
|
|
$event->setResult(QueueConsumerResults::REJECT); |
204
|
|
|
|
205
|
|
|
// Log the rejection with event payload as context. |
206
|
|
|
if ($this->logger) { |
207
|
|
|
$this->logger->addNotice( |
208
|
|
|
'QUEUE MESSAGE REJECTED: Lead or Hit not found', |
209
|
|
|
$payload |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if ($isRedirect) { |
217
|
|
|
$page = $pageId ? $redirectRepo->find((int) $pageId) : null; |
218
|
|
|
} else { |
219
|
|
|
$page = $pageId ? $pageRepo->find((int) $pageId) : null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Also reject messages when processing causes any other exception. |
223
|
|
|
try { |
224
|
|
|
$this->pageModel->processPageHit($hit, $page, $request, $lead, $trackingNewlyGenerated, false); |
225
|
|
|
$event->setResult(QueueConsumerResults::ACKNOWLEDGE); |
226
|
|
|
} catch (\Exception $e) { |
227
|
|
|
$event->setResult(QueueConsumerResults::REJECT); |
228
|
|
|
|
229
|
|
|
// Log the exception with event payload as context. |
230
|
|
|
if ($this->logger) { |
231
|
|
|
$this->logger->addError( |
232
|
|
|
'QUEUE CONSUMER ERROR ('.QueueEvents::PAGE_HIT.'): '.$e->getMessage(), |
233
|
|
|
$payload |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|