Fix DeviceDetectorHelper when no user agent (#7365)

* Enh: Fix DeviceDetectorHelper::isMobile() and DeviceDetectorHelper::isTablet() when no user agent

* Add PR ID to CHANGELOG
This commit is contained in:
Marc Farré 2024-12-30 09:01:12 +00:00 committed by GitHub
parent 6f1c3cf1e6
commit d0b0803a4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 9 deletions

View File

@ -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)
---------------------------------

View File

@ -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,

View File

@ -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