1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: gilbert |
5
|
|
|
* Date: 21/10/16 |
6
|
|
|
* Time: 14:05 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MtMail\ComposerPlugin; |
10
|
|
|
|
11
|
|
|
use MtMail\Event\ComposerEvent; |
12
|
|
|
use Zend\EventManager\AbstractListenerAggregate; |
13
|
|
|
use Zend\EventManager\EventManagerInterface; |
14
|
|
|
use Zend\Mime\Part as MimePart; |
15
|
|
|
use Zend\Mime\Mime; |
16
|
|
|
use Zend\Mime\Message as MimeMessage; |
17
|
|
|
use Zend\Mime\Part; |
18
|
|
|
|
19
|
|
|
class EmbeddingImages extends AbstractListenerAggregate implements PluginInterface |
20
|
|
|
{ |
21
|
|
|
public function addImages(ComposerEvent $event) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$parts = $event->getBody()->getParts(); |
24
|
|
|
|
25
|
|
|
$htmlBody = null; |
26
|
|
|
$htmlPart = null; |
|
|
|
|
27
|
|
|
$textBody = null; |
28
|
|
|
|
29
|
|
|
// locate HTML body |
30
|
|
|
foreach ($parts as $part) { |
31
|
|
|
foreach ($part->getHeadersArray() as $header) { |
32
|
|
|
if ($header[0] == 'Content-Type' && strpos($header[1], 'text/html') === 0) { |
33
|
|
|
$htmlPart = $part; |
|
|
|
|
34
|
|
|
$htmlBody = $part->getRawContent(); |
35
|
|
|
} elseif ($header[0] == 'Content-Type' && strpos($header[1], 'text/plain') === 0) { |
36
|
|
|
$textBody = $part->getRawContent(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!$htmlBody) { |
42
|
|
|
// can only work if HTML body exists |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$doc = new \DOMDocument(); |
47
|
|
|
$doc->loadHTML($htmlBody); |
48
|
|
|
$elements = $doc->getElementsByTagName('img'); |
49
|
|
|
$elements = (array) $elements; |
50
|
|
|
|
51
|
|
|
if (count($elements ) > 0) { |
52
|
|
|
$attachement = []; |
53
|
|
|
foreach ($elements as $key => $element) { |
54
|
|
|
// traitement du nom de fichiers pour apprécier le contexte |
55
|
|
|
$file = $element->getAttribute("src"); |
56
|
|
|
$filename = $file; |
57
|
|
|
$pos = strpos($file , "~"); |
|
|
|
|
58
|
|
|
if ($pos !== false) { |
59
|
|
|
$finUser = strpos($file , "/", $pos); |
|
|
|
|
60
|
|
|
$filename = $_SERVER["CONTEXT_DOCUMENT_ROOT"].substr($file, $finUser); |
61
|
|
|
} else { |
62
|
|
|
$pos = strpos($file, $_SERVER['HTTP_HOST']); |
63
|
|
|
if ($pos !== false) { |
64
|
|
|
$filename = $_SERVER["DOCUMENT_ROOT"].substr($file, $pos + strlen($_SERVER['HTTP_HOST'])); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
if (is_readable($filename)) { |
68
|
|
|
$at = new MimePart(file_get_contents($filename)); |
69
|
|
|
$at->type = $this->mimeByExtension($filename); |
70
|
|
|
$at->disposition = Mime::DISPOSITION_INLINE; |
71
|
|
|
$at->encoding = Mime::ENCODING_BASE64; |
72
|
|
|
$at->id = 'cid_' . md5_file($filename); |
73
|
|
|
$htmlBody = str_replace($file, 'cid:' . $at->id, $htmlBody); |
74
|
|
|
$attachement[] = $at; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (sizeof($attachement) > 0) { |
79
|
|
|
$textPart = new MimePart($textBody); |
80
|
|
|
$textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
81
|
|
|
$textPart->type = "text/plain; charset=UTF-8"; |
82
|
|
|
|
83
|
|
|
$htmlPart = new MimePart($htmlBody); |
84
|
|
|
$htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
85
|
|
|
$htmlPart->type = "text/html; charset=UTF-8"; |
86
|
|
|
|
87
|
|
|
$content = new MimeMessage(); |
88
|
|
|
$content->addPart($textPart); |
89
|
|
|
$content->addPart($htmlPart); |
90
|
|
|
|
91
|
|
|
$contentPart = new Part($content->generateMessage()); |
92
|
|
|
$contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; |
93
|
|
|
|
94
|
|
|
$event->getBody()->setParts([$contentPart]); |
95
|
|
|
|
96
|
|
|
foreach ($attachement as $at) { |
97
|
|
|
$event->getBody()->addPart($at); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// force multipart/alternative content type |
101
|
|
|
$event->getMessage()->getHeaders()->get('content-type')->setType('multipart/related') |
102
|
|
|
->addParameter('boundary', $event->getBody()->getMime()->boundary()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function mimeByExtension($filename) |
108
|
|
|
{ |
109
|
|
|
if (is_readable($filename) ) { |
|
|
|
|
110
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
111
|
|
|
switch ($extension) { |
112
|
|
|
case 'gif': |
113
|
|
|
$type = 'image/gif'; |
114
|
|
|
break; |
115
|
|
|
case 'jpg': |
116
|
|
|
case 'jpeg': |
117
|
|
|
$type = 'image/jpg'; |
118
|
|
|
break; |
119
|
|
|
case 'png': |
120
|
|
|
$type = 'image/png'; |
121
|
|
|
break; |
122
|
|
|
default: |
123
|
|
|
$type = 'application/octet-stream'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $type; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function attach(EventManagerInterface $events, $priority = 2) |
131
|
|
|
{ |
132
|
|
|
$this->listeners[] = $events->attach(ComposerEvent::EVENT_COMPOSE_POST, [$this, 'addImages']); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: