summaryrefslogtreecommitdiff
path: root/lib/facebook-graph-sdk/src/Facebook/Http
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2017-01-16 22:04:43 +0100
committeremkael <emkael@tlen.pl>2017-01-16 22:50:24 +0100
commit677953067f2bb5502a70f0d004f1ac844b18a128 (patch)
tree003c26454b543c2a8d73f0602446482fdbbef8db /lib/facebook-graph-sdk/src/Facebook/Http
parentf7b2bfae9778af2c99e0c7fe7b2634e0f4f0973f (diff)
* Facebook support
Diffstat (limited to 'lib/facebook-graph-sdk/src/Facebook/Http')
-rw-r--r--lib/facebook-graph-sdk/src/Facebook/Http/GraphRawResponse.php137
-rw-r--r--lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyInterface.php39
-rw-r--r--lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyMultipart.php170
-rw-r--r--lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyUrlEncoded.php55
4 files changed, 401 insertions, 0 deletions
diff --git a/lib/facebook-graph-sdk/src/Facebook/Http/GraphRawResponse.php b/lib/facebook-graph-sdk/src/Facebook/Http/GraphRawResponse.php
new file mode 100644
index 0000000..583d303
--- /dev/null
+++ b/lib/facebook-graph-sdk/src/Facebook/Http/GraphRawResponse.php
@@ -0,0 +1,137 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright notice
+ * shall be included in all copies or substantial portions of the software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+namespace Facebook\Http;
+
+/**
+ * Class GraphRawResponse
+ *
+ * @package Facebook
+ */
+class GraphRawResponse
+{
+ /**
+ * @var array The response headers in the form of an associative array.
+ */
+ protected $headers;
+
+ /**
+ * @var string The raw response body.
+ */
+ protected $body;
+
+ /**
+ * @var int The HTTP status response code.
+ */
+ protected $httpResponseCode;
+
+ /**
+ * Creates a new GraphRawResponse entity.
+ *
+ * @param string|array $headers The headers as a raw string or array.
+ * @param string $body The raw response body.
+ * @param int $httpStatusCode The HTTP response code (if sending headers as parsed array).
+ */
+ public function __construct($headers, $body, $httpStatusCode = null)
+ {
+ if (is_numeric($httpStatusCode)) {
+ $this->httpResponseCode = (int)$httpStatusCode;
+ }
+
+ if (is_array($headers)) {
+ $this->headers = $headers;
+ } else {
+ $this->setHeadersFromString($headers);
+ }
+
+ $this->body = $body;
+ }
+
+ /**
+ * Return the response headers.
+ *
+ * @return array
+ */
+ public function getHeaders()
+ {
+ return $this->headers;
+ }
+
+ /**
+ * Return the body of the response.
+ *
+ * @return string
+ */
+ public function getBody()
+ {
+ return $this->body;
+ }
+
+ /**
+ * Return the HTTP response code.
+ *
+ * @return int
+ */
+ public function getHttpResponseCode()
+ {
+ return $this->httpResponseCode;
+ }
+
+ /**
+ * Sets the HTTP response code from a raw header.
+ *
+ * @param string $rawResponseHeader
+ */
+ public function setHttpResponseCodeFromHeader($rawResponseHeader)
+ {
+ preg_match('|HTTP/\d\.\d\s+(\d+)\s+.*|', $rawResponseHeader, $match);
+ $this->httpResponseCode = (int)$match[1];
+ }
+
+ /**
+ * Parse the raw headers and set as an array.
+ *
+ * @param string $rawHeaders The raw headers from the response.
+ */
+ protected function setHeadersFromString($rawHeaders)
+ {
+ // Normalize line breaks
+ $rawHeaders = str_replace("\r\n", "\n", $rawHeaders);
+
+ // There will be multiple headers if a 301 was followed
+ // or a proxy was followed, etc
+ $headerCollection = explode("\n\n", trim($rawHeaders));
+ // We just want the last response (at the end)
+ $rawHeader = array_pop($headerCollection);
+
+ $headerComponents = explode("\n", $rawHeader);
+ foreach ($headerComponents as $line) {
+ if (strpos($line, ': ') === false) {
+ $this->setHttpResponseCodeFromHeader($line);
+ } else {
+ list($key, $value) = explode(': ', $line);
+ $this->headers[$key] = $value;
+ }
+ }
+ }
+}
diff --git a/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyInterface.php b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyInterface.php
new file mode 100644
index 0000000..97e0a2e
--- /dev/null
+++ b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyInterface.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright notice
+ * shall be included in all copies or substantial portions of the software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+namespace Facebook\Http;
+
+/**
+ * Interface
+ *
+ * @package Facebook
+ */
+interface RequestBodyInterface
+{
+ /**
+ * Get the body of the request to send to Graph.
+ *
+ * @return string
+ */
+ public function getBody();
+}
diff --git a/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyMultipart.php b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyMultipart.php
new file mode 100644
index 0000000..82f1438
--- /dev/null
+++ b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyMultipart.php
@@ -0,0 +1,170 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright notice
+ * shall be included in all copies or substantial portions of the software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+namespace Facebook\Http;
+
+use Facebook\FileUpload\FacebookFile;
+
+/**
+ * Class RequestBodyMultipartt
+ *
+ * Some things copied from Guzzle
+ *
+ * @package Facebook
+ *
+ * @see https://github.com/guzzle/guzzle/blob/master/src/Post/MultipartBody.php
+ */
+class RequestBodyMultipart implements RequestBodyInterface
+{
+ /**
+ * @var string The boundary.
+ */
+ private $boundary;
+
+ /**
+ * @var array The parameters to send with this request.
+ */
+ private $params;
+
+ /**
+ * @var array The files to send with this request.
+ */
+ private $files = [];
+
+ /**
+ * @param array $params The parameters to send with this request.
+ * @param array $files The files to send with this request.
+ * @param string $boundary Provide a specific boundary.
+ */
+ public function __construct(array $params = [], array $files = [], $boundary = null)
+ {
+ $this->params = $params;
+ $this->files = $files;
+ $this->boundary = $boundary ?: uniqid();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getBody()
+ {
+ $body = '';
+
+ // Compile normal params
+ $params = $this->getNestedParams($this->params);
+ foreach ($params as $k => $v) {
+ $body .= $this->getParamString($k, $v);
+ }
+
+ // Compile files
+ foreach ($this->files as $k => $v) {
+ $body .= $this->getFileString($k, $v);
+ }
+
+ // Peace out
+ $body .= "--{$this->boundary}--\r\n";
+
+ return $body;
+ }
+
+ /**
+ * Get the boundary
+ *
+ * @return string
+ */
+ public function getBoundary()
+ {
+ return $this->boundary;
+ }
+
+ /**
+ * Get the string needed to transfer a file.
+ *
+ * @param string $name
+ * @param FacebookFile $file
+ *
+ * @return string
+ */
+ private function getFileString($name, FacebookFile $file)
+ {
+ return sprintf(
+ "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"%s\r\n\r\n%s\r\n",
+ $this->boundary,
+ $name,
+ $file->getFileName(),
+ $this->getFileHeaders($file),
+ $file->getContents()
+ );
+ }
+
+ /**
+ * Get the string needed to transfer a POST field.
+ *
+ * @param string $name
+ * @param string $value
+ *
+ * @return string
+ */
+ private function getParamString($name, $value)
+ {
+ return sprintf(
+ "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n",
+ $this->boundary,
+ $name,
+ $value
+ );
+ }
+
+ /**
+ * Returns the params as an array of nested params.
+ *
+ * @param array $params
+ *
+ * @return array
+ */
+ private function getNestedParams(array $params)
+ {
+ $query = http_build_query($params, null, '&');
+ $params = explode('&', $query);
+ $result = [];
+
+ foreach ($params as $param) {
+ list($key, $value) = explode('=', $param, 2);
+ $result[urldecode($key)] = urldecode($value);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Get the headers needed before transferring the content of a POST file.
+ *
+ * @param FacebookFile $file
+ *
+ * @return string
+ */
+ protected function getFileHeaders(FacebookFile $file)
+ {
+ return "\r\nContent-Type: {$file->getMimetype()}";
+ }
+}
diff --git a/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyUrlEncoded.php b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyUrlEncoded.php
new file mode 100644
index 0000000..77c2b64
--- /dev/null
+++ b/lib/facebook-graph-sdk/src/Facebook/Http/RequestBodyUrlEncoded.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright notice
+ * shall be included in all copies or substantial portions of the software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+namespace Facebook\Http;
+
+/**
+ * Class RequestBodyUrlEncoded
+ *
+ * @package Facebook
+ */
+class RequestBodyUrlEncoded implements RequestBodyInterface
+{
+ /**
+ * @var array The parameters to send with this request.
+ */
+ protected $params = [];
+
+ /**
+ * Creates a new GraphUrlEncodedBody entity.
+ *
+ * @param array $params
+ */
+ public function __construct(array $params)
+ {
+ $this->params = $params;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getBody()
+ {
+ return http_build_query($this->params, null, '&');
+ }
+}