using System; using System.Web; using System.Runtime.Serialization.Json; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Web.Services.Protocols; using System.Security.Cryptography; using System.Text; using System.Web.SessionState; using System.Data; using System.Collections.Generic; using System.IO.Compression; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using Facebook; using System.Net; using System.Web.UI.HtmlControls; using System.Activities.Expressions; using System.Web.UI.WebControls; public partial class test_redir : System.Web.UI.Page { public string url_fbPixelCode = ""; public string url_googleAwConversionId = ""; public string public_url = ""; protected void Page_Load(object sender, EventArgs e) { string code = (Request["code"] == null) ? "" : Request["code"].ToString(); if (code == "") { Server.Transfer("auth-error-v3.html"); } autoBindDataTable objUrl = new autoBindDataTable(string.Format("select * from url where url_Code Collate SQL_Latin1_General_CP1_CS_AS = '{0}' and url_isRevoke = 'N' ", code)); if (objUrl.dataRows.Count == 0) { Server.Transfer("auth-error-v3.html"); } DataRow urlRow = objUrl.dataRows[0]; string url_uid = urlRow["url_uid"].ToString(); string target_url = urlRow["url_directToUrl"].ToString(); public_url = target_url; autoBindDataTable objProj = new autoBindDataTable(string.Format("select * from projects where project_uid = '{0}' and project_isRevoke = 'N'", urlRow["project_uid"].ToString())); if (objProj.dataRows.Count == 0) { Server.Transfer("auth-error-v3.html"); } DataRow projRow = objProj.dataRows[0]; if (projRow["project_dateLimit"].ToString() == "Y") { if (DateTime.Now > DateTime.Parse(projRow["project_endDate"].ToString())) { Response.StatusCode = 302; Response.AddHeader("Location", projRow["project_endToRedirect"].ToString()); //Response.Redirect(projRow["project_endToRedirect"].ToString()); return; } } string recTableName = projRow["project_recTableName"].ToString(); string project_uid = projRow["project_uid"].ToString(); string hostName = ""; string thisIP = globalClass.GetIPAddress(); //thisIP = "60.251.161.103"; string queryIPUrl = string.Format("https://ipinfo.io/{0}/json", thisIP); string rec_country = ""; string rec_city = ""; string rec_loc = ""; try { IPHostEntry hostEntry = Dns.GetHostEntry(globalClass.GetIPAddress()); hostName = hostEntry.HostName; } catch { } try { WebClient wc = new WebClient(); string result = wc.DownloadString(queryIPUrl);//取得IP位置 dynamic resultJson = JValue.Parse(result); rec_country = (resultJson["country"] == null) ? "" : resultJson["country"]; rec_city = (resultJson["city"] == null) ? "" : resultJson["city"]; rec_loc = (resultJson["loc"] == null) ? "" : resultJson["loc"]; string recTableString = string.Format("select * from {0} where rec_sn = -1", recTableName); autoBindDataTable recTable = new autoBindDataTable(recTableString); DataRow recRow = recTable.newRow; recTable.dataRows.Add(recRow); recRow["project_uid"] = project_uid; recRow["url_uid"] = url_uid; recRow["rec_sourceIP"] = thisIP; recRow["rec_sourceDomain"] = hostName; recRow["rec_country"] = rec_country; recRow["rec_city"] = rec_city; recRow["rec_loc"] = rec_loc; recTable.updateDataTable(); recTable.disConnection(); string todayDate = DateTime.Now.ToString("yyyy/MM/dd 23:59:59"); string recTotalString = string.Format("select * from recSubTotal where project_uid = '{0}' and url_uid = '{1}' and recSubTotal_date = '{2}'", project_uid, url_uid, todayDate); autoBindDataTable recTotalTable = new autoBindDataTable(recTotalString); DataRow recTotalRow; if (recTotalTable.dataRows.Count > 0) { recTotalRow = recTotalTable.dataRows[0]; } else { recTotalRow = recTotalTable.newRow; recTotalTable.dataRows.Add(recTotalRow); recTotalRow["project_uid"] = project_uid; recTotalRow["url_uid"] = url_uid; recTotalRow["recSubTotal_date"] = todayDate; recTotalRow["recSubTotal_count"] = 0; } recTotalRow["recSubTotal_count"] = int.Parse(recTotalRow["recSubTotal_count"].ToString()) + 1; recTotalTable.updateDataTable(); recTotalTable.disConnection(); } catch (Exception ex) { string msg = ex.Message; string recTableString = string.Format("select * from {0} where rec_sn = -1", recTableName); autoBindDataTable recTable = new autoBindDataTable(recTableString); DataRow recRow = recTable.newRow; recTable.dataRows.Add(recRow); recRow["project_uid"] = project_uid; recRow["url_uid"] = url_uid; recRow["rec_sourceIP"] = thisIP; recRow["rec_sourceDomain"] = hostName; recRow["rec_country"] = ""; recRow["rec_city"] = ""; recRow["rec_loc"] = ""; recTable.updateDataTable(); recTable.disConnection(); string todayDate = DateTime.Now.ToString("yyyy/MM/dd 23:59:59"); string recTotalString = string.Format("select * from recSubTotal where project_uid = '{0}' and url_uid = '{1}' and recSubTotal_date = '{2}'", project_uid, url_uid, todayDate); autoBindDataTable recTotalTable = new autoBindDataTable(recTotalString); DataRow recTotalRow; if (recTotalTable.dataRows.Count > 0) { recTotalRow = recTotalTable.dataRows[0]; } else { recTotalRow = recTotalTable.newRow; recTotalTable.dataRows.Add(recTotalRow); recTotalRow["project_uid"] = project_uid; recTotalRow["url_uid"] = url_uid; recTotalRow["recSubTotal_date"] = todayDate; recTotalRow["recSubTotal_count"] = 0; } recTotalRow["recSubTotal_count"] = int.Parse(recTotalRow["recSubTotal_count"].ToString()) + 1; recTotalTable.updateDataTable(); recTotalTable.disConnection(); } string agent = Request.UserAgent; urlObj oUrl = new urlObj(url_uid); if (agent.ToLower().IndexOf("facebook") > -1) { if (oUrl.url_fbTag == "Y") { HtmlMeta tagId = new HtmlMeta(); tagId.Attributes.Add("property", "fb:app_id"); tagId.Content = "563584377383502"; Page.Header.Controls.Add(tagId); HtmlMeta tagTitle = new HtmlMeta(); tagTitle.Attributes.Add("property", "og:title"); tagTitle.Content = oUrl.url_fbTitle; Page.Header.Controls.Add(tagTitle); HtmlMeta tagDescript = new HtmlMeta(); tagDescript.Attributes.Add("property", "og:description"); tagDescript.Content = oUrl.url_fbDescript; Page.Header.Controls.Add(tagDescript); HtmlMeta tagType = new HtmlMeta(); tagType.Attributes.Add("property", "og:type"); tagType.Content = "website"; Page.Header.Controls.Add(tagType); HtmlMeta tagUrl = new HtmlMeta(); tagUrl.Attributes.Add("property", "og:url"); tagUrl.Content = oUrl.url_shortUrl; Page.Header.Controls.Add(tagUrl); HtmlMeta tagImage = new HtmlMeta(); tagImage.Attributes.Add("property", "og:image"); tagImage.Content = oUrl.Url_fbCacheUrl; Page.Header.Controls.Add(tagImage); if (oUrl.Url_fbCacheUrl != "") { HtmlMeta tagWidth = new HtmlMeta(); tagWidth.Attributes.Add("property", "og:image:width"); tagWidth.Content = oUrl.pic_image.Width.ToString(); Page.Header.Controls.Add(tagWidth); HtmlMeta tagHeight = new HtmlMeta(); tagHeight.Attributes.Add("property", "og:image:height"); tagHeight.Content = oUrl.pic_image.Height.ToString(); Page.Header.Controls.Add(tagHeight); } HtmlMeta tagImageAlt = new HtmlMeta(); tagImageAlt.Attributes.Add("property", "og:image:alt"); tagImageAlt.Content = oUrl.url_fbTitle; Page.Header.Controls.Add(tagImageAlt); return; } if (oUrl.url_gifTag == "Y") { Response.StatusCode = 302; Response.AddHeader("Location", oUrl.url_gifLink); return; } } if (agent.ToLower().IndexOf("twitter") > -1) { if (oUrl.url_fbTag == "Y") { HtmlMeta tagTitle = new HtmlMeta(); //tagTitle.Attributes.Add("property", "og:title"); tagTitle.Name = "twitter:title"; tagTitle.Content = oUrl.url_fbTitle; Page.Header.Controls.Add(tagTitle); HtmlMeta tagDescript = new HtmlMeta(); //tagDescript.Attributes.Add("property", "og:description"); tagDescript.Name = "twitter:description"; tagDescript.Content = oUrl.url_fbDescript; Page.Header.Controls.Add(tagDescript); HtmlMeta tagType = new HtmlMeta(); tagType.Attributes.Add("property", "og:type"); tagType.Content = "website"; Page.Header.Controls.Add(tagType); HtmlMeta tagUrl = new HtmlMeta(); tagUrl.Attributes.Add("property", "og:url"); tagUrl.Name = "twitter:site"; tagUrl.Content = oUrl.url_shortUrl; Page.Header.Controls.Add(tagUrl); HtmlMeta tagImage = new HtmlMeta(); //tagImage.Attributes.Add("property", "og:image"); tagImage.Name = "twitter:image:src"; tagImage.Content = oUrl.Url_fbCacheUrl; Page.Header.Controls.Add(tagImage); HtmlMeta tagImageAlt = new HtmlMeta(); //tagImageAlt.Attributes.Add("property", "og:image:alt"); tagImageAlt.Name = "twitter:card"; tagImageAlt.Content = "summary_large_image"; Page.Header.Controls.Add(tagImageAlt); return; } } if (oUrl.url_fbPixel == "Y" || oUrl.url_googleAds == "Y") { if (oUrl.url_fbPixel == "Y") { globalClass.AddScriptToHead(Page.Header, "\r\n", false); globalClass.AddScriptToHead(Page.Header, "\r\n", false); globalClass.AddScriptToHead(Page.Header, "\r\n", false); string fbPixelCode = ""; fbPixelCode += "\r\n"; fbPixelCode += "\r\n"; fbPixelCode += "\r\n"; fbPixelCode += "\r\n\r\n"; globalClass.AddScriptToHead(Page.Header, fbPixelCode, false); } if (oUrl.url_googleAds == "Y") { globalClass.AddScriptToHead(Page.Header, "\r\n\r\n", false); string googleAdCode = ""; googleAdCode += "\r\n"; globalClass.AddScriptToHead(Page.Header, googleAdCode, false); } HtmlMeta tagRedirect = new HtmlMeta(); tagRedirect.HttpEquiv = "refresh"; tagRedirect.Content = "5;url=" + target_url; Page.Header.Controls.Add(tagRedirect); url_fbPixelCode = oUrl.url_fbPixelCode; url_googleAwConversionId = oUrl.url_googleAwConversionId; return; } Response.StatusCode = 302; Response.AddHeader("Location", target_url); return; //Response.Redirect(target_url); } }