summaryrefslogtreecommitdiff
path: root/inc/mailgun/php-http/multipart-stream-builder/src
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
committerCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
commitb76e2ff898b23745d4c9aaee49eeb7d88f2896ab (patch)
tree9b794be8db310a575d70165d9ebde0a183b61b01 /inc/mailgun/php-http/multipart-stream-builder/src
parentbfcc9f7a7656a2db0c905b3c13114664f00f6c37 (diff)
downloadbulletin-b76e2ff898b23745d4c9aaee49eeb7d88f2896ab.tar.gz
Updated mailgun plugin
Diffstat (limited to 'inc/mailgun/php-http/multipart-stream-builder/src')
-rw-r--r--inc/mailgun/php-http/multipart-stream-builder/src/ApacheMimetypeHelper.php142
-rw-r--r--inc/mailgun/php-http/multipart-stream-builder/src/CustomMimetypeHelper.php51
-rw-r--r--inc/mailgun/php-http/multipart-stream-builder/src/MimetypeHelper.php27
-rw-r--r--inc/mailgun/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php279
4 files changed, 499 insertions, 0 deletions
diff --git a/inc/mailgun/php-http/multipart-stream-builder/src/ApacheMimetypeHelper.php b/inc/mailgun/php-http/multipart-stream-builder/src/ApacheMimetypeHelper.php
new file mode 100644
index 0000000..6e4ca66
--- /dev/null
+++ b/inc/mailgun/php-http/multipart-stream-builder/src/ApacheMimetypeHelper.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace Http\Message\MultipartStream;
+
+/**
+ * This class helps to find the proper mime types. The source of this file is taken
+ * from Guzzle.
+ *
+ * @author Michael Dowling and contributors to guzzlehttp/psr7
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+class ApacheMimetypeHelper implements MimetypeHelper
+{
+ /**
+ * {@inheritdoc}
+ *
+ * @see http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
+ */
+ public function getMimetypeFromFilename($filename)
+ {
+ return $this->getMimetypeFromExtension(pathinfo($filename, PATHINFO_EXTENSION));
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @see http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
+ */
+ public function getMimetypeFromExtension($extension)
+ {
+ static $mimetypes = [
+ '7z' => 'application/x-7z-compressed',
+ 'aac' => 'audio/x-aac',
+ 'ai' => 'application/postscript',
+ 'aif' => 'audio/x-aiff',
+ 'asc' => 'text/plain',
+ 'asf' => 'video/x-ms-asf',
+ 'atom' => 'application/atom+xml',
+ 'avi' => 'video/x-msvideo',
+ 'bmp' => 'image/bmp',
+ 'bz2' => 'application/x-bzip2',
+ 'cer' => 'application/pkix-cert',
+ 'crl' => 'application/pkix-crl',
+ 'crt' => 'application/x-x509-ca-cert',
+ 'css' => 'text/css',
+ 'csv' => 'text/csv',
+ 'cu' => 'application/cu-seeme',
+ 'deb' => 'application/x-debian-package',
+ 'doc' => 'application/msword',
+ 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'dvi' => 'application/x-dvi',
+ 'eot' => 'application/vnd.ms-fontobject',
+ 'eps' => 'application/postscript',
+ 'epub' => 'application/epub+zip',
+ 'etx' => 'text/x-setext',
+ 'flac' => 'audio/flac',
+ 'flv' => 'video/x-flv',
+ 'gif' => 'image/gif',
+ 'gz' => 'application/gzip',
+ 'htm' => 'text/html',
+ 'html' => 'text/html',
+ 'ico' => 'image/x-icon',
+ 'ics' => 'text/calendar',
+ 'ini' => 'text/plain',
+ 'iso' => 'application/x-iso9660-image',
+ 'jar' => 'application/java-archive',
+ 'jpe' => 'image/jpeg',
+ 'jpeg' => 'image/jpeg',
+ 'jpg' => 'image/jpeg',
+ 'js' => 'text/javascript',
+ 'json' => 'application/json',
+ 'latex' => 'application/x-latex',
+ 'log' => 'text/plain',
+ 'm4a' => 'audio/mp4',
+ 'm4v' => 'video/mp4',
+ 'mid' => 'audio/midi',
+ 'midi' => 'audio/midi',
+ 'mov' => 'video/quicktime',
+ 'mp3' => 'audio/mpeg',
+ 'mp4' => 'video/mp4',
+ 'mp4a' => 'audio/mp4',
+ 'mp4v' => 'video/mp4',
+ 'mpe' => 'video/mpeg',
+ 'mpeg' => 'video/mpeg',
+ 'mpg' => 'video/mpeg',
+ 'mpg4' => 'video/mp4',
+ 'oga' => 'audio/ogg',
+ 'ogg' => 'audio/ogg',
+ 'ogv' => 'video/ogg',
+ 'ogx' => 'application/ogg',
+ 'pbm' => 'image/x-portable-bitmap',
+ 'pdf' => 'application/pdf',
+ 'pgm' => 'image/x-portable-graymap',
+ 'png' => 'image/png',
+ 'pnm' => 'image/x-portable-anymap',
+ 'ppm' => 'image/x-portable-pixmap',
+ 'ppt' => 'application/vnd.ms-powerpoint',
+ 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ 'ps' => 'application/postscript',
+ 'qt' => 'video/quicktime',
+ 'rar' => 'application/x-rar-compressed',
+ 'ras' => 'image/x-cmu-raster',
+ 'rss' => 'application/rss+xml',
+ 'rtf' => 'application/rtf',
+ 'sgm' => 'text/sgml',
+ 'sgml' => 'text/sgml',
+ 'svg' => 'image/svg+xml',
+ 'swf' => 'application/x-shockwave-flash',
+ 'tar' => 'application/x-tar',
+ 'tif' => 'image/tiff',
+ 'tiff' => 'image/tiff',
+ 'torrent' => 'application/x-bittorrent',
+ 'ttf' => 'application/x-font-ttf',
+ 'txt' => 'text/plain',
+ 'wav' => 'audio/x-wav',
+ 'webm' => 'video/webm',
+ 'wma' => 'audio/x-ms-wma',
+ 'wmv' => 'video/x-ms-wmv',
+ 'woff' => 'application/x-font-woff',
+ 'wsdl' => 'application/wsdl+xml',
+ 'xbm' => 'image/x-xbitmap',
+ 'xls' => 'application/vnd.ms-excel',
+ 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ 'xml' => 'application/xml',
+ 'xpm' => 'image/x-xpixmap',
+ 'xwd' => 'image/x-xwindowdump',
+ 'yaml' => 'text/yaml',
+ 'yml' => 'text/yaml',
+ 'zip' => 'application/zip',
+
+ // Non-Apache standard
+ 'pkpass' => 'application/vnd.apple.pkpass',
+ 'msg' => 'application/vnd.ms-outlook',
+ ];
+
+ $extension = strtolower($extension);
+
+ return isset($mimetypes[$extension])
+ ? $mimetypes[$extension]
+ : null;
+ }
+}
diff --git a/inc/mailgun/php-http/multipart-stream-builder/src/CustomMimetypeHelper.php b/inc/mailgun/php-http/multipart-stream-builder/src/CustomMimetypeHelper.php
new file mode 100644
index 0000000..9f3f0d9
--- /dev/null
+++ b/inc/mailgun/php-http/multipart-stream-builder/src/CustomMimetypeHelper.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Http\Message\MultipartStream;
+
+/**
+ * Let you add your own mimetypes. The mimetype lookup will fallback on the ApacheMimeTypeHelper.
+ *
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+class CustomMimetypeHelper extends ApacheMimetypeHelper
+{
+ /**
+ * @var array
+ */
+ private $mimetypes = [];
+
+ /**
+ * @param array $mimetypes should be of type extension => mimetype
+ */
+ public function __construct(array $mimetypes = [])
+ {
+ $this->mimetypes = $mimetypes;
+ }
+
+ /**
+ * @param string $extension
+ * @param string $mimetype
+ *
+ * @return $this
+ */
+ public function addMimetype($extension, $mimetype)
+ {
+ $this->mimetypes[$extension] = $mimetype;
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * Check if we have any defined mimetypes and of not fallback to ApacheMimetypeHelper
+ */
+ public function getMimetypeFromExtension($extension)
+ {
+ $extension = strtolower($extension);
+
+ return isset($this->mimetypes[$extension])
+ ? $this->mimetypes[$extension]
+ : parent::getMimetypeFromExtension($extension);
+ }
+}
diff --git a/inc/mailgun/php-http/multipart-stream-builder/src/MimetypeHelper.php b/inc/mailgun/php-http/multipart-stream-builder/src/MimetypeHelper.php
new file mode 100644
index 0000000..fa7cf18
--- /dev/null
+++ b/inc/mailgun/php-http/multipart-stream-builder/src/MimetypeHelper.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Http\Message\MultipartStream;
+
+/**
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+interface MimetypeHelper
+{
+ /**
+ * Determines the mimetype of a file by looking at its extension.
+ *
+ * @param string $filename
+ *
+ * @return null|string
+ */
+ public function getMimetypeFromFilename($filename);
+
+ /**
+ * Maps a file extensions to a mimetype.
+ *
+ * @param string $extension The file extension
+ *
+ * @return string|null
+ */
+ public function getMimetypeFromExtension($extension);
+}
diff --git a/inc/mailgun/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php b/inc/mailgun/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php
new file mode 100644
index 0000000..f8170b8
--- /dev/null
+++ b/inc/mailgun/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php
@@ -0,0 +1,279 @@
+<?php
+
+namespace Http\Message\MultipartStream;
+
+use Http\Discovery\StreamFactoryDiscovery;
+use Http\Message\StreamFactory;
+use Psr\Http\Message\StreamInterface;
+
+/**
+ * Build your own Multipart stream. A Multipart stream is a collection of streams separated with a $bounary. This
+ * class helps you to create a Multipart stream with stream implementations from any PSR7 library.
+ *
+ * @author Michael Dowling and contributors to guzzlehttp/psr7
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+class MultipartStreamBuilder
+{
+ /**
+ * @var StreamFactory
+ */
+ private $streamFactory;
+
+ /**
+ * @var MimetypeHelper
+ */
+ private $mimetypeHelper;
+
+ /**
+ * @var string
+ */
+ private $boundary;
+
+ /**
+ * @var array Element where each Element is an array with keys ['contents', 'headers', 'filename']
+ */
+ private $data = [];
+
+ /**
+ * @param StreamFactory|null $streamFactory
+ */
+ public function __construct(StreamFactory $streamFactory = null)
+ {
+ $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
+ }
+
+ /**
+ * Add a resource to the Multipart Stream. If the same $name is used twice the first resource will
+ * be overwritten.
+ *
+ * @param string $name the formpost name
+ * @param string|resource|StreamInterface $resource
+ * @param array $options {
+ *
+ * @var array $headers additional headers ['header-name' => 'header-value']
+ * @var string $filename
+ * }
+ *
+ * @return MultipartStreamBuilder
+ */
+ public function addResource($name, $resource, array $options = [])
+ {
+ $stream = $this->streamFactory->createStream($resource);
+
+ // validate options['headers'] exists
+ if (!isset($options['headers'])) {
+ $options['headers'] = [];
+ }
+
+ // Try to add filename if it is missing
+ if (empty($options['filename'])) {
+ $options['filename'] = null;
+ $uri = $stream->getMetadata('uri');
+ if (substr($uri, 0, 6) !== 'php://') {
+ $options['filename'] = $uri;
+ }
+ }
+
+ $this->prepareHeaders($name, $stream, $options['filename'], $options['headers']);
+ $this->data[$name] = ['contents' => $stream, 'headers' => $options['headers'], 'filename' => $options['filename']];
+
+ return $this;
+ }
+
+ /**
+ * Build the stream.
+ *
+ * @return StreamInterface
+ */
+ public function build()
+ {
+ $streams = '';
+ foreach ($this->data as $data) {
+ // Add start and headers
+ $streams .= "--{$this->getBoundary()}\r\n".
+ $this->getHeaders($data['headers'])."\r\n";
+
+ // Convert the stream to string
+ /* @var $contentStream StreamInterface */
+ $contentStream = $data['contents'];
+ if ($contentStream->isSeekable()) {
+ $streams .= $contentStream->__toString();
+ } else {
+ $streams .= $contentStream->getContents();
+ }
+
+ $streams .= "\r\n";
+ }
+
+ // Append end
+ $streams .= "--{$this->getBoundary()}--\r\n";
+
+ return $this->streamFactory->createStream($streams);
+ }
+
+ /**
+ * Add extra headers if they are missing.
+ *
+ * @param string $name
+ * @param StreamInterface $stream
+ * @param string $filename
+ * @param array &$headers
+ */
+ private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers)
+ {
+ $hasFilename = $filename === '0' || $filename;
+
+ // Set a default content-disposition header if one was not provided
+ if (!$this->hasHeader($headers, 'content-disposition')) {
+ $headers['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
+ if ($hasFilename) {
+ $headers['Content-Disposition'] .= sprintf('; filename="%s"', $this->basename($filename));
+ }
+ }
+
+ // Set a default content-length header if one was not provided
+ if (!$this->hasHeader($headers, 'content-length')) {
+ if ($length = $stream->getSize()) {
+ $headers['Content-Length'] = (string) $length;
+ }
+ }
+
+ // Set a default Content-Type if one was not provided
+ if (!$this->hasHeader($headers, 'content-type') && $hasFilename) {
+ if ($type = $this->getMimetypeHelper()->getMimetypeFromFilename($filename)) {
+ $headers['Content-Type'] = $type;
+ }
+ }
+ }
+
+ /**
+ * Get the headers formatted for the HTTP message.
+ *
+ * @param array $headers
+ *
+ * @return string
+ */
+ private function getHeaders(array $headers)
+ {
+ $str = '';
+ foreach ($headers as $key => $value) {
+ $str .= sprintf("%s: %s\r\n", $key, $value);
+ }
+
+ return $str;
+ }
+
+ /**
+ * Check if header exist.
+ *
+ * @param array $headers
+ * @param string $key case insensitive
+ *
+ * @return bool
+ */
+ private function hasHeader(array $headers, $key)
+ {
+ $lowercaseHeader = strtolower($key);
+ foreach ($headers as $k => $v) {
+ if (strtolower($k) === $lowercaseHeader) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Get the boundary that separates the streams.
+ *
+ * @return string
+ */
+ public function getBoundary()
+ {
+ if ($this->boundary === null) {
+ $this->boundary = uniqid('', true);
+ }
+
+ return $this->boundary;
+ }
+
+ /**
+ * @param string $boundary
+ *
+ * @return MultipartStreamBuilder
+ */
+ public function setBoundary($boundary)
+ {
+ $this->boundary = $boundary;
+
+ return $this;
+ }
+
+ /**
+ * @return MimetypeHelper
+ */
+ private function getMimetypeHelper()
+ {
+ if ($this->mimetypeHelper === null) {
+ $this->mimetypeHelper = new ApacheMimetypeHelper();
+ }
+
+ return $this->mimetypeHelper;
+ }
+
+ /**
+ * If you have custom file extension you may overwrite the default MimetypeHelper with your own.
+ *
+ * @param MimetypeHelper $mimetypeHelper
+ *
+ * @return MultipartStreamBuilder
+ */
+ public function setMimetypeHelper(MimetypeHelper $mimetypeHelper)
+ {
+ $this->mimetypeHelper = $mimetypeHelper;
+
+ return $this;
+ }
+
+ /**
+ * Reset and clear all stored data. This allows you to use builder for a subsequent request.
+ *
+ * @return MultipartStreamBuilder
+ */
+ public function reset()
+ {
+ $this->data = [];
+ $this->boundary = null;
+
+ return $this;
+ }
+
+ /**
+ * Gets the filename from a given path.
+ *
+ * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
+ *
+ * @author Drupal 8.2
+ *
+ * @param string $path
+ *
+ * @return string
+ */
+ private function basename($path)
+ {
+ $separators = '/';
+ if (DIRECTORY_SEPARATOR != '/') {
+ // For Windows OS add special separator.
+ $separators .= DIRECTORY_SEPARATOR;
+ }
+
+ // Remove right-most slashes when $path points to directory.
+ $path = rtrim($path, $separators);
+
+ // Returns the trailing part of the $path starting after one of the directory separators.
+ $filename = preg_match('@[^'.preg_quote($separators, '@').']+$@', $path, $matches) ? $matches[0] : '';
+
+ return $filename;
+ }
+}