Implements what described in #6688 (#7356)

Fixes #6688
This commit is contained in:
Hermes Sbicego
2016-11-17 22:02:54 +01:00
committed by Sébastien Ros
parent 7e859f2ccf
commit 4299239482

View File

@@ -1,16 +1,21 @@
using System;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Localization;
using System.Globalization;
using System.Text.RegularExpressions;
namespace Orchard.Tokens.Providers {
public class RequestTokens : ITokenProvider {
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IContentManager _contentManager;
private static string[] _textChainableTokens;
public RequestTokens(IWorkContextAccessor workContextAccessor, IContentManager contentManager) {
_workContextAccessor = workContextAccessor;
_contentManager = contentManager;
_textChainableTokens = new string[] { "QueryString", "Form" };
T = NullLocalizer.Instance;
}
@@ -18,8 +23,8 @@ namespace Orchard.Tokens.Providers {
public void Describe(DescribeContext context) {
context.For("Request", T("Http Request"), T("Current Http Request tokens."))
.Token("QueryString:*", T("QueryString:<element>"), T("The Query String value for the specified element."))
.Token("Form:*", T("Form:<element>"), T("The Form value for the specified element."))
.Token("QueryString:*", T("QueryString:<element>"), T("The Query String value for the specified element. To chain text, surround the <element> with parentheses, e.g. 'QueryString:(param1)'."))
.Token("Form:*", T("Form:<element>"), T("The Form value for the specified element. To chain text, surround the <element> with parentheses, e.g. 'Form:(param1)'."))
.Token("Route:*", T("Route:<key>"), T("The Route value for the specified key."))
.Token("Content", T("Content"), T("The request routed Content Item."), "Content")
;
@@ -29,20 +34,30 @@ namespace Orchard.Tokens.Providers {
if (_workContextAccessor.GetContext().HttpContext == null) {
return;
}
/* Supported Syntaxes for Request and Form tokens are:
* 1. QueryString:(param1) or Form:(param1)
* 2. QueryString:param1 or Form:param1
* 3. QueryString:(param1).SomeOtherTextToken or Form:(param1).SomeOtherTextToken
*
* If you want to Chain TextTokens you have to use the 3rd syntax
* the element, here param1, has been surrounded with parentheses in order to preserve backward compatibility.
*/
context.For("Request", _workContextAccessor.GetContext().HttpContext.Request)
.Token(
token => token.StartsWith("QueryString:", StringComparison.OrdinalIgnoreCase) ? token.Substring("QueryString:".Length) : null,
(token, request) => request.QueryString.Get(token)
FilterTokenParam,
(token, request) => {
return request.QueryString.Get(token);
}
)
.Token(
token => token.StartsWith("Form:", StringComparison.OrdinalIgnoreCase) ? token.Substring("Form:".Length) : null,
FilterTokenParam,
(token, request) => request.Form.Get(token)
)
.Token(
token => token.StartsWith("Route:", StringComparison.OrdinalIgnoreCase) ? token.Substring("Route:".Length) : null,
(token, request) => GetRouteValue(token, request)
)
.Chain(FilterChainParam, "Text", (token, request) => request.QueryString.Get(token))
.Token("Content",
(request) => DisplayText(GetRoutedContentItem(request))
)
@@ -98,5 +113,53 @@ namespace Orchard.Tokens.Providers {
return _contentManager.GetItemMetadata(content).DisplayText;
}
private static string FilterTokenParam(string token) {
string tokenPrefix;
int chainIndex, tokenLength;
if (token.IndexOf(":") == -1) {
return null;
}
tokenPrefix = token.Substring(0, token.IndexOf(":"));
if (!_textChainableTokens.Contains(tokenPrefix, StringComparer.OrdinalIgnoreCase)) {
return null;
}
// use ")." as chars combination to discover the end of the parameter
chainIndex = token.IndexOf(").") + 1;
tokenLength = (tokenPrefix + ":").Length;
if (chainIndex == 0) {// ")." has not be found
return token.Substring(tokenLength).Trim(new char[] { '(', ')' });
}
if (!token.StartsWith((tokenPrefix + ":"), StringComparison.OrdinalIgnoreCase) || chainIndex <= tokenLength) {
return null;
}
return token.Substring(tokenLength, chainIndex - tokenLength).Trim(new char[] { '(', ')' });
}
private static Tuple<string, string> FilterChainParam(string token) {
string tokenPrefix;
int chainIndex, tokenLength;
if (token.IndexOf(":") == -1) {
return null;
}
tokenPrefix = token.Substring(0, token.IndexOf(":"));
if (!_textChainableTokens.Contains(tokenPrefix, StringComparer.OrdinalIgnoreCase)) {
return null;
}
// use ")." as chars combination to discover the end of the parameter
chainIndex = token.IndexOf(").") + 1;
tokenLength = (tokenPrefix + ":").Length;
if (chainIndex == 0) { // ")." has not be found
return new Tuple<string, string>(token.Substring(tokenLength).Trim(new char[] { '(', ')' }), token.Length.ToString());
}
if (!token.StartsWith((tokenPrefix + ":"), StringComparison.OrdinalIgnoreCase) || chainIndex <= tokenLength) {
return null;
}
return new Tuple<string, string>(token.Substring(tokenLength, chainIndex - tokenLength).Trim(new char[] { '(', ')' }), token.Substring(chainIndex + 1));
}
}
}
}