|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Escopecz\MauticFormSubmit\Mautic; |
|
4
|
|
|
|
|
5
|
|
|
use Escopecz\MauticFormSubmit\Mautic; |
|
6
|
|
|
use Escopecz\MauticFormSubmit\Mautic\Cookie; |
|
7
|
|
|
use Escopecz\MauticFormSubmit\HttpHeader; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Mautic form |
|
11
|
|
|
*/ |
|
12
|
|
|
class Form |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Mautic |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $mautic; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Form ID |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $id; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor |
|
28
|
|
|
* |
|
29
|
|
|
* @param Mautic $mautic |
|
30
|
14 |
|
* @param int $id |
|
31
|
|
|
*/ |
|
32
|
14 |
|
public function __construct(Mautic $mautic, $id) |
|
33
|
14 |
|
{ |
|
34
|
14 |
|
$this->mautic = $mautic; |
|
35
|
|
|
$this->id = $id; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Submit the $data array to the Mautic form, using the optional $curlOpts |
|
40
|
|
|
* array to override curl settings |
|
41
|
|
|
* Returns array containing info about the request, response and cookie |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $data |
|
44
|
|
|
* @param array $curlOpts |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function submit(array $data, array $curlOpts = []) |
|
49
|
|
|
{ |
|
50
|
|
|
$originalCookie = $this->mautic->getCookie()->getSuperGlobalCookie(); |
|
51
|
|
|
$response = []; |
|
52
|
|
|
$request = $this->prepareRequest($data); |
|
53
|
|
|
|
|
54
|
|
|
$ch = curl_init($request['url']); |
|
55
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
56
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $request['query']); |
|
57
|
|
|
|
|
58
|
|
|
if (isset($request['header'])) { |
|
59
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $request['header']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (isset($request['referer'])) { |
|
63
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $request['referer']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($request['cookie'])) { |
|
67
|
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->mautic->getCookie()->createCookieFile()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
71
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, 1); |
|
72
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 1); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($curlOpts as $key => $value) { |
|
75
|
|
|
curl_setopt($ch, $key, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$result = curl_exec($ch); |
|
79
|
|
|
|
|
80
|
|
|
$response['header'] = null; |
|
81
|
|
|
$response['content'] = null; |
|
82
|
|
|
if (is_string($result) && strpos($result, "\r\n\r\n") !== false) { |
|
83
|
|
|
list($header, $content) = explode("\r\n\r\n", $result, 2); |
|
84
|
|
|
if (stripos($header, '100 Continue') !== false && strpos($content, "\r\n\r\n") !== false) { |
|
85
|
|
|
list($header, $content) = explode("\r\n\r\n", $content, 2); |
|
86
|
|
|
} |
|
87
|
|
|
$response['header'] = $header; |
|
88
|
|
|
$response['content'] = htmlentities($content); |
|
89
|
|
|
} |
|
90
|
|
|
$response['info'] = curl_getinfo($ch); |
|
91
|
|
|
curl_close($ch); |
|
92
|
|
|
|
|
93
|
|
|
$contact = $this->mautic->getContact(); |
|
94
|
|
|
$httpHeader = new HttpHeader($response['header']); |
|
95
|
|
|
$sessionId = $httpHeader->getCookieValue(Cookie::MAUTIC_SESSION_ID); |
|
96
|
|
|
$deviceId = $httpHeader->getCookieValue(Cookie::MAUTIC_DEVICE_ID); |
|
97
|
|
|
$contactId = $httpHeader->getCookieValue($sessionId); |
|
98
|
|
|
|
|
99
|
|
|
if ($sessionId) { |
|
|
|
|
|
|
100
|
2 |
|
$contact->setSessionId($sessionId); |
|
101
|
|
|
} |
|
102
|
2 |
|
|
|
103
|
2 |
|
if ($deviceId) { |
|
|
|
|
|
|
104
|
|
|
$contact->setDeviceId($deviceId); |
|
105
|
|
|
} |
|
106
|
2 |
|
|
|
107
|
|
|
if ($contactId) { |
|
|
|
|
|
|
108
|
2 |
|
$contact->setId((int)$contactId); |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
'original_cookie' => $originalCookie, |
|
113
|
|
|
'new_cookie' => $this->mautic->getCookie()->toArray(), |
|
114
|
|
|
'request' => $request, |
|
115
|
|
|
'response' => $response, |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Prepares data for CURL request based on provided form data, $_COOKIE and $_SERVER |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $data |
|
123
|
2 |
|
* |
|
124
|
|
|
* @return array |
|
125
|
2 |
|
*/ |
|
126
|
2 |
|
public function prepareRequest(array $data) |
|
127
|
|
|
{ |
|
128
|
|
|
$contact = $this->mautic->getContact(); |
|
129
|
2 |
|
$request = ['header' => []]; |
|
130
|
|
|
|
|
131
|
2 |
|
$data['formId'] = $this->id; |
|
132
|
2 |
|
|
|
133
|
|
|
// return has to be part of the form data array so Mautic would accept the submission |
|
134
|
|
|
if (!isset($data['return'])) { |
|
135
|
|
|
$data['return'] = ''; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$request['url'] = $this->getUrl(); |
|
139
|
|
|
$request['data'] = ['mauticform' => $data]; |
|
140
|
|
|
|
|
141
|
|
|
if ($contactId = $contact->getId()) { |
|
142
|
|
|
$request['data']['mtc_id'] = $contactId; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
if ($contactIp = $contact->getIp()) { |
|
146
|
|
|
$request['header'][] = "X-Forwarded-For: $contactIp"; |
|
147
|
2 |
|
$request['header'][] = "Client-Ip: $contactIp"; |
|
148
|
2 |
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
if ($sessionId = $contact->getSessionId()) { |
|
151
|
|
|
$request['header'][] = "Cookie: mautic_session_id=$sessionId"; |
|
152
|
|
|
$request['header'][] = "Cookie: mautic_device_id=$sessionId"; |
|
153
|
2 |
|
} |
|
154
|
2 |
|
|
|
155
|
|
|
if (isset($_SERVER['HTTP_REFERER'])) { |
|
156
|
|
|
$request['referer'] = $_SERVER["HTTP_REFERER"]; |
|
157
|
2 |
|
} |
|
158
|
2 |
|
|
|
159
|
|
|
$request['query'] = http_build_query($request['data']); |
|
160
|
2 |
|
|
|
161
|
|
|
return $request; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
2 |
|
/** |
|
165
|
|
|
* Builds the form URL |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
2 |
|
*/ |
|
169
|
|
|
public function getUrl() |
|
170
|
|
|
{ |
|
171
|
|
|
return sprintf('%s/form/submit?formId=%d', $this->mautic->getBaseUrl(), $this->id); |
|
172
|
2 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns the Form ID |
|
176
|
2 |
|
* |
|
177
|
|
|
* @return int |
|
178
|
2 |
|
*/ |
|
179
|
|
|
public function getId() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->id; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: