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
|
18 |
|
$mauticSessionId = $_COOKIE['mautic_session_id']; |
75
|
18 |
|
if (isset($_COOKIE[$mauticSessionId])) { |
76
|
2 |
|
return (int) $_COOKIE[$mauticSessionId]; |
77
|
|
|
} |
78
|
8 |
|
} |
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
|
2 |
|
public function getMauticSessionIdFromCookie() |
|
|
|
|
89
|
|
|
{ |
90
|
2 |
|
if (isset($_COOKIE['mautic_session_id'])) { |
91
|
2 |
|
return $_COOKIE['mautic_session_id']; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Guesses IP address from $_SERVER |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
32 |
|
public function getIpFromServer() |
|
|
|
|
103
|
|
|
{ |
104
|
32 |
|
$ip = ''; |
105
|
|
|
$ipHolders = [ |
106
|
32 |
|
'HTTP_CLIENT_IP', |
107
|
16 |
|
'HTTP_X_FORWARDED_FOR', |
108
|
16 |
|
'HTTP_X_FORWARDED', |
109
|
16 |
|
'HTTP_X_CLUSTER_CLIENT_IP', |
110
|
16 |
|
'HTTP_FORWARDED_FOR', |
111
|
16 |
|
'HTTP_FORWARDED', |
112
|
|
|
'REMOTE_ADDR' |
113
|
16 |
|
]; |
114
|
|
|
|
115
|
32 |
|
foreach ($ipHolders as $key) { |
116
|
32 |
|
if (!empty($_SERVER[$key])) { |
117
|
4 |
|
$ip = $_SERVER[$key]; |
118
|
4 |
|
if (strpos($ip, ',') !== false) { |
119
|
|
|
// Multiple IPs are present so use the last IP which should be |
120
|
|
|
// the most reliable IP that last connected to the proxy |
121
|
|
|
$ips = explode(',', $ip); |
122
|
|
|
array_walk($ips, create_function('&$val', '$val = trim($val);')); |
|
|
|
|
123
|
|
|
$ip = end($ips); |
124
|
|
|
} |
125
|
4 |
|
$ip = trim($ip); |
126
|
18 |
|
break; |
127
|
|
|
} |
128
|
16 |
|
} |
129
|
|
|
|
130
|
32 |
|
return $ip; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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: