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