DataTableManipulator
2 years ago
ApiRenderer.php
2 years ago
CORSHandler.php
2 years ago
DataTableGenericFilter.php
2 years ago
DataTableManipulator.php
2 years ago
DataTablePostProcessor.php
2 years ago
DocumentationGenerator.php
2 years ago
Inconsistencies.php
2 years ago
NoDefaultValue.php
2 years ago
Proxy.php
2 years ago
Request.php
2 years ago
ResponseBuilder.php
2 years ago
CORSHandler.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\API; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Url; |
| 14 | class CORSHandler |
| 15 | { |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $domains; |
| 20 | public function __construct() |
| 21 | { |
| 22 | $this->domains = Url::getCorsHostsFromConfig(); |
| 23 | } |
| 24 | public function handle() |
| 25 | { |
| 26 | if (empty($this->domains)) { |
| 27 | return; |
| 28 | } |
| 29 | Common::sendHeader('Vary: Origin'); |
| 30 | // allow Piwik to serve data to all domains |
| 31 | if (in_array("*", $this->domains)) { |
| 32 | Common::sendHeader('Access-Control-Allow-Credentials: true'); |
| 33 | if (!empty($_SERVER['HTTP_ORIGIN'])) { |
| 34 | Common::sendHeader('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); |
| 35 | return; |
| 36 | } |
| 37 | Common::sendHeader('Access-Control-Allow-Origin: *'); |
| 38 | return; |
| 39 | } |
| 40 | // specifically allow if it is one of the allowlisted CORS domains |
| 41 | if (!empty($_SERVER['HTTP_ORIGIN'])) { |
| 42 | $origin = $_SERVER['HTTP_ORIGIN']; |
| 43 | if (in_array($origin, $this->domains, true)) { |
| 44 | Common::sendHeader('Access-Control-Allow-Credentials: true'); |
| 45 | Common::sendHeader('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 |