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(char *supplied_charset, int treat_as_wiki, int msgnum) {
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 */
112 safestrncpy(charset, supplied_charset, sizeof charset);
114 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
116 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
117 line_length = strlen(buf);
118 buffer_length = content_length + line_length + 2;
119 ptr = realloc(msg, buffer_length);
122 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
125 wprintf("</b><br /><br />\n");
126 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
133 strcpy(&msg[content_length], buf);
134 content_length += line_length;
135 strcpy(&msg[content_length], "\n");
139 /** Do a first pass to isolate the message body */
142 msgend = &msg[content_length];
144 while (ptr < msgend) {
146 /** Advance to next tag */
147 ptr = strchr(ptr, '<');
148 if ((ptr == NULL) || (ptr >= msgend)) break;
150 if ((ptr == NULL) || (ptr >= msgend)) break;
153 * Look for META tags. Some messages (particularly in
154 * Asian locales) illegally declare a message's character
155 * set in the HTML instead of in the MIME headers. This
156 * is wrong but we have to work around it anyway.
158 if (!strncasecmp(ptr, "META", 4)) {
164 char *meta_http_equiv;
168 meta_start = &ptr[4];
169 meta_end = strchr(ptr, '>');
170 if ((meta_end != NULL) && (meta_end <= msgend)) {
171 meta_length = meta_end - meta_start + 1;
172 meta = malloc(meta_length + 1);
173 safestrncpy(meta, meta_start, meta_length);
174 meta[meta_length] = 0;
176 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
177 meta_http_equiv = strdup(&meta[11]);
178 spaceptr = strchr(meta_http_equiv, ' ');
179 if (spaceptr != NULL) {
181 meta_content = strdup(++spaceptr);
182 if (!strncasecmp(meta_content, "content=", 8)) {
183 strcpy(meta_content, &meta_content[8]);
184 stripquotes(meta_http_equiv);
185 stripquotes(meta_content);
186 extract_charset_from_meta(charset,
187 meta_http_equiv, meta_content);
191 free(meta_http_equiv);
198 * Any of these tags cause everything up to and including
199 * the tag to be removed.
201 if ( (!strncasecmp(ptr, "HTML", 4))
202 ||(!strncasecmp(ptr, "HEAD", 4))
203 ||(!strncasecmp(ptr, "/HEAD", 5))
204 ||(!strncasecmp(ptr, "BODY", 4)) ) {
205 ptr = strchr(ptr, '>');
206 if ((ptr == NULL) || (ptr >= msgend)) break;
208 if ((ptr == NULL) || (ptr >= msgend)) break;
213 * Any of these tags cause everything including and following
214 * the tag to be removed.
216 if ( (!strncasecmp(ptr, "/HTML", 5))
217 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
226 if (msgstart > msg) {
227 strcpy(msg, msgstart);
230 /** Convert foreign character sets to UTF-8 if necessary. */
232 if ( (strcasecmp(charset, "us-ascii"))
233 && (strcasecmp(charset, "UTF-8"))
234 && (strcasecmp(charset, ""))
236 lprintf(9, "Converting %s to UTF-8\n", charset);
237 ic = ctdl_iconv_open("UTF-8", charset);
238 if (ic == (iconv_t)(-1) ) {
239 lprintf(5, "%s:%d iconv_open() failed: %s\n",
240 __FILE__, __LINE__, strerror(errno));
243 if (ic != (iconv_t)(-1) ) {
245 ibuflen = content_length;
246 obuflen = content_length + (content_length / 2) ;
247 obuf = (char *) malloc(obuflen);
249 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
250 content_length = content_length + (content_length / 2) - obuflen;
251 osav[content_length] = 0;
259 * At this point, the message has been stripped down to
260 * only the content inside the <BODY></BODY> tags, and has
261 * been converted to UTF-8 if it was originally in a foreign
262 * character set. The text is also guaranteed to be null
266 /** Now go through the message, parsing tags as necessary. */
267 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
268 if (converted_msg == NULL) {
269 wprintf("Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
274 msgend = strchr(msg, 0);
275 while (ptr < msgend) {
277 /** Try to sanitize the html of any rogue scripts */
278 if (!strncasecmp(ptr, "<script", 7)) {
279 if (scriptlevel == 0) {
280 script_start_pos = StrLength(converted_msg);
284 if (!strncasecmp(ptr, "</script", 8)) {
289 * Change mailto: links to WebCit mail, by replacing the
290 * link with one that points back to our mail room. Due to
291 * the way we parse URL's, it'll even handle mailto: links
292 * that have "?subject=" in them.
294 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
295 content_length += 64;
296 StrBufAppendPrintf(converted_msg,
297 "<a href=\"display_enter?force_room=_MAIL_&recp=");
302 /** Make external links open in a separate window */
303 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
306 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
307 && ((strchr(ptr, '/') < strchr(ptr, '>')))
309 /* open external links to new window */
310 StrBufAppendPrintf(converted_msg, new_window);
313 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
314 content_length += 64;
315 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
319 StrBufAppendPrintf(converted_msg, "<a href=\"");
323 /** Fixup <img src="cid:... ...> to fetch the mime part */
324 else if (!strncasecmp(ptr, "<img ", 5)) {
325 char* tag_end=strchr(ptr,'>');
326 char* src=strstr(ptr, " src=\"cid:");
327 char *cid_start, *cid_end;
331 (cid_start=strchr(src,':')) &&
332 (cid_end=strchr(cid_start,'"')) &&
333 (cid_end < tag_end)) {
335 /* copy tag and attributes up to src="cid: */
336 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
339 /* add in /webcit/mimepart/<msgno>/CID/
340 trailing / stops dumb URL filters getting excited */
341 StrBufAppendPrintf(converted_msg,
342 "src=\"/webcit/mimepart/%d/",msgnum);
343 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
344 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
348 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
353 * Turn anything that looks like a URL into a real link, as long
354 * as it's not inside a tag already
356 else if ( (brak == 0) && (alevel == 0)
357 && (!strncasecmp(ptr, "http://", 7))) {
358 /** Find the end of the link */
362 strlenptr = strlen(ptr);
363 for (i=0; i<=strlenptr; ++i) {
377 /* did s.b. send us an entity? */
379 if ((ptr[i+2] ==';') ||
386 if (linklen > 0) break;
395 linkedchar = ptr[len];
397 /* spot for some subject strings tinymce tends to give us. */
398 ltreviewptr = strchr(ptr, '<');
399 if (ltreviewptr != NULL) {
401 linklen = ltreviewptr - ptr;
404 nbspreviewptr = strstr(ptr, " ");
405 if (nbspreviewptr != NULL) {
406 ///*nbspreviewptr = '\0';
407 linklen = nbspreviewptr - ptr;
409 if (ltreviewptr != 0)
412 ptr[len] = linkedchar;
414 content_length += (32 + linklen);
415 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
416 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
417 StrBufAppendPrintf(converted_msg, "\">");
418 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
420 StrBufAppendPrintf(converted_msg, "</A>");
424 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
429 * We need to know when we're inside a tag,
430 * so we don't turn things that look like URL's into
431 * links, when they're already links - or image sources.
433 if (*(ptr-1) == '<') {
436 if (*(ptr-1) == '>') {
438 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
439 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
440 script_start_pos = (-1);
443 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
446 /** uncomment these two lines to override conversion */
447 /** memcpy(converted_msg, msg, content_length); */
448 /** output_length = content_length; */
450 /** Output our big pile of markup */
451 StrBufAppendBuf(WC->WBuf, converted_msg, 0);
453 BAIL: /** A little trailing vertical whitespace... */
454 wprintf("<br /><br />\n");
456 /** Now give back the memory */
457 if (converted_msg != NULL) free(converted_msg);
458 if (msg != NULL) free(msg);