diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index effc634..63c336e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -40,7 +40,6 @@ class Application extends App implements IBootstrap { public const APP_ID = 'integration_openstreetmap'; public const OSM_URL = 'https://www.openstreetmap.org'; - public const DEFAULT_MAPTILER_API_KEY = 'get_your_own_OpIi9ZULNHzrESv6T2vL'; public const DEFAULT_SEARCH_LOCATION_ENABLED_VALUE = '0'; public const DEFAULT_PROXY_OSM_VALUE = '1'; diff --git a/lib/Controller/OsmAPIController.php b/lib/Controller/OsmAPIController.php index 1d63dcf..007c42f 100644 --- a/lib/Controller/OsmAPIController.php +++ b/lib/Controller/OsmAPIController.php @@ -62,7 +62,7 @@ public function nominatimSearch( 'namedetails' => $namedetails, 'extratags' => $extratags, ]; - $searchResults = $this->osmAPIService->searchLocation($this->userId, $q, $rformat, $extraParams, 0, $limit); + $searchResults = $this->osmAPIService->searchLocation($q, $rformat, $extraParams, 0, $limit); if (isset($searchResults['error'])) { return new DataResponse('', Http::STATUS_BAD_REQUEST); } diff --git a/lib/Listener/CSPListener.php b/lib/Listener/CSPListener.php index f468a26..bb8a0c3 100644 --- a/lib/Listener/CSPListener.php +++ b/lib/Listener/CSPListener.php @@ -39,6 +39,7 @@ class CSPListener implements IEventListener { public function __construct( private IRequest $request, private IAppConfig $appConfig, + private ?string $userId, ) { } @@ -56,7 +57,10 @@ public function handle(Event $event): void { ->addAllowedFrameDomain('https://www.openstreetmap.org') ->addAllowedImageDomain('https://*.tile.openstreetmap.org'); - $proxyOsm = $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1'; + // we do not proxy on public pages + $proxyOsm = $this->userId === null + ? false + : $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1'; if (!$proxyOsm) { $policy ->addAllowedConnectDomain('https://*.openstreetmap.org') diff --git a/lib/Listener/OsmReferenceListener.php b/lib/Listener/OsmReferenceListener.php index d4c0a14..1fd1519 100644 --- a/lib/Listener/OsmReferenceListener.php +++ b/lib/Listener/OsmReferenceListener.php @@ -50,12 +50,25 @@ public function handle(Event $event): void { return; } - $maptilerApiKey = $this->appConfig->getValueString(Application::APP_ID, 'maptiler_api_key', Application::DEFAULT_MAPTILER_API_KEY) ?: Application::DEFAULT_MAPTILER_API_KEY; + $maptilerApiKey = $this->appConfig->getValueString(Application::APP_ID, 'maptiler_api_key'); $userConfig = [ 'maptiler_api_key' => $maptilerApiKey, ]; $this->initialState->provideInitialState('api-keys', $userConfig); + // public pages + if ($this->userId === null) { + $this->initialState->provideInitialState('prefer-osm-frame', false); + $this->initialState->provideInitialState('proxy-map-requests', false); + if ($maptilerApiKey === '') { + $this->initialState->provideInitialState('last-map-state', [ + 'mapStyle' => 'osmRaster', + ]); + } + Util::addScript(Application::APP_ID, Application::APP_ID . '-referenceLocation'); + return; + } + $preferSimpleOsmIframe = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'prefer_simple_osm_iframe', '0') === '1'; $this->initialState->provideInitialState('prefer-osm-frame', $preferSimpleOsmIframe); $proxyMapRequests = $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1'; diff --git a/lib/Reference/BingReferenceProvider.php b/lib/Reference/BingReferenceProvider.php index 81f6bfd..7c446fc 100644 --- a/lib/Reference/BingReferenceProvider.php +++ b/lib/Reference/BingReferenceProvider.php @@ -24,6 +24,7 @@ namespace OCA\Osm\Reference; use OCA\Osm\AppInfo\Application; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -32,7 +33,7 @@ use OCP\Config\IUserConfig; use OCP\IAppConfig; -class BingReferenceProvider implements IReferenceProvider { +class BingReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -50,7 +51,9 @@ public function __construct( */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -58,6 +61,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -152,6 +159,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/DuckduckgoReferenceProvider.php b/lib/Reference/DuckduckgoReferenceProvider.php index 4466813..b2d635e 100644 --- a/lib/Reference/DuckduckgoReferenceProvider.php +++ b/lib/Reference/DuckduckgoReferenceProvider.php @@ -24,6 +24,7 @@ namespace OCA\Osm\Reference; use OCA\Osm\AppInfo\Application; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -32,7 +33,7 @@ use OCP\Config\IUserConfig; use OCP\IAppConfig; -class DuckduckgoReferenceProvider implements IReferenceProvider { +class DuckduckgoReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -50,7 +51,9 @@ public function __construct( */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -58,6 +61,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -119,6 +126,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/GoogleMapsReferenceProvider.php b/lib/Reference/GoogleMapsReferenceProvider.php index 063552f..9cefd99 100644 --- a/lib/Reference/GoogleMapsReferenceProvider.php +++ b/lib/Reference/GoogleMapsReferenceProvider.php @@ -26,6 +26,7 @@ use OCA\Osm\AppInfo\Application; use OCA\Osm\Service\OsmAPIService; use OCA\Osm\Service\UtilsService; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -36,7 +37,7 @@ use OCP\IURLGenerator; -class GoogleMapsReferenceProvider implements IReferenceProvider { +class GoogleMapsReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -57,7 +58,9 @@ public function __construct( */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -65,6 +68,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -72,7 +79,7 @@ public function resolveReference(string $referenceText): ?IReference { if ($this->matchReference($referenceText)) { $coords = $this->getCoordinates($referenceText); if ($coords !== null) { - $pointInfo = $this->osmAPIService->geocode($this->userId, $coords['lat'], $coords['lon'], false); + $pointInfo = $this->osmAPIService->geocode($coords['lat'], $coords['lon'], false); if (!isset($pointInfo['error'])) { $pointInfo['url'] = $referenceText; $reference = new Reference($referenceText); @@ -248,6 +255,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/HereMapsReferenceProvider.php b/lib/Reference/HereMapsReferenceProvider.php index dba382c..b2ac515 100644 --- a/lib/Reference/HereMapsReferenceProvider.php +++ b/lib/Reference/HereMapsReferenceProvider.php @@ -25,6 +25,7 @@ use OCA\Osm\AppInfo\Application; use OCA\Osm\Service\OsmAPIService; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -35,7 +36,7 @@ use OCP\IURLGenerator; -class HereMapsReferenceProvider implements IReferenceProvider { +class HereMapsReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -55,7 +56,9 @@ public function __construct( */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -63,6 +66,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -70,7 +77,7 @@ public function resolveReference(string $referenceText): ?IReference { if ($this->matchReference($referenceText)) { $coords = $this->getCoordinates($referenceText); if ($coords !== null) { - $pointInfo = $this->osmAPIService->geocode($this->userId, $coords['lat'], $coords['lon'], false); + $pointInfo = $this->osmAPIService->geocode($coords['lat'], $coords['lon'], false); if (!isset($pointInfo['error'])) { $pointInfo['url'] = $referenceText; $reference = new Reference($referenceText); @@ -173,6 +180,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/OsmLocationReferenceProvider.php b/lib/Reference/OsmLocationReferenceProvider.php index 56e5a2c..8024ebc 100644 --- a/lib/Reference/OsmLocationReferenceProvider.php +++ b/lib/Reference/OsmLocationReferenceProvider.php @@ -26,6 +26,7 @@ use OCA\Osm\AppInfo\Application; use OCA\Osm\Service\OsmAPIService; use OCA\Osm\Service\UtilsService; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -36,7 +37,7 @@ use OCP\IURLGenerator; -class OsmLocationReferenceProvider implements IReferenceProvider { +class OsmLocationReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -57,7 +58,9 @@ public function __construct( */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -65,6 +68,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null || $this->getLocationTypeId($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -72,7 +79,7 @@ public function resolveReference(string $referenceText): ?IReference { if ($this->matchReference($referenceText)) { $coords = $this->getCoordinates($referenceText); $locationTypeId = $this->getLocationTypeId($referenceText); - $locationInfo = $this->osmAPIService->getLocationInfo($this->userId, $locationTypeId['id'], $locationTypeId['type']); + $locationInfo = $this->osmAPIService->getLocationInfo($locationTypeId['id'], $locationTypeId['type']); if ($locationInfo !== null) { $locationInfo['url'] = $referenceText; $reference = new Reference($referenceText); @@ -185,6 +192,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/OsmPointReferenceProvider.php b/lib/Reference/OsmPointReferenceProvider.php index 2721f93..4ec23e5 100644 --- a/lib/Reference/OsmPointReferenceProvider.php +++ b/lib/Reference/OsmPointReferenceProvider.php @@ -27,6 +27,7 @@ use OCA\Osm\Service\OsmAPIService; use OCA\Osm\Service\UtilsService; use OCP\Collaboration\Reference\ADiscoverableReferenceProvider; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\ISearchableReferenceProvider; @@ -38,7 +39,7 @@ use OCP\IURLGenerator; -class OsmPointReferenceProvider extends ADiscoverableReferenceProvider implements ISearchableReferenceProvider { +class OsmPointReferenceProvider extends ADiscoverableReferenceProvider implements ISearchableReferenceProvider, IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_location'; @@ -97,7 +98,9 @@ public function getSupportedSearchProviderIds(): array { */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -105,6 +108,10 @@ public function matchReference(string $referenceText): bool { return $this->getCoordinates($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -112,10 +119,9 @@ public function resolveReference(string $referenceText): ?IReference { if ($this->matchReference($referenceText)) { $coords = $this->getCoordinates($referenceText); if (isset($coords['markerLat'], $coords['markerLon'])) { - $pointInfo = $this->osmAPIService->geocode($this->userId, $coords['markerLat'], $coords['markerLon']); + $pointInfo = $this->osmAPIService->geocode($coords['markerLat'], $coords['markerLon']); } else { // do not geocode if no marker, the widget will simply show the map centered correctly - // $pointInfo = $this->osmAPIService->geocode($this->userId, $coords['lat'], $coords['lon']); $pointInfo = []; } if (!isset($pointInfo['error'])) { @@ -283,8 +289,6 @@ public static function getFragmentInfo(string $url, array $urlInfo): array { } /** - * We use the userId here because when connecting/disconnecting from the GitHub account, - * we want to invalidate all the user cache and this is only possible with the cache prefix * @inheritDoc */ public function getCachePrefix(string $referenceId): string { @@ -299,6 +303,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Reference/OsmRouteReferenceProvider.php b/lib/Reference/OsmRouteReferenceProvider.php index 4c2cc79..26a59e9 100644 --- a/lib/Reference/OsmRouteReferenceProvider.php +++ b/lib/Reference/OsmRouteReferenceProvider.php @@ -26,6 +26,7 @@ use OCA\Osm\AppInfo\Application; use OCA\Osm\Service\RoutingService; use OCP\Collaboration\Reference\ADiscoverableReferenceProvider; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\LinkReferenceProvider; @@ -36,7 +37,7 @@ use OCP\IL10N; use OCP\IURLGenerator; -class OsmRouteReferenceProvider extends ADiscoverableReferenceProvider { +class OsmRouteReferenceProvider extends ADiscoverableReferenceProvider implements IPublicReferenceProvider { private const RICH_OBJECT_TYPE = Application::APP_ID . '_route'; @@ -87,7 +88,9 @@ public function getIconUrl(): string { */ public function matchReference(string $referenceText): bool { $adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1'; - $userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; + $userLinkPreviewEnabled = $this->userId === null + ? true + : $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1'; if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) { return false; } @@ -95,6 +98,10 @@ public function matchReference(string $referenceText): bool { return $this->getLinkInfo($referenceText) !== null; } + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + /** * @inheritDoc */ @@ -321,6 +328,13 @@ public function getCacheKey(string $referenceId): ?string { return $referenceId; } + /** + * @inheritDoc + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } + /** * @param string $userId * @return void diff --git a/lib/Search/OsmSearchLocationProvider.php b/lib/Search/OsmSearchLocationProvider.php index 1ce1f46..1b93b4f 100644 --- a/lib/Search/OsmSearchLocationProvider.php +++ b/lib/Search/OsmSearchLocationProvider.php @@ -105,7 +105,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult { 'extratags' => 1, 'namedetails' => 1, ]; - $searchResult = $this->osmAPIService->searchLocation($user->getUID(), $term, 'json', $extraParams, $offset, $limit); + $searchResult = $this->osmAPIService->searchLocation($term, 'json', $extraParams, $offset, $limit); if (isset($searchResult['error'])) { $items = []; } else { diff --git a/lib/Service/OsmAPIService.php b/lib/Service/OsmAPIService.php index 951139f..d6f9a78 100644 --- a/lib/Service/OsmAPIService.php +++ b/lib/Service/OsmAPIService.php @@ -113,12 +113,11 @@ public function getLinkFromOsmId(int $osmId, string $osmType): string { * Example location ID: 87515 * Example location type: relation * - * @param string $userId * @param int $locationId * @param string $locationType * @return array|null */ - public function getLocationInfo(string $userId, int $locationId, string $locationType): ?array { + public function getLocationInfo(int $locationId, string $locationType): ?array { // example: // curl https://nominatim.openstreetmap.org/lookup?osm_ids=R87515&format=json&addressdetails=1&extratags=1&namedetails=1&polygon_geojson=1 $prefix = $locationType === 'relation' @@ -134,7 +133,7 @@ public function getLocationInfo(string $userId, int $locationId, string $locatio 'namedetails' => 1, 'polygon_geojson' => 1, ]; - $result = $this->request($userId, 'lookup', $params); + $result = $this->request('lookup', $params); if (count($result) === 1) { return $result[0]; } @@ -146,7 +145,6 @@ public function getLocationInfo(string $userId, int $locationId, string $locatio * Search items * Example query string: "montcuq" * - * @param string $userId * @param string $query * @param string $format * @param array $extraParams @@ -154,7 +152,7 @@ public function getLocationInfo(string $userId, int $locationId, string $locatio * @param int $limit * @return array request result */ - public function searchLocation(string $userId, string $query, string $format, array $extraParams = [], + public function searchLocation(string $query, string $format, array $extraParams = [], int $offset = 0, int $limit = 5): array { // no pagination... $limitParam = $offset + $limit; @@ -168,7 +166,7 @@ public function searchLocation(string $userId, string $query, string $format, ar $params[$k] = $v; } } - $result = $this->request($userId, 'search', $params); + $result = $this->request('search', $params); if (!isset($result['error'])) { return array_slice($result, $offset, $limit); } @@ -181,13 +179,12 @@ public function searchLocation(string $userId, string $query, string $format, ar * lat: 44.3383486 * lon: 1.2086886 * - * @param string $userId * @param float $lat * @param float $lon * @param bool $includePolygon * @return array */ - public function geocode(string $userId, float $lat, float $lon, bool $includePolygon = true): array { + public function geocode(float $lat, float $lon, bool $includePolygon = true): array { // example: // curl https://nominatim.openstreetmap.org/reverse?format=json&lat=44.3383486&lon=1.2086886&addressdetails=1&polygon_geojson=1 $params = [ @@ -199,20 +196,19 @@ public function geocode(string $userId, float $lat, float $lon, bool $includePol if ($includePolygon) { $params['polygon_geojson'] = 1; } - return $this->request($userId, 'reverse', $params); + return $this->request('reverse', $params); } /** * Make an HTTP request to the Osm API * - * @param string|null $userId * @param string $endPoint The path to reach in nominatim * @param array $params Query parameters (key/val pairs) * @param string $method HTTP query method * @param bool $rawResponse * @return array decoded request result or error */ - public function request(?string $userId, string $endPoint, array $params = [], string $method = 'GET', bool $rawResponse = false): array { + public function request(string $endPoint, array $params = [], string $method = 'GET', bool $rawResponse = false): array { try { $url = 'https://nominatim.openstreetmap.org/' . $endPoint; $options = [ diff --git a/src/components/map/MaplibreMap.vue b/src/components/map/MaplibreMap.vue index b13ca86..1879fda 100644 --- a/src/components/map/MaplibreMap.vue +++ b/src/components/map/MaplibreMap.vue @@ -79,7 +79,7 @@ export default { }, mapStyle: { type: String, - default: 'streets', + default: 'osmRaster', }, area: { type: Object, @@ -122,12 +122,17 @@ export default { globeControl: null, myUseGlobe: this.useGlobe, apiKeys: loadState('integration_openstreetmap', 'api-keys'), - // https://api.maptiler.com/resources/logo.svg - maptilerLogoUrl: generateUrl('/apps/integration_openstreetmap/maptiler/resources/logo.svg'), + proxyMapRequests: loadState('integration_openstreetmap', 'proxy-map-requests', false), + maptilerLogoUrl: loadState('integration_openstreetmap', 'proxy-map-requests', false) + ? generateUrl('/apps/integration_openstreetmap/maptiler/resources/logo.svg') + : 'https://api.maptiler.com/resources/logo.svg', } }, computed: { + hasMaptilerApiKey() { + return !!this.apiKeys?.maptiler_api_key + }, }, watch: { @@ -191,11 +196,16 @@ export default { initMap() { const apiKey = this.apiKeys.maptiler_api_key // tile servers and styles + const vectorStyles = apiKey ? getVectorStyles(apiKey) : {} this.styles = { - ...getVectorStyles(apiKey), + ...vectorStyles, ...getRasterTileServers(apiKey), } - const restoredStyleKey = Object.keys(this.styles).includes(this.mapStyle) ? this.mapStyle : 'streets' + const restoredStyleKey = Object.keys(this.styles).includes(this.mapStyle) + ? this.mapStyle + : apiKey + ? 'streets' + : 'osmRaster' const restoredStyleObj = this.styles[restoredStyleKey] this.$emit('update:mapStyle', restoredStyleKey) @@ -209,6 +219,12 @@ export default { // bounds, maxPitch: 75, maxZoom: restoredStyleObj.maxzoom ? (restoredStyleObj.maxzoom - 0.01) : DEFAULT_MAP_MAX_ZOOM, + transformRequest: (url, resourceType) => { + return { + url, + referrerPolicy: 'origin-when-cross-origin', + } + }, } this.map = new Map(mapOptions) if (this.bbox) { @@ -278,9 +294,11 @@ export default { this.map.addControl(tileControl, 'top-right') this.map.addControl(fullscreenControl, 'top-right') - this.terrainControl = new TerrainControl() - this.terrainControl.on('toggleTerrain', this.toggleTerrain) - this.map.addControl(this.terrainControl, 'top-right') + if (this.hasMaptilerApiKey) { + this.terrainControl = new TerrainControl() + this.terrainControl.on('toggleTerrain', this.toggleTerrain) + this.map.addControl(this.terrainControl, 'top-right') + } this.globeControl = new GlobeControl() this.globeControl.on('toggleGlobe', this.toggleGlobe) @@ -307,7 +325,9 @@ export default { this.map.on('load', () => { this.loadImages() - this.terrainControl.updateTerrainIcon(this.myUseTerrain) + if (this.hasMaptilerApiKey) { + this.terrainControl.updateTerrainIcon(this.myUseTerrain) + } this.globeControl.updateGlobeIcon(this.myUseGlobe) setTimeout(() => { @@ -363,11 +383,14 @@ export default { // re render the layers this.mapLoaded = false this.loadImages() - if (this.myUseTerrain) { + if (this.hasMaptilerApiKey && this.myUseTerrain) { this.enableTerrain() } }, toggleTerrain() { + if (!this.hasMaptilerApiKey) { + return + } this.myUseTerrain = !this.myUseTerrain this.$emit('update:useTerrain', this.myUseTerrain) if (this.myUseTerrain) { @@ -378,6 +401,9 @@ export default { this.terrainControl.updateTerrainIcon(this.myUseTerrain) }, enableTerrain() { + if (!this.hasMaptilerApiKey) { + return + } this.addTerrainSource() this.map.setTerrain({ source: 'terrain', @@ -385,17 +411,24 @@ export default { }) }, disableTerrain() { + if (!this.hasMaptilerApiKey) { + return + } this.map.setTerrain() }, addTerrainSource() { + if (!this.hasMaptilerApiKey) { + return + } if (this.map.getSource('terrain')) { return } const apiKey = this.apiKeys.maptiler_api_key this.map.addSource('terrain', { type: 'raster-dem', - // url: 'https://api.maptiler.com/tiles/terrain-rgb/tiles.json?key=' + apiKey, - url: generateUrl('/apps/integration_openstreetmap/maptiler/tiles/terrain-rgb-v2/tiles.json?key=' + apiKey), + url: this.proxyMapRequests + ? generateUrl('/apps/integration_openstreetmap/maptiler/tiles/terrain-rgb-v2/tiles.json?key=' + apiKey) + : 'https://api.maptiler.com/tiles/terrain-rgb/tiles.json?key=' + apiKey, }) }, handleMapEvents() { diff --git a/src/lastMapStateHelper.js b/src/lastMapStateHelper.js index ce60515..8957c48 100644 --- a/src/lastMapStateHelper.js +++ b/src/lastMapStateHelper.js @@ -1,5 +1,6 @@ import { loadState } from '@nextcloud/initial-state' import { generateUrl } from '@nextcloud/router' +import { getCurrentUser } from '@nextcloud/auth' import axios from '@nextcloud/axios' import { linkTypes, routingLinkTypes } from './mapUtils.js' @@ -24,6 +25,12 @@ export function getLastMapState() { } export function setLastMapState({ lat, lon, zoom, pitch, bearing, mapStyle, terrain, globe, linkType, routingLinkType }) { + // in public pages + const currentUser = getCurrentUser() + if (currentUser === null) { + return + } + const state = { lat, lon, zoom, pitch, bearing, mapStyle, terrain, globe, linkType, routingLinkType } Object.keys(state).forEach(k => { if (state[k] !== undefined) { diff --git a/tests/integration/OsmAPIServiceIntegrationTest.php b/tests/integration/OsmAPIServiceIntegrationTest.php index 6d07188..18d6551 100644 --- a/tests/integration/OsmAPIServiceIntegrationTest.php +++ b/tests/integration/OsmAPIServiceIntegrationTest.php @@ -24,7 +24,7 @@ protected function setUp(): void { * that the response can be consumed by reference providers without crashing. */ public function testGeocode(): void { - $result = $this->service->geocode('integration-test', 44.3383486, 1.2086886, false); + $result = $this->service->geocode(44.3383486, 1.2086886, false); $this->assertIsArray($result); $this->assertArrayNotHasKey('error', $result, json_encode($result)); @@ -53,7 +53,7 @@ public function testGeocode(): void { * checks that entries contain the fields used by OsmSearchLocationProvider. */ public function testSearchLocation(): void { - $result = $this->service->searchLocation('integration-test', 'montcuq', 'json', [ + $result = $this->service->searchLocation('montcuq', 'json', [ 'addressdetails' => 1, 'extratags' => 1, 'namedetails' => 1, @@ -86,7 +86,7 @@ public function testSearchLocation(): void { * checks that the returned data can be used by OsmLocationReferenceProvider. */ public function testGetLocationInfo(): void { - $result = $this->service->getLocationInfo('integration-test', 87515, 'relation'); + $result = $this->service->getLocationInfo(87515, 'relation'); $this->assertNotNull($result); $this->assertArrayNotHasKey('error', $result, json_encode($result));