1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Escopecz\MauticFormSubmit\Mautic; |
4
|
|
|
|
5
|
|
|
use Escopecz\MauticFormSubmit\Mautic\Cookie; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Mautic Contact |
9
|
|
|
*/ |
10
|
|
|
class Contact |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Mautic contact IP address |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $ip; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Mautic Cookie |
21
|
|
|
* |
22
|
|
|
* @var Cookie |
23
|
|
|
*/ |
24
|
|
|
protected $cookie; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param Cookie $cookie |
30
|
|
|
*/ |
31
|
38 |
|
public function __construct(Cookie $cookie) |
32
|
|
|
{ |
33
|
38 |
|
$this->cookie = $cookie; |
34
|
38 |
|
$this->ip = $this->getIpFromServer(); |
35
|
38 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns Contact ID |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
16 |
|
public function getId() |
43
|
|
|
{ |
44
|
16 |
|
return (int) $this->cookie->getContactId(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set Mautic Contact ID to global cookie |
49
|
|
|
* |
50
|
|
|
* @param int $contactId |
51
|
|
|
* |
52
|
|
|
* @return Contact |
53
|
|
|
*/ |
54
|
6 |
|
public function setId($contactId) |
55
|
|
|
{ |
56
|
6 |
|
$this->cookie->setContactId($contactId); |
57
|
|
|
|
58
|
6 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns Contact IP address |
63
|
|
|
* |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
12 |
|
public function getIp() |
67
|
|
|
{ |
68
|
12 |
|
return $this->ip; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sert Contact IP address |
73
|
|
|
* |
74
|
|
|
* @param string $ip |
75
|
|
|
* |
76
|
|
|
* @return Contact |
77
|
|
|
*/ |
78
|
4 |
|
public function setIp($ip) |
79
|
|
|
{ |
80
|
4 |
|
$this->ip = $ip; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns Mautic Contact Session ID |
85
|
|
|
* |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
4 |
|
public function getSessionId() |
89
|
|
|
{ |
90
|
4 |
|
return $this->cookie->getSessionId(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set Mautic session ID to global cookie |
95
|
|
|
* |
96
|
|
|
* @param string $sessionId |
97
|
|
|
* |
98
|
|
|
* @return Contact |
99
|
|
|
*/ |
100
|
2 |
|
public function setSessionId($sessionId) |
101
|
|
|
{ |
102
|
2 |
|
$this->cookie->setSessionId($sessionId); |
103
|
|
|
|
104
|
2 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Guesses IP address from $_SERVER |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
38 |
|
public function getIpFromServer() |
|
|
|
|
113
|
|
|
{ |
114
|
38 |
|
$ip = ''; |
115
|
|
|
$ipHolders = [ |
116
|
38 |
|
'HTTP_CLIENT_IP', |
117
|
|
|
'HTTP_X_FORWARDED_FOR', |
118
|
|
|
'HTTP_X_FORWARDED', |
119
|
|
|
'HTTP_X_CLUSTER_CLIENT_IP', |
120
|
|
|
'HTTP_FORWARDED_FOR', |
121
|
|
|
'HTTP_FORWARDED', |
122
|
|
|
'REMOTE_ADDR' |
123
|
|
|
]; |
124
|
|
|
|
125
|
38 |
|
foreach ($ipHolders as $key) { |
126
|
38 |
|
if (!empty($_SERVER[$key])) { |
127
|
4 |
|
$ip = $_SERVER[$key]; |
128
|
4 |
|
if (strpos($ip, ',') !== false) { |
129
|
|
|
// Multiple IPs are present so use the last IP which should be |
130
|
|
|
// the most reliable IP that last connected to the proxy |
131
|
|
|
$ips = explode(',', $ip); |
132
|
|
|
array_walk($ips, create_function('&$val', '$val = trim($val);')); |
|
|
|
|
133
|
|
|
$ip = end($ips); |
134
|
|
|
} |
135
|
4 |
|
$ip = trim($ip); |
136
|
38 |
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
38 |
|
return $ip; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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: