Skip to content

Commit 0f0d43d

Browse files
authored
Merge pull request #61 from nextcloud/fix/noid/public-pages
Fix public pages
2 parents 99aeed7 + b406046 commit 0f0d43d

16 files changed

Lines changed: 199 additions & 52 deletions

‎lib/AppInfo/Application.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class Application extends App implements IBootstrap {
4040
public const APP_ID = 'integration_openstreetmap';
4141
public const OSM_URL = 'https://www.openstreetmap.org';
4242

43-
public const DEFAULT_MAPTILER_API_KEY = 'get_your_own_OpIi9ZULNHzrESv6T2vL';
4443
public const DEFAULT_SEARCH_LOCATION_ENABLED_VALUE = '0';
4544
public const DEFAULT_PROXY_OSM_VALUE = '1';
4645

‎lib/Controller/OsmAPIController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function nominatimSearch(
6262
'namedetails' => $namedetails,
6363
'extratags' => $extratags,
6464
];
65-
$searchResults = $this->osmAPIService->searchLocation($this->userId, $q, $rformat, $extraParams, 0, $limit);
65+
$searchResults = $this->osmAPIService->searchLocation($q, $rformat, $extraParams, 0, $limit);
6666
if (isset($searchResults['error'])) {
6767
return new DataResponse('', Http::STATUS_BAD_REQUEST);
6868
}

‎lib/Listener/CSPListener.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CSPListener implements IEventListener {
3939
public function __construct(
4040
private IRequest $request,
4141
private IAppConfig $appConfig,
42+
private ?string $userId,
4243
) {
4344
}
4445

@@ -56,7 +57,10 @@ public function handle(Event $event): void {
5657
->addAllowedFrameDomain('https://www.openstreetmap.org')
5758
->addAllowedImageDomain('https://*.tile.openstreetmap.org');
5859

59-
$proxyOsm = $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1';
60+
// we do not proxy on public pages
61+
$proxyOsm = $this->userId === null
62+
? false
63+
: $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1';
6064
if (!$proxyOsm) {
6165
$policy
6266
->addAllowedConnectDomain('https://*.openstreetmap.org')

‎lib/Listener/OsmReferenceListener.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,25 @@ public function handle(Event $event): void {
5050
return;
5151
}
5252

53-
$maptilerApiKey = $this->appConfig->getValueString(Application::APP_ID, 'maptiler_api_key', Application::DEFAULT_MAPTILER_API_KEY) ?: Application::DEFAULT_MAPTILER_API_KEY;
53+
$maptilerApiKey = $this->appConfig->getValueString(Application::APP_ID, 'maptiler_api_key');
5454
$userConfig = [
5555
'maptiler_api_key' => $maptilerApiKey,
5656
];
5757
$this->initialState->provideInitialState('api-keys', $userConfig);
5858

59+
// public pages
60+
if ($this->userId === null) {
61+
$this->initialState->provideInitialState('prefer-osm-frame', false);
62+
$this->initialState->provideInitialState('proxy-map-requests', false);
63+
if ($maptilerApiKey === '') {
64+
$this->initialState->provideInitialState('last-map-state', [
65+
'mapStyle' => 'osmRaster',
66+
]);
67+
}
68+
Util::addScript(Application::APP_ID, Application::APP_ID . '-referenceLocation');
69+
return;
70+
}
71+
5972
$preferSimpleOsmIframe = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'prefer_simple_osm_iframe', '0') === '1';
6073
$this->initialState->provideInitialState('prefer-osm-frame', $preferSimpleOsmIframe);
6174
$proxyMapRequests = $this->appConfig->getValueString(Application::APP_ID, 'proxy_osm', Application::DEFAULT_PROXY_OSM_VALUE) === '1';

‎lib/Reference/BingReferenceProvider.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace OCA\Osm\Reference;
2525

2626
use OCA\Osm\AppInfo\Application;
27+
use OCP\Collaboration\Reference\IPublicReferenceProvider;
2728
use OCP\Collaboration\Reference\IReference;
2829
use OCP\Collaboration\Reference\IReferenceManager;
2930
use OCP\Collaboration\Reference\IReferenceProvider;
@@ -32,7 +33,7 @@
3233
use OCP\Config\IUserConfig;
3334
use OCP\IAppConfig;
3435

35-
class BingReferenceProvider implements IReferenceProvider {
36+
class BingReferenceProvider implements IReferenceProvider, IPublicReferenceProvider {
3637

3738
private const RICH_OBJECT_TYPE = Application::APP_ID . '_location';
3839

@@ -50,14 +51,20 @@ public function __construct(
5051
*/
5152
public function matchReference(string $referenceText): bool {
5253
$adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1';
53-
$userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
54+
$userLinkPreviewEnabled = $this->userId === null
55+
? true
56+
: $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
5457
if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) {
5558
return false;
5659
}
5760

5861
return $this->getCoordinates($referenceText) !== null;
5962
}
6063

64+
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
65+
return $this->resolveReference($referenceText);
66+
}
67+
6168
/**
6269
* @inheritDoc
6370
*/
@@ -152,6 +159,13 @@ public function getCacheKey(string $referenceId): ?string {
152159
return $referenceId;
153160
}
154161

162+
/**
163+
* @inheritDoc
164+
*/
165+
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
166+
return null;
167+
}
168+
155169
/**
156170
* @param string $userId
157171
* @return void

‎lib/Reference/DuckduckgoReferenceProvider.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace OCA\Osm\Reference;
2525

2626
use OCA\Osm\AppInfo\Application;
27+
use OCP\Collaboration\Reference\IPublicReferenceProvider;
2728
use OCP\Collaboration\Reference\IReference;
2829
use OCP\Collaboration\Reference\IReferenceManager;
2930
use OCP\Collaboration\Reference\IReferenceProvider;
@@ -32,7 +33,7 @@
3233
use OCP\Config\IUserConfig;
3334
use OCP\IAppConfig;
3435

35-
class DuckduckgoReferenceProvider implements IReferenceProvider {
36+
class DuckduckgoReferenceProvider implements IReferenceProvider, IPublicReferenceProvider {
3637

3738
private const RICH_OBJECT_TYPE = Application::APP_ID . '_location';
3839

@@ -50,14 +51,20 @@ public function __construct(
5051
*/
5152
public function matchReference(string $referenceText): bool {
5253
$adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1';
53-
$userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
54+
$userLinkPreviewEnabled = $this->userId === null
55+
? true
56+
: $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
5457
if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) {
5558
return false;
5659
}
5760

5861
return $this->getCoordinates($referenceText) !== null;
5962
}
6063

64+
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
65+
return $this->resolveReference($referenceText);
66+
}
67+
6168
/**
6269
* @inheritDoc
6370
*/
@@ -119,6 +126,13 @@ public function getCacheKey(string $referenceId): ?string {
119126
return $referenceId;
120127
}
121128

129+
/**
130+
* @inheritDoc
131+
*/
132+
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
133+
return null;
134+
}
135+
122136
/**
123137
* @param string $userId
124138
* @return void

‎lib/Reference/GoogleMapsReferenceProvider.php‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCA\Osm\AppInfo\Application;
2727
use OCA\Osm\Service\OsmAPIService;
2828
use OCA\Osm\Service\UtilsService;
29+
use OCP\Collaboration\Reference\IPublicReferenceProvider;
2930
use OCP\Collaboration\Reference\IReference;
3031
use OCP\Collaboration\Reference\IReferenceManager;
3132
use OCP\Collaboration\Reference\IReferenceProvider;
@@ -36,7 +37,7 @@
3637

3738
use OCP\IURLGenerator;
3839

39-
class GoogleMapsReferenceProvider implements IReferenceProvider {
40+
class GoogleMapsReferenceProvider implements IReferenceProvider, IPublicReferenceProvider {
4041

4142
private const RICH_OBJECT_TYPE = Application::APP_ID . '_location';
4243

@@ -57,22 +58,28 @@ public function __construct(
5758
*/
5859
public function matchReference(string $referenceText): bool {
5960
$adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1';
60-
$userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
61+
$userLinkPreviewEnabled = $this->userId === null
62+
? true
63+
: $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
6164
if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) {
6265
return false;
6366
}
6467

