From a1e047397a43babbb19c82aeaac64a8d8e77597c Mon Sep 17 00:00:00 2001 From: John Linhart Date: Wed, 4 Jul 2018 10:53:51 +0200 Subject: [PATCH] Update to track by device_id instead of session_id cookie --- src/HttpHeader.php | 73 +++++++++++++++++++++++++++++++++++++++ src/Mautic/Contact.php | 14 ++++++++ src/Mautic/Cookie.php | 21 +++++++++++ src/Mautic/Form.php | 60 +++++++------------------------- tests/HttpHeaderTest.php | 35 +++++++++++++++++++ tests/Mautic/FormTest.php | 32 ----------------- 6 files changed, 156 insertions(+), 79 deletions(-) create mode 100644 src/HttpHeader.php create mode 100644 tests/HttpHeaderTest.php diff --git a/src/HttpHeader.php b/src/HttpHeader.php new file mode 100644 index 0000000..5b68a5a --- /dev/null +++ b/src/HttpHeader.php @@ -0,0 +1,73 @@ +parse($textHeaders); + } + + /** + * @param string $key + * + * @return string|null + */ + public function getHeaderValue($key) + { + return isset($this->headers[$key]) ? $this->headers[$key] : null; + } + + /** + * @param string $key + * + * @return string|null + */ + public function getCookieValue($key) + { + return isset($this->cookies[$key]) ? $this->cookies[$key] : null; + } + + /** + * Parse text headers and fills in cookies and headers properites + * + * @param string $headers + */ + private function parse($headers) + { + foreach (preg_split('/\r\n|\r|\n/', $headers) as $i => $line) { + if ($i === 0) { + $this->headers['http_code'] = $line; + } else { + list($key, $value) = explode(': ', $line); + + if ($key === 'Set-Cookie') { + list($textCookie) = explode(';', $value); + list($cookieKey, $cookieValue) = explode('=', $textCookie); + + $this->cookies[$cookieKey] = $cookieValue; + } else { + $this->headers[$key] = $value; + } + } + } + } +} diff --git a/src/Mautic/Contact.php b/src/Mautic/Contact.php index 6b22275..35f6ace 100644 --- a/src/Mautic/Contact.php +++ b/src/Mautic/Contact.php @@ -104,6 +104,20 @@ public function setSessionId($sessionId) return $this; } + /** + * Set Mautic device ID to global cookie + * + * @param string $deviceId + * + * @return Contact + */ + public function setDeviceId($deviceId) + { + $this->cookie->setDeviceId($deviceId); + + return $this; + } + /** * Guesses IP address from $_SERVER * diff --git a/src/Mautic/Cookie.php b/src/Mautic/Cookie.php index 404fecd..db313e8 100644 --- a/src/Mautic/Cookie.php +++ b/src/Mautic/Cookie.php @@ -9,6 +9,13 @@ */ class Cookie extends StandardCookie { + /** + * Holds Mautic session ID defined by PHP + * + * @var string + */ + const MAUTIC_DEVICE_ID = 'mautic_device_id'; + /** * Holds Mautic session ID defined by PHP * @@ -114,6 +121,20 @@ public function setSessionId($sessionId) return $this; } + /** + * Set Mautic Device ID cookies + * + * @param string $deviceId + * + * @return Cookie + */ + public function setDeviceId($deviceId) + { + $this->set(self::MAUTIC_DEVICE_ID, $deviceId); + + return $this; + } + /** * Unset Mautic Session ID cookies * diff --git a/src/Mautic/Form.php b/src/Mautic/Form.php index 8b65df6..489b07e 100644 --- a/src/Mautic/Form.php +++ b/src/Mautic/Form.php @@ -3,6 +3,8 @@ namespace Escopecz\MauticFormSubmit\Mautic; use Escopecz\MauticFormSubmit\Mautic; +use Escopecz\MauticFormSubmit\Mautic\Cookie; +use Escopecz\MauticFormSubmit\HttpHeader; /** * Mautic form @@ -80,12 +82,20 @@ public function submit(array $data, array $curlOpts = []) curl_close($ch); $contact = $this->mautic->getContact(); + $httpHeader = new HttpHeader($response['header']); + $sessionId = $httpHeader->getCookieValue(Cookie::MAUTIC_SESSION_ID); + $deviceId = $httpHeader->getCookieValue(Cookie::MAUTIC_DEVICE_ID); + $contactId = $httpHeader->getCookieValue($sessionId); - if ($sessionId = $this->getSessionIdFromHeader($response['header'])) { + if ($sessionId) { $contact->setSessionId($sessionId); } - if ($contactId = $this->getContactIdFromHeader($response['header'], $sessionId)) { + if ($deviceId) { + $contact->setDeviceId($deviceId); + } + + if ($contactId) { $contact->setId($contactId); } @@ -97,51 +107,6 @@ public function submit(array $data, array $curlOpts = []) ]; } - /** - * Finds the session ID hash in the response header - * - * @param string $headers - * - * @return string|null - */ - public function getSessionIdFromHeader($headers) - { - if (!$headers) { - return null; - } - - preg_match("/mautic_session_id=(.+?);/", $headers, $matches); - - if (isset($matches[1])) { - return $matches[1]; - } - - return null; - } - - /** - * Finds the Mautic Contact ID hash in the response header - * - * @param string $headers - * @param string $sessionId - * - * @return int|null - */ - public function getContactIdFromHeader($headers, $sessionId) - { - if (!$headers || !$sessionId) { - return null; - } - - preg_match("/$sessionId=(.+?);/", $headers, $matches); - - if (isset($matches[1])) { - return (int) $matches[1]; - } - - return null; - } - /** * Prepares data for CURL request based on provided form data, $_COOKIE and $_SERVER * @@ -175,6 +140,7 @@ public function prepareRequest(array $data) if ($sessionId = $contact->getSessionId()) { $request['header'][] = "Cookie: mautic_session_id=$sessionId"; + $request['header'][] = "Cookie: mautic_device_id=$sessionId"; } if (isset($_SERVER['HTTP_REFERER'])) { diff --git a/tests/HttpHeaderTest.php b/tests/HttpHeaderTest.php new file mode 100644 index 0000000..432288d --- /dev/null +++ b/tests/HttpHeaderTest.php @@ -0,0 +1,35 @@ +testTextHeader); + $this->assertEquals('6txmz3mu2dslmkhrera668e', $headerHelper->getCookieValue('mautic_session_id')); + $this->assertEquals('18061', $headerHelper->getCookieValue('mtc_id')); + $this->assertEquals('18061', $headerHelper->getCookieValue('6txmz3mu2dslmkhrera668e')); + $this->assertEquals('Apache/2.4.33 (Unix) OpenSSL/1.0.2o PHP/7.1.16', $headerHelper->getHeaderValue('Server')); + } +} diff --git a/tests/Mautic/FormTest.php b/tests/Mautic/FormTest.php index 631ccf2..7c15cb8 100644 --- a/tests/Mautic/FormTest.php +++ b/tests/Mautic/FormTest.php @@ -9,18 +9,6 @@ class FormTest extends \PHPUnit_Framework_TestCase { private $baseUrl = 'https://mymautic.com'; - private $header = 'HTTP/1.1 302 Found -Date: Wed, 26 Apr 2017 06:08:08 GMT -Server: Apache/2.4.25 (Unix) OpenSSL/0.9.8zh PHP/7.0.15 -X-Powered-By: PHP/7.0.15 -Set-Cookie: 9743595cf0a472cb3ec0272949ffe7e8=67ma3ug969qnk5so4u6982mua0; path=/; HttpOnly -Set-Cookie: mautic_session_id=0435e490c376144325baa1a278c483cf071f92bf; expires=Thu, 26-Apr-2018 06:08:09 GMT; Max-Age=31536000; path=/ -Set-Cookie: 0435e490c376144325baa1a278c483cf071f92bf=4491; expires=Thu, 26-Apr-2018 06:08:09 GMT; Max-Age=31536000; path=/ -Cache-Control: no-cache -Location: http://localhost/mautic-form-submit/examples/simple-email-form/ -Content-Length: 496 -Content-Type: text/html; charset=UTF-8'; - function test_get_id_int_standalone() { $mautic = new Mautic($this->baseUrl); @@ -67,24 +55,4 @@ function test_get_url() $this->assertSame($this->baseUrl.'/form/submit?formId='.$formId, $form->getUrl()); } - - function test_get_session_id_from_header() - { - $mautic = new Mautic($this->baseUrl); - $form = $mautic->getForm(3434); - $sessionId = $form->getSessionIdFromHeader($this->header); - - $this->assertSame('0435e490c376144325baa1a278c483cf071f92bf', $sessionId); - $this->assertSame(null, $form->getSessionIdFromHeader('')); - } - - function test_get_contact_id_from_request() - { - $mautic = new Mautic($this->baseUrl); - $form = $mautic->getForm(3434); - $contactId = $form->getContactIdFromHeader($this->header, '0435e490c376144325baa1a278c483cf071f92bf'); - - $this->assertSame(4491, $contactId); - $this->assertSame(null, $form->getContactIdFromHeader('', '')); - } }