14050_cleaning_t3lib_arraybrowser
[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 // Headerinfo:
185 var $recipient = '';
186 var $recipient_copy = ''; // This recipient (or list of...) will also receive the mail. Regard it as a copy.
187 var $recipient_blindcopy = ''; // This recipient (or list of...) will also receive the mail as a blind copy. Regard it as a copy.
188 var $subject = '';
189 var $from_email = '';
190 var $from_name = '';
191 var $replyto_email = '';
192 var $replyto_name = '';
193 var $organisation = '';
194 var $priority = 3; // 1 = highest, 5 = lowest, 3 = normal
195 var $mailer = ''; // X-mailer, set to TYPO3 Major.Minor in constructor
196 var $alt_base64 = 0;
197 var $alt_8bit = 0;
198 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.
199 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
200 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
201 var $http_password = '';
202 var $http_username = '';
203 var $postfix_version1 = false;
204
205 // Internal
206 /*
207 This is how the $theParts-array is normally looking
208 var $theParts = array(
209 'plain' => array(
210 'content' => ''
211 ),
212 'html' => array(
213 'content' => '',
214 'path' => '',
215 'media' => array(),
216 'hrefs' => array()
217 ),
218 'attach' => array()
219 );
220 */
221 var $theParts = array();
222
223 var $messageid = '';
224 var $returnPath = '';
225 var $Xid = '';
226 var $dontEncodeHeader = false; // If set, the header will not be encoded
227
228 var $headers = '';
229 var $message = '';
230 var $part = 0;
231 var $image_fullpath_list = '';
232 var $href_fullpath_list = '';
233
234 var $plain_text_header = '';
235 var $html_text_header = '';
236 var $charset = '';
237 var $defaultCharset = 'iso-8859-1';
238
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 $this->forceReturnPath = $GLOBALS['TYPO3_CONF_VARS']['SYS']['forceReturnPath'];
250
251 $this->mailer = 'TYPO3';
252 }
253
254
255 /**
256 * start action that sets the message ID and the charset
257 *
258 * @return void
259 */
260 public function start() {
261 global $TYPO3_CONF_VARS;
262
263 // Sets the message id
264 $host = t3lib_div::getHostname();
265 if (!$host || $host == '127.0.0.1' || $host == 'localhost' || $host == 'localhost.localdomain') {
266 $host = ($TYPO3_CONF_VARS['SYS']['sitename'] ? preg_replace('/[^A-Za-z0-9_\-]/', '_', $TYPO3_CONF_VARS['SYS']['sitename']) : 'localhost') . '.TYPO3';
267 }
268 $this->messageid = md5(microtime()) . '@' . $host;
269
270
271 // Default line break for Unix systems.
272 $this->linebreak = LF;
273 // Line break for Windows. This is needed because PHP on Windows systems
274 // send mails via SMTP instead of using sendmail, and thus the linebreak needs to be \r\n.
275 if (TYPO3_OS == 'WIN') {
276 $this->linebreak = CRLF;
277 }
278
279 // Sets the Charset
280 if (!$this->charset) {
281 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->renderCharset) {
282 $this->charset = $GLOBALS['TSFE']->renderCharset;
283 } elseif (is_object($GLOBALS['LANG']) && $GLOBALS['LANG']->charSet) {
284 $this->charset = $GLOBALS['LANG']->charSet;
285 } elseif ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) {
286 $this->charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
287 } else {
288 $this->charset = $this->defaultCharset;
289 }
290 }
291
292 // Use quoted-printable headers by default
293 $this->useQuotedPrintable();
294 }
295
296
297 /**
298 * sets the header of both Plain Text and HTML mails to quoted printable
299 *
300 * @return void
301 */
302 public function useQuotedPrintable() {
303 $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: quoted-printable';
304 $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: quoted-printable';
305 }
306
307 /**
308 * sets the encoding headers to base64 for both the Plain Text and HTML mail
309 *
310 * @return void
311 */
312 public function useBase64() {
313 $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: base64';
314 $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: base64';
315 $this->alt_base64 = 1;
316 }
317
318
319 /**
320 * sets the encoding to 8bit and the current charset of both the Plain Text and the HTML mail
321 *
322 * @return void
323 */
324 public function use8Bit() {
325 $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: 8bit';
326 $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: 8bit';
327 $this->alt_8bit = 1;
328 }
329
330
331 /**
332 * Encodes the message content according to the options "alt_base64" and "alt_8bit" (no need to encode here)
333 * or to "quoted_printable" if no option is set.
334 *
335 * @param string $content the content that will be encoded
336 * @return string the encoded content
337 */
338 public function encodeMsg($content) {
339 if ($this->alt_base64) {
340 return $this->makeBase64($content);
341 } elseif ($this->alt_8bit) {
342 return $content;
343 } else {
344 return t3lib_div::quoted_printable($content);
345 }
346 }
347
348
349 /**
350 * Adds plain-text, replaces the HTTP urls in the plain text and then encodes it
351 *
352 * @param string $content that will be added
353 * @return void
354 */
355 public function addPlain($content) {
356 $content = $this->substHTTPurlsInPlainText($content);
357 $this->setPlain($this->encodeMsg($content));
358 }
359
360
361 /**
362 * Adds an attachment to the mail
363 *
364 * @param string $file: the filename to add
365 * @return boolean whether the attachment was added or not
366 */
367 public function addAttachment($file) {
368 // Fetching the content and the mime-type
369 $fileInfo = $this->getExtendedURL($file);
370 if ($fileInfo) {
371 if (!$fileInfo['content_type']) {
372 $fileInfo['content_type'] = 'application/octet-stream';
373 }
374 $temp = $this->split_fileref($file);
375 if ($temp['file']) {
376 $fileInfo['filename'] = $temp['file'];
377 } elseif (strpos(' '.$fileInfo['content_type'], 'htm')) {
378 $fileInfo['filename'] = 'index.html';
379 } else {
380 $fileInfo['filename'] = 'unknown';
381 }
382 $this->theParts['attach'][] = $fileInfo;
383 return true;
384 }
385 return false;
386 }
387
388
389 /**
390 * Adds HTML and media, encodes it from a URL or file
391 *
392 * @param string $file: the filename to add
393 * @return boolean whether the attachment was added or not
394 */
395 public function addHTML($file) {
396 $status = $this->fetchHTML($file);
397 if (!$status) {
398 return false;
399 }
400 if ($this->extractFramesInfo()) {
401 return 'Document was a frameset. Stopped';
402 }
403 $this->extractMediaLinks();
404 $this->extractHyperLinks();
405 $this->fetchHTMLMedia();
406 $this->substMediaNamesInHTML(0); // 0 = relative
407 $this->substHREFsInHTML();
408 $this->setHtml($this->encodeMsg($this->theParts['html']['content']));
409 }
410
411
412 /**
413 * Extract HTML-parts, used externally
414 *
415 * @param string $html: will be added to the html "content" part
416 * @param string $url: will be added to the html "path" part
417 * @return void
418 */
419 public function extractHtmlInit($html,$url) {
420 $this->theParts['html']['content'] = $html;
421 $this->theParts['html']['path'] = $url;
422 }
423
424
425 /**
426 * Assembles the message by headers and content and finally send it to the provided recipient.
427 *
428 * @param string $recipient: The recipient the message should be delivered to (if blank, $this->recipient will be used instead)
429 * @return boolean Returns whether the mail was sent (successfully accepted for delivery)
430 */
431 public function send($recipient) {
432 if ($recipient) {
433 $this->recipient = $recipient;
434 }
435 $this->setHeaders();
436 $this->setContent();
437 $mailWasSent = $this->sendTheMail();
438 return $mailWasSent;
439 }
440
441
442
443
444
445
446
447
448
449
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 $this->headers = '';
466 // Message_id
467 $this->add_header('Message-ID: <'.$this->messageid.'>');
468 // Return path
469 if ($this->returnPath) {
470 $this->add_header('Return-Path: '.$this->returnPath);
471 $this->add_header('Errors-To: '.$this->returnPath);
472 }
473 // X-id
474 if ($this->Xid) {
475 $this->add_header('X-Typo3MID: '.$this->Xid);
476 }
477
478 // From
479 if ($this->from_email) {
480 if ($this->from_name && !t3lib_div::isBrokenEmailEnvironment()) {
481 $this->add_header('From: '.$this->from_name.' <'.$this->from_email.'>');
482 } else {
483 $this->add_header('From: '.$this->from_email);
484 }
485 }
486
487 // Cc
488 if ($this->recipient_copy) {
489 $this->add_header('Cc: ' . $this->recipient_copy);
490 }
491
492 // Bcc
493 if ($this->recipient_blindcopy) {
494 $this->add_header('Bcc: ' . $this->recipient_blindcopy);
495 }
496
497 // Reply
498 if ($this->replyto_email) {
499 if ($this->replyto_name) {
500 $this->add_header('Reply-To: '.$this->replyto_name.' <'.$this->replyto_email.'>');
501 } else {
502 $this->add_header('Reply-To: '.$this->replyto_email);
503 }
504 }
505 // Organization, using american english spelling (organization / organisation) as defined in RFC 1036 / 2076
506 if ($this->organisation) {
507 $this->add_header('Organization: ' . $this->organisation);
508 }
509 // mailer
510 if ($this->mailer) {
511 $this->add_header('X-Mailer: '.$this->mailer);
512 }
513 // priority
514 if ($this->priority) {
515 $this->add_header('X-Priority: '.$this->priority);
516 }
517 $this->add_header('Mime-Version: 1.0');
518
519 if (!$this->dontEncodeHeader) {
520 $enc = $this->alt_base64 ? 'base64' : 'quoted_printable'; // Header must be ASCII, therefore only base64 or quoted_printable are allowed!
521 // Quote recipient and subject
522 $this->recipient = t3lib_div::encodeHeader($this->recipient,$enc,$this->charset);
523 $this->subject = t3lib_div::encodeHeader($this->subject,$enc,$this->charset);
524 }
525 }
526
527
528 /**
529 * Sets the recipient(s). If you supply a string, you set one recipient.
530 * If you supply an array, every value is added as a recipient.
531 *
532 * @param mixed $recipient: the recipient(s) to set
533 * @return void
534 */
535 public function setRecipient($recipient) {
536 $this->recipient = (is_array($recipient) ? implode(',', $recipient) : $recipient);
537 }
538
539
540 /**
541 * Returns the content type based on whether the mail has media / attachments or no
542 *
543 * @return string the content type
544 */
545 public function getHTMLContentType() {
546 return (count($this->theParts['html']['media']) ? 'multipart/related' : 'multipart/alternative');
547 }
548
549
550 /**
551 * Begins building the message-body
552 *
553 * @return void
554 */
555 public function setContent() {
556 $this->message = '';
557 $boundary = $this->getBoundary();
558
559 // Setting up headers
560 if (count($this->theParts['attach'])) {
561 // Generate (plain/HTML) / attachments
562 $this->add_header('Content-Type: multipart/mixed;');
563 $this->add_header(' boundary="' . $boundary . '"');
564 $this->add_message('This is a multi-part message in MIME format.' . LF);
565 $this->constructMixed($boundary);
566 } elseif ($this->theParts['html']['content']) {
567 // Generate plain/HTML mail
568 $this->add_header('Content-Type: ' . $this->getHTMLContentType() . ';');
569 $this->add_header(' boundary="' . $boundary . '"');
570 $this->add_message('This is a multi-part message in MIME format.' . LF);
571 $this->constructHTML($boundary);
572 } else {
573 // Generate plain only
574 $this->add_header($this->plain_text_header);
575 $this->add_message($this->getContent('plain'));
576 }
577 }
578
579
580 /**
581 * This functions combines the plain / HTML content with the attachments
582 *
583 * @param string $boundary: the mail boundary
584 * @return void
585 */
586 public function constructMixed($boundary) {
587 $this->add_message('--' . $boundary);
588
589 if ($this->theParts['html']['content']) {
590 // HTML and plain is added
591 $newBoundary = $this->getBoundary();
592 $this->add_message('Content-Type: '.$this->getHTMLContentType() . ';');
593 $this->add_message(' boundary="' . $newBoundary . '"');
594 $this->add_message('');
595 $this->constructHTML($newBoundary);
596 } else {
597 // Purely plain
598 $this->add_message($this->plain_text_header);
599 $this->add_message('');
600 $this->add_message($this->getContent('plain'));
601 }
602 // attachments are added
603 if (is_array($this->theParts['attach'])) {
604 foreach ($this->theParts['attach'] as $media) {
605 $this->add_message('--' . $boundary);
606 $this->add_message('Content-Type: ' . $media['content_type'] . ';');
607 $this->add_message(' name="' . $media['filename'] . '"');
608 $this->add_message('Content-Transfer-Encoding: base64');
609 $this->add_message('Content-Disposition: attachment;');
610 $this->add_message(' filename="' . $media['filename'] . '"');
611 $this->add_message('');
612 $this->add_message($this->makeBase64($media['content']));
613 }
614 }
615 $this->add_message('--' . $boundary . '--' . LF);
616 }
617
618
619 /**
620 * this function creates the HTML part of the mail
621 *
622 * @param string $boundary: the boundary to use
623 * @return void
624 */
625 public function constructHTML($boundary) {
626 // If media, then we know, the multipart/related content-type has been set before this function call
627 if (count($this->theParts['html']['media'])) {
628 $this->add_message('--' . $boundary);
629 // HTML has media
630 $newBoundary = $this->getBoundary();
631 $this->add_message('Content-Type: multipart/alternative;');
632 $this->add_message(' boundary="' . $newBoundary . '"');
633 $this->add_message('Content-Transfer-Encoding: 7bit');
634 $this->add_message('');
635
636 // Adding the plaintext/html mix, and use $newBoundary
637 $this->constructAlternative($newBoundary);
638 $this->constructHTML_media($boundary);
639 } else {
640 // if no media, just use the $boundary for adding plaintext/html mix
641 $this->constructAlternative($boundary);
642 }
643 }
644
645
646 /**
647 * Here plain is combined with HTML
648 *
649 * @param string $boundary: the boundary to use
650 * @return void
651 */
652 public function constructAlternative($boundary) {
653 $this->add_message('--'.$boundary);
654
655 // plain is added
656 $this->add_message($this->plain_text_header);
657 $this->add_message('');
658 $this->add_message($this->getContent('plain'));
659 $this->add_message('--' . $boundary);
660
661 // html is added
662 $this->add_message($this->html_text_header);
663 $this->add_message('');
664 $this->add_message($this->getContent('html'));
665 $this->add_message('--' . $boundary . '--' . LF);
666 }
667
668
669 /**
670 * Constructs the HTML-part of message if the HTML contains media
671 *
672 * @param string $boundary: the boundary to use
673 * @return void
674 */
675 public function constructHTML_media($boundary) {
676 // media is added
677 if (is_array($this->theParts['html']['media'])) {
678 foreach($this->theParts['html']['media'] as $key => $media) {
679 if (!$this->mediaList || t3lib_div::inList($this->mediaList, $key)) {
680 $this->add_message('--' . $boundary);
681 $this->add_message('Content-Type: ' . $media['ctype']);
682 $this->add_message('Content-ID: <part' . $key . '.' . $this->messageid . '>');
683 $this->add_message('Content-Transfer-Encoding: base64');
684 $this->add_message('');
685 $this->add_message($this->makeBase64($media['content']));
686 }
687 }
688 }
689 $this->add_message('--' . $boundary . '--' . LF);
690 }
691
692
693 /**
694 * Sends the mail by calling the mail() function in php. On Linux systems this will invoke the MTA
695 * defined in php.ini (sendmail -t -i by default), on Windows a SMTP must be specified in the sys.ini.
696 * Most common MTA's on Linux has a Sendmail interface, including Postfix and Exim.
697 * For setting the return-path correctly, the parameter -f has to be added to the system call to sendmail.
698 * This obviously does not have any effect on Windows, but on Sendmail compliant systems this works. If safe mode
699 * is enabled, then extra parameters is not allowed, so a safe mode check is made before the mail() command is
700 * invoked. When using the -f parameter, some MTA's will put an X-AUTHENTICATION-WARNING saying that
701 * the return path was modified manually with the -f flag. To disable this warning make sure that the user running
702 * Apache is in the /etc/mail/trusted-users table.
703 *
704 * POSTFIX: With postfix version below 2.0 there is a problem that the -f parameter can not be used in conjunction
705 * with -t. Postfix will give an error in the maillog:
706 *
707 * cannot handle command-line recipients with -t
708 *
709 * The -f parameter is only enabled if the parameter forceReturnPath is enabled in the install tool.
710 *
711 * This whole problem of return-path turns out to be quite tricky. If you have a solution that works better, on all
712 * standard MTA's then we are very open for suggestions.
713 *
714 * With time this function should be made such that several ways of sending the mail is possible (local MTA, smtp other).
715 *
716 * @return boolean Returns whether the mail was sent (successfully accepted for delivery)
717 */
718 public function sendTheMail() {
719 $mailWasSent = false;
720
721 // Sending the mail requires the recipient and message to be set.
722 if (!trim($this->recipient) || !trim($this->message)) {
723 return false;
724 }
725
726 // On windows the -f flag is not used (specific for Sendmail and Postfix),
727 // but instead the php.ini parameter sendmail_from is used.
728 $returnPath = ($this->forceReturnPath && strlen($this->returnPath) > 0) ? '-f ' . escapeshellarg($this->returnPath) : '';
729 if (TYPO3_OS == 'WIN' && $this->returnPath) {
730 @ini_set('sendmail_from', t3lib_div::normalizeMailAddress($this->returnPath));
731 }
732 $recipient = t3lib_div::normalizeMailAddress($this->recipient);
733
734 // If safe mode is on, the fifth parameter to mail is not allowed, so the fix wont work on unix with safe_mode=On
735 $returnPathPossible = (!t3lib_utility_PhpOptions::isSafeModeEnabled() && $this->forceReturnPath);
736 if ($returnPathPossible) {
737 $mailWasSent = t3lib_utility_Mail::mail($recipient,
738 $this->subject,
739 $this->message,
740 $this->headers,
741 $returnPath);
742 } else {
743 $mailWasSent = t3lib_utility_Mail::mail($recipient,
744 $this->subject,
745 $this->message,
746 $this->headers);
747 }
748
749 // Auto response
750 if ($this->auto_respond_msg) {
751 $theParts = explode('/',$this->auto_respond_msg,2);
752 $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]);
753 $theParts[1] = str_replace("/",LF,$theParts[1]);
754 $theParts[1] = str_replace("###MESSAGE###", $this->getContent('plain'), $theParts[1]);
755 if ($returnPathPossible) {
756 $mailWasSent = t3lib_utility_Mail::mail($this->from_email,
757 $theParts[0],
758 $theParts[1],
759 'From: ' . $recipient,
760 $returnPath);
761 } else {
762 $mailWasSent = t3lib_utility_Mail::mail($this->from_email,
763 $theParts[0],
764 $theParts[1],
765 'From: ' . $recipient);
766 }
767 }
768 if ($this->returnPath) {
769 ini_restore('sendmail_from');
770 }
771 return $mailWasSent;
772 }
773
774
775 /**
776 * Returns boundaries
777 *
778 * @return string the boundary
779 */
780 public function getBoundary() {
781 $this->part++;
782 return "----------".uniqid("part_".$this->part."_");
783 }
784
785
786 /**
787 * Sets the plain-text part. No processing done.
788 *
789 * @param string $content: the plain content
790 * @return void
791 */
792 public function setPlain($content) {
793 $this->theParts['plain']['content'] = $content;
794 }
795
796
797 /**
798 * Sets the HTML-part. No processing done.
799 *
800 * @param string $content: the HTML content
801 * @return void
802 */
803 public function setHtml($content) {
804 $this->theParts['html']['content'] = $content;
805 }
806
807
808 /**
809 * Adds a header to the mail. Use this AFTER the setHeaders()-function
810 *
811 * @param string $header: the header in form of "key: value"
812 * @return void
813 */
814 public function add_header($header) {
815 // Mail headers must be ASCII, therefore we convert the whole header to either base64 or quoted_printable
816 if (!$this->dontEncodeHeader && !stristr($header,'Content-Type') && !stristr($header,'Content-Transfer-Encoding')) {
817 // Field tags must not be encoded
818 $parts = explode(': ',$header,2);
819 if (count($parts) == 2) {
820 $enc = $this->alt_base64 ? 'base64' : 'quoted_printable';
821 $parts[1] = t3lib_div::encodeHeader($parts[1], $enc, $this->charset);
822 $header = implode(': ', $parts);
823 }
824 }
825
826 $this->headers .= $header.LF;
827 }
828
829
830 /**
831 * Adds a line of text to the mail-body. Is normally used internally
832 *
833 * @param string $msg: the message to add
834 * @return void
835 */
836 public function add_message($msg) {
837 $this->message .= $msg.LF;
838 }
839
840
841 /**
842 * returns the content specified by the type (plain, html etc.)
843 *
844 * @param string $type: the content type, can either plain or html
845 * @return void
846 */
847 public function getContent($type) {
848 return $this->theParts[$type]['content'];
849 }
850
851
852 /**
853 * shows a preview of the email of the headers and the message
854 *
855 * @return void
856 */
857 public function preview() {
858 echo nl2br(htmlspecialchars($this->headers));
859 echo "<BR>";
860 echo nl2br(htmlspecialchars($this->message));
861 }
862
863
864
865
866
867
868
869
870 /****************************************************
871 *
872 * Functions for acquiring attachments, HTML, analyzing and so on **
873 *
874 ***************************************************/
875
876 /**
877 * Fetches the HTML-content from either url og local serverfile
878 *
879 * @param string $file: the file to load
880 * @return boolean whether the data was fetched or not
881 */
882 public function fetchHTML($file) {
883 // Fetches the content of the page
884 $this->theParts['html']['content'] = $this->getURL($file);
885 if ($this->theParts['html']['content']) {
886 $addr = $this->extParseUrl($file);
887 $path = ($addr['scheme']) ? $addr['scheme'].'://'.$addr['host'].(($addr['port'])?':'.$addr['port']:'').(($addr['filepath'])?$addr['filepath']:'/') : $addr['filepath'];
888 $this->theParts['html']['path'] = $path;
889 return true;
890 } else {
891 return false;
892 }
893 }
894
895
896 /**
897 * Fetches the mediafiles which are found by extractMediaLinks()
898 *
899 * @return void
900 */
901 public function fetchHTMLMedia() {
902 if (!is_array($this->theParts['html']['media']) || !count($this->theParts['html']['media'])) return;
903 foreach ($this->theParts['html']['media'] as $key => $media) {
904 // fetching the content and the mime-type
905 $picdata = $this->getExtendedURL($this->theParts['html']['media'][$key]['absRef']);
906 if (is_array($picdata)) {
907 $this->theParts['html']['media'][$key]['content'] = $picdata['content'];
908 $this->theParts['html']['media'][$key]['ctype'] = $picdata['content_type'];
909 }
910 }
911 }
912
913
914 /**
915 * extracts all media-links from $this->theParts['html']['content']
916 *
917 * @return void
918 */
919 public function extractMediaLinks() {
920 $html_code = $this->theParts['html']['content'];
921 $attribRegex = $this->tag_regex(array('img','table','td','tr','body','iframe','script','input','embed'));
922
923 // split the document by the beginning of the above tags
924 $codepieces = preg_split($attribRegex, $html_code);
925 $len = strlen($codepieces[0]);
926 $pieces = count($codepieces);
927 $reg = array();
928 for ($i = 1; $i < $pieces; $i++) {
929 $tag = strtolower(strtok(substr($html_code,$len+1,10),' '));
930 $len += strlen($tag)+strlen($codepieces[$i])+2;
931 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
932 $attributes = $this->get_tag_attributes($reg[0]); // Fetches the attributes for the tag
933 $imageData = array();
934
935 // Finds the src or background attribute
936 $imageData['ref'] = ($attributes['src'] ? $attributes['src'] : $attributes['background']);
937 if ($imageData['ref']) {
938 // find out if the value had quotes around it
939 $imageData['quotes'] = (substr($codepieces[$i], strpos($codepieces[$i], $imageData['ref'])-1,1) == '"') ? '"' : '';
940 // subst_str is the string to look for, when substituting lateron
941 $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
942 if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
943 $this->image_fullpath_list .= "|".$imageData['subst_str']."|";
944 $imageData['absRef'] = $this->absRef($imageData['ref']);
945 $imageData['tag'] = $tag;
946 $imageData['use_jumpurl'] = $attributes['dmailerping']?1:0;
947 $this->theParts['html']['media'][] = $imageData;
948 }
949 }
950 }
951
952 // Extracting stylesheets
953 $attribRegex = $this->tag_regex(array('link'));
954 // Split the document by the beginning of the above tags
955 $codepieces = preg_split($attribRegex, $html_code);
956 $pieces = count($codepieces);
957 for ($i = 1; $i < $pieces; $i++) {
958 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
959 // fetches the attributes for the tag
960 $attributes = $this->get_tag_attributes($reg[0]);
961 $imageData = array();
962 if (strtolower($attributes['rel']) == 'stylesheet' && $attributes['href']) {
963 // Finds the src or background attribute
964 $imageData['ref'] = $attributes['href'];
965 // Finds out if the value had quotes around it
966 $imageData['quotes'] = (substr($codepieces[$i],strpos($codepieces[$i], $imageData['ref'])-1,1) == '"') ? '"' : '';
967 // subst_str is the string to look for, when substituting lateron
968 $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
969 if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
970 $this->image_fullpath_list .= "|".$imageData["subst_str"]."|";
971 $imageData['absRef'] = $this->absRef($imageData["ref"]);
972 $this->theParts['html']['media'][] = $imageData;
973 }
974 }
975 }
976
977 // fixes javascript rollovers
978 $codepieces = explode('.src', $html_code);
979 $pieces = count($codepieces);
980 $expr = '/^[^'.quotemeta('"').quotemeta("'").']*/';
981 for($i = 1; $i < $pieces; $i++) {
982 $temp = $codepieces[$i];
983 $temp = trim(str_replace('=','',trim($temp)));
984 preg_match($expr,substr($temp,1,strlen($temp)),$reg);
985 $imageData['ref'] = $reg[0];
986 $imageData['quotes'] = substr($temp,0,1);
987 // subst_str is the string to look for, when substituting lateron
988 $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
989 $theInfo = $this->split_fileref($imageData['ref']);
990
991 switch ($theInfo['fileext']) {
992 case 'gif':
993 case 'jpeg':
994 case 'jpg':
995 if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
996 $this->image_fullpath_list .= "|".$imageData['subst_str']."|";
997 $imageData['absRef'] = $this->absRef($imageData['ref']);
998 $this->theParts['html']['media'][] = $imageData;
999 }
1000 break;
1001 }
1002 }
1003 }
1004
1005
1006 /**
1007 * extracts all hyper-links from $this->theParts["html"]["content"]
1008 *
1009 * @return void
1010 */
1011 public function extractHyperLinks() {
1012 $html_code = $this->theParts['html']['content'];
1013 $attribRegex = $this->tag_regex(array('a','form','area'));
1014 $codepieces = preg_split($attribRegex, $html_code); // Splits the document by the beginning of the above tags
1015 $len = strlen($codepieces[0]);
1016 $pieces = count($codepieces);
1017 for($i = 1; $i < $pieces; $i++) {
1018 $tag = strtolower(strtok(substr($html_code,$len+1,10)," "));
1019 $len += strlen($tag) + strlen($codepieces[$i]) + 2;
1020
1021 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
1022 // Fetches the attributes for the tag
1023 $attributes = $this->get_tag_attributes($reg[0]);
1024 $hrefData = array();
1025 $hrefData['ref'] = ($attributes['href'] ? $attributes['href'] : $hrefData['ref'] = $attributes['action']);
1026 if ($hrefData['ref']) {
1027 // Finds out if the value had quotes around it
1028 $hrefData['quotes'] = (substr($codepieces[$i],strpos($codepieces[$i], $hrefData["ref"])-1,1) == '"') ? '"' : '';
1029 // subst_str is the string to look for, when substituting lateron
1030 $hrefData['subst_str'] = $hrefData['quotes'].$hrefData['ref'].$hrefData['quotes'];
1031 if ($hrefData['ref'] && substr(trim($hrefData['ref']),0,1) != "#" && !strstr($this->href_fullpath_list,"|".$hrefData['subst_str']."|")) {
1032 $this->href_fullpath_list .= "|".$hrefData['subst_str']."|";
1033 $hrefData['absRef'] = $this->absRef($hrefData['ref']);
1034 $hrefData['tag'] = $tag;
1035 $this->theParts['html']['hrefs'][] = $hrefData;
1036 }
1037 }
1038 }
1039 // Extracts TYPO3 specific links made by the openPic() JS function
1040 $codepieces = explode("onClick=\"openPic('", $html_code);
1041 $pieces = count($codepieces);
1042 for($i = 1; $i < $pieces; $i++) {
1043 $showpic_linkArr = explode("'",$codepieces[$i]);
1044 $hrefData['ref'] = $showpic_linkArr[0];
1045 if ($hrefData['ref']) {
1046 $hrefData['quotes'] = "'";
1047 // subst_str is the string to look for, when substituting lateron
1048 $hrefData['subst_str'] = $hrefData['quotes'].$hrefData['ref'].$hrefData['quotes'];
1049 if ($hrefData['ref'] && !strstr($this->href_fullpath_list,"|".$hrefData['subst_str']."|")) {
1050 $this->href_fullpath_list .= "|".$hrefData['subst_str']."|";
1051 $hrefData['absRef'] = $this->absRef($hrefData['ref']);
1052 $this->theParts['html']['hrefs'][] = $hrefData;
1053 }
1054 }
1055 }
1056 }
1057
1058
1059 /**
1060 * extracts all media-links from $this->theParts["html"]["content"]
1061 *
1062 * @return array two-dimensional array with information about each frame
1063 */
1064 public function extractFramesInfo() {
1065 $htmlCode = $this->theParts['html']['content'];
1066 $info = array();
1067 if (strpos(' '.$htmlCode,'<frame ')) {
1068 $attribRegex = $this->tag_regex('frame');
1069 // Splits the document by the beginning of the above tags
1070 $codepieces = preg_split($attribRegex, $htmlCode, 1000000);
1071 $pieces = count($codepieces);
1072 for($i = 1; $i < $pieces; $i++) {
1073 $dummy = preg_match('/[^>]*/', $codepieces[$i], $reg);
1074 // Fetches the attributes for the tag
1075 $attributes = $this->get_tag_attributes($reg[0]);
1076 $frame = array();
1077 $frame['src'] = $attributes['src'];
1078 $frame['name'] = $attributes['name'];
1079 $frame['absRef'] = $this->absRef($frame['src']);
1080 $info[] = $frame;
1081 }
1082 return $info;
1083 }
1084 }
1085
1086
1087 /**
1088 * This function substitutes the media-references in $this->theParts["html"]["content"]
1089 *
1090 * @param boolean $absolute: If true, then the refs are substituted with http:// ref's indstead of Content-ID's (cid).
1091 * @return void
1092 */
1093 public function substMediaNamesInHTML($absolute) {
1094 if (is_array($this->theParts['html']['media'])) {
1095 foreach ($this->theParts['html']['media'] as $key => $val) {
1096 if ($val['use_jumpurl'] && $this->jumperURL_prefix) {
1097 $subst = $this->jumperURL_prefix.t3lib_div::rawUrlEncodeFP($val['absRef']);
1098 } else {
1099 $subst = ($absolute) ? $val['absRef'] : 'cid:part'.$key.'.'.$this->messageid;
1100 }
1101 $this->theParts['html']['content'] = str_replace(
1102 $val['subst_str'],
1103 $val['quotes'] . $subst . $val['quotes'],
1104 $this->theParts['html']['content']);
1105 }
1106 }
1107 if (!$absolute) {
1108 $this->fixRollOvers();
1109 }
1110 }
1111
1112
1113 /**
1114 * This function substitutes the hrefs in $this->theParts["html"]["content"]
1115 *
1116 * @return void
1117 */
1118 public function substHREFsInHTML() {
1119 if (!is_array($this->theParts['html']['hrefs'])) return;
1120 foreach ($this->theParts['html']['hrefs'] as $key => $val) {
1121 // Form elements cannot use jumpurl!
1122 if ($this->jumperURL_prefix && $val['tag'] != 'form') {
1123 if ($this->jumperURL_useId) {
1124 $substVal = $this->jumperURL_prefix.$key;
1125 } else {
1126 $substVal = $this->jumperURL_prefix.t3lib_div::rawUrlEncodeFP($val['absRef']);
1127 }
1128 } else {
1129 $substVal = $val['absRef'];
1130 }
1131 $this->theParts['html']['content'] = str_replace(
1132 $val['subst_str'],
1133 $val['quotes'] . $substVal . $val['quotes'],
1134 $this->theParts['html']['content']);
1135 }
1136 }
1137
1138
1139 /**
1140 * This substitutes the http:// urls in plain text with links
1141 *
1142 * @param string $content: the content to use to substitute
1143 * @return string the changed content
1144 */
1145 public function substHTTPurlsInPlainText($content) {
1146 if (!$this->jumperURL_prefix) return $content;
1147
1148 $textpieces = explode("http://", $content);
1149 $pieces = count($textpieces);
1150 $textstr = $textpieces[0];
1151 for($i = 1; $i<$pieces; $i++) {
1152 $len = strcspn($textpieces[$i],chr(32).TAB.CRLF);
1153 if (trim(substr($textstr,-1)) == '' && $len) {
1154 $lastChar = substr($textpieces[$i],$len-1,1);
1155 if (!preg_match('/[A-Za-z0-9\/#]/',$lastChar)) {
1156 $len--;
1157 }
1158
1159 $parts = array();
1160 $parts[0] = "http://".substr($textpieces[$i],0,$len);
1161 $parts[1] = substr($textpieces[$i],$len);
1162
1163 if ($this->jumperURL_useId) {
1164 $this->theParts['plain']['link_ids'][$i] = $parts[0];
1165 $parts[0] = $this->jumperURL_prefix.'-'.$i;
1166 } else {
1167 $parts[0] = $this->jumperURL_prefix.t3lib_div::rawUrlEncodeFP($parts[0]);
1168 }
1169 $textstr .= $parts[0].$parts[1];
1170 } else {
1171 $textstr .= 'http://'.$textpieces[$i];
1172 }
1173 }
1174 return $textstr;
1175 }
1176
1177
1178 /**
1179 * JavaScript rollOvers cannot support graphics inside of mail.
1180 * If these exists we must let them refer to the absolute url. By the way:
1181 * Roll-overs seems to work only on some mail-readers and so far I've seen it
1182 * work on Netscape 4 message-center (but not 4.5!!)
1183 *
1184 * @return void
1185 */
1186 public function fixRollOvers() {
1187 $newContent = '';
1188 $items = explode('.src',$this->theParts['html']['content']);
1189 if (count($items) <= 1) return;
1190
1191 foreach($items as $key => $part) {
1192 $sub = substr($part, 0, 200);
1193 if (preg_match('/cid:part[^ "\']*/',$sub,$reg)) {
1194 // The position of the string
1195 $thePos = strpos($part,$reg[0]);
1196 // Finds the id of the media...
1197 preg_match('/cid:part([^\.]*).*/',$sub,$reg2);
1198 $theSubStr = $this->theParts['html']['media'][intval($reg2[1])]['absRef'];
1199 if ($thePos && $theSubStr) {
1200 // ... and substitutes the javaScript rollover image with this instead
1201 // If the path is NOT and url, the reference is set to nothing
1202 if (!strpos(' '.$theSubStr, 'http://')) {
1203 $theSubStr = 'http://';
1204 }
1205 $part = substr($part, 0, $thePos) . $theSubStr . substr($part,$thePos+strlen($reg[0]),strlen($part));
1206 }
1207 }
1208 $newContent .= $part . ((($key+1) != count($items)) ? '.src' : '');
1209 }
1210 $this->theParts['html']['content'] = $newContent;
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220 /*******************************************
1221 *
1222 * File and URL-functions
1223 *
1224 *******************************************/
1225
1226 /**
1227 * Returns base64-encoded content, which is broken every 76 character
1228 *
1229 * @param string $inputstr: the string to encode
1230 * @return string the encoded string
1231 */
1232 public function makeBase64($inputstr) {
1233 return chunk_split(base64_encode($inputstr));
1234 }
1235
1236
1237 /**
1238 * reads the URL or file and determines the Content-type by either guessing or opening a connection to the host
1239 *
1240 * @param string $url: the URL to get information of
1241 * @return mixed either false or the array with information
1242 */
1243 public function getExtendedURL($url) {
1244 $res = array();
1245 $res['content'] = $this->getURL($url);
1246 if (!$res['content']) return false;
1247 $pathInfo = parse_url($url);
1248 $fileInfo = $this->split_fileref($pathInfo['path']);
1249 switch ($fileInfo['fileext']) {
1250 case 'gif':
1251 case 'png':
1252 $res['content_type'] = 'image/'.$fileInfo['fileext'];
1253 break;
1254 case 'jpg':
1255 case 'jpeg':
1256 $res['content_type'] = 'image/jpeg';
1257 break;
1258 case 'html':
1259 case 'htm':
1260 $res['content_type'] = 'text/html';
1261 break;
1262 case 'css':
1263 $res['content_type'] = 'text/css';
1264 break;
1265 case 'swf':
1266 $res['content_type'] = 'application/x-shockwave-flash';
1267 break;
1268 default:
1269 $res['content_type'] = $this->getMimeType($url);
1270 }
1271 return $res;
1272 }
1273
1274
1275 /**
1276 * Adds HTTP user and password (from $this->http_username) to a URL
1277 *
1278 * @param string $url: the URL
1279 * @return string the URL with the added values
1280 */
1281 public function addUserPass($url) {
1282 $user = $this->http_username;
1283 $pass = $this->http_password;
1284 $matches = array();
1285 if ($user && $pass && preg_match('/^(https?:\/\/)/', $url, $matches)) {
1286 return $matches[1].$user.':'.$pass.'@'.substr($url,strlen($matches[1]));
1287 }
1288 return $url;
1289 }
1290
1291
1292 /**
1293 * reads a url or file
1294 *
1295 * @param string $url: the URL to fetch
1296 * @return string the content of the URL
1297 */
1298 public function getURL($url) {
1299 $url = $this->addUserPass($url);
1300 return t3lib_div::getURL($url);
1301 }
1302
1303
1304 /**
1305 * reads a url or file and strips the HTML-tags AND removes all
1306 * empty lines. This is used to read plain-text out of a HTML-page
1307 *
1308 * @param string $url: the URL to load
1309 * @return the content
1310 */
1311 public function getStrippedURL($url) {
1312 $content = '';
1313 if ($fd = fopen($url, "rb")) {
1314 while (!feof($fd)) {
1315 $line = fgetss($fd, 5000);
1316 if (trim($line)) {
1317 $content .= trim($line) . LF;
1318 }
1319 }
1320 fclose($fd);
1321 }
1322 return $content;
1323 }
1324
1325
1326 /**
1327 * This function returns the mime type of the file specified by the url
1328 *
1329 * @param string $url: the url
1330 * @return string $mimeType: the mime type found in the header
1331 */
1332 public function getMimeType($url) {
1333 $mimeType = '';
1334 $headers = trim(t3lib_div::getURL($url, 2));
1335 if ($headers) {
1336 $matches = array();
1337 if (preg_match('/(Content-Type:[\s]*)([a-zA-Z_0-9\/\-\.\+]*)([\s]|$)/', $headers, $matches)) {
1338 $mimeType = trim($matches[2]);
1339 }
1340 }
1341 return $mimeType;
1342 }
1343
1344
1345 /**
1346 * Returns the absolute address of a link. This is based on
1347 * $this->theParts["html"]["path"] being the root-address
1348 *
1349 * @param string $ref: address to use
1350 * @return string the absolute address
1351 */
1352 public function absRef($ref) {
1353 $ref = trim($ref);
1354 $info = parse_url($ref);
1355 if ($info['scheme']) {
1356 return $ref;
1357 } elseif (preg_match('/^\//',$ref)) {
1358 $addr = parse_url($this->theParts['html']['path']);
1359 return $addr['scheme'].'://'.$addr['host'].($addr['port']?':'.$addr['port']:'').$ref;
1360 } else {
1361 // If the reference is relative, the path is added, in order for us to fetch the content
1362 return $this->theParts['html']['path'] . $ref;
1363 }
1364 }
1365
1366
1367 /**
1368 * Returns information about a file reference
1369 *
1370 * @param string $fileref: the file to use
1371 * @return array path, filename, filebody, fileext
1372 */
1373 public function split_fileref($fileref) {
1374 $info = array();
1375 if (preg_match('/(.*\/)(.*)$/', $fileref, $reg)) {
1376 $info['path'] = $reg[1];
1377 $info['file'] = $reg[2];
1378 } else {
1379 $info['path'] = '';
1380 $info['file'] = $fileref;
1381 }
1382 $reg = '';
1383 if (preg_match('/(.*)\.([^\.]*$)/', $info['file'], $reg)) {
1384 $info['filebody'] = $reg[1];
1385 $info['fileext'] = strtolower($reg[2]);
1386 $info['realFileext'] = $reg[2];
1387 } else {
1388 $info['filebody'] = $info['file'];
1389 $info['fileext'] = '';
1390 }
1391 return $info;
1392 }
1393
1394
1395 /**
1396 * Returns an array with file or url-information
1397 *
1398 * @param string $path: url to check
1399 * @return array information about the path / URL
1400 */
1401 public function extParseUrl($path) {
1402 $res = parse_url($path);
1403 preg_match('/(.*\/)([^\/]*)$/', $res['path'], $reg);
1404 $res['filepath'] = $reg[1];
1405 $res['filename'] = $reg[2];
1406 return $res;
1407 }
1408
1409
1410 /**
1411 * Creates a regular expression out of a list of tags
1412 *
1413 * @param mixed $tagArray: the list of tags (either as array or string if it is one tag)
1414 * @return string the regular expression
1415 */
1416 public function tag_regex($tags) {
1417 $tags = (!is_array($tags) ? array($tags) : $tags);
1418 $regexp = '/';
1419 $c = count($tags);
1420 foreach($tags as $tag) {
1421 $c--;
1422 $regexp .= '<' . $tag . '[[:space:]]' . (($c) ? '|' : '');
1423 }
1424 return $regexp . '/i';
1425 }
1426
1427
1428 /**
1429 * This function analyzes a HTML tag
1430 * If an attribute is empty (like OPTION) the value of that key is just empty. Check it with is_set();
1431 *
1432 * @param string $tag: is either like this "<TAG OPTION ATTRIB=VALUE>" or
1433 * this " OPTION ATTRIB=VALUE>" which means you can omit the tag-name
1434 * @return array array with attributes as keys in lower-case
1435 */
1436 public function get_tag_attributes($tag) {
1437 $attributes = array();
1438 $tag = ltrim(preg_replace('/^<[^ ]*/','',trim($tag)));
1439 $tagLen = strlen($tag);
1440 $safetyCounter = 100;
1441 // Find attribute
1442 while ($tag) {
1443 $value = '';
1444 $reg = preg_split('/[[:space:]=>]/', $tag, 2);
1445 $attrib = $reg[0];
1446
1447 $tag = ltrim(substr($tag,strlen($attrib),$tagLen));
1448 if (substr($tag,0,1) == '=') {
1449 $tag = ltrim(substr($tag,1,$tagLen));
1450 if (substr($tag,0,1) == '"') {
1451 // Quotes around the value
1452 $reg = explode('"',substr($tag,1,$tagLen),2);
1453 $tag = ltrim($reg[1]);
1454 $value = $reg[0];
1455 } else {
1456 // No quotes around value
1457 preg_match('/^([^[:space:]>]*)(.*)/',$tag,$reg);
1458 $value = trim($reg[1]);
1459 $tag = ltrim($reg[2]);
1460 if (substr($tag,0,1) == '>') {
1461 $tag = '';
1462 }
1463 }
1464 }
1465 $attributes[strtolower($attrib)] = $value;
1466 $safetyCounter--;
1467 if ($safetyCounter < 0) break;
1468 }
1469 return $attributes;
1470 }
1471
1472
1473 /**
1474 * Implementation of quoted-printable encode.
1475 * This function was a duplicate of t3lib_div::quoted_printable, thus it's going to be removed.
1476 * Deprecated since TYPO3 4.0
1477 *
1478 * @param string Content to encode
1479 * @return string The QP encoded string
1480 * @deprecated since TYPO3 4.0, will be removed in TYPO3 4.6
1481 */
1482 public function quoted_printable($string) {
1483 t3lib_div::logDeprecatedFunction();
1484
1485 return t3lib_div::quoted_printable($string, 76);
1486 }
1487
1488
1489 /**
1490 * Converts a name field
1491 * Deprecated since TYPO3 4.0
1492 *
1493 * @param string $name: the name
1494 * @return string the name
1495 * @deprecated since TYPO3 4.0, will be removed in TYPO3 4.6
1496 */
1497 public function convertName($name) {
1498 t3lib_div::logDeprecatedFunction();
1499
1500 return $name;
1501 }
1502 }
1503
1504 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_htmlmail.php']) {
1505 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_htmlmail.php']);
1506 }
1507
1508 ?>