private string GetLinkClickURL(ref DialogParams dialogParams, ref string link)
		{
			link = GetLinkUrl(ref dialogParams, link);

			return GetDomain() + Globals.LinkClick(link, dialogParams.TabId, dialogParams.ModuleId, true, false, dialogParams.PortalId, dialogParams.EnableUrlLanguage, dialogParams.PortalGuid);

		}
		private string GetLinkUrl(ref DialogParams dialogParams, string link)
		{
			ArrayList aliasList = _portalAliasController.GetPortalAliasArrayByPortalID(dialogParams.PortalId);

			if (dialogParams.LinkUrl.Contains(dialogParams.HomeDirectory))
			{
				string filePath = dialogParams.LinkUrl.Substring(dialogParams.LinkUrl.IndexOf(dialogParams.HomeDirectory)).Replace(dialogParams.HomeDirectory, "");
				var linkedFileId = FileManager.Instance.GetFile(dialogParams.PortalId, HttpUtility.UrlDecode(filePath)).FileId;
				link = string.Format("fileID={0}", linkedFileId);
			}
			else
			{
				foreach (PortalAliasInfo portalAlias in aliasList)
				{
					dialogParams.LinkUrl = dialogParams.LinkUrl.Replace(Globals.AddHTTP(portalAlias.HTTPAlias), "");
				}

				string tabPath = dialogParams.LinkUrl.Replace("http://", "").Replace("/", "//").Replace(".aspx", "");
				string cultureCode = Localization.SystemLocale;
                //get language info from url.
                var dicLocales = LocaleController.Instance.GetLocales(dialogParams.PortalId);
                foreach (var part in tabPath.Split(new char[]{'/'}, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (dicLocales != null && part.IndexOf("-") > -1)
                    {
                        foreach (KeyValuePair<string, Locale> key in dicLocales)
                        {
                            if (key.Key.ToLower().Equals(part.ToLower()))
                            {
                                cultureCode = key.Value.Code;
                                tabPath = tabPath.Replace("//" + part, string.Empty);
                                break;
                            }
                        }
                    }
                }

				//Try HumanFriendlyUrl TabPath
				link = TabController.GetTabByTabPath(dialogParams.PortalId, tabPath, cultureCode).ToString();

				if (link == Null.NullInteger.ToString())
				{
					//Try getting the tabId from the querystring
					string[] arrParams = dialogParams.LinkUrl.Split('/');
					for (int i = 0; i < arrParams.Length; i++)
					{
						if (arrParams[i].ToLowerInvariant() == "tabid")
						{
							link = arrParams[i + 1];
							break;
						}
					}
                    if (link == Null.NullInteger.ToString())
					{
						link = dialogParams.LinkUrl;
					}
				}

			}

			return link;

		}
        private string GetLinkClickURL(ref DialogParams dialogParams, ref string link)
        {
            link = GetLinkUrl(ref dialogParams, link);

            return(GetDomain() + Globals.LinkClick(link, dialogParams.TabId, dialogParams.ModuleId, true, false, dialogParams.PortalId, dialogParams.EnableUrlLanguage, dialogParams.PortalGuid));
        }
        private string GetLinkUrl(ref DialogParams dialogParams, string link)
        {
            var aliasList = _portalAliasController.GetPortalAliasesByPortalId(dialogParams.PortalId);

            if (dialogParams.LinkUrl.Contains(dialogParams.HomeDirectory))
            {
                string filePath     = dialogParams.LinkUrl.Substring(dialogParams.LinkUrl.IndexOf(dialogParams.HomeDirectory)).Replace(dialogParams.HomeDirectory, "");
                var    linkedFileId = FileManager.Instance.GetFile(dialogParams.PortalId, HttpUtility.UrlDecode(filePath)).FileId;
                link = string.Format("fileID={0}", linkedFileId);
            }
            else
            {
                foreach (PortalAliasInfo portalAlias in aliasList)
                {
                    dialogParams.LinkUrl = dialogParams.LinkUrl.Replace(Globals.AddHTTP(portalAlias.HTTPAlias), "");
                }

                string tabPath     = dialogParams.LinkUrl.Replace("http://", "").Replace("/", "//").Replace(".aspx", "");
                string cultureCode = Localization.SystemLocale;
                //get language info from url.
                var dicLocales = LocaleController.Instance.GetLocales(dialogParams.PortalId);
                foreach (var part in tabPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (dicLocales != null && part.IndexOf("-") > -1)
                    {
                        foreach (KeyValuePair <string, Locale> key in dicLocales)
                        {
                            if (key.Key.ToLower().Equals(part.ToLower()))
                            {
                                cultureCode = key.Value.Code;
                                tabPath     = tabPath.Replace("//" + part, string.Empty);
                                break;
                            }
                        }
                    }
                }

                //Try HumanFriendlyUrl TabPath
                link = TabController.GetTabByTabPath(dialogParams.PortalId, tabPath, cultureCode).ToString();

                if (link == Null.NullInteger.ToString())
                {
                    //Try getting the tabId from the querystring
                    string[] arrParams = dialogParams.LinkUrl.Split('/');
                    for (int i = 0; i < arrParams.Length; i++)
                    {
                        if (arrParams[i].ToLowerInvariant() == "tabid")
                        {
                            link = arrParams[i + 1];
                            break;
                        }
                    }
                    if (link == Null.NullInteger.ToString())
                    {
                        link = dialogParams.LinkUrl;
                    }
                }
            }

            return(link);
        }
        public override void HandleRequest()
        {
            string       output       = null;
            DialogParams dialogParams = Content.FromJson <DialogParams>();            // This uses the new JSON Extensions in DotNetNuke.Common.Utilities.JsonExtensionsWeb

            string link = dialogParams.LinkUrl;

            dialogParams.LinkClickUrl = link;

            if (dialogParams != null)
            {
                if (!(dialogParams.LinkAction == "GetLinkInfo"))
                {
                    if (dialogParams.Track)
                    {
                        string tempVar = dialogParams.LinkUrl;
                        dialogParams.LinkClickUrl = GetLinkClickURL(ref dialogParams, ref tempVar);
                        dialogParams.LinkUrl      = tempVar;
                        UrlTrackingInfo linkTrackingInfo = _urlController.GetUrlTracking(dialogParams.PortalId, dialogParams.LinkUrl, dialogParams.ModuleId);

                        if (linkTrackingInfo != null)
                        {
                            dialogParams.Track       = linkTrackingInfo.TrackClicks;
                            dialogParams.TrackUser   = linkTrackingInfo.LogActivity;
                            dialogParams.DateCreated = linkTrackingInfo.CreatedDate.ToString();
                            dialogParams.LastClick   = linkTrackingInfo.LastClick.ToString();
                            dialogParams.Clicks      = linkTrackingInfo.Clicks.ToString();
                        }
                        else
                        {
                            dialogParams.Track     = false;
                            dialogParams.TrackUser = false;
                        }
                        dialogParams.LinkUrl = link;
                    }
                }

                switch (dialogParams.LinkAction)
                {
                case "GetLoggingInfo":                         //also meant for the tracking tab but this is to retrieve the user information
                    DateTime logStartDate = DateTime.MinValue;
                    DateTime logEndDate   = DateTime.MinValue;
                    string   logText      = "<table><tr><th>Date</th><th>User</th></tr><tr><td colspan='2'>The selected date-range did<br /> not return any results.</td></tr>";

                    if (DateTime.TryParse(dialogParams.LogStartDate, out logStartDate))
                    {
                        if (!(DateTime.TryParse(dialogParams.LogEndDate, out logEndDate)))
                        {
                            logEndDate = logStartDate.AddDays(1);
                        }

                        UrlController _urlController = new UrlController();
                        ArrayList     urlLog         = _urlController.GetUrlLog(dialogParams.PortalId, GetLinkUrl(ref dialogParams, dialogParams.LinkUrl), dialogParams.ModuleId, logStartDate, logEndDate);

                        if (urlLog != null)
                        {
                            logText = GetUrlLoggingInfo(urlLog);
                        }
                    }

                    dialogParams.TrackingLog = logText;

                    break;

                case "GetLinkInfo":
                    if (dialogParams.Track)
                    {
                        link = link.Replace(@"\", @"/");

                        //this section is for when the user clicks ok in the dialog box, we actually create a record for the linkclick urls.
                        if (!(dialogParams.LinkUrl.ToLower().Contains("linkclick.aspx")))
                        {
                            dialogParams.LinkClickUrl = GetLinkClickURL(ref dialogParams, ref link);
                        }

                        _urlController.UpdateUrl(dialogParams.PortalId, link, GetURLType(Globals.GetURLType(link)), dialogParams.TrackUser, true, dialogParams.ModuleId, false);
                    }
                    else
                    {
                        //this section is meant for retrieving/displaying the original links and determining if the links are being tracked(making sure the track checkbox properly checked)
                        UrlTrackingInfo linkTrackingInfo = null;

                        if (dialogParams.LinkUrl.Contains("fileticket"))
                        {
                            var queryString     = dialogParams.LinkUrl.Split('=');
                            var encryptedFileId = queryString[1].Split('&')[0];

                            string   fileID    = UrlUtils.DecryptParameter(encryptedFileId, dialogParams.PortalGuid);
                            FileInfo savedFile = _fileController.GetFileById(Int32.Parse(fileID), dialogParams.PortalId);

                            linkTrackingInfo = _urlController.GetUrlTracking(dialogParams.PortalId, string.Format("fileID={0}", fileID), dialogParams.ModuleId);
                        }
                        else if (dialogParams.LinkUrl.ToLowerInvariant().Contains("linkclick.aspx"))
                        {
                            try
                            {
                                if (dialogParams.LinkUrl.Contains("?"))
                                {
                                    link = dialogParams.LinkUrl.Split('?')[1].Split('&')[0];
                                    if (link.Contains("="))
                                    {
                                        link = link.Split('=')[1];
                                    }
                                }

                                int tabId;
                                if (int.TryParse(link, out tabId))                                     //if it's a tabid get the tab path
                                {
                                    dialogParams.LinkClickUrl = TabController.Instance.GetTab(tabId, dialogParams.PortalId, true).FullUrl;
                                    linkTrackingInfo          = _urlController.GetUrlTracking(dialogParams.PortalId, tabId.ToString(), dialogParams.ModuleId);
                                }
                                else
                                {
                                    dialogParams.LinkClickUrl = HttpContext.Current.Server.UrlDecode(link);                                             //get the actual link
                                    linkTrackingInfo          = _urlController.GetUrlTracking(dialogParams.PortalId, dialogParams.LinkClickUrl, dialogParams.ModuleId);
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                                dialogParams.LinkClickUrl = dialogParams.LinkUrl;
                            }
                        }

                        if (linkTrackingInfo == null)
                        {
                            dialogParams.Track     = false;
                            dialogParams.TrackUser = false;
                        }
                        else
                        {
                            dialogParams.Track     = linkTrackingInfo.TrackClicks;
                            dialogParams.TrackUser = linkTrackingInfo.LogActivity;
                        }
                    }
                    break;
                }
                output = dialogParams.ToJson();
            }

            Response.Write(output);
        }