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.
82 * \param supplied_charset the input charset as declared in the MIME headers
84 void output_html(char *supplied_charset, int treat_as_wiki) {
91 size_t converted_alloc = 0;
92 int buffer_length = 1;
94 int content_length = 0;
95 int output_length = 0;
100 int script_start_pos = (-1);
105 iconv_t ic = (iconv_t)(-1) ;
106 char *ibuf; /**< Buffer of characters to be converted */
107 char *obuf; /**< Buffer for converted characters */
108 size_t ibuflen; /**< Length of input buffer */
109 size_t obuflen; /**< Length of output buffer */
110 char *osav; /**< Saved pointer to output buffer */
113 safestrncpy(charset, supplied_charset, sizeof charset);
115 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
117 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
118 line_length = strlen(buf);
119 buffer_length = content_length + line_length + 2;
120 ptr = realloc(msg, buffer_length);
123 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
126 wprintf("</b><br /><br />\n");
127 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
134 strcpy(&msg[content_length], buf);
135 content_length += line_length;
136 strcpy(&msg[content_length], "\n");
140 /** Do a first pass to isolate the message body */
143 msgend = &msg[content_length];
145 while (ptr < msgend) {
147 /** Advance to next tag */
148 ptr = strchr(ptr, '<');
149 if ((ptr == NULL) || (ptr >= msgend)) break;
151 if ((ptr == NULL) || (ptr >= msgend)) break;
154 * Look for META tags. Some messages (particularly in
155 * Asian locales) illegally declare a message's character
156 * set in the HTML instead of in the MIME headers. This
157 * is wrong but we have to work around it anyway.
159 if (!strncasecmp(ptr, "META", 4)) {
165 char *meta_http_equiv;
169 meta_start = &ptr[4];
170 meta_end = strchr(ptr, '>');
171 if ((meta_end != NULL) && (meta_end <= msgend)) {
172 meta_length = meta_end - meta_start + 1;
173 meta = malloc(meta_length + 1);
174 safestrncpy(meta, meta_start, meta_length);
175 meta[meta_length] = 0;
177 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
178 meta_http_equiv = strdup(&meta[11]);
179 spaceptr = strchr(meta_http_equiv, ' ');
180 if (spaceptr != NULL) {
182 meta_content = strdup(++spaceptr);
183 if (!strncasecmp(meta_content, "content=", 8)) {
184 strcpy(meta_content, &meta_content[8]);
185 stripquotes(meta_http_equiv);
186 stripquotes(meta_content);
187 extract_charset_from_meta(charset,
188 meta_http_equiv, meta_content);
192 free(meta_http_equiv);
199 * Any of these tags cause everything up to and including
200 * the tag to be removed.
202 if ( (!strncasecmp(ptr, "HTML", 4))
203 ||(!strncasecmp(ptr, "HEAD", 4))
204 ||(!strncasecmp(ptr, "/HEAD", 5))
205 ||(!strncasecmp(ptr, "BODY", 4)) ) {
206 ptr = strchr(ptr, '>');
207 if ((ptr == NULL) || (ptr >= msgend)) break;
209 if ((ptr == NULL) || (ptr >= msgend)) break;
214 * Any of these tags cause everything including and following
215 * the tag to be removed.
217 if ( (!strncasecmp(ptr, "/HTML", 5))
218 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
227 if (msgstart > msg) {
228 strcpy(msg, msgstart);
231 /** Convert foreign character sets to UTF-8 if necessary. */
233 if ( (strcasecmp(charset, "us-ascii"))
234 && (strcasecmp(charset, "UTF-8"))
235 && (strcasecmp(charset, ""))
237 lprintf(9, "Converting %s to UTF-8\n", charset);
238 ic = ctdl_iconv_open("UTF-8", charset);
239 if (ic == (iconv_t)(-1) ) {
240 lprintf(5, "%s:%d iconv_open() failed: %s\n",
241 __FILE__, __LINE__, strerror(errno));
244 if (ic != (iconv_t)(-1) ) {
246 ibuflen = content_length;
247 obuflen = content_length + (content_length / 2) ;
248 obuf = (char *) malloc(obuflen);
250 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
251 content_length = content_length + (content_length / 2) - obuflen;
252 osav[content_length] = 0;
260 * At this point, the message has been stripped down to
261 * only the content inside the <BODY></BODY> tags, and has
262 * been converted to UTF-8 if it was originally in a foreign
263 * character set. The text is also guaranteed to be null
267 /** Now go through the message, parsing tags as necessary. */
268 converted_alloc = content_length + 8192;
269 converted_msg = malloc(converted_alloc);
270 if (converted_msg == NULL) {
271 wprintf("Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
275 strcpy(converted_msg, "");
277 msgend = strchr(msg, 0);
278 while (ptr < msgend) {
280 /** Try to sanitize the html of any rogue scripts */
281 if (!strncasecmp(ptr, "<script", 7)) {
282 if (scriptlevel == 0) {
283 script_start_pos = output_length;
287 if (!strncasecmp(ptr, "</script", 8)) {
292 * Change mailto: links to WebCit mail, by replacing the
293 * link with one that points back to our mail room. Due to
294 * the way we parse URL's, it'll even handle mailto: links
295 * that have "?subject=" in them.
297 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
298 content_length += 64;
299 if (content_length >= converted_alloc) {
300 converted_alloc += 8192;
301 converted_msg = realloc(converted_msg, converted_alloc);
302 if (converted_msg == NULL) {
306 sprintf(&converted_msg[output_length],
307 "<a href=\"display_enter?force_room=_MAIL_&recp=");
313 /** Make external links open in a separate window */
314 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
317 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
318 && ((strchr(ptr, '/') < strchr(ptr, '>')))
320 /* open external links to new window */
321 content_length += 64;
322 if (content_length >= converted_alloc) {
323 converted_alloc += 8192;
324 converted_msg = realloc(converted_msg, converted_alloc);
325 if (converted_msg == NULL) {
329 sprintf(&converted_msg[output_length], new_window);
330 output_length += strlen(new_window);
333 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
334 content_length += 64;
335 if (content_length >= converted_alloc) {
336 converted_alloc += 8192;
337 converted_msg = realloc(converted_msg, converted_alloc);
338 if (converted_msg == NULL) {
342 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
347 sprintf(&converted_msg[output_length], "<a href=\"");
354 * Turn anything that looks like a URL into a real link, as long
355 * as it's not inside a tag already
357 else if ( (brak == 0) && (alevel == 0)
358 && (!strncasecmp(ptr, "http://", 7))) {
359 /** Find the end of the link */
363 strlenptr = strlen(ptr);
364 for (i=0; i<=strlenptr; ++i) {
378 /* did s.b. send us an entity? */
380 if ((ptr[i+2] ==';') ||
387 if (linklen > 0) break;
396 linkedchar = ptr[len];
398 /* spot for some subject strings tinymce tends to give us. */
399 ltreviewptr = strchr(ptr, '<');
400 if (ltreviewptr != NULL) {
402 linklen = ltreviewptr - ptr;
405 nbspreviewptr = strstr(ptr, " ");
406 if (nbspreviewptr != NULL) {
407 ///*nbspreviewptr = '\0';
408 linklen = nbspreviewptr - ptr;
410 if (ltreviewptr != 0)
413 ptr[len] = linkedchar;
415 content_length += (32 + linklen);
416 if (content_length >= converted_alloc) {
417 converted_alloc += 8192;
418 converted_msg = realloc(converted_msg, converted_alloc);
419 if (converted_msg == NULL) {
423 sprintf(&converted_msg[output_length], new_window);
424 output_length += strlen(new_window);
425 converted_msg[output_length] = '\"';
426 converted_msg[++output_length] = 0;
427 for (i=0; i<linklen; ++i) {
428 converted_msg[output_length] = ptr[i];
429 converted_msg[++output_length] = 0;
431 sprintf(&converted_msg[output_length], "\">");
433 for (i=0; i<linklen; ++i) {
434 converted_msg[output_length] = *ptr++;
435 converted_msg[++output_length] = 0;
437 sprintf(&converted_msg[output_length], "</A>");
442 converted_msg[output_length] = *ptr++;
443 converted_msg[++output_length] = 0;
447 * We need to know when we're inside a tag,
448 * so we don't turn things that look like URL's into
449 * links, when they're already links - or image sources.
451 if (*(ptr-1) == '<') {
454 if (*(ptr-1) == '>') {
456 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
457 output_length = script_start_pos;
458 converted_msg[output_length] = 0;
459 script_start_pos = (-1);
462 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
465 /** uncomment these two lines to override conversion */
466 /** memcpy(converted_msg, msg, content_length); */
467 /** output_length = content_length; */
469 /** Output our big pile of markup */
470 StrBufAppendBufPlain(WC->WBuf, converted_msg, output_length, 0);
472 BAIL: /** A little trailing vertical whitespace... */
473 wprintf("<br /><br />\n");
475 /** Now give back the memory */
476 if (converted_msg != NULL) free(converted_msg);
477 if (msg != NULL) free(msg);