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