Skip to content

Beautiful Open-Source Form Builder

Add metadata tracking for form views and enhance analytics reporting (#853)

Chirag Chhatralaca19d980d1a0
diff --git a/api/.env.example b/api/.env.example
index f5c6ab9da..d62674c3b 100644
--- a/api/.env.example
+++ b/api/.env.example
@@ -100,4 +100,7 @@ STRIPE_CLIENT_ID=
STRIPE_CLIENT_SECRET=
STRIPE_REDIRECT_URI=http://localhost:3000/connections/callback/stripe
-SLACK_BOT_TOKEN=
\ No newline at end of file
+SLACK_BOT_TOKEN=
+
+# https://ipinfo.io/login
+IPINFO_TOKEN=
\ No newline at end of file
diff --git a/api/app/Http/Controllers/Forms/FormStatsController.php b/api/app/Http/Controllers/Forms/FormStatsController.php
index f2fed17b8..b408f897f 100644
--- a/api/app/Http/Controllers/Forms/FormStatsController.php
+++ b/api/app/Http/Controllers/Forms/FormStatsController.php
@@ -8,6 +8,7 @@ use App\Models\Forms\FormSubmission;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
class FormStatsController extends Controller
{
@@ -53,11 +54,69 @@ class FormStatsController extends Controller
return $submissionsWithDuration > 0 ? round($totalDuration / $submissionsWithDuration) : null;
});
+ // Aggregate metadata from form views using single database query
+ $metaStats = Cache::remember('form_stats_metadata_' . $form->id, 3600, function () use ($form) {
+ $metadataFields = [
+ 'source',
+ 'device',
+ 'country',
+ 'browser',
+ 'os'
+ ];
+
+ // Determine JSON extraction syntax based on database type
+ $isMySQL = config('database.default') === 'mysql';
+ $jsonExtractPattern = $isMySQL
+ // Normalize empty string and literal 'null' to 'Unknown' in MySQL
+ ? "COALESCE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.%s')), ''), 'null'), 'Unknown')"
+ : "COALESCE(meta->>'%s', 'Unknown')";
+
+ // Build query with safe field binding
+ $query = DB::table('form_views')->where('form_id', $form->id);
+
+ $selectFields = [];
+ $groupByFields = [];
+
+ foreach ($metadataFields as $field) {
+ $extractExpression = sprintf($jsonExtractPattern, $field);
+ $selectFields[] = DB::raw("$extractExpression as $field");
+ $groupByFields[] = DB::raw($extractExpression);
+ }
+
+ $results = $query
+ ->select(array_merge($selectFields, [DB::raw('COUNT(*) as count')]))
+ ->groupBy($groupByFields)
+ ->orderBy('count', 'desc')
+ ->get();
+
+ // Initialize stats arrays
+ $stats = [];
+ foreach ($metadataFields as $field) {
+ $stats[$field] = [];
+ }
+
+ // Aggregate results into stats arrays
+ foreach ($results as $row) {
+ foreach ($metadataFields as $field) {
+ $value = $row->$field;
+ $stats[$field][$value] = ($stats[$field][$value] ?? 0) + $row->count;
+ }
+ }
+
+ // Sort each field by count (descending)
+ foreach ($metadataFields as $field) {
+ arsort($stats[$field]);
+ }
+
+ return $stats;
+ });
+
return [
'views' => $totalViews,
'submissions' => $totalSubmissions,
'completion_rate' => $totalViews > 0 ? round(($totalSubmissions / $totalViews) * 100, 2) : 0,
- 'average_duration' => $averageDuration ? $this->formatDuration($averageDuration) : null
+ 'average_duration' => $averageDuration ? $this->formatDuration($averageDuration) : null,
+ 'meta_stats' => $metaStats
];
}
diff --git a/api/app/Http/Controllers/Forms/PublicFormController.php b/api/app/Http/Controllers/Forms/PublicFormController.php
index ae4a509d7..c0082a927 100644
--- a/api/app/Http/Controllers/Forms/PublicFormController.php
+++ b/api/app/Http/Controllers/Forms/PublicFormController.php
@@ -9,6 +9,7 @@ use App\Http\Resources\FormSubmissionResource;
use App\Jobs\Form\StoreFormSubmissionJob;
use App\Models\Forms\Form;
use App\Models\Forms\FormSubmission;
+use App\Service\Forms\Analytics\UserAgentHelper;
use App\Service\Forms\FormSubmissionProcessor;
use App\Service\Forms\FormCleaner;
use App\Service\WorkspaceHelper;
@@ -44,15 +45,34 @@ class PublicFormController extends Controller
->getData()
);
- // Increase form view counter if not login
- if (!Auth::check()) {
- $form->views()->create();
- }
-
return (new FormResource($form))
->setCleanings($formCleaner->getPerformedCleanings());
}
+ public function view(Request $request, string $slug)
+ {
+ $form = Form::whereSlug($slug)->where('visibility', 'public')->firstOrFail();
+ if ($form->workspace == null) {
+ return $this->error([
+ 'message' => 'Form not found.',
+ ], 404);
+ }
+
+ if (Auth::check()) {
+ return $this->success([
+ 'message' => 'Form viewed by logged in user.',
+ ]);
+ }
+
+ // Increment view count and store metadata for analytics
+ $userAgent = new UserAgentHelper($request);
+ $form->views()->create(['meta' => $userAgent->getMetadata()]);
+
+ return $this->success([
+ 'message' => 'Form viewed.',
+ ]);
+ }
+
public function listUsers(Request $request)
{
// Check that form has user field
diff --git a/api/app/Models/Forms/FormView.php b/api/app/Models/Forms/FormView.php
index 1601d0da4..519ae5319 100644
--- a/api/app/Models/Forms/FormView.php
+++ b/api/app/Models/Forms/FormView.php
@@ -9,6 +9,15 @@ class FormView extends Model
{
use HasFactory;
+ protected $fillable = ['meta'];
+
+ protected function casts(): array
+ {
+ return [
+ 'meta' => 'array',
+ ];
+ }
+
/**
* RelationShips
*/
diff --git a/api/app/Service/Forms/Analytics/BrowserDetection.php b/api/app/Service/Forms/Analytics/BrowserDetection.php
new file mode 100644
index 000000000..4da1f6844
--- /dev/null
+++ b/api/app/Service/Forms/Analytics/BrowserDetection.php
@@ -0,0 +1,1891 @@
+<?php
+
+/**
+ *
+ * PHP Browser Detection Class
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020-2024 Artem Murugov <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @version 2.8
+ * @last-modified September 3, 2024
+ * @link https://github.com/foroco/php-browser-detection
+ */
+
+namespace App\Service\Forms\Analytics;
+
+class BrowserDetection
+{
+ public $useragent;
+ private $get_mode;
+ private $touch_support_mode;
+ private $real_os_name;
+ private $macos_version_minor;
+ private $result_ios;
+ private $result_mobile;
+ private $result_os_type;
+ private $result_os_family;
+ private $result_os_name;
+ private $result_os_version;
+ private $result_os_title;
+ private $result_device_type;
+ private $result_browser_name;
+ private $result_browser_version;
+ private $result_browser_title;
+ private $result_browser_chromium_version;
+ private $result_browser_gecko_version;
+ private $result_browser_webkit_version;
+ private $result_browser_chrome_original;
+ private $result_browser_firefox_original;
+ private $result_browser_safari_original;
+ private $result_browser_android_webview;
+ private $result_browser_ios_webview;
+ private $result_browser_desktop_mode;
+ private $result_64bits_mode;
+
+ /**
+ * Reset all common defined properties method
+ *
+ * @return null
+ */
+
+ private function resetProperties()
+ {
+ $this->real_os_name = '';
+ $this->result_ios = false;
+ $this->result_mobile = 0;
+ $this->macos_version_minor = 0;
+ $this->result_os_type = 'unknown';
+ $this->result_os_family = 'unknown';
+ $this->result_os_name = 'unknown';
+ $this->result_os_version = 0;
+ $this->result_os_title = 'unknown';
+ $this->result_device_type = 'unknown';
+ $this->result_browser_name = 'unknown';
+ $this->result_browser_version = 0;
+ $this->result_browser_title = 'unknown';
+ $this->result_browser_chromium_version = 0;
+ $this->result_browser_gecko_version = 0;
+ $this->result_browser_webkit_version = 0;
+ $this->result_browser_chrome_original = 0;
+ $this->result_browser_firefox_original = 0;
+ $this->result_browser_safari_original = 0;
+ $this->result_browser_android_webview = 0;
+ $this->result_browser_ios_webview = 0;
+ $this->result_browser_desktop_mode = 0;
+ $this->result_64bits_mode = 0;
+ return null;
+ }
+
+ /**
+ * Common User-Agent matching
+ *
+ * @param string $data The string to data search for
+ * @param boolean $case_s Determines if we do a case-sensitive search (false) or a case insensitive (true)
+ * @return boolean Returns true if $data found in $useragent property, false otherwise.
+ */
+
+ private function match_ua($data, $case_s = false)
+ {
+ if (empty($data)) {
+ return false;
+ }
+
+ if (substr($data, 0, 1) === '/' && substr($data, -1) === '/') {
+ if ($case_s == true) {
+ $data = $data . 'i';
+ }
+ if (preg_match($data, $this->useragent, $matches)) {
+ if (!isset($matches[0])) {
+ $matches[0] = 0;
+ }
+ if (!isset($matches[1])) {
+ $matches[1] = 0;
+ }
+ if (!isset($matches[2])) {
+ $matches[2] = 0;
+ }
+ return $matches;
+ } else {
+ return false;
+ }
+ } else {
+ $data_a = explode('|', $data);
+ foreach ($data_a as $v) {
+ if ($case_s == false) {
+ if (strpos($this->useragent, $v) !== false) {
+ return true;
+ }
+ } else {
+ if (stripos($this->useragent, $v) !== false) {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Method to call User-Agent matching method if matching data is case insensitive
+ *
+ * @param string $data The string to data search for
+ * @return boolean Returns true if case insensitive $data found in $useragent property, false otherwise.
+ */
+
+ private function matchi_ua($data)
+ {
+ return $this->match_ua($data, true);
+ }
+
+ /**
+ * Detect iOS
+ * The method will try to recognize iOS signs from $useragent property and will placed true in result_ios property if iOS found, false otherwise. Also returns true if iOS found, false otherwise
+ *
+ * @return bool
+ */
+
+ private function match_ios()
+ {
+ if ($this->match_ua('iPhone|iphone|iPad;|iPod') && !$this->match_ua('x86_64|i386')) {
+ $this->result_ios = true;
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Detect mobile OS
+ * The method will try to recognize mobile OS signs from $useragent property and will placed true in result_mobile property if mobile OS found, false otherwise.
+ *
+ * @return null
+ */
+
+ private function match_mobile()
+ {
+ // Match 64 bits Windows Desktop OS
+
+ if ($this->match_ua('WOW64|Win64')) {
+ $this->result_64bits_mode = 1;
+ return null;
+ }
+
+ // Match Windows Desktop OS
+
+ if ($this->match_ua('Windows NT')) {
+ return null;
+ }
+
+ // Match Qt embedded system
+
+ if ($this->match_ua('QtEmbedded;')) {
+ return null;
+ }
+
+ // Match Android OS
+
+ if ($this->match_ua('Android')) {
+ $this->result_mobile = 1;
+ return null;
+ }
+
+ // Match iOS
+
+ if ($this->match_ios()) {
+ $this->result_mobile = 1;
+ return null;
+ }
+
+ // Match iOS browsers in Desktop Mode
+
+ if ($this->match_ua('Mac OS X')) {
+ if ($this->match_ua('/Mac\sOS\sX.*iOS/')) {
+ $this->result_mobile = 1;
+ }
+ return null;
+ }
+
+ // Match Android browsers in Desktop Mode
+
+ if ($this->match_ua('/X11\;(?:[U\;\s]+)?\sLinux/') && $this->match_ua('Version/4.0 Chrome/|SamsungBrowser|Miui|XiaoMi|EdgA|Puffin|UCBrowser|JioPages|Ecosia android')) {
+ $this->result_mobile = 1;
+ return null;
+ }
+
+ // Match other mobile signs
+
+ if ($this->matchi_ua('mobile|tablet') || $this->match_ua('BlackBerry|BB10;|MIDP|PlayBook|Windows Phone|Windows Mobile|Windows CE|IEMobile|Opera Mini|OPiOS|Opera Mobi|CrKey armv|Kindle|Silk/|Bada|Tizen|Lumia|Symbian|SymbOS|(Series|PalmOS|PalmSource|Dolfin|Crosswalk|Obigo|MQQBrowser|CriOS|WhatsApp/') || $this->matchi_ua('nokia|playstation|watch')) {
+ $this->result_mobile = 1;
+ }
+ return null;
+ }
+
+ /**
+ * Convert MacOS numeric version to MacOS codename
+ *
+ * @param int $version The given MacOS version
+ * @return string Returns MacOS codename string
+ */
+
+ private function macos_codename($version)
+ {
+ switch ($version) {
+ case 0:
+ $result_codename = 'Cheetah';
+ break;
+ case 1:
+ $result_codename = 'Puma';
+ break;
+ case 2:
+ $result_codename = 'Jaguar';
+ break;
+ case 3:
+ $result_codename = 'Panther';
+ break;
+ case 4:
+ $result_codename = 'Tiger';
+ break;
+ case 5:
+ $result_codename = 'Leopard';
+ break;
+ case 6:
+ $result_codename = 'Snow Leopard';
+ break;
+ case 7:
+ $result_codename = 'Lion';
+ break;
+ case 8:
+ $result_codename = 'Mountain Lion';
+ break;
+ case 9:
+ $result_codename = 'Mavericks';
+ break;
+ case 10:
+ $result_codename = 'Yosemite';
+ break;
+ case 11:
+ $result_codename = 'El Capitan';
+ break;
+ case 12:
+ $result_codename = 'Sierra';
+ break;
+ case 13:
+ $result_codename = 'High Sierra';
+ break;
+ case 14:
+ $result_codename = 'Mojave';
+ break;
+ case 15:
+ $result_codename = 'Catalina';
+ break;
+ case 16:
+ $result_codename = 'Big Sur';
+ break;
+ case 17:
+ $result_codename = 'Monterey';
+ break;
+ case 18:
+ $result_codename = 'Ventura';
+ break;
+ default:
+ $result_codename = 'New';
+ break;
+ }
+ return $result_codename;
+ }
+
+ /**
+ * Common User-Agent parsing
+ * The method will try to recognize environment data from $useragent property and will placed it in defined properties.
+ *
+ * @return null
+ */
+
+ private function getResult()
+ {
+ // Detect mobile device
+
+ $this->match_mobile();
+
+ // Detect mobile browser Desktop Mode
+
+ if ($this->touch_support_mode == true) {
+ // Android Desktop Mode
+
+ if ($this->match_ua('X11; Linux x86_64|X11; Linux aarch64|X11; U; U; Linux x86_64|X11; Linux; U;|X11; Linux zbov') && !$this->match_ua('kded/|kioclient|queror|Goanna|Epiphany|Brick|Iceweasel|SeaMonkey|Thunderbird|Qt|Arora|Ubuntu|Debian|Fedora|Linux Mint|elementary|Raspbian|Slackware|ArchLinux|Gentoo')) {
+ $this->real_os_name = 'Android';
+ $this->result_mobile = 1;
+ $this->result_browser_desktop_mode = 1;
+ }
+
+ // iOS Desktop Mode
+
+ if ($this->match_ua('Intel Mac OS X')) {
+ $this->real_os_name = 'iOS';
+ $this->result_mobile = 1;
+ $this->result_browser_desktop_mode = 1;
+ }
+ }
+
+ // Windows Phone Desktop Mode
+
+ if ($this->match_ua('WPDesktop;')) {
+ $this->real_os_name = 'Windows Phone';
+ $this->result_mobile = 1;
+ $this->result_browser_desktop_mode = 1;
+ }
+
+ // Android Desktop Mode without touch support check
+
+ if ($this->result_mobile == 1 && $this->result_os_type === 'unknown') {
+ if ($this->match_ua('/X11\;(?:[U\;\s]+)?\sLinux/') && $this->match_ua('Version/4.0 Chrome/|SamsungBrowser|Miui|XiaoMi|EdgA|Puffin|UCBrowser|JioPages|Ecosia android')) {
+ $this->real_os_name = 'Android';
+ $this->result_browser_desktop_mode = 1;
+ }
+ }
+
+ // iOS Desktop Mode without touch support check
+
+ if ($this->result_mobile == 1 && $this->result_ios == false && $this->result_os_type === 'unknown') {
+ if ($this->match_ua('/Mac\sOS\sX.*iOS/')) {
+ $this->real_os_name = 'iOS';
+ $this->result_browser_desktop_mode = 1;
+ }
+ }
+
+ /*
+ --------------
+ OS DETECTION
+ --------------
+
+ --------------
+ Mixed OS(es)
+ --------------
+ */
+
+ $os_need_continue = true;
+
+ // Windows OS
+
+ if ($this->get_mode !== 'browser' && $this->match_ua('Windows|Win32') && !$this->match_ua('Windows Phone|Windows Mobile|Windows CE|WPDesktop')) {
+ $this->result_os_name = 'Windows';
+ $matches = $this->match_ua('/Windows ([ .a-zA-Z0-9]+)[;\\)]/');
+ $version = is_array($matches) ? $matches[1] : 0;
+ if ($version === 'NT 11.0') {
+ $this->result_os_version = '11';
+ }
+ if ($version === 'NT 10.1') {
+ $this->result_os_version = '11';
+ }
+ if ($version === 'NT 10.0') {
+ $this->result_os_version = '10';
+ }
+ if ($version === 'NT 6.4') {
+ $this->result_os_version = '10';
+ }
+ if ($version === 'NT 6.3') {
+ $this->result_os_version = '8.1';
+ }
+ if ($version === 'NT 6.2') {
+ $this->result_os_version = '8';
+ }
+ if ($version === 'NT 6.1') {
+ $this->result_os_version = '7';
+ }
+ if ($version === 'NT 6.0') {
+ $this->result_os_version = 'Vista';
+ }
+ if ($version === 'NT 6') {
+ $this->result_os_version = 'NT';
+ }
+ if ($version === 'NT 5.2') {
+ $this->result_os_version = 'XP';
+ }
+ if ($version === 'NT 5.1') {
+ $this->result_os_version = 'XP';
+ }
+ if ($version === 'NT 5.01') {
+ $this->result_os_version = '2000';
+ }
+ if ($version === 'NT 5.0') {
+ $this->result_os_version = '2000';
+ }
+ if ($version === 'NT 5') {
+ $this->result_os_version = '2000';
+ }
+ if ($version === 'NT 4.0') {
+ $this->result_os_version = 'NT 4.0';
+ }
+ if ($version === 'NT4.0') {
+ $this->result_os_version = 'NT 4.0';
+ }
+ if ($version === 'NT') {
+ $this->result_os_version = 'NT';
+ }
+ if ($version === '98') {
+ $this->result_os_version = '98';
+ }
+ if ($version === '95') {
+ $this->result_os_version = '95';
+ }
+ if ($version === 'ME') {
+ $this->result_os_version = 'ME';
+ }
+ if (empty($this->result_os_version)) {
+ if ($this->match_ua('/Win16/')) {
+ $this->result_os_version = '3.1';
+ }
+ if ($this->match_ua('/(Windows\s95|Win95|Windows_95)/')) {
+ $this->result_os_version = '95';
+ }
+ if ($this->match_ua('/(Windows\s98|Win98)/')) {
+ $this->result_os_version = '98';
+ }
+ if ($this->match_ua('/Windows\s2000/')) {
+ $this->result_os_version = '2000';
+ }
+ if ($this->match_ua('/Win\sNT\s5\.0/')) {
+ $this->result_os_version = '2000';
+ }
+ if ($this->match_ua('/Windows\sXP/')) {
+ $this->result_os_version = 'XP';
+ }
+ if ($this->match_ua('/WinNT4\.0/')) {
+ $this->result_os_version = 'NT 4.0';
+ }
+ if ($this->match_ua('/Windows\sVista/')) {
+ $this->result_os_version = 'Vista';
+ }
+ if ($this->match_ua('/Windows\s7/')) {
+ $this->result_os_version = '7';
+ }
+ if ($this->match_ua('/Windows\s8/')) {
+ $this->result_os_version = '8';
+ }
+ if ($this->match_ua('/Windows\s8.1/')) {
+ $this->result_os_version = '8.1';
+ }
+ if ($this->match_ua('/Windows\s10/')) {
+ $this->result_os_version = '10';
+ }
+ if ($this->match_ua('/Windows\s11/')) {
+ $this->result_os_version = '11';
+ }
+ }
+ if (!empty($this->result_os_version)) {
+ $this->result_os_title = 'Windows ' . $this->result_os_version;
+ } else {
+ $this->result_os_title = 'Windows (unknown version)';
+ }
+ if (intval($this->result_os_version) < 7 || intval($this->result_os_version) > 10) {
+ $this->result_os_type = 'desktop';
+ } else {
+ $this->result_os_type = 'mixed';
+ }
+ $this->result_os_family = 'windows';
+ $os_need_continue = false;
+ }
+
+ /*
+ -------------
+ Desktop OS
+ -------------
+ */
+
+ if ($this->get_mode !== 'browser' && $this->result_mobile == 0) {
+ $this->result_os_type = 'desktop';
+
+ // Mac OS (X) / macOS
+
+ if ($os_need_continue && $this->match_ua('Mac OS X|Mac_PowerPC|Macintosh|Mac_68K') && !$this->match_ua('AmigaOS')) {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'MacOS';
+ if ($this->match_ua('Mac OS X')) {
+ $matches = $this->match_ua('/Mac OS X (\d+)[_.](\d+)(?:[_.](\d+)|)/');
+
+ $version = isset($matches[1]) ? $matches[1] : 0;
+ $version_minor = isset($matches[2]) ? $matches[2] : 0;
+ $version_revision = isset($matches[3]) ? $matches[3] : -1;
+
+ // macOS version to minor version conversion (needs since Big Sur)
+ if ($version == 10 && $version_minor == 0) {
+ $version_minor = 16;
+ }
+ if ($version == 11) {
+ $version_minor = 16;
+ }
+ if ($version == 12) {
+ $version_minor = 17;
+ }
+
+ // macOS with a particular major/minor/revision version structure (needs since Big Sur)
+ if ($version == 10 && $version_minor == 15 && $version_revision == 7) {
+ $version_minor = 16;
+ }
+ if ($version == 10 && $version_minor == 16 && $version_revision == 0) {
+ $version_minor = 17;
+ }
+
+ if (!empty($version_minor)) {
+ if ($version >= 10) {
+ $this->result_os_version = $this->macos_codename($version_minor);
+ }
+ $this->macos_version_minor = $version_minor;
+ }
+
+ if (!empty($this->result_os_version)) {
+ $this->result_os_title = 'MacOS ' . $this->result_os_version;
+ } else {
+ $this->result_os_title = 'MacOS (unknown version)';
+ }
+ } else {
+ $this->result_os_title = 'MacOS';
+ }
+ $this->result_os_family = 'macintosh';
+ $os_need_continue = false;
+ }
+
+ // Linux family OS(es)
+
+ if ($os_need_continue) {
+ $os_list[] = array('Ubuntu' => '/Ubuntu(?: )?(?:\/)?([0-9]+\.[0-9]+)/');
+ $os_list[] = array('Kubuntu' => '/Kubuntu(?: )?(?:\/)?([0-9]+\.[0-9]+)/');
+ $os_list[] = array('Linux Mint' => '/Linux Mint\/([.0-9]+)/');
+ $os_list[] = array('CentOS' => '/CentOS\/([0-9]+\.[0-9]+)/');
+ $os_list[] = array('OpenSUSE' => '/SUSE\/([0-9]+\.[0-9]+)/');
+ $os_list[] = array('Red Hat' => '/Red\sHat\/([0-9]+\.[0-9]+)/');
+
+ foreach ($os_list as $os_list_va) {
+ $k = key($os_list_va);
+ $v = $os_list_va[$k];
+
+ $matches = $this->match_ua($v);
+ if ($matches) {
+ $this->result_os_family = 'linux';
+ $this->result_os_name = $k;
+ $this->result_os_version = is_array($matches) ? (float)$matches[1] : 0;
+ $os_need_continue = false;
+ break;
+ }
+ }
+ $os_list = null;
+ }
+
+ // Other Desktop OS(es)
+
+ if ($os_need_continue) {
+ $other_os = array();
+
+ $other_os[] = array('Chrome OS' => 'CrOS');
+ $other_os[] = array('Chrome OS' => 'Chromium OS');
+ $other_os[] = array('Linux Mint' => 'Linux Mint');
+ $other_os[] = array('Kubuntu' => 'Kubuntu');
+ $other_os[] = array('Ubuntu' => 'Ubuntu');
+ $other_os[] = array('Ubuntu' => 'ubuntu');
+ $other_os[] = array('Debian' => 'Debian');
+ $other_os[] = array('CentOS' => 'CentOS');
+ $other_os[] = array('Fedora' => 'Fedora');
+ $other_os[] = array('Arch Linux' => 'ArchLinux');
+ $other_os[] = array('OpenSUSE' => 'SUSE');
+ $other_os[] = array('Red Hat' => 'Red Hat');
+ $other_os[] = array('Elementary' => ' elementary');
+ $other_os[] = array('OpenBSD' => 'OpenBSD');
+ $other_os[] = array('NetBSD' => 'NetBSD');
+ $other_os[] = array('FreeBSD' => 'FreeBSD');
+ $other_os[] = array('SunOS' => 'SunOS');
+ $other_os[] = array('RISC OS' => 'RISC OS');
+ $other_os[] = array('Linux' => 'Linux');
+ $other_os[] = array('Linux' => 'X11;');
+ $other_os[] = array('Linux' => 'Mozilla/5.0 (x86_64)');
+ $other_os[] = array('Linux' => 'Mozilla/5.0 (i686)');
+ $other_os[] = array('Linux' => 'U; NETFLIX');
+ $other_os[] = array('Linux' => 'GNU; ');
+ $other_os[] = array('Linux' => 'QtEmbedded; U; Linux;');
+ $other_os[] = array('AmigaOS' => 'AmigaOS');
+ $other_os[] = array('Haiku' => 'Haiku');
+ $other_os[] = array('Roku OS' => 'Roku/');
+ $other_os[] = array('BeOS' => 'BeOS');
+
+ foreach ($other_os as $other_os_va) {
+ $k = key($other_os_va);
+ $v = $other_os_va[$k];
+
+ if ($this->match_ua($v)) {
+ $this->result_os_family = 'linux';
+ if ($k === 'AmigaOS') {
+ $this->result_os_family = 'amiga';
+ }
+ if ($k === 'Haiku' || $k === 'BeOS') {
+ $this->result_os_family = 'beos';
+ }
+ if ($k === 'OpenBSD' || $k === 'NetBSD' || $k === 'FreeBSD' || $k === 'SunOS' || $k === 'RISC OS') {
+ $this->result_os_family = 'unix';
+ }
+ $this->result_os_version = 0;
+ $this->result_os_name = $k;
+ $this->result_os_title = $k;
+ $os_need_continue = false;
+ break;
+ }
+ }
+ $other_os = null;
+ }
+
+ // End of desktop OS detection
+ }
+
+ /*
+ -------------
+ Mobile OS
+ -------------
+ */
+
+ if ($this->result_mobile == 1) {
+ if ($this->result_os_type === 'unknown') {
+ $this->result_os_type = 'mobile';
+ }
+
+ // Desktop Mode on generic mobile OS
+
+ // Android Desktop Mode
+
+ if ($os_need_continue && $this->result_browser_desktop_mode == 1 && $this->real_os_name === 'Android') {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'Android';
+ $this->result_os_family = 'android';
+ $os_need_continue = false;
+ }
+
+ // iOS Desktop Mode
+
+ if ($os_need_continue && $this->result_browser_desktop_mode == 1 && $this->real_os_name === 'iOS') {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'iOS';
+ $this->result_os_family = 'macintosh';
+ $os_need_continue = false;
+ }
+
+ // WP Desktop Mode
+
+ if ($os_need_continue && $this->result_browser_desktop_mode == 1 && $this->real_os_name === 'Windows Phone') {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'Windows Phone';
+ $this->result_os_family = 'windows';
+ $os_need_continue = false;
+ }
+
+ // Android OS
+
+ if ($os_need_continue && $this->match_ua('Android') && !$this->match_ua('Windows Phone|Windows Mobile|Tizen')) {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'Android';
+ $matches = $this->match_ua('/Android(?: |\-)([0-9]+\.[0-9]+)/');
+ $this->result_os_version = is_array($matches) ? (float)$matches[1] : 0;
+ if (empty($this->result_os_version)) {
+ $matches = $this->match_ua('/Android(?: |\-)(\d+)/');
+ $this->result_os_version = is_array($matches) ? (float)$matches[1] : 0;
+ }
+ $this->result_os_family = 'android';
+ $os_need_continue = false;
+ }
+
+ // iOS OS
+
+ if ($os_need_continue && $this->result_ios != false && !$this->match_ua('Windows Phone|Windows Mobile')) {
+ $this->result_os_version = 0;
+ $this->result_os_name = 'iOS';
+ $matches = $this->match_ua('/\sOS\s(\d+)[_.](\d+)/');
+ $version = is_array($matches) ? $matches[1] : 0;
+ $version_minor = is_array($matches) ? $matches[2] : 0;
+ if (!empty($version)) {
+ $this->result_os_version = floatval($version . '.' . $version_minor);
+ }
+ $this->result_os_family = 'macintosh';
+ $os_need_continue = false;
+ }
+
+ // WhatsApp Android / iOS
+
+ if ($os_need_continue && $this->match_ua('WhatsApp/')) {
+ $matches = $this->match_ua('/WhatsApp\/([.0-9]+)\s(A|i)$/');
+ $os_match = is_array($matches) ? $matches[2] : 0;
+
+ if ($os_match === 'A') {
+ $this->result_os_name = 'Android';
+ $this->result_os_family = 'android';
+ }
+
+ if ($os_match === 'i') {
+ $this->result_os_name = 'iOS';
+ $this->result_os_family = 'macintosh';
+ }
+ }
+
+ // Windows Phone OS, Tizen OS, Bada OS, Kindle OS, FIre OS, Java Platform
+
+ if ($os_need_continue) {
+ $os_list[] = array('Windows Phone' => '/Phone(?: OS)? ([.0-9]+)/');
+ $os_list[] = array('Windows Mobile' => '/Windows Mobile ([.0-9]+)/');
+ $os_list[] = array('Windows Mobile' => 'Windows Mobile');
+ $os_list[] = array('Windows CE' => 'Windows CE');
+ $os_list[] = array('Firefox OS' => '/Mozilla\/5\.0\s\((?:Mobile|Tablet);(?:.*;)?\srv:[.0-9]+\)\sGecko\/[.0-9]+\sFirefox\/[.0-9]+$/');
+ $os_list[] = array('Tizen' => '/Tizen[\/\s]([.0-9]+)/');
+ $os_list[] = array('Bada' => '/Bada[;\/]([.0-9]+)/');
+ $os_list[] = array('Kindle' => '/Kindle\/([0-9]+\.[0-9]+)/');
+ $os_list[] = array('Fire OS' => 'KFJW|Silk/');
+ $os_list[] = array('Java Platform' => '/MIDP\-([0-9]+\.[0-9]+)/');
+ $os_list[] = array('Java Platform' => '/MIDP/');
+ $os_list[] = array('MAUI Platform' => '/MAUI\s|\sMAUI/');
+
+ foreach ($os_list as $os_list_va) {
+ $k = key($os_list_va);
+ $v = $os_list_va[$k];
+
+ $matches = $this->match_ua($v);
+ if ($matches) {
+ $this->result_os_family = 'linux';
+ if (strpos($k, 'Windows') !== false) {
+ $this->result_os_family = 'windows';
+ }
+ $this->result_os_name = $k;
+ $this->result_os_version = is_array($matches) ? (float)$matches[1] : 0;
+ $os_need_continue = false;
+
+ // J2ME/MIDP or MAUI
+
+ if (strpos($k, 'Java Platform') !== false || strpos($k, 'MAUI Platform') !== false) {
+ $this->result_os_family = 'unknown';
+ $os_need_continue = true;
+ }
+ break;
+ }
+ }
+ $os_list = null;
+ }
+
+ // Other Mobile OS(es)
+
+ if ($os_need_continue) {
+ $other_os = array();
+
+ $other_os[] = array('Android' => 'CrKey armv');
+ $other_os[] = array('Android' => 'SpreadTrum;');
+ $other_os[] = array('BlackBerry' => 'BlackBerry');
+ $other_os[] = array('BlackBerry' => 'BB10;');
+ $other_os[] = array('BlackBerry' => 'RIM Tablet');
+ $other_os[] = array('Symbian' => 'Symbian');
+ $other_os[] = array('Symbian' => '(Series');
+ $other_os[] = array('Symbian' => 'SymbOS');
+ $other_os[] = array('WatchOS' => 'Watch');
+ $other_os[] = array('KaiOS' => 'KAIOS');
+ $other_os[] = array('RemixOS' => 'Remix Mini');
+ $other_os[] = array('RemixOS' => 'RemixOS');
+ $other_os[] = array('Sailfish OS' => 'Sailfish');
+ $other_os[] = array('LiveArea' => 'PlayStation Vita');
+ $other_os[] = array('PalmOS' => 'PalmOS');
+ $other_os[] = array('PalmOS' => 'PalmSource');
+ $other_os[] = array('Maemo' => 'Maemo');
+ $other_os[] = array('PlayStation Platform' => 'PlayStation');
+ $other_os[] = array('PlayStation Platform' => 'PLAYSTATION');
+
+ foreach ($other_os as $other_os_va) {
+ $k = key($other_os_va);
+ $v = $other_os_va[$k];
+
+ if ($this->match_ua($v)) {
+ if ($k === 'Android') {
+ $this->result_os_family = 'android';
+ }
+ if ($k === 'BlackBerry') {
+ $this->result_os_family = 'blackberry';
+ }
+ if ($k === 'Symbian') {
+ $this->result_os_family = 'symbian';
+ }
+ if ($k === 'WatchOS') {
+ $this->result_os_family = 'macintosh';
+ }
+ if ($k === 'Sailfish OS' || $k === 'KaiOS' || $k === 'Maemo') {
+ $this->result_os_family = 'linux';
+ }
+ if ($k === 'RemixOS') {
+ $this->result_os_family = 'android';
+ }
+ if ($k === 'LiveArea') {
+ $this->result_os_family = 'livearea';
+ }
+ if ($k === 'PalmOS') {
+ $this->result_os_family = 'palm';
+ }
+ if ($k === 'PlayStation Platform') {
+ $this->result_os_family = 'unix';
+ }
+ $this->result_os_version = 0;
+ $this->result_os_name = $k;
+ $this->result_os_title = $k;
+ $os_need_continue = false;
+ break;
+ }
+ }
+
+ $other_os = null;
+ }
+
+ // End of mobile OS detection
+ }
+
+ // Other mixed-type linux-family OS(es)
+
+ if ($this->get_mode !== 'browser' && ($this->result_os_family === 'linux' || $this->result_os_family === 'unknown')) {
+ $other_os = array();
+
+ $other_os[] = array('WebOS' => 'hpwOS');
+ $other_os[] = array('WebOS' => 'Web0S');
+ $other_os[] = array('WebOS' => 'WebOS');
+ $other_os[] = array('WebOS' => 'webOS');
+
+ foreach ($other_os as $other_os_va) {
+ $k = key($other_os_va);
+ $v = $other_os_va[$k];
+
+ if ($this->match_ua($v)) {
+ $this->result_os_family = 'linux';
+ $this->result_os_type = 'mixed';
+ $this->result_os_version = 0;
+ $this->result_os_name = $k;
+ $this->result_os_title = $k;
+ break;
+ }
+ }
+ $other_os = null;
+ }
+
+ // Darwin (MacOS and iOS)
+
+ if ($os_need_continue && $this->match_ua(' Darwin') && !$this->match_ua('X11;')) {
+ $this->result_os_family = 'macintosh';
+
+ if ($this->match_ua('x86_64|i386|Mac')) {
+ $this->result_os_type = 'desktop';
+ $this->result_os_name = 'MacOS';
+ } else {
+ $this->result_os_type = 'mobile';
+ $this->result_mobile = 1;
+ $this->result_os_name = 'iOS';
+ $this->result_ios = true;
+ }
+
+ $this->result_os_version = 0;
+ $darwin_os_version = 0;
+
+ $matches = $this->match_ua('/\sDarwin(\s|\/)([0-9]+\.[0-9]+)/');
+ if (!empty($matches[2])) {
+ $darwin_os_version = (float)$matches[2];
+ }
+
+ $darwin_macos_map = array('1.3' => '0', '1.4' => '1', '5.1' => '1', '5.5' => '1', '6.0' => '2', '6.8' => '2', '7.0' => '3', '7.9' => '3', '8.0' => '4', '8.1' => '4', '9.0' => '5', '9.8' => '5', '10.0' => '6', '10.8' => '6', '11.0' => '7', '11.4' => '7', '12.0' => '8', '12.6' => '8', '13.0' => '9', '13.4' => '9', '14.0' => '10', '14.5' => '10', '15.0' => '11', '15.6' => '11', '16.0' => '12', '16.6' => '12', '17.0' => '13', '17.7' => '13', '18.0' => '14', '18.2' => '14', '19.0' => '15', '19.6' => '15', '20.0' => '16', '20.6' => '16', '21.0' => '17', '21.6' => '17', '22.0' => '18', '22.3' => '18');
+ $darwin_ios_map = array('9.0' => '1', '9.8' => '1', '10.0' => '4', '10.8' => '4', '11.0' => '5', '11.4' => '5', '12.0' => '6', '13.0' => '7', '14.0' => '8', '15.0' => '9', '15.6' => '9', '16.0' => '10', '16.6' => '10', '17.0' => '11', '17.7' => '11', '18.0' => '12', '18.2' => '12', '19.0' => '13', '19.6' => '13', '20.0' => '14', '20.6' => '14', '21.0' => '15', '21.6' => '15', '22.0' => '16', '22.3' => '16');
+
+ if ($this->result_os_name === 'MacOS') {
+ $darwin_map = $darwin_macos_map;
+ } else {
+ $darwin_map = $darwin_ios_map;
+ }
+
+ foreach ($darwin_map as $k => $v) {
+ $darwin_map_min[$k] = abs($k - $darwin_os_version);
+ }
+
+ asort($darwin_map_min);
+ $darwin_map_key = key($darwin_map_min);
+ $this->result_os_version = $darwin_map[$darwin_map_key];
+
+ if ($this->result_os_name === 'MacOS') {
+ $this->result_os_version = $this->macos_codename($darwin_map[$darwin_map_key]);
+ if (!empty($this->result_os_version)) {
+ $this->result_os_title = 'MacOS ' . $this->result_os_version;
+ } else {
+ $this->result_os_title = 'MacOS';
+ }
+ }
+
+ $darwin_map = null;
+ $darwin_map_min = null;
+ $darwin_macos_map = null;
+ $darwin_ios_map = null;
+ $os_need_continue = false;
+ }
+
+ // Set OS title and type
+
+ if ($this->get_mode !== 'browser' && $this->result_os_name !== 'MacOS') {
+ if (!empty($this->result_os_version)) {
+ $this->result_os_title = $this->result_os_name . ' ' . $this->result_os_version;
+ } else {
+ if ($this->result_os_name !== 'Windows') {
+ $this->result_os_title = $this->result_os_name;
+ } else {
+ $this->result_os_title = $this->result_os_name . ' (unknown version)';
+ }
+ }
+ if (strpos($this->result_os_name, 'unknown') !== false) {
+ $this->result_os_type = 'unknown';
+ }
+ if ($this->result_os_version == null) {
+ $this->result_os_version = 0;
+ }
+ }
+
+ /*
+ ---------------------
+ Detect 64bits mode
+ ---------------------
+ */
+
+ if ($this->result_os_name === 'iOS' && $this->result_os_version >= 11) {
+ $this->result_64bits_mode = 1;
+ }
+ if ($this->result_os_name === 'MacOS' && $this->macos_version_minor >= 8) {
+ $this->result_64bits_mode = 1;
+ }
+
+ if ($this->result_64bits_mode == 0) {
+ if ($this->match_ua('64') && $this->match_ua('x64;|x86_64|x86-64|arm64|arm_64|aarch64|Linux x64|mips64|amd64|AMD64|ia64|IRIX64|ppc64|sparc64|x64_64')) {
+ $this->result_64bits_mode = 1;
+ }
+ }
+
+ if ($this->result_os_name === 'Android' && $this->result_browser_desktop_mode == 1) {
+ $this->result_64bits_mode = 0;
+ }
+ if ($this->result_64bits_mode == 1 && $this->match_ua('86') && $this->match_ua('i386|i686')) {
+ $this->result_64bits_mode = 0;
+ }
+
+ // If mode 'os' only
+
+ if ($this->get_mode === 'os') {
+ return null;
+ }
+
+ /*
+ -------------------
+ BROWSER DETECTION
+ -------------------
+
+ ---------------------
+ Mixed-type Browsers
+ ---------------------
+ */
+
+ $browser_need_continue = true;
+ if ($this->get_mode === 'device') {
+ $browser_need_continue = false;
+ }
+
+ // Chrome / Chromium
+
+ if ($browser_need_continue && $this->match_ua('Chrome/|Chromium/|CriOS/|CrMo/')) {
+ $this->result_browser_version = 0;
+ $matches = $this->match_ua('/(Chrome|Chromium|CriOS|CrMo)\/([0-9]+)\.?/');
+ $this->result_browser_name = 'Chrome';
+ if (!empty($matches[1]) && $matches[1] === 'Chromium') {
+ $this->result_browser_name = 'Chromium';
+ }
+ if (!empty($matches[2])) {
+ $this->result_browser_version = (int)$matches[2];
+ }
+ $this->result_browser_chromium_version = $this->result_browser_version;
+ if ($this->match_ua('CriOS/')) {
+ $this->result_browser_chromium_version = 0;
+ }
+ if ($this->match_ua('/Gecko\)\s(Chrome|CrMo)\/(\d+\.\d+\.\d+\.\d+)\s(?:Mobile)?(?:\/[.0-9A-Za-z]+\s|\s)?Safari\/[.0-9]+$/') && !$this->match_ua('SalamWeb| Valve | GOST')) {
+ $this->result_browser_chrome_original = 1;
+ }
+ }
+
+ // Firefox
+
+ if ($browser_need_continue && $this->result_browser_chromium_version == 0 && $this->match_ua('Firefox/') && !$this->match_ua('Focus/|FxiOS|Waterfox|Ghostery:')) {
+ $this->result_browser_version = 0;
+ $matches = $this->match_ua('/Firefox\/([0-9]+)\./');
+ $this->result_browser_name = 'Firefox';
+ if (!empty($matches[1])) {
+ $this->result_browser_version = (int)$matches[1];
+ }
+ if ($this->match_ua('/\)\sGecko\/[.0-9]+\sFirefox\/[.0-9]+$/')) {
+ $this->result_browser_firefox_original = 1;
+ }
+ }
+
+ // Cross-device browsers (Yandex, Edge, Firefox, Opera, Puffin, Vivaldi, QQ Browser, Whale, Brave etc)
+
+ if ($browser_need_continue && $this->result_browser_chrome_original == 0 && $this->result_browser_firefox_original == 0) {
+ $browser_list[] = array('Yandex Browser', 'YaBrowser', '/YaBrowser\/([0-9]+\.[0-9]+)/', '1', 'YaApp_');
+ $browser_list[] = array('Edge', 'Edg', '/Edg(|e|A|iOS)\/([0-9]+)\./', '2', '');
+ $browser_list[] = array('Opera', ' OPR/', '/OPR\/(\d+)/', '1', 'Opera Mini|OPiOS|OPT/|OPRGX/|AlohaBrowser');
+ $browser_list[] = array('Opera', 'Opera', '/Opera.*Version\/([0-9]+\.[0-9]+)/', '1', 'Opera Mini|OPiOS|OPT/|InettvBrowser/');
+ $browser_list[] = array('Opera', 'Opera', '/Opera(\s|\/)([0-9]+\.[0-9]+)/', '2', 'Opera Mini|OPiOS|OPT/|InettvBrowser/');
+ $browser_list[] = array('UC Browser', 'UBrowser|UCBrowser|UCMini', '/(UBrowser|UCBrowser|UCMini)\/([0-9]+\.[0-9]+)/', '2', 'UCTurbo|AliApp');
+ $browser_list[] = array('UC Browser Turbo', 'UCTurbo/', '/UCTurbo\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Puffin', 'Puffin/', '/Puffin\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Vivaldi', 'Vivaldi/', '/Vivaldi\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('QQ Browser', 'QQBrowser/', '/QQBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Coc Coc', 'coc_coc_browser', '/coc_coc_browser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Whale', 'Whale/', '/Whale\/([0-9]+\.[0-9]+)/', '1', 'NAVER(inapp;');
+ $browser_list[] = array('Brave', 'Brave', '/Brave(?: Chrome)?\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Maxthon', 'Maxthon/', '/Maxthon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Maxthon', 'MxBrowser/', '/MxBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('2345 Explorer', '2345Explorer|2345Browser', '/(2345Explorer|2345Browser)(?: |\/)?([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('IceCat', 'IceCat/', '/IceCat\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Lunascape', 'Lunascape', '/Lunascape(?: |\/)?([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Seznam Browser', 'Seznam.cz/|SznProhlizec/', '/(Seznam\.cz|SznProhlizec)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Sleipnir', 'Sleipnir/', '/Sleipnir\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Sputnik', 'SputnikBrowser/', '/SputnikBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Oculus Browser', 'OculusBrowser/', '/OculusBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('PaleMoon', 'PaleMoon', '/PaleMoon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('SalamWeb', 'SalamWeb|Salam Browser', '/SalamWeb\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Swing', 'Swing/|Swing(And)/', '/Swing(?:\(And\))?\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Safe Exam Browser', 'SEB/', '/SEB\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Colibri', 'Colibri/', '/Colibri\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Opera GX', 'OPRGX', '/OPRGX\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Opera GX', 'OPX/', '/OPX\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Xvast', 'Xvast/', '/Xvast\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Atom', 'Atom/', '/Atom\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('NetCast', 'SmartTV/', '/SmartTV\/([0-9]+)/', '1', '');
+ $browser_list[] = array('LG Browser', ' LG Browser/', '/\sLG\sBrowser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Ghostery Browser', 'Ghostery:', '/Ghostery\:([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Aloha Browser', 'AlohaBrowser/', '/AlohaBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Lenovo Browser', 'SLBrowser/', '/SLBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Samsung Browser TV', '/SamsungBrowser\/[.0-9]+\sTV\s/', '/SamsungBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('WeChat App', 'MicroMessenger/', '/MicroMessenger\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('WKWebView', '/Intel\sMac\sOS\sX\s\d+.*\)\sAppleWebKit\/[.0-9]+.*Gecko\)$/', 'AppleWebKit', '1', '');
+ $browser_list[] = array('Sber Browser', 'SberBrowser/', '/SberBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+
+ foreach ($browser_list as $browser_list_va) {
+ $match = $browser_list_va[1];
+ $match_exclude = $browser_list_va[4];
+
+ if ($this->match_ua($match) && !$this->match_ua($match_exclude)) {
+ $this->result_browser_version = 0;
+ $this->result_browser_name = $browser_list_va[0];
+ $matches = $this->match_ua($browser_list_va[2]);
+ $matches_n = intval($browser_list_va[3]);
+ $this->result_browser_version = is_array($matches) ? (float)$matches[$matches_n] : 0;
+ if (!empty($this->result_browser_version)) {
+ $browser_need_continue = false;
+ break;
+ }
+ }
+ }
+
+ $browser_list = null;
+ }
+
+ // Gecko engine detection
+
+ if ($this->get_mode !== 'device' && $this->result_browser_chromium_version == 0 && $this->match_ua('Gecko/|ArtisBrowser/') && ($this->match_ua('/\srv:[.0-9a-z]+(?:\;\s.*|)\)\sGecko\/[.0-9]+\s/') || $this->match_ua('QwantMobile/|ArtisBrowser/'))) {
+ $this->result_browser_gecko_version = 0;
+ $match = $this->match_ua('/\srv:([0-9]+\.[0-9]+)(?:[.0-9a-z]+)?(?:\;\s.*|)\)\sGecko\/[.0-9]+\s/');
+
+ if (is_array($match)) {
+ if ($match[1] >= 5) {
+ $match[1] = intval($match[1]);
+ } else {
+ $match[1] = (float)$match[1];
+ }
+ }
+
+ if (!empty($match[1])) {
+ $this->result_browser_gecko_version = $match[1];
+ } else {
+ $match = $this->match_ua('/\sGecko\/([.0-9]+)\s.*QwantMobile\/[.0-9]+$/');
+ if (!empty($match[1])) {
+ $this->result_browser_gecko_version = intval($match[1]);
+ } else {
+ $match = $this->match_ua('/\srv:([0-9]+\.[0-9]+)\)\s.*ArtisBrowser\/[.0-9]+$/');
+ if (!empty($match[1])) {
+ $this->result_browser_gecko_version = intval($match[1]);
+ }
+ }
+ }
+
+ // Gecko >= 109 issue
+
+ if ($this->result_browser_gecko_version >= 109 && $this->result_browser_gecko_version < 120) {
+ $match = $this->match_ua('/\srv:[.0-9a-z]+(?:\;\s.*|)\)\sGecko\/[.0-9]+\s.*(?:Firefox|MaxBrowser)\/([0-9]+)\./');
+ if (!empty($match[1])) {
+ $this->result_browser_gecko_version = intval($match[1]);
+ }
+ }
+ }
+
+ // WebKit engine detection
+
+ if ($this->get_mode !== 'device' && $this->result_browser_chromium_version == 0 && $this->result_browser_gecko_version == 0 && $this->matchi_ua('AppleWebKit/')) {
+ $this->result_browser_webkit_version = 0;
+ $match = $this->matchi_ua('/AppleWebKit\/([0-9]+\.[0-9]+)/');
+ if (!empty($match[1])) {
+ $this->result_browser_webkit_version = (float)$match[1];
+ }
+ }
+
+ /*
+ ------------------
+ Desktop Browsers
+ ------------------
+ */
+
+ if ($browser_need_continue && $this->result_browser_chrome_original == 0 && $this->result_browser_firefox_original == 0 && $this->result_mobile == 0) {
+ // Safari
+
+ $browser_list[] = array('Safari', '/AppleWebKit\/[.0-9]+.*Gecko\)\sSafari\/[.0-9A-Za-z]+$/', '/Safari\/(\d+)/', '1', 'Version/');
+ $browser_list[] = array('Safari', '/Version\/([0-9]+\.[0-9]+).*Safari/', '/Version\/([0-9]+\.[0-9]+).*Safari/', '1', 'Epiphany|Arora/|Midori|midori|SlimBoat|Roccat');
+
+ // IE
+
+ $browser_list[] = array('Internet Explorer', 'MSIE', '/MSIE(?:\s|)([0-9]+)/', '1', 'Opera|IEMobile|Trident/');
+ $browser_list[] = array('Internet Explorer', 'Trident/', '/Trident\/([0-9]+)/', '1', 'Opera|IEMobile');
+
+ // Chromium based browsers
+
+ $browser_list[] = array('Avast Browser', 'Avast/', '/Avast\/([0-9]+)/', '1', '');
+ $browser_list[] = array('AVG Browser', 'AVG/', '/AVG\/([0-9]+)/', '1', '');
+ $browser_list[] = array('CCleaner Browser', 'CCleaner/', '/CCleaner\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Comodo Dragon', 'Dragon/', '/Dragon\/([0-9]+)/', '1', 'IceDragon');
+ $browser_list[] = array('Iridium', 'Iridium/', '/Iridium\/.*Safari.*Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Maxthon Nitro', 'MxNitro|mxnitro', '/(MxNitro|mxnitro)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Iron', ' Iron ', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Iron', 'Iron/', '/Iron\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Hola Browser', 'Hola/', '/Hola\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Sogou Explorer', '/Chrome.*Safari.*SE\s.*MetaSr/', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('360 browser', 'QIHU 360', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Slimjet', 'Slimjet/', '/Slimjet\/([0-9]+)/', '1', '');
+ $browser_list[] = array('FreeU', 'FreeU|Freeu', '/(FreeU|Freeu)\/([0-9]+)/', '2', '');
+ $browser_list[] = array('Kinza', 'Kinza/', '/Kinza\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Station', 'Station/', '/Station\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Superbird', 'Superbird|SuperBird', '/(Superbird|SuperBird)\/([0-9]+)/', '2', '');
+ $browser_list[] = array('Polypane', 'Polypane/', '/Polypane\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('OhHai Browser', 'OhHaiBrowser/', '/OhHaiBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Sizzy', 'Sizzy', '/Sizzy\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('AOL Desktop', 'ADG/', '/ADG\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Elements Browser', 'Elements Browser', '/Elements\sBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('115 Browser', '115Browser', '/115Browser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Falkon', 'Falkon/', '/Falkon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('AOL Shield Pro', 'AOLShield', '/AOLShield\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Google Earth', 'Google Earth', '/Google Earth(?:|\sPro)\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Chedot', 'Chedot/', '/Chedot\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('SlimBoat', 'SlimBoat/', '/SlimBoat\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Rambler Browser', 'Nichrome/', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Beaker Browser', 'BeakerBrowser/', '/BeakerBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Amigo', 'Amigo/', '/Amigo\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Opera Neon', ' MMS/', '/\sMMS\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('SmartTV Browser', 'Thano/', '/Thano\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Biscuit', 'Biscuit/', '/Biscuit\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Chromium GOST', 'Chromium GOST', '/Chrome\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Dashob', 'Dashob/', '/Dashob\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Dezor', 'Dezor/', '/Dezor\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Duoyu Browser', 'duoyu_business/', '/duoyu_business\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('iTop Browser', ' iTop', '/Chrome\/([0-9]+\.[0-9]+)*.+\siTop$/', '1', '');
+ $browser_list[] = array('Polarity', 'Polarity/', '/Polarity\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Sielo', 'Sielo/', '/Sielo\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Norton Browser', 'Norton/', '/Norton\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Avira Browser', 'Avira/', '/Avira\/([0-9]+)/', '1', '');
+
+ // Webkit, Gecko and other engine based browsers
+
+ $browser_list[] = array('Cyberfox', 'Cyberfox/', '/Cyberfox\/([0-9]+)/', '1', '');
+ $browser_list[] = array('SeaMonkey', 'SeaMonkey/', '/SeaMonkey\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('K-Meleon', 'K-Meleon', '/K\-Meleon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Iceweasel', '/[iI]ce[wW]easel/', '/[iI]ce[wW]easel/', '1', '');
+ $browser_list[] = array('IceApe', 'Iceape/', '/Iceape\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Comodo Ice Dragon', 'IceDragon/', '/IceDragon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('QtWeb', 'QtWeb Internet Browser/', '/QtWeb\sInternet\sBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('QtWebEngine', 'QtWebEngine', '/QtWebEngine\/([0-9]+\.[0-9]+)/', '1', 'Konqueror');
+ $browser_list[] = array('Qt', 'Qt/', '/Qt\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('WebKitGTK', 'WebKitGTK+/', '/WebKitGTK\+\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Konqueror', 'Konqueror', '/Konqueror\/([0-9]+\.[0-9]+)/', '1', 'QtWebEngine');
+ $browser_list[] = array('Konqueror', 'konqueror', '/konqueror\/([0-9]+\.[0-9]+)/', '1', 'QtWebEngine');
+ $browser_list[] = array('Konqueror', 'Konqueror', '/\sQtWebEngine\/([0-9]+\.[0-9]+)(|[.0-9]+)\sChrome\/[.0-9]+.*\sSafari\/[.0-9]+.*Konqueror\s/', '1', '');
+ $browser_list[] = array('Epiphany', 'Epiphany', '/Epiphany\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('OmniWeb', 'OmniWeb/', '/OmniWeb\//', '1', '');
+ $browser_list[] = array('iCab', 'iCab', '/iCab(\s|\/)([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Camino', 'Camino/', '/Camino\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Midori', 'Midori|midori', '/Midori\/([.0-9]+)/', '1', '');
+ $browser_list[] = array('Otter', 'Otter/', '/Otter\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Kazehakase', 'Kazehakase/', '/Kazehakase\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('QupZilla', 'QupZilla/', '/QupZilla\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Waterfox', 'Waterfox/', '/Waterfox\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Waterfox', 'Waterfox', '/Firefox\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Basilisk', 'Basilisk/', '/\srv:([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Dooble', 'Dooble/', '/Dooble\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Fluid', 'Fluid/', '/Fluid\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Arora', 'Arora/', '/Arora\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Artis Browser', 'ArtisBrowser/', '/ArtisBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Steam Client', ' Valve ', '/Valve\s(|Steam\s)Client/', '1', 'Tenfoot');
+ $browser_list[] = array('Steam Overlay', ' Valve ', '/Valve\sSteam\sGameOverlay/', '1', 'Tenfoot|Client/');
+ $browser_list[] = array('Rekonq', ' rekonq', '/rekonq\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Odyssey Web Browser', 'Odyssey Web Browser', '/OWB\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Safari SDK', '/^Safari\/[.0-9]+\sCFNetwork\/[.0-9]+\sDarwin\/[.0-9]+/', '/Safari\//', '1', '');
+ $browser_list[] = array('Internet TV Browser', 'InettvBrowser/', '/InettvBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox', 'BonEcho/', '/BonEcho\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox', 'GranParadiso/', '/GranParadiso\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox', 'Shiretoko/', '/Shiretoko\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox', 'Namoroka/', '/Namoroka\/([0-9]+)/', '1', '');
+ $browser_list[] = array('iTunes App', 'iTunes/', '/iTunes\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Mypal Browser', 'Mypal/', '/Mypal\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Mercury', 'Mercury/', '/Firefox\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Arctic Fox', 'ArcticFox/', '/ArcticFox\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Roccat', ' Roccat/', '/Roccat\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Lunascape', ' lswebkit ', ' lswebkit ', '1', '');
+
+ foreach ($browser_list as $browser_list_va) {
+ $match = $browser_list_va[1];
+ $match_exclude = $browser_list_va[4];
+
+ if ($this->match_ua($match) && !$this->match_ua($match_exclude)) {
+ $this->result_browser_version = 0;
+ $this->result_browser_name = $browser_list_va[0];
+ $matches = $this->match_ua($browser_list_va[2]);
+ $matches_n = intval($browser_list_va[3]);
+ $this->result_browser_version = is_array($matches) ? (float)$matches[$matches_n] : 0;
+
+ // Gnome Web browser detection on Linux/Unix OS
+
+ if ($this->result_browser_name === 'Safari' && ($this->result_os_family === 'linux' || $this->result_os_family === 'unix')) {
+ $this->result_browser_name = 'Gnome Web';
+ }
+
+ // Safari old version conversion
+
+ if ($match === '/AppleWebKit\/[.0-9]+.*Gecko\)\sSafari\/[.0-9A-Za-z]+$/') {
+ $ev = intval($this->result_browser_version);
+ if (!empty($ev)) {
+ $this->result_browser_version = 1;
+ }
+ if ($ev > 400) {
+ $this->result_browser_version = 2;
+ }
+ if ($ev > 500) {
+ $this->result_browser_version = 3;
+ }
+ if ($ev > 527) {
+ $this->result_browser_version = 4;
+ }
+ if ($ev > 532) {
+ $this->result_browser_version = 5;
+ }
+ if ($ev > 535) {
+ $this->result_browser_version = 6;
+ }
+ $this->result_browser_safari_original = 1;
+ }
+
+ // IE Trident engine version conversion
+
+ if ($match === 'Trident/') {
+ if ($this->result_browser_version == 4) {
+ $this->result_browser_version = 8;
+ }
+ if ($this->result_browser_version == 5) {
+ $this->result_browser_version = 9;
+ }
+ if ($this->result_browser_version == 6) {
+ $this->result_browser_version = 10;
+ }
+ if ($this->result_browser_version == 7) {
+ $this->result_browser_version = 11;
+ }
+ }
+
+ if (!empty($this->result_browser_version)) {
+ $browser_need_continue = false;
+ break;
+ }
+ }
+ }
+
+ $browser_list = null;
+
+ // End of desktop browsers detection
+ }
+
+ /*
+ -----------------
+ Mobile Browsers
+ -----------------
+ */
+
+ if ($browser_need_continue && $this->result_browser_chrome_original == 0 && $this->result_browser_firefox_original == 0 && $this->result_mobile == 1) {
+ // Mobile browsers with detectable versions
+
+ $browser_list[] = array('Safari Mobile', '/(iPhone|iphone|iPad;|iPod).*AppleWebKit\/[.0-9]+\s\(KHTML,\slike\sGecko\)\s.*Version\/[.,0-9]+\sMobile\//', '/Version\/([0-9]+\.[0-9]+)(|\.[0-9]+|\,[0-9]+)\sMobile\//', '1', 'RDDocuments|AlohaBrowser|DuckDuckGo|MiuiBrowser|Snapchat|NAVER(inapp;|1Password|OPT/|Ddg/');
+ $browser_list[] = array('Safari Mobile', '/(Intel\sMac\sOS\sX).*AppleWebKit\/.*Version\/[.,0-9]+\s(?:|Mobile\/\w+\s)Safari\/[.0-9A-Za-z]+(|\/[0-9]+|\s\(.*\))+$/', '/Version\/([0-9]+\.[0-9]+)(|\.[0-9]+|\,[0-9]+)/', '1', 'CriOS|RDDocuments|AlohaBrowser|DuckDuckGo|MiuiBrowser|Snapchat|NAVER(inapp;|1Password|OPT/|Ddg/');
+ $browser_list[] = array('Android Browser', '/Android.*Version\/[.0-9]+\s(?:Mobile\s)?Safari\/[.0-9]+(|\-[0-9]+)$/', '/Android.*Version\/([0-9]+\.[0-9]+)/', '1', 'Chrome/');
+ $browser_list[] = array('Android Browser', 'Dalvik/', '/Dalvik\/([.0-9]+)\s\(Linux;\sU;\sAndroid\s/', '2', 'Chrome/');
+ $browser_list[] = array('Samsung Browser', 'SamsungBrowser', '/SamsungBrowser\/([0-9]+\.[0-9]+)/', '1', 'CrossApp');
+ $browser_list[] = array('Firefox Focus', 'Focus/', '/Focus\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox iOS', 'FxiOS', '/FxiOS\/([0-9]*[.]?[0-9]+)/', '1', '');
+ $browser_list[] = array('Opera Mini', 'Opera Mini|OPiOS', '/(Opera Mini|OPiOS)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Opera Touch', 'OPT/', '/OPT\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Huawei Browser', 'HuaweiBrowser/', '/HuaweiBrowser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('DuckDuckGo', 'DuckDuckGo/', '/DuckDuckGo\/([0-9]+)/', '1', '');
+ $browser_list[] = array('DuckDuckGo', 'Ddg/', '/Ddg\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Mi Browser', 'MiuiBrowser/', '/MiuiBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Mint Browser', 'Mint Browser/', '/Mint\sBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Avast Browser', 'AvastSecureBrowser/', '/AvastSecureBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Google App', '/(iPhone|iphone|iPad;|iPod).*\)\sGSA\/([0-9]+)/', '/(iPhone|iphone|iPad;|iPod).*\)\sGSA\/([0-9]+)/', '2', '');
+ $browser_list[] = array('Google App', '/\sGSA\//', '/\sGSA\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Facebook App', 'FBAV/|FBSV/', '/(FBAV|FBSV)\/([0-9]+)\./', '2', 'FBAN/Messenger|FB_IAB/MESSENGER');
+ $browser_list[] = array('Instagram App', 'Instagram', '/Instagram\s([0-9]+)\./', '1', '');
+ $browser_list[] = array('Facebook Messenger', 'FBAN/Messenger|FB_IAB/MESSENGER', '/(FBAV|FBSV)\/([0-9]+)\./', '2', '');
+ $browser_list[] = array('Snapchat', 'Snapchat', '/\sSnapchat\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('WhatsApp', 'WhatsApp', '/WhatsApp\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Viber', 'Viber/', '/Viber\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Yandex App', 'YaApp_', '/YaApp_(Android|iOS)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Yandex App', 'YandexSearch/', '/YandexSearch\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Yandex App', 'YaSearchBrowser/', '/YaSearchBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Dolfin', 'Dolfin/', '/Dolfin\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Flipboard App', 'Flipboard/', '/Flipboard\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('FreeU', 'FreeU|Freeu', '/(FreeU|Freeu)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Iron', 'Iron Safari|MobileIron', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Sogou Mobile Browser', 'SogouMobileBrowser/', '/SogouMobileBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Meizu Browser', 'MZBrowser/', '/MZBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('360 Mobile Browser', '360 Aphone', '/360\sAphone\sBrowser\s\(([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Adblock Browser', 'ABB/', '/ABB\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Ecosia Browser', 'Ecosia', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('HeyTap Browser', 'HeyTapBrowser/', '/HeyTapBrowser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Oppo Browser', 'OppoBrowser/', '/OppoBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Silk', 'Silk/', '/Silk\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Surf Browser', 'SurfBrowser/', '/SurfBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Phoenix Browser', 'PHX/', '/PHX\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('CM Mobile', 'ACHEETAHI', '/Chrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Bing App', ' BingWeb', '/BingWeb\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Firefox Klar', 'Klar/', '/Klar\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Super Fast Browser', 'SFBrowser|tssomas', '/(SFBrowser|tssomas)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Tenta Browser', 'Tenta/', '/Tenta\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Kiwi Browser', 'Kiwi Chrome/', '/Kiwi\sChrome\/([0-9]+)/', '1', '');
+ $browser_list[] = array('JioPages', 'JioBrowser/', '/JioBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('JioPages', 'JioPages/', '/JioPages\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Qwant Mobile', 'QwantBrowser', '/QwantBrowser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Qwant Mobile', 'QwantMobile', '/QwantMobile\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Cake Browser', '; Cake) ', '/\;\sCake\).*AppleWebKit.*Version\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('DU Browser', 'bdbrowser', '/bdbrowser(|_i18n)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('DU Browser', 'baidubrowser', '/baidubrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Baidu Search App', 'baiduboxapp/', '/baiduboxapp\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('DU Browser HD', 'BaiduHD/', '/BaiduHD\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Hi Browser', 'HiBrowser/', '/HiBrowser\/?(?:v)?([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('VmWare Browser', 'AirWatch Browser', '/AirWatch\sBrowser\sv([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Iron Mobile', 'MobileIron/', '/MobileIron\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('NineSky', 'Ninesky-android-mobile/', '/Ninesky\-android\-mobile\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Realme Browser', 'RealmeBrowser/', '/RealmeBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Vivo Browser', 'VivoBrowser/', '/VivoBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Web Explorer', 'Web Explorer/', '/Web\sExplorer\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('NTENT Browser', 'NTENTBrowser/', '/NTENTBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Nox Browser', 'NoxBrowser/', '/NoxBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Quark Browser', 'Quark/', '/Quark\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Yaani Browser', 'YaaniBrowser', '/YaaniBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Tizen Browser', '/Tizen(?: )?(?:\/)?[.0-9]+.*AppleWebKit\/.*(Version|SLP Browser|Tizen Browser|TizenBrowser)\/([0-9]+\.[0-9]+)/', '/Tizen(?: )?(?:\/)?[.0-9]+.*AppleWebKit\/.*(Version|SLP Browser|Tizen Browser|TizenBrowser)\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Internet Explorer Mobile', 'IEMobile', '/IEMobile(?: )?(?:\/)?([0-9]+)/', '1', '');
+ $browser_list[] = array('Internet Explorer Mobile', 'Trident/', '/Trident\/[.0-9].*Touch;*.rv\:([0-9]+)/', '1', '');
+ $browser_list[] = array('ZetaKey', 'Zetakey/', '/Zetakey\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Nokia Browser', 'NokiaBrowser/', '/NokiaBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('NetFront', 'NetFront/', '/NetFront\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('BlackBerry Browser', '/(BB10;|BlackBerry|PlayBook).*AppleWebKit.*Version\/([0-9]+\.[0-9]+)/', '/(BB10;|BlackBerry|PlayBook).*AppleWebKit.*Version\/([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('ONE Browser', 'OneBrowser/', '/OneBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Web Explorer', 'webexplorer/', '/webexplorer\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Start Internet Browser', 'Start/', '/Start\/([0-9]+)/', '1', '');
+ $browser_list[] = array('EUI Browser', 'EUI Browser', '/EUI\sBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('iCab Mobile', 'iCabMobile', '/iCabMobile(\s|\/)([0-9]+\.[0-9]+)/', '2', '');
+ $browser_list[] = array('Mercury', 'Mercury/', '/Mercury\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Samsung Browser', '; SAMSUNG S|; SAMSUNG G', '/\;\sSAMSUNG\s(S|G)+.*\sBuild\/.*\)\sAppleWebKit\/.*Version\/([0-9]+\.[0-9]+)\sChrome\/[.0-9]+.*\sSafari\/[.0-9]+$/', '2', 'SamsungBrowser');
+ $browser_list[] = array('Documents App', 'RDDocuments/', '/RDDocuments\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Opera Coast', 'Coast/', '/Coast\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Android Browser', '/Android.*Version\/[.0-9]+\s(?:Mobile\s)?Safari(|\/[.0-9]+\sCyanogenMod.*)+$/', '/Android.*Version\/([0-9]+\.[0-9]+)/', '1', 'Chrome/');
+ $browser_list[] = array('Playstation Browser', '/(PlayStation\s|PLAYSTATION\s)/', '/(PlayStation\s|PLAYSTATION\s)/', '1', '');
+ $browser_list[] = array('Naver App', 'NAVER(inapp;', '/NAVER\(inapp;\ssearch;\s\d+;\s([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Line App', ' Line/', '/\sLine\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('QQ App', ' QQ/', '/\sQQ\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('GNews App', ' GNews ', '/\sGNews\s.*\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('1Password App', '1Password', '/\s1Password\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Pinterest App', 'Pinterest', '/Pinterest/', '1', '');
+ $browser_list[] = array('Twitter App', ' Twitter', '/Twitter/', '1', '');
+ $browser_list[] = array('Ali App', ' AliApp', '/\sAliApp/', '1', '');
+ $browser_list[] = array('Alipay', 'AlipayClient', '/AlipayClient\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Samsung CrossApp', 'CrossApp/', '/CrossApp\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Diigo', 'DiigoBrowser', '/DiigoBrowser/', '1', '');
+ $browser_list[] = array('Stargon', 'Stargon/', '/Stargon\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('You Browser', 'YouBrowser/', '/YouBrowser\/([0-9]+\.[0-9]+)/', '1', '');
+ $browser_list[] = array('Max Browser', 'MaxBrowser/', '/MaxBrowser\/([0-9]+)/', '1', '');
+ $browser_list[] = array('Soul Browser', 'Soul/', '/Soul\/([0-9]+)/', '1', '');
+
+ foreach ($browser_list as $browser_list_va) {
+ $match = $browser_list_va[1];
+ $match_exclude = $browser_list_va[4];
+
+ // Some OS specified browsers matching optimization
+
+ $current_browser_continue = true;
+ if ($this->get_mode !== 'browser') {
+ $current_browser = $browser_list_va[0];
+
+ // Some iOS specified browsers matching optimization
+
+ if ($current_browser === 'Safari Mobile' || $current_browser === 'Firefox iOS') {
+ if ($this->result_os_name !== 'iOS') {
+ $current_browser_continue = false;
+ }
+ }
+
+ // Some Android specified browsers matching optimization
+
+ if ($current_browser === 'Android Browser' || $current_browser === 'Samsung Browser' || $current_browser === 'Huawei Browser') {
+ if ($this->result_os_name !== 'Android') {
+ $current_browser_continue = false;
+ }
+ }
+
+ // WP specified browser matching optimization
+
+ if ($current_browser === 'Internet Explorer Mobile' && $this->result_os_name !== 'Windows Phone' && $this->result_os_name !== 'Windows Mobile') {
+ $current_browser_continue = false;
+ }
+
+ // Tizen specified browser matching optimization
+
+ if ($current_browser === 'Tizen Browser' && $this->result_os_name !== 'Tizen') {
+ $current_browser_continue = false;
+ }
+
+ // BlackBerry specified browser matching optimization
+
+ if ($current_browser === 'BlackBerry Browser' && $this->result_os_name !== 'BlackBerry') {
+ $current_browser_continue = false;
+ }
+
+ // Google App matching optimization
+
+ if ($current_browser === 'Google App' && !$this->match_ua(' GSA/')) {
+ $current_browser_continue = false;
+ }
+ }
+
+ if ($current_browser_continue == true && $this->match_ua($match) && !$this->match_ua($match_exclude)) {
+ $this->result_browser_version = 0;
+ $this->result_browser_name = $browser_list_va[0];
+ $matches = $this->match_ua($browser_list_va[2]);
+ $matches_n = intval($browser_list_va[3]);
+
+ if (is_array($matches)) {
+ if ($this->result_browser_name === 'Safari Mobile' && strpos($matches[$matches_n], '_') !== false) {
+ $matches[$matches_n] = str_replace('_', '.', $matches[$matches_n]);
+ }
+ $this->result_browser_version = (float)$matches[$matches_n];
+ } else {
+ $this->result_browser_version = 0;
+ }
+
+ if (!empty($this->result_browser_version)) {
+ $browser_need_continue = false;
+ break;
+ }
+ }
+ }
+
+ $browser_list = null;
+
+ // End of mobile browsers detection
+ }
+
+ // Other browsers without version specified
+
+ if ($browser_need_continue) {
+ $other_bsr = array();
+
+ $other_bsr[] = array('Maple' => 'Maple');
+ $other_bsr[] = array('Espial' => 'Espial');
+
+ foreach ($other_bsr as $other_bsr_va) {
+ $k = key($other_bsr_va);
+ $v = $other_bsr_va[$k];
+
+ if ($this->match_ua($v)) {
+ $this->result_browser_version = 0;
+ $this->result_browser_name = $k;
+ $this->result_browser_title = $k;
+ break;
+ }
+ }
+ $other_bsr = null;
+ }
+
+ // MacOS and iOS Apps
+
+ if ($this->result_browser_name === 'unknown' && $this->match_ua('CFNetwork/') && $this->match_ua('Darwin/') && !$this->match_ua('Safari/')) {
+ $matches = $this->match_ua('/^(.*)(\/[.0-9]+|\s\(unknown\sversion\))(.*)\sCFNetwork\/[.0-9]+\sDarwin\/[.0-9]+/');
+ if (!empty($matches[1])) {
+ $matches[1] = str_replace('%20', ' ', $matches[1]);
+ $matches[1] = str_replace('com.apple.Safari.', '', $matches[1]);
+ $matches[1] = str_replace('com.apple.WebKit.', '', $matches[1]);
+ $matches[1] = str_replace('com.apple.Notes.', '', $matches[1]);
+ $matches[1] = str_replace('com.apple.mobilenotes.', '', $matches[1]);
+ $this->result_browser_name = $matches[1] . ' App';
+ if ($this->result_browser_name === 'Browser App') {
+ $this->result_browser_name = 'Darwin Browser';
+ }
+ $darwin_app = true;
+ }
+ }
+
+ /*
+ -------------------
+ WebView detection
+ -------------------
+ */
+
+ if ($this->result_mobile == 1) {
+ // Android WebView
+
+ if ($this->result_os_name === 'Android') {
+ if ($this->match_ua('; wv|;FB|FB_IAB|OKApp|DuckDuckGo')) {
+ $this->result_browser_android_webview = 1;
+ }
+ if ($this->result_browser_chrome_original == 0 && $this->result_browser_chromium_version != 0 && $this->match_ua('/like\sGecko\)\sVersion\/[.0-9]+\sChrome\/[.0-9]+\s/')) {
+ $this->result_browser_android_webview = 1;
+ }
+ }
+
+ // WKWebView
+
+ if ($this->result_ios) {
+ $webkit_webview = false;
+
+ if ($this->match_ua('/\s\((iPhone|iphone|iPad|iPod);.*\)\sAppleWebKit\/[.0-9]+\s\(KHTML\,\slike Gecko\)\s(?!CriOS|FxiOS|OPiOS|EdgiOS)(?!Version).*Mobile\/([0-9A-Z]+)/')) {
+ $webkit_webview = true;
+ }
+ if ($this->result_browser_name === 'unknown' && $this->match_ua('MobileSafari/') && $this->match_ua('CFNetwork/')) {
+ $webkit_webview = true;
+ }
+
+ if ($webkit_webview) {
+ $this->result_browser_ios_webview = 1;
+
+ if ($this->result_browser_name === 'unknown') {
+ $this->result_browser_name = 'WKWebView';
+ $this->result_browser_version = 0;
+ }
+ }
+ }
+ }
+
+ /*
+ -------------------------
+ Detect Safari original
+ -------------------------
+ */
+
+ if ($this->result_browser_name === 'Safari' || $this->result_browser_name === 'Safari Mobile') {
+ if ($this->match_ua('/AppleWebKit\/[.0-9]+.*Gecko\)\sVersion\/[.0-9].*Safari\/[.0-9A-Za-z]+$/')) {
+ $this->result_browser_safari_original = 1;
+ }
+ }
+
+ // Check and correct browser version anomaly
+
+ if (intval($this->result_browser_version) > 200 && !$this->match_ua('FBAV/|FBSV/|GSA/|Instagram')) {
+ $this->result_browser_version = 0;
+ }
+
+ // Set Browser title
+
+ if (!empty($this->result_browser_version)) {
+ $this->result_browser_title = $this->result_browser_name . ' ' . $this->result_browser_version;
+ } else {
+ $this->result_browser_title = $this->result_browser_name . ' (unknown version)';
+
+ $browsers_without_versions = array();
+ $browsers_without_versions[] = 'Android Browser';
+ $browsers_without_versions[] = 'WKWebView';
+ $browsers_without_versions[] = 'Safari SDK';
+ $browsers_without_versions[] = 'Playstation Browser';
+ $browsers_without_versions[] = 'OmniWeb';
+ $browsers_without_versions[] = 'Steam Client';
+ $browsers_without_versions[] = 'Steam Overlay';
+ $browsers_without_versions[] = 'Maple';
+ $browsers_without_versions[] = 'Espial';
+ $browsers_without_versions[] = 'Diigo Browser';
+ $browsers_without_versions[] = 'IceWeasel';
+ $browsers_without_versions[] = 'Iceweasel';
+ $browsers_without_versions[] = 'Facebook App';
+ $browsers_without_versions[] = 'Twitter App';
+ $browsers_without_versions[] = 'Bing App';
+ $browsers_without_versions[] = 'Pinterest App';
+ $browsers_without_versions[] = 'Ali App';
+ $browsers_without_versions[] = 'Alipay App';
+ $browsers_without_versions[] = 'Lunascape';
+
+ if (in_array($this->result_browser_name, $browsers_without_versions) || isset($darwin_app)) {
+ $this->result_browser_title = $this->result_browser_name;
+ }
+
+ $browsers_without_versions = null;
+ }
+
+ if (strpos($this->result_browser_name, 'unknown') !== false) {
+ $this->result_browser_title = 'unknown';
+ }
+ if ($this->result_browser_version == null) {
+ $this->result_browser_version = 0;
+ }
+
+ // EdgeHTML browser should not be detected as a Chromium engine
+ if ($this->result_browser_name === 'Edge' && $this->result_browser_version >= 12 && $this->result_browser_version <= 18) {
+ $this->result_browser_chromium_version = 0;
+ }
+
+ if ($this->get_mode === 'browser') {
+ return null;
+ }
+
+ /*
+ ---------------------
+ Detect device types
+ ---------------------
+ */
+
+ // Desktop
+
+ if ($this->result_os_family !== 'unknown' && $this->result_os_type !== 'mixed') {
+ $this->result_device_type = 'desktop';
+ }
+
+ // Mobile
+
+ if ($this->result_mobile == 1) {
+ $this->result_device_type = 'mobile';
+ }
+
+ // TV
+
+ if ($this->match_ua('TV|HDMI|CrKey| Escape |Kylo/|SmartLabs|SC/IHD|Viera|BRAVIA|NetCast|Roku/DVP| Roku |Maple|DuneHD|CE-HTML|EIS iPanel|Sunniwell; Resolution|Freebox|Netbox|Netgem|AFTT|AFTM|AFTB|DLNADOC| iconBIT |olleh tv|stbapp |; MIBOX|ABOX-I|; H96 |; X96|HX S905|; M8S |MINIM8S|MXIII-|; NEO-X|; NEO-U| Nexus Player |TPM171E|; V88 |MXQPRO|NEXBOX-|; Leelbox|ZIDOO|; A95X| Beelink |; T95Z|; TX3 ') && !$this->match_ua('BTV-')) {
+ $this->result_device_type = 'tv';
+ }
+
+ // Console
+
+ if ($this->matchi_ua('playstation') || $this->match_ua('Xbox|GAMEPAD|Nintendo|OUYA|; SHIELD Build')) {
+ $this->result_device_type = 'console';
+ }
+
+ // MediaPlayer
+
+ if ($this->match_ua('iPod|AlexaMediaPlayer|AppleCoreMedia')) {
+ $this->result_device_type = 'mediaplayer';
+ }
+
+ // Car
+
+ if ($this->match_ua('CarBrowser|Tesla/')) {
+ $this->result_device_type = 'car';
+ }
+
+ // Watch
+
+ if ($this->matchi_ua('watch') && !$this->match_ua('AirWatch')) {
+ $this->result_device_type = 'watch';
+ }
+
+ if ($this->get_mode === 'device') {
+ return null;
+ }
+
+ /*
+ ---------------------------------------------
+ Detect 64bits mode (browser + OS specified)
+ ---------------------------------------------
+ */
+
+ if ($this->result_os_name === 'MacOS' && $this->macos_version_minor >= 6) {
+ if ($this->result_browser_name === 'Safari' || $this->result_browser_name === 'Safari SDK') {
+ $this->result_64bits_mode = 1;
+ }
+ }
+
+ /*
+ ---------------------------------------------------------
+ MacOS Big Sur + Firefox version > 86 detection feature
+ ---------------------------------------------------------
+ */
+
+ if ($this->result_os_name === 'MacOS' && $this->macos_version_minor == 15 && $this->result_browser_name === 'Firefox' && $this->result_browser_version > 86) {
+ $this->result_os_version = 'Big Sur';
+ $this->result_os_title = 'MacOS ' . $this->result_os_version;
+ }
+
+ return null;
+ }
+
+ /**
+ * The method returns all possible environment data from User-Agent
+ *
+ * @param string $ua The User-Agent string
+ * @param string $result_format If specified, method returns result as JSON
+ * @return mixed Returns User-Agent parsing result as array or JSON string (optional)
+ */
+
+ public function getAll($ua, $result_format = '')
+ {
+ $this->useragent = trim($ua);
+ $this->get_mode = 'all';
+ $this->resetProperties();
+ $this->getResult();
+ $result = array(
+ 'os_type' => $this->result_os_type,
+ 'os_family' => $this->result_os_family,
+ 'os_name' => $this->result_os_name,
+ 'os_version' => $this->result_os_version,
+ 'os_title' => $this->result_os_title,
+ 'device_type' => $this->result_device_type,
+ 'browser_name' => $this->result_browser_name,
+ 'browser_version' => $this->result_browser_version,
+ 'browser_title' => $this->result_browser_title,
+ 'browser_chrome_original' => $this->result_browser_chrome_original,
+ 'browser_firefox_original' => $this->result_browser_firefox_original,
+ 'browser_safari_original' => $this->result_browser_safari_original,
+ 'browser_chromium_version' => $this->result_browser_chromium_version,
+ 'browser_gecko_version' => $this->result_browser_gecko_version,
+ 'browser_webkit_version' => $this->result_browser_webkit_version,
+ 'browser_android_webview' => $this->result_browser_android_webview,
+ 'browser_ios_webview' => $this->result_browser_ios_webview,
+ 'browser_desktop_mode' => $this->result_browser_desktop_mode,
+ '64bits_mode' => $this->result_64bits_mode
+ );
+
+ if (strtolower($result_format) === 'json') {
+ $result = json_encode($result);
+ }
+ $this->touch_support_mode = false;
+ return $result;
+ }
+
+ /**
+ * The method returns OS data from User-Agent
+ *
+ * @param string $ua The User-Agent string
+ * @param string $result_format If specified, method returns result as JSON
+ * @return mixed Returns User-Agent parsing result as array or JSON string (optional)
+ */
+
+ public function getOS($ua, $result_format = '')
+ {
+ $this->useragent = trim($ua);
+ $this->get_mode = 'os';
+ $this->resetProperties();
+ $this->getResult();
+ $result = array(
+ 'os_type' => $this->result_os_type,
+ 'os_family' => $this->result_os_family,
+ 'os_name' => $this->result_os_name,
+ 'os_version' => $this->result_os_version,
+ 'os_title' => $this->result_os_title,
+ '64bits_mode' => $this->result_64bits_mode
+ );
+
+ if (strtolower($result_format) === 'json') {
+ $result = json_encode($result);
+ }
+ $this->touch_support_mode = false;
+ return $result;
+ }
+
+ /**
+ * The method returns Browser data from User-Agent
+ *
+ * @param string $ua The User-Agent string
+ * @param string $result_format If specified, method returns result as JSON
+ * @return mixed Returns User-Agent parsing result as array or JSON string (optional)
+ */
+
+ public function getBrowser($ua, $result_format = '')
+ {
+ $this->useragent = trim($ua);
+ $this->get_mode = 'browser';
+ $this->resetProperties();
+ $this->getResult();
+ $result = array(
+ 'browser_name' => $this->result_browser_name,
+ 'browser_version' => $this->result_browser_version,
+ 'browser_title' => $this->result_browser_title,
+ 'browser_chrome_original' => $this->result_browser_chrome_original,
+ 'browser_firefox_original' => $this->result_browser_firefox_original,
+ 'browser_safari_original' => $this->result_browser_safari_original,
+ 'browser_chromium_version' => $this->result_browser_chromium_version,
+ 'browser_gecko_version' => $this->result_browser_gecko_version,
+ 'browser_webkit_version' => $this->result_browser_webkit_version,
+ 'browser_android_webview' => $this->result_browser_android_webview,
+ 'browser_ios_webview' => $this->result_browser_ios_webview,
+ 'browser_desktop_mode' => $this->result_browser_desktop_mode
+ );
+
+ if (strtolower($result_format) === 'json') {
+ $result = json_encode($result);
+ }
+ $this->touch_support_mode = false;
+ return $result;
+ }
+
+ /**
+ * The method returns Device Type data from User-Agent
+ *
+ * @param string $ua The User-Agent string
+ * @param string $result_format If specified, method returns result as JSON
+ * @return mixed Returns User-Agent parsing result as array or JSON string (optional)
+ */
+
+ public function getDevice($ua, $result_format = '')
+ {
+ $this->useragent = trim($ua);
+ $this->get_mode = 'device';
+ $this->resetProperties();
+ $this->getResult();
+ $result = array('device_type' => $this->result_device_type);
+
+ if (strtolower($result_format) === 'json') {
+ $result = json_encode($result);
+ }
+ $this->touch_support_mode = false;
+ return $result;
+ }
+
+ /**
+ * This method is needed to detect mobile browsers in Desktop Mode (Android and iOS)
+ * The method should call if browser supports Touch events (client-side JS detection is necessary)
+ *
+ * @return null
+ */
+
+ public function setTouchSupport()
+ {
+ $this->touch_support_mode = true;
+ return null;
+ }
+}
diff --git a/api/app/Service/Forms/Analytics/IpLocationService.php b/api/app/Service/Forms/Analytics/IpLocationService.php
new file mode 100644
index 000000000..5a13da0d2
--- /dev/null
+++ b/api/app/Service/Forms/Analytics/IpLocationService.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace App\Service\Forms\Analytics;
+
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+
+class IpLocationService
+{
+ private const CACHE_PREFIX = 'ip_location:';
+
+ /**
+ * Get location data for an IP address with caching
+ */
+ public function getLocationData(string $ip): array
+ {
+ // Don't process local/private IPs
+ if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+ return $this->getDefaultLocationData();
+ }
+
+ $cacheKey = $this->getCacheKey($ip);
+
+ // Try to get from cache first
+ $cachedData = Cache::get($cacheKey);
+ if ($cachedData !== null) {
+ return $cachedData;
+ }
+
+ // Fetch from API
+ $locationData = $this->fetchFromApi($ip);
+
+ // Cache the result (even if empty to avoid repeated API calls)
+ $cacheTtl = config('services.ipinfo.cache_ttl_hours', 24);
+ Cache::put($cacheKey, $locationData, now()->addHours($cacheTtl));
+
+ return $locationData;
+ }
+
+ /**
+ * Fetch location data from IPINFO API
+ */
+ private function fetchFromApi(string $ip): array
+ {
+ $token = config('services.ipinfo.token');
+
+ if (!$token) {
+ return $this->getDefaultLocationData();
+ }
+
+ try {
+ $url = "https://api.ipinfo.io/lite/{$ip}?token={$token}";
+ $timeout = config('services.ipinfo.request_timeout', 5);
+ $response = Http::timeout($timeout)->get($url);
+
+ if ($response->successful()) {
+ $data = $response->json();
+ return $this->formatLocationData($data);
+ }
+
+ Log::warning('IPINFO API request failed', [
+ 'ip' => $ip,
+ 'status' => $response->status(),
+ 'response' => $response->body()
+ ]);
+ } catch (\Exception $e) {
+ Log::error('IPINFO API request exception', [
+ 'ip' => $ip,
+ 'error' => $e->getMessage()
+ ]);
+ }
+
+ return $this->getDefaultLocationData();
+ }
+
+ /**
+ * Format and validate location data from API response
+ */
+ private function formatLocationData(array $data): array
+ {
+ return [
+ 'country' => $data['country'] ?? 'Unknown',
+ 'region' => $data['region'] ?? null,
+ 'city' => $data['city'] ?? null,
+ 'timezone' => $data['timezone'] ?? null,
+ 'org' => $data['org'] ?? null,
+ ];
+ }
+
+ /**
+ * Get default location data when API is unavailable
+ */
+ private function getDefaultLocationData(): array
+ {
+ return [
+ 'country' => 'Unknown',
+ 'region' => null,
+ 'city' => null,
+ 'timezone' => null,
+ 'org' => null,
+ ];
+ }
+
+ /**
+ * Generate cache key for IP address
+ */
+ private function getCacheKey(string $ip): string
+ {
+ return self::CACHE_PREFIX . md5($ip);
+ }
+
+ /**
+ * Clear cache for a specific IP (useful for testing)
+ */
+ public function clearCache(string $ip): bool
+ {
+ return Cache::forget($this->getCacheKey($ip));
+ }
+
+ /**
+ * Get cache statistics (for monitoring)
+ */
+ public function getCacheStats(array $ips): array
+ {
+ $stats = [
+ 'cached' => 0,
+ 'missing' => 0,
+ 'total' => count($ips)
+ ];
+
+ foreach ($ips as $ip) {
+ if (Cache::has($this->getCacheKey($ip))) {
+ $stats['cached']++;
+ } else {
+ $stats['missing']++;
+ }
+ }
+
+ return $stats;
+ }
+}
diff --git a/api/app/Service/Forms/Analytics/UserAgentHelper.php b/api/app/Service/Forms/Analytics/UserAgentHelper.php
new file mode 100644
index 000000000..2a9df7747
--- /dev/null
+++ b/api/app/Service/Forms/Analytics/UserAgentHelper.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Service\Forms\Analytics;
+
+use Illuminate\Http\Request;
+
+class UserAgentHelper
+{
+ private $browser;
+ private $request;
+ private $userAgent;
+ private $ipLocationService;
+
+ public function __construct(Request $request, ?IpLocationService $ipLocationService = null)
+ {
+ $this->browser = new BrowserDetection();
+ $this->request = $request;
+ $this->userAgent = $request->userAgent() ?? '';
+ $this->ipLocationService = $ipLocationService ?? new IpLocationService();
+ }
+
+ public function getMetadata(): array
+ {
+ // Get browser metadata
+ $result = $this->browser->getAll($this->userAgent);
+
+ // Get location info
+ $location = $this->getLocation();
+
+ // Return metadata
+ return [
+ 'ip' => $this->getHashedIp(),
+ 'source' => ucfirst($this->getTrafficSource()),
+ 'device' => $result['device_type'],
+ 'browser' => $result['browser_name'],
+ 'os' => $result['os_name'],
+ 'country' => $location['country'] ?? 'Unknown'
+ ];
+ }
+
+ // Get Traffic Source from Referer
+ public function getTrafficSource(): string
+ {
+ $referer = $this->request->header('Referer');
+ $origin = $this->request->header('Origin');
+ if (!$referer) {
+ return 'Direct';
+ }
+
+ // Guard against null/empty origin to avoid errors in str_starts_with
+ if (is_string($origin) && $origin !== '' && str_starts_with($referer, $origin)) {
+ return 'Direct';
+ }
+
+ $host = parse_url($referer, PHP_URL_HOST);
+ if (!$host) {
+ return 'Unknown';
+ }
+
+ // Match common sources
+ $patterns = [
+ 'google' => '/google\./i',
+ 'facebook' => '/facebook\.|fb\./i',
+ 'twitter' => '/twitter\.|t\.co/i',
+ 'linkedin' => '/linkedin\./i',
+ 'instagram' => '/instagram\.|ig\./i',
+ 'youtube' => '/youtube\./i',
+ 'bing' => '/bing\./i',
+ 'yahoo' => '/yahoo\./i',
+ ];
+
+ foreach ($patterns as $source => $pattern) {
+ if (preg_match($pattern, $host)) {
+ return ucfirst($source);
+ }
+ }
+
+ // If no match found, return the domain as the source
+ return preg_replace('/^www\./', '', $host);
+ }
+
+ // Get Location from IP (cached)
+ public function getLocation(): array
+ {
+ return $this->ipLocationService->getLocationData($this->request->ip());
+ }
+
+ // Get hashed IP with daily rotating salt for privacy
+ private function getHashedIp(): string
+ {
+ $ip = $this->request->ip();
+ $dailySalt = date('Y-m-d'); // Rotates daily
+ return hash('sha256', $ip . $dailySalt);
+ }
+}
diff --git a/api/config/services.php b/api/config/services.php
index a7618183c..d03be3c19 100644
--- a/api/config/services.php
+++ b/api/config/services.php
@@ -89,5 +89,11 @@ return [
'client_id' => env('STRIPE_CLIENT_ID'),
'client_secret' => env('STRIPE_CLIENT_SECRET', env('STRIPE_SECRET')),
'redirect' => env('STRIPE_REDIRECT_URL', front_url('/oauth/stripe/callback')),
- ]
+ ],
+
+ 'ipinfo' => [
+ 'token' => env('IPINFO_TOKEN'),
+ 'cache_ttl_hours' => env('IPINFO_CACHE_TTL_HOURS', 24),
+ 'request_timeout' => env('IPINFO_REQUEST_TIMEOUT', 5),
+ ],
];
diff --git a/api/database/migrations/2025_08_15_045647_add_metadata_to_form_views_table.php b/api/database/migrations/2025_08_15_045647_add_metadata_to_form_views_table.php
new file mode 100644
index 000000000..fb3c31a3a
--- /dev/null
+++ b/api/database/migrations/2025_08_15_045647_add_metadata_to_form_views_table.php
@@ -0,0 +1,27 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class () extends Migration {
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::table('form_views', function (Blueprint $table) {
+ $table->json('meta')->nullable();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('form_views', function (Blueprint $table) {
+ $table->dropColumn('meta');
+ });
+ }
+};
diff --git a/api/database/migrations/2025_08_26_120000_add_indexes_to_form_views_table.php b/api/database/migrations/2025_08_26_120000_add_indexes_to_form_views_table.php
new file mode 100644
index 000000000..91ca9b1c2
--- /dev/null
+++ b/api/database/migrations/2025_08_26_120000_add_indexes_to_form_views_table.php
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class () extends Migration {
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::table('form_views', function (Blueprint $table) {
+ // Composite index matches WHERE form_id = ? AND created_at BETWEEN ... queries
+ $table->index(['form_id', 'created_at'], 'form_views_form_id_created_at_index');
+ // Keep single-column index for cleanup jobs on created_at alone
+ $table->index('created_at');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('form_views', function (Blueprint $table) {
+ $table->dropIndex('form_views_form_id_created_at_index');
+ $table->dropIndex(['created_at']);
+ });
+ }
+};
diff --git a/api/routes/api.php b/api/routes/api.php
index 6d43732c0..ac6e7c54d 100644
--- a/api/routes/api.php
+++ b/api/routes/api.php
@@ -306,6 +306,7 @@ Route::prefix('oauth')->name('oauth.')->group(function () {
*/
Route::prefix('forms')->name('forms.')->group(function () {
Route::middleware('protected-form')->group(function () {
+ Route::get('{slug}/view', [PublicFormController::class, 'view'])->name('view');
Route::post('{slug}/answer', [PublicFormController::class, 'answer'])->name('answer')->middleware(HandlePrecognitiveRequests::class);
Route::get('{slug}/stripe-connect/get-account', [FormPaymentController::class, 'getAccount'])->name('stripe-connect.get-account')->middleware(HandlePrecognitiveRequests::class);
Route::post('{slug}/stripe-connect/payment-intent', [FormPaymentController::class, 'createIntent'])->name('stripe-connect.create-intent')->middleware(HandlePrecognitiveRequests::class);
diff --git a/api/tests/Feature/Forms/FormStatTest.php b/api/tests/Feature/Forms/FormStatTest.php
index 0d1c586a1..abd9c4b88 100644
--- a/api/tests/Feature/Forms/FormStatTest.php
+++ b/api/tests/Feature/Forms/FormStatTest.php
@@ -82,8 +82,20 @@ it('checks form stats details', function () {
[] // Incomplete submission
]);
- // Create form views
- $form->views()->createMany(array_fill(0, 10, []));
+ // Create form views with metadata for testing aggregation
+ $form->views()->createMany([
+ ['meta' => ['source' => 'Google', 'device' => 'Desktop', 'country' => 'US', 'browser' => 'Chrome', 'os' => 'Windows']],
+ ['meta' => ['source' => 'Google', 'device' => 'Mobile', 'country' => 'US', 'browser' => 'Chrome', 'os' => 'Android']],
+ ['meta' => ['source' => 'Facebook', 'device' => 'Desktop', 'country' => 'UK', 'browser' => 'Firefox', 'os' => 'MacOS']],
+ ['meta' => ['source' => 'Direct', 'device' => 'Desktop', 'country' => 'CA', 'browser' => 'Safari', 'os' => 'MacOS']],
+ ['meta' => ['source' => 'Google', 'device' => 'Tablet', 'country' => 'US', 'browser' => 'Safari', 'os' => 'iOS']],
+ // Views with incomplete metadata (should default to 'Unknown')
+ ['meta' => ['source' => null]],
+ ['meta' => ['device' => null]],
+ ['meta' => []], // Empty metadata object
+ ['meta' => null], // Null metadata
+ [] // No metadata field at all
+ ]);
$this->getJson(route('open.workspaces.form.stats-details', [$workspace->id, $form->id]))
->assertSuccessful()
@@ -92,10 +104,33 @@ it('checks form stats details', function () {
->has('submissions')
->has('completion_rate')
->has('average_duration')
+ ->has('meta_stats')
->where('views', 10)
->where('submissions', 6)
->where('completion_rate', 60)
->where('average_duration', '1m 24s')
+ // Test metadata aggregation
+ ->where('meta_stats.source.Google', 3)
+ ->where('meta_stats.source.Facebook', 1)
+ ->where('meta_stats.source.Direct', 1)
+ ->where('meta_stats.source.Unknown', 5)
+ ->where('meta_stats.device.Desktop', 3)
+ ->where('meta_stats.device.Mobile', 1)
+ ->where('meta_stats.device.Tablet', 1)
+ ->where('meta_stats.device.Unknown', 5)
+ ->where('meta_stats.country.US', 3)
+ ->where('meta_stats.country.UK', 1)
+ ->where('meta_stats.country.CA', 1)
+ ->where('meta_stats.country.Unknown', 5)
+ ->where('meta_stats.browser.Chrome', 2)
+ ->where('meta_stats.browser.Firefox', 1)
+ ->where('meta_stats.browser.Safari', 2)
+ ->where('meta_stats.browser.Unknown', 5)
+ ->where('meta_stats.os.Windows', 1)
+ ->where('meta_stats.os.Android', 1)
+ ->where('meta_stats.os.MacOS', 2)
+ ->where('meta_stats.os.iOS', 1)
+ ->where('meta_stats.os.Unknown', 5)
->etc();
});
});
diff --git a/api/tests/Feature/Forms/FormViewsTest.php b/api/tests/Feature/Forms/FormViewsTest.php
index df4fedcee..c972d53f9 100644
--- a/api/tests/Feature/Forms/FormViewsTest.php
+++ b/api/tests/Feature/Forms/FormViewsTest.php
@@ -5,14 +5,8 @@ it('can see form without counting view for form owner', function () {
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
- $this->getJson(route('forms.show', $form->slug))
- ->assertSuccessful()
- ->assertJson(function (\Illuminate\Testing\Fluent\AssertableJson $json) use ($form) {
- return $json->where('id', $form->id)
- ->where('title', $form->title)
- ->whereType('properties', 'array')
- ->etc();
- });
+ $this->getJson(route('forms.view', $form->slug))
+ ->assertSuccessful();
expect($form->views()->count())->toBe(0);
});
@@ -24,14 +18,8 @@ it('can see form and count view for guest', function () {
$this->actingAsGuest();
- $this->getJson(route('forms.show', $form->slug))
- ->assertSuccessful()
- ->assertJson(function (\Illuminate\Testing\Fluent\AssertableJson $json) use ($form) {
- return $json->where('id', $form->id)
- ->where('title', $form->title)
- ->whereType('properties', 'array')
- ->etc();
- });
+ $this->getJson(route('forms.view', $form->slug))
+ ->assertSuccessful();
expect($form->views()->count())->toBe(1);
});
diff --git a/api/tests/Unit/Service/Forms/Analytics/BrowserDetectionTest.php b/api/tests/Unit/Service/Forms/Analytics/BrowserDetectionTest.php
new file mode 100644
index 000000000..2178843a3
--- /dev/null
+++ b/api/tests/Unit/Service/Forms/Analytics/BrowserDetectionTest.php
@@ -0,0 +1,352 @@
+<?php
+
+use App\Service\Forms\Analytics\BrowserDetection;
+
+afterEach(function () {
+ \Mockery::close();
+});
+
+it('detects Chrome browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Chrome');
+ expect($result['browser_version'])->toBe(91);
+ expect($result['browser_chrome_original'])->toBe(1);
+ expect($result['browser_chromium_version'])->toBe(91);
+});
+
+it('detects Firefox browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Firefox');
+ expect($result['browser_version'])->toBe(89);
+ expect($result['browser_firefox_original'])->toBe(1);
+ expect($result['browser_gecko_version'])->toBe(89);
+});
+
+it('detects Safari browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Safari');
+ expect($result['browser_version'])->toBe(14.1);
+ expect($result['browser_safari_original'])->toBe(1);
+ expect($result['browser_webkit_version'])->toBe(605.1);
+});
+
+it('detects Edge browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Edge');
+ expect($result['browser_version'])->toBe(91.0);
+});
+
+it('detects Windows OS correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
+
+ $result = $detector->getOS($userAgent);
+
+ expect($result['os_name'])->toBe('Windows');
+ expect($result['os_version'])->toBe('10');
+ expect($result['os_family'])->toBe('windows');
+ expect($result['os_type'])->toBe('desktop');
+ expect($result['64bits_mode'])->toBe(1);
+});
+
+it('detects macOS correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36';
+
+ $result = $detector->getOS($userAgent);
+
+ expect($result['os_name'])->toBe('MacOS');
+ expect($result['os_family'])->toBe('macintosh');
+ expect($result['os_type'])->toBe('desktop');
+ expect($result['64bits_mode'])->toBe(1);
+});
+
+it('detects Android OS correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Linux; Android 11; SM-G991B) AppleWebKit/537.36';
+
+ $result = $detector->getOS($userAgent);
+
+ expect($result['os_name'])->toBe('Android');
+ expect($result['os_version'])->toBe(11.0);
+ expect($result['os_family'])->toBe('android');
+ expect($result['os_type'])->toBe('mobile');
+});
+
+it('detects iOS correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15';
+
+ $result = $detector->getOS($userAgent);
+
+ expect($result['os_name'])->toBe('iOS');
+ expect($result['os_version'])->toBe(14.6);
+ expect($result['os_family'])->toBe('macintosh');
+ expect($result['os_type'])->toBe('mobile');
+ expect($result['64bits_mode'])->toBe(1);
+});
+
+it('detects Linux OS correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0';
+
+ $result = $detector->getOS($userAgent);
+
+ expect($result['os_name'])->toBe('Ubuntu');
+ expect($result['os_family'])->toBe('linux');
+ expect($result['os_type'])->toBe('desktop');
+ expect($result['64bits_mode'])->toBe(1);
+});
+
+it('detects desktop device type', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
+
+ $result = $detector->getDevice($userAgent);
+
+ expect($result['device_type'])->toBe('desktop');
+});
+
+it('detects mobile device type', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15';
+
+ $result = $detector->getDevice($userAgent);
+
+ expect($result['device_type'])->toBe('mobile');
+});
+
+it('detects tablet device type', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15';
+
+ $result = $detector->getDevice($userAgent);
+
+ expect($result['device_type'])->toBe('mobile'); // iPads are classified as mobile
+});
+
+it('detects TV device type', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (SmartTV; Linux; Tizen 6.0) AppleWebKit/537.36 TV Safari/537.36';
+
+ $result = $detector->getDevice($userAgent);
+
+ expect($result['device_type'])->toBe('tv');
+});
+
+it('detects console device type', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (PlayStation 4 3.11) AppleWebKit/537.73';
+
+ $result = $detector->getDevice($userAgent);
+
+ expect($result['device_type'])->toBe('console');
+});
+
+it('detects mobile Safari correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Safari Mobile');
+ expect($result['browser_version'])->toBe(14.1);
+ expect($result['browser_ios_webview'])->toBe(0);
+});
+
+it('detects Chrome Mobile correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Linux; Android 11; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Chrome');
+ expect($result['browser_version'])->toBe(91);
+ expect($result['browser_chrome_original'])->toBe(1);
+});
+
+it('detects Samsung Browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Linux; Android 11; SAMSUNG SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/14.2 Chrome/87.0.4280.141 Mobile Safari/537.36';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Samsung Browser');
+ expect($result['browser_version'])->toBe(14.2);
+});
+
+it('detects Opera browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 OPR/77.0.4054.277';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Opera');
+ expect($result['browser_version'])->toBe(77.0);
+});
+
+it('detects Android WebView correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Linux; Android 11; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.120 Mobile Safari/537.36';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_android_webview'])->toBe(1);
+});
+
+it('detects iOS WebView correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('WKWebView');
+ expect($result['browser_ios_webview'])->toBe(1);
+});
+
+it('returns all data with getAll method', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
+
+ $result = $detector->getAll($userAgent);
+
+ expect($result)->toHaveKeys([
+ 'os_type',
+ 'os_family',
+ 'os_name',
+ 'os_version',
+ 'os_title',
+ 'device_type',
+ 'browser_name',
+ 'browser_version',
+ 'browser_title',
+ 'browser_chrome_original',
+ 'browser_firefox_original',
+ 'browser_safari_original',
+ 'browser_chromium_version',
+ 'browser_gecko_version',
+ 'browser_webkit_version',
+ 'browser_android_webview',
+ 'browser_ios_webview',
+ 'browser_desktop_mode',
+ '64bits_mode'
+ ]);
+
+ expect($result['browser_name'])->toBe('Chrome');
+ expect($result['os_name'])->toBe('Windows');
+ expect($result['device_type'])->toBe('desktop');
+});
+
+it('returns JSON format when requested', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
+
+ $result = $detector->getAll($userAgent, 'json');
+
+ expect($result)->toBeString();
+
+ $decoded = json_decode($result, true);
+ expect($decoded)->toBeArray();
+ expect($decoded)->toHaveKey('browser_name');
+ expect($decoded)->toHaveKey('os_name');
+});
+
+it('handles unknown user agents gracefully', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Unknown/1.0';
+
+ $result = $detector->getAll($userAgent);
+
+ expect($result['browser_name'])->toBe('unknown');
+ expect($result['os_name'])->toBe('unknown');
+ expect($result['device_type'])->toBe('unknown');
+});
+
+it('handles empty user agent', function () {
+ $detector = new BrowserDetection();
+ $userAgent = '';
+
+ $result = $detector->getAll($userAgent);
+
+ expect($result['browser_name'])->toBe('unknown');
+ expect($result['os_name'])->toBe('unknown');
+ expect($result['device_type'])->toBe('unknown');
+});
+
+it('detects Facebook App correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 [FBAN/FBIOS;FBAV/325.0.0.44.273;]';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Facebook App');
+ expect($result['browser_version'])->toBe(325.0);
+});
+
+it('detects Instagram App correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 190.0.0.32.124';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Instagram App');
+ expect($result['browser_version'])->toBe(190.0);
+});
+
+it('detects touch support mode', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
+
+ // Set touch support and get result
+ $detector->setTouchSupport();
+ $result = $detector->getAll($userAgent);
+
+ // This should detect desktop mode on mobile when touch support is enabled
+ expect($result)->toHaveKey('browser_desktop_mode');
+});
+
+it('detects Yandex Browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 YaBrowser/21.6.0.616 Safari/537.36';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Yandex Browser');
+ expect($result['browser_version'])->toBe(21.6);
+});
+
+it('detects Internet Explorer correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('Internet Explorer');
+ expect($result['browser_version'])->toBe(11);
+});
+
+it('detects UC Browser correctly', function () {
+ $detector = new BrowserDetection();
+ $userAgent = 'Mozilla/5.0 (Linux; Android 11) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36 UCBrowser/13.4.0.1306';
+
+ $result = $detector->getBrowser($userAgent);
+
+ expect($result['browser_name'])->toBe('UC Browser');
+ expect($result['browser_version'])->toBe(13.4);
+});
diff --git a/api/tests/Unit/Service/Forms/Analytics/IpLocationServiceTest.php b/api/tests/Unit/Service/Forms/Analytics/IpLocationServiceTest.php
new file mode 100644
index 000000000..800b86e09
--- /dev/null
+++ b/api/tests/Unit/Service/Forms/Analytics/IpLocationServiceTest.php
@@ -0,0 +1,171 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Service\Forms\Analytics\IpLocationService;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Config;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+class IpLocationServiceTest extends TestCase
+{
+ private IpLocationService $service;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->service = new IpLocationService();
+ Cache::flush(); // Clear cache before each test
+ }
+
+ protected function tearDown(): void
+ {
+ Cache::flush(); // Clean up after each test
+ parent::tearDown();
+ }
+
+ public function test_returns_default_data_for_private_ip()
+ {
+ $privateIp = '192.168.1.1';
+ $result = $this->service->getLocationData($privateIp);
+
+ $this->assertEquals('Unknown', $result['country']);
+ $this->assertNull($result['region']);
+ $this->assertNull($result['city']);
+ }
+
+ public function test_returns_default_data_when_no_token_configured()
+ {
+ Config::set('services.ipinfo.token', null);
+
+ $publicIp = '8.8.8.8';
+ $result = $this->service->getLocationData($publicIp);
+
+ $this->assertEquals('Unknown', $result['country']);
+ }
+
+ public function test_caches_successful_api_response()
+ {
+ Config::set('services.ipinfo.token', 'test-token');
+ Config::set('services.ipinfo.cache_ttl_hours', 1);
+
+ $publicIp = '8.8.8.8';
+ $mockResponse = [
+ 'country' => 'US',
+ 'region' => 'California',
+ 'city' => 'Mountain View',
+ 'timezone' => 'America/Los_Angeles',
+ 'org' => 'AS15169 Google LLC',
+ ];
+
+ // Mock successful API response
+ Http::fake([
+ 'api.ipinfo.io/*' => Http::response($mockResponse, 200)
+ ]);
+
+ // First call should hit API
+ $result1 = $this->service->getLocationData($publicIp);
+
+ // Second call should hit cache (no API call)
+ Http::fake([]); // Clear HTTP fakes to ensure no API call
+ $result2 = $this->service->getLocationData($publicIp);
+
+ $this->assertEquals($result1, $result2);
+ $this->assertEquals('US', $result1['country']);
+ $this->assertEquals('California', $result1['region']);
+ }
+
+ public function test_handles_api_failure_gracefully()
+ {
+ Config::set('services.ipinfo.token', 'test-token');
+
+ $publicIp = '8.8.8.8';
+
+ // Mock failed API response
+ Http::fake([
+ 'api.ipinfo.io/*' => Http::response('Server Error', 500)
+ ]);
+
+ $result = $this->service->getLocationData($publicIp);
+
+ $this->assertEquals('Unknown', $result['country']);
+ $this->assertNull($result['region']);
+ }
+
+ public function test_cache_clear_functionality()
+ {
+ Config::set('services.ipinfo.token', 'test-token');
+
+ $publicIp = '8.8.8.8';
+ $callCount = 0;
+
+ // Mock API to return different responses on sequential calls
+ Http::fake([
+ 'api.ipinfo.io/*' => function () use (&$callCount) {
+ $callCount++;
+ return Http::response([
+ 'country' => $callCount === 1 ? 'US' : 'CA'
+ ], 200);
+ }
+ ]);
+
+ // First call should cache 'US'
+ $result1 = $this->service->getLocationData($publicIp);
+ $this->assertEquals('US', $result1['country']);
+
+ // Second call should return cached 'US' (no API call)
+ $result2 = $this->service->getLocationData($publicIp);
+ $this->assertEquals('US', $result2['country']);
+ $this->assertEquals(1, $callCount); // Still only 1 API call
+
+ // Clear the cache
+ $cleared = $this->service->clearCache($publicIp);
+ $this->assertTrue($cleared);
+
+ // Next call should hit API again and get 'CA'
+ $result3 = $this->service->getLocationData($publicIp);
+ $this->assertEquals('CA', $result3['country']);
+ $this->assertEquals(2, $callCount); // Now 2 API calls
+ }
+
+ public function test_cache_statistics()
+ {
+ Config::set('services.ipinfo.token', 'test-token');
+
+ $ips = ['8.8.8.8', '1.1.1.1', '208.67.222.222'];
+
+ // Cache one IP
+ Http::fake([
+ 'api.ipinfo.io/*' => Http::response(['country' => 'US'], 200)
+ ]);
+ $this->service->getLocationData($ips[0]);
+
+ // Get cache stats
+ $stats = $this->service->getCacheStats($ips);
+
+ $this->assertEquals(3, $stats['total']);
+ $this->assertEquals(1, $stats['cached']);
+ $this->assertEquals(2, $stats['missing']);
+ }
+
+ public function test_respects_custom_configuration()
+ {
+ Config::set('services.ipinfo.token', 'test-token');
+ Config::set('services.ipinfo.request_timeout', 10);
+ Config::set('services.ipinfo.cache_ttl_hours', 48);
+
+ $publicIp = '8.8.8.8';
+
+ Http::fake([
+ 'api.ipinfo.io/*' => function ($request) {
+ // Verify the request uses custom configuration
+ $this->assertStringContainsString('test-token', $request->url());
+ return Http::response(['country' => 'US'], 200);
+ }
+ ]);
+
+ $result = $this->service->getLocationData($publicIp);
+ $this->assertEquals('US', $result['country']);
+ }
+}
diff --git a/api/tests/Unit/Service/Forms/Analytics/UserAgentHelperTest.php b/api/tests/Unit/Service/Forms/Analytics/UserAgentHelperTest.php
new file mode 100644
index 000000000..cb9d9d66e
--- /dev/null
+++ b/api/tests/Unit/Service/Forms/Analytics/UserAgentHelperTest.php
@@ -0,0 +1,243 @@
+<?php
+
+use App\Service\Forms\Analytics\IpLocationService;
+use App\Service\Forms\Analytics\UserAgentHelper;
+use Illuminate\Http\Request;
+
+afterEach(function () {
+ \Mockery::close();
+});
+
+it('constructs with default IP location service when none provided', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+
+ $helper = new UserAgentHelper($mockRequest);
+
+ expect($helper)->toBeInstanceOf(UserAgentHelper::class);
+});
+
+it('constructs with provided IP location service', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+
+ expect($helper)->toBeInstanceOf(UserAgentHelper::class);
+});
+
+it('handles null user agent gracefully', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn(null);
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+
+ expect($helper)->toBeInstanceOf(UserAgentHelper::class);
+});
+
+it('returns Direct when no referer header', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn(null);
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Direct');
+});
+
+it('returns Direct when referer matches origin', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://example.com/page');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Direct');
+});
+
+it('detects Google as traffic source', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://www.google.com/search?q=test');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Google');
+});
+
+it('detects Facebook as traffic source', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://www.facebook.com/');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Facebook');
+});
+
+it('detects Twitter as traffic source', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://t.co/abc123');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Twitter');
+});
+
+it('detects LinkedIn as traffic source', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://www.linkedin.com/feed/');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Linkedin');
+});
+
+it('returns domain name for unknown traffic sources', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://some-unknown-site.com/page');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('some-unknown-site.com');
+});
+
+it('removes www prefix from domain names', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('https://www.unknown-site.com/page');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('unknown-site.com');
+});
+
+it('returns Unknown for invalid referer URLs', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn('invalid-url');
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn('https://example.com');
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $source = $helper->getTrafficSource();
+
+ expect($source)->toBe('Unknown');
+});
+
+it('delegates location requests to IP location service', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('ip')->andReturn('8.8.8.8');
+
+ $expectedLocation = [
+ 'country' => 'US',
+ 'region' => 'California',
+ 'city' => 'Mountain View'
+ ];
+
+ $mockIpLocationService->shouldReceive('getLocationData')
+ ->with('8.8.8.8')
+ ->andReturn($expectedLocation);
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $location = $helper->getLocation();
+
+ expect($location)->toBe($expectedLocation);
+});
+
+it('returns hashed IP in metadata for privacy', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('ip')->andReturn('8.8.8.8');
+ $mockRequest->shouldReceive('header')->with('Referer')->andReturn(null);
+ $mockRequest->shouldReceive('header')->with('Origin')->andReturn(null);
+
+ $mockIpLocationService->shouldReceive('getLocationData')
+ ->with('8.8.8.8')
+ ->andReturn(['country' => 'US']);
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+
+ // Mock the browser detection
+ $mockBrowser = \Mockery::mock(\App\Service\Forms\Analytics\BrowserDetection::class);
+ $mockBrowser->shouldReceive('getAll')->andReturn([
+ 'device_type' => 'Desktop',
+ 'browser_name' => 'Chrome',
+ 'os_name' => 'Windows'
+ ]);
+
+ // Use reflection to inject the mock browser
+ $reflection = new ReflectionClass($helper);
+ $browserProperty = $reflection->getProperty('browser');
+ $browserProperty->setAccessible(true);
+ $browserProperty->setValue($helper, $mockBrowser);
+
+ $metadata = $helper->getMetadata();
+
+ // The IP should be hashed, not the raw IP
+ expect($metadata['ip'])->not()->toBe('8.8.8.8');
+ expect($metadata['ip'])->toBeString();
+ expect(strlen($metadata['ip']))->toBe(64); // SHA256 hash length
+});
+
+it('handles empty location data', function () {
+ $mockRequest = \Mockery::mock(Request::class);
+ $mockIpLocationService = \Mockery::mock(IpLocationService::class);
+
+ $mockRequest->shouldReceive('userAgent')->andReturn('Mozilla/5.0');
+ $mockRequest->shouldReceive('ip')->andReturn('192.168.1.1');
+
+ $mockIpLocationService->shouldReceive('getLocationData')
+ ->with('192.168.1.1')
+ ->andReturn([]);
+
+ $helper = new UserAgentHelper($mockRequest, $mockIpLocationService);
+ $location = $helper->getLocation();
+
+ expect($location)->toBe([]);
+});
diff --git a/client/api/forms.js b/client/api/forms.js
index 63e9a7c57..6bfbb6e3d 100644
--- a/client/api/forms.js
+++ b/client/api/forms.js
@@ -1,6 +1,9 @@
import { apiService } from './base'
export const formsApi = {
+ // Form views
+ view: (slug, options) => apiService.get(`/forms/${slug}/view`, options),
+
// Form operations
list: (workspaceId, options) => apiService.get(`/open/workspaces/${workspaceId}/forms`, options),
get: (slug, options) => apiService.get(`/open/forms/${slug}`, options),
diff --git a/client/components/open/forms/components/FormTrafficBreakdown.vue b/client/components/open/forms/components/FormTrafficBreakdown.vue
new file mode 100644
index 000000000..0f7348a8b
--- /dev/null
+++ b/client/components/open/forms/components/FormTrafficBreakdown.vue
@@ -0,0 +1,236 @@
+<template>
+ <div class="w-full">
+ <h3 class="font-medium text-lg mb-4">Traffic Breakdown</h3>
+
+ <div v-if="!form.is_pro" class="border border-neutral-300 rounded-lg shadow-xs p-4 relative overflow-hidden">
+ <div class="absolute inset-0 z-10">
+ <div class="p-5 max-w-md mx-auto flex flex-col items-center justify-center h-full">
+ <p class="text-center">
+ You need a <pro-tag
+ upgrade-modal-title="Upgrade today to access detailed analytics"
+ class="mx-1"
+ /> subscription to access detailed traffic breakdown.
+ </p>
+ <UButton
+ class="mt-5 flex justify-center"
+ @click.prevent="openSubscriptionModal({modal_title: 'Upgrade to unlock detailed Analytics'})"
+ label="Subscribe"
+ />
+ </div>
+ </div>
+ <img
+ src="/img/pages/forms/blurred_graph.png"
+ alt="Sample Graph"
+ class="mx-auto w-full filter blur-md z-0 pointer-events-none"
+ >
+ </div>
+
+ <VTransition v-else name="fade">
+ <div v-if="isLoading" class="grid grid-cols-1 md:grid-cols-2 gap-4">
+ <div v-for="i in 6" :key="i" class="border border-neutral-300 rounded-lg shadow-xs p-4 space-y-4">
+ <USkeleton class="h-6 w-24" />
+ <div class="space-y-4">
+ <USkeleton class="h-4 w-full rounded-full" />
+ <div class="flex flex-wrap gap-2 justify-center">
+ <USkeleton v-for="j in 3" :key="j" class="h-4 w-16 rounded" />
+ </div>
+ </div>
+ </div>
+ </div>
+ <div v-else class="grid grid-cols-1 md:grid-cols-2 gap-4">
+ <div
+ v-for="chart in chartTypes"
+ :key="chart.id"
+ class="border border-neutral-300 rounded-lg shadow-xs p-4"
+ >
+ <h4 class="font-medium mb-3">{{ chart.title }}</h4>
+
+ <div v-if="Object.keys(metaStats[chart.id] || {}).length === 0" class="text-sm text-gray-500 text-center py-2">
+ No data available
+ </div>
+
+ <div v-else class="space-y-4">
+ <div class="w-full h-4 flex rounded-full overflow-hidden">
+ <div
+ v-for="(item, index) in getDisplayChartData(chart.id)"
+ :key="index"
+ :style="{
+ width: item.percentage + '%',
+ backgroundColor: item.color
+ }"
+ class="h-full"
+ :title="`${item.displayName}: ${item.count} (${item.percentage}%)`"
+ ></div>
+ </div>
+
+ <div class="flex flex-wrap gap-3 items-center justify-center mt-2">
+ <template v-for="(item, index) in getDisplayChartData(chart.id)" :key="index">
+ <div v-if="item.type !== 'others'" class="flex items-center gap-1">
+ <div
+ class="w-3 h-3 rounded-sm"
+ :style="{ backgroundColor: item.color }"
+ ></div>
+ <span class="text-xs">{{ item.displayName }} ({{ item.count }})</span>
+ </div>
+
+ <!-- "Other" group with popover -->
+ <UPopover v-else arrow>
+ <div class="flex items-center gap-1 cursor-pointer">
+ <div class="w-3 h-3 rounded-sm" :style="{ backgroundColor: item.color }"></div>
+ <span class="text-xs">{{ item.displayName }} ({{ item.count }})</span>
+ </div>
+
+ <template #content>
+ <div class="w-72 p-3 max-h-40 overflow-y-auto bg-white shadow-lg rounded-md">
+ <div class="space-y-2">
+ <div
+ v-for="otherItem in getOtherItems(chart.id)"
+ :key="otherItem.type"
+ class="flex items-center justify-between"
+ >
+ <span class="text-sm">{{ otherItem.displayName }}</span>
+ <span class="text-sm font-medium">{{ otherItem.count }}</span>
+ </div>
+ </div>
+ </div>
+ </template>
+ </UPopover>
+ </template>
+ </div>
+ </div>
+ </div>
+ </div>
+ </VTransition>
+ </div>
+</template>
+
+<script setup>
+import ProTag from "~/components/app/ProTag.vue"
+
+const props = defineProps({
+ form: {
+ type: Object,
+ required: true
+ },
+ metaData: {
+ type: Object,
+ required: true
+ },
+ isLoading: {
+ type: Boolean,
+ default: false
+ }
+})
+
+const { openSubscriptionModal } = useAppModals()
+
+// Chart types configuration
+const chartTypes = [
+ {
+ id: 'source',
+ title: 'Traffic Sources'
+ },
+ {
+ id: 'device',
+ title: 'Devices'
+ },
+ {
+ id: 'browser',
+ title: 'Browsers'
+ },
+ {
+ id: 'os',
+ title: 'Operating Systems'
+ },
+ {
+ id: 'country',
+ title: 'Countries'
+ }
+]
+
+// Consistent colors for all chart types
+const CHART_COLORS = [
+ '#2563eb', // blue-600 (1st item)
+ '#16a34a', // green-600 (2nd item)
+ '#f59e0b', // amber-500 (3rd item)
+ '#ec4899', // pink-500 (4th item)
+ '#8b5cf6', // purple-500 (5th item)
+ '#9ca3af', // gray-400 (Other)
+]
+
+// Maximum number of items to show in legend before grouping as "Other"
+const MAX_LEGEND_ITEMS = 5
+
+// Meta stats data
+const metaStats = computed(() => {
+ return {
+ source: props.metaData?.source || {},
+ device: props.metaData?.device || {},
+ country: props.metaData?.country || {},
+ browser: props.metaData?.browser || {},
+ os: props.metaData?.os || {}
+ }
+})
+
+// Generate chart data for a specific chart type
+const getChartData = (chartId) => {
+ const chart = chartTypes.find(c => c.id === chartId)
+ if (!chart) return []
+
+ const data = metaStats.value[chart.id]
+ if (!data || Object.keys(data).length === 0) return []
+
+ // Calculate total
+ const total = Object.values(data).reduce((sum, count) => sum + count, 0)
+
+ // Format and sort data
+ return Object.entries(data).map(([type, count]) => {
+ const displayName = type.charAt(0).toUpperCase() + type.slice(1)
+ return {
+ type,
+ displayName,
+ count,
+ percentage: Math.round((count / total) * 100)
+ }
+ }).sort((a, b) => b.count - a.count) // Sort by count, highest first
+}
+
+// Get all items not shown in the main legend - this is still needed for the popover content
+const getOtherItems = (chartId) => {
+ const data = getChartData(chartId)
+ if (data.length <= MAX_LEGEND_ITEMS) return []
+
+ return data.slice(MAX_LEGEND_ITEMS)
+}
+
+// Get display-ready chart data with Others included
+const getDisplayChartData = (chartId) => {
+ const allData = getChartData(chartId)
+ if (allData.length === 0) return []
+
+ // Get the top N items
+ const topItems = allData.slice(0, MAX_LEGEND_ITEMS).map((item, index) => ({
+ ...item,
+ color: CHART_COLORS[index] // Assign color based on position
+ }))
+
+ // If there are more items, add an "Others" category
+ if (allData.length > MAX_LEGEND_ITEMS) {
+ // Calculate total count and percentage for "Others"
+ const othersCount = allData.slice(MAX_LEGEND_ITEMS).reduce((sum, item) => sum + item.count, 0)
+ const totalCount = allData.reduce((sum, item) => sum + item.count, 0)
+ const othersPercentage = Math.round((othersCount / totalCount) * 100)
+
+ // Add Others to the result
+ topItems.push({
+ type: 'others',
+ displayName: 'Others',
+ count: othersCount,
+ percentage: othersPercentage,
+ color: CHART_COLORS[CHART_COLORS.length - 1] // Last color is for Others
+ })
+ }
+
+ return topItems
+}
+</script>
\ No newline at end of file
diff --git a/client/pages/forms/[slug]/index.vue b/client/pages/forms/[slug]/index.vue
index 7b79c4aa7..a8a0d9dc1 100644
--- a/client/pages/forms/[slug]/index.vue
+++ b/client/pages/forms/[slug]/index.vue
@@ -68,6 +68,7 @@ import {
useDarkMode
} from '~/lib/forms/public-page'
import { FormMode } from "~/lib/forms/FormModeStrategy.js"
+import { formsApi } from '~/api'
const crisp = useCrisp()
const appStore = useAppStore()
@@ -100,6 +101,8 @@ const passwordEntered = function (password) {
refetchForm().then(() => {
if (form.value?.is_password_protected) {
openCompleteForm.value.addPasswordError(t('forms.invalid_password'))
+ } else {
+ trackFormView()
}
})
})
@@ -163,9 +166,22 @@ onMounted(() => {
}
if (!isIframe && form.value?.auto_focus) focusOnFirstFormElement()
}
+
+ trackFormView()
}
})
+ // Track form view
+let hasViewedForm = false
+const trackFormView = () => {
+ if (import.meta.client && !form.value?.is_password_protected && !hasViewedForm) {
+ hasViewedForm = true
+ nextTick(() => {
+ formsApi.view(form.value.slug)
+ })
+ }
+}
+
onBeforeRouteLeave(() => {
appStore.showFeatureBaseButton()
document.body.classList.remove('public-page')
diff --git a/client/pages/forms/[slug]/show/stats.vue b/client/pages/forms/[slug]/show/stats.vue
index eee87fc8e..8ba359bd0 100644
--- a/client/pages/forms/[slug]/show/stats.vue
+++ b/client/pages/forms/[slug]/show/stats.vue
@@ -36,12 +36,23 @@
</div>
</div>
- <FormStats class="w-full max-w-4xl mx-auto" :form="form" />
+ <FormStats
+ class="w-full max-w-4xl mx-auto"
+ :form="form"
+ />
+
+ <FormTrafficBreakdown
+ class="w-full max-w-4xl mx-auto mt-8"
+ :form="form"
+ :meta-data="statsData?.meta_stats ?? {}"
+ :is-loading="isLoading"
+ />
</div>
</template>
<script setup>
import FormStats from "~/components/open/forms/components/FormStats.vue"
+import FormTrafficBreakdown from "~/components/open/forms/components/FormTrafficBreakdown.vue"
const props = defineProps({
form: { type: Object, required: true },