Skip to content

Commit

Permalink
fixing 1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Mar 8, 2022
1 parent 2713496 commit 0e63739
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 64 deletions.
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
}
],
"require": {
"php": "^7.2|^8.0",
"ext-dom": "*",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0",
"illuminate/mail": "^6.0|^7.0|^8.0",
"php": "^7.2|^8.0",
"ext-dom": "*",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0",
"illuminate/mail": "^6.0|^7.0|^8.0",
"squizlabs/php_codesniffer": "^3.5",
"masterminds/html5": "^2.0"
"masterminds/html5": "^2.0",
"ext-curl": "*",
"ext-fileinfo": "*"
},
"require-dev": {
"php": ">=7.0|^8.0",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^8.0|^9.0"
},
Expand Down
26 changes: 15 additions & 11 deletions src/Embedder/AttachmentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Eduardokum\LaravelMailAutoEmbed\Embedder;

use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity;
use Illuminate\Support\Str;
use Swift_EmbeddedFile;
use Swift_Image;
use Swift_Message;
Expand Down Expand Up @@ -41,6 +40,11 @@ public function fromUrl($url)
return $url;
}

/**
* @param $path
*
* @return string
*/
public function fromPath($path)
{
if (file_exists($path)) {
Expand All @@ -66,15 +70,6 @@ public function fromEntity(EmbeddableEntity $entity)
);
}

/**
* @param Swift_EmbeddedFile $attachment
* @return string
*/
protected function embed(Swift_EmbeddedFile $attachment)
{
return $this->message->embed($attachment);
}

/**
* @param string $url
*/
Expand All @@ -84,7 +79,6 @@ public function fromRemoteUrl($url)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$raw = curl_exec($ch);
Expand All @@ -104,5 +98,15 @@ public function fromRemoteUrl($url)
);
}
}
return $url;
}

/**
* @param Swift_EmbeddedFile $attachment
* @return string
*/
protected function embed(Swift_EmbeddedFile $attachment)
{
return $this->message->embed($attachment);
}
}
29 changes: 16 additions & 13 deletions src/Embedder/Base64Embedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Eduardokum\LaravelMailAutoEmbed\Embedder;

use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity;
use Illuminate\Support\Str;
use Swift_EmbeddedFile;
use Swift_Image;

class Base64Embedder extends Embedder
{
Expand All @@ -27,6 +24,11 @@ public function fromUrl($url)
return $url;
}

/**
* @param $path
*
* @return string
*/
public function fromPath($path)
{
if (file_exists($path)) {
Expand All @@ -45,15 +47,6 @@ public function fromEntity(EmbeddableEntity $entity)
return $this->base64String($entity->getMimeType(), $entity->getRawContent());
}

/**
* @param string $mimeType
* @param mixed $content
*/
private function base64String($mimeType, $content)
{
return 'data:'.$mimeType.';base64,'.base64_encode($content);
}

/**
* @param string $url
*/
Expand All @@ -63,7 +56,6 @@ public function fromRemoteUrl($url)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$raw = curl_exec($ch);
Expand All @@ -75,5 +67,16 @@ public function fromRemoteUrl($url)
return $this->base64String($contentType, $raw);
}
}

return $url;
}

/**
* @param string $mimeType
* @param mixed $content
*/
private function base64String($mimeType, $content)
{
return 'data:'.$mimeType.';base64,'.base64_encode($content);
}
}
50 changes: 21 additions & 29 deletions src/Listeners/SwiftEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Eduardokum\LaravelMailAutoEmbed\Embedder\Base64Embedder;
use Eduardokum\LaravelMailAutoEmbed\Embedder\Embedder;
use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity;
use Illuminate\Support\Facades\App;
use Masterminds\HTML5;
use ReflectionClass;
use Swift_Events_SendEvent;
Expand Down Expand Up @@ -55,7 +54,7 @@ public function sendPerformed(Swift_Events_SendEvent $evt)
}

