1 #include "config.h"
2 #ifdef ENABLE_MYSQL
3 
4 #include <my_global.h>
5 #include <my_sys.h>
6 #include <mysql.h>
7 #include <string.h>
8 #include "global.h"
9 #include "alphabetize.h"
10 
11 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
12 	Edit below to set host, username, password, and database name for mysql option
13 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
14 
15 static const char *host="localhost";
16 static const char *user="my_user";
17 static const char *passwd="my_password";  /* NULL for no password */
18 static const char *db="my_database";
19 
20 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
21 		D o   N o T   E d i T  B e L o W (unless you're hackin' source!)
22 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
23 
24 static MYSQL *conn=NULL;
25 static const char *sock=NULL;
26 static const unsigned int port=0;
27 static const unsigned int flags=0;
28 
29 extern unsigned int acl_features;
30 extern char WarningBuff[MAX_WARNING_BUFF];
31 
32 /* forward defs */
33 static void submit_warning(char *, char *);
34 
35 /*
36  * Shows the add domain page, retrieving possible owners from
37  * the MySQL db and adding them as selections to a combobox
38  */
display_add_domain(void)39 void display_add_domain(void)
40 {
41 	MYSQL_ROW my_row;
42 	MYSQL_RES *my_result=NULL;
43 	char *func="display_add_domain";
44 	struct node_t *list=NULL, *lptr=NULL;
45 
46 	if(!(acl_features & ACL_DOMAIN_CREATE)) {
47     	global_warning("Display domain: Permission denied");
48    	 	t_open(T_MAIN, 1);
49   	}
50 
51 	memset(&my_row, 0, sizeof(my_row));
52 
53 	/* initialize connection handler */
54 	conn=mysql_init(NULL);
55 			                                                                                                                                  if(!conn)
56 		submit_warning(func, mysql_error(conn));
57 	else if(!mysql_real_connect(conn, host, user, passwd, db, port, sock, flags))
58 		submit_warning(func, mysql_error(conn));
59 	else if(mysql_query(conn, "SELECT owner FROM domain_owner GROUP BY owner") != 0)
60 		submit_warning(func, mysql_error(conn));
61 	else {
62 		my_result=mysql_use_result(conn);
63 
64 		if(!my_result)
65 			submit_warning(func, mysql_error(conn));
66 	}
67 
68 	t_open(T_ADD_HEAD, 0);
69 
70 	if(!my_result)
71 		t_open(T_ADD_BODY, 0);
72 	else {
73 		/* build list of query results */
74 		while((my_row=mysql_fetch_row(my_result)))
75 			list=append_string(list, my_row[0]);
76 
77 		/* alphabetize list and display in list box */
78 		for(lptr=alpha_mergesort(get_head(list)); lptr; lptr=lptr->next) {
79 			global_par("OW", lptr->string);
80 			t_open(T_ADD_BODY, 0);
81 		}
82 
83 		free_list(list);
84 		mysql_free_result(my_result);
85 	}
86 
87 	if(conn)
88 		mysql_close(conn);
89 
90 	t_open(T_ADD_FOOT, 1);
91 }
92 
93 
94 /*
95  * Inserts domain owner info into MySQL db
96  * PARAM domain
97  * 	The domain whose owner info we must insert
98  * RETURN
99  * 	0 upon success
100  * 	1 if domain is NULL
101  * 	2 if the values of the domain owner text field or combobox could not be retrieved
102  * 	3 if the user did not enter correct input
103  */
insert_owner(char * domain)104 int insert_owner(char *domain)
105 {
106 	char my_query[500]={ 0 };
107 	char *name=NULL, *selection=NULL, *func="insert_owner", *ptr=NULL;
108 
109 	if(!(acl_features & ACL_DOMAIN_CREATE)) {
110     	global_warning("Insert owner: Permission denied");
111     	t_open(T_MAIN, 1);
112     }
113 
114 	if(!domain)
115 		return 1;
116 
117 	name=cgi_is_var("owner_text");
118 	selection=cgi_is_var("owner_select");
119 
120 	if(!name || !selection)
121 		return 2;
122 
123 	if((strcmp(name, "") == 0) && (strcmp(selection, "manual_select") != 0))
124 		name=selection;
125 	else if((strcmp(name, "") != 0) && (strcmp(selection, "manual_select") == 0))
126     	;
127 	else
128 		return 3;
129 
130 	/* strip out any '+' chars the html may have submitted as spaces */
131 	while((ptr=strchr(name, '+')))
132 		*ptr=' ';
133 
134 	snprintf(my_query, sizeof(my_query)-1, "INSERT INTO domain_owner VALUES('%s', '%s')", domain, name);
135 
136 	/* connect to database */
137     conn=mysql_init(NULL);
138 
139     if(!conn)
140 		submit_warning(func, mysql_error(conn));
141     else if(!mysql_real_connect(conn, host, user, passwd, db, port, sock, flags))
142 		submit_warning(func, mysql_error(conn));
143     else if(mysql_query(conn, my_query) != 0)
144 		submit_warning(func, mysql_error(conn));
145 
146 	if(conn)
147 		mysql_close(conn);
148 
149 	return 0;
150 }
151 
152 
submit_warning(char * func,char * msg)153 static void submit_warning(char *func, char *msg)
154 {
155 	if(!func)
156 		func="function";
157 
158 	if(!msg)
159 		msg="problem encountered";
160 
161 	snprintf(WarningBuff, sizeof(WarningBuff)-1, "Database error<BR>%s: %s", func, msg);
162 	global_warning(WarningBuff);
163 }
164 #endif
165