/// <summary>
        /// Returns the list of subdirectories
        /// in the context web directory.
        /// </summary>
        /// <param name="context">Web site HttpContext object</param>
        /// <param name="onlyPublic">If true restrict to public subdirectories</param>
        public static List <string> MakeSubdirectoryList
            (HttpContext context, bool onlyPublic)
        {
            List <string> list = new List <string>();

            if (context == null)
            {
                return(list);
            }

            string path = FileTools.GetDirectoryPath(context);

            DirectoryInfo thisdir = new DirectoryInfo(path);

            DirectoryInfo[] subdirs = thisdir.GetDirectories();

            foreach (DirectoryInfo di in subdirs)
            {
                string name = di.Name;

                if (onlyPublic)
                {
                    if (!SourceTools.IsPublic(name))
                    {
                        continue;
                    }
                }

                list.Add(name);
            }

            return(list);
        }
        /// <summary>
        /// Returns the markup from the search
        /// of either the public directories
        /// or all directories.
        /// </summary>
        /// <param name="page">
        ///     The page calling this method</param>
        /// <param name="pattern">
        ///     The search pattern</param>
        /// <param name="isRegex">
        ///     Is the pattern a regular expression?</param>
        /// <param name="ignoreCase">
        ///     Ignore case in the search?</param>
        /// <param name="statistics">
        ///     Include file statistics markup?</param>
        /// <param name="download">
        ///     Include download button markup?</param>
        /// <param name="onlyPublic">
        ///     Whether or not to restrict to public directories</param>
        public static string SearchSiteMarkup
            (Page page,
            string pattern,
            bool isRegex,
            bool ignoreCase,
            bool statistics,
            bool download,
            bool onlyPublic)
        {
            StringBuilder builder = new StringBuilder();

            string rootPath = FileTools.GetRoot(page);

            List <string> directoryList =
                SourceTools.MakeDirectoryList(rootPath, onlyPublic);

            List <string> tildeDirectoryList =
                FileTools.GetTildePaths(rootPath, directoryList);

            foreach (string tildeDirectoryPath in tildeDirectoryList)
            {
                string markup =
                    SearchDirectoryMarkup(page, tildeDirectoryPath, pattern,
                                          isRegex, ignoreCase, statistics, download, onlyPublic);

                if (!StringTools.IsTrivial(markup))
                {
                    builder.Append(markup);
                }
            }

            return(builder.ToString());
        }
        /// <summary>
        /// Returns the list of files
        /// that reside in the context web directory
        /// and that match the given file type.
        ///
        /// The filetype should take one of seven values:
        ///   1: Text files
        ///   2: Image files
        ///   3: Viewable files, that is, text and image files
        ///   4: Non-viewable files
        ///   5: Text files plus non-viewable files
        ///   6: Image files plus non-viewable files
        ///   7: All files
        /// </summary>
        /// <param name="context">Web site HttpContext object</param>
        /// <param name="fileType">The filetype mask</param>
        public static List <string> MakeFileList
            (HttpContext context, int fileType)
        {
            if (context == null)
            {
                return(new List <string>());
            }

            string directorypath = FileTools.GetDirectoryPath(context);

            return(SourceTools.MakeFileList(directorypath, fileType));
        }
        /// <summary>
        /// Returns the markup from the search
        /// of either the public directories
        /// or all directories that start at
        /// tildeDirectoryPath.
        /// </summary>
        /// <param name="page">
        ///     The page calling this method</param>
        /// <param name="tildeDirectoryPath">
        ///     The root of the directory tree to search</param>
        /// <param name="pattern">
        ///     The search pattern</param>
        /// <param name="isRegex">
        ///     Is the pattern a regular expression?</param>
        /// <param name="ignoreCase">
        ///     Ignore case in the search?</param>
        /// <param name="statistics">
        ///     Include file statistics markup?</param>
        /// <param name="download">
        ///     Include download button markup?</param>
        /// <param name="onlyPublic">
        ///     Whether or not to restrict to public directories</param>
        public static string SearchTreeMarkup
            (Page page,
            string tildeDirectoryPath,
            string pattern,
            bool isRegex,
            bool ignoreCase,
            bool statistics,
            bool download,
            bool onlyPublic)
        {
            StringBuilder builder = new StringBuilder();

            int n = tildeDirectoryPath.Length;

            if (tildeDirectoryPath[n - 1] != SourceTools.slash)
            {
                tildeDirectoryPath = tildeDirectoryPath + SourceTools.slash;
            }

            string directoryPath = page.MapPath(tildeDirectoryPath);

            string rootPath = FileTools.GetRoot(page);

            List <string> directoryList =
                SourceTools.MakeDirectoryList(directoryPath, onlyPublic);

            List <string> tildeDirectoryList =
                FileTools.GetTildePaths(rootPath, directoryList);

            foreach (string tdp in tildeDirectoryList)
            {
                string markup =
                    SearchDirectoryMarkup(page, tdp, pattern,
                                          isRegex, ignoreCase, statistics, download, onlyPublic);

                if (!StringTools.IsTrivial(markup))
                {
                    builder.Append(markup);
                }
            }

            return(builder.ToString());
        }
        private void CommonCode(HttpServerUtility server, string tildeFilePath)
        {
            _path = tildeFilePath;

            if (_path[0] != '~')
            {
                return;
            }

            if (!SourceTools.OKtoServe(tildeFilePath, true))
            {
                return;
            }

            try
            {
                string   filePath = server.MapPath(tildeFilePath);
                FileInfo info     = new FileInfo(filePath);
                _size  = info.Length;
                _date  = info.MostRecentTime();
                _valid = true;

                int  category = FileTools.GetFileCategory(_path);
                bool image    = category == FileTools.IMAGE;

                if (image)
                {
                    using (FileStream stream =
                               new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        try
                        {
                            Bitmap bitmap = new Bitmap(stream);
                            _width  = bitmap.Width;
                            _height = bitmap.Height;
                        }
                        catch { }
                    }
                }
            }
            catch { }
        }
Exemple #6
0
        /// <summary>
        /// First converts tildeDirectoryPath to lowercase.
        ///
        /// Then uses the server to convert tildeDirectoryPath
        /// to an absolute base directory path.
        ///
        /// Then produces the list of absolute directory paths
        /// whose tree roots itself at this base path.
        ///
        /// If there is an error then may return an empty list.
        /// </summary>
        public static List <string> AbsoluteDirectoryPathList
            (HttpServerUtility server, string tildeDirectoryPath)
        {
            List <string> list = new List <string>();

            string path = tildeDirectoryPath.ToLower();

            if (StringTools.IsTrivial(path))
            {
                goto returnstatement;
            }

            if (path[0] != '~')
            {
                goto returnstatement;
            }

            int n = path.Length;

            if (path[n - 1] != '/')
            {
                path = path + '/';
            }

            if (!SourceTools.OKtoServe(path, true))
            {
                goto returnstatement;
            }

            try
            {
                string directoryPath = server.MapPath(path);
                AbsoluteDirectoryPathListHelper(list, directoryPath);
            }
            catch { }


returnstatement:

            return(list);
        }
Exemple #7
0
        /// <summary>
        /// Helper method to perform recursion to populate list.
        ///
        /// The directory path is an absolute path.
        /// </summary>
        private static void AbsoluteDirectoryPathListHelper
            (List <string> list, string directoryPath)
        {
            try
            {
                // If the next 2 steps do not throw an exception
                // then we are fine to proceed
                DirectoryInfo   info = new DirectoryInfo(directoryPath);
                DirectoryInfo[] dirs = info.GetDirectories();

                // Put directoryPath into the list
                list.Add(directoryPath);

                // Do recursion on the subdirectories
                foreach (DirectoryInfo subdir in dirs)
                {
                    string name = subdir.Name;

                    if (!SourceTools.IsPublic(name))
                    {
                        continue;
                    }

                    string path = subdir.FullName;

                    int n = path.Length;

                    if (path[n - 1] != '\\')
                    {
                        path += '\\';
                    }

                    AbsoluteDirectoryPathListHelper(list, path);
                }
            }
            catch { }
        }
        public void ProcessRequest(HttpContext context)
        {
            string url = null;

            try
            {
                url = RequestTools.Query(context.Request);

                if (url.StartsWith("url="))
                {
                    url = url.Substring(4);
                }
            }
            catch (Exception) { }

            if (StringTools.IsTrivial(url))
            {
                context.Response.Redirect(errorpage);
            }

            if (!SourceTools.OKtoServeFile(context.Server, url, true))
            {
                context.Response.Redirect(errorpage + "?" + url);
            }

            string filePath = context.Server.MapPath(url);
            var    fileInfo = new FileInfo(filePath);

            context.Response.AppendHeader
                ("content-type", "application/octet-stream");

            context.Response.AppendHeader
                ("content-disposition", "attachment; filename=" + fileInfo.Name);

            context.Response.TransmitFile(filePath);
        }
        /// <summary>
        /// Returns the list of servable files
        /// in the same directory
        /// as the context web directory.
        /// </summary>
        /// <param name="context">Web site HttpContext object</param>
        public static List <string> MakeServableList(HttpContext context)
        {
            if (context == null)
            {
                return(new List <string>());
            }

            string directorypath = FileTools.GetDirectoryPath(context);

            List <string> filelist =
                SourceTools.MakeFileList(directorypath, FileTools.ALL);

            List <string> servablelist = new List <string>();

            foreach (string name in filelist)
            {
                if (IsServable(name))
                {
                    servablelist.Add(name);
                }
            }

            return(servablelist);
        }
        /// <summary>
        /// Returns the markup from the search
        /// of a file given its tilde path.
        ///
        /// Preconditions: The page that calls this method must:
        ///
        ///   1. Have executed the call
        ///
        ///        SourceTools.LoadCSSandJavascript(this);
        ///
        ///      during the initial call to PageLoad.
        ///
        ///   2. Be able to guarantee that the file is OK to serve
        ///      in context.
        /// </summary>
        /// <param name="page">
        ///     The page calling this method</param>
        /// <param name="tildeFilePath">
        ///     The file to search</param>
        /// <param name="pattern">
        ///     The search pattern</param>
        /// <param name="isRegex">
        ///     Is the pattern a regular expression?</param>
        /// <param name="ignoreCase">
        ///     Ignore case in the search?</param>
        /// <param name="statistics">
        ///     Include file statistics markup?</param>
        /// <param name="download">
        ///     Include download button markup?</param>
        /// <param name="showAllLines">
        ///     Show all lines in the file?</param>
        /// <param name="onlyPublic">
        ///     Whether or not to restrict to public directories</param>
        public static string SearchFileMarkup
            (Page page,
            string tildeFilePath,
            string pattern,
            bool isRegex,
            bool ignoreCase,
            bool statistics,
            bool download,
            bool showAllLines,
            bool onlyPublic)
        {
            string content = null;

            List <Range> nameMatches =
                SearchFileName(tildeFilePath, pattern, isRegex, ignoreCase);

            List <Range> contentMatches =
                SearchFileContent
                    (page, tildeFilePath, pattern,
                    isRegex, ignoreCase, out content);

            bool matchName = !StringTools.IsTrivial(nameMatches);

            bool matchContent = !StringTools.IsTrivial(contentMatches);

            if (!(matchName || matchContent))
            {
                return("");
            }

            StringBuilder builder = new StringBuilder();

            // markup for highlighted tilde file path
            // with possible file view link

            string markupText =
                matchName
                    ? StringTools.HighlightMarkup(tildeFilePath, nameMatches, 4, false, true)
                    : tildeFilePath;

            long     bytes;
            DateTime?created;
            DateTime?modified;

            builder.Append
                (SourceTools.StatisticsMarkup
                    (page, tildeFilePath, markupText, onlyPublic,
                    true, true, statistics, download, null,
                    out bytes, out created, out modified));

            // markup for highlighted content

            if (matchContent)
            {
                builder.Append(HTML_Tools.open_pre);

                builder.Append
                    (StringTools.HighlightMarkup
                        (content, contentMatches, 4, true, showAllLines));

                builder.Append(HTML_Tools.shut_pre);

                builder.Append("\n");
            }

            return(builder.ToString());
        }