/**
* Attaches images by parsing the HTML document.
*
*/
private function attachImages()
{
Expand All @@ -80,13 +79,19 @@ private function attachImages()

// Replace body
$this->message->setBody($parser->saveHTML($document));

// $html_body = $this->message->getBody();
//
/* $html_body = preg_replace_callback('/<img.*src="(.*?)"\s?(.*)?>/', [$this, 'replaceCallback'], $html_body);*/
//
// $this->message->setBody($html_body);
}

/**
* @param DOMDocument $document
* @return bool
*/
private function shouldSkipDocument($document)
private function shouldSkipDocument(DOMDocument $document)
{
if ($document->childNodes->count() != 1) {
return false;
Expand All @@ -101,9 +106,8 @@ private function shouldSkipDocument($document)

/**
* @param DOMDocument $document
* @return string
*/
private function attachImagesToDom(&$document)
private function attachImagesToDom(DOMDocument &$document)
{
foreach ($document->getElementsByTagName('img') as $image) {
\assert($image instanceof DOMElement);
Expand All @@ -130,7 +134,7 @@ private function attachImagesToDom(&$document)
* @param DOMElement $imageTag
* @return bool
*/
private function needsEmbed($imageTag)
private function needsEmbed(DOMElement $imageTag)
{
// Don't embed if 'data-skip-embed' is present
if ($imageTag->hasAttribute('data-skip-embed')) {
Expand All @@ -146,22 +150,23 @@ private function needsEmbed($imageTag)
}

/**
* @param DOMElement $imageTag
* @param DOMElement $imageTag
* @return Embedder
*/
private function getEmbedder($imageTag)
private function getEmbedder(DOMElement $imageTag)
{
$method = $imageTag->getAttribute('data-auto-embed');
if (empty($method)) {
$method = $this->config['method'];
}

$embedderName = "mail-auto-embed.{$method}";
if (! App::bound($embedderName)) {
$embedderName = 'mail-auto-embed.attachment';
switch ($method) {
case 'attachment':
default:
return new AttachmentEmbedder($this->message);
case 'base64':
return new Base64Embedder();
}

return App::make($embedderName, [$this->message]);
}

/**
Expand All @@ -173,6 +178,7 @@ private function embed(Embedder $embedder, $src)
{
// Entity embedding
if (strpos($src, 'embed:') === 0) {

$embedParams = explode(':', $src);
if (count($embedParams) < 3) {
return $src;
Expand All @@ -181,12 +187,12 @@ private function embed(Embedder $embedder, $src)
$className = urldecode($embedParams[1]);
$id = $embedParams[2];

if (! class_exists($className)) {
if (!class_exists($className)) {
return $src;
}

$class = new ReflectionClass($className);
if (! $class->implementsInterface(EmbeddableEntity::class)) {
if (! $class->implementsInterface(EmbeddableEntity::class) ) {
return $src;
}

Expand All @@ -203,20 +209,6 @@ private function embed(Embedder $embedder, $src)
return $embedder->fromUrl($src);
}

// Path embedding
$publicPath = public_path($src);
$appPath = app_path($src);
$storagePath = storage_path($src);
if (file_exists($src)) {
return $embedder->fromPath($src);
} elseif (file_exists($publicPath)) { // Try to guess where the file is at that priority level
return $embedder->fromPath($publicPath);
} elseif (file_exists($appPath)) {
return $embedder->fromPath($appPath);
} elseif (file_exists($storagePath)) {
return $embedder->fromPath($storagePath);
}

return $src;
}
}
4 changes: 0 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public function boot()
public function register()
{
$this->mergeConfigFrom($this->getConfigPath(), 'mail-auto-embed');

// Register default embedder
$this->app->bind('mail-auto-embed.attachment', AttachmentEmbedder::class);
$this->app->bind('mail-auto-embed.base64', Base64Embedder::class);
}

/**
Expand Down

0 comments on commit 0e63739

Please sign in to comment.