6568
return $this->getCoordinates($referenceText) !== null;
6669
}
6770

71+
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
72+
return $this->resolveReference($referenceText);
73+
}
74+
6875
/**
6976
* @inheritDoc
7077
*/
7178
public function resolveReference(string $referenceText): ?IReference {
7279
if ($this->matchReference($referenceText)) {
7380
$coords = $this->getCoordinates($referenceText);
7481
if ($coords !== null) {
75-
$pointInfo = $this->osmAPIService->geocode($this->userId, $coords['lat'], $coords['lon'], false);
82+
$pointInfo = $this->osmAPIService->geocode($coords['lat'], $coords['lon'], false);
7683
if (!isset($pointInfo['error'])) {
7784
$pointInfo['url'] = $referenceText;
7885
$reference = new Reference($referenceText);
@@ -248,6 +255,13 @@ public function getCacheKey(string $referenceId): ?string {
248255
return $referenceId;
249256
}
250257

258+
/**
259+
* @inheritDoc
260+
*/
261+
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
262+
return null;
263+
}
264+
251265
/**
252266
* @param string $userId
253267
* @return void

‎lib/Reference/HereMapsReferenceProvider.php‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use OCA\Osm\AppInfo\Application;
2727
use OCA\Osm\Service\OsmAPIService;
28+
use OCP\Collaboration\Reference\IPublicReferenceProvider;
2829
use OCP\Collaboration\Reference\IReference;
2930
use OCP\Collaboration\Reference\IReferenceManager;
3031
use OCP\Collaboration\Reference\IReferenceProvider;
@@ -35,7 +36,7 @@
3536

3637
use OCP\IURLGenerator;
3738

38-
class HereMapsReferenceProvider implements IReferenceProvider {
39+
class HereMapsReferenceProvider implements IReferenceProvider, IPublicReferenceProvider {
3940

4041
private const RICH_OBJECT_TYPE = Application::APP_ID . '_location';
4142

@@ -55,22 +56,28 @@ public function __construct(
5556
*/
5657
public function matchReference(string $referenceText): bool {
5758
$adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1';
58-
$userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
59+
$userLinkPreviewEnabled = $this->userId === null
60+
? true
61+
: $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
5962
if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) {
6063
return false;
6164
}
6265

6366
return $this->getCoordinates($referenceText) !== null;
6467
}
6568

69+
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
70+
return $this->resolveReference($referenceText);
71+
}
72+
6673
/**
6774
* @inheritDoc
6875
*/
6976
public function resolveReference(string $referenceText): ?IReference {
7077
if ($this->matchReference($referenceText)) {
7178
$coords = $this->getCoordinates($referenceText);
7279
if ($coords !== null) {
73-
$pointInfo = $this->osmAPIService->geocode($this->userId, $coords['lat'], $coords['lon'], false);
80+
$pointInfo = $this->osmAPIService->geocode($coords['lat'], $coords['lon'], false);
7481
if (!isset($pointInfo['error'])) {
7582
$pointInfo['url'] = $referenceText;
7683
$reference = new Reference($referenceText);
@@ -173,6 +180,13 @@ public function getCacheKey(string $referenceId): ?string {
173180
return $referenceId;
174181
}
175182

183+
/**
184+
* @inheritDoc
185+
*/
186+
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
187+
return null;
188+
}
189+
176190
/**
177191
* @param string $userId
178192
* @return void

‎lib/Reference/OsmLocationReferenceProvider.php‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCA\Osm\AppInfo\Application;
2727
use OCA\Osm\Service\OsmAPIService;
2828
use OCA\Osm\Service\UtilsService;
29+
use OCP\Collaboration\Reference\IPublicReferenceProvider;
2930
use OCP\Collaboration\Reference\IReference;
3031
use OCP\Collaboration\Reference\IReferenceManager;
3132
use OCP\Collaboration\Reference\IReferenceProvider;
@@ -36,7 +37,7 @@
3637

3738
use OCP\IURLGenerator;
3839

39-
class OsmLocationReferenceProvider implements IReferenceProvider {
40+
class OsmLocationReferenceProvider implements IReferenceProvider, IPublicReferenceProvider {
4041

4142
private const RICH_OBJECT_TYPE = Application::APP_ID . '_location';
4243

@@ -57,22 +58,28 @@ public function __construct(
5758
*/
5859
public function matchReference(string $referenceText): bool {
5960
$adminLinkPreviewEnabled = $this->appConfig->getValueString(Application::APP_ID, 'link_preview_enabled', '1') === '1';
60-
$userLinkPreviewEnabled = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
61+
$userLinkPreviewEnabled = $this->userId === null
62+
? true
63+
: $this->userConfig->getValueString($this->userId, Application::APP_ID, 'link_preview_enabled', '1') === '1';
6164
if (!$adminLinkPreviewEnabled || !$userLinkPreviewEnabled) {
6265
return false;
6366
}
6467

6568
return $this->getCoordinates($referenceText) !== null || $this->getLocationTypeId($referenceText) !== null;
6669
}
6770

71+
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
72+
return $this->resolveReference($referenceText);
73+
}
74+
6875
/**
6976
* @inheritDoc
7077
*/
7178
public function resolveReference(string $referenceText): ?IReference {
7279
if ($this->matchReference($referenceText)) {
7380
$coords = $this->getCoordinates($referenceText);
7481
$locationTypeId = $this->getLocationTypeId($referenceText);
75-
$locationInfo = $this->osmAPIService->getLocationInfo($this->userId, $locationTypeId['id'], $locationTypeId['type']);
82+
$locationInfo = $this->osmAPIService->getLocationInfo($locationTypeId['id'], $locationTypeId['type']);
7683
if ($locationInfo !== null) {
7784
$locationInfo['url'] = $referenceText;
7885
$reference = new Reference($referenceText);
@@ -185,6 +192,13 @@ public function getCacheKey(string $referenceId): ?string {
185192
return $referenceId;
186193
}
187194

195+
/**
196+
* @inheritDoc
197+
*/
198+
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
199+
return null;
200+
}
201+
188202
/**
189203
* @param string $userId
190204
* @return void

0 commit comments

Comments
 (0)