internal UrlBuilder(HttpContextBase httpContext, VirtualPathUtilityBase virtualPathUtility, string path, object parameters)
        {
            _virtualPathUtility = virtualPathUtility;
            Uri uri;
            if (Uri.TryCreate(path, UriKind.Absolute, out uri))
            {
                _path = uri.GetLeftPart(UriPartial.Path);
                _params.Append(uri.Query);
            }
            else
            {
                // If the url is being built as part of a WebPages request, use the template stack to identify the current template's virtual path.
                _path = GetPageRelativePath(httpContext, path);
                int queryStringIndex = (_path ?? String.Empty).IndexOf('?');
                if (queryStringIndex != -1)
                {
                    _params.Append(_path.Substring(queryStringIndex));
                    _path = _path.Substring(0, queryStringIndex);
                }
            }

            if (parameters != null)
            {
                AddParam(parameters);
            }
        }
        internal UrlBuilder(HttpContextBase httpContext, VirtualPathUtilityBase virtualPathUtility, string path, object parameters)
        {
            _virtualPathUtility = virtualPathUtility;
            Uri uri;

            if (Uri.TryCreate(path, UriKind.Absolute, out uri))
            {
                _path = uri.GetLeftPart(UriPartial.Path);
                _params.Append(uri.Query);
            }
            else
            {
                // If the url is being built as part of a WebPages request, use the template stack to identify the current template's virtual path.
                _path = GetPageRelativePath(httpContext, path);
                int queryStringIndex = (_path ?? String.Empty).IndexOf('?');
                if (queryStringIndex != -1)
                {
                    _params.Append(_path.Substring(queryStringIndex));
                    _path = _path.Substring(0, queryStringIndex);
                }
            }

            if (parameters != null)
            {
                AddParam(parameters);
            }
        }
Example #3
0
        internal static HelperResult Flash(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path,
                                           string width           = null, string height  = null, bool play = true, bool loop = true, bool menu = true,
                                           string backgroundColor = null, string quality = null, string scale   = null, string windowMode     = null,
                                           string baseUrl         = null, string version = null, object options = null, object htmlAttributes = null, string embedName = null)
        {
            var parameters = ObjectToDictionary(options, "options", _flashBlacklist);

            if (!play)
            {
                parameters["play"] = false;
            }
            if (!loop)
            {
                parameters["loop"] = false;
            }
            if (!menu)
            {
                parameters["menu"] = false;
            }
            if (!String.IsNullOrEmpty(backgroundColor))
            {
                parameters["bgColor"] = backgroundColor;
            }
            if (!String.IsNullOrEmpty(quality))
            {
                parameters["quality"] = quality;
            }
            if (!String.IsNullOrEmpty(scale))
            {
                parameters["scale"] = scale;
            }
            if (!String.IsNullOrEmpty(windowMode))
            {
                parameters["wmode"] = windowMode;
            }
            if (!String.IsNullOrEmpty(baseUrl))
            {
                parameters["base"] = baseUrl;
            }

            string cab = FlashCab;

            if (!String.IsNullOrEmpty(version))
            {
                cab += "#version=" + version.Replace('.', ',');
            }
            return(GetHtml(context, pathUtility, path, width, height,
                           OleMimeType, null, FlashClassId, cab, "movie", FlashMimeType, parameters, htmlAttributes, embedName));
        }
Example #4
0
        internal static HelperResult MediaPlayer(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width = null, string height = null,
                                                 bool autoStart         = true, int playCount         = 1, string uiMode       = null, bool stretchToFit = false,
                                                 bool enableContextMenu = true, bool mute             = false, int volume      = -1, string baseUrl      = null,
                                                 object options         = null, object htmlAttributes = null, string embedName = null)
        {
            var parameters = ObjectToDictionary(options, "options", _mediaPlayerBlacklist);

            if (!autoStart)
            {
                parameters["autoStart"] = false;
            }
            if (playCount != 1)
            {
                parameters["playCount"] = playCount;
            }
            if (!String.IsNullOrEmpty(uiMode))
            {
                parameters["uiMode"] = uiMode;
            }
            if (stretchToFit)
            {
                parameters["stretchToFit"] = true;
            }
            if (!enableContextMenu)
            {
                parameters["enableContextMenu"] = false;
            }
            if (mute)
            {
                parameters["mute"] = true;
            }
            if (volume >= 0)
            {
                parameters["volume"] = Math.Min(volume, 100);
            }
            if (!String.IsNullOrEmpty(baseUrl))
            {
                parameters["baseURL"] = baseUrl;
            }

            return(GetHtml(context, pathUtility, path, width, height,
                           null, null, MediaPlayerClassId, null, "URL", MediaPlayerMimeType, parameters, htmlAttributes, embedName));
        }
Example #5
0
        internal static HelperResult Silverlight(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width, string height,
                                                 string backgroundColor = null, string initParameters = null, string minimumVersion = null, bool autoUpgrade = true,
                                                 object options         = null, object htmlAttributes = null)
        {
            if (String.IsNullOrEmpty(width))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "width");
            }
            if (String.IsNullOrEmpty(height))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "height");
            }

            var parameters = ObjectToDictionary(options, "options", _silverlightBlacklist);

            if (!String.IsNullOrEmpty(backgroundColor))
            {
                parameters["background"] = backgroundColor;
            }
            if (!String.IsNullOrEmpty(initParameters))
            {
                parameters["initparams"] = initParameters;
            }
            if (!String.IsNullOrEmpty(minimumVersion))
            {
                parameters["minruntimeversion"] = minimumVersion;
            }
            if (!autoUpgrade)
            {
                parameters["autoUpgrade"] = autoUpgrade;
            }

            return(GetHtml(context, pathUtility, path, width, height,
                           SilverlightMimeType, "data:" + SilverlightMimeType + ",", // ',' required for Opera support
                           null, null, "source", null, parameters, htmlAttributes, null,
                           tw =>
            {
                tw.WriteLine("<a href=\"http://go.microsoft.com/fwlink/?LinkID=149156\" style=\"text-decoration:none\">");
                tw.WriteLine("<img src=\"http://go.microsoft.com/fwlink?LinkId=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-style:none\"/>");
                tw.WriteLine("</a>");
            }));
        }
Example #6
0
        internal static bool Validate(HttpContextBase context, string privateKey, VirtualPathUtilityBase virtualPathUtility)
        {
            privateKey = privateKey ?? PrivateKey;

            if (String.IsNullOrEmpty(privateKey))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "privateKey");
            }

            SetLastError(context, String.Empty);
            string postedBody = GetValidatePostData(context, privateKey, virtualPathUtility);

            if (String.IsNullOrEmpty(postedBody))
            {
                return(false);
            }
            string result = ExecuteValidateRequest(postedBody);

            return(HandleValidateResponse(context, result));
        }
Example #7
0
        private static string ValidatePath(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "path");
            }
            string originalPath = path;

            if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                // resolve relative paths
                path = pathUtility.Combine(context.Request.AppRelativeCurrentExecutionFilePath, path);
                // resolve to app absolute - SL doesn't support app relative
                path = pathUtility.ToAbsolute(path);
                if (!File.Exists(context.Server.MapPath(path)))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                                                                      HelpersToolkitResources.Video_FileDoesNotExist, originalPath));
                }
            }
            return(path);
        }
