Skip to content

Commit e25e538

Browse files
committed
Merge branch 'multiple-base-urls'
This is a slightly updated version of pull request #419. Should fix the #416 issue.
2 parents 1807576 + 70a8da2 commit e25e538

File tree

4 files changed

+120
-11
lines changed

4 files changed

+120
-11
lines changed

okapi/core.php

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,81 @@ public static function get_oc_schema_code()
12191219
}
12201220
}
12211221

1222+
/**
1223+
* Return the recommended okapi_base_url.
1224+
*
1225+
* This is the URL which we want all *new* client applications to use.
1226+
* OKAPI will suggest URLs with this prefix in various context, e.g. in all
1227+
* the dynamically generated docs.
1228+
*
1229+
* Also see `get_allowed_base_urls` method.
1230+
*/
1231+
public static function get_recommended_base_url()
1232+
{
1233+
return Settings::get('SITE_URL')."okapi/";
1234+
}
1235+
1236+
/**
1237+
* Return a list of okapi_base_urls allowed to be used when calling OKAPI
1238+
* methods in this installation.
1239+
*
1240+
* Since issue #416, the "recommended" okapi_base_url is *not* the only one
1241+
* allowed (actually, there were more allowed before issue #416, but they
1242+
* weren't allowed "officially").
1243+
*/
1244+
public static function get_allowed_base_urls()
1245+
{
1246+
/* Currently, there are no config settings which would let us allow
1247+
* to determine the proper values for this list. So, we need to have it
1248+
* hardcoded. (Perhaps we should move this to etc/installations.xml?
1249+
* But this wouldn't be efficient...) */
1250+
1251+
switch (self::get_oc_schema_code()) {
1252+
case 'OCPL':
1253+
$urls = array(
1254+
"http://opencaching.pl/okapi/",
1255+
"http://www.opencaching.pl/okapi/",
1256+
);
1257+
break;
1258+
case 'OCDE':
1259+
$urls = array(
1260+
"http://www.opencaching.de/okapi/",
1261+
"https://www.opencaching.de/okapi/",
1262+
);
1263+
break;
1264+
case 'OCNL':
1265+
$urls = array(
1266+
"http://www.opencaching.nl/okapi/",
1267+
);
1268+
break;
1269+
case 'OCRO':
1270+
$urls = array(
1271+
"http://www.opencaching.ro/okapi/",
1272+
);
1273+
break;
1274+
case 'OCORGUK':
1275+
$urls = array(
1276+
"http://www.opencaching.org.uk/okapi/",
1277+
);
1278+
break;
1279+
case 'OCUS':
1280+
$urls = array(
1281+
"http://www.opencaching.us/okapi/",
1282+
"http://opencaching.us/okapi/",
1283+
);
1284+
break;
1285+
default:
1286+
/* Unknown site. No extra allowed URLs. */
1287+
$urls = array();
1288+
}
1289+
1290+
if (!in_array(self::get_recommended_base_url(), $urls)) {
1291+
$urls[] = self::get_recommended_base_url();
1292+
}
1293+
1294+
return $urls;
1295+
}
1296+
12221297
/**
12231298
* Pick text from $langdict based on language preference $langpref.
12241299
*
@@ -2407,11 +2482,33 @@ public function __construct($options)
24072482
private function init_request()
24082483
{
24092484
$this->request = OAuthRequest::from_request();
2410-
if (!in_array($this->request->get_normalized_http_method(),
2411-
array('GET', 'POST')))
2412-
{
2485+
2486+
/* Verify if the request was issued with proper HTTP method. */
2487+
2488+
if (!in_array(
2489+
$this->request->get_normalized_http_method(),
2490+
array('GET', 'POST')
2491+
)) {
24132492
throw new BadRequest("Use GET and POST methods only.");
24142493
}
2494+
2495+
/* Verify if the request was issued with proper okapi_base_url. */
2496+
2497+
$url = $this->request->get_normalized_http_url();
2498+
$allowed = false;
2499+
foreach (Okapi::get_allowed_base_urls() as $allowed_prefix) {
2500+
if (strpos($url, $allowed_prefix) === 0) {
2501+
$allowed = true;
2502+
break;
2503+
}
2504+
}
2505+
if (!$allowed) {
2506+
throw new BadRequest(
2507+
"Unrecognized base URL prefix! See `okapi_base_urls` field ".
2508+
"in the `services/apisrv/installation` method. (Recommended ".
2509+
"base URL to use is '".Okapi::get_recommended_base_url()."'.)"
2510+
);
2511+
}
24152512
}
24162513

24172514
/**

okapi/services/apisrv/installation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static function call(OkapiRequest $request)
2020
{
2121
$result = array();
2222
$result['site_url'] = Settings::get('SITE_URL');
23-
$result['okapi_base_url'] = $result['site_url']."okapi/";
23+
$result['okapi_base_url'] = Okapi::get_recommended_base_url();
24+
$result['okapi_base_urls'] = Okapi::get_allowed_base_urls();
2425
$result['site_name'] = Okapi::get_normalized_site_name();
2526
$result['okapi_version_number'] = Okapi::$version_number;
2627
$result['okapi_revision'] = Okapi::$version_number; /* Important for backward-compatibility! */

okapi/services/apisrv/installation.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
level domain of a country).
1616
</li>
1717
<li>
18-
<b>okapi_base_url</b> - URL of the OKAPI installation (usually this is
19-
<b>site_url</b> with "okapi/" appended, but you should not assume
20-
that); this value is to be used as a prefix when constructing service
21-
method URLs,
18+
<b>okapi_base_url</b> - the <i>recommended</i> base URL to be used when
19+
accessing this OKAPI installation (it should be used as a prefix when
20+
constructing method URLs),
21+
</li>
22+
<li>
23+
<p><b>okapi_base_urls</b> - a list of <i>all</i> base URLs allowed to be used
24+
when calling OKAPI methods in this installation.</p>
25+
26+
<p>In theory, once a new base URL appears on this list, it should never
27+
disappear (it should be allowed to use it forever). However, for various
28+
reasons, we cannot guarantee it will indeed be so. Clients SHOULD use the
29+
recommended base URL provided in the <b>okapi_base_url</b> field.</p>
2230
</li>
2331
<li>
2432
<b>site_name</b> - international name of the Opencaching site,

okapi/settings.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ final class Settings
130130
'DB_CHARSET' => 'utf8',
131131

132132
/**
133-
* URL of this site (with slash, and without "/okapi"). If this is a
134-
* development installation, it should point to the local URL (e.g.
135-
* "http://localhost/".
133+
* Canonical URL of this Opencaching site (with a trailing slash). If
134+
* you prefer your site to be accessed via HTTPS, then this should be
135+
* a "https://" URL.
136+
*
137+
* If this is a development installation, it should point to the local
138+
* URL (e.g. "http://localhost/").
136139
*/
137140
'SITE_URL' => null,
138141

0 commit comments

Comments
 (0)