static char * alloc_cookie_id(request_rec *r) { char *cookie_id; char *uuid_string; unsigned char *md5_value; apr_uuid_t uuid; apr_status_t retval; apr_uuid_get(&uuid); uuid_string = apr_palloc(r->pool, APR_UUID_FORMATTED_LENGTH + 1); memset(uuid_string, 0, APR_UUID_FORMATTED_LENGTH + 1); apr_uuid_format(uuid_string, &uuid);; md5_value = (unsigned char*)apr_palloc(r->pool, APR_MD5_DIGESTSIZE + 1); memset(md5_value, 0, APR_MD5_DIGESTSIZE + 1); retval = apr_md5(md5_value, (const char*)uuid_string, APR_UUID_FORMATTED_LENGTH); if (retval != APR_SUCCESS) { ERR(r, "md5 failed."); return NULL; } cookie_id = apr_palloc(r->pool, apr_base64_encode_len(APR_MD5_DIGESTSIZE)+1); memset(cookie_id, 0, APR_MD5_DIGESTSIZE+1); apr_base64_encode(cookie_id, (char*)md5_value, APR_MD5_DIGESTSIZE); DBG(r, "cookie_id=[%s]", cookie_id); cookie_id = chxj_url_encode(r->pool,cookie_id); DBG(r, "cookie_id=[%s]", cookie_id); return cookie_id; }
static void s_track_page_view(request_rec *r) { apr_time_t timestamp = apr_time_now(); char *domain_name = NULL; char *document_referer = NULL; char *document_path = NULL; char *args; char *pstate; char *vstate; char *dates; char *account; char *user_agent; char *pair; char *name; char *value; char *guid_header = NULL; char *visitor_id = NULL; int utmdebug = 0; int ii; apr_array_header_t *headers = NULL; apr_table_entry_t *hentryp = NULL; char *cookie_str = NULL; char *utmUrl = NULL; DBG(r, "REQ[%X] start %s()", TO_ADDR(r),__func__); domain_name = (char *)apr_table_get(r->headers_in, "Host"); if (domain_name == NULL || *domain_name == '\0') { domain_name = ""; } args = r->args ? apr_pstrdup(r->pool, r->args) : ""; for (;;) { char *pair_sv; pair = apr_strtok(args, "&", &pstate); if (pair == NULL) break; args = NULL; pair_sv = apr_pstrdup(r->pool, pair); name = apr_strtok(pair, "=", &vstate); value = apr_strtok(NULL, "=", &vstate); if (! name) continue; if (STRCASEEQ('u','U',"utmr",name)) { if (value && *value && strcmp(value, "0") != 0) { document_referer = chxj_url_decode(r->pool, value); } else { document_referer = "-"; } } else if (STRCASEEQ('u','U',"utmp",name)) { if (value && *value) { document_path = chxj_url_decode(r->pool, value); } else { document_path = ""; } } else if (STRCASEEQ('u','U',"utmac",name)) { if (value && *value) { account = value; } else { ERR(r, "why not set account? - [%s]", r->args); } } else if (STRCASEEQ('u','U',"utmdebug",name)) { utmdebug = 1; } } user_agent = (char *)apr_table_get(r->headers_in,"User-Agent"); if (!user_agent || *user_agent == '\0') { user_agent = ""; } headers = (apr_array_header_t*)apr_table_elts(r->headers_in); hentryp = (apr_table_entry_t*)headers->elts; cookie_str = NULL; for (ii=0; ii<headers->nelts; ii++) { if (strcasecmp(hentryp[ii].key, "Cookie") == 0) { DBG(r,"REQ[%X] FOUND Cookie:[%s]",TO_ADDR(r),hentryp[ii].val); char *val = hentryp[ii].val; for (;;) { char *pair_sv = NULL; pair = apr_strtok(val, ";", &pstate); if (pair == NULL) break; val = NULL; pair_sv = apr_pstrdup(r->pool, pair); name = apr_strtok(pair, "=", &vstate); value = apr_strtok(NULL, "=", &vstate); if (! name) continue; if (strcasecmp(DL_COOKIE_NAME, name) == 0) { cookie_str = apr_pstrdup(r->pool, value); break; } } } } guid_header = (char *)apr_table_get(r->headers_in, "X-DCMGUID"); if (!guid_header || *guid_header == 0) { guid_header = (char *)apr_table_get(r->headers_in, "X-UP-SUBNO"); } if (!guid_header || *guid_header == 0) { guid_header = (char *)apr_table_get(r->headers_in, "X-JPHONE-UID"); } if (!guid_header || *guid_header == 0) { guid_header = (char *)apr_table_get(r->headers_in, "X-EM-UID"); } visitor_id = s_get_visitor_id(r, guid_header, account, user_agent, cookie_str); dates = apr_pcalloc(r->pool, MAX_STRING_LEN); apr_rfc822_date(dates, timestamp + DL_COOKIE_USER_PERSISTENCE); apr_table_setn(r->headers_out, "Set-Cookie",apr_psprintf(r->pool, "%s=%s; expires=%s; path=%s", DL_COOKIE_NAME,visitor_id, dates, DL_COOKIE_PATH)); utmUrl = apr_pstrdup(r->pool, DL_UTM_GIF_LOCATION); utmUrl = apr_pstrcat(r->pool, utmUrl, "?", NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "utmwv=", DL_VERSION, NULL); srand(time(0)); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmn=", apr_psprintf(r->pool, "%d", rand()), NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmhn=", chxj_url_encode(r->pool, domain_name), NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmr=", chxj_url_encode(r->pool, document_referer), NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmp=", chxj_url_encode(r->pool, document_path), NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmac=", account, NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmcc=__utma%3D999.999.999.999.999.1%3B", NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmvid=", visitor_id, NULL); utmUrl = apr_pstrcat(r->pool, utmUrl, "&utmip=", s_get_ip(r,r->connection->remote_ip), NULL); DBG(r, "REQ[%X] utmUrl:[%s]", TO_ADDR(r), utmUrl); s_send_request_to_google_analytics(r, utmUrl); if (utmdebug) { apr_table_setn(r->headers_out, "X-GA-MOBILE-URL", utmUrl); } s_write_gif_data(r); DBG(r, "REQ[%X] end %s()", TO_ADDR(r),__func__); }
char * chxj_google_analytics_get_image_url(request_rec *r, const char *pagetitle) { char *url; char *referer; char *query; char *path; char *ptitle; mod_chxj_config *conf; int rdm; apr_pool_t *pool; apr_size_t plen; DBG(r, "REQ[%X] start %s()", TO_ADDR(r),__func__); conf = chxj_get_module_config(r->per_dir_config, &chxj_module); if (conf->use_google_analytics != 1 || conf->google_analytics_account == NULL || conf->google_analytics_target == NULL) { DBG(r, "REQ[%X] end %s()", TO_ADDR(r),__func__); return ""; } srand(time(0)); rdm = rand(); apr_pool_create(&pool, r->pool); plen = 0; ptitle = ""; if (pagetitle) { plen = strlen(pagetitle); ptitle = chxj_iconv(r,pool,apr_pstrdup(pool, pagetitle),&plen,"CP932","UTF-8"); } else { ptitle = "(no name)"; } url = apr_pstrdup(pool, conf->google_analytics_target); url = apr_pstrcat(pool, url, "?", NULL); url = apr_pstrcat(pool, url, "utmac=", conf->google_analytics_account, NULL); url = apr_pstrcat(pool, url, "&utmn=", apr_psprintf(pool, "%d",rdm), NULL); url = apr_pstrcat(pool, url, "&utmdt=", chxj_url_encode(pool, ptitle), NULL); referer = (char *)apr_table_get(r->headers_in, "Referer"); DBG(r, "REQ[%X] referer:[%s]", TO_ADDR(r), referer); query = r->args ? apr_pstrdup(pool, r->args) : ""; DBG(r, "REQ[%X] query:[%s]", TO_ADDR(r), query); path = r->unparsed_uri ? apr_pstrdup(pool, r->unparsed_uri) : ""; DBG(r, "REQ[%X] path:[%s]", TO_ADDR(r), path); if (! referer || *referer == '\0') { referer = "-"; } url = apr_pstrcat(pool, url, "&utmr=", chxj_url_encode(pool,referer), NULL); DBG(r, "REQ[%X] url:[%s]", TO_ADDR(r), url); if (path && *path != '\0') { if (query && *query != '\0') { path = apr_pstrcat(pool, path, "?", query, NULL); } url = apr_pstrcat(pool, url, "&utmp=", chxj_url_encode(pool,path), NULL); } DBG(r, "REQ[%X] url:[%s]", TO_ADDR(r), url); url = apr_pstrcat(pool, url, "&guid=ON", NULL); DBG(r, "REQ[%X] url:[%s]", TO_ADDR(r), url); int len = strlen(url); int ii, jj; char *ret = apr_palloc(pool, len * 4 + 1); memset(ret, 0, len * 4 + 1); for (ii=0,jj=0; ii<len; ii++) { if (url[ii] == '&') { ret[jj++] = '&'; ret[jj++] = 'a'; ret[jj++] = 'm'; ret[jj++] = 'p'; ret[jj++] = ';'; } else { ret[jj++] = url[ii]; } } DBG(r, "REQ[%X] end %s()", TO_ADDR(r),__func__); return ret; }