Example #8
0
        private static HelperResult GetHtml(HttpContextBase context, VirtualPathUtilityBase pathUtility,
                                            string path, string width, string height, string objectType, string objectDataType,
                                            string objectClassId, string objectCodeBase, string pathParamName, string embedContentType,
                                            IDictionary <string, object> parameters = null, object htmlAttributes = null, string embedName = null,
                                            Action <TextWriter> plugin = null)
        {
            path = ValidatePath(context, pathUtility, path);

            var objectAttr = ObjectToDictionary(htmlAttributes, "htmlAttributes", _globalBlacklist);

            objectAttr["width"]    = width;
            objectAttr["height"]   = height;
            objectAttr["type"]     = objectType;
            objectAttr["data"]     = objectDataType;
            objectAttr["classid"]  = objectClassId;
            objectAttr["codebase"] = objectCodeBase;

            return(new HelperResult(tw =>
            {
                tw.Write("<object ");
                foreach (var a in objectAttr.OrderBy(a => a.Key, StringComparer.OrdinalIgnoreCase))
                {
                    var value = (a.Value == null) ? null : a.Value.ToString();
                    WriteIfNotNullOrEmpty(tw, a.Key, value);
                }
                tw.WriteLine(">");

                // object parameters
                if (!String.IsNullOrEmpty(pathParamName))
                {
                    tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />",
                                 HttpUtility.HtmlAttributeEncode(pathParamName),
                                 HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
                }
                if (parameters != null)
                {
                    foreach (var p in parameters)
                    {
                        tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />",
                                     HttpUtility.HtmlAttributeEncode(p.Key),
                                     HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
                    }
                }

                if (!String.IsNullOrEmpty(embedContentType))
                {
                    tw.Write("<embed src=\"{0}\" ", HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
                    WriteIfNotNullOrEmpty(tw, "width", width);
                    WriteIfNotNullOrEmpty(tw, "height", height);
                    WriteIfNotNullOrEmpty(tw, "name", embedName);
                    WriteIfNotNullOrEmpty(tw, "type", embedContentType);
                    if (parameters != null)
                    {
                        foreach (var p in parameters)
                        {
                            tw.Write("{0}=\"{1}\" ", HttpUtility.HtmlEncode(p.Key), HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
                        }
                    }
                    tw.WriteLine("/>");
                }
                if (plugin != null)
                {
                    plugin(tw);
                }
                tw.WriteLine("</object>");
            }));
        }
Example #9
0
        internal static HelperResult Flash(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path,
            string width = null, string height = null, bool play = true, bool loop = true, bool menu = true,
            string bgColor = null, string quality = null, string scale = null, string windowMode = null,
            string baseUrl = null, string version = null, object options = null, object htmlAttributes = null, string embedName = null) {

            Dictionary<string, object> parameters = ObjectToDictionary(options, "options", FlashBlacklist);
            if (!play) {
                parameters["play"] = false;
            }
            if (!loop) {
                parameters["loop"] = false;
            }
            if (!menu) {
                parameters["menu"] = false;
            }
            if (!String.IsNullOrEmpty(bgColor)) {
                parameters["bgColor"] = bgColor;
            }
            if (!String.IsNullOrEmpty(quality)) {
                parameters["quality"] = quality;
            }
            if (!String.IsNullOrEmpty(scale)) {
                parameters["scale"] = scale;
            }
            if (!String.IsNullOrEmpty(windowMode)) {
                parameters["wmode"] = windowMode;
            }
            if (!String.IsNullOrEmpty(baseUrl)) {
                parameters["base"] = baseUrl;
            }

            string cab = FlashCab;
            if (!String.IsNullOrEmpty(version)) {
                cab += "#version=" + version.Replace('.', ',');
            }
            return GetHtml(context, pathUtility, path, width, height,
                OleMimeType, null, FlashClassId, cab, "movie", FlashMimeType, parameters, htmlAttributes, embedName);
        }
Example #10
0
 private static string ValidatePath(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path) {
     if (String.IsNullOrEmpty(path)) {
         throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "path");
     }
     string _path = path;
     if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) {
         // resolve relative paths
         path = pathUtility.Combine(context.Request.AppRelativeCurrentExecutionFilePath, path);
         // resolve to app absolute - SL doesn't support app relative
         path = pathUtility.ToAbsolute(path);
         if (!File.Exists(context.Server.MapPath(path))) {
             throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                 HelpersToolkitResources.Video_FileDoesNotExist, _path));
         }
     }
     return path;
 }
