forked from mautic/mautic-joomla
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmauticApiHelper.php
141 lines (125 loc) · 3.1 KB
/
mauticApiHelper.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
<?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' );
/**
* Helper class which initialize Mautic API library with needed data.
* It is stand-alone class with no dependencies so it can be used from
* other extensions easily.
*/
class mauticApiHelper
{
/**
* JTableExtension
*/
protected $table;
/**
* JRegistry
*/
protected $params;
/**
* Constructor initialize necessary variables
*
* @return void
*/
public function __construct()
{
$this->table = $this->getTable();
$this->params = new JRegistry($this->table->get('params'));
/*
* Register the autoloader for the Mautic library
*
* Joomla! 3.2 has a native namespace capable autoloader so prefer that and use our fallback loader for older versions
*/
if (version_compare(JVERSION, '3.2', 'ge'))
{
JLoader::registerNamespace('Mautic', __DIR__ . '/lib');
}
else
{
include_once __DIR__ . '/lib/Mautic/AutoLoader.php';
}
}
/**
* Create sanitized Mautic Base URL without the slash at the end.
*
* @return string
*/
public function getMauticBaseUrl()
{
return trim($this->params->get('base_url'), " \t\n\r\0\x0B/");
}
/**
* Get Table instance of this plugin
*
* @return JTableExtension
*/
public function getTable()
{
if ($this->table)
{
return $this->table;
}
$table = JTable::getInstance('Extension', 'JTable', array('dbo' => JFactory::getDBO()));
$table->load(array('element' => 'mautic', 'folder' => 'system'));
return $table;
}
/**
* Create settings needed for Mautic authentication
*
* @return array
*/
public function getApiSettings()
{
$settings = array(
'baseUrl' => $this->getMauticBaseUrl(),
'version' => $this->params->get('oauth_version'),
'clientKey' => $this->params->get('public_key'),
'clientSecret' => $this->params->get('secret_key'),
'callback' => JURI::root() . 'administrator'
);
if ($this->params->get('access_token'))
{
$settings['accessToken'] = $this->params->get('access_token');
$settings['accessTokenSecret'] = $this->params->get('access_token_secret');
$settings['accessTokenExpires'] = $this->params->get('access_token_expires', $this->params->get('expires'));
$settings['refreshToken'] = $this->params->get('refresh_token');
}
return $settings;
}
/**
* Returns params of Mautic plugin
*
* @return JRegistry
*/
public function getPluginParams()
{
return $this->params;
}
/**
* Initiate Auth object
*
* @return string
*/
public function getMauticAuth($clearAccessToken = false)
{
$settings = $this->getApiSettings();
if ($clearAccessToken)
{
unset($settings['accessToken']);
unset($settings['accessTokenSecret']);
unset($settings['accessTokenExpires']);
unset($settings['refreshToken']);
unset($_SESSION['OAuth1a']);
unset($_SESSION['OAuth2']);
unset($_SESSION['oauth']);
}
return \Mautic\Auth\ApiAuth::initiate($settings);
}
}