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