2015-04-16 16:39:44 -04:00
< ? php
2024-05-27 11:39:07 -04:00
2015-04-16 16:39:44 -04:00
/**
2024-05-27 11:39:07 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2015-04-16 16:39:44 -04:00
*/
2015-08-30 13:13:01 -04:00
namespace OCA\DAV\Connector\Sabre ;
2015-04-16 16:39:44 -04:00
2024-09-06 08:39:32 -04:00
use OCA\Theming\ThemingDefaults ;
2015-04-16 16:39:44 -04:00
use OCP\IConfig ;
2023-06-23 03:50:46 -04:00
use OCP\IRequest ;
2022-04-12 08:01:13 -04:00
use Sabre\DAV\Server ;
2015-04-16 16:39:44 -04:00
use Sabre\DAV\ServerPlugin ;
2019-11-22 14:52:10 -05:00
use Sabre\HTTP\RequestInterface ;
2015-04-16 16:39:44 -04:00
/**
* Class BlockLegacyClientPlugin is used to detect old legacy sync clients and
2015-04-20 06:53:40 -04:00
* returns a 403 status to those clients
2015-04-16 16:39:44 -04:00
*
2015-08-30 13:13:01 -04:00
* @ package OCA\DAV\Connector\Sabre
2015-04-16 16:39:44 -04:00
*/
class BlockLegacyClientPlugin extends ServerPlugin {
2022-05-05 18:01:08 -04:00
protected ? Server $server = null ;
2015-04-16 16:39:44 -04:00
2024-09-06 08:39:32 -04:00
public function __construct (
private IConfig $config ,
private ThemingDefaults $themingDefaults ,
) {
2015-04-16 16:39:44 -04:00
}
/**
* @ return void
*/
2022-04-12 08:01:13 -04:00
public function initialize ( Server $server ) {
2015-04-16 16:39:44 -04:00
$this -> server = $server ;
2020-03-09 11:32:04 -04:00
$this -> server -> on ( 'beforeMethod:*' , [ $this , 'beforeHandler' ], 200 );
2015-04-16 16:39:44 -04:00
}
/**
2015-04-20 06:53:40 -04:00
* Detects all unsupported clients and throws a \Sabre\DAV\Exception\Forbidden
* exception which will result in a 403 to them .
2015-04-16 16:39:44 -04:00
* @ param RequestInterface $request
2015-04-20 06:53:40 -04:00
* @ throws \Sabre\DAV\Exception\Forbidden If the client version is not supported
2015-04-16 16:39:44 -04:00
*/
public function beforeHandler ( RequestInterface $request ) {
$userAgent = $request -> getHeader ( 'User-Agent' );
2015-04-23 10:33:51 -04:00
if ( $userAgent === null ) {
return ;
}
2025-08-10 20:34:04 -04:00
$minimumSupportedDesktopVersion = $this -> config -> getSystemValueString ( 'minimum.supported.desktop.version' , '3.1.0' );
2024-11-27 04:06:14 -05:00
$maximumSupportedDesktopVersion = $this -> config -> getSystemValueString ( 'maximum.supported.desktop.version' , '99.99.99' );
// Check if the client is a desktop client
2023-06-23 03:50:46 -04:00
preg_match ( IRequest :: USER_AGENT_CLIENT_DESKTOP , $userAgent , $versionMatches );
2024-11-27 04:06:14 -05:00
// If the client is a desktop client and the version is too old, block it
if ( isset ( $versionMatches [ 1 ]) && version_compare ( $versionMatches [ 1 ], $minimumSupportedDesktopVersion ) === - 1 ) {
2024-09-06 08:39:32 -04:00
$customClientDesktopLink = htmlspecialchars ( $this -> themingDefaults -> getSyncClientUrl ());
$minimumSupportedDesktopVersion = htmlspecialchars ( $minimumSupportedDesktopVersion );
throw new \Sabre\DAV\Exception\Forbidden ( " This version of the client is unsupported. Upgrade to <a href= \" $customClientDesktopLink\ " > version $minimumSupportedDesktopVersion or later </ a >. " );
2015-04-16 16:39:44 -04:00
}
2024-11-27 04:06:14 -05:00
// If the client is a desktop client and the version is too new, block it
if ( isset ( $versionMatches [ 1 ]) && version_compare ( $versionMatches [ 1 ], $maximumSupportedDesktopVersion ) === 1 ) {
$customClientDesktopLink = htmlspecialchars ( $this -> themingDefaults -> getSyncClientUrl ());
$maximumSupportedDesktopVersion = htmlspecialchars ( $maximumSupportedDesktopVersion );
throw new \Sabre\DAV\Exception\Forbidden ( " This version of the client is unsupported. Downgrade to <a href= \" $customClientDesktopLink\ " > version $maximumSupportedDesktopVersion or earlier </ a >. " );
}
2015-04-16 16:39:44 -04:00
}
}