Commit 5bb48c23 authored by Gaurav Kapoor's avatar Gaurav Kapoor Committed by Gaurav Kapoor
Browse files

Issue #3264232 by gaurav.kapoor: Use matomo-org device-detector library to get...

Issue #3264232 by gaurav.kapoor: Use matomo-org device-detector library to get browser and device information
parent 2d97ea4a
Loading
Loading
Loading
Loading

composer.json

0 → 100644
+10 −0
Original line number Diff line number Diff line
{
    "name": "drupal/counter",
    "type": "drupal-module",
    "description": "This module used for displaying a Site Counter, Node Count, Unique Visitor and Client IP.",
    "homepage": "http://drupal.org/project/counter",
    "license": "GPL-2.0-or-later",
    "require": {
        "matomo-org/device-detector": "^5.0"
    }
}
+17 −66
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * The counter module used for displaying Site Counter.
 */

use DeviceDetector\DeviceDetector;
use Drupal\Core\Routing\RouteMatchInterface;

/**
@@ -24,82 +25,32 @@ function counter_help($route_name, RouteMatchInterface $route_match) {
 * To find out browser type and return browser info.
 */
function counter_get_browser() {
  // Using device detector library to get user agent information.
  $user_agent = \Drupal::request()->server->get('HTTP_USER_AGENT');
  $device_detector = new DeviceDetector($user_agent);
  $device_detector->parse();

  $browser_name    = 'Unknown';
  $browser_version = '';
  $platform        = 'Unknown';
  $ub              = 'Unknown';

  // First get the platform?
  if (preg_match('/linux/i', $user_agent)) {
    $platform = 'Linux';
  }
  elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {
    $platform = 'Mac';
  }
  elseif (preg_match('/windows|win32/i', $user_agent)) {
    $platform = 'Windows';
  }

  // Next get the name of the useragent yes separately and for good reason.
  if (preg_match('/MSIE/i', $user_agent) && !preg_match('/Opera/i', $user_agent)) {
    $browser_name = 'Internet Explorer';
    $ub = "IE";
  }
  elseif (preg_match('/Firefox/i', $user_agent)) {
    $browser_name = 'Mozilla Firefox';
    $ub = "Firefox";
  }
  elseif (preg_match('/Chrome/i', $user_agent)) {
    $browser_name = 'Google Chrome';
    $ub = "Chrome";
  }
  elseif (preg_match('/Safari/i', $user_agent)) {
    $browser_name = 'Apple Safari';
    $ub = "Safari";
  }
  elseif (preg_match('/Opera/i', $user_agent)) {
    $browser_name = 'Opera';
    $ub = "Opera";
  }
  elseif (preg_match('/Netscape/i', $user_agent)) {
    $browser_name = 'Netscape';
    $ub = "Netscape";
  }

  // Finally get the correct browser_version number.
  $known = ['browser_version', $ub, 'other'];
  $pattern = '#(?<browser>' . implode('|', $known) .
  ')[/ ]+(?<browser_version>[0-9.|a-zA-Z.]*)#';
  if (!preg_match_all($pattern, $user_agent, $matches)) {
    // We have no matching number just continue.
  }

  // See how many we have.
  $i = count($matches['browser']);
  if ($i != 1) {
    // We will have two since we are not using 'other' argument yet.
    // See if browser_version is before or after the name.
    if (strripos($user_agent, "browser_version") < strripos($user_agent, $ub)) {
      $browser_version = $matches['browser_version'][0];
    }
    else {
      $browser_version = $matches['browser_version'][1];
    }
  }
  else {
    $browser_version = $matches['browser_version'][0];
  // Return unknown in case user is a bot.
  if ($device_detector->isBot()) {
    return [
      'browser_name'    => $browser_name,
      'browser_version' => $browser_version,
      'platform'        => $platform,
    ];
  }

  // Check if we have a number.
  if ($browser_version == NULL || $browser_version == "") {
    $browser_version = "?";
  }
  // Fetching required information using helper functions.
  $browser_name = $device_detector->getClient('name');
  $browser_version = $device_detector->getClient('version');
  $platform = $device_detector->getOs()['name'];

  return [
    'browser_name'    => $browser_name,
    'browser_version' => $browser_version,
    'platform'        => $platform,
    'pattern'         => $pattern,
  ];
}