예제 #1
0
파일: driver_mysql.c 프로젝트: oikomi/bakup
/* Insert a request in the backend */
static void apm_driver_mysql_insert_request(TSRMLS_D)
{
	APM_DEBUG("[MySQL driver] ======apm_driver_mysql_insert_request \n");
	char *application_esc = NULL, *script_esc = NULL, *uri_esc = NULL, *host_esc = NULL, *cookies_esc = NULL, *post_vars_esc = NULL, *referer_esc = NULL, *method_esc = NULL, *status_esc = NULL, *sql = NULL;
	unsigned int application_len = 0, script_len = 0, uri_len = 0, host_len = 0, ip_int = 0, cookies_len = 0, post_vars_len = 0, referer_len = 0, method_len = 0, status_len = 0;
	struct in_addr ip_addr;
	MYSQL *connection;

	extract_data();

	APM_DEBUG("[MySQL driver] Begin insert request\n");
	if (APM_G(mysql_is_request_created)) {
		APM_DEBUG("[MySQL driver] SKIPPED, request already created.\n");
		return;
	}

	MYSQL_INSTANCE_INIT

	if (APM_G(application_id)) {
		application_len = strlen(APM_G(application_id));
		application_esc = emalloc(application_len * 2 + 1);
		application_len = mysql_real_escape_string(connection, application_esc, APM_G(application_id), application_len);
	}
	
	APM_MYSQL_ESCAPE_STR(script);
	APM_MYSQL_ESCAPE_STR(uri);
	APM_MYSQL_ESCAPE_STR(host);
	APM_MYSQL_ESCAPE_STR(referer);
	APM_MYSQL_ESCAPE_STR(method);
	APM_MYSQL_ESCAPE_STR(status);
	APM_MYSQL_ESCAPE_SMART_STR(cookies);
	APM_MYSQL_ESCAPE_SMART_STR(post_vars);

	if (APM_RD(ip_found) && (inet_pton(AF_INET, APM_RD_STRVAL(ip), &ip_addr) == 1)) {
		ip_int = ntohl(ip_addr.s_addr);
	}

	sql = emalloc(166 + application_len + script_len + uri_len + host_len + cookies_len + post_vars_len + referer_len + method_len);
	sprintf(
		sql,
		"INSERT INTO request (application, script, uri, host, ip, cookies, post_vars, referer, method, status) VALUES ('%s', '%s', '%s', '%s', %u, '%s', '%s', '%s', '%s', '%s')",
		application_esc ? application_esc : "",
		APM_RD(script_found) ? script_esc : "",
		APM_RD(uri_found) ? uri_esc : "",
		APM_RD(host_found) ? host_esc : "",
		ip_int, APM_RD(cookies_found) ? cookies_esc : "",
		APM_RD(post_vars_found) ? post_vars_esc : "",
		APM_RD(referer_found) ? referer_esc : "",
		APM_RD(method_found) ? method_esc : "",
		APM_RD(status_found) ? status_esc : "");

	APM_DEBUG("[MySQL driver] Sending: %s\n", sql);
	if (mysql_query(connection, sql) != 0)
		APM_DEBUG("[MySQL driver] Error: %s\n", mysql_error(APM_G(mysql_event_db)));

	mysql_query(connection, "SET @request_id = LAST_INSERT_ID()");

	efree(sql);
	if (application_esc)
		efree(application_esc);
	if (script_esc)
		efree(script_esc);
	if (uri_esc)
		efree(uri_esc);
	if (host_esc)
		efree(host_esc);
	if (cookies_esc)
		efree(cookies_esc);
	if (post_vars_esc)
		efree(post_vars_esc);
	if (referer_esc)
		efree(referer_esc);
	if (method_esc)
		efree(method_esc);
	if (status_esc)
		efree(status_esc);

	APM_G(mysql_is_request_created) = 1;
	APM_DEBUG("[MySQL driver] End insert request\n");
}
예제 #2
0
/* Insert a request in the backend */
void apm_driver_mysql_insert_request(TSRMLS_D)
{
	char *script = NULL, *application_esc, *script_esc = NULL, *uri_esc = NULL, *host_esc = NULL, *cookies_esc = NULL, *post_vars_esc = NULL, *referer_esc = NULL, *sql = NULL;
	unsigned int application_len = 0, script_len = 0, uri_len = 0, host_len = 0, ip_int = 0, cookies_len = 0, post_vars_len = 0, referer_len = 0;
	struct in_addr ip_addr;
	MYSQL *connection;
	zval *tmp;

	EXTRACT_DATA();

	APM_DEBUG("[MySQL driver] Begin insert request\n");
	if (APM_MY_G(is_request_created)) {
		APM_DEBUG("[MySQL driver] SKIPPED, request already created.\n");
		return;
	}

	MYSQL_INSTANCE_INIT

	if (APM_G(application_id)) {
		application_len = strlen(APM_G(application_id));
		application_esc = emalloc(application_len * 2 + 1);
		application_len = mysql_real_escape_string(connection, application_esc, APM_G(application_id), application_len);
	}

	get_script(&script);

	if (script) {
		script_len = strlen(script);
		script_esc = emalloc(script_len * 2 + 1);
		script_len = mysql_real_escape_string(connection, script_esc, script, script_len);
	}

	if (APM_RD(uri_found)) {
		uri_len = strlen(Z_STRVAL_PP(APM_RD(uri)));
		uri_esc = emalloc(uri_len * 2 + 1);
		uri_len = mysql_real_escape_string(connection, uri_esc, Z_STRVAL_PP(APM_RD(uri)), uri_len);
	}

	if (APM_RD(host_found)) {
		host_len = strlen(Z_STRVAL_PP(APM_RD(host)));
		host_esc = emalloc(host_len * 2 + 1);
		host_len = mysql_real_escape_string(connection, host_esc, Z_STRVAL_PP(APM_RD(host)), host_len);
	}

	if (APM_RD(ip_found) && (inet_pton(AF_INET, Z_STRVAL_PP(APM_RD(ip)), &ip_addr) == 1)) {
		ip_int = ntohl(ip_addr.s_addr);
	}
	
	if (APM_RD(cookies_found)) {
		cookies_len = strlen(APM_RD(cookies).c);
		cookies_esc = emalloc(cookies_len * 2 + 1);
		cookies_len = mysql_real_escape_string(connection, cookies_esc, APM_RD(cookies).c, cookies_len);
	}

	if (APM_RD(post_vars_found)) {
		post_vars_len = strlen(APM_RD(post_vars).c);
		post_vars_esc = emalloc(post_vars_len * 2 + 1);
		post_vars_len = mysql_real_escape_string(connection, post_vars_esc, APM_RD(post_vars).c, post_vars_len);
	}

	if (APM_RD(referer_found)) {
		referer_len = strlen(Z_STRVAL_PP(APM_RD(referer)));
		referer_esc = emalloc(referer_len * 2 + 1);
		referer_len = mysql_real_escape_string(connection, referer_esc, Z_STRVAL_PP(APM_RD(referer)), referer_len);
	}

	sql = emalloc(154 + application_len + script_len + uri_len + host_len + cookies_len + post_vars_len + referer_len);
	sprintf(
		sql,
		"INSERT INTO request (application, script, uri, host, ip, cookies, post_vars, referer) VALUES ('%s', '%s', '%s', '%s', %u, '%s', '%s', '%s')",
		APM_G(application_id) ? application_esc : "", script ? script_esc : "", APM_RD(uri_found) ? uri_esc : "", APM_RD(host_found) ? host_esc : "", ip_int, APM_RD(cookies_found) ? cookies_esc : "", APM_RD(post_vars_found) ? post_vars_esc : "", APM_RD(referer_found) ? referer_esc : "");

	APM_DEBUG("[MySQL driver] Sending: %s\n", sql);
	if (mysql_query(connection, sql) != 0)
		APM_DEBUG("[MySQL driver] Error: %s\n", mysql_error(APM_MY_G(event_db)));

	mysql_query(connection, "SET @request_id = LAST_INSERT_ID()");

	efree(sql);
	efree(script_esc);
	efree(uri_esc);
	efree(host_esc);
	efree(cookies_esc);
	efree(post_vars_esc);
	efree(referer_esc);

	APM_MY_G(is_request_created) = 1;
	APM_DEBUG("[MySQL driver] End insert request\n");
}