示例#1
0
        // Override to modify the attributes of a link
        public virtual void OnPrepareLink(HtmlTag tag)
        {
            if (PrepareLink != null)
            {
                if (PrepareLink(tag))
                {
                    return;
                }
            }

            string url = tag.attributes["href"];

            // No follow?
            if (NoFollowLinks)
            {
                tag.attributes["rel"] = "nofollow";
            }

            // New window?
            if ((NewWindowForExternalLinks && Utils.IsUrlFullyQualified(url)) ||
                (NewWindowForLocalLinks && !Utils.IsUrlFullyQualified(url)))
            {
                tag.attributes["target"] = "_blank";
            }

            // Qualify url
            tag.attributes["href"] = OnQualifyUrl(url);
        }
示例#2
0
        // Override to qualify non-local image and link urls
        public virtual string OnQualifyUrl(string url)
        {
            var q = QualifyUrl?.Invoke(url);

            if (q != null)
            {
                return(url);
            }

            // Quit if we don't have a base location
            if (string.IsNullOrEmpty(UrlBaseLocation))
            {
                return(url);
            }

            // Is the url a fragment?
            if (url.StartsWith("#"))
            {
                return(url);
            }

            // Is the url already fully qualified?
            if (Utils.IsUrlFullyQualified(url))
            {
                return(url);
            }

            if (url.StartsWith("/"))
            {
                if (!string.IsNullOrEmpty(UrlRootLocation))
                {
                    return(UrlRootLocation + url);
                }

                // Need to find domain root
                var pos = UrlBaseLocation.IndexOf("://", StringComparison.Ordinal);
                if (pos == -1)
                {
                    pos = 0;
                }
                else
                {
                    pos += 3;
                }

                // Find the first slash after the protocol separator
                pos = UrlBaseLocation.IndexOf('/', pos);

                // Get the domain name
                var strDomain = pos < 0 ? UrlBaseLocation : UrlBaseLocation.Substring(0, pos);

                // Join em
                return(strDomain + url);
            }
            if (!UrlBaseLocation.EndsWith("/"))
            {
                return(UrlBaseLocation + "/" + url);
            }
            return(UrlBaseLocation + url);
        }
示例#3
0
		// Override to supply the size of an image
		public virtual bool OnGetImageSize(string url, bool TitledImage, out int width, out int height)
		{
			if (GetImageSize != null)
			{
				var info = new ImageInfo() { url = url, titled_image=TitledImage };
				if (GetImageSize(info))
				{
					width = info.width;
					height = info.height;
					return true;
				}
			}

			width = 0;
			height = 0;

			if (Utils.IsUrlFullyQualified(url))
				return false;

			// Work out base location
			string str = url.StartsWith("/") ? DocumentRoot : DocumentLocation;
			if (String.IsNullOrEmpty(str))
				return false;

			// Work out file location
			if (str.EndsWith("/") || str.EndsWith("\\"))
			{
				str=str.Substring(0, str.Length-1);
			}

			if (url.StartsWith("/"))
			{
				url=url.Substring(1);
			}

			str=str + "\\" + url.Replace("/", "\\");


			// 

			//Create an image object from the uploaded file
			try
			{
				var img = System.Drawing.Image.FromFile(str);
				width=img.Width;
				height=img.Height;

				if (MaxImageWidth != 0 && width>MaxImageWidth)
				{
					height=(int)((double)height * (double)MaxImageWidth / (double)width);
					width=MaxImageWidth;
				}

				return true;
			}
			catch (Exception)
			{
				return false;
			}
		}
示例#4
0
		// Override to qualify non-local image and link urls
		public virtual string OnQualifyUrl(string url)
		{
			if (QualifyUrl != null)
			{
				var q = QualifyUrl(url);
				if (q != null)
					return q;
			}

			// Quit if we don't have a base location
			if (String.IsNullOrEmpty(UrlBaseLocation))
				return url;

            // Is the url a fragment?
            if (url.StartsWith("#"))
                return url;

			// Is the url already fully qualified?
			if (Utils.IsUrlFullyQualified(url))
				return url;

			if (url.StartsWith("/"))
			{
				if (!string.IsNullOrEmpty(UrlRootLocation))
				{
					return UrlRootLocation + url;
				}

				// Need to find domain root
				int pos = UrlBaseLocation.IndexOf("://");
				if (pos == -1)
					pos = 0;
				else
					pos += 3;

				// Find the first slash after the protocol separator
				pos = UrlBaseLocation.IndexOf('/', pos);

				// Get the domain name
				string strDomain=pos<0 ? UrlBaseLocation : UrlBaseLocation.Substring(0, pos);

				// Join em
				return strDomain + url;
			}
			else
			{
				if (!UrlBaseLocation.EndsWith("/"))
					return UrlBaseLocation + "/" + url;
				else
					return UrlBaseLocation + url;
			}
		}
 /// <summary>
 /// Validate that a URL is a fully qualified URL or not.
 /// </summary>
 /// <returns><c>true</c> if is URL fully qualified; otherwise, <c>false</c>.</returns>
 /// <param name="url">URL to validate.</param>
 public static bool IsUrlFullyQualified(string url)
 {
     return(Utils.IsUrlFullyQualified(url));
 }
示例#6
0
        // Override to supply the size of an image
        public virtual bool OnGetImageSize(string url, bool TitledImage, out int width, out int height, out string finalUrl)
        {
            finalUrl = url;

            if (GetImageSizeFunc != null)
            {
                var info = new ImageInfo()
                {
                    url = url, titled_image = TitledImage
                };
                if (GetImageSizeFunc(info))
                {
                    width  = info.width;
                    height = info.height;
                    return(true);
                }
            }

            width  = 0;
            height = 0;

            if (Utils.IsUrlFullyQualified(url))
            {
                return(false);
            }

            // Work out base location
            string str = url.StartsWith("/") ? DocumentRoot : DestinationDocumentLocation;

            if (String.IsNullOrEmpty(str))
            {
                return(false);
            }

            // Work out file location
            if (str.EndsWith("/") || str.EndsWith("\\"))
            {
                str = str.Substring(0, str.Length - 1);
            }

            if (url.StartsWith("/"))
            {
                url = url.Substring(1);
            }

            var success = false;

            // Because PathSpecification.RelativeAsFolder creates an additional layer of directories,
            // this trial & error code was implemented to ensure that images could be found
            var count = 0;

            while (count < 2)
            {
                //Create an image object from the uploaded file
                try
                {
                    var fileName   = str + "\\";
                    var currentUrl = url;

                    for (int i = 0; i < count; i++)
                    {
                        currentUrl = "../" + currentUrl;
                    }

                    fileName += currentUrl.Replace("/", "\\");

                    if (File.Exists(fileName))
                    {
                        var img = System.Drawing.Image.FromFile(fileName);
                        width    = img.Width;
                        height   = img.Height;
                        finalUrl = currentUrl;

                        if (MaxImageWidth != 0 && width > MaxImageWidth)
                        {
                            height = (int)((double)height * (double)MaxImageWidth / (double)width);
                            width  = MaxImageWidth;
                        }

                        success = true;
                        break;
                    }
                }
                catch (Exception)
                {
                }

                count++;
            }

            return(success);
        }