Example #11
0
        private static HelperResult GetHtml(HttpContextBase context, VirtualPathUtilityBase pathUtility,
            string path, string width, string height, string objectType, string objectDataType,
            string objectClassId, string objectCodeBase, string pathParamName, string embedContentType,
            IDictionary<string, object> parameters = null, object htmlAttributes = null, string embedName = null,
            Action<TextWriter> plugin = null) {

            path = ValidatePath(context, pathUtility, path);

            Dictionary<string, object> objectAttr = ObjectToDictionary(htmlAttributes, "htmlAttributes", GlobalBlacklist);
            objectAttr["width"] = width;
            objectAttr["height"] = height;
            objectAttr["type"] = objectType;
            objectAttr["data"] = objectDataType;
            objectAttr["classid"] = objectClassId;
            objectAttr["codebase"] = objectCodeBase;

            return new HelperResult(tw => {
                tw.Write("<object ");
                foreach (var a in objectAttr.OrderBy(a => a.Key, StringComparer.OrdinalIgnoreCase)) {
                    var value = (a.Value == null) ? null : a.Value.ToString();
                    WriteIfNotNullOrEmpty(tw, a.Key, value);
                }
                tw.WriteLine(">");

                // object parameters
                if (!String.IsNullOrEmpty(pathParamName)) {
                    tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />", 
                        HttpUtility.HtmlAttributeEncode(pathParamName), 
                        HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
                }
                if (parameters != null) {
                    foreach (var p in parameters) {
                        tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />", 
                            HttpUtility.HtmlAttributeEncode(p.Key),
                            HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
                    }
                }

                // embed tag and parameters: used by Netscape and IE on Mac
                if (!String.IsNullOrEmpty(embedContentType)) {
                    tw.Write("<embed src=\"{0}\" ", HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
                    WriteIfNotNullOrEmpty(tw, "width", width);
                    WriteIfNotNullOrEmpty(tw, "height", height);
                    WriteIfNotNullOrEmpty(tw, "name", embedName);
                    WriteIfNotNullOrEmpty(tw, "type", embedContentType);
                    if (parameters != null) {
                        foreach (var p in parameters) {
                            tw.Write("{0}=\"{1}\" ", HttpUtility.HtmlEncode(p.Key), HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
                        }
                    }
                    tw.WriteLine("/>");
                }
                if (plugin != null) {
                    plugin(tw);
                }
                tw.WriteLine("</object>");
            });
        }
Example #12
0
        internal static HelperResult Silverlight(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width, string height,
            string bgColor = null, string initParameters = null, string minimumVersion = null, bool autoUpgrade = true,
            object options = null, object htmlAttributes = null) {

            if (String.IsNullOrEmpty(width)) {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "width");
            }
            if (String.IsNullOrEmpty(height)) {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "height");
            }

            Dictionary<string, object> parameters = ObjectToDictionary(options, "options", SilverlightBlacklist);
            if (!String.IsNullOrEmpty(bgColor)) {
                parameters["background"] = bgColor;
            }
            if (!String.IsNullOrEmpty(initParameters)) {
                parameters["initparams"] = initParameters;
            }
            if (!String.IsNullOrEmpty(minimumVersion)) {
                parameters["minruntimeversion"] = minimumVersion;
            }
            if (!autoUpgrade) {
                parameters["autoUpgrade"] = autoUpgrade;
            }

            return GetHtml(context, pathUtility, path, width, height,
                SilverlightMimeType, "data:" + SilverlightMimeType + ",", // ',' required for Opera support
                null, null, "source", null, parameters, htmlAttributes, null,
                tw => {
                    tw.WriteLine("<a href=\"http://go.microsoft.com/fwlink/?LinkID=149156\" style=\"text-decoration:none\">");
                    tw.WriteLine("<img src=\"http://go.microsoft.com/fwlink?LinkId=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-style:none\"/>");
                    tw.WriteLine("</a>");
                });
        }
