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