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