/*
FILE
	tDUL source code of mysqlSendmail.cgi
	(built by mysqlRAD2.cgi (c) Gary Wallis 2001,2002 GPL Licensed)
	$Id: tdul.c 6 2005-11-24 00:02:59Z ggw $
	(tAuthorize.cPasswd template set member)
PURPOSE
*/

#include "mysqlrad.h"

//Table Variables
//uDUL: Primary Key
static unsigned uDUL=0;
//cLabel: Short label
static char cLabel[33]={""};
//uProviderID: POP Provider ID if known
static unsigned uProviderID=0;
//cRangeList: List of class c, class c, supernet CIDR blocks
static char *cRangeList={""};
//uServer: Server account is on
static unsigned uServer=0;
static char cuServerPullDown[256]={""};
//uStatus: Status of DUL
static unsigned uStatus=0;
static char cuStatusPullDown[256]={""};
//uOwner: Record owner
static unsigned uOwner=0;
//uCreatedBy: uClient for last insert
static unsigned uCreatedBy=0;
#define ISM3FIELDS
//uCreatedDate: Unix seconds date last insert
static long unsigned uCreatedDate=0;
//uModBy: uClient for last update
static unsigned uModBy=0;
//uModDate: Unix seconds date last update
static long unsigned uModDate=0;




#define VAR_LIST_tDUL "tDUL.uDUL,tDUL.cLabel,tDUL.uProviderID,tDUL.cRangeList,tDUL.uServer,tDUL.uStatus,tDUL.uOwner,tDUL.uCreatedBy,tDUL.uCreatedDate,tDUL.uModBy,tDUL.uModDate"

 //Local only
void Insert_tDUL(void);
void Update_tDUL(char *rowid);
int InsertUpdate_tDUL(char *cKey);
void ProcesstDULListVars(pentry entries[], int x);

 //In tDULfunc.h file included below
void ExtProcesstDULVars(pentry entries[], int x);
void ExttDULCommands(pentry entries[], int x);
void ExttDULButtons(void);
void ExttDULNavBar(void);
void ExttDULGetHook(entry gentries[], int x);
void ExttDULSelect(void);
void ExttDULSelectRow(void);
void ExttDULListSelect(void);
void ExttDULListFilter(void);
void ExttDULAuxTable(void);

#include "tdulfunc.h"

 //Table Variables Assignment Function
void ProcesstDULVars(pentry entries[], int x)
{
	register int i;


	for(i=0;i<x;i++)
	{
		if(!strcmp(entries[i].name,"uDUL"))
			sscanf(entries[i].val,"%u",&uDUL);
		else if(!strcmp(entries[i].name,"cLabel"))
			sprintf(cLabel,"%.32s",entries[i].val);
		else if(!strcmp(entries[i].name,"uProviderID"))
			sscanf(entries[i].val,"%u",&uProviderID);
		else if(!strcmp(entries[i].name,"cRangeList"))
			cRangeList=entries[i].val;
		else if(!strcmp(entries[i].name,"uServer"))
			sscanf(entries[i].val,"%u",&uServer);
		else if(!strcmp(entries[i].name,"cuServerPullDown"))
		{
			sprintf(cuServerPullDown,"%.255s",entries[i].val);
			uServer=ReadPullDown("tServer","cLabel",cuServerPullDown);
		}
		else if(!strcmp(entries[i].name,"uStatus"))
			sscanf(entries[i].val,"%u",&uStatus);
		else if(!strcmp(entries[i].name,"cuStatusPullDown"))
		{
			sprintf(cuStatusPullDown,"%.255s",entries[i].val);
			uStatus=ReadPullDown("tStatus","cLabel",cuStatusPullDown);
		}
		else if(!strcmp(entries[i].name,"uOwner"))
			sscanf(entries[i].val,"%u",&uOwner);
		else if(!strcmp(entries[i].name,"uCreatedBy"))
			sscanf(entries[i].val,"%u",&uCreatedBy);
		else if(!strcmp(entries[i].name,"uCreatedDate"))
			sscanf(entries[i].val,"%lu",&uCreatedDate);
		else if(!strcmp(entries[i].name,"uModBy"))
			sscanf(entries[i].val,"%u",&uModBy);
		else if(!strcmp(entries[i].name,"uModDate"))
			sscanf(entries[i].val,"%lu",&uModDate);

	}

	//After so we can overwrite form data if needed.
	ExtProcesstDULVars(entries,x);

}//ProcesstDULVars()


void ProcesstDULListVars(pentry entries[], int x)
{
        register int i;

        for(i=0;i<x;i++)
        {
                if(!strncmp(entries[i].name,"ED",2))
                {
                        sscanf(entries[i].name+2,"%u",&uDUL);
                        mode=6;
                        tDUL("");
                }
        }
}//void ProcesstDULListVars(pentry entries[], int x)


int tDULCommands(pentry entries[], int x)
{
	ProcessControlVars(entries,x);

	ExttDULCommands(entries,x);

	if(!strcmp(function,"tDULTools"))
	{
		if(!strcmp(find,LANG_NB_LIST))
		{
			tDULList();
		}

		//Default
		ProcesstDULVars(entries,x);
		tDUL("");
	}
	else if(!strcmp(function,"tDULList"))
	{
		ProcessControlVars(entries,x);
		ProcesstDULListVars(entries,x);
		tDULList();
	}

	return(0);

}//tDULCommands()


void tDUL(const char *result)
{
	MYSQL_RES *res;
	MYSQL_RES *res2;
	MYSQL_ROW field;

	//Internal skip reloading
	if(!result[0])
	{
		if(mode)
			ExttDULSelectRow();
		else
			ExttDULSelect();

		mysql_query(&mysql,query);
		if(mysql_errno(&mysql))
        	{
			if(strstr(mysql_error(&mysql)," doesn't exist"))
                	{
				CreatetDUL();
				mysqlSendmail("New tDUL table created");
                	}
			else
			{
				mysqlSendmail(mysql_error(&mysql));
			}
        	}

		res=mysql_store_result(&mysql);
		if((i=mysql_num_rows(res)))
		{
			if(mode==6)
			{
			sprintf(query,"SELECT _rowid FROM tDUL WHERE uDUL=%u"
						,uDUL);
			mysql_query(&mysql,query);
			res2=mysql_store_result(&mysql);
			field=mysql_fetch_row(res2);
			sscanf(field[0],"%lu",&rowid);
			rowid++;
			}
			PageMachine("",0,"");
			if(!mode) mysql_data_seek(res,rowid-1);
			field=mysql_fetch_row(res);
		sscanf(field[0],"%u",&uDUL);
		sprintf(cLabel,"%.32s",field[1]);
		sscanf(field[2],"%u",&uProviderID);
		cRangeList=field[3];
		sscanf(field[4],"%u",&uServer);
		sscanf(field[5],"%u",&uStatus);
		sscanf(field[6],"%u",&uOwner);
		sscanf(field[7],"%u",&uCreatedBy);
		sscanf(field[8],"%lu",&uCreatedDate);
		sscanf(field[9],"%u",&uModBy);
		sscanf(field[10],"%lu",&uModDate);

		}

	}//Internal Skip

	Header_ism3(":: tDUL",0);
	printf("<form action=mysqlSendmail.cgi method=post >\n");
	printf("<table width=900 border=0 bgcolor=#A9BEC0 valign=center>\n");
	printf("<tr><td colspan=2><font face=Arial,Helvetica size=2>");


	ExttDULNavBar();

	printf("<input type=hidden name=function value=tDULTools>");
	printf("<input type=hidden name=rowid value=%lu>",rowid);
	if(i)
	{
		printf(" <font color=blue >");
		if(mode==6)
			//printf(" Found");
			printf(LANG_NBR_FOUND);
		else if(mode==5)
			//printf(" Modified");
			printf(LANG_NBR_MODIFIED);
		else if(mode==4)
			//printf(" New");
			printf(LANG_NBR_NEW);
		printf(LANG_NBRF_SHOWING,rowid,i);
	}
	else
	{
		if(!result[0])
		//printf(" No records found");
		printf(LANG_NBR_NORECS);
	}
	if(result[0]) printf(" <b> %s",result);
	printf("</td></tr>");
	printf("<tr><td valign=top>");

        ExttDULButtons();

        printf("</td><td valign=top>");
	printf("<table width=630 border=0 bgcolor=#9BC1B3 valign=center>\n");

	if(mode==2000 || mode==2002)
		tDULInputContent();
	else
		tDULContent();

	printf("</table>\n");
	printf("</table>\n");

	//Bottom table
        ExttDULAuxTable();

	printf("</font></form>\n");
	Footer_ism3();

}//end of tDUL();


