From 43de9af71f7f4ca5731b94a06d688ae8412ba427 Mon Sep 17 00:00:00 2001 From: M.Gergo Date: Fri, 6 Jul 2018 11:14:41 +0200 Subject: 2018/Feb/28 -i állapot hozzáadva, mint a módosítások kiindulási állapota --- .../share/facebook/HttpClients/FacebookCurl.php | 129 ++++++++++++++++ .../HttpClients/FacebookCurlHttpClient.php | 163 +++++++++++++++++++++ .../HttpClients/FacebookGuzzleHttpClient.php | 97 ++++++++++++ .../HttpClients/FacebookHttpClientInterface.php | 47 ++++++ .../share/facebook/HttpClients/FacebookStream.php | 80 ++++++++++ .../HttpClients/FacebookStreamHttpClient.php | 94 ++++++++++++ .../facebook/HttpClients/HttpClientsFactory.php | 99 +++++++++++++ .../certs/DigiCertHighAssuranceEVRootCA.pem | 23 +++ 8 files changed, 732 insertions(+) create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookCurl.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookCurlHttpClient.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookGuzzleHttpClient.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookHttpClientInterface.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookStream.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/FacebookStreamHttpClient.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/HttpClientsFactory.php create mode 100644 mayor-orig/www/include/share/facebook/HttpClients/certs/DigiCertHighAssuranceEVRootCA.pem (limited to 'mayor-orig/www/include/share/facebook/HttpClients') diff --git a/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurl.php b/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurl.php new file mode 100644 index 00000000..28e4ba59 --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurl.php @@ -0,0 +1,129 @@ +curl = curl_init(); + } + + /** + * Set a curl option + * + * @param $key + * @param $value + */ + public function setopt($key, $value) + { + curl_setopt($this->curl, $key, $value); + } + + /** + * Set an array of options to a curl resource + * + * @param array $options + */ + public function setoptArray(array $options) + { + curl_setopt_array($this->curl, $options); + } + + /** + * Send a curl request + * + * @return mixed + */ + public function exec() + { + return curl_exec($this->curl); + } + + /** + * Return the curl error number + * + * @return int + */ + public function errno() + { + return curl_errno($this->curl); + } + + /** + * Return the curl error message + * + * @return string + */ + public function error() + { + return curl_error($this->curl); + } + + /** + * Get info from a curl reference + * + * @param $type + * + * @return mixed + */ + public function getinfo($type) + { + return curl_getinfo($this->curl, $type); + } + + /** + * Get the currently installed curl version + * + * @return array + */ + public function version() + { + return curl_version(); + } + + /** + * Close the resource connection to curl + */ + public function close() + { + curl_close($this->curl); + } +} diff --git a/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurlHttpClient.php b/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurlHttpClient.php new file mode 100644 index 00000000..059e75a5 --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/FacebookCurlHttpClient.php @@ -0,0 +1,163 @@ +facebookCurl = $facebookCurl ?: new FacebookCurl(); + } + + /** + * @inheritdoc + */ + public function send($url, $method, $body, array $headers, $timeOut) + { + $this->openConnection($url, $method, $body, $headers, $timeOut); + $this->sendRequest(); + + if ($curlErrorCode = $this->facebookCurl->errno()) { + throw new FacebookSDKException($this->facebookCurl->error(), $curlErrorCode); + } + + // Separate the raw headers from the raw body + list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody(); + + $this->closeConnection(); + + return new GraphRawResponse($rawHeaders, $rawBody); + } + + /** + * Opens a new curl connection. + * + * @param string $url The endpoint to send the request to. + * @param string $method The request method. + * @param string $body The body of the request. + * @param array $headers The request headers. + * @param int $timeOut The timeout in seconds for the request. + */ + public function openConnection($url, $method, $body, array $headers, $timeOut) + { + $options = [ + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_HTTPHEADER => $this->compileRequestHeaders($headers), + CURLOPT_URL => $url, + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_TIMEOUT => $timeOut, + CURLOPT_RETURNTRANSFER => true, // Follow 301 redirects + CURLOPT_HEADER => true, // Enable header processing + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_CAINFO => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', + ]; + + if ($method !== "GET") { + $options[CURLOPT_POSTFIELDS] = $body; + } + + $this->facebookCurl->init(); + $this->facebookCurl->setoptArray($options); + } + + /** + * Closes an existing curl connection + */ + public function closeConnection() + { + $this->facebookCurl->close(); + } + + /** + * Send the request and get the raw response from curl + */ + public function sendRequest() + { + $this->rawResponse = $this->facebookCurl->exec(); + } + + /** + * Compiles the request headers into a curl-friendly format. + * + * @param array $headers The request headers. + * + * @return array + */ + public function compileRequestHeaders(array $headers) + { + $return = []; + + foreach ($headers as $key => $value) { + $return[] = $key . ': ' . $value; + } + + return $return; + } + + /** + * Extracts the headers and the body into a two-part array + * + * @return array + */ + public function extractResponseHeadersAndBody() + { + $parts = explode("\r\n\r\n", $this->rawResponse); + $rawBody = array_pop($parts); + $rawHeaders = implode("\r\n\r\n", $parts); + + return [trim($rawHeaders), trim($rawBody)]; + } +} diff --git a/mayor-orig/www/include/share/facebook/HttpClients/FacebookGuzzleHttpClient.php b/mayor-orig/www/include/share/facebook/HttpClients/FacebookGuzzleHttpClient.php new file mode 100644 index 00000000..8feb7cb6 --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/FacebookGuzzleHttpClient.php @@ -0,0 +1,97 @@ +guzzleClient = $guzzleClient ?: new Client(); + } + + /** + * @inheritdoc + */ + public function send($url, $method, $body, array $headers, $timeOut) + { + $options = [ + 'headers' => $headers, + 'body' => $body, + 'timeout' => $timeOut, + 'connect_timeout' => 10, + 'verify' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', + ]; + $request = $this->guzzleClient->createRequest($method, $url, $options); + + try { + $rawResponse = $this->guzzleClient->send($request); + } catch (RequestException $e) { + $rawResponse = $e->getResponse(); + + if ($e->getPrevious() instanceof RingException || !$rawResponse instanceof ResponseInterface) { + throw new FacebookSDKException($e->getMessage(), $e->getCode()); + } + } + + $rawHeaders = $this->getHeadersAsString($rawResponse); + $rawBody = $rawResponse->getBody(); + $httpStatusCode = $rawResponse->getStatusCode(); + + return new GraphRawResponse($rawHeaders, $rawBody, $httpStatusCode); + } + + /** + * Returns the Guzzle array of headers as a string. + * + * @param ResponseInterface $response The Guzzle response. + * + * @return string + */ + public function getHeadersAsString(ResponseInterface $response) + { + $headers = $response->getHeaders(); + $rawHeaders = []; + foreach ($headers as $name => $values) { + $rawHeaders[] = $name . ": " . implode(", ", $values); + } + + return implode("\r\n", $rawHeaders); + } +} diff --git a/mayor-orig/www/include/share/facebook/HttpClients/FacebookHttpClientInterface.php b/mayor-orig/www/include/share/facebook/HttpClients/FacebookHttpClientInterface.php new file mode 100644 index 00000000..1fbf953d --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/FacebookHttpClientInterface.php @@ -0,0 +1,47 @@ +stream = stream_context_create($options); + } + + /** + * The response headers from the stream wrapper + * + * @return array + */ + public function getResponseHeaders() + { + return $this->responseHeaders; + } + + /** + * Send a stream wrapped request + * + * @param string $url + * + * @return mixed + */ + public function fileGetContents($url) + { + $rawResponse = file_get_contents($url, false, $this->stream); + $this->responseHeaders = $http_response_header ?: []; + + return $rawResponse; + } +} diff --git a/mayor-orig/www/include/share/facebook/HttpClients/FacebookStreamHttpClient.php b/mayor-orig/www/include/share/facebook/HttpClients/FacebookStreamHttpClient.php new file mode 100644 index 00000000..1cdfd539 --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/FacebookStreamHttpClient.php @@ -0,0 +1,94 @@ +facebookStream = $facebookStream ?: new FacebookStream(); + } + + /** + * @inheritdoc + */ + public function send($url, $method, $body, array $headers, $timeOut) + { + $options = [ + 'http' => [ + 'method' => $method, + 'header' => $this->compileHeader($headers), + 'content' => $body, + 'timeout' => $timeOut, + 'ignore_errors' => true + ], + 'ssl' => [ + 'verify_peer' => true, + 'verify_peer_name' => true, + 'allow_self_signed' => true, // All root certificates are self-signed + 'cafile' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', + ], + ]; + + $this->facebookStream->streamContextCreate($options); + $rawBody = $this->facebookStream->fileGetContents($url); + $rawHeaders = $this->facebookStream->getResponseHeaders(); + + if ($rawBody === false || empty($rawHeaders)) { + throw new FacebookSDKException('Stream returned an empty response', 660); + } + + $rawHeaders = implode("\r\n", $rawHeaders); + + return new GraphRawResponse($rawHeaders, $rawBody); + } + + /** + * Formats the headers for use in the stream wrapper. + * + * @param array $headers The request headers. + * + * @return string + */ + public function compileHeader(array $headers) + { + $header = []; + foreach ($headers as $k => $v) { + $header[] = $k . ': ' . $v; + } + + return implode("\r\n", $header); + } +} diff --git a/mayor-orig/www/include/share/facebook/HttpClients/HttpClientsFactory.php b/mayor-orig/www/include/share/facebook/HttpClients/HttpClientsFactory.php new file mode 100644 index 00000000..d9f2a8d3 --- /dev/null +++ b/mayor-orig/www/include/share/facebook/HttpClients/HttpClientsFactory.php @@ -0,0 +1,99 @@ +