/*
 * comment.c
 * jskim@bulls.kordic.re.kr
 */

#include "comment.h"

int GetUniqueFileName(FileName)
	char *FileName;
{
	pid_t		pid;
	time_t		timeval;
	int		i;

	time(&timeval);
	pid = getpid();

	sprintf(FileName, "Comment%d%d", timeval, pid);

	return (int) 1;
}

int main()
{
	FILE		*fp;
	char		comFile[256], email[256];	/* file to comment */
	char		tmpFile[40];
	char		*comment;
	postentry	entries[10];

	char		buf[256];
	register	int	i, x;
	int sendNow = NO;

	printf("content-type: text/html\n\n");	/* HTML Header */
	printf("<html>\n");
	printf("<head>\n");
	printf("<title>Comment To WebMaster</title>\n");
	printf("</head>\n");
	printf("<body bgcolor=\"black\">\n");

	i = ProcessPostMethod( entries );

	GetUniqueFileName(tmpFile);

	email[0] = '\0';
	for (x = 0; x <= i; x++)
	{
		if (!strcasecmp(entries[x].name, "comFile"))
		{
			strcpy(comFile, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "email"))
		{
			strcpy(email, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "comment"))
		{
			comment = (char *) malloc(strlen(entries[x].val)+1);
			strcpy(comment, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "submit") && \
			!strcasecmp(entries[x].val, "send"))
		{
			sendNow = YES;
			continue;
		}
	}

	if (email[0] == '\0') sendNow = NO;
	if (sendNow)
	{
		sprintf(buf, "%s/%s", TEMP_DIR, tmpFile);
		if ( (fp=fopen(buf, "w")) == NULL )
		{
			printf("File Open Error<br>\n");
			exit(1);
		}
		else
		{
			getdate(buf);
			fprintf(fp, "Comments\n========\n");
			fprintf(fp, "From: %s\n", email);
			fprintf(fp, "Date: %s\n", buf);
			fprintf(fp, "File To Comment: %s\n\n", comFile);
			fprintf(fp, "%s\n", comment);
			fclose(fp);
		}
		
		printf("<table width=100%c height=100%c\n>", '%', '%');
		printf("<tr><th valign=middle>\n");
		printf("<table cellpadding=20 border=0 bgcolor=#BBBBBB>\n");
		printf("<tr><th bgcolor=#dddddd>\n");
		printf("<center>Your comments were successfuly sent\n");
		printf("to the WebMaster.<P><font size=+3 color=blue>Thanks.</font><br></center>\n");
		printf("</th></tr></table>\n");
		printf("</th></tr></table>\n");
		exit(0);
	}
	else
	{
	    char *ptr;
	    strcpy(buf, comFile);
	    ptr = (char *)strchr(buf, '/');
	    strcpy(buf, ptr+1);
	    for (i=strlen(buf); i > 0; i--) {
		if (buf[i] == '.') {
		    if (!strcasecmp(buf+i, ".jpg"))
			strcpy(buf+i, "-s160.jpg");
		    else if (!strcasecmp(buf+i, ".gif"))
			strcpy(buf+i, "-s160.gif");
		    break;
		}
	    }

	    printf("<center><p><br>&nbsp;%s\n", comFile);
	    printf("<form action=\"comment.cgi\" method=\"POST\">\n");
	    printf("<table cellpadding=1 border=1 bgcolor=#BBBBBB>\n");
	    printf("<tr><th><a href=ViewImg.cgi?img=%s>", comFile);
	    printf("<img border=3 src=PREVIEW/%s></a>\n", buf);
	    printf("<th><font size=+2>Comment to the Webmaster</font><br>\n");
	    printf("About<br><font size=-1>%s</font><br>\n", comFile);
	    printf("<input type=hidden name=comFile value=\"%s\">\n", comFile);
	    printf("<tr><td colspan=2>\n");
	    printf("<br>Your Email Address: <input type=\"text\" name=\"email\" value=\"%s\" size=40>\n", email);
	    printf("<tr><td colspan=2>\n");
	    printf("<br><b>Comments:</b><br><textarea name=\"comment\" cols=60 rows=15></textarea>");
	    printf("<tr><th colspan=2>\n");
	    printf("<input type=\"submit\" name=\"submit\" value=\"Send\"> \n");
	    printf("<input type=\"reset\" value=\"Clear\"> \n");
	    printf("</table>\n</form>\n");
	}

	free(comment);

	exit(0);
}

/* NCSA Web Library */
void 
getword(char *word, char *line, char stop)
{
	int             x = 0, y;

	for (x = 0; ((line[x]) && (line[x] != stop)); x++)
		word[x] = line[x];

	word[x] = '\0';
	if (line[x])
		++x;
	y = 0;

	while (line[y++] = line[x++]);
}

char           *
makeword(char *line, char stop)
{
	int             x = 0, y;
	char           *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));

	for (x = 0; ((line[x]) && (line[x] != stop)); x++)
		word[x] = line[x];

	word[x] = '\0';
	if (line[x])
		++x;
	y = 0;

	while (line[y++] = line[x++]);
	return word;
}

char           *
fmakeword(FILE * f, char stop, int *cl)
{
	int             wsize;
	char           *word;
	int             ll;

	wsize = 102400;
	ll = 0;
	word = (char *) malloc(sizeof(char) * (wsize + 1));

	while (1)
	{
		word[ll] = (char) fgetc(f);
		if (ll == wsize)
		{
			word[ll + 1] = '\0';
			wsize += 102400;
			word = (char *) realloc(word, sizeof(char) * (wsize + 1));
		}
		--(*cl);
		if ((word[ll] == stop) || (feof(f)) || (!(*cl)))
		{
			if (word[ll] != stop)
				ll++;
			word[ll] = '\0';
			return word;
		}
		++ll;
	}
}

char 
x2c(char *what)
{
	register char   digit;

	digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
	digit *= 16;
	digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
	return (digit);
}

void 
unescape_url(char *url)
{
	register int    x, y;

	for (x = 0, y = 0; url[y]; ++x, ++y)
	{
		if ((url[x] = url[y]) == '%')
		{
			url[x] = x2c(&url[y + 1]);
			y += 2;
		}
	}
	url[x] = '\0';
}

void 
plustospace(char *str)
{
	register int    x;

	for (x = 0; str[x]; x++)
		if (str[x] == '+')
			str[x] = ' ';
}

int 
rind(char *s, char c)
{
	register int    x;
	for (x = strlen(s) - 1; x != -1; x--)
		if (s[x] == c)
			return x;
	return -1;
}

int 
getline(char *s, int n, FILE * f)
{
	register int    i = 0;

	while (1)
	{
		s[i] = (char) fgetc(f);

		if (s[i] == CR)
			s[i] = fgetc(f);

		if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1)))
		{
			s[i] = '\0';
			return (feof(f) ? 1 : 0);
		}
		++i;
	}
}

void 
send_fd(FILE * f, FILE * fd)
{
	int             num_chars = 0;
	char            c;

	while (1)
	{
		c = fgetc(f);
		if (feof(f))
			return;
		fputc(c, fd);
	}
}

/* coded by armian@www.kordic.re.kr */

int	ProcessPostMethod(entries)
postentry       *entries;
{
	int	cl;
	int	m, x;

	if (strcmp((char *) getenv("REQUEST_METHOD"), "POST"))
        {
                printf("This script should be referenced with a METHOD of POST.\n");
                printf("If you don't understand this, see this ");
                printf("<A HREF=\"http://www.ncsa.uiuc.edu/SGC/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>/%c", 10);
                exit(0);
        }
        if (strcmp((char *) getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded"))
        {
                printf("This script can only be used to decode form results. \n");
                exit(1);
        }

        cl = atoi(getenv("CONTENT_LENGTH"));

	for (x = 0; cl && (!feof(stdin)); x++)
        {
                m = x;
                entries[x].val = (char *) fmakeword(stdin, '&', &cl);
                plustospace(entries[x].val);
                unescape_url(entries[x].val);
                entries[x].name = (char *) makeword(entries[x].val, '=');
        }

	return m;
}
