vendor/drenso/symfony-oidc-bundle/src/OidcUrlFetcher.php line 78

Open in your IDE?
  1. <?php
  2. namespace Drenso\OidcBundle;
  3. use Drenso\OidcBundle\Security\Exception\OidcAuthenticationException;
  4. /**
  5.  * Helper for resource loading.
  6.  */
  7. class OidcUrlFetcher
  8. {
  9.   public function __construct(private readonly array $customClientHeaders)
  10.   {
  11.   }
  12.   /**
  13.    * Retrieve the content from the specified url.
  14.    *
  15.    * @param array|null $params  if this is set the request type will be POST
  16.    * @param array      $headers extra headers to be sent with the request
  17.    */
  18.   public function fetchUrl(string $url, ?array $params null, array $headers = []): string
  19.   {
  20.     // Create a new cURL resource handle
  21.     $ch curl_init();
  22.     // Determine whether this is a GET or POST
  23.     if ($params != null) {
  24.       // Check params
  25.       if (!is_array($params)) {
  26.         throw new OidcAuthenticationException('The parameters should be specified as array!');
  27.       }
  28.       $params http_build_query($params);
  29.       // Allows to keep the POST method even after redirect
  30.       curl_setopt($chCURLOPT_CUSTOMREQUEST'POST');
  31.       curl_setopt($chCURLOPT_POSTFIELDS$params);
  32.       // Add POST-specific headers
  33.       $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  34.       $headers[] = 'Content-Length: ' strlen($params);
  35.     }
  36.     // Add a User-Agent header to prevent firewall blocks
  37.     $curlVersion curl_version()['version'];
  38.     $headers[]   = "User-Agent: curl/$curlVersion drenso/symfony-oidc";
  39.     // Add custom headers to a existing headers
  40.     $headers array_merge($headers$this->customClientHeaders);
  41.     // Include headers
  42.     curl_setopt($chCURLOPT_HTTPHEADER$headers);
  43.     // Set URL to download
  44.     curl_setopt($chCURLOPT_URL$url);
  45.     // Include header in result? (0 = yes, 1 = no)
  46.     curl_setopt($chCURLOPT_HEADER0);
  47.     // Allows following redirects
  48.     curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
  49.     // Setup certificate checking
  50.     curl_setopt($chCURLOPT_SSL_VERIFYHOST2);
  51.     curl_setopt($chCURLOPT_SSL_VERIFYPEERtrue);
  52.     // Should cURL return or print out the data? (true = return, false = print)
  53.     curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  54.     // Timeout in seconds
  55.     curl_setopt($chCURLOPT_TIMEOUT20);
  56.     // Download the given URL, and return output
  57.     $output curl_exec($ch);
  58.     if ($output === false) {
  59.       throw new OidcAuthenticationException('Curl error: ' curl_error($ch));
  60.     }
  61.     // Close the cURL resource, and free system resources
  62.     curl_close($ch);
  63.     return $output;
  64.   }
  65. }