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-2011 by the citadel.org team
7 * This program is open source 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 syslog(9, "Converting %s to UTF-8\n", charset);
305 ctdl_iconv_open("UTF-8", charset, &ic);
306 if (ic == (iconv_t)(-1) ) {
307 syslog(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);
398 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
399 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
400 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
402 content_length += 64;
403 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
407 StrBufAppendPrintf(converted_msg, "<a href=\"");
411 /** Fixup <img src="cid:... ...> to fetch the mime part */
412 else if (!strncasecmp(ptr, "<img ", 5)) {
413 char *cid_start, *cid_end;
414 char* tag_end=strchr(ptr,'>');
416 /* FIXME - handle this situation (maybe someone opened an <img cid...
417 * and then ended the message)
420 syslog(9, "tag_end is null and ptr is:\n");
421 syslog(9, "%s\n", ptr);
422 syslog(9, "Theoretical bytes remaining: %d\n", (int)(msgend - ptr));
425 src=strstr(ptr, "src=\"cid:");
431 && (cid_start=strchr(src,':'))
432 && (cid_end=strchr(cid_start,'"'))
433 && (cid_end < tag_end)
435 /* copy tag and attributes up to src="cid: */
436 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
439 /* add in /webcit/mimepart/<msgno>/CID/
440 trailing / stops dumb URL filters getting excited */
441 StrBufAppendPrintf(converted_msg,
442 " src=\"/webcit/mimepart/%d/",msgnum);
443 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
444 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
448 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
453 * Turn anything that looks like a URL into a real link, as long
454 * as it's not inside a tag already
456 else if ( (brak == 0) && (alevel == 0)
457 && (!strncasecmp(ptr, "http://", 7))) {
458 /** Find the end of the link */
462 strlenptr = strlen(ptr);
463 for (i=0; i<=strlenptr; ++i) {
477 /* did s.b. send us an entity? */
479 if ((ptr[i+2] ==';') ||
486 if (linklen > 0) break;
495 linkedchar = ptr[len];
497 /* spot for some subject strings tinymce tends to give us. */
498 ltreviewptr = strchr(ptr, '<');
499 if (ltreviewptr != NULL) {
501 linklen = ltreviewptr - ptr;
504 nbspreviewptr = strstr(ptr, " ");
505 if (nbspreviewptr != NULL) {
506 /* nbspreviewptr = '\0'; */
507 linklen = nbspreviewptr - ptr;
509 if (ltreviewptr != 0)
512 ptr[len] = linkedchar;
514 content_length += (32 + linklen);
515 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
516 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
517 StrBufAppendPrintf(converted_msg, "\">");
518 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
520 StrBufAppendPrintf(converted_msg, "</A>");
524 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
529 if ((ptr >= msg) && (ptr <= msgend)) {
531 * We need to know when we're inside a tag,
532 * so we don't turn things that look like URL's into
533 * links, when they're already links - or image sources.
535 if ((ptr > msg) && (*(ptr-1) == '<')) {
538 if ((ptr > msg) && (*(ptr-1) == '>')) {
540 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
541 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
542 script_start_pos = (-1);
545 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
549 if (BodyArea != NULL) {
550 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
551 FreeStrBuf(&BodyArea);
554 /** uncomment these two lines to override conversion */
555 /** memcpy(converted_msg, msg, content_length); */
556 /** output_length = content_length; */
558 /** Output our big pile of markup */
559 StrBufAppendBuf(Target, converted_msg, 0);
561 BAIL: /** A little trailing vertical whitespace... */
562 StrBufAppendPrintf(Target, "<br><br>\n");
564 /** Now give back the memory */
565 FreeStrBuf(&converted_msg);
566 if ((msg != NULL) && (Source == NULL)) free(msg);
575 * Look for URL's embedded in a buffer and make them linkable. We use a
576 * target window in order to keep the Citadel session in its own window.
578 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
580 int len, UrlLen, Offset, TrailerLen;
581 const char *start, *end, *pos;
586 len = StrLength(Source);
587 end = ChrPtr(Source) + len;
588 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
589 if (!strncasecmp(pos, "http://", 7))
591 else if (!strncasecmp(pos, "ftp://", 6))
596 StrBufAppendBuf(Target, Source, 0);
601 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
602 if ( (!isprint(*pos))
621 UrlLen = end - start;
622 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
624 Offset = start - ChrPtr(Source);
626 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
627 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
628 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
629 QU, RB, ChrPtr(WrkBuf), LB, RB);
631 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
633 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
637 void url(char *buf, size_t bufsize)
639 int len, UrlLen, Offset, TrailerLen, outpos;
640 char *start, *end, *pos;
647 syslog(1, "URL: content longer than buffer!");
651 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
652 if (!strncasecmp(pos, "http://", 7))
654 if (!strncasecmp(pos, "ftp://", 6))
661 for (pos = buf+len; pos > start; --pos) {
662 if ( (!isprint(*pos))
681 UrlLen = end - start;
682 if (UrlLen > sizeof(urlbuf)){
683 syslog(1, "URL: content longer than buffer!");
686 memcpy(urlbuf, start, UrlLen);
687 urlbuf[UrlLen] = '\0';
689 Offset = start - buf;
690 if ((Offset != 0) && (Offset < sizeof(outbuf)))
691 memcpy(outbuf, buf, Offset);
692 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
693 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
694 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
695 if (outpos >= sizeof(outbuf) - Offset) {
696 syslog(1, "URL: content longer than buffer!");
700 TrailerLen = len - (end - start);
702 memcpy(outbuf + Offset + outpos, end, TrailerLen);
703 if (Offset + outpos + TrailerLen > bufsize) {
704 syslog(1, "URL: content longer than buffer!");
707 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
708 *(buf + Offset + outpos + TrailerLen) = '\0';