1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Escopecz\MauticFormSubmit\Mautic; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Mautic Contact |
7
|
|
|
*/ |
8
|
|
|
class Contact |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Mautic contact ID |
12
|
|
|
* |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Mautic contact IP address |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $ip; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Mautic Session ID |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $sessionId; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param int $id will be taken from $_COOKIE if not provided |
35
|
|
|
* @param string $ip will be taken from $_SERVER if not provided |
36
|
|
|
* @param string $sessionId will be taken from $_COOKIE if not provided |
37
|
|
|
*/ |
38
|
38 |
|
public function __construct($id = null, $ip = null, $sessionId = null) |
39
|
|
|
{ |
40
|
38 |
|
if ($id === null) { |
41
|
36 |
|
$id = $this->getIdFromCookie(); |
42
|
18 |
|
} |
43
|
|
|
|
44
|
38 |
|
if ($ip === null) { |
45
|
36 |
|
$ip = $this->getIpFromServer(); |
46
|
18 |
|
} |
47
|
|
|
|
48
|
38 |
|
if ($sessionId === null) { |
49
|
38 |
|
$sessionId = $this->getMauticSessionIdFromCookie(); |
50
|
19 |
|
} |
51
|
|
|
|
52
|
38 |
|
$this->id = (int) $id; |
53
|
38 |
|
$this->ip = $ip; |
54
|
38 |
|
$this->sessionId = $sessionId; |
55
|
38 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns Contact ID |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
18 |
|
public function getId() |
63
|
|
|
{ |
64
|
18 |
|
return $this->id; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns Contact IP address |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
14 |
|
public function getIp() |
73
|
|
|
{ |
74
|
14 |
|
return $this->ip; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns Mautic Contact Session DI |
79
|
|
|
* |
80
|
|
|
* @return string|null |
81
|
|
|
*/ |
82
|
2 |
|
public function getSessionId() |
83
|
|
|
{ |
84
|
2 |
|
return $this->sessionId; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets Contact ID from $_COOKIE |
89
|
|
|
* |
90
|
|
|
* @return int|null |
91
|
|
|
*/ |
92
|
36 |
|
public function getIdFromCookie() |
|
|
|
|
93
|
|
|
{ |
94
|
36 |
|
if (isset($_COOKIE['mtc_id'])) { |
95
|
6 |
|
return (int) $_COOKIE['mtc_id']; |
96
|
34 |
|
} elseif (isset($_COOKIE['mautic_session_id'])) { |
97
|
2 |
|
$mauticSessionId = $_COOKIE['mautic_session_id']; |
98
|
2 |
|
if (isset($_COOKIE[$mauticSessionId])) { |
99
|
2 |
|
return (int) $_COOKIE[$mauticSessionId]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
32 |
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns Mautic session ID if it exists in the cookie |
108
|
|
|
* |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
38 |
|
public function getMauticSessionIdFromCookie() |
|
|
|
|
112
|
|
|
{ |
113
|
38 |
|
if (isset($_COOKIE['mautic_session_id'])) { |
114
|
6 |
|
return $_COOKIE['mautic_session_id']; |
115
|
|
|
} |
116
|
|
|
|
117
|
36 |
|
if (isset($_COOKIE['mtc_sid'])) { |
118
|
2 |
|
return $_COOKIE['mtc_sid']; |
119
|
|
|
} |
120
|
|
|
|
121
|
36 |
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set Mautic session ID to global cookie |
126
|
|
|
* |
127
|
|
|
* @param string $sessionId |
128
|
|
|
* |
129
|
|
|
* @return Contact |
130
|
|
|
*/ |
131
|
2 |
|
public function setSessionIdCookie($sessionId) |
|
|
|
|
132
|
|
|
{ |
133
|
2 |
|
$this->sessionId = $sessionId; |
134
|
2 |
|
$_COOKIE['mautic_session_id'] = $sessionId; |
135
|
2 |
|
$_COOKIE['mtc_sid'] = $sessionId; |
136
|
|
|
|
137
|
2 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set Mautic Contact ID to global cookie |
142
|
|
|
* |
143
|
|
|
* @param string $contactId |
144
|
|
|
* |
145
|
|
|
* @return Contact |
146
|
|
|
*/ |
147
|
2 |
|
public function setIdCookie($contactId) |
|
|
|
|
148
|
|
|
{ |
149
|
2 |
|
$this->id = $contactId; |
|
|
|
|
150
|
|
|
|
151
|
2 |
|
$_COOKIE['mtc_id'] = $contactId; |
152
|
|
|
|
153
|
2 |
|
if ($this->sessionId) { |
154
|
|
|
$_COOKIE[$this->sessionId] = $contactId; |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Guesses IP address from $_SERVER |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
36 |
|
public function getIpFromServer() |
|
|
|
|
166
|
|
|
{ |
167
|
36 |
|
$ip = ''; |
168
|
|
|
$ipHolders = [ |
169
|
36 |
|
'HTTP_CLIENT_IP', |
170
|
18 |
|
'HTTP_X_FORWARDED_FOR', |
171
|
18 |
|
'HTTP_X_FORWARDED', |
172
|
18 |
|
'HTTP_X_CLUSTER_CLIENT_IP', |
173
|
18 |
|
'HTTP_FORWARDED_FOR', |
174
|
18 |
|
'HTTP_FORWARDED', |
175
|
|
|
'REMOTE_ADDR' |
176
|
18 |
|
]; |
177
|
|
|
|
178
|
36 |
|
foreach ($ipHolders as $key) { |
179
|
36 |
|
if (!empty($_SERVER[$key])) { |
180
|
4 |
|
$ip = $_SERVER[$key]; |
181
|
4 |
|
if (strpos($ip, ',') !== false) { |
182
|
|
|
// Multiple IPs are present so use the last IP which should be |
183
|
|
|
// the most reliable IP that last connected to the proxy |
184
|
|
|
$ips = explode(',', $ip); |
185
|
|
|
array_walk($ips, create_function('&$val', '$val = trim($val);')); |
|
|
|
|
186
|
|
|
$ip = end($ips); |
187
|
|
|
} |
188
|
4 |
|
$ip = trim($ip); |
189
|
20 |
|
break; |
190
|
|
|
} |
191
|
18 |
|
} |
192
|
|
|
|
193
|
36 |
|
return $ip; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: