#include "config.h"
#ifdef ENABLE_MYSQL

#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <string.h>
#include "global.h"
#include "alphabetize.h"

/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	Edit below to set host, username, password, and database name for mysql option
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */

static const char *host="localhost";
static const char *user="my_user";
static const char *passwd="my_password";  /* NULL for no password */
static const char *db="my_database";

/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		D o   N o T   E d i T  B e L o W (unless you're hackin' source!)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */

static MYSQL *conn=NULL;
static const char *sock=NULL;        
static const unsigned int port=0;
static const unsigned int flags=0; 

extern unsigned int acl_features;
extern char WarningBuff[MAX_WARNING_BUFF];

/* forward defs */
static void submit_warning(char *, char *);

/*
 * Shows the add domain page, retrieving possible owners from 
 * the MySQL db and adding them as selections to a combobox
 */
void display_add_domain(void)
{
	MYSQL_ROW my_row;
	MYSQL_RES *my_result=NULL;
	char *func="display_add_domain";
	struct node_t *list=NULL, *lptr=NULL;

	if(!(acl_features & ACL_DOMAIN_CREATE)) {
    	global_warning("Display domain: Permission denied");
   	 	t_open(T_MAIN, 1);
  	}
	
	memset(&my_row, 0, sizeof(my_row));
	
	/* initialize connection handler */
	conn=mysql_init(NULL);
			                                                                                                                                  if(!conn)
		submit_warning(func, mysql_error(conn));
	else if(!mysql_real_connect(conn, host, user, passwd, db, port, sock, flags)) 
		submit_warning(func, mysql_error(conn));
	else if(mysql_query(conn, "SELECT owner FROM domain_owner GROUP BY owner") != 0) 
		submit_warning(func, mysql_error(conn));
	else { 
		my_result=mysql_use_result(conn);	

		if(!my_result) 
			submit_warning(func, mysql_error(conn));
	}

	t_open(T_ADD_HEAD, 0);

	if(!my_result)
		t_open(T_ADD_BODY, 0);
	else {
		/* build list of query results */
		while((my_row=mysql_fetch_row(my_result))) 
			list=append_string(list, my_row[0]);
		
		/* alphabetize list and display in list box */
		for(lptr=alpha_mergesort(get_head(list)); lptr; lptr=lptr->next) {		
			global_par("OW", lptr->string);
			t_open(T_ADD_BODY, 0);
		}

		free_list(list);
		mysql_free_result(my_result);
	}

	if(conn)	
		mysql_close(conn);
	
	t_open(T_ADD_FOOT, 1);
}


/*
 * Inserts domain owner info into MySQL db
 * PARAM domain
 * 	The domain whose owner info we must insert
 * RETURN
 * 	0 upon success
 * 	1 if domain is NULL
 * 	2 if the values of the domain owner text field or combobox could not be retrieved
 * 	3 if the user did not enter correct input
 */
int insert_owner(char *domain)
{
	char my_query[500]={ 0 };
	char *name=NULL, *selection=NULL, *func="insert_owner", *ptr=NULL;

	if(!(acl_features & ACL_DOMAIN_CREATE)) {
    	global_warning("Insert owner: Permission denied");
    	t_open(T_MAIN, 1);
    }

	if(!domain)
		return 1;		
	
	name=cgi_is_var("owner_text");
	selection=cgi_is_var("owner_select");

	if(!name || !selection)
		return 2;
	
	if((strcmp(name, "") == 0) && (strcmp(selection, "manual_select") != 0)) 	
		name=selection;
	else if((strcmp(name, "") != 0) && (strcmp(selection, "manual_select") == 0)) 
    	;
	else 
		return 3;	
	
	/* strip out any '+' chars the html may have submitted as spaces */
	while((ptr=strchr(name, '+')))
		*ptr=' ';

	snprintf(my_query, sizeof(my_query)-1, "INSERT INTO domain_owner VALUES('%s', '%s')", domain, name);
	
	/* connect to database */
    conn=mysql_init(NULL);
                                                                                                                                          
    if(!conn)
		submit_warning(func, mysql_error(conn));
    else if(!mysql_real_connect(conn, host, user, passwd, db, port, sock, flags))
		submit_warning(func, mysql_error(conn));
    else if(mysql_query(conn, my_query) != 0)
		submit_warning(func, mysql_error(conn));

	if(conn)	
		mysql_close(conn);
	
	return 0;
}


static void submit_warning(char *func, char *msg)
{
	if(!func)
		func="function";
	
	if(!msg)
		msg="problem encountered";
	
	snprintf(WarningBuff, sizeof(WarningBuff)-1, "Database error<BR>%s: %s", func, msg);
	global_warning(WarningBuff);
}
#endif


syntax highlighted by Code2HTML, v. 0.9.1