--- asterisk-addons-1.4.5/cdr_addon_mysql.c 2007-10-26 10:40:32.000000000 -0400 +++ asterisk-addons-1.4.5.new/cdr_addon_mysql.c 2007-12-27 12:16:06.000000000 -0500 @@ -51,8 +51,8 @@ static char *desc = "MySQL CDR Backend"; static char *name = "mysql"; static char *config = "cdr_mysql.conf"; -static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL; -static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0; +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL, *additionalfields = NULL; +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0, additionalfields_alloc = 0; static int dbport = 0; static int connected = 0; static time_t connect_time = 0; @@ -116,9 +116,13 @@ { struct tm tm; struct timeval tv; + struct ast_var_t *variables; + struct varshead *headpa; struct ast_module_user *u; char *userfielddata = NULL; - char sqlcmd[2048], timestr[128]; + char sqlcmd[2048], sqlcmd2[2048], timestr[128]; + char temp[1024]; + char *var, *val, *allowedfield; char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL, *src=NULL, *dst=NULL, *accountcode=NULL; int retries = 5; #ifdef MYSQL_LOGUNIQUEID @@ -131,6 +135,8 @@ ast_mutex_lock(&mysql_lock); memset(sqlcmd, 0, 2048); + memset(sqlcmd, 0, 2048); + memset(temp, 0, 1024); ast_localtime(&cdr->start.tv_sec, &tm, NULL); strftime(timestr, 128, DATE_FORMAT, &tm); @@ -224,20 +230,43 @@ if (option_debug) ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n"); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode", dbtable); + sprintf(sqlcmd2, ") VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s'", timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode); + if (additionalfields) { + headpa = &cdr->varshead; + + AST_LIST_TRAVERSE(headpa,variables,entries) { + if (variables && (var = ast_var_name(variables)) && (val = ast_var_value(variables)) && !ast_strlen_zero(var)) { + char fields[1024]; + strcpy(fields,additionalfields); + allowedfield=strtok(fields,"|"); + while (allowedfield) { + if (!strcmp(allowedfield,var)) { + sprintf(temp, ",`%s`", var); + strcat(sqlcmd,temp); + sprintf(temp, ",'%s'",val); + strcat(sqlcmd2,temp); + break; + } + allowedfield = strtok(NULL,"|"); + } + } + } + } if (userfield && userfielddata) { -#ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid, userfielddata); -#else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, userfielddata); -#endif - } else { -#ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid); -#else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode); -#endif + strcat(sqlcmd,",userfield"); + sprintf(temp,",'%s'",userfielddata); + strcat(sqlcmd2,temp); } + +#ifdef MYSQL_LOGUNIQUEID + strcat(sqlcmd,",uniqueid"); + sprintf(temp,",'%s'",uniqueid); + strcat(sqlcmd2,temp); +#endif + strcat(sqlcmd,sqlcmd2); + strcat(sqlcmd,")"); if (option_debug) ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd); @@ -434,6 +463,20 @@ userfield = 0; } } + tmp = ast_variable_retrieve(cfg, "global", "additionalfields"); + if (tmp) { + additionalfields = malloc(strlen(tmp) + 1); + if (additionalfields != NULL) { + additionalfields_alloc = 1; + strcpy(additionalfields, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "Additional fields not specified. Assuming none\n"); + additionalfields = NULL; + } ast_config_destroy(cfg);