Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DeviaVir/Mobile-Detect in…
Browse files Browse the repository at this point in the history
…to devel
  • Loading branch information
serbanghita committed Jun 18, 2015
2 parents 30459d2 + d56bc93 commit c375448
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
73 changes: 72 additions & 1 deletion Mobile_Detect.php
Expand Up @@ -96,6 +96,12 @@ class Mobile_Detect
*/
protected $httpHeaders = array();

/**
* CloudFront headers. E.g. CloudFront-Is-Desktop-Viewer, CloudFront-Is-Mobile-Viewer & CloudFront-Is-Tablet-Viewer.
* @var array
*/
protected $cloudfrontHeaders = array();

/**
* The matching Regex.
* This is good for debug.
Expand Down Expand Up @@ -680,6 +686,9 @@ public function setHttpHeaders($httpHeaders = null)
$this->httpHeaders[$key] = $value;
}
}

// In case we're dealing with CloudFront, we need to know.
$this->setCfHeaders($httpHeaders);
}

/**
Expand Down Expand Up @@ -739,6 +748,46 @@ public function getUaHttpHeaders()
return self::$uaHttpHeaders;
}


/**
* Set CloudFront headers
*
* @param array $cfHeaders List of HTTP headers
*
* @return boolean If there were CloudFront headers to be set
*/
public function setCfHeaders($cfHeaders = null) {
// use global _SERVER if $cfHeaders aren't defined
if (!is_array($cfHeaders) || !count($cfHeaders)) {
$cfHeaders = $_SERVER;
}

// clear existing headers
$this->cloudfrontHeaders = array();

// Only save CLOUDFRONT headers. In PHP land, that means only _SERVER vars that
// start with cloudfront-.
$response = false;
foreach ($cfHeaders as $key => $value) {
if (substr(strtolower($key), 0, 16) === 'http_cloudfront_') {
$this->cloudfrontHeaders[strtoupper($key)] = $value;
$response = true;
}
}

return $response;
}

/**
* Retrieves the cloudfront headers.
*
* @return array
*/
public function getCfHeaders()
{
return $this->cloudfrontHeaders;
}

/**
* Set the User-Agent to be used.
*
Expand All @@ -761,9 +810,15 @@ public function setUserAgent($userAgent = null)
}
}

return $this->userAgent = (!empty($this->userAgent) ? trim($this->userAgent) : null);
if (!empty($this->userAgent)) {
return $this->userAgent = trim($this->userAgent);
}
}

if (count($this->getCfHeaders()) > 0) {
return $this->userAgent = 'Amazon CloudFront';
}
return $this->userAgent = null;
}

/**
Expand Down Expand Up @@ -1059,6 +1114,14 @@ public function isMobile($userAgent = null, $httpHeaders = null)
$this->setUserAgent($userAgent);
}

// Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
if ($this->getUserAgent() === 'Amazon CloudFront') {
$cfHeaders = $this->getCfHeaders();
if(array_key_exists('HTTP_CLOUDFRONT_IS_MOBILE_VIEWER', $cfHeaders) && $cfHeaders['HTTP_CLOUDFRONT_IS_MOBILE_VIEWER'] === 'true') {
return true;
}
}

$this->setDetectionType(self::DETECTION_TYPE_MOBILE);

if ($this->checkHttpHeadersForMobile()) {
Expand All @@ -1079,6 +1142,14 @@ public function isMobile($userAgent = null, $httpHeaders = null)
*/
public function isTablet($userAgent = null, $httpHeaders = null)
{
// Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
if ($this->getUserAgent() === 'Amazon CloudFront') {
$cfHeaders = $this->getCfHeaders();
if(array_key_exists('HTTP_CLOUDFRONT_IS_TABLET_VIEWER', $cfHeaders) && $cfHeaders['HTTP_CLOUDFRONT_IS_TABLET_VIEWER'] === 'true') {
return true;
}
}

$this->setDetectionType(self::DETECTION_TYPE_MOBILE);

foreach (self::$tabletDevices as $_regex) {
Expand Down
48 changes: 48 additions & 0 deletions tests/BasicsTest.php
Expand Up @@ -179,6 +179,54 @@ public function testSetHttpHeaders()
$this->assertSame($md->getHttpHeaders(), $header2);
}

/**
* Read response from cloudfront, if the cloudfront headers are detected
* @covers Mobile_Detect::setCfHeaders
*/
public function testSetCfHeaders()
{
// Test mobile detected
$header1 = array(
'HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER' => 'false',
'HTTP_CLOUDFRONT_IS_MOBILE_VIEWER' => 'true',
'HTTP_CLOUDFRONT_IS_TABLET_VIEWER' => 'false'
);
$md = new Mobile_Detect($header1);
$this->assertSame($md->getCfHeaders(), $header1);
$this->assertSame($md->getUserAgent(), 'Amazon CloudFront');
$this->assertSame($md->isTablet(), false);
$this->assertSame($md->isMobile(), true);

// Test neither mobile nor tablet (desktop)
$header2 = array(
'HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER' => 'true',
'HTTP_CLOUDFRONT_IS_MOBILE_VIEWER' => 'false',
'HTTP_CLOUDFRONT_IS_TABLET_VIEWER' => 'false'
);
$md->setHttpHeaders($header2);
$this->assertSame($md->getCfHeaders(), $header2);
$this->assertSame($md->getUserAgent(), 'Amazon CloudFront');
$this->assertSame($md->isTablet(), false);
$this->assertSame($md->isMobile(), false);

// Test tablet detected
$header3 = array(
'HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER' => 'false',
'HTTP_CLOUDFRONT_IS_MOBILE_VIEWER' => 'false',
'HTTP_CLOUDFRONT_IS_TABLET_VIEWER' => 'true'
);
$md->setCfHeaders($header3);
$this->assertSame($md->getCfHeaders(), $header3);
$this->assertSame($md->getUserAgent(), 'Amazon CloudFront');
$this->assertSame($md->isTablet(), true);
$this->assertSame($md->isMobile(), false);

// Check if the headers are cleared
$header4 = array();
$md->setHttpHeaders($header4);
$this->assertSame($md->getCfHeaders(), $header4);
}

/**
* @covers Mobile_Detect::setUserAgent
* @covers Mobile_Detect::getUserAgent
Expand Down

0 comments on commit c375448

Please sign in to comment.