From d0b0803a4ae56ce2993f6f663072b8ad9d054578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= <23310825+marc-farre@users.noreply.github.com> Date: Mon, 30 Dec 2024 09:01:12 +0000 Subject: [PATCH] Fix DeviceDetectorHelper when no user agent (#7365) * Enh: Fix DeviceDetectorHelper::isMobile() and DeviceDetectorHelper::isTablet() when no user agent * Add PR ID to CHANGELOG --- CHANGELOG.md | 3 +++ protected/humhub/config/common.php | 2 +- .../humhub/helpers/DeviceDetectorHelper.php | 22 ++++++++++++------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a399f72d2..812d201b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ HumHub Changelog ================ +1.17.0-beta.5 (Unreleased) +--------------------------------- +- Fix #7365: `DeviceDetectorHelper::isMobile()` and `DeviceDetectorHelper::isTablet()` when no user agent 1.17.0-beta.4 (December 24, 2024) --------------------------------- diff --git a/protected/humhub/config/common.php b/protected/humhub/config/common.php index 31ff0f7120..88c09da945 100644 --- a/protected/humhub/config/common.php +++ b/protected/humhub/config/common.php @@ -42,7 +42,7 @@ $logTargetConfig = [ $config = [ 'name' => 'HumHub', - 'version' => '1.17.0-beta.4', + 'version' => '1.17.0-beta.5', 'minRecommendedPhpVersion' => '8.1', 'minSupportedPhpVersion' => '8.1', 'basePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR, diff --git a/protected/humhub/helpers/DeviceDetectorHelper.php b/protected/humhub/helpers/DeviceDetectorHelper.php index 6a16c59735..f0e7c78d4c 100644 --- a/protected/humhub/helpers/DeviceDetectorHelper.php +++ b/protected/humhub/helpers/DeviceDetectorHelper.php @@ -20,11 +20,8 @@ class DeviceDetectorHelper { public static function isMobile(): bool { - $detect = new MobileDetect(); - $detect->setUserAgent(Yii::$app->request->getUserAgent()); - try { - return $detect->isMobile(); + return (bool)static::getMobileDetect()?->isMobile(); } catch (MobileDetectException $e) { Yii::error('DeviceDetectorHelper::isMobile() error: ' . $e->getMessage()); return false; @@ -33,17 +30,26 @@ class DeviceDetectorHelper public static function isTablet(): bool { - $detect = new MobileDetect(); - $detect->setUserAgent(Yii::$app->request->getUserAgent()); - try { - return $detect->isTablet(); + return (bool)static::getMobileDetect()?->isTablet(); } catch (MobileDetectException $e) { Yii::error('DeviceDetectorHelper::isTablet() error: ' . $e->getMessage()); return false; } } + private static function getMobileDetect(): ?MobileDetect + { + $userAgent = Yii::$app->request->getUserAgent(); + if (!$userAgent) { + return null; + } + + $detect = new MobileDetect(); + $detect->setUserAgent($userAgent); + return $detect; + } + public static function isAppRequest(): bool { return