void tDULContent(void)
{

	OpenRow(LANG_FL_tDUL_uDUL,"white");
	printf("%u<input type=hidden name=uDUL value=%u >\n",uDUL,uDUL);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_cLabel,EmptyString(cLabel));
	printf("%s<input type=hidden name=cLabel value=\"%s\">",cLabel,EncodeDoubleQuotes(cLabel));
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uProviderID,"white");
	printf("%u<input type=hidden name=uProviderID value=%u >\n",uProviderID,uProviderID);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_cRangeList,EmptyString(cRangeList));
	printf("<pre><font face=Arial,Helvetica>%s</pre>\n\n",
		TransformAngleBrackets(cRangeList));
	printf("<input type=hidden name=cRangeList value=\"%s\">",
		EncodeDoubleQuotes(cRangeList));
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uServer,IsZero(uServer));
	tTablePullDownReadOnly("tServer;cuServerPullDown","cLabel","cLabel",uServer);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uStatus,IsZero(uStatus));
	tTablePullDownReadOnly("tStatus;cuStatusPullDown","cLabel","cLabel",uStatus);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uOwner,"white");
	printf("%s<input type=hidden name=uOwner value=%u >\n",ForeignKey("tClient","cLabel",uOwner),uOwner);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uCreatedBy,"white");
	printf("%s<input type=hidden name=uCreatedBy value=%u >\n",ForeignKey("tClient","cLabel",uCreatedBy),uCreatedBy);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uCreatedDate,"white");
	if(uCreatedDate)
		printf("%s\n\n",ctime((time_t *)&uCreatedDate));
	else
		printf("---\n\n");
	printf("<input type=hidden name=uCreatedDate value=%lu >\n",uCreatedDate);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uModBy,"white");
	printf("%s<input type=hidden name=uModBy value=%u >\n",ForeignKey("tClient","cLabel",uModBy),uModBy);
	printf("&nbsp;&nbsp;");
	OpenRow(LANG_FL_tDUL_uModDate,"white");
	if(uModDate)
		printf("%s\n\n",ctime((time_t *)&uModDate));
	else
		printf("---\n\n");
	printf("<input type=hidden name=uModDate value=%lu >\n",uModDate);
	printf("&nbsp;&nbsp;");
	printf("</tr>\n");



}//tDULContent()


void tDULInputContent(void)
{

	OpenRow(LANG_FL_tDUL_uDUL,"white");
	if(uPermLevel>=20)
		printf("<input title='%s' type=text name=uDUL value=%u size=16 maxlength=10>\n",LANG_FT_tDUL_uDUL,uDUL);
	else
		printf("%u<input type=hidden name=uDUL value=%u >\n",uDUL,uDUL);
	OpenRow(LANG_FL_tDUL_cLabel,EmptyString(cLabel));
	if(uPermLevel>=8)
		printf("<input title='%s' type=text name=cLabel value=\"%s\" size=40 maxlength=32>\n",LANG_FT_tDUL_cLabel,EncodeDoubleQuotes(cLabel));
	else
		printf("%s<input type=hidden name=cLabel value=\"%s\">",cLabel,EncodeDoubleQuotes(cLabel));
	OpenRow(LANG_FL_tDUL_uProviderID,"white");
	if(uPermLevel>=8)
		printf("<input title='%s' type=text name=uProviderID value=%u size=16 maxlength=10>\n",LANG_FT_tDUL_uProviderID,uProviderID);
	else
		printf("%u<input type=hidden name=uProviderID value=%u >\n",uProviderID,uProviderID);
	OpenRow(LANG_FL_tDUL_cRangeList,EmptyString(cRangeList));
	if(uPermLevel>=8)
		printf("<textarea title='%s' cols=60 wrap=hard rows=8 name=cRangeList>%s</textarea>\n\n",LANG_FT_tDUL_cRangeList,cRangeList);
	else
	{
		printf("<pre><font face=Arial,Helvetica>%s</pre>\n\n",
			TransformAngleBrackets(cRangeList));
		printf("<input type=hidden name=cRangeList value=\"%s\">",
			EncodeDoubleQuotes(cRangeList));
	}
	OpenRow(LANG_FL_tDUL_uServer,IsZero(uServer));
	if(uPermLevel>=8)
		tTablePullDown("tServer;cuServerPullDown","cLabel","cLabel",uServer);
	else
	{
		tTablePullDownReadOnly("tServer;cuServerPullDown","cLabel","cLabel",uServer);
	}
	OpenRow(LANG_FL_tDUL_uStatus,IsZero(uStatus));
	if(uPermLevel>=8)
		tTablePullDown("tStatus;cuStatusPullDown","cLabel","cLabel",uStatus);
	else
	{
		tTablePullDownReadOnly("tStatus;cuStatusPullDown","cLabel","cLabel",uStatus);
	}
	OpenRow(LANG_FL_tDUL_uOwner,"white");
	printf("%s<input type=hidden name=uOwner value=%u >\n",ForeignKey("tClient","cLabel",uOwner),uOwner);
	OpenRow(LANG_FL_tDUL_uCreatedBy,"white");
	printf("%s<input type=hidden name=uCreatedBy value=%u >\n",ForeignKey("tClient","cLabel",uCreatedBy),uCreatedBy);
	OpenRow(LANG_FL_tDUL_uCreatedDate,"white");
	if(uCreatedDate)
		printf("%s\n\n",ctime((time_t *)&uCreatedDate));
	else
		printf("---\n\n");
	printf("<input type=hidden name=uCreatedDate value=%lu >\n",uCreatedDate);
	OpenRow(LANG_FL_tDUL_uModBy,"white");
	printf("%s<input type=hidden name=uModBy value=%u >\n",ForeignKey("tClient","cLabel",uModBy),uModBy);
	OpenRow(LANG_FL_tDUL_uModDate,"white");
	if(uModDate)
		printf("%s\n\n",ctime((time_t *)&uModDate));
	else
		printf("---\n\n");
	printf("<input type=hidden name=uModDate value=%lu >\n",uModDate);
	printf("</tr>\n");



}//void tDULInputContent(void)


void NewtDUL(unsigned uMode)
{
	register int i=0;
	MYSQL_RES *res;

	//New select
	sprintf(query,"SELECT uDUL FROM tDUL\
				WHERE uDUL=%u"
							,uDUL);
	mysql_query(&mysql,query);
	if(mysql_errno(&mysql)) mysqlSendmail(mysql_error(&mysql));
	res=mysql_store_result(&mysql);
	i=mysql_num_rows(res);

	if(i) 
		//tDUL("<blink>Record already exists");
		tDUL(LANG_NBR_RECEXISTS);

	//insert query
	Insert_tDUL();
	if(mysql_errno(&mysql)) mysqlSendmail(mysql_error(&mysql));
	//sprintf(query,"New record %u added");
	uDUL=mysql_insert_id(&mysql);

	if(!uMode)
	{
	sprintf(query,LANG_NBR_NEWRECADDED,uDUL);
	tDUL(query);
	}

}//NewtDUL(unsigned uMode)


void DeletetDUL(void)
{
#ifdef ISM3FIELDS
	sprintf(query,"DELETE FROM tDUL WHERE uDUL=%u AND ( uOwner=%u OR %u>9 )"
					,uDUL,uLoginClient,uPermLevel);
#else
	sprintf(query,"DELETE FROM tDUL WHERE uDUL=%u"
					,uDUL);
#endif
	mysql_query(&mysql,query);
	if(mysql_errno(&mysql)) mysqlSendmail(mysql_error(&mysql));

	//tDUL("Record Deleted");
	if(mysql_affected_rows(&mysql)>0)
		tDUL(LANG_NBR_RECDELETED);
	else
		tDUL(LANG_NBR_RECNOTDELETED);

}//void DeletetDUL(void)


void Insert_tDUL(void)
{

#ifdef ISM3FIELDS
	time_t clock;

        time(&clock);
#endif

	//insert query

	//insert query
	sprintf(query,"INSERT INTO tDUL SET  uDUL=%u, cLabel='%s', uProviderID=%u, cRangeList='%s', uServer=%u, uStatus=%u, uOwner=%u, uCreatedBy=%u, uCreatedDate=%lu, uModBy=%u, uModDate=0",
			uDUL
			,TextAreaSave(cLabel)
			,uProviderID
			,TextAreaSave(cRangeList)
			,uServer
			,uStatus
			,uOwner
			,uCreatedBy
			,(long unsigned)clock
			,uModBy
			);


	mysql_query(&mysql,query);
#ifdef ISM3FIELDS
	uCreatedDate=clock;
#endif

}//void Insert_tDUL(void)


void Update_tDUL(char *rowid)
{
#ifdef ISM3FIELDS
	time_t clock;

        time(&clock);
#endif

	//update query

	//update query
	sprintf(query,"UPDATE tDUL SET uDUL=%u,cLabel='%s',uProviderID=%u,cRangeList='%s',uServer=%u,uStatus=%u,uModBy=%u,uModDate=%lu WHERE _rowid=%s",
			uDUL
			,TextAreaSave(cLabel)
			,uProviderID
			,TextAreaSave(cRangeList)
			,uServer
			,uStatus
			,uModBy
			,(long unsigned)clock
			,rowid);


	mysql_query(&mysql,query);

#ifdef ISM3FIELDS
	uModDate=clock;
#endif

}//void Update_tDUL(void)


void ModtDUL(void)
{
	register int i=0;
	MYSQL_RES *res;
	MYSQL_ROW field;
#ifdef ISM3FIELDS
	unsigned uPreModDate=0;

	//Mod select query
	if(uPermLevel<10)
	sprintf(query,"SELECT tDUL.uDUL,\
				tDUL.uModDate\
				FROM tDUL,tClient\
				WHERE tDUL.uDUL=%u\
				AND tDUL.uOwner=tClient.uClient\
				AND (tClient.uOwner=%u OR tClient.uClient=%u)"
			,uDUL,uLoginClient,uLoginClient);
	else
	sprintf(query,"SELECT uDUL,uModDate FROM tDUL\
				WHERE uDUL=%u"
						,uDUL);
#else
	sprintf(query,"SELECT uDUL FROM tDUL\
				WHERE uDUL=%u"
						,uDUL);
#endif

	mysql_query(&mysql,query);
	if(mysql_errno(&mysql)) mysqlSendmail(mysql_error(&mysql));
	res=mysql_store_result(&mysql);
	i=mysql_num_rows(res);

	//if(i<1) tDUL("<blink>Record does not exist");
	if(i<1) tDUL(LANG_NBR_RECNOTEXIST);
	//if(i>1) tDUL("<blink>Multiple rows!");
	if(i>1) tDUL(LANG_NBR_MULTRECS);

	field=mysql_fetch_row(res);
#ifdef ISM3FIELDS
	sscanf(field[1],"%u",&uPreModDate);
	if(uPreModDate!=uModDate) tDUL(LANG_NBR_EXTMOD);
#endif

	Update_tDUL(field[0]);
	if(mysql_errno(&mysql)) mysqlSendmail(mysql_error(&mysql));
	//sprintf(query,"record %s modified",field[0]);
	sprintf(query,LANG_NBRF_REC_MODIFIED,field[0]);
	tDUL(query);

}//ModtDUL(void)