Example #13
0
        internal static HelperResult MediaPlayer(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width = null, string height = null,
            bool autoStart = true, int playCount = 1, string uiMode = null, bool stretchToFit = false,
            bool enableContextMenu = true, bool mute = false, int volume = -1, string baseUrl = null,
            object options = null, object htmlAttributes = null, string embedName = null) {

            Dictionary<string, object> parameters = ObjectToDictionary(options, "options", MediaPlayerBlacklist);
            if (!autoStart) {
                parameters["autoStart"] = false;
            }
            if (playCount != 1) {
                parameters["playCount"] = playCount;
            }
            if (!String.IsNullOrEmpty(uiMode)) {
                parameters["uiMode"] = uiMode;
            }
            if (stretchToFit) {
                parameters["stretchToFit"] = true;
            }
            if (!enableContextMenu) {
                parameters["enableContextMenu"] = false;
            }
            if (mute) {
                parameters["mute"] = true;
            }
            if (volume >= 0) {
                parameters["volume"] = Math.Min(volume, 100);
            }
            if (!string.IsNullOrEmpty(baseUrl)) {
                parameters["baseURL"] = baseUrl;
            }

            return GetHtml(context, pathUtility, path, width, height,
                null, null, MediaPlayerClassId, null, "URL", MediaPlayerMimeType, parameters, htmlAttributes, embedName);
        }
    internal static string GetValidatePostData(HttpContextBase context, string privateKey, VirtualPathUtilityBase virtualPathUtility) {
        string remoteIP = context.Request.ServerVariables["REMOTE_ADDR"];
        if (String.IsNullOrEmpty(remoteIP)) {
            throw new InvalidOperationException(HelpersToolkitResources.ReCaptcha_RemoteIPNotFound);
        }

        // Noscript rendering requires the user to copy and paste the challenge string to a textarea.
        // When the challenge is invalid the recaptcha service doesn't return an error that affects
        // UI rendering, so Validate should just return false without issuing the web request.
        string challenge = context.Request.Form["recaptcha_challenge_field"];
        if (String.IsNullOrEmpty(challenge)) {
            return String.Empty;
        }
        string response = (context.Request.Form["recaptcha_response_field"] ?? String.Empty).Trim();

        var builder = new UrlBuilder(context, virtualPathUtility, path: null, parameters: null)
                .AddParam("privatekey", privateKey)
                .AddParam("remoteip", context.Request.ServerVariables["REMOTE_ADDR"])
                .AddParam("challenge", challenge)
                .AddParam("response", response);
        
        // Trim the leading ? and return the QueryString
        return builder.QueryString.Substring(1);
    }
    internal static bool Validate(HttpContextBase context, string privateKey, VirtualPathUtilityBase virtualPathUtility) {
        privateKey = privateKey ?? PrivateKey;

        if (String.IsNullOrEmpty(privateKey)) {
            throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "privateKey");
        }

        SetLastError(context, String.Empty);
        string postedBody = GetValidatePostData(context, privateKey, virtualPathUtility);
        if (String.IsNullOrEmpty(postedBody)) {
            return false;
        }
        string result = ExecuteValidateRequest(postedBody);
        return HandleValidateResponse(context, result);
    }
Example #16
0
        internal static string GetValidatePostData(HttpContextBase context, string privateKey, VirtualPathUtilityBase virtualPathUtility)
        {
            string remoteIP = context.Request.ServerVariables["REMOTE_ADDR"];

            if (String.IsNullOrEmpty(remoteIP))
            {
                throw new InvalidOperationException(HelpersToolkitResources.ReCaptcha_RemoteIPNotFound);
            }

            // Noscript rendering requires the user to copy and paste the challenge string to a textarea.
            // When the challenge is invalid the recaptcha service doesn't return an error that affects
            // UI rendering, so Validate should just return false without issuing the web request.
            string challenge = context.Request.Form["recaptcha_challenge_field"];

            if (String.IsNullOrEmpty(challenge))
            {
                return(String.Empty);
            }
            string response = (context.Request.Form["recaptcha_response_field"] ?? String.Empty).Trim();

            var builder = new UrlBuilder(context, virtualPathUtility, path: null, parameters: null)
                          .AddParam("privatekey", privateKey)
                          .AddParam("remoteip", context.Request.ServerVariables["REMOTE_ADDR"])
                          .AddParam("challenge", challenge)
                          .AddParam("response", response);

            // Trim the leading ? and return the QueryString
            return(builder.QueryString.Substring(1));
        }