Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_htmlmail
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_htmlmail.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * HTML mail class
29 *
30 * $Id$
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 193: class t3lib_htmlmail
40 * 261: function t3lib_htmlmail ()
41 * 268: function start ()
42 * 305: function useQuotedPrintable()
43 * 315: function useBase64()
44 * 326: function use8Bit()
45 * 338: function encodeMsg($content)
46 * 348: function addPlain ($content)
47 * 360: function addAttachment($file)
48 * 378: function addHTML ($file)
49 * 401: function extractHtmlInit($html,$url)
50 * 412: function send($recipient)
51 *
52 * SECTION: Main functions
53 * 441: function setHeaders()
54 * 500: function setRecipient ($recip)
55 * 518: function getHTMLContentType()
56 * 527: function setContent()
57 * 554: function constructMixed ($boundary)
58 * 593: function constructHTML ($boundary)
59 * 617: function constructAlternative($boundary)
60 * 638: function constructHTML_media ($boundary)
61 * 691: function sendTheMail ()
62 * 757: function getBoundary()
63 * 769: function setPlain ($content)
64 * 780: function setHtml ($content)
65 * 791: function add_header($header)
66 * 812: function add_message($string)
67 * 823: function getContent($type)
68 * 832: function preview()
69 *
70 * SECTION: Functions for acquiring attachments, HTML, analyzing and so on **
71 * 860: function fetchHTML($file)
72 * 878: function fetchHTMLMedia()
73 * 899: function extractMediaLinks()
74 * 976: function extractHyperLinks()
75 * 1025: function extractFramesInfo()
76 * 1051: function substMediaNamesInHTML($absolute)
77 * 1078: function substHREFsInHTML()
78 * 1106: function substHTTPurlsInPlainText($content)
79 * 1142: function fixRollOvers()
80 *
81 * SECTION: File and URL-functions
82 * 1189: function makeBase64($inputstr)
83 * 1200: function getExtendedURL($url)
84 * 1222: function addUserPass($url)
85 * 1238: function getURL($url)
86 * 1250: function getStrippedURL($url)
87 * 1271: function getMimeType($url)
88 * 1300: function absRef($ref)
89 * 1320: function split_fileref($fileref)
90 * 1347: function extParseUrl($path)
91 * 1362: function tag_regex($tagArray)
92 * 1384: function get_tag_attributes($tag)
93 * 1426: function quoted_printable($string)
94 * 1437: function convertName($name)
95 *
96 * TOTAL FUNCTIONS: 49
97 * (This index is automatically created/updated by the extension "extdeveval")
98 *
99 */
100 /**
101 * NOTES on MIME mail structures:
102 *
103 * Plain + HTML
104 * multipart/alternative (text, html)
105 * multipart/alternative (text, html)
106 *
107 * Plain + HTML + image
108 * multipart/related (m/a, cids)
109 * multipart/alternative (text, html)
110 *
111 * multipart/related (m/a, cids)
112 * multipart/alternative (text, html)
113 *
114 * plain + attachment
115 * multipart/mixed
116 *
117 * HTML + Attachment:
118 * multipart/mixed (text/html , attachments)
119 *
120 * Plain + HTML + Attachments:
121 * multipart/mixed (m/a, attachments)
122 * multipart/alternative (text, html)
123 *
124 * Plain + HTML + image + attachment
125 *
126 * Outlook expr.
127 * multipart/mixed (m/r, attachments)
128 * multipart/related (m/a, cids)
129 * multipart/alternative (text, html)
130 *
131 *
132 *
133 * FROM RFC 1521:
134 *
135 * 5.1 Quoted-Printable Content-Transfer-Encoding
136 * The Quoted-Printable encoding is intended to represent data that largely consists of octets that correspond to printable characters in the ASCII character set. It encodes the data in such a way that the resulting octets are unlikely to be modified by mail transport. If the data being encoded are mostly ASCII text, the encoded form of the data remains largely recognizable by humans. A body which is entirely ASCII may also be encoded in Quoted-Printable to ensure the integrity of the data should the message pass through a character- translating, and/or line-wrapping gateway.
137 *
138 * In this encoding, octets are to be represented as determined by the following rules:
139 * Rule #1: (General 8-bit representation) Any octet, except those indicating a line break according to the newline convention of the canonical (standard) form of the data being encoded, may be represented by an "=" followed by a two digit hexadecimal representation of the octet's value. The digits of the hexadecimal alphabet, for this purpose, are "0123456789ABCDEF". Uppercase letters must be used when sending hexadecimal data, though a robust implementation may choose to recognize lowercase letters on receipt. Thus, for example, the value 12 (ASCII form feed) can be represented by "=0C", and the value 61 (ASCII EQUAL SIGN) can be represented by "=3D". Except when the following rules allow an alternative encoding, this rule is mandatory.
140 * Rule #2: (Literal representation) Octets with decimal values of 33 through 60 inclusive, and 62 through 126, inclusive, MAY be represented as the ASCII characters which correspond to those octets (EXCLAMATION POINT through LESS THAN, and GREATER THAN through TILDE, respectively).
141 * Rule #3: (White Space): Octets with values of 9 and 32 MAY be represented as ASCII TAB (HT) and SPACE characters, respectively, but MUST NOT be so represented at the end of an encoded line. Any TAB (HT) or SPACE characters on an encoded line MUST thus be followed on that line by a printable character. In particular, an
142 * "=" at the end of an encoded line, indicating a soft line break (see rule #5) may follow one or more TAB (HT) or SPACE characters. It follows that an octet with value 9 or 32 appearing at the end of an encoded line must be represented according to Rule #1. This rule is necessary because some MTAs (Message Transport Agents, programs which transport messages from one user to another, or perform a part of such transfers) are known to pad lines of text with SPACEs, and others are known to remove "white space" characters from the end of a line. Therefore, when decoding a Quoted-Printable body, any trailing white space on a line must be deleted, as it will necessarily have been added by intermediate transport agents.
143 * Rule #4 (Line Breaks): A line break in a text body, independent of what its representation is following the canonical representation of the data being encoded, must be represented by a (RFC 822) line break, which is a CRLF sequence, in the Quoted-Printable encoding. Since the canonical representation of types other than text do not generally include the representation of line breaks, no hard line breaks (i.e. line breaks that are intended to be meaningful and to be displayed to the user) should occur in the quoted-printable encoding of such types. Of course, occurrences of "=0D", "=0A", "0A=0D" and "=0D=0A" will eventually be encountered. In general, however, base64 is preferred over quoted-printable for binary data.
144 * Note that many implementations may elect to encode the local representation of various content types directly, as described in Appendix G. In particular, this may apply to plain text material on systems that use newline conventions other than CRLF delimiters. Such an implementation is permissible, but the generation of line breaks must be generalized to account for the case where alternate representations of newline sequences are used.
145 * Rule #5 (Soft Line Breaks): The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76 characters long. If longer lines are to be encoded with the Quoted-Printable encoding, 'soft' line breaks must be used. An equal sign as the last character on a encoded line indicates such a non-significant ('soft') line break in the encoded text. Thus if the "raw" form of the line is a single unencoded line that says:
146 * Now's the time for all folk to come to the aid of their country.
147 *
148 * This can be represented, in the Quoted-Printable encoding, as
149 *
150 * Now's the time =
151 * for all folk to come=
152 * to the aid of their country.
153 *
154 * This provides a mechanism with which long lines are encoded in such a way as to be restored by the user agent. The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
155 * Since the hyphen character ("-") is represented as itself in the Quoted-Printable encoding, care must be taken, when encapsulating a quoted-printable encoded body in a multipart entity, to ensure that the encapsulation boundary does not appear anywhere in the encoded body. (A good strategy is to choose a boundary that includes a character sequence such as "=_" which can never appear in a quoted- printable body. See the definition of multipart messages later in this document.)
156 * NOTE: The quoted-printable encoding represents something of a compromise between readability and reliability in transport. Bodies encoded with the quoted-printable encoding will work reliably over most mail gateways, but may not work perfectly over a few gateways, notably those involving translation into EBCDIC. (In theory, an EBCDIC gateway could decode a quoted-printable body and re-encode it using base64, but such gateways do not yet exist.) A higher level of confidence is offered by the base64 Content-Transfer-Encoding. A way to get reasonably reliable transport through EBCDIC gateways is to also quote the ASCII characters
157 * !"#$@[\]^`{|}~
158 * according to rule #1. See Appendix B for more information.
159 * Because quoted-printable data is generally assumed to be line- oriented, it is to be expected that the representation of the breaks between the lines of quoted printable data may be altered in transport, in the same manner that plain text mail has always been altered in Internet mail when passing between systems with differing newline conventions. If such alterations are likely to constitute a corruption of the data, it is probably more sensible to use the base64 encoding rather than the quoted-printable encoding.
160 * WARNING TO IMPLEMENTORS: If binary data are encoded in quoted- printable, care must be taken to encode CR and LF characters as "=0D" and "=0A", respectively. In particular, a CRLF sequence in binary data should be encoded as "=0D=0A". Otherwise, if CRLF were represented as a hard line break, it might be incorrectly decoded on
161 * platforms with different line break conventions.
162 * For formalists, the syntax of quoted-printable data is described by the following grammar:
163 *
164 * quoted-printable := ([*(ptext / SPACE / TAB) ptext] ["="] CRLF)
165 * ; Maximum line length of 76 characters excluding CRLF
166 *
167 * ptext := octet /<any ASCII character except "=", SPACE, or TAB>
168 * ; characters not listed as "mail-safe" in Appendix B
169 * ; are also not recommended.
170 *
171 * octet := "=" 2(DIGIT / "A" / "B" / "C" / "D" / "E" / "F")
172 * ; octet must be used for characters > 127, =, SPACE, or TAB,
173 * ; and is recommended for any characters not listed in
174 * ; Appendix B as "mail-safe".
175 */
176 /**
177 * HTML mail class
178 *
179 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
180 * @package TYPO3
181 * @subpackage t3lib
182 */
183 class t3lib_htmlmail
184 {
185 // Headerinfo:
186 var $recipient = '';
187 var $recipient_copy = ''; // This recipient (or list of...) will also receive the mail. Regard it as a copy.
188 var $recipient_blindcopy = ''; // This recipient (or list of...) will also receive the mail as a blind copy. Regard it as a copy.
189 var $subject = '';
190 var $from_email = '';
191 var $from_name = '';
192 var $replyto_email = '';
193 var $replyto_name = '';
194 var $organisation = '';
195 var $priority = 3; // 1 = highest, 5 = lowest, 3 = normal
196 var $mailer = ''; // X-mailer, set to TYPO3 Major.Minor in constructor
197 var $alt_base64 = 0;
198 var $alt_8bit = 0;
199 var $jumperURL_prefix = ''; // This is a prefix that will be added to all links in the mail. Example: 'http://www.mydomain.com/jump?userid=###FIELD_uid###&url='. if used, anything after url= is urlencoded.
200 var $jumperURL_useId = 0; // If set, then the array-key of the urls are inserted instead of the url itself. Smart in order to reduce link-length
201 var $mediaList = ''; // If set, this is a list of the media-files (index-keys to the array) that should be represented in the html-mail
202 var $http_password = '';
203 var $http_username = '';
204 var $postfix_version1 = FALSE;
205
206 // Internal
207 /*
208 This is how the $theParts-array is normally looking
209 var $theParts = array(
210 'plain' => array(
211 'content' => ''
212 ),
213 'html' => array(
214 'content' => '',
215 'path' => '',
216 'media' => array(),
217 'hrefs' => array()
218 ),
219 'attach' => array()
220 );
221 */
222 var $theParts = array();
223
224 var $messageid = '';
225 var $returnPath = '';
226 var $Xid = '';
227 var $dontEncodeHeader = FALSE; // If set, the header will not be encoded
228
229 var $headers = '';
230 var $message = '';
231 var $part = 0;
232 var $image_fullpath_list = '';
233 var $href_fullpath_list = '';
234
235 var $plain_text_header = '';
236 var $html_text_header = '';
237 var $charset = '';
238 var $defaultCharset = 'iso-8859-1';
239
240
241 /**
242 * Constructor. If the configuration variable forceReturnPath is set,
243 * calls to mail will be called with a 5th parameter.
244 * See function sendTheMail for more info
245 *
246 * @return void
247 */
248 public function t3lib_htmlmail()
249 {
250 $this->forceReturnPath = $GLOBALS['TYPO3_CONF_VARS']['SYS']['forceReturnPath'];
251
252 $this->mailer = 'TYPO3';
253 }
254
255
256 /**
257 * start action that sets the message ID and the charset
258 *
259 * @return void
260 */
261 public function start()
262 {
263 global $TYPO3_CONF_VARS;
264
265 // Sets the message id
266 $host = t3lib_div::getHostname();
267 if (!$host || $host == '127.0.0.1' || $host == 'localhost' || $host == 'localhost.localdomain') {
268 $host = ($TYPO3_CONF_VARS['SYS']['sitename'] ? preg_replace('/[^A-Za-z0-9_\-]/', '_', $TYPO3_CONF_VARS['SYS']['sitename']) : 'localhost') . '.TYPO3';
269 }
270 $this->messageid = md5(microtime()) . '@' . $host;
271
272
273 // Default line break for Unix systems.
274 $this->linebreak = LF;
275 // Line break for Windows. This is needed because PHP on Windows systems
276 // send mails via SMTP instead of using sendmail, and thus the linebreak needs to be \r\n.
277 if (TYPO3_OS == 'WIN') {
278 $this->linebreak = CRLF;
279 }
280
281 // Sets the Charset
282 if (!$this->charset) {
283 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->renderCharset) {
284 $this->charset = $GLOBALS['TSFE']->renderCharset;
285 } elseif (is_object($GLOBALS['LANG']) && $GLOBALS['LANG']->charSet) {
286 $this->charset = $GLOBALS['LANG']->charSet;
287 } elseif ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) {
288 $this->charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
289 } else {
290 $this->charset = $this->defaultCharset;
291 }
292 }
293
294 // Use quoted-printable headers by default
295 $this->useQuotedPrintable();
296 }
297
298
299 /**
300 * sets the header of both Plain Text and HTML mails to quoted printable
301 *
302 * @return void
303 */
304 public function useQuotedPrintable()
305 {
306 $this->plain_text_header = 'Content-Type: text/plain; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: quoted-printable';
307 $this->html_text_header = 'Content-Type: text/html; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: quoted-printable';
308 }
309
310 /**
311 * sets the encoding headers to base64 for both the Plain Text and HTML mail
312 *
313 * @return void
314 */
315 public function useBase64()
316 {
317 $this->plain_text_header = 'Content-Type: text/plain; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: base64';
318 $this->html_text_header = 'Content-Type: text/html; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: base64';
319 $this->alt_base64 = 1;
320 }
321
322
323 /**
324 * sets the encoding to 8bit and the current charset of both the Plain Text and the HTML mail
325 *
326 * @return void
327 */
328 public function use8Bit()
329 {
330 $this->plain_text_header = 'Content-Type: text/plain; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: 8bit';
331 $this->html_text_header = 'Content-Type: text/html; charset=' . $this->charset . $this->linebreak . 'Content-Transfer-Encoding: 8bit';
332 $this->alt_8bit = 1;
333 }
334
335
336 /**
337 * Encodes the message content according to the options "alt_base64" and "alt_8bit" (no need to encode here)
338 * or to "quoted_printable" if no option is set.
339 *
340 * @param string $content the content that will be encoded
341 * @return string the encoded content
342 */
343 public function encodeMsg($content)
344 {
345 if ($this->alt_base64) {
346 return $this->makeBase64($content);
347 } elseif ($this->alt_8bit) {
348 return $content;
349 } else {
350 return t3lib_div::quoted_printable($content);
351 }
352 }
353
354
355 /**
356 * Adds plain-text, replaces the HTTP urls in the plain text and then encodes it
357 *
358 * @param string $content that will be added
359 * @return void
360 */
361 public function addPlain($content)
362 {
363 $content = $this->substHTTPurlsInPlainText($content);
364 $this->setPlain($this->encodeMsg($content));
365 }
366
367
368 /**
369 * Adds an attachment to the mail
370 *
371 * @param string $file: the filename to add
372 * @return boolean whether the attachment was added or not
373 */
374 public function addAttachment($file)
375 {
376 // Fetching the content and the mime-type
377 $fileInfo = $this->getExtendedURL($file);
378 if ($fileInfo) {
379 if (!$fileInfo['content_type']) {
380 $fileInfo['content_type'] = 'application/octet-stream';
381 }
382 $temp = $this->split_fileref($file);
383 if ($temp['file']) {
384 $fileInfo['filename'] = $temp['file'];
385 } elseif (strpos(' ' . $fileInfo['content_type'], 'htm')) {
386 $fileInfo['filename'] = 'index.html';
387 } else {
388 $fileInfo['filename'] = 'unknown';
389 }
390 $this->theParts['attach'][] = $fileInfo;
391 return TRUE;
392 }
393 return FALSE;
394 }
395
396
397 /**
398 * Adds HTML and media, encodes it from a URL or file
399 *
400 * @param string $file: the filename to add
401 * @return boolean whether the attachment was added or not
402 */
403 public function addHTML($file)
404 {
405 $status = $this->fetchHTML($file);
406 if (!$status) {
407 return FALSE;
408 }
409 if ($this->extractFramesInfo()) {
410 return 'Document was a frameset. Stopped';
411 }
412 $this->extractMediaLinks();
413 $this->extractHyperLinks();
414 $this->fetchHTMLMedia();
415 $this->substMediaNamesInHTML(0); // 0 = relative
416 $this->substHREFsInHTML();
417 $this->setHtml($this->encodeMsg($this->theParts['html']['content']));
418 }
419
420
421 /**
422 * Extract HTML-parts, used externally
423 *
424 * @param string $html: will be added to the html "content" part
425 * @param string $url: will be added to the html "path" part
426 * @return void
427 */
428 public function extractHtmlInit($html, $url)
429 {
430 $this->theParts['html']['content'] = $html;
431 $this->theParts['html']['path'] = $url;
432 }
433
434
435 /**
436 * Assembles the message by headers and content and finally send it to the provided recipient.
437 *
438 * @param string $recipient: The recipient the message should be delivered to (if blank, $this->recipient will be used instead)
439 * @return boolean Returns whether the mail was sent (successfully accepted for delivery)
440 */
441 public function send($recipient)
442 {
443 if ($recipient) {
444 $this->recipient = $recipient;
445 }
446 $this->setHeaders();
447 $this->setContent();
448 $mailWasSent = $this->sendTheMail();
449 return $mailWasSent;
450 }
451
452
453 /*****************************************
454 *
455 * Main functions
456 *
457 *****************************************/
458
459 /**
460 * Clears the header-string and sets the headers based on object-vars.
461 *
462 * @return void
463 */
464 public function setHeaders()
465 {
466 $this->headers = '';
467 // Message_id
468 $this->add_header('Message-ID: <' . $this->messageid . '>');
469 // Return path
470 if ($this->returnPath) {
471 $this->add_header('Return-Path: ' . $this->returnPath);
472 $this->add_header('Errors-To: ' . $this->returnPath);
473 }
474 // X-id
475 if ($this->Xid) {
476 $this->add_header('X-Typo3MID: ' . $this->Xid);
477 }
478
479 // From
480 if ($this->from_email) {
481 if ($this->from_name && !t3lib_div::isBrokenEmailEnvironment()) {
482 $this->add_header('From: ' . $this->from_name . ' <' . $this->from_email . '>');
483 } else {
484 $this->add_header('From: ' . $this->from_email);
485 }
486 }
487
488 // Cc
489 if ($this->recipient_copy) {
490 $this->add_header('Cc: ' . $this->recipient_copy);
491 }
492
493 // Bcc
494 if ($this->recipient_blindcopy) {
495 $this->add_header('Bcc: ' . $this->recipient_blindcopy);
496 }
497
498 // Reply
499 if ($this->replyto_email) {
500 if ($this->replyto_name) {
501 $this->add_header('Reply-To: ' . $this->replyto_name . ' <' . $this->replyto_email . '>');
502 } else {
503 $this->add_header('Reply-To: ' . $this->replyto_email);
504 }
505 }
506 // Organization, using american english spelling (organization / organisation) as defined in RFC 1036 / 2076
507 if ($this->organisation) {
508 $this->add_header('Organization: ' . $this->organisation);
509 }
510 // mailer
511 if ($this->mailer) {
512 $this->add_header('X-Mailer: ' . $this->mailer);
513 }
514 // priority
515 if ($this->priority) {
516 $this->add_header('X-Priority: ' . $this->priority);
517 }
518 $this->add_header('Mime-Version: 1.0');
519
520 if (!$this->dontEncodeHeader) {
521 $enc = $this->alt_base64 ? 'base64' : 'quoted_printable'; // Header must be ASCII, therefore only base64 or quoted_printable are allowed!
522 // Quote recipient and subject
523 $this->recipient = t3lib_div::encodeHeader($this->recipient, $enc, $this->charset);
524 $this->subject = t3lib_div::encodeHeader($this->subject, $enc, $this->charset);
525 }
526 }
527
528
529 /**
530 * Sets the recipient(s). If you supply a string, you set one recipient.
531 * If you supply an array, every value is added as a recipient.
532 *
533 * @param mixed $recipient: the recipient(s) to set
534 * @return void
535 */
536 public function setRecipient($recipient)
537 {
538 $this->recipient = (is_array($recipient) ? implode(',', $recipient) : $recipient);
539 }
540
541
542 /**
543 * Returns the content type based on whether the mail has media / attachments or no
544 *
545 * @return string the content type
546 */
547 public function getHTMLContentType()
548 {
549 return (count($this->theParts['html']['media']) ? 'multipart/related' : 'multipart/alternative');
550 }
551
552
553 /**
554 * Begins building the message-body
555 *
556 * @return void
557 */
558 public function setContent()
559 {
560 $this->message = '';
561 $boundary = $this->getBoundary();
562
563 // Setting up headers
564 if (count($this->theParts['attach'])) {
565 // Generate (plain/HTML) / attachments
566 $this->add_header('Content-Type: multipart/mixed;');
567 $this->add_header(' boundary="' . $boundary . '"');
568 $this->add_message('This is a multi-part message in MIME format.' . LF);
569 $this->constructMixed($boundary);
570 } elseif ($this->theParts['html']['content']) {
571 // Generate plain/HTML mail
572 $this->add_header('Content-Type: ' . $this->getHTMLContentType() . ';');
573 $this->add_header(' boundary="' . $boundary . '"');
574 $this->add_message('This is a multi-part message in MIME format.' . LF);
575 $this->constructHTML($boundary);
576 } else {
577 // Generate plain only
578 $this->add_header($this->plain_text_header);
579 $this->add_message($this->getContent('plain'));
580 }
581 }
582
583
584 /**
585 * This functions combines the plain / HTML content with the attachments
586 *
587 * @param string $boundary: the mail boundary
588 * @return void
589 */
590 public function constructMixed($boundary)
591 {
592 $this->add_message('--' . $boundary);
593
594 if ($this->theParts['html']['content']) {
595 // HTML and plain is added
596 $newBoundary = $this->getBoundary();
597 $this->add_message('Content-Type: ' . $this->getHTMLContentType() . ';');
598 $this->add_message(' boundary="' . $newBoundary . '"');
599 $this->add_message('');
600 $this->constructHTML($newBoundary);
601 } else {
602 // Purely plain
603 $this->add_message($this->plain_text_header);
604 $this->add_message('');
605 $this->add_message($this->getContent('plain'));
606 }
607 // attachments are added
608 if (is_array($this->theParts['attach'])) {
609 foreach ($this->theParts['attach'] as $media) {
610 $this->add_message('--' . $boundary);
611 $this->add_message('Content-Type: ' . $media['content_type'] . ';');
612 $this->add_message(' name="' . $media['filename'] . '"');
613 $this->add_message('Content-Transfer-Encoding: base64');
614 $this->add_message('Content-Disposition: attachment;');
615 $this->add_message(' filename="' . $media['filename'] . '"');
616 $this->add_message('');
617 $this->add_message($this->makeBase64($media['content']));
618 }
619 }
620 $this->add_message('--' . $boundary . '--' . LF);
621 }
622
623
624 /**
625 * this function creates the HTML part of the mail
626 *
627 * @param string $boundary: the boundary to use
628 * @return void
629 */
630 public function constructHTML($boundary)
631 {
632 // If media, then we know, the multipart/related content-type has been set before this function call
633 if (count($this->theParts['html']['media'])) {
634 $this->add_message('--' . $boundary);
635 // HTML has media
636 $newBoundary = $this->getBoundary();
637 $this->add_message('Content-Type: multipart/alternative;');
638 $this->add_message(' boundary="' . $newBoundary . '"');
639 $this->add_message('Content-Transfer-Encoding: 7bit');
640 $this->add_message('');
641
642 // Adding the plaintext/html mix, and use $newBoundary
643 $this->constructAlternative($newBoundary);
644 $this->constructHTML_media($boundary);
645 } else {
646 // if no media, just use the $boundary for adding plaintext/html mix
647 $this->constructAlternative($boundary);
648 }
649 }
650
651
652 /**
653 * Here plain is combined with HTML
654 *
655 * @param string $boundary: the boundary to use
656 * @return void
657 */
658 public function constructAlternative($boundary)
659 {
660 $this->add_message('--' . $boundary);
661
662 // plain is added
663 $this->add_message($this->plain_text_header);
664 $this->add_message('');
665 $this->add_message($this->getContent('plain'));
666 $this->add_message('--' . $boundary);
667
668 // html is added
669 $this->add_message($this->html_text_header);
670 $this->add_message('');
671 $this->add_message($this->getContent('html'));
672 $this->add_message('--' . $boundary . '--' . LF);
673 }
674
675
676 /**
677 * Constructs the HTML-part of message if the HTML contains media
678 *
679 * @param string $boundary: the boundary to use
680 * @return void
681 */
682 public function constructHTML_media($boundary)
683 {
684 // media is added
685 if (is_array($this->theParts['html']['media'])) {
686 foreach ($this->theParts['html']['media'] as $key => $media) {
687 if (!$this->mediaList || t3lib_div::inList($this->mediaList, $key)) {
688 $this->add_message('--' . $boundary);
689 $this->add_message('Content-Type: ' . $media['ctype']);
690 $this->add_message('Content-ID: <part' . $key . '.' . $this->messageid . '>');
691 $this->add_message('Content-Transfer-Encoding: base64');
692 $this->add_message('');
693 $this->add_message($this->makeBase64($media['content']));
694 }
695 }
696 }
697 $this->add_message('--' . $boundary . '--' . LF);
698 }
699
700
701 /**
702 * Sends the mail by calling the mail() function in php. On Linux systems this will invoke the MTA
703 * defined in php.ini (sendmail -t -i by default), on Windows a SMTP must be specified in the sys.ini.
704 * Most common MTA's on Linux has a Sendmail interface, including Postfix and Exim.
705 * For setting the return-path correctly, the parameter -f has to be added to the system call to sendmail.
706 * This obviously does not have any effect on Windows, but on Sendmail compliant systems this works. If safe mode
707 * is enabled, then extra parameters is not allowed, so a safe mode check is made before the mail() command is
708 * invoked. When using the -f parameter, some MTA's will put an X-AUTHENTICATION-WARNING saying that
709 * the return path was modified manually with the -f flag. To disable this warning make sure that the user running
710 * Apache is in the /etc/mail/trusted-users table.
711 *
712 * POSTFIX: With postfix version below 2.0 there is a problem that the -f parameter can not be used in conjunction
713 * with -t. Postfix will give an error in the maillog:
714 *
715 * cannot handle command-line recipients with -t
716 *
717 * The -f parameter is only enabled if the parameter forceReturnPath is enabled in the install tool.
718 *
719 * This whole problem of return-path turns out to be quite tricky. If you have a solution that works better, on all
720 * standard MTA's then we are very open for suggestions.
721 *
722 * With time this function should be made such that several ways of sending the mail is possible (local MTA, smtp other).
723 *
724 * @return boolean Returns whether the mail was sent (successfully accepted for delivery)
725 */
726 public function sendTheMail()
727 {
728 $mailWasSent = FALSE;
729
730 // Sending the mail requires the recipient and message to be set.
731 if (!trim($this->recipient) || !trim($this->message)) {
732 return FALSE;
733 }
734
735 // On windows the -f flag is not used (specific for Sendmail and Postfix),
736 // but instead the php.ini parameter sendmail_from is used.
737 $returnPath = ($this->forceReturnPath && strlen($this->returnPath) > 0) ? '-f ' . escapeshellarg($this->returnPath) : '';
738 if (TYPO3_OS == 'WIN' && $this->returnPath) {
739 @ini_set('sendmail_from', t3lib_div::normalizeMailAddress($this->returnPath));
740 }
741 $recipient = t3lib_div::normalizeMailAddress($this->recipient);
742
743 // If safe mode is on, the fifth parameter to mail is not allowed, so the fix wont work on unix with safe_mode=On
744 $returnPathPossible = (!t3lib_utility_PhpOptions::isSafeModeEnabled() && $this->forceReturnPath);
745 if ($returnPathPossible) {
746 $mailWasSent = t3lib_utility_Mail::mail($recipient,
747 $this->subject,
748 $this->message,
749 $this->headers,
750 $returnPath);
751 } else {
752 $mailWasSent = t3lib_utility_Mail::mail($recipient,
753 $this->subject,
754 $this->message,
755 $this->headers);
756 }
757
758 // Auto response
759 if ($this->auto_respond_msg) {
760 $theParts = explode('/', $this->auto_respond_msg, 2);
761 $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]);
762 $theParts[1] = str_replace("/", LF, $theParts[1]);
763 $theParts[1] = str_replace("###MESSAGE###", $this->getContent('plain'), $theParts[1]);
764 if ($returnPathPossible) {
765 $mailWasSent = t3lib_utility_Mail::mail($this->from_email,
766 $theParts[0],
767 $theParts[1],
768 'From: ' . $recipient,
769 $returnPath);
770 } else {
771 $mailWasSent = t3lib_utility_Mail::mail($this->from_email,
772 $theParts[0],
773 $theParts[1],
774 'From: ' . $recipient);
775 }
776 }
777 if ($this->returnPath) {
778 ini_restore('sendmail_from');
779 }
780 return $mailWasSent;
781 }
782
783
784 /**
785 * Returns boundaries
786 *
787 * @return string the boundary
788 */
789 public function getBoundary()
790 {
791 $this->part++;
792 return "----------" . uniqid("part_" . $this->part . "_");
793 }
794
795
796 /**
797 * Sets the plain-text part. No processing done.
798 *
799 * @param string $content: the plain content
800 * @return void
801 */
802 public function setPlain($content)
803 {
804 $this->theParts['plain']['content'] = $content;
805 }
806
807
808 /**
809 * Sets the HTML-part. No processing done.
810 *
811 * @param string $content: the HTML content
812 * @return void
813 */
814 public function setHtml($content)
815 {
816 $this->theParts['html']['content'] = $content;
817 }
818
819
820 /**
821 * Adds a header to the mail. Use this AFTER the setHeaders()-function
822 *
823 * @param string $header: the header in form of "key: value"
824 * @return void
825 */
826 public function add_header($header)
827 {
828 // Mail headers must be ASCII, therefore we convert the whole header to either base64 or quoted_printable
829 if (!$this->dontEncodeHeader && !stristr($header, 'Content-Type') && !stristr($header, 'Content-Transfer-Encoding')) {
830 // Field tags must not be encoded
831 $parts = explode(': ', $header, 2);
832 if (count($parts) == 2) {
833 $enc = $this->alt_base64 ? 'base64' : 'quoted_printable';
834 $parts[1] = t3lib_div::encodeHeader($parts[1], $enc, $this->charset);
835 $header = implode(': ', $parts);
836 }
837 }
838
839 $this->headers .= $header . LF;
840 }
841
842
843 /**
844 * Adds a line of text to the mail-body. Is normally used internally
845 *
846 * @param string $msg: the message to add
847 * @return void
848 */
849 public function add_message($msg)
850 {
851 $this->message .= $msg . LF;
852 }
853
854
855 /**
856 * returns the content specified by the type (plain, html etc.)
857 *
858 * @param string $type: the content type, can either plain or html
859 * @return void
860 */
861 public function getContent($type)
862 {
863 return $this->theParts[$type]['content'];
864 }
865
866
867 /**
868 * shows a preview of the email of the headers and the message
869 *
870 * @return void
871 */
872 public function preview()
873 {
874 echo nl2br(htmlspecialchars($this->headers));
875 echo "<BR>";
876 echo nl2br(htmlspecialchars($this->message));
877 }
878
879
880 /****************************************************
881 *
882 * Functions for acquiring attachments, HTML, analyzing and so on **
883 *
884 ***************************************************/
885
886 /**
887 * Fetches the HTML-content from either url og local serverfile
888 *
889 * @param string $file: the file to load
890 * @return boolean whether the data was fetched or not
891 */
892 public function fetchHTML($file)
893 {
894 // Fetches the content of the page
895 $this->theParts['html']['content'] = $this->getURL($file);
896 if ($this->theParts['html']['content']) {
897 $addr = $this->extParseUrl($file);
898 $path = ($addr['scheme']) ? $addr['scheme'] . '://' . $addr['host'] . (($addr['port']) ? ':' . $addr['port'] : '') . (($addr['filepath']) ? $addr['filepath'] : '/') : $addr['filepath'];
899 $this->theParts['html']['path'] = $path;
900 return TRUE;
901 } else {
902 return FALSE;
903 }
904 }
905
906
907 /**
908 * Fetches the mediafiles which are found by extractMediaLinks()
909 *
910 * @return void
911 */
912 public function fetchHTMLMedia()
913 {
914 if (!is_array($this->theParts['html']['media']) || !count($this->theParts['html']['media'])) {
915 return;
916 }
917 foreach ($this->theParts['html']['media'] as $key => $media) {
918 // fetching the content and the mime-type
919 $picdata = $this->getExtendedURL($this->theParts['html']['media'][$key]['absRef']);
920 if (is_array($picdata)) {
921 $this->theParts['html']['media'][$key]['content'] = $picdata['content'];
922 $this->theParts['html']['media'][$key]['ctype'] = $picdata['content_type'];
923 }
924 }
925 }
926
927
928 /**
929 * extracts all media-links from $this->theParts['html']['content']
930 *
931 * @return void
932 */
933 public function extractMediaLinks()
934 {
935 $html_code = $this->theParts['html']['content'];
936 $attribRegex = $this->tag_regex(array('img', 'table', 'td', 'tr', 'body', 'iframe', 'script', 'input', 'embed'));
937
938 // split the document by the beginning of the above tags
939 $codepieces = preg_split($attribRegex, $html_code);
940 $len = strlen($codepieces[0]);
941 $pieces = count($codepieces);
942 $reg = array();
943 for ($i = 1; $i < $pieces; $i++) {
944 $tag = strtolower(strtok(substr($html_code, $len + 1, 10), ' '));
945 $len += strlen($tag) + strlen($codepieces[$i]) + 2;
946 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
947 $attributes = $this->get_tag_attributes($reg[0]); // Fetches the attributes for the tag
948 $imageData = array();
949
950 // Finds the src or background attribute
951 $imageData['ref'] = ($attributes['src'] ? $attributes['src'] : $attributes['background']);
952 if ($imageData['ref']) {
953 // find out if the value had quotes around it
954 $imageData['quotes'] = (substr($codepieces[$i], strpos($codepieces[$i], $imageData['ref']) - 1, 1) == '"') ? '"' : '';
955 // subst_str is the string to look for, when substituting lateron
956 $imageData['subst_str'] = $imageData['quotes'] . $imageData['ref'] . $imageData['quotes'];
957 if ($imageData['ref'] && !strstr($this->image_fullpath_list, "|" . $imageData["subst_str"] . "|")) {
958 $this->image_fullpath_list .= "|" . $imageData['subst_str'] . "|";
959 $imageData['absRef'] = $this->absRef($imageData['ref']);
960 $imageData['tag'] = $tag;
961 $imageData['use_jumpurl'] = $attributes['dmailerping'] ? 1 : 0;
962 $this->theParts['html']['media'][] = $imageData;
963 }
964 }
965 }
966
967 // Extracting stylesheets
968 $attribRegex = $this->tag_regex(array('link'));
969 // Split the document by the beginning of the above tags
970 $codepieces = preg_split($attribRegex, $html_code);
971 $pieces = count($codepieces);
972 for ($i = 1; $i < $pieces; $i++) {
973 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
974 // fetches the attributes for the tag
975 $attributes = $this->get_tag_attributes($reg[0]);
976 $imageData = array();
977 if (strtolower($attributes['rel']) == 'stylesheet' && $attributes['href']) {
978 // Finds the src or background attribute
979 $imageData['ref'] = $attributes['href'];
980 // Finds out if the value had quotes around it
981 $imageData['quotes'] = (substr($codepieces[$i], strpos($codepieces[$i], $imageData['ref']) - 1, 1) == '"') ? '"' : '';
982 // subst_str is the string to look for, when substituting lateron
983 $imageData['subst_str'] = $imageData['quotes'] . $imageData['ref'] . $imageData['quotes'];
984 if ($imageData['ref'] && !strstr($this->image_fullpath_list, "|" . $imageData["subst_str"] . "|")) {
985 $this->image_fullpath_list .= "|" . $imageData["subst_str"] . "|";
986 $imageData['absRef'] = $this->absRef($imageData["ref"]);
987 $this->theParts['html']['media'][] = $imageData;
988 }
989 }
990 }
991
992 // fixes javascript rollovers
993 $codepieces = explode('.src', $html_code);
994 $pieces = count($codepieces);
995 $expr = '/^[^' . quotemeta('"') . quotemeta("'") . ']*/';
996 for ($i = 1; $i < $pieces; $i++) {
997 $temp = $codepieces[$i];
998 $temp = trim(str_replace('=', '', trim($temp)));
999 preg_match($expr, substr($temp, 1, strlen($temp)), $reg);
1000 $imageData['ref'] = $reg[0];
1001 $imageData['quotes'] = substr($temp, 0, 1);
1002 // subst_str is the string to look for, when substituting lateron
1003 $imageData['subst_str'] = $imageData['quotes'] . $imageData['ref'] . $imageData['quotes'];
1004 $theInfo = $this->split_fileref($imageData['ref']);
1005
1006 switch ($theInfo['fileext']) {
1007 case 'gif':
1008 case 'jpeg':
1009 case 'jpg':
1010 if ($imageData['ref'] && !strstr($this->image_fullpath_list, "|" . $imageData["subst_str"] . "|")) {
1011 $this->image_fullpath_list .= "|" . $imageData['subst_str'] . "|";
1012 $imageData['absRef'] = $this->absRef($imageData['ref']);
1013 $this->theParts['html']['media'][] = $imageData;
1014 }
1015 break;
1016 }
1017 }
1018 }
1019
1020
1021 /**
1022 * extracts all hyper-links from $this->theParts["html"]["content"]
1023 *
1024 * @return void
1025 */
1026 public function extractHyperLinks()
1027 {
1028 $html_code = $this->theParts['html']['content'];
1029 $attribRegex = $this->tag_regex(array('a', 'form', 'area'));
1030 $codepieces = preg_split($attribRegex, $html_code); // Splits the document by the beginning of the above tags
1031 $len = strlen($codepieces[0]);
1032 $pieces = count($codepieces);
1033 for ($i = 1; $i < $pieces; $i++) {
1034 $tag = strtolower(strtok(substr($html_code, $len + 1, 10), " "));
1035 $len += strlen($tag) + strlen($codepieces[$i]) + 2;
1036
1037 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
1038 // Fetches the attributes for the tag
1039 $attributes = $this->get_tag_attributes($reg[0]);
1040 $hrefData = array();
1041 $hrefData['ref'] = ($attributes['href'] ? $attributes['href'] : $hrefData['ref'] = $attributes['action']);
1042 if ($hrefData['ref']) {
1043 // Finds out if the value had quotes around it
1044 $hrefData['quotes'] = (substr($codepieces[$i], strpos($codepieces[$i], $hrefData["ref"]) - 1, 1) == '"') ? '"' : '';
1045 // subst_str is the string to look for, when substituting lateron
1046 $hrefData['subst_str'] = $hrefData['quotes'] . $hrefData['ref'] . $hrefData['quotes'];
1047 if ($hrefData['ref'] && substr(trim($hrefData['ref']), 0, 1) != "#" && !strstr($this->href_fullpath_list, "|" . $hrefData['subst_str'] . "|")) {
1048 $this->href_fullpath_list .= "|" . $hrefData['subst_str'] . "|";
1049 $hrefData['absRef'] = $this->absRef($hrefData['ref']);
1050 $hrefData['tag'] = $tag;
1051 $this->theParts['html']['hrefs'][] = $hrefData;
1052 }
1053 }
1054 }
1055 // Extracts TYPO3 specific links made by the openPic() JS function
1056 $codepieces = explode("onClick=\"openPic('", $html_code);
1057 $pieces = count($codepieces);
1058 for ($i = 1; $i < $pieces; $i++) {
1059 $showpic_linkArr = explode("'", $codepieces[$i]);
1060 $hrefData['ref'] = $showpic_linkArr[0];
1061 if ($hrefData['ref']) {
1062 $hrefData['quotes'] = "'";
1063 // subst_str is the string to look for, when substituting lateron
1064 $hrefData['subst_str'] = $hrefData['quotes'] . $hrefData['ref'] . $hrefData['quotes'];
1065 if ($hrefData['ref'] && !strstr($this->href_fullpath_list, "|" . $hrefData['subst_str'] . "|")) {
1066 $this->href_fullpath_list .= "|" . $hrefData['subst_str'] . "|";
1067 $hrefData['absRef'] = $this->absRef($hrefData['ref']);
1068 $this->theParts['html']['hrefs'][] = $hrefData;
1069 }
1070 }
1071 }
1072 }
1073
1074
1075 /**
1076 * extracts all media-links from $this->theParts["html"]["content"]
1077 *
1078 * @return array two-dimensional array with information about each frame
1079 */
1080 public function extractFramesInfo()
1081 {
1082 $htmlCode = $this->theParts['html']['content'];
1083 $info = array();
1084 if (strpos(' ' . $htmlCode, '<frame ')) {
1085 $attribRegex = $this->tag_regex('frame');
1086 // Splits the document by the beginning of the above tags
1087 $codepieces = preg_split($attribRegex, $htmlCode, 1000000);
1088 $pieces = count($codepieces);
1089 for ($i = 1; $i < $pieces; $i++) {
1090 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
1091 // Fetches the attributes for the tag
1092 $attributes = $this->get_tag_attributes($reg[0]);
1093 $frame = array();
1094 $frame['src'] = $attributes['src'];
1095 $frame['name'] = $attributes['name'];
1096 $frame['absRef'] = $this->absRef($frame['src']);
1097 $info[] = $frame;
1098 }
1099 return $info;
1100 }
1101 }
1102
1103
1104 /**
1105 * This function substitutes the media-references in $this->theParts["html"]["content"]
1106 *
1107 * @param boolean $absolute: If true, then the refs are substituted with http:// ref's indstead of Content-ID's (cid).
1108 * @return void
1109 */
1110 public function substMediaNamesInHTML($absolute)
1111 {
1112 if (is_array($this->theParts['html']['media'])) {
1113 foreach ($this->theParts['html']['media'] as $key => $val) {
1114 if ($val['use_jumpurl'] && $this->jumperURL_prefix) {
1115 $subst = $this->jumperURL_prefix . t3lib_div::rawUrlEncodeFP($val['absRef']);
1116 } else {
1117 $subst = ($absolute) ? $val['absRef'] : 'cid:part' . $key . '.' . $this->messageid;
1118 }
1119 $this->theParts['html']['content'] = str_replace(
1120 $val['subst_str'],
1121 $val['quotes'] . $subst . $val['quotes'],
1122 $this->theParts['html']['content']);
1123 }
1124 }
1125 if (!$absolute) {
1126 $this->fixRollOvers();
1127 }
1128 }
1129
1130
1131 /**
1132 * This function substitutes the hrefs in $this->theParts["html"]["content"]
1133 *
1134 * @return void
1135 */
1136 public function substHREFsInHTML()
1137 {
1138 if (!is_array($this->theParts['html']['hrefs'])) {
1139 return;
1140 }
1141 foreach ($this->theParts['html']['hrefs'] as $key => $val) {
1142 // Form elements cannot use jumpurl!
1143 if ($this->jumperURL_prefix && $val['tag'] != 'form') {
1144 if ($this->jumperURL_useId) {
1145 $substVal = $this->jumperURL_prefix . $key;
1146 } else {
1147 $substVal = $this->jumperURL_prefix . t3lib_div::rawUrlEncodeFP($val['absRef']);
1148 }
1149 } else {
1150 $substVal = $val['absRef'];
1151 }
1152 $this->theParts['html']['content'] = str_replace(
1153 $val['subst_str'],
1154 $val['quotes'] . $substVal . $val['quotes'],
1155 $this->theParts['html']['content']);
1156 }
1157 }
1158
1159
1160 /**
1161 * This substitutes the http:// urls in plain text with links
1162 *
1163 * @param string $content: the content to use to substitute
1164 * @return string the changed content
1165 */
1166 public function substHTTPurlsInPlainText($content)
1167 {
1168 if (!$this->jumperURL_prefix) {
1169 return $content;
1170 }
1171
1172 $textpieces = explode("http://", $content);
1173 $pieces = count($textpieces);
1174 $textstr = $textpieces[0];
1175 for ($i = 1; $i < $pieces; $i++) {
1176 $len = strcspn($textpieces[$i], chr(32) . TAB . CRLF);
1177 if (trim(substr($textstr, -1)) == '' && $len) {
1178 $lastChar = substr($textpieces[$i], $len - 1, 1);
1179 if (!preg_match('/[A-Za-z0-9\/#]/', $lastChar)) {
1180 $len--;
1181 }
1182
1183 $parts = array();
1184 $parts[0] = "http://" . substr($textpieces[$i], 0, $len);
1185 $parts[1] = substr($textpieces[$i], $len);
1186
1187 if ($this->jumperURL_useId) {
1188 $this->theParts['plain']['link_ids'][$i] = $parts[0];
1189 $parts[0] = $this->jumperURL_prefix . '-' . $i;
1190 } else {
1191 $parts[0] = $this->jumperURL_prefix . t3lib_div::rawUrlEncodeFP($parts[0]);
1192 }
1193 $textstr .= $parts[0] . $parts[1];
1194 } else {
1195 $textstr .= 'http://' . $textpieces[$i];
1196 }
1197 }
1198 return $textstr;
1199 }
1200
1201
1202 /**
1203 * JavaScript rollOvers cannot support graphics inside of mail.
1204 * If these exists we must let them refer to the absolute url. By the way:
1205 * Roll-overs seems to work only on some mail-readers and so far I've seen it
1206 * work on Netscape 4 message-center (but not 4.5!!)
1207 *
1208 * @return void
1209 */
1210 public function fixRollOvers()
1211 {
1212 $newContent = '';
1213 $items = explode('.src', $this->theParts['html']['content']);
1214 if (count($items) <= 1) {
1215 return;
1216 }
1217
1218 foreach ($items as $key => $part) {
1219 $sub = substr($part, 0, 200);
1220 if (preg_match('/cid:part[^ "\']*/', $sub, $reg)) {
1221 // The position of the string
1222 $thePos = strpos($part, $reg[0]);
1223 // Finds the id of the media...
1224 preg_match('/cid:part([^\.]*).*/', $sub, $reg2);
1225 $theSubStr = $this->theParts['html']['media'][intval($reg2[1])]['absRef'];
1226 if ($thePos && $theSubStr) {
1227 // ... and substitutes the javaScript rollover image with this instead
1228 // If the path is NOT and url, the reference is set to nothing
1229 if (!strpos(' ' . $theSubStr, 'http://')) {
1230 $theSubStr = 'http://';
1231 }
1232 $part = substr($part, 0, $thePos) . $theSubStr . substr($part, $thePos + strlen($reg[0]), strlen($part));
1233 }
1234 }
1235 $newContent .= $part . ((($key + 1) != count($items)) ? '.src' : '');
1236 }
1237 $this->theParts['html']['content'] = $newContent;
1238 }
1239
1240
1241 /*******************************************
1242 *
1243 * File and URL-functions
1244 *
1245 *******************************************/
1246
1247 /**
1248 * Returns base64-encoded content, which is broken every 76 character
1249 *
1250 * @param string $inputstr: the string to encode
1251 * @return string the encoded string
1252 */
1253 public function makeBase64($inputstr)
1254 {
1255 return chunk_split(base64_encode($inputstr));
1256 }
1257
1258
1259 /**
1260 * reads the URL or file and determines the Content-type by either guessing or opening a connection to the host
1261 *
1262 * @param string $url: the URL to get information of
1263 * @return mixed either false or the array with information
1264 */
1265 public function getExtendedURL($url)
1266 {
1267 $res = array();
1268 $res['content'] = $this->getURL($url);
1269 if (!$res['content']) {
1270 return FALSE;
1271 }
1272 $pathInfo = parse_url($url);
1273 $fileInfo = $this->split_fileref($pathInfo['path']);
1274 switch ($fileInfo['fileext']) {
1275 case 'gif':
1276 case 'png':
1277 $res['content_type'] = 'image/' . $fileInfo['fileext'];
1278 break;
1279 case 'jpg':
1280 case 'jpeg':
1281 $res['content_type'] = 'image/jpeg';
1282 break;
1283 case 'html':
1284 case 'htm':
1285 $res['content_type'] = 'text/html';
1286 break;
1287 case 'css':
1288 $res['content_type'] = 'text/css';
1289 break;
1290 case 'swf':
1291 $res['content_type'] = 'application/x-shockwave-flash';
1292 break;
1293 default:
1294 $res['content_type'] = $this->getMimeType($url);
1295 }
1296 return $res;
1297 }
1298
1299
1300 /**
1301 * Adds HTTP user and password (from $this->http_username) to a URL
1302 *
1303 * @param string $url: the URL
1304 * @return string the URL with the added values
1305 */
1306 public function addUserPass($url)
1307 {
1308 $user = $this->http_username;
1309 $pass = $this->http_password;
1310 $matches = array();
1311 if ($user && $pass && preg_match('/^(https?:\/\/)/', $url, $matches)) {
1312 return $matches[1] . $user . ':' . $pass . '@' . substr($url, strlen($matches[1]));
1313 }
1314 return $url;
1315 }
1316
1317
1318 /**
1319 * reads a url or file
1320 *
1321 * @param string $url: the URL to fetch
1322 * @return string the content of the URL
1323 */
1324 public function getURL($url)
1325 {
1326 $url = $this->addUserPass($url);
1327 return t3lib_div::getURL($url);
1328 }
1329
1330
1331 /**
1332 * reads a url or file and strips the HTML-tags AND removes all
1333 * empty lines. This is used to read plain-text out of a HTML-page
1334 *
1335 * @param string $url: the URL to load
1336 * @return the content
1337 */
1338 public function getStrippedURL($url)
1339 {
1340 $content = '';
1341 if ($fd = fopen($url, "rb")) {
1342 while (!feof($fd)) {
1343 $line = fgetss($fd, 5000);
1344 if (trim($line)) {
1345 $content .= trim($line) . LF;
1346 }
1347 }
1348 fclose($fd);
1349 }
1350 return $content;
1351 }
1352
1353
1354 /**
1355 * This function returns the mime type of the file specified by the url
1356 *
1357 * @param string $url: the url
1358 * @return string $mimeType: the mime type found in the header
1359 */
1360 public function getMimeType($url)
1361 {
1362 $mimeType = '';
1363 $headers = trim(t3lib_div::getURL($url, 2));
1364 if ($headers) {
1365 $matches = array();
1366 if (preg_match('/(Content-Type:[\s]*)([a-zA-Z_0-9\/\-\.\+]*)([\s]|$)/', $headers, $matches)) {
1367 $mimeType = trim($matches[2]);
1368 }
1369 }
1370 return $mimeType;
1371 }
1372
1373
1374 /**
1375 * Returns the absolute address of a link. This is based on
1376 * $this->theParts["html"]["path"] being the root-address
1377 *
1378 * @param string $ref: address to use
1379 * @return string the absolute address
1380 */
1381 public function absRef($ref)
1382 {
1383 $ref = trim($ref);
1384 $info = parse_url($ref);
1385 if ($info['scheme']) {
1386 return $ref;
1387 } elseif (preg_match('/^\//', $ref)) {
1388 $addr = parse_url($this->theParts['html']['path']);
1389 return $addr['scheme'] . '://' . $addr['host'] . ($addr['port'] ? ':' . $addr['port'] : '') . $ref;
1390 } else {
1391 // If the reference is relative, the path is added, in order for us to fetch the content
1392 return $this->theParts['html']['path'] . $ref;
1393 }
1394 }
1395
1396
1397 /**
1398 * Returns information about a file reference
1399 *
1400 * @param string $fileref: the file to use
1401 * @return array path, filename, filebody, fileext
1402 */
1403 public function split_fileref($fileref)
1404 {
1405 $info = array();
1406 if (preg_match('/(.*\/)(.*)$/', $fileref, $reg)) {
1407 $info['path'] = $reg[1];
1408 $info['file'] = $reg[2];
1409 } else {
1410 $info['path'] = '';
1411 $info['file'] = $fileref;
1412 }
1413 $reg = '';
1414 if (preg_match('/(.*)\.([^\.]*$)/', $info['file'], $reg)) {
1415 $info['filebody'] = $reg[1];
1416 $info['fileext'] = strtolower($reg[2]);
1417 $info['realFileext'] = $reg[2];
1418 } else {
1419 $info['filebody'] = $info['file'];
1420 $info['fileext'] = '';
1421 }
1422 return $info;
1423 }
1424
1425
1426 /**
1427 * Returns an array with file or url-information
1428 *
1429 * @param string $path: url to check
1430 * @return array information about the path / URL
1431 */
1432 public function extParseUrl($path)
1433 {
1434 $res = parse_url($path);
1435 preg_match('/(.*\/)([^\/]*)$/', $res['path'], $reg);
1436 $res['filepath'] = $reg[1];
1437 $res['filename'] = $reg[2];
1438 return $res;
1439 }
1440
1441
1442 /**
1443 * Creates a regular expression out of a list of tags
1444 *
1445 * @param mixed $tagArray: the list of tags (either as array or string if it is one tag)
1446 * @return string the regular expression
1447 */
1448 public function tag_regex($tags)
1449 {
1450 $tags = (!is_array($tags) ? array($tags) : $tags);
1451 $regexp = '/';
1452 $c = count($tags);
1453 foreach ($tags as $tag) {
1454 $c--;
1455 $regexp .= '<' . $tag . '[[:space:]]' . (($c) ? '|' : '');
1456 }
1457 return $regexp . '/i';
1458 }
1459
1460
1461 /**
1462 * This function analyzes a HTML tag
1463 * If an attribute is empty (like OPTION) the value of that key is just empty. Check it with is_set();
1464 *
1465 * @param string $tag: is either like this "<TAG OPTION ATTRIB=VALUE>" or
1466 * this " OPTION ATTRIB=VALUE>" which means you can omit the tag-name
1467 * @return array array with attributes as keys in lower-case
1468 */
1469 public function get_tag_attributes($tag)
1470 {
1471 $attributes = array();
1472 $tag = ltrim(preg_replace('/^<[^ ]*/', '', trim($tag)));
1473 $tagLen = strlen($tag);
1474 $safetyCounter = 100;
1475 // Find attribute
1476 while ($tag) {
1477 $value = '';
1478 $reg = preg_split('/[[:space:]=>]/', $tag, 2);
1479 $attrib = $reg[0];
1480
1481 $tag = ltrim(substr($tag, strlen($attrib), $tagLen));
1482 if (substr($tag, 0, 1) == '=') {
1483 $tag = ltrim(substr($tag, 1, $tagLen));
1484 if (substr($tag, 0, 1) == '"') {
1485 // Quotes around the value
1486 $reg = explode('"', substr($tag, 1, $tagLen), 2);
1487 $tag = ltrim($reg[1]);
1488 $value = $reg[0];
1489 } else {
1490 // No quotes around value
1491 preg_match('/^([^[:space:]>]*)(.*)/', $tag, $reg);
1492 $value = trim($reg[1]);
1493 $tag = ltrim($reg[2]);
1494 if (substr($tag, 0, 1) == '>') {
1495 $tag = '';
1496 }
1497 }
1498 }
1499 $attributes[strtolower($attrib)] = $value;
1500 $safetyCounter--;
1501 if ($safetyCounter < 0) {
1502 break;
1503 }
1504 }
1505 return $attributes;
1506 }
1507
1508
1509 /**
1510 * Implementation of quoted-printable encode.
1511 * This function was a duplicate of t3lib_div::quoted_printable, thus it's going to be removed.
1512 * Deprecated since TYPO3 4.0
1513 *
1514 * @param string Content to encode
1515 * @return string The QP encoded string
1516 * @deprecated since TYPO3 4.0, will be removed in TYPO3 4.6
1517 */
1518 public function quoted_printable($string)
1519 {
1520 t3lib_div::logDeprecatedFunction();
1521
1522 return t3lib_div::quoted_printable($string, 76);
1523 }
1524
1525
1526 /**
1527 * Converts a name field
1528 * Deprecated since TYPO3 4.0
1529 *
1530 * @param string $name: the name
1531 * @return string the name
1532 * @deprecated since TYPO3 4.0, will be removed in TYPO3 4.6
1533 */
1534 public function convertName($name)
1535 {
1536 t3lib_div::logDeprecatedFunction();
1537
1538 return $name;
1539 }
1540 }
1541
1542 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_htmlmail.php']) {
1543 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_htmlmail.php']);
1544 }
1545
1546 ?>