|
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
|
|
|
* Constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param int $id will be taken from $_COOKIE if not provided |
|
28
|
|
|
* @param string $ip will be taken from $_SERVER if not provided |
|
29
|
|
|
*/ |
|
30
|
34 |
|
public function __construct($id = null, $ip = null) |
|
31
|
|
|
{ |
|
32
|
34 |
|
if ($id === null) { |
|
33
|
32 |
|
$id = $this->getIdFromCookie(); |
|
34
|
16 |
|
} |
|
35
|
|
|
|
|
36
|
34 |
|
if ($ip === null) { |
|
37
|
32 |
|
$ip = $this->getIpFromServer(); |
|
38
|
16 |
|
} |
|
39
|
|
|
|
|
40
|
34 |
|
$this->id = (int) $id; |
|
41
|
34 |
|
$this->ip = $ip; |
|
42
|
34 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns Contact ID |
|
46
|
|
|
* |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
16 |
|
public function getId() |
|
50
|
|
|
{ |
|
51
|
16 |
|
return $this->id; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns Contact IP address |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
14 |
|
public function getIp() |
|
60
|
|
|
{ |
|
61
|
14 |
|
return $this->ip; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets Contact ID from $_COOKIE |
|
66
|
|
|
* |
|
67
|
|
|
* @return int|null |
|
68
|
|
|
*/ |
|
69
|
32 |
|
public function getIdFromCookie() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
32 |
|
if (isset($_COOKIE['mtc_id'])) { |
|
72
|
4 |
|
return (int) $_COOKIE['mtc_id']; |
|
73
|
30 |
|
} elseif (isset($_COOKIE['mautic_session_id'])) { |
|
74
|
2 |
|
$mauticSessionId = $_COOKIE['mautic_session_id']; |
|
75
|
2 |
|
if (isset($_COOKIE[$mauticSessionId])) { |
|
76
|
2 |
|
return (int) $_COOKIE[$mauticSessionId]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
28 |
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns Mautic session ID if it exists in the cookie |
|
85
|
|
|
* |
|
86
|
|
|
* @return string|null |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getMauticSessionIdFromCookie() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
4 |
|
if (isset($_COOKIE['mautic_session_id'])) { |
|
91
|
2 |
|
return $_COOKIE['mautic_session_id']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
if (isset($_COOKIE['mtc_sid'])) { |
|
95
|
2 |
|
return $_COOKIE['mtc_sid']; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Guesses IP address from $_SERVER |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
32 |
|
public function getIpFromServer() |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
32 |
|
$ip = ''; |
|
109
|
|
|
$ipHolders = [ |
|
110
|
32 |
|
'HTTP_CLIENT_IP', |
|
111
|
16 |
|
'HTTP_X_FORWARDED_FOR', |
|
112
|
16 |
|
'HTTP_X_FORWARDED', |
|
113
|
16 |
|
'HTTP_X_CLUSTER_CLIENT_IP', |
|
114
|
16 |
|
'HTTP_FORWARDED_FOR', |
|
115
|
16 |
|
'HTTP_FORWARDED', |
|
116
|
|
|
'REMOTE_ADDR' |
|
117
|
16 |
|
]; |
|
118
|
|
|
|
|
119
|
32 |
|
foreach ($ipHolders as $key) { |
|
120
|
32 |
|
if (!empty($_SERVER[$key])) { |
|
121
|
4 |
|
$ip = $_SERVER[$key]; |
|
122
|
4 |
|
if (strpos($ip, ',') !== false) { |
|
123
|
|
|
// Multiple IPs are present so use the last IP which should be |
|
124
|
|
|
// the most reliable IP that last connected to the proxy |
|
125
|
|
|
$ips = explode(',', $ip); |
|
126
|
|
|
array_walk($ips, create_function('&$val', '$val = trim($val);')); |
|
|
|
|
|
|
127
|
|
|
$ip = end($ips); |
|
128
|
|
|
} |
|
129
|
4 |
|
$ip = trim($ip); |
|
130
|
18 |
|
break; |
|
131
|
|
|
} |
|
132
|
16 |
|
} |
|
133
|
|
|
|
|
134
|
32 |
|
return $ip; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
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: