2014-07-03 18:26:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
class CORSHelper
|
|
|
|
{
|
2017-03-10 15:10:56 +01:00
|
|
|
public static function enableCrossOriginRequests(&$request, &$response)
|
2014-07-03 18:26:09 +02:00
|
|
|
{
|
|
|
|
//Chrome sends the Origin header for all requests, so we whitelist the webserver's hostname as well.
|
|
|
|
$origin = $request->getHeader('Origin');
|
2014-10-27 16:07:12 +01:00
|
|
|
if ((!(preg_match("/https?:\/\/localhost/", $origin) === 1)) && ($origin != "") &&
|
2014-11-21 01:33:11 +01:00
|
|
|
(!in_array($origin, self::getAllowedOrigins())))
|
2014-07-03 18:26:09 +02:00
|
|
|
{
|
|
|
|
//Don't allow CORS from other domains to prevent XSS.
|
|
|
|
throw new Zend_Controller_Action_Exception('Forbidden', 403);
|
|
|
|
}
|
2017-03-10 15:10:56 +01:00
|
|
|
//Allow AJAX requests from configured websites. We use this to allow other pages to use LibreTimes API.
|
|
|
|
if ($origin) {
|
|
|
|
$response = $response->setHeader('Access-Control-Allow-Origin', $origin);
|
|
|
|
}
|
2014-07-03 18:26:09 +02:00
|
|
|
}
|
2014-11-21 01:33:11 +01:00
|
|
|
|
|
|
|
public static function getAllowedOrigins()
|
|
|
|
{
|
2017-03-10 15:10:56 +01:00
|
|
|
$allowedCorsUrls = array_map(
|
|
|
|
function($v) { return trim($v); },
|
|
|
|
explode(PHP_EOL, Application_Model_Preference::GetAllowedCorsUrls())
|
|
|
|
);
|
|
|
|
return array_merge($allowedCorsUrls, array(
|
2014-11-21 01:33:11 +01:00
|
|
|
"http://" . $_SERVER['SERVER_NAME'],
|
2017-03-10 15:10:56 +01:00
|
|
|
"https://" . $_SERVER['SERVER_NAME']));
|
2014-11-21 01:33:11 +01:00
|
|
|
}
|
2014-09-05 01:11:09 +02:00
|
|
|
}
|