|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Piwik - Open source web analytics |
| 4 | + * |
| 5 | + * @link http://piwik.org |
| 6 | + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace helpers; |
| 10 | + |
| 11 | +/** |
| 12 | + * Proxy for demo.matomo.cloud, allows us to make single origin requests by doing them through this class |
| 13 | + */ |
| 14 | +class DemoProxy |
| 15 | +{ |
| 16 | + |
| 17 | + private const MATOMO_SWAGGER_PROXY_TARGET = 'https://demo.matomo.cloud'; |
| 18 | + private const DEMO_AUTHORIZATION_HEADER = 'Bearer anonymous'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Fetches a demo API GET response and returns the body and status code. |
| 22 | + * |
| 23 | + * @param string $url Demo API URL to request. |
| 24 | + * @return array{body: string, statusCode: int} |
| 25 | + */ |
| 26 | + public static function get(string $url): array |
| 27 | + { |
| 28 | + $context = self::createContext(); |
| 29 | + $proxiedResponse = self::fetchResponse($url, $context); |
| 30 | + $statusCode = self::parseStatusCode($proxiedResponse['responseHeaders']); |
| 31 | + |
| 32 | + return [ |
| 33 | + 'body' => $proxiedResponse['body'], |
| 34 | + 'statusCode' => $statusCode, |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Builds a validated demo API URL for proxying. |
| 40 | + * |
| 41 | + * @param string $path Relative request path. |
| 42 | + * @param array $queryParams Query parameters to append to the URL. |
| 43 | + * @return string The validated absolute demo API URL. |
| 44 | + * @throws \InvalidArgumentException If the path is not index.php. |
| 45 | + * @throws \InvalidArgumentException If the module query parameter is not API. |
| 46 | + */ |
| 47 | + public static function buildValidatedApiUrl(string $path, array $queryParams): string |
| 48 | + { |
| 49 | + $normalizedPath = trim($path, '/'); |
| 50 | + if ($normalizedPath !== 'index.php') { |
| 51 | + throw new \InvalidArgumentException('Path must be index.php'); |
| 52 | + } |
| 53 | + |
| 54 | + if (($queryParams['module'] ?? null) !== 'API') { |
| 55 | + throw new \InvalidArgumentException('Module must be API'); |
| 56 | + } |
| 57 | + |
| 58 | + $targetUrl = rtrim(self::MATOMO_SWAGGER_PROXY_TARGET, '/') . '/' . $normalizedPath; |
| 59 | + $query = http_build_query($queryParams); |
| 60 | + |
| 61 | + if ($query !== '') { |
| 62 | + $targetUrl .= '?' . $query; |
| 63 | + } |
| 64 | + |
| 65 | + return $targetUrl; |
| 66 | + } |
| 67 | + |
| 68 | + private static function createContext() |
| 69 | + { |
| 70 | + return stream_context_create([ |
| 71 | + 'http' => [ |
| 72 | + 'method' => 'GET', |
| 73 | + 'header' => self::buildHeaders(), |
| 74 | + 'ignore_errors' => true, |
| 75 | + 'timeout' => 30, |
| 76 | + ], |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + private static function buildHeaders(): string |
| 81 | + { |
| 82 | + return 'Authorization: ' . self::DEMO_AUTHORIZATION_HEADER; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array{body: string, responseHeaders: array} |
| 87 | + */ |
| 88 | + private static function fetchResponse(string $url, $context): array |
| 89 | + { |
| 90 | + $body = @file_get_contents($url, false, $context); |
| 91 | + if ($body === false) { |
| 92 | + throw new \RuntimeException('Could not proxy HTTP request'); |
| 93 | + } |
| 94 | + |
| 95 | + return [ |
| 96 | + 'body' => $body, |
| 97 | + 'responseHeaders' => $http_response_header ?? [], |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + private static function parseStatusCode(array $responseHeaders): int |
| 102 | + { |
| 103 | + $statusLine = $responseHeaders[0] ?? ''; |
| 104 | + |
| 105 | + if (preg_match('#^HTTP/\S+\s+(\d{3})#', $statusLine, $matches)) { |
| 106 | + return (int) $matches[1]; |
| 107 | + } |
| 108 | + |
| 109 | + return 200; |
| 110 | + } |
| 111 | +} |
0 commit comments