1 /* $Id: map.c,v 1.30 2003/09/24 18:49:00 ukai Exp $ */
2 /*
3  * client-side image maps
4  */
5 #include "fm.h"
6 #include <math.h>
7 
8 MapList *
searchMapList(Buffer * buf,char * name)9 searchMapList(Buffer *buf, char *name)
10 {
11     MapList *ml;
12 
13     if (name == NULL)
14 	return NULL;
15     for (ml = buf->maplist; ml != NULL; ml = ml->next) {
16 	if (!Strcmp_charp(ml->name, name))
17 	    break;
18     }
19     return ml;
20 }
21 
22 #ifdef USE_IMAGE
23 static int
inMapArea(MapArea * a,int x,int y)24 inMapArea(MapArea * a, int x, int y)
25 {
26     int i;
27     double r1, r2, s, c, t;
28 
29     if (!a)
30 	return FALSE;
31     switch (a->shape) {
32     case SHAPE_RECT:
33 	if (x >= a->coords[0] && y >= a->coords[1] &&
34 	    x <= a->coords[2] && y <= a->coords[3])
35 	    return TRUE;
36 	break;
37     case SHAPE_CIRCLE:
38 	if ((x - a->coords[0]) * (x - a->coords[0])
39 	    + (y - a->coords[1]) * (y - a->coords[1])
40 	    <= a->coords[2] * a->coords[2])
41 	    return TRUE;
42 	break;
43     case SHAPE_POLY:
44 	for (t = 0, i = 0; i < a->ncoords; i += 2) {
45 	    r1 = sqrt((double)(x - a->coords[i]) * (x - a->coords[i])
46 		      + (double)(y - a->coords[i + 1]) * (y -
47 							  a->coords[i + 1]));
48 	    r2 = sqrt((double)(x - a->coords[i + 2]) * (x - a->coords[i + 2])
49 		      + (double)(y - a->coords[i + 3]) * (y -
50 							  a->coords[i + 3]));
51 	    if (r1 == 0 || r2 == 0)
52 		return TRUE;
53 	    s = ((double)(x - a->coords[i]) * (y - a->coords[i + 3])
54 		 - (double)(x - a->coords[i + 2]) * (y -
55 						     a->coords[i +
56 							       1])) / r1 / r2;
57 	    c = ((double)(x - a->coords[i]) * (x - a->coords[i + 2])
58 		 + (double)(y - a->coords[i + 1]) * (y -
59 						     a->coords[i +
60 							       3])) / r1 / r2;
61 	    t += atan2(s, c);
62 	}
63 	if (fabs(t) > 2 * 3.14)
64 	    return TRUE;
65 	break;
66     case SHAPE_DEFAULT:
67 	return TRUE;
68     default:
69 	break;
70     }
71     return FALSE;
72 }
73 
74 static int
nearestMapArea(MapList * ml,int x,int y)75 nearestMapArea(MapList *ml, int x, int y)
76 {
77     ListItem *al;
78     MapArea *a;
79     int i, l, n = -1, min = -1, limit = pixel_per_char * pixel_per_char
80 	+ pixel_per_line * pixel_per_line;
81 
82     if (!ml || !ml->area)
83 	return n;
84     for (i = 0, al = ml->area->first; al != NULL; i++, al = al->next) {
85 	a = (MapArea *) al->ptr;
86 	if (a) {
87 	    l = (a->center_x - x) * (a->center_x - x)
88 		+ (a->center_y - y) * (a->center_y - y);
89 	    if ((min < 0 || l < min) && l < limit) {
90 		n = i;
91 		min = l;
92 	    }
93 	}
94     }
95     return n;
96 }
97 
98 static int
searchMapArea(Buffer * buf,MapList * ml,Anchor * a_img)99 searchMapArea(Buffer *buf, MapList *ml, Anchor *a_img)
100 {
101     ListItem *al;
102     MapArea *a;
103     int i, n;
104     int px, py;
105 
106     if (!(ml && ml->area && ml->area->nitem))
107 	return -1;
108     if (!getMapXY(buf, a_img, &px, &py))
109 	return -1;
110     n = -ml->area->nitem;
111     for (i = 0, al = ml->area->first; al != NULL; i++, al = al->next) {
112 	a = (MapArea *) al->ptr;
113 	if (!a)
114 	    continue;
115 	if (n < 0 && inMapArea(a, px, py)) {
116 	    if (a->shape == SHAPE_DEFAULT) {
117 		if (n == -ml->area->nitem)
118 		    n = -i;
119 	    }
120 	    else
121 		n = i;
122 	}
123     }
124     if (n == -ml->area->nitem)
125 	return nearestMapArea(ml, px, py);
126     else if (n < 0)
127 	return -n;
128     return n;
129 }
130 
131 MapArea *
retrieveCurrentMapArea(Buffer * buf)132 retrieveCurrentMapArea(Buffer *buf)
133 {
134     Anchor *a_img, *a_form;
135     FormItemList *fi;
136     MapList *ml;
137     ListItem *al;
138     MapArea *a;
139     int i, n;
140 
141     a_img = retrieveCurrentImg(buf);
142     if (!(a_img && a_img->image && a_img->image->map))
143 	return NULL;
144     a_form = retrieveCurrentForm(buf);
145     if (!(a_form && a_form->url))
146 	return NULL;
147     fi = (FormItemList *)a_form->url;
148     if (!(fi && fi->parent && fi->parent->item))
149 	return NULL;
150     fi = fi->parent->item;
151     ml = searchMapList(buf, fi->value ? fi->value->ptr : NULL);
152     if (!ml)
153 	return NULL;
154     n = searchMapArea(buf, ml, a_img);
155     if (n < 0)
156 	return NULL;
157     for (i = 0, al = ml->area->first; al != NULL; i++, al = al->next) {
158 	a = (MapArea *) al->ptr;
159 	if (a && i == n)
160 	    return a;
161     }
162     return NULL;
163 }
164 
165 int
getMapXY(Buffer * buf,Anchor * a,int * x,int * y)166 getMapXY(Buffer *buf, Anchor *a, int *x, int *y)
167 {
168     if (!buf || !a || !a->image || !x || !y)
169 	return 0;
170     *x = (int)((buf->currentColumn + buf->cursorX
171 		- COLPOS(buf->currentLine, a->start.pos) + 0.5)
172 	       * pixel_per_char) - a->image->xoffset;
173     *y = (int)((buf->currentLine->linenumber - a->image->y + 0.5)
174 	       * pixel_per_line) - a->image->yoffset;
175     if (*x <= 0)
176 	*x = 1;
177     if (*y <= 0)
178 	*y = 1;
179     return 1;
180 }
181 #endif
182 
183 Anchor *
retrieveCurrentMap(Buffer * buf)184 retrieveCurrentMap(Buffer *buf)
185 {
186     Anchor *a;
187     FormItemList *fi;
188 
189     a = retrieveCurrentForm(buf);
190     if (!a || !a->url)
191 	return NULL;
192     fi = (FormItemList *)a->url;
193     if (fi->parent->method == FORM_METHOD_INTERNAL &&
194 	!Strcmp_charp(fi->parent->action, "map"))
195 	return a;
196     return NULL;
197 }
198 
199 #if defined(USE_IMAGE) || defined(MENU_MAP)
200 MapArea *
follow_map_menu(Buffer * buf,char * name,Anchor * a_img,int x,int y)201 follow_map_menu(Buffer *buf, char *name, Anchor *a_img, int x, int y)
202 {
203     MapList *ml;
204     ListItem *al;
205     int i, selected = -1;
206     int initial = 0;
207 #ifdef MENU_MAP
208     MapArea *a;
209     char **label;
210 #endif
211 
212     ml = searchMapList(buf, name);
213     if (ml == NULL || ml->area == NULL || ml->area->nitem == 0)
214 	return NULL;
215 
216 #ifdef USE_IMAGE
217     initial = searchMapArea(buf, ml, a_img);
218     if (initial < 0)
219 	initial = 0;
220     else if (!image_map_list) {
221 	selected = initial;
222 	goto map_end;
223     }
224 #endif
225 
226 #ifdef MENU_MAP
227     label = New_N(char *, ml->area->nitem + 1);
228     for (i = 0, al = ml->area->first; al != NULL; i++, al = al->next) {
229 	a = (MapArea *) al->ptr;
230 	if (a)
231 	    label[i] = *a->alt ? a->alt : a->url;
232 	else
233 	    label[i] = "";
234     }
235     label[ml->area->nitem] = NULL;
236 
237     optionMenu(x, y, label, &selected, initial, NULL);
238 #endif
239 
240 #ifdef USE_IMAGE
241   map_end:
242 #endif
243     if (selected >= 0) {
244 	for (i = 0, al = ml->area->first; al != NULL; i++, al = al->next) {
245 	    if (al->ptr && i == selected)
246 		return (MapArea *) al->ptr;
247 	}
248     }
249     return NULL;
250 }
251 #endif
252 
253 #ifndef MENU_MAP
254 char *map1 = "<HTML><HEAD><TITLE>Image map links</TITLE></HEAD>\
255 <BODY><H1>Image map links</H1>\
256 <table>";
257 
258 Buffer *
follow_map_panel(Buffer * buf,char * name)259 follow_map_panel(Buffer *buf, char *name)
260 {
261     Str mappage;
262     MapList *ml;
263     ListItem *al;
264     MapArea *a;
265     ParsedURL pu;
266     char *p, *q;
267     Buffer *newbuf;
268 
269     ml = searchMapList(buf, name);
270     if (ml == NULL)
271 	return NULL;
272 
273     mappage = Strnew_charp(map1);
274     for (al = ml->area->first; al != NULL; al = al->next) {
275 	a = (MapArea *) al->ptr;
276 	if (!a)
277 	    continue;
278 	parseURL2(a->url, &pu, baseURL(buf));
279 	p = parsedURL2Str(&pu)->ptr;
280 	q = html_quote(p);
281 	if (DecodeURL)
282 	    p = html_quote(url_decode2(p, buf));
283 	else
284 	    p = q;
285 	Strcat_m_charp(mappage, "<tr valign=top><td><a href=\"", q, "\">",
286 		       html_quote(*a->alt ? a->alt : mybasename(a->url)),
287 		       "</a><td>", p, NULL);
288     }
289     Strcat_charp(mappage, "</table></body></html>");
290 
291     newbuf = loadHTMLString(mappage);
292 #ifdef USE_M17N
293     if (newbuf)
294 	newbuf->document_charset = buf->document_charset;
295 #endif
296     return newbuf;
297 }
298 #endif
299 
300 MapArea *
newMapArea(char * url,char * target,char * alt,char * shape,char * coords)301 newMapArea(char *url, char *target, char *alt, char *shape, char *coords)
302 {
303     MapArea *a = New(MapArea);
304 #ifdef USE_IMAGE
305     char *p;
306     int i, max;
307 #endif
308 
309     a->url = url;
310     a->target = target;
311     a->alt = alt ? alt : "";
312 #ifdef USE_IMAGE
313     a->shape = SHAPE_RECT;
314     if (shape) {
315 	if (!strcasecmp(shape, "default"))
316 	    a->shape = SHAPE_DEFAULT;
317 	else if (!strncasecmp(shape, "rect", 4))
318 	    a->shape = SHAPE_RECT;
319 	else if (!strncasecmp(shape, "circ", 4))
320 	    a->shape = SHAPE_CIRCLE;
321 	else if (!strncasecmp(shape, "poly", 4))
322 	    a->shape = SHAPE_POLY;
323 	else
324 	    a->shape = SHAPE_UNKNOWN;
325     }
326     a->coords = NULL;
327     a->ncoords = 0;
328     a->center_x = 0;
329     a->center_y = 0;
330     if (a->shape == SHAPE_UNKNOWN || a->shape == SHAPE_DEFAULT)
331 	return a;
332     if (!coords) {
333 	a->shape = SHAPE_UNKNOWN;
334 	return a;
335     }
336     if (a->shape == SHAPE_RECT) {
337 	a->coords = New_N(short, 4);
338 	a->ncoords = 4;
339     }
340     else if (a->shape == SHAPE_CIRCLE) {
341 	a->coords = New_N(short, 3);
342 	a->ncoords = 3;
343     }
344     max = a->ncoords;
345     for (i = 0, p = coords; (a->shape == SHAPE_POLY || i < a->ncoords) && *p;) {
346 	while (IS_SPACE(*p))
347 	    p++;
348 	if (!IS_DIGIT(*p) && *p != '-' && *p != '+')
349 	    break;
350 	if (a->shape == SHAPE_POLY) {
351 	    if (max <= i) {
352 		max = i ? i * 2 : 6;
353 		a->coords = New_Reuse(short, a->coords, max + 2);
354 	    }
355 	    a->ncoords++;
356 	}
357 	a->coords[i] = (short)atoi(p);
358 	i++;
359 	if (*p == '-' || *p == '+')
360 	    p++;
361 	while (IS_DIGIT(*p))
362 	    p++;
363 	if (*p != ',' && !IS_SPACE(*p))
364 	    break;
365 	while (IS_SPACE(*p))
366 	    p++;
367 	if (*p == ',')
368 	    p++;
369     }
370     if (i != a->ncoords || (a->shape == SHAPE_POLY && a->ncoords < 6)) {
371 	a->shape = SHAPE_UNKNOWN;
372 	a->coords = NULL;
373 	a->ncoords = 0;
374 	return a;
375     }
376     if (a->shape == SHAPE_POLY) {
377 	a->ncoords = a->ncoords / 2 * 2;
378 	a->coords[a->ncoords] = a->coords[0];
379 	a->coords[a->ncoords + 1] = a->coords[1];
380     }
381     if (a->shape == SHAPE_CIRCLE) {
382 	a->center_x = a->coords[0];
383 	a->center_y = a->coords[1];
384     }
385     else {
386 	for (i = 0; i < a->ncoords / 2; i++) {
387 	    a->center_x += a->coords[2 * i];
388 	    a->center_y += a->coords[2 * i + 1];
389 	}
390 	a->center_x /= a->ncoords / 2;
391 	a->center_y /= a->ncoords / 2;
392     }
393 #endif
394     return a;
395 }
396 
397 /* append image map links */
398 static void
append_map_info(Buffer * buf,Str tmp,FormItemList * fi)399 append_map_info(Buffer *buf, Str tmp, FormItemList *fi)
400 {
401     MapList *ml;
402     ListItem *al;
403     MapArea *a;
404     ParsedURL pu;
405     char *p, *q;
406 
407     ml = searchMapList(buf, fi->value ? fi->value->ptr : NULL);
408     if (ml == NULL)
409 	return;
410 
411     Strcat_m_charp(tmp,
412 		   "<tr valign=top><td colspan=2>Links of current image map",
413 		   "<tr valign=top><td colspan=2><table>", NULL);
414     for (al = ml->area->first; al != NULL; al = al->next) {
415 	a = (MapArea *) al->ptr;
416 	if (!a)
417 	    continue;
418 	parseURL2(a->url, &pu, baseURL(buf));
419 	q = html_quote(parsedURL2Str(&pu)->ptr);
420 	p = html_quote(url_decode2(a->url, buf));
421 	Strcat_m_charp(tmp, "<tr valign=top><td>&nbsp;&nbsp;<td><a href=\"",
422 		       q, "\">",
423 		       html_quote(*a->alt ? a->alt : mybasename(a->url)),
424 		       "</a><td>", p, "\n", NULL);
425     }
426     Strcat_charp(tmp, "</table>");
427 }
428 
429 /* append links */
430 static void
append_link_info(Buffer * buf,Str html,LinkList * link)431 append_link_info(Buffer *buf, Str html, LinkList * link)
432 {
433     LinkList *l;
434     ParsedURL pu;
435     char *url;
436 
437     if (!link)
438 	return;
439 
440     Strcat_charp(html, "<hr width=50%><h1>Link information</h1><table>\n");
441     for (l = link; l; l = l->next) {
442 	if (l->url) {
443 	    parseURL2(l->url, &pu, baseURL(buf));
444 	    url = html_quote(parsedURL2Str(&pu)->ptr);
445 	}
446 	else
447 	    url = "(empty)";
448 	Strcat_m_charp(html, "<tr valign=top><td><a href=\"", url, "\">",
449 		       l->title ? html_quote(l->title) : "(empty)", "</a><td>",
450 		       NULL);
451 	if (l->type == LINK_TYPE_REL)
452 	    Strcat_charp(html, "[Rel]");
453 	else if (l->type == LINK_TYPE_REV)
454 	    Strcat_charp(html, "[Rev]");
455 	if (!l->url)
456 	    url = "(empty)";
457 	else
458 	    url = html_quote(url_decode2(l->url, buf));
459 	Strcat_m_charp(html, "<td>", url, NULL);
460 	if (l->ctype)
461 	    Strcat_m_charp(html, " (", html_quote(l->ctype), ")", NULL);
462 	Strcat_charp(html, "\n");
463     }
464     Strcat_charp(html, "</table>\n");
465 }
466 
467 /* append frame URL */
468 static void
append_frame_info(Buffer * buf,Str html,struct frameset * set,int level)469 append_frame_info(Buffer *buf, Str html, struct frameset *set, int level)
470 {
471     char *p, *q;
472     int i, j;
473 
474     if (!set)
475 	return;
476 
477     for (i = 0; i < set->col * set->row; i++) {
478 	union frameset_element frame = set->frame[i];
479 	if (frame.element != NULL) {
480 	    switch (frame.element->attr) {
481 	    case F_UNLOADED:
482 	    case F_BODY:
483 		if (frame.body->url == NULL)
484 		    break;
485 		Strcat_charp(html, "<pre_int>");
486 		for (j = 0; j < level; j++)
487 		    Strcat_charp(html, "   ");
488 		q = html_quote(frame.body->url);
489 		Strcat_m_charp(html, "<a href=\"", q, "\">", NULL);
490 		if (frame.body->name) {
491 		    p = html_quote(url_unquote_conv(frame.body->name,
492 						    buf->document_charset));
493 		    Strcat_charp(html, p);
494 		}
495 		if (DecodeURL)
496 		    p = html_quote(url_decode2(frame.body->url, buf));
497 		else
498 		    p = q;
499 		Strcat_m_charp(html, " ", p, "</a></pre_int><br>\n", NULL);
500 #ifdef USE_SSL
501 		if (frame.body->ssl_certificate)
502 		    Strcat_m_charp(html,
503 				   "<blockquote><h2>SSL certificate</h2><pre>\n",
504 				   html_quote(frame.body->ssl_certificate),
505 				   "</pre></blockquote>\n", NULL);
506 #endif
507 		break;
508 	    case F_FRAMESET:
509 		append_frame_info(buf, html, frame.set, level + 1);
510 		break;
511 	    }
512 	}
513     }
514 }
515 
516 /*
517  * information of current page and link
518  */
519 Buffer *
page_info_panel(Buffer * buf)520 page_info_panel(Buffer *buf)
521 {
522     Str tmp = Strnew_size(1024);
523     Anchor *a;
524     ParsedURL pu;
525     TextListItem *ti;
526     struct frameset *f_set = NULL;
527     int all;
528     char *p, *q;
529 #ifdef USE_M17N
530     wc_ces_list *list;
531     char charset[16];
532 #endif
533     Buffer *newbuf;
534 
535     Strcat_charp(tmp, "<html><head>\
536 <title>Information about current page</title>\
537 </head><body>\
538 <h1>Information about current page</h1>\n");
539     if (buf == NULL)
540 	goto end;
541     all = buf->allLine;
542     if (all == 0 && buf->lastLine)
543 	all = buf->lastLine->linenumber;
544 #ifdef USE_M17N
545     Strcat_charp(tmp, "<form method=internal action=charset>");
546 #endif
547     p = url_decode2(parsedURL2Str(&buf->currentURL)->ptr, NULL);
548     Strcat_m_charp(tmp, "<table cellpadding=0>",
549 		   "<tr valign=top><td nowrap>Title<td>",
550 		   html_quote(buf->buffername),
551 		   "<tr valign=top><td nowrap>Current URL<td>",
552 		   html_quote(p),
553 		   "<tr valign=top><td nowrap>Document Type<td>",
554 		   buf->real_type ? html_quote(buf->real_type) : "unknown",
555 		   "<tr valign=top><td nowrap>Last Modified<td>",
556 		   html_quote(last_modified(buf)), NULL);
557 #ifdef USE_M17N
558     if (buf->document_charset != InnerCharset) {
559 	list = wc_get_ces_list();
560 	Strcat_charp(tmp,
561 		     "<tr><td nowrap>Document Charset<td><select name=charset>");
562 	for (; list->name != NULL; list++) {
563 	    sprintf(charset, "%d", (unsigned int)list->id);
564 	    Strcat_m_charp(tmp, "<option value=", charset,
565 			   (buf->document_charset == list->id) ? " selected>"
566 			   : ">", list->desc, NULL);
567 	}
568 	Strcat_charp(tmp, "</select>");
569 	Strcat_charp(tmp, "<tr><td><td><input type=submit value=Change>");
570     }
571 #endif
572     Strcat_m_charp(tmp,
573 		   "<tr valign=top><td nowrap>Number of lines<td>",
574 		   Sprintf("%d", all)->ptr,
575 		   "<tr valign=top><td nowrap>Transferred bytes<td>",
576 		   Sprintf("%lu", (unsigned long)buf->trbyte)->ptr, NULL);
577 
578     a = retrieveCurrentAnchor(buf);
579     if (a != NULL) {
580 	parseURL2(a->url, &pu, baseURL(buf));
581 	p = parsedURL2Str(&pu)->ptr;
582 	q = html_quote(p);
583 	if (DecodeURL)
584 	    p = html_quote(url_decode2(p, buf));
585 	else
586 	    p = q;
587 	Strcat_m_charp(tmp,
588 		       "<tr valign=top><td nowrap>URL of current anchor<td><a href=\"",
589 		       q, "\">", p, "</a>", NULL);
590     }
591     a = retrieveCurrentImg(buf);
592     if (a != NULL) {
593 	parseURL2(a->url, &pu, baseURL(buf));
594 	p = parsedURL2Str(&pu)->ptr;
595 	q = html_quote(p);
596 	if (DecodeURL)
597 	    p = html_quote(url_decode2(p, buf));
598 	else
599 	    p = q;
600 	Strcat_m_charp(tmp,
601 		       "<tr valign=top><td nowrap>URL of current image<td><a href=\"",
602 		       q, "\">", p, "</a>", NULL);
603     }
604     a = retrieveCurrentForm(buf);
605     if (a != NULL) {
606 	FormItemList *fi = (FormItemList *)a->url;
607 	p = form2str(fi);
608 	p = html_quote(url_decode2(p, buf));
609 	Strcat_m_charp(tmp,
610 		       "<tr valign=top><td nowrap>Method/type of current form&nbsp;<td>",
611 		       p, NULL);
612 	if (fi->parent->method == FORM_METHOD_INTERNAL
613 	    && !Strcmp_charp(fi->parent->action, "map"))
614 	    append_map_info(buf, tmp, fi->parent->item);
615     }
616     Strcat_charp(tmp, "</table>\n");
617 #ifdef USE_M17N
618     Strcat_charp(tmp, "</form>");
619 #endif
620 
621     append_link_info(buf, tmp, buf->linklist);
622 
623     if (buf->document_header != NULL) {
624 	Strcat_charp(tmp, "<hr width=50%><h1>Header information</h1><pre>\n");
625 	for (ti = buf->document_header->first; ti != NULL; ti = ti->next)
626 	    Strcat_m_charp(tmp, "<pre_int>", html_quote(ti->ptr),
627 			   "</pre_int>\n", NULL);
628 	Strcat_charp(tmp, "</pre>\n");
629     }
630 
631     if (buf->frameset != NULL)
632 	f_set = buf->frameset;
633     else if (buf->bufferprop & BP_FRAME &&
634 	     buf->nextBuffer != NULL && buf->nextBuffer->frameset != NULL)
635 	f_set = buf->nextBuffer->frameset;
636 
637     if (f_set) {
638 	Strcat_charp(tmp, "<hr width=50%><h1>Frame information</h1>\n");
639 	append_frame_info(buf, tmp, f_set, 0);
640     }
641 #ifdef USE_SSL
642     if (buf->ssl_certificate)
643 	Strcat_m_charp(tmp, "<h1>SSL certificate</h1><pre>\n",
644 		       html_quote(buf->ssl_certificate), "</pre>\n", NULL);
645 #endif
646   end:
647     Strcat_charp(tmp, "</body></html>");
648     newbuf = loadHTMLString(tmp);
649 #ifdef USE_M17N
650     if (newbuf)
651 	newbuf->document_charset = buf->document_charset;
652 #endif
653     return newbuf;
654 }
655