Skip to content

Commit

Permalink
Update to track by device_id instead of session_id cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
escopecz committed Jul 4, 2018
1 parent 5e45aca commit a1e0473
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 79 deletions.
73 changes: 73 additions & 0 deletions src/HttpHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Escopecz\MauticFormSubmit;

/**
* HTTP Header Helper
*/
class HttpHeader
{
/**
* Key-valye paries of headers
*
* @var array
*/
private $headers = [];

/**
* Key-valye paries of cookies
*
* @var array
*/
private $cookies = [];

public function __construct($textHeaders)
{
$this->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;
}
}
}
}
}
14 changes: 14 additions & 0 deletions src/Mautic/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
21 changes: 21 additions & 0 deletions src/Mautic/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
60 changes: 13 additions & 47 deletions src/Mautic/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Escopecz\MauticFormSubmit\Mautic;

use Escopecz\MauticFormSubmit\Mautic;
use Escopecz\MauticFormSubmit\Mautic\Cookie;
use Escopecz\MauticFormSubmit\HttpHeader;

/**
* Mautic form
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
*
Expand Down Expand Up @@ -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'])) {
Expand Down
35 changes: 35 additions & 0 deletions tests/HttpHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Escopecz\MauticFormSubmit\Test;

use Escopecz\MauticFormSubmit\HttpHeader;

class HttpHeaderTest extends \PHPUnit_Framework_TestCase
{
private $testTextHeader = 'HTTP/1.1 302 Found
Date: Wed, 04 Jul 2018 08:33:39 GMT
Server: Apache/2.4.33 (Unix) OpenSSL/1.0.2o PHP/7.1.16
X-Powered-By: PHP/7.1.16
Set-Cookie: 10b307a255ccdba6a2d32498b0c61978=5329avlkmibr4galpllvo1rmf7; path=/; HttpOnly
Set-Cookie: mautic_session_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/
Set-Cookie: mautic_device_id=6txmz3mu2dslmkhrera668e; expires=Thu, 04-Jul-2019 08:33:39 GMT; Max-Age=31536000; path=/
Set-Cookie: mtc_id=18061; path=/
Set-Cookie: mtc_sid=6txmz3mu2dslmkhrera668e; path=/
Set-Cookie: mautic_session_id=6txmz3mu2dslmkhrera668e; expires=Thu, 04-Jul-2019 08:33:39 GMT; Max-Age=31536000; path=/
Set-Cookie: 6txmz3mu2dslmkhrera668e=18061; expires=Thu, 04-Jul-2019 08:33:39 GMT; Max-Age=31536000; path=/
Cache-Control: no-cache
Location: http://localhost/mautic-form-submit/examples/simple-email-form/?mauticMessage=Hello%21
X-Debug-Token: 152d17
X-Debug-Token-Link: http://mautic.test/index_dev.php/_profiler/152d17
Content-Length: 588
Content-Type: text/html; charset=UTF-8';

function test_get_base_url()
{
$headerHelper = new HttpHeader($this->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'));
}
}
32 changes: 0 additions & 32 deletions tests/Mautic/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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('', ''));
}
}

0 comments on commit a1e0473

Please sign in to comment.