# Makefile for Bioinfo Animal Pictures Archive CGIs
# jskim@bioinfo.kordic.re.kr
CC = cc
#CFLAGS = -DFILEOUT
CFLAGS = -O
LIBS =
# CGI directory
CGIBIN_DIR = ../..
ALL = greetings
all: $(ALL)
greetings :
$(CC) -o greetings greetings.c ../getdate.c $(LIBS) $(CFLAGS)
mv -f greetings $(CGIBIN_DIR)/greetings.cgi
chmod 4755 $(CGIBIN_DIR)/greetings.cgi
/* end of Makefile */
/*
* greetings.c
* jskim@www.best5.net
*/
#include "greetings.h"
int GetUniqueFileName(FileName)
char *FileName;
{
pid_t pid;
time_t timeval;
int i;
time(&timeval);
pid = getpid();
sprintf(FileName, "%05d%05d", timeval%1000000, pid);
return (int) 1;
}
int main()
{
FILE *fp, *fout;
char ToName[256], ToEmail[256];
char FromName[256], FromEmail[256];
char tmpFile[256], path[256];
char imgName[256]; /* `archive dir`/`file name` */
char Message[8192];
char ErrMsg[1024];
postentry entries[10];
char buf[256];
char line[256];
register int i, x;
int sendNow = NO;
int preview = NO;
printf("content-type: text/html\n\n"); /* HTML Header */
i = ProcessPostMethod( entries );
ToEmail[0] = '\0';
ToName[0] = '\0';
FromEmail[0] = '\0';
FromName[0] = '\0';
imgName[0] = '\0';
Message[0] = '\0';
ErrMsg[0] = '\0';
for (x = 0; x <= i; x++)
{
if (!strcasecmp(entries[x].name, "img")) {
strcpy(imgName, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "FromEmail")) {
strcpy(FromEmail, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "FromName")) {
strcpy(FromName, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "ToEmail")) {
strcpy(ToEmail, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "ToName")) {
strcpy(ToName, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "Message")) {
strcpy(Message, entries[x].val);
continue;
}
if (!strcasecmp(entries[x].name, "Command")) {
if (!strncasecmp(entries[x].val, "send", 4)) sendNow=YES;
else if (!strncasecmp(entries[x].val, "preview", 7)) preview=YES;
else if (!strncasecmp(entries[x].val, "edit", 7)) {
sendNow=NO;
preview=NO;
}
/*
printf("EntVal=%s
", entries[x].val);
printf("SendNow=%d
", sendNow);
printf("PREVIEW=%d
", preview);
*/
continue;
}
if (!strcasecmp(entries[x].name, "ErrMsg")) {
strcpy(ErrMsg, entries[x].val);
continue;
}
}
/* check data integraty */
if ( strlen(ToName) < 1) {
sprintf(ErrMsg, "Recipient's name is invalid.");
sendNow = NO;
}
if ( strlen(ToEmail) < 7 || !strstr(ToEmail, "@") ) {
sprintf(ErrMsg, "Please Check out the recipient's E-amil address");
sendNow = NO;
}
if ( strlen(FromName) < 1) {
sprintf(ErrMsg, "Sender's name is invalid.");
sendNow = NO;
}
if ( strlen(FromEmail) < 7 || !strstr(ToEmail, "@") ) {
sprintf(ErrMsg, "Please Check out the sender's E-amil address");
sendNow = NO;
}
/* end of integrity check */
if (sendNow) {
GetUniqueFileName(tmpFile);
sprintf(path, "%s/%s", TEMP_DIR, tmpFile);
fout = fopen(path, "w");
if (fout == NULL) {
printf("File Open Error\n");
exit(0);
}
fprintf(fout, "%s(%s) sent you a greeting card!\n",FromName,FromEmail);
fprintf(fout, "To view your greeting, click the following address\n");
fprintf(fout, "\n%s/GREETINGS/%s.html\n",URL_BASE, tmpFile);
fprintf(fout, "\nYour card will be kept over the next two weeks.\n");
fprintf(fout, "\nThanks.\n");
fprintf(fout, "\n\n(C)opyLeft, Animal Pictures Archive\n");
fprintf(fout, " http://AnimalPicturesArchive.com/\n");
fclose(fout);
sprintf(buf, "/bin/mail -s \"A greeting card from %s\" %s < %s", FromName, ToEmail, path);
system(buf);
unlink(path);
sprintf(path, "%s/%s.html", TEMP_DIR, tmpFile);
fout = fopen(path, "w");
if (fout == NULL) {
printf("File [%s] open error\n", path);
exit(0);
}
}
else {
fout = stdout;
}
if (sendNow || preview) {
getdate(buf);
sprintf(ErrMsg, "Sent on %s by \"%s\" <%s> to \"%s\" <%s>\n", buf, FromName, FromEmail, ToName, ToEmail);
}
fp = fopen(TEMPLATE, "r");
if (fp == NULL) {
printf("Template [%s] open error\n", TEMPLATE);
exit(0);
}
while(fgets(line, 256, fp)) {
char *ptr;
if (!strncmp(line, "___FROM_NAME_HERE___", 20)) {
if (sendNow || preview)
fprintf(fout, "%s\n", FromName,FromName);
else
fprintf(fout, "Name
\n", FromName);
}
else if (!strncmp(line, "___FROM_EMAIL_HERE___", 21)) {
if (sendNow || preview) {
fprintf(fout, "(%s)\n", FromEmail,FromEmail);
}
else {
fprintf(fout, " ");
fprintf(fout, " \n");
fprintf(fout, "E-mail \n", FromEmail);
}
}
else if (!strncmp(line, "___TO_NAME_HERE___", 18)) {
if (sendNow || preview)
fprintf(fout, "%s\n", ToName,ToName);
else
fprintf(fout, "Name \n", ToName);
}
else if (!strncmp(line, "___TO_EMAIL_HERE___", 19)) {
if (sendNow || preview) {
fprintf(fout, "(%s)\n", ToEmail,ToEmail);
}
else {
fprintf(fout, "E-mail \n", ToEmail);
}
}
else if (!strncmp(line, "___MESSAGE_BODY_HERE___", 23)) {
if (sendNow || preview) {
fprintf(fout, "\n", Message);
fprintf(fout, "\n");
for (i=0; i < strlen(Message); i++) {
if (Message[i] == '\n') fprintf(fout, "
\n");
else fputc(Message[i], fout);
}
}
else {
fprintf(fout, "\n", Message);
}
}
else if (ptr = (char *)strstr(line, "___IMAGE_PATH_HERE___", 21)) {
*ptr = '\0';
fprintf(fout, "%s", line);
fprintf(fout, "%s", imgName);
fprintf(fout, "%s", ptr+21);
}
else if (!strncmp(line, "___ERROR_MESSAGE_HERE___", 24)) {
fprintf(fout, "%s\n", ErrMsg);
}
else if (!strncmp(line, "___SEND_BUTTON_HERE___", 22)) {
fprintf(fout, "\n", imgName);
if (!sendNow) {
fprintf(fout, "\n");
}
}
else if (!strncmp(line, "___PREVIEW_BUTTON_HERE___", 25)) {
if (!sendNow) {
fprintf(fout, "\n");
}
}
else if (!strncmp(line, "___EDIT_BUTTON_HERE___", 22)) {
if (preview) {
fprintf(fout, "\n");
}
}
else {
fprintf(fout, "%s", line);
}
}
fclose(fout);
fclose(fp);
if (sendNow) {
fp = fopen(DONE_HTML, "r");
if (fp == NULL) {
printf("Template [%s] open error\n", DONE_HTML);
exit(0);
}
while(fgets(line, 256, fp)) {
if (!strncmp(line, "___FROM_NAME_HERE___", 20))
printf("%s\n", FromName);
else if (!strncmp(line, "___TO_NAME_HERE___", 18))
printf("%s\n", ToName);
else
printf("%s", line);
}
}
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("forms overview/%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;
}
/* start of greetings.h */
#include
#include
#include
#ifndef NO_STDLIB_H
#include
#else
char *getenv();
#endif
#define TEMP_DIR "/home/httpd/html/animal/GREETINGS"
#define TEMPLATE "/home/httpd/html/animal/HTMLSrc/GR_VIEW.html"
#define DONE_HTML "/home/httpd/html/animal/HTMLSrc/GR_DONE.html"
#define URL_BASE "http://AnimalPicturesArchive.com/animal"
#define TRUE 1
#define FALSE 0
#define YES 1
#define NO 0
#define LF 10
#define CR 13
#define MAX_ENTRIES 10
/* for Get method */
typedef struct {
char name[256];
char val[256];
} getentry;
/* for Post method */
typedef struct {
char *name;
char *val;
} postentry;
/* For Using Get Method */
void getword(char *word, char *line, char stop);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);
/* For Using Post Method */
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);