void tDULList(void)
{
	MYSQL_RES *res;
	MYSQL_ROW field;

	ExttDULListSelect();

	mysql_query(&mysql,query);
	if(mysql_error(&mysql)[0]) mysqlSendmail(mysql_error(&mysql));
	res=mysql_store_result(&mysql);
	i=mysql_num_rows(res);

	PageMachine("tDULList",1,"");//1 is auto header list mode. Opens table!

	//Filter select drop down
	ExttDULListFilter();

	printf("<input type=text size=16 name=command maxlength=98 value=\"%s\" >",command);

	printf("</table>\n");

	printf("<table bgcolor=#9BC1B3 border=0 width=900>\n");
	printf("<tr bgcolor=black><td><font face=arial,helvetica color=white>uDUL<td><font face=arial,helvetica color=white>cLabel<td><font face=arial,helvetica color=white>uProviderID<td><font face=arial,helvetica color=white>cRangeList<td><font face=arial,helvetica color=white>uServer<td><font face=arial,helvetica color=white>uStatus<td><font face=arial,helvetica color=white>uOwner<td><font face=arial,helvetica color=white>uCreatedBy<td><font face=arial,helvetica color=white>uCreatedDate<td><font face=arial,helvetica color=white>uModBy<td><font face=arial,helvetica color=white>uModDate</tr>");



	mysql_data_seek(res,start-1);

	for(n=0;n<(end-start+1);n++)
	{
		field=mysql_fetch_row(res);
		if(!field)
		{
			printf("<tr><td><font face=arial,helvetica>End of data</table>");
			Footer_ism3();
		}
			if(n % 2)
				printf("<tr bgcolor=#BBE1D3>");
			else
				printf("<tr>");
		printf("<td><font face=arial,helvetica size=1><input type=submit name=ED%s value=Edit></font> %s<td>%s<td>%s<td><pre><font face=Arial,Helvetica>%s<td>%s<td>%s<td>%s<td>%s<td>%s<td>%s<td>%s</tr>"
			,field[0]
			,field[0]
			,field[1]
			,field[2]
			,field[3]
			,field[4]
			,field[5]
			,field[6]
			,field[7]
			,field[8]
			,field[9]
			,field[10]
				);

	}

	printf("</table></form>\n");
	Footer_ism3();

}//tDULList()


void CreatetDUL(void)
{
	sprintf(query,"CREATE TABLE tDUL ( uDUL int unsigned PRIMARY KEY AUTO_INCREMENT, cLabel varchar(32) NOT NULL DEFAULT '', uOwner int unsigned NOT NULL DEFAULT 0,index (uOwner), uCreatedBy int unsigned NOT NULL DEFAULT 0, uCreatedDate int unsigned NOT NULL DEFAULT 0, uModBy int unsigned NOT NULL DEFAULT 0, uModDate int unsigned NOT NULL DEFAULT 0, uServer int unsigned NOT NULL DEFAULT 0,index (uServer), cRangeList text NOT NULL DEFAULT '', uProviderID int unsigned NOT NULL DEFAULT 0, uStatus int unsigned NOT NULL DEFAULT 0 )");
	mysql_query(&mysql,qstr);
	if(mysql_errno(&mysql))
		mysqlSendmail(mysql_error(&mysql));
}//CreatetDUL()

//sedall patch1
