forked from mautic/mautic-joomla
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mautic.php
548 lines (474 loc) · 15.2 KB
/
mautic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
/**
* Mautic-Joomla plugin
* @author Mautic
* @copyright Copyright (C) 2014 Mautic All Rights Reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* Website http://www.mautic.org
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.plugin.plugin');
jimport('joomla.log.log');
require_once __DIR__ . '/mauticApiHelper.php';
/**
*
* @package Joomla
* @subpackage System.Mautic
*/
class plgSystemMautic extends JPlugin
{
/**
* Regex to capture all {mautic} tags in content
*
* @var string
*/
protected $mauticRegex = '/\{(\{?)(mautic)(?![\w-])([^\}\/]*(?:\/(?!\})[^\}\/]*)*?)(?:(\/)\}|\}(?:([^\{]*+(?:\{(?!\/\2\})[^\{]*+)*+)\{\/\2\})?)(\}?)/i';
/**
* Taken from WP get_shortcode_atts_regex
*
* @var string
*/
protected $attsRegex = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
/**
* mauticApiHelper
*/
protected $apiHelper;
/**
* Object Constructor.
*
* @access public
* @param object $subject The object to observe -- event dispatcher.
* @param object $config The configuration object for the plugin.
* @since 1.0
*/
function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
// Define the logger.
JLog::addLogger(array('text_file' => 'plg_mautic.php'), JLog::ALL, array('plg_mautic'));
}
/**
* This event is triggered before the framework creates the Head section of the Document.
*
* @return mixed
*/
public function onBeforeCompileHead()
{
$app = JFactory::getApplication();
$document = JFactory::getDocument();
$input = $app->input;
// Check to make sure we are loading an HTML view and there is a main component area
if ($document->getType() !== 'html' || $input->get('tmpl', '', 'cmd') === 'component' || $app->isAdmin())
{
return true;
}
$attrs = array();
$user = JFactory::getUser();
// Get info about the user if logged in
if (!$user->guest)
{
$attrs['email'] = $user->email;
$name = explode(' ', $user->name);
if (isset($name[0]))
{
$attrs['firstname'] = $name[0];
}
$count = count($name);
$lastNamePos = $count -1;
if ($lastNamePos !== 0 && isset($name[$lastNamePos]))
{
$attrs['lastname'] = $name[$lastNamePos];
}
}
$mauticUrl = trim($this->params->get('base_url'), " \t\n\r\0\x0B/");
$attrs = json_encode($attrs, JSON_FORCE_OBJECT);
$mauticTrackingJS = <<<JS
(function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n;
w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t),
m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)
})(window,document,'script','{$mauticUrl}/mtc.js','mt');
mt('send', 'pageview', {$attrs});
JS;
$document->addScriptDeclaration($mauticTrackingJS);
return true;
}
/**
* Insert form script to the content
*
* @param string $context The context of the content being passed to the plugin.
* @param object $article The article object. Note $article->text is also available
* @param object $params The article params
* @param integer $page The 'page' number
*
* @return mixed
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
$app = JFactory::getApplication();
$document = JFactory::getDocument();
$input = $app->input;
// Check to make sure we are loading an HTML view and there is a main component area and content is not being indexed
if ($document->getType() !== 'html'
|| $input->get('tmpl', '', 'cmd') === 'component'
|| $app->isAdmin()
|| $context == 'com_finder.indexer')
{
return true;
}
// simple performance check to determine whether bot should process further
if (strpos($article->text, '{mautic') === false)
{
return true;
}
// Replace {mauticform with {mautic type="form"
$article->text = str_replace('{mauticform', '{mautic type="form"', $article->text);
preg_match_all($this->mauticRegex, $article->text, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
{
$atts = $this->parseShortcodeAtts($match[3]);
$method = 'do' . ucfirst(strtolower($atts['type'])) . 'Shortcode';
$newContent = '';
if (method_exists($this, $method))
{
$newContent = call_user_func(array($this, $method), $atts, $match[5]);
}
$article->text = str_replace($match[0], $newContent, $article->text);
}
}
/**
* Do a find/replace for Mautic forms
*
* @param array $atts
*
* @return string
*/
public function doFormShortcode($atts)
{
$id = isset($atts['id']) ? $atts['id'] : $atts[0];
return '<script type="text/javascript" src="' . trim($this->params->get('base_url'), " \t\n\r\0\x0B/") . '/form/generate.js?id=' . $id . '"></script>';
}
/**
* Do a find/replace for Mautic dynamic content
*
* @param array $atts
* @param string $content
*
* @return string
*/
public function doContentShortcode($atts, $content)
{
return '<div class="mautic-slot" data-slot-name="' . $atts['slot'] . '">' . $content . '</div>';
}
/**
* Do a find/replace for Mautic gated video
*
* @param array $atts
*
* @return string
*/
public function doVideoShortcode($atts)
{
$video_type = '';
$atts = $this->filterAtts(array(
'gate-time' => 15,
'form-id' => '',
'src' => '',
'width' => 640,
'height' => 360
), $atts);
if (empty($atts['src']))
{
return 'You must provide a video source. Add a src="URL" attribute to your shortcode. Replace URL with the source url for your video.';
}
if (empty($atts['form-id']))
{
return 'You must provide a mautic form id. Add a form-id="#" attribute to your shortcode. Replace # with the id of the form you want to use.';
}
if (preg_match('/^.*((youtu.be)|(youtube.com))\/((v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\??v?=?([^#\&\?]*).*/', $atts['src']))
{
$video_type = 'youtube';
}
if (preg_match('/^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/', $atts['src']))
{
$video_type = 'vimeo';
}
if (strtolower(substr($atts['src'], -3)) === 'mp4')
{
$video_type = 'mp4';
}
if (empty($video_type))
{
return 'Please use a supported video type. The supported types are youtube, vimeo, and MP4.';
}
return '<video height="' . $atts['height'] . '" width="' . $atts['width'] . '" data-form-id="' . $atts['form-id'] . '" data-gate-time="' . $atts['gate-time'] . '">' .
'<source type="video/' . $video_type . '" src="' . $atts['src'] . '" /></video>';
}
/**
* Mautic API call
*/
public function onAfterRoute()
{
$user = JFactory::getUser();
$app = JFactory::getApplication();
$input = $app->input;
$isRoot = $user->authorise('core.admin');
if ($input->get('plugin') == 'mautic')
{
$lang = JFactory::getLanguage();
$lang->load('plg_system_mautic', JPATH_ADMINISTRATOR);
if ($isRoot)
{
if ($input->get('authorize', false, 'BOOLEAN'))
{
$this->authorize($input->get('reauthorize', false, 'BOOLEAN'));
}
if ($input->get('reauthorize', false, 'BOOLEAN'))
{
$this->authorize(true);
}
if ($input->get('reset', false, 'BOOLEAN'))
{
$this->resetAccessToken();
}
}
else
{
$app->enqueueMessage(JText::_('PLG_MAUTIC_ERROR_ONLY_ADMIN_CAN_AUTHORIZE'), 'warning');
$this->log(JText::_('PLG_MAUTIC_ERROR_ONLY_ADMIN_CAN_AUTHORIZE'), JLog::ERROR);
}
}
if (($input->get('oauth_token') && $input->get('oauth_verifier'))
|| ($input->get('state') && $input->get('code')))
{
$this->authorize($input->get('reauthorize', false, 'BOOLEAN'));
}
}
/**
* Reset Access token so the plugin could authorize again
*
* @return void
*/
public function resetAccessToken()
{
$app = JFactory::getApplication();
$apiHelper = $this->getMauticApiHelper();
$table = $apiHelper->getTable();
$this->params->set('access_token', '');
$this->params->set('access_token_secret', '');
$this->params->set('access_token_expires', '');
$table->set('params', $this->params->toString());
$table->store();
$app->enqueueMessage(JText::_('PLG_MAUTIC_RESET_SUCCESSFUL'));
$this->log(JText::_('PLG_MAUTIC_RESET_SUCCESSFUL'), JLog::INFO);
$app->redirect(JRoute::_('index.php?option=com_plugins&view=plugin&layout=edit&extension_id=' . $table->get('extension_id'), false));
}
/**
* Create sanitized Mautic Base URL without the slash at the end.
*
* @return string
*/
public function getMauticApiHelper()
{
if ($this->apiHelper)
{
return $this->apiHelper;
}
$this->apiHelper = new mauticApiHelper;
return $this->apiHelper;
}
/**
* Get Table instance of this plugin
*
* @return JTableExtension
*/
public function authorize($reauthorize = false)
{
$app = JFactory::getApplication();
$apiHelper = $this->getMauticApiHelper();
$mauticBaseUrl = $apiHelper->getMauticBaseUrl();
$auth = $apiHelper->getMauticAuth($reauthorize);
$table = $apiHelper->getTable();
$lang = JFactory::getLanguage();
$lang->load('plg_system_mautic', JPATH_ADMINISTRATOR);
$this->log('Authorize method called.', JLog::INFO);
try
{
if ($auth->validateAccessToken())
{
if ($auth->accessTokenUpdated())
{
$accessTokenData = new JRegistry($auth->getAccessTokenData());
$this->log('authorize::accessTokenData: ' . var_export($accessTokenData, true), JLog::INFO);
$this->params->merge($accessTokenData);
$table = $apiHelper->getTable();
$table->set('params', $this->params->toString());
$table->store();
$extraWord = $reauthorize ? 'PLG_MAUTIC_REAUTHORIZED' : 'PLG_MAUTIC_AUTHORIZED';
$app->enqueueMessage(JText::sprintf('PLG_MAUTIC_REAUTHORIZE_SUCCESS', JText::_($extraWord)));
}
else
{
$app->enqueueMessage(JText::_('PLG_MAUTIC_REAUTHORIZE_NOT_NEEDED'));
$this->log(JText::_('PLG_MAUTIC_REAUTHORIZE_NOT_NEEDED'), JLog::INFO);
}
}
}
catch (Exception $e)
{
$app->enqueueMessage($e->getMessage(), 'error');
$this->log($e->getMessage(), JLog::ERROR);
}
$app->redirect(JRoute::_('index.php?option=com_plugins&view=plugin&layout=edit&extension_id=' . $table->get('extension_id'), false));
}
/**
* Create new lead on Joomla user registration
*
* For debug is better to switch function to:
* public function onUserBeforeSave($success, $isNew, $user)
*
* @param array $user array with user information
* @param boolean $isNew whether the user is new
* @param boolean $success whether the user was saved successfully
* @param string $msg error message
*/
public function onUserAfterSave($user, $isNew, $success, $msg = '')
{
$this->log('onUserAfterSave method called.', JLog::INFO);
$this->log('onUserAfterSave::isNew: ' . var_export($isNew, true), JLog::INFO);
$this->log('onUserAfterSave::success: ' . var_export($success, true), JLog::INFO);
$this->log('onUserAfterSave::send_registered: ' . var_export($this->params->get('send_registered'), true), JLog::INFO);
if ($isNew && $success && $this->params->get('send_registered') == 1)
{
$this->log('onUserAfterSave: Send the user to Mautic.', JLog::INFO);
try
{
$apiHelper = $this->getMauticApiHelper();
$mauticBaseUrl = $apiHelper->getMauticBaseUrl();
$auth = $apiHelper->getMauticAuth();
$leadApi = \Mautic\MauticApi::getContext("leads", $auth, $mauticBaseUrl . '/api/');
$ip = $this->getUserIP();
$name = explode(' ', $user['name']);
$mauticUser = array(
'ipAddress' => $ip,
'firstname' => isset($name[0]) ? $name[0] : '',
'lastname' => isset($name[1]) ? $name[1] : '',
'email' => $user['email'],
);
$this->log('onUserAfterSave::mauticUser: ' . var_export($mauticUser, true), JLog::INFO);
$result = $leadApi->create($mauticUser);
if (isset($result['error']))
{
$this->log('onUserAfterSave::leadApi::create - response: ' . $result['error']['code'] . ": " . $result['error']['message'], JLog::ERROR);
}
elseif (!empty($result['lead']['id']))
{
$this->log('onUserAfterSave: Mautic lead was successfully created with ID ' . $result['lead']['id'], JLog::INFO);
}
else
{
$this->log('onUserAfterSave: Mautic lead was NOT successfully created. ' . var_export($result, true), JLog::ERROR);
}
}
catch (Exception $e)
{
$this->log($e->getMessage(), JLog::ERROR);
}
}
else
{
$this->log('onUserAfterSave: Do not send the user to Mautic.', JLog::INFO);
}
}
/**
* Try to guess the real user IP address
*
* @return string
*/
public function getUserIP()
{
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Log helper function
*
* @return string
*/
public function log($msg, $type)
{
if ($this->params->get('log_on', 1) == 1)
{
JLog::add($msg, $type, 'plg_mautic');
}
}
/**
* Taken from WP wp_parse_shortcode_atts
*
* @param $text
*
* @return array|string
*/
private function parseShortcodeAtts($text)
{
$atts = array();
$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
if ( preg_match_all($this->attsRegex, $text, $match, PREG_SET_ORDER) ) {
foreach ($match as $m) {
if (!empty($m[1]))
$atts[strtolower($m[1])] = stripcslashes($m[2]);
elseif (!empty($m[3]))
$atts[strtolower($m[3])] = stripcslashes($m[4]);
elseif (!empty($m[5]))
$atts[strtolower($m[5])] = stripcslashes($m[6]);
elseif (isset($m[7]) && strlen($m[7]))
$atts[] = stripcslashes($m[7]);
elseif (isset($m[8]))
$atts[] = stripcslashes($m[8]);
}
// Reject any unclosed HTML elements
foreach( $atts as &$value ) {
if ( false !== strpos( $value, '<' ) ) {
if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
$value = '';
}
}
}
} else {
$atts = ltrim($text);
}
return $atts;
}
/**
* Taken fro WP wp_shortcode_atts
*
* @param array $pairs
* @param array $atts
*
* @return array
*/
private function filterAtts(array $pairs, array $atts)
{
$out = array();
foreach ($pairs as $name => $default) {
if (array_key_exists($name, $atts)) {
$out[$name] = $atts[$name];
} else {
$out[$name] = $default;
}
}
return $out;
}
}