2 * Output an HTML message, modifying it slightly to make sure it plays nice
3 * with the rest of our web framework.
5 * Copyright (c) 2005-2010 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
22 #include "webserver.h"
26 * Strip surrounding single or double quotes from a string.
28 void stripquotes(char *s)
37 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
45 * Check to see if a META tag has overridden the declared MIME character set.
47 * charset Character set name (left unchanged if we don't do anything)
48 * meta_http_equiv Content of the "http-equiv" portion of the META tag
49 * meta_content Content of the "content" portion of the META tag
51 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
57 if (!meta_http_equiv) return;
58 if (!meta_content) return;
61 if (strcasecmp(meta_http_equiv, "Content-type")) return;
63 ptr = strchr(meta_content, ';');
66 safestrncpy(buf, ++ptr, sizeof buf);
68 if (!strncasecmp(buf, "charset=", 8)) {
69 strcpy(charset, &buf[8]);
72 * The brain-damaged webmail program in Microsoft Exchange declares
73 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
74 * treats "unicode" as an alias for "UTF-16" so we have to manually
75 * fix this here, otherwise messages generated in Exchange webmail
76 * show up as a big pile of weird characters.
78 if (!strcasecmp(charset, "unicode")) {
79 strcpy(charset, "UTF-8");
82 /* Remove wandering punctuation */
83 if ((ptr=strchr(charset, '\"'))) *ptr = 0;
91 * Sanitize and enhance an HTML message for display.
92 * Also convert weird character sets to UTF-8 if necessary.
93 * Also fixup img src="cid:..." type inline images to fetch the image
96 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
102 StrBuf *converted_msg;
103 int buffer_length = 1;
105 int content_length = 0;
106 char new_window[SIZ];
110 int script_start_pos = (-1);
114 StrBuf *BodyArea = NULL;
116 iconv_t ic = (iconv_t)(-1) ;
117 char *ibuf; /* Buffer of characters to be converted */
118 char *obuf; /* Buffer for converted characters */
119 size_t ibuflen; /* Length of input buffer */
120 size_t obuflen; /* Length of output buffer */
121 char *osav; /* Saved pointer to output buffer */
126 safestrncpy(charset, supplied_charset, sizeof charset);
128 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
130 if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
131 line_length = strlen(buf);
132 buffer_length = content_length + line_length + 2;
133 ptr = realloc(msg, buffer_length);
135 StrBufAppendPrintf(Target, "<b>");
136 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
139 StrBufAppendPrintf(Target, "</b><br /><br />\n");
140 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
147 strcpy(&msg[content_length], buf);
148 content_length += line_length;
149 strcpy(&msg[content_length], "\n");
153 content_length = StrLength(Source);
155 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
156 buffer_length = content_length;
159 /** Do a first pass to isolate the message body */
162 msgend = &msg[content_length];
164 while (ptr < msgend) {
166 /** Advance to next tag */
167 ptr = strchr(ptr, '<');
168 if ((ptr == NULL) || (ptr >= msgend)) break;
170 if ((ptr == NULL) || (ptr >= msgend)) break;
173 * Look for META tags. Some messages (particularly in
174 * Asian locales) illegally declare a message's character
175 * set in the HTML instead of in the MIME headers. This
176 * is wrong but we have to work around it anyway.
178 if (!strncasecmp(ptr, "META", 4)) {
184 char *meta_http_equiv;
188 meta_start = &ptr[4];
189 meta_end = strchr(ptr, '>');
190 if ((meta_end != NULL) && (meta_end <= msgend)) {
191 meta_length = meta_end - meta_start + 1;
192 meta = malloc(meta_length + 1);
193 safestrncpy(meta, meta_start, meta_length);
194 meta[meta_length] = 0;
196 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
197 meta_http_equiv = strdup(&meta[11]);
198 spaceptr = strchr(meta_http_equiv, ' ');
199 if (spaceptr != NULL) {
201 meta_content = strdup(++spaceptr);
202 if (!strncasecmp(meta_content, "content=", 8)) {
203 strcpy(meta_content, &meta_content[8]);
204 stripquotes(meta_http_equiv);
205 stripquotes(meta_content);
206 extract_charset_from_meta(charset,
207 meta_http_equiv, meta_content);
211 free(meta_http_equiv);
218 * Any of these tags cause everything up to and including
219 * the tag to be removed.
221 if ( (!strncasecmp(ptr, "HTML", 4))
222 ||(!strncasecmp(ptr, "HEAD", 4))
223 ||(!strncasecmp(ptr, "/HEAD", 5))
224 ||(!strncasecmp(ptr, "BODY", 4)) ) {
227 if (!strncasecmp(ptr, "BODY", 4)) {
230 ptr = strchr(ptr, '>');
231 if ((ptr == NULL) || (ptr >= msgend)) break;
232 if ((pBody != NULL) && (ptr - pBody > 4)) {
234 char *cid_start, *cid_end;
238 while ((isspace(*pBody)) && (pBody < ptr))
240 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
243 src = strstr(pBody, "cid:");
247 while ((*cid_end != '"') &&
248 !isspace(*cid_end) &&
252 /* copy tag and attributes up to src="cid: */
253 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
255 /* add in /webcit/mimepart/<msgno>/CID/
256 trailing / stops dumb URL filters getting excited */
257 StrBufAppendPrintf(BodyArea,
258 "/webcit/mimepart/%d/",msgnum);
259 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
261 if (ptr - cid_end > 0)
262 StrBufAppendBufPlain(BodyArea,
267 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
272 if ((ptr == NULL) || (ptr >= msgend)) break;
277 * Any of these tags cause everything including and following
278 * the tag to be removed.
280 if ( (!strncasecmp(ptr, "/HTML", 5))
281 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
290 if (msgstart > msg) {
291 strcpy(msg, msgstart);
294 /* Now go through the message, parsing tags as necessary. */
295 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
298 /** Convert foreign character sets to UTF-8 if necessary. */
300 if ( (strcasecmp(charset, "us-ascii"))
301 && (strcasecmp(charset, "UTF-8"))
302 && (strcasecmp(charset, ""))
304 lprintf(9, "Converting %s to UTF-8\n", charset);
305 ctdl_iconv_open("UTF-8", charset, &ic);
306 if (ic == (iconv_t)(-1) ) {
307 lprintf(5, "%s:%d iconv_open() failed: %s\n",
308 __FILE__, __LINE__, strerror(errno));
311 if (Source == NULL) {
312 if (ic != (iconv_t)(-1) ) {
314 ibuflen = content_length;
315 obuflen = content_length + (content_length / 2) ;
316 obuf = (char *) malloc(obuflen);
318 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
319 content_length = content_length + (content_length / 2) - obuflen;
320 osav[content_length] = 0;
327 if (ic != (iconv_t)(-1) ) {
328 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
329 StrBufConvert(Source, Buf, &ic);
332 msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
339 * At this point, the message has been stripped down to
340 * only the content inside the <BODY></BODY> tags, and has
341 * been converted to UTF-8 if it was originally in a foreign
342 * character set. The text is also guaranteed to be null
346 if (converted_msg == NULL) {
347 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
351 if (BodyArea != NULL) {
352 StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);
353 StrBufAppendBuf(converted_msg, BodyArea, 0);
354 StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
357 msgend = strchr(msg, 0);
358 while (ptr < msgend) {
360 /** Try to sanitize the html of any rogue scripts */
361 if (!strncasecmp(ptr, "<script", 7)) {
362 if (scriptlevel == 0) {
363 script_start_pos = StrLength(converted_msg);
367 if (!strncasecmp(ptr, "</script", 8)) {
372 * Change mailto: links to WebCit mail, by replacing the
373 * link with one that points back to our mail room. Due to
374 * the way we parse URL's, it'll even handle mailto: links
375 * that have "?subject=" in them.
377 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
378 content_length += 64;
379 StrBufAppendPrintf(converted_msg,
380 "<a href=\"display_enter?force_room=_MAIL_?recp=");
385 /** Make external links open in a separate window */
386 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
389 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
390 && ((strchr(ptr, '/') < strchr(ptr, '>')))
392 /* open external links to new window */
393 StrBufAppendPrintf(converted_msg, new_window);
396 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
397 content_length += 64;
398 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
402 StrBufAppendPrintf(converted_msg, "<a href=\"");
406 /** Fixup <img src="cid:... ...> to fetch the mime part */
407 else if (!strncasecmp(ptr, "<img ", 5)) {
408 char *cid_start, *cid_end;
409 char* tag_end=strchr(ptr,'>');
411 /* FIXME - handle this situation (maybe someone opened an <img cid...
412 * and then ended the message)
415 lprintf(9, "tag_end is null and ptr is:\n");
416 lprintf(9, "%s\n", ptr);
417 lprintf(9, "Theoretical bytes remaining: %d\n", msgend - ptr);
420 src=strstr(ptr, "src=\"cid:");
426 && (cid_start=strchr(src,':'))
427 && (cid_end=strchr(cid_start,'"'))
428 && (cid_end < tag_end)
430 /* copy tag and attributes up to src="cid: */
431 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
434 /* add in /webcit/mimepart/<msgno>/CID/
435 trailing / stops dumb URL filters getting excited */
436 StrBufAppendPrintf(converted_msg,
437 " src=\"/webcit/mimepart/%d/",msgnum);
438 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
439 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
443 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
448 * Turn anything that looks like a URL into a real link, as long
449 * as it's not inside a tag already
451 else if ( (brak == 0) && (alevel == 0)
452 && (!strncasecmp(ptr, "http://", 7))) {
453 /** Find the end of the link */
457 strlenptr = strlen(ptr);
458 for (i=0; i<=strlenptr; ++i) {
472 /* did s.b. send us an entity? */
474 if ((ptr[i+2] ==';') ||
481 if (linklen > 0) break;
490 linkedchar = ptr[len];
492 /* spot for some subject strings tinymce tends to give us. */
493 ltreviewptr = strchr(ptr, '<');
494 if (ltreviewptr != NULL) {
496 linklen = ltreviewptr - ptr;
499 nbspreviewptr = strstr(ptr, " ");
500 if (nbspreviewptr != NULL) {
501 /* nbspreviewptr = '\0'; */
502 linklen = nbspreviewptr - ptr;
504 if (ltreviewptr != 0)
507 ptr[len] = linkedchar;
509 content_length += (32 + linklen);
510 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
511 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
512 StrBufAppendPrintf(converted_msg, "\">");
513 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
515 StrBufAppendPrintf(converted_msg, "</A>");
519 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
524 if ((ptr >= msg) && (ptr <= msgend)) {
526 * We need to know when we're inside a tag,
527 * so we don't turn things that look like URL's into
528 * links, when they're already links - or image sources.
530 if ((ptr > msg) && (*(ptr-1) == '<')) {
533 if ((ptr > msg) && (*(ptr-1) == '>')) {
535 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
536 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
537 script_start_pos = (-1);
540 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
544 if (BodyArea != NULL) {
545 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
546 FreeStrBuf(&BodyArea);
549 /** uncomment these two lines to override conversion */
550 /** memcpy(converted_msg, msg, content_length); */
551 /** output_length = content_length; */
553 /** Output our big pile of markup */
554 StrBufAppendBuf(Target, converted_msg, 0);
556 BAIL: /** A little trailing vertical whitespace... */
557 StrBufAppendPrintf(Target, "<br /><br />\n");
559 /** Now give back the memory */
560 FreeStrBuf(&converted_msg);
561 if ((msg != NULL) && (Source == NULL)) free(msg);
570 * Look for URL's embedded in a buffer and make them linkable. We use a
571 * target window in order to keep the Citadel session in its own window.
573 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
575 int len, UrlLen, Offset, TrailerLen;
576 const char *start, *end, *pos;
581 len = StrLength(Source);
582 end = ChrPtr(Source) + len;
583 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
584 if (!strncasecmp(pos, "http://", 7))
586 else if (!strncasecmp(pos, "ftp://", 6))
591 StrBufAppendBuf(Target, Source, 0);
596 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
597 if ( (!isprint(*pos))
616 UrlLen = end - start;
617 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
619 Offset = start - ChrPtr(Source);
621 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
622 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
623 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
624 QU, RB, ChrPtr(WrkBuf), LB, RB);
626 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
628 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
632 void url(char *buf, size_t bufsize)
634 int len, UrlLen, Offset, TrailerLen, outpos;
635 char *start, *end, *pos;
642 lprintf(1, "URL: content longer than buffer!");
646 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
647 if (!strncasecmp(pos, "http://", 7))
649 if (!strncasecmp(pos, "ftp://", 6))
656 for (pos = buf+len; pos > start; --pos) {
657 if ( (!isprint(*pos))
676 UrlLen = end - start;
677 if (UrlLen > sizeof(urlbuf)){
678 lprintf(1, "URL: content longer than buffer!");
681 memcpy(urlbuf, start, UrlLen);
682 urlbuf[UrlLen] = '\0';
684 Offset = start - buf;
685 if ((Offset != 0) && (Offset < sizeof(outbuf)))
686 memcpy(outbuf, buf, Offset);
687 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
688 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
689 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
690 if (outpos >= sizeof(outbuf) - Offset) {
691 lprintf(1, "URL: content longer than buffer!");
695 TrailerLen = len - (end - start);
697 memcpy(outbuf + Offset + outpos, end, TrailerLen);
698 if (Offset + outpos + TrailerLen > bufsize) {
699 lprintf(1, "URL: content longer than buffer!");
702 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
703 *(buf + Offset + outpos + TrailerLen) = '\0';