5 * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
6 * with the rest of our web framework.
7 * \ingroup WebcitHttpServer
11 #include "webserver.h"
15 * \brief Strip surrounding single or double quotes from a string.
17 * \param s String to be stripped.
19 void stripquotes(char *s)
28 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
36 * \brief Check to see if a META tag has overridden the declared MIME character set.
38 * \param charset Character set name (left unchanged if we don't do anything)
39 * \param meta_http_equiv Content of the "http-equiv" portion of the META tag
40 * \param meta_content Content of the "content" portion of the META tag
42 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
48 if (!meta_http_equiv) return;
49 if (!meta_content) return;
52 if (strcasecmp(meta_http_equiv, "Content-type")) return;
54 ptr = strchr(meta_content, ';');
57 safestrncpy(buf, ++ptr, sizeof buf);
59 if (!strncasecmp(buf, "charset=", 8)) {
60 strcpy(charset, &buf[8]);
63 * The brain-damaged webmail program in Microsoft Exchange declares
64 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
65 * treats "unicode" as an alias for "UTF-16" so we have to manually
66 * fix this here, otherwise messages generated in Exchange webmail
67 * show up as a big pile of weird characters.
69 if (!strcasecmp(charset, "unicode")) {
70 strcpy(charset, "UTF-8");
79 * \brief Sanitize and enhance an HTML message for display.
80 * Also convert weird character sets to UTF-8 if necessary.
81 * Also fixup img src="cid:..." type inline images to fetch the image
83 * \param supplied_charset the input charset as declared in the MIME headers
85 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
91 StrBuf *converted_msg;
92 int buffer_length = 1;
94 int content_length = 0;
99 int script_start_pos = (-1);
104 iconv_t ic = (iconv_t)(-1) ;
105 char *ibuf; /**< Buffer of characters to be converted */
106 char *obuf; /**< Buffer for converted characters */
107 size_t ibuflen; /**< Length of input buffer */
108 size_t obuflen; /**< Length of output buffer */
109 char *osav; /**< Saved pointer to output buffer */
114 safestrncpy(charset, supplied_charset, sizeof charset);
116 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
118 if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
119 line_length = strlen(buf);
120 buffer_length = content_length + line_length + 2;
121 ptr = realloc(msg, buffer_length);
123 StrBufAppendPrintf(Target, "<b>");
124 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
127 StrBufAppendPrintf(Target, "</b><br /><br />\n");
128 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
135 strcpy(&msg[content_length], buf);
136 content_length += line_length;
137 strcpy(&msg[content_length], "\n");
141 content_length = StrLength(Source);
142 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
143 buffer_length = content_length;
146 /** Do a first pass to isolate the message body */
149 msgend = &msg[content_length];
151 while (ptr < msgend) {
153 /** Advance to next tag */
154 ptr = strchr(ptr, '<');
155 if ((ptr == NULL) || (ptr >= msgend)) break;
157 if ((ptr == NULL) || (ptr >= msgend)) break;
160 * Look for META tags. Some messages (particularly in
161 * Asian locales) illegally declare a message's character
162 * set in the HTML instead of in the MIME headers. This
163 * is wrong but we have to work around it anyway.
165 if (!strncasecmp(ptr, "META", 4)) {
171 char *meta_http_equiv;
175 meta_start = &ptr[4];
176 meta_end = strchr(ptr, '>');
177 if ((meta_end != NULL) && (meta_end <= msgend)) {
178 meta_length = meta_end - meta_start + 1;
179 meta = malloc(meta_length + 1);
180 safestrncpy(meta, meta_start, meta_length);
181 meta[meta_length] = 0;
183 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
184 meta_http_equiv = strdup(&meta[11]);
185 spaceptr = strchr(meta_http_equiv, ' ');
186 if (spaceptr != NULL) {
188 meta_content = strdup(++spaceptr);
189 if (!strncasecmp(meta_content, "content=", 8)) {
190 strcpy(meta_content, &meta_content[8]);
191 stripquotes(meta_http_equiv);
192 stripquotes(meta_content);
193 extract_charset_from_meta(charset,
194 meta_http_equiv, meta_content);
198 free(meta_http_equiv);
205 * Any of these tags cause everything up to and including
206 * the tag to be removed.
208 if ( (!strncasecmp(ptr, "HTML", 4))
209 ||(!strncasecmp(ptr, "HEAD", 4))
210 ||(!strncasecmp(ptr, "/HEAD", 5))
211 ||(!strncasecmp(ptr, "BODY", 4)) ) {
212 ptr = strchr(ptr, '>');
213 if ((ptr == NULL) || (ptr >= msgend)) break;
215 if ((ptr == NULL) || (ptr >= msgend)) break;
220 * Any of these tags cause everything including and following
221 * the tag to be removed.
223 if ( (!strncasecmp(ptr, "/HTML", 5))
224 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
233 if (msgstart > msg) {
234 strcpy(msg, msgstart);
237 /** Convert foreign character sets to UTF-8 if necessary. */
239 if ( (strcasecmp(charset, "us-ascii"))
240 && (strcasecmp(charset, "UTF-8"))
241 && (strcasecmp(charset, ""))
243 lprintf(9, "Converting %s to UTF-8\n", charset);
244 ic = ctdl_iconv_open("UTF-8", charset);
245 if (ic == (iconv_t)(-1) ) {
246 lprintf(5, "%s:%d iconv_open() failed: %s\n",
247 __FILE__, __LINE__, strerror(errno));
250 if (ic != (iconv_t)(-1) ) {
252 ibuflen = content_length;
253 obuflen = content_length + (content_length / 2) ;
254 obuf = (char *) malloc(obuflen);
256 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
257 content_length = content_length + (content_length / 2) - obuflen;
258 osav[content_length] = 0;
266 * At this point, the message has been stripped down to
267 * only the content inside the <BODY></BODY> tags, and has
268 * been converted to UTF-8 if it was originally in a foreign
269 * character set. The text is also guaranteed to be null
273 /** Now go through the message, parsing tags as necessary. */
274 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
275 if (converted_msg == NULL) {
276 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
281 msgend = strchr(msg, 0);
282 while (ptr < msgend) {
284 /** Try to sanitize the html of any rogue scripts */
285 if (!strncasecmp(ptr, "<script", 7)) {
286 if (scriptlevel == 0) {
287 script_start_pos = StrLength(converted_msg);
291 if (!strncasecmp(ptr, "</script", 8)) {
296 * Change mailto: links to WebCit mail, by replacing the
297 * link with one that points back to our mail room. Due to
298 * the way we parse URL's, it'll even handle mailto: links
299 * that have "?subject=" in them.
301 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
302 content_length += 64;
303 StrBufAppendPrintf(converted_msg,
304 "<a href=\"display_enter?force_room=_MAIL_&recp=");
309 /** Make external links open in a separate window */
310 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
313 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
314 && ((strchr(ptr, '/') < strchr(ptr, '>')))
316 /* open external links to new window */
317 StrBufAppendPrintf(converted_msg, new_window);
320 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
321 content_length += 64;
322 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
326 StrBufAppendPrintf(converted_msg, "<a href=\"");
330 /** Fixup <img src="cid:... ...> to fetch the mime part */
331 else if (!strncasecmp(ptr, "<img ", 5)) {
332 char* tag_end=strchr(ptr,'>');
333 char* src=strstr(ptr, " src=\"cid:");
334 char *cid_start, *cid_end;
338 (cid_start=strchr(src,':')) &&
339 (cid_end=strchr(cid_start,'"')) &&
340 (cid_end < tag_end)) {
342 /* copy tag and attributes up to src="cid: */
343 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
346 /* add in /webcit/mimepart/<msgno>/CID/
347 trailing / stops dumb URL filters getting excited */
348 StrBufAppendPrintf(converted_msg,
349 "src=\"/webcit/mimepart/%d/",msgnum);
350 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
351 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
355 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
360 * Turn anything that looks like a URL into a real link, as long
361 * as it's not inside a tag already
363 else if ( (brak == 0) && (alevel == 0)
364 && (!strncasecmp(ptr, "http://", 7))) {
365 /** Find the end of the link */
369 strlenptr = strlen(ptr);
370 for (i=0; i<=strlenptr; ++i) {
384 /* did s.b. send us an entity? */
386 if ((ptr[i+2] ==';') ||
393 if (linklen > 0) break;
402 linkedchar = ptr[len];
404 /* spot for some subject strings tinymce tends to give us. */
405 ltreviewptr = strchr(ptr, '<');
406 if (ltreviewptr != NULL) {
408 linklen = ltreviewptr - ptr;
411 nbspreviewptr = strstr(ptr, " ");
412 if (nbspreviewptr != NULL) {
413 ///*nbspreviewptr = '\0';
414 linklen = nbspreviewptr - ptr;
416 if (ltreviewptr != 0)
419 ptr[len] = linkedchar;
421 content_length += (32 + linklen);
422 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
423 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
424 StrBufAppendPrintf(converted_msg, "\">");
425 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
427 StrBufAppendPrintf(converted_msg, "</A>");
431 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
436 * We need to know when we're inside a tag,
437 * so we don't turn things that look like URL's into
438 * links, when they're already links - or image sources.
440 if (*(ptr-1) == '<') {
443 if (*(ptr-1) == '>') {
445 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
446 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
447 script_start_pos = (-1);
450 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
453 /** uncomment these two lines to override conversion */
454 /** memcpy(converted_msg, msg, content_length); */
455 /** output_length = content_length; */
457 /** Output our big pile of markup */
458 StrBufAppendBuf(Target, converted_msg, 0);
460 BAIL: /** A little trailing vertical whitespace... */
461 StrBufAppendPrintf(Target, "<br /><br />\n");
463 /** Now give back the memory */
464 FreeStrBuf(&converted_msg);
465 if (msg != NULL) free(msg);