@@ -1219,6 +1219,81 @@ public static function get_oc_schema_code()
1219
1219
}
1220
1220
}
1221
1221
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
+
1222
1297
/**
1223
1298
* Pick text from $langdict based on language preference $langpref.
1224
1299
*
@@ -2407,11 +2482,33 @@ public function __construct($options)
2407
2482
private function init_request ()
2408
2483
{
2409
2484
$ 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
+ )) {
2413
2492
throw new BadRequest ("Use GET and POST methods only. " );
2414
2493
}
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
+ }
2415
2512
}
2416
2513
2417
2514
/**
0 commit comments