4 * Output an HTML message, modifying it slightly to make sure it plays nice
5 * with the rest of our web framework.
7 * Copyright (c) 2005-2010 by the citadel.org team
9 * This program is 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 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
24 #include "webserver.h"
28 * Strip surrounding single or double quotes from a string.
30 void stripquotes(char *s)
39 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
47 * Check to see if a META tag has overridden the declared MIME character set.
49 * charset Character set name (left unchanged if we don't do anything)
50 * meta_http_equiv Content of the "http-equiv" portion of the META tag
51 * meta_content Content of the "content" portion of the META tag
53 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
59 if (!meta_http_equiv) return;
60 if (!meta_content) return;
63 if (strcasecmp(meta_http_equiv, "Content-type")) return;
65 ptr = strchr(meta_content, ';');
68 safestrncpy(buf, ++ptr, sizeof buf);
70 if (!strncasecmp(buf, "charset=", 8)) {
71 strcpy(charset, &buf[8]);
74 * The brain-damaged webmail program in Microsoft Exchange declares
75 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
76 * treats "unicode" as an alias for "UTF-16" so we have to manually
77 * fix this here, otherwise messages generated in Exchange webmail
78 * show up as a big pile of weird characters.
80 if (!strcasecmp(charset, "unicode")) {
81 strcpy(charset, "UTF-8");
84 /* Remove wandering punctuation */
85 if ((ptr=strchr(charset, '\"'))) *ptr = 0;
93 * Sanitize and enhance an HTML message for display.
94 * Also convert weird character sets to UTF-8 if necessary.
95 * Also fixup img src="cid:..." type inline images to fetch the image
98 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
104 StrBuf *converted_msg;
105 int buffer_length = 1;
107 int content_length = 0;
108 char new_window[SIZ];
112 int script_start_pos = (-1);
116 StrBuf *BodyArea = NULL;
118 iconv_t ic = (iconv_t)(-1) ;
119 char *ibuf; /* Buffer of characters to be converted */
120 char *obuf; /* Buffer for converted characters */
121 size_t ibuflen; /* Length of input buffer */
122 size_t obuflen; /* Length of output buffer */
123 char *osav; /* Saved pointer to output buffer */
128 safestrncpy(charset, supplied_charset, sizeof charset);
130 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
132 if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
133 line_length = strlen(buf);
134 buffer_length = content_length + line_length + 2;
135 ptr = realloc(msg, buffer_length);
137 StrBufAppendPrintf(Target, "<b>");
138 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
141 StrBufAppendPrintf(Target, "</b><br /><br />\n");
142 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
149 strcpy(&msg[content_length], buf);
150 content_length += line_length;
151 strcpy(&msg[content_length], "\n");
155 content_length = StrLength(Source);
157 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
158 buffer_length = content_length;
161 /** Do a first pass to isolate the message body */
164 msgend = &msg[content_length];
166 while (ptr < msgend) {
168 /** Advance to next tag */
169 ptr = strchr(ptr, '<');
170 if ((ptr == NULL) || (ptr >= msgend)) break;
172 if ((ptr == NULL) || (ptr >= msgend)) break;
175 * Look for META tags. Some messages (particularly in
176 * Asian locales) illegally declare a message's character
177 * set in the HTML instead of in the MIME headers. This
178 * is wrong but we have to work around it anyway.
180 if (!strncasecmp(ptr, "META", 4)) {
186 char *meta_http_equiv;
190 meta_start = &ptr[4];
191 meta_end = strchr(ptr, '>');
192 if ((meta_end != NULL) && (meta_end <= msgend)) {
193 meta_length = meta_end - meta_start + 1;
194 meta = malloc(meta_length + 1);
195 safestrncpy(meta, meta_start, meta_length);
196 meta[meta_length] = 0;
198 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
199 meta_http_equiv = strdup(&meta[11]);
200 spaceptr = strchr(meta_http_equiv, ' ');
201 if (spaceptr != NULL) {
203 meta_content = strdup(++spaceptr);
204 if (!strncasecmp(meta_content, "content=", 8)) {
205 strcpy(meta_content, &meta_content[8]);
206 stripquotes(meta_http_equiv);
207 stripquotes(meta_content);
208 extract_charset_from_meta(charset,
209 meta_http_equiv, meta_content);
213 free(meta_http_equiv);
220 * Any of these tags cause everything up to and including
221 * the tag to be removed.
223 if ( (!strncasecmp(ptr, "HTML", 4))
224 ||(!strncasecmp(ptr, "HEAD", 4))
225 ||(!strncasecmp(ptr, "/HEAD", 5))
226 ||(!strncasecmp(ptr, "BODY", 4)) ) {
229 if (!strncasecmp(ptr, "BODY", 4)) {
232 ptr = strchr(ptr, '>');
233 if ((ptr == NULL) || (ptr >= msgend)) break;
234 if ((pBody != NULL) && (ptr - pBody > 4)) {
236 char *cid_start, *cid_end;
240 while ((isspace(*pBody)) && (pBody < ptr))
242 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
245 src = strstr(pBody, "cid:");
249 while ((*cid_end != '"') &&
250 !isspace(*cid_end) &&
254 /* copy tag and attributes up to src="cid: */
255 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
257 /* add in /webcit/mimepart/<msgno>/CID/
258 trailing / stops dumb URL filters getting excited */
259 StrBufAppendPrintf(BodyArea,
260 "/webcit/mimepart/%d/",msgnum);
261 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
263 if (ptr - cid_end > 0)
264 StrBufAppendBufPlain(BodyArea,
269 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
274 if ((ptr == NULL) || (ptr >= msgend)) break;
279 * Any of these tags cause everything including and following
280 * the tag to be removed.
282 if ( (!strncasecmp(ptr, "/HTML", 5))
283 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
292 if (msgstart > msg) {
293 strcpy(msg, msgstart);
296 /* Now go through the message, parsing tags as necessary. */
297 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
300 /** Convert foreign character sets to UTF-8 if necessary. */
302 if ( (strcasecmp(charset, "us-ascii"))
303 && (strcasecmp(charset, "UTF-8"))
304 && (strcasecmp(charset, ""))
306 lprintf(9, "Converting %s to UTF-8\n", charset);
307 ctdl_iconv_open("UTF-8", charset, &ic);
308 if (ic == (iconv_t)(-1) ) {
309 lprintf(5, "%s:%d iconv_open() failed: %s\n",
310 __FILE__, __LINE__, strerror(errno));
313 if (Source == NULL) {
314 if (ic != (iconv_t)(-1) ) {
316 ibuflen = content_length;
317 obuflen = content_length + (content_length / 2) ;
318 obuf = (char *) malloc(obuflen);
320 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
321 content_length = content_length + (content_length / 2) - obuflen;
322 osav[content_length] = 0;
329 if (ic != (iconv_t)(-1) ) {
330 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
331 StrBufConvert(Source, Buf, &ic);
334 msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
341 * At this point, the message has been stripped down to
342 * only the content inside the <BODY></BODY> tags, and has
343 * been converted to UTF-8 if it was originally in a foreign
344 * character set. The text is also guaranteed to be null
348 if (converted_msg == NULL) {
349 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
353 if (BodyArea != NULL) {
354 StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);
355 StrBufAppendBuf(converted_msg, BodyArea, 0);
356 StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
359 msgend = strchr(msg, 0);
360 while (ptr < msgend) {
362 /** Try to sanitize the html of any rogue scripts */
363 if (!strncasecmp(ptr, "<script", 7)) {
364 if (scriptlevel == 0) {
365 script_start_pos = StrLength(converted_msg);
369 if (!strncasecmp(ptr, "</script", 8)) {
374 * Change mailto: links to WebCit mail, by replacing the
375 * link with one that points back to our mail room. Due to
376 * the way we parse URL's, it'll even handle mailto: links
377 * that have "?subject=" in them.
379 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
380 content_length += 64;
381 StrBufAppendPrintf(converted_msg,
382 "<a href=\"display_enter?force_room=_MAIL_?recp=");
387 /** Make external links open in a separate window */
388 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
391 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
392 && ((strchr(ptr, '/') < strchr(ptr, '>')))
394 /* open external links to new window */
395 StrBufAppendPrintf(converted_msg, new_window);
398 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
399 content_length += 64;
400 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
404 StrBufAppendPrintf(converted_msg, "<a href=\"");
408 /** Fixup <img src="cid:... ...> to fetch the mime part */
409 else if (!strncasecmp(ptr, "<img ", 5)) {
410 char *cid_start, *cid_end;
411 char* tag_end=strchr(ptr,'>');
413 /* FIXME - handle this situation (maybe someone opened an <img cid...
414 * and then ended the message)
417 lprintf(9, "tag_end is null and ptr is:\n");
418 lprintf(9, "%s\n", ptr);
419 lprintf(9, "Theoretical bytes remaining: %d\n", msgend - ptr);
422 src=strstr(ptr, "src=\"cid:");
428 && (cid_start=strchr(src,':'))
429 && (cid_end=strchr(cid_start,'"'))
430 && (cid_end < tag_end)
432 /* copy tag and attributes up to src="cid: */
433 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
436 /* add in /webcit/mimepart/<msgno>/CID/
437 trailing / stops dumb URL filters getting excited */
438 StrBufAppendPrintf(converted_msg,
439 " src=\"/webcit/mimepart/%d/",msgnum);
440 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
441 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
445 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
450 * Turn anything that looks like a URL into a real link, as long
451 * as it's not inside a tag already
453 else if ( (brak == 0) && (alevel == 0)
454 && (!strncasecmp(ptr, "http://", 7))) {
455 /** Find the end of the link */
459 strlenptr = strlen(ptr);
460 for (i=0; i<=strlenptr; ++i) {
474 /* did s.b. send us an entity? */
476 if ((ptr[i+2] ==';') ||
483 if (linklen > 0) break;
492 linkedchar = ptr[len];
494 /* spot for some subject strings tinymce tends to give us. */
495 ltreviewptr = strchr(ptr, '<');
496 if (ltreviewptr != NULL) {
498 linklen = ltreviewptr - ptr;
501 nbspreviewptr = strstr(ptr, " ");
502 if (nbspreviewptr != NULL) {
503 /* nbspreviewptr = '\0'; */
504 linklen = nbspreviewptr - ptr;
506 if (ltreviewptr != 0)
509 ptr[len] = linkedchar;
511 content_length += (32 + linklen);
512 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
513 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
514 StrBufAppendPrintf(converted_msg, "\">");
515 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
517 StrBufAppendPrintf(converted_msg, "</A>");
521 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
526 if ((ptr >= msg) && (ptr <= msgend)) {
528 * We need to know when we're inside a tag,
529 * so we don't turn things that look like URL's into
530 * links, when they're already links - or image sources.
532 if ((ptr != NULL) && (ptr > msg) && (*(ptr-1) == '<')) {
535 if ((ptr != NULL) && (ptr > msg) && (*(ptr-1) == '>')) {
537 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
538 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
539 script_start_pos = (-1);
543 !strncasecmp(ptr, "</A>", 3))
548 if (BodyArea != NULL) {
549 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
550 FreeStrBuf(&BodyArea);
553 /** uncomment these two lines to override conversion */
554 /** memcpy(converted_msg, msg, content_length); */
555 /** output_length = content_length; */
557 /** Output our big pile of markup */
558 StrBufAppendBuf(Target, converted_msg, 0);
560 BAIL: /** A little trailing vertical whitespace... */
561 StrBufAppendPrintf(Target, "<br /><br />\n");
563 /** Now give back the memory */
564 FreeStrBuf(&converted_msg);
565 if ((msg != NULL) && (Source == NULL)) free(msg);
574 * Look for URL's embedded in a buffer and make them linkable. We use a
575 * target window in order to keep the Citadel session in its own window.
577 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
579 int len, UrlLen, Offset, TrailerLen;
580 const char *start, *end, *pos;
585 len = StrLength(Source);
586 end = ChrPtr(Source) + len;
587 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
588 if (!strncasecmp(pos, "http://", 7))
590 else if (!strncasecmp(pos, "ftp://", 6))
595 StrBufAppendBuf(Target, Source, 0);
600 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
601 if ( (!isprint(*pos))
620 UrlLen = end - start;
621 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
623 Offset = start - ChrPtr(Source);
625 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
626 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
627 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
628 QU, RB, ChrPtr(WrkBuf), LB, RB);
630 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
632 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
636 void url(char *buf, size_t bufsize)
638 int len, UrlLen, Offset, TrailerLen, outpos;
639 char *start, *end, *pos;
646 lprintf(1, "URL: content longer than buffer!");
650 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
651 if (!strncasecmp(pos, "http://", 7))
653 if (!strncasecmp(pos, "ftp://", 6))
660 for (pos = buf+len; pos > start; --pos) {
661 if ( (!isprint(*pos))
680 UrlLen = end - start;
681 if (UrlLen > sizeof(urlbuf)){
682 lprintf(1, "URL: content longer than buffer!");
685 memcpy(urlbuf, start, UrlLen);
686 urlbuf[UrlLen] = '\0';
688 Offset = start - buf;
689 if ((Offset != 0) && (Offset < sizeof(outbuf)))
690 memcpy(outbuf, buf, Offset);
691 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
692 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
693 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
694 if (outpos >= sizeof(outbuf) - Offset) {
695 lprintf(1, "URL: content longer than buffer!");
699 TrailerLen = len - (end - start);
701 memcpy(outbuf + Offset + outpos, end, TrailerLen);
702 if (Offset + outpos + TrailerLen > bufsize) {
703 lprintf(1, "URL: content longer than buffer!");
706 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
707 *(buf + Offset + outpos + TrailerLen) = '\0';