Skip to content

Commit 6ba17f7

Browse files
committed
Several fixes & code improvements
1 parent 3e1cb68 commit 6ba17f7

18 files changed

Lines changed: 321 additions & 75 deletions

File tree

src/Libraries/SmartStore.Core/Html/BBCodeHelper.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public partial class BBCodeHelper
1515
private static readonly Regex regexUnderLine = new Regex(@"\[u\](.+?)\[/u\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
1616
private static readonly Regex regexUrl1 = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
1717
private static readonly Regex regexUrl2 = new Regex(@"\[url\](.+?)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
18-
private static readonly Regex regexQuote = new Regex(@"\[quote\](.+?)\[/quote\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
18+
private static readonly Regex regexQuote = new Regex(@"\[quote(=.+?)?\](.+?)\[/quote\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
1919
#endregion
2020

2121
#region Methods
@@ -70,7 +70,30 @@ public static string FormatText(string text, bool replaceBold, bool replaceItali
7070

7171
if (replaceQuote)
7272
{
73-
text = regexQuote.Replace(text, "<blockquote class='muted'>$1</blockquote>");
73+
while (regexQuote.IsMatch(text))
74+
{
75+
text = regexQuote.Replace(text, (m) =>
76+
{
77+
var from = m.Groups[1].Value;
78+
var quote = m.Groups[2].Value;
79+
80+
if (quote.IsEmpty())
81+
{
82+
return "";
83+
}
84+
85+
string result = "";
86+
if (from.HasValue())
87+
{
88+
result += "<span class='quotefrom'>{0}:</span>".FormatCurrent(from.Substring(1));
89+
}
90+
91+
result += "<blockquote class='muted'>{0}</blockquote>".FormatCurrent(quote);
92+
93+
return result;
94+
});
95+
}
96+
7497
}
7598

7699
if (replaceCode)

src/Libraries/SmartStore.Core/IWorkContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public interface IWorkContext
5656
TaxDisplayType GetTaxDisplayTypeFor(Customer customer, int storeId);
5757

5858
/// <summary>
59-
/// Get or set value indicating whether we're in admin area
59+
/// Gets or sets a value indicating whether we're in admin area
6060
/// </summary>
61-
bool IsAdmin { get; set; }
61+
bool IsAdmin { get; set; }
6262

6363
///// <summary>
64-
///// Get or set a value indicating whether we're in the public shop
64+
///// Gets a value indicating whether we're in the public shop
6565
///// </summary>
66-
//bool IsPublic { get; set; }
66+
//bool IsPublic { get; }
6767
}
6868
}

src/Libraries/SmartStore.Core/Infrastructure/ComparableObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ protected void RegisterSignatureProperty(string propertyName)
200200
/// <summary>
201201
/// Generic version of <see cref="ComparableObject" />.
202202
/// </summary>
203+
[Serializable]
203204
public abstract class ComparableObject<T> : ComparableObject, IEquatable<T>
204205
{
205206

src/Libraries/SmartStore.Services/Orders/CheckoutState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Web.Routing;
34

45
namespace SmartStore.Services.Orders
56
{
7+
[Serializable]
68
public partial class CheckoutState
79
{
810
public CheckoutState()

src/Presentation/SmartStore.Web.Framework/Controllers/AdminControllerBase.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Web.Mvc;
4+
using System.Web.Routing;
45
using SmartStore.Core;
56
using SmartStore.Core.Infrastructure;
67
using SmartStore.Services.Localization;
@@ -22,11 +23,13 @@ public abstract class AdminControllerBase : SmartController
2223
/// Initialize controller
2324
/// </summary>
2425
/// <param name="requestContext">Request context</param>
25-
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
26+
protected override void Initialize(RequestContext requestContext)
2627
{
27-
//set work context to admin mode
28-
EngineContext.Current.Resolve<IWorkContext>().IsAdmin = true;
29-
28+
var routeData = requestContext.RouteData;
29+
if (routeData != null && !routeData.DataTokens.ContainsKey("ParentActionViewContext"))
30+
{
31+
EngineContext.Current.Resolve<IWorkContext>().IsAdmin = true;
32+
}
3033
base.Initialize(requestContext);
3134
}
3235

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.Serialization.Formatters.Binary;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Web;
9+
using System.Web.Mvc;
10+
11+
namespace SmartStore.Web.Framework.Controllers
12+
{
13+
internal class CookieTempDataProvider : ITempDataProvider
14+
{
15+
internal const string TempDataCookieKey = "__ControllerTempData";
16+
HttpContextBase _httpContext;
17+
18+
public CookieTempDataProvider(HttpContextBase httpContext)
19+
{
20+
if (httpContext == null)
21+
{
22+
throw new ArgumentNullException("httpContext");
23+
}
24+
_httpContext = httpContext;
25+
}
26+
27+
public System.Web.HttpContextBase HttpContext
28+
{
29+
get
30+
{
31+
return _httpContext;
32+
}
33+
}
34+
35+
protected virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
36+
{
37+
System.Web.HttpCookie cookie = _httpContext.Request.Cookies[TempDataCookieKey];
38+
if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
39+
{
40+
IDictionary<string, object> deserializedTempData = DeserializeTempData(cookie.Value);
41+
return deserializedTempData;
42+
}
43+
44+
return new Dictionary<string, object>();
45+
}
46+
47+
protected virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
48+
{
49+
bool isDirty = (values != null && values.Count > 0);
50+
51+
string cookieValue = SerializeToBase64EncodedString(values);
52+
var cookie = new System.Web.HttpCookie(TempDataCookieKey);
53+
cookie.HttpOnly = true;
54+
55+
// Remove cookie
56+
if (!isDirty)
57+
{
58+
cookie.Expires = DateTime.Now.AddDays(-4.0);
59+
cookie.Value = string.Empty;
60+
61+
_httpContext.Response.Cookies.Set(cookie);
62+
63+
return;
64+
65+
}
66+
cookie.Value = cookieValue;
67+
68+
_httpContext.Response.Cookies.Add(cookie);
69+
}
70+
71+
public static IDictionary<string, object> DeserializeTempData(string base64EncodedSerializedTempData)
72+
{
73+
byte[] bytes = Convert.FromBase64String(base64EncodedSerializedTempData);
74+
var memStream = new MemoryStream(bytes);
75+
var binFormatter = new BinaryFormatter();
76+
return binFormatter.Deserialize(memStream, null) as IDictionary<string, object> /*TempDataDictionary : This returns NULL*/;
77+
}
78+
79+
public static string SerializeToBase64EncodedString(IDictionary<string, object> values)
80+
{
81+
MemoryStream memStream = new MemoryStream();
82+
memStream.Seek(0, SeekOrigin.Begin);
83+
var binFormatter = new BinaryFormatter();
84+
binFormatter.Serialize(memStream, values);
85+
memStream.Seek(0, SeekOrigin.Begin);
86+
byte[] bytes = memStream.ToArray();
87+
return Convert.ToBase64String(bytes);
88+
}
89+
90+
IDictionary<string, object> ITempDataProvider.LoadTempData(ControllerContext controllerContext)
91+
{
92+
return LoadTempData(controllerContext);
93+
}
94+
95+
void ITempDataProvider.SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
96+
{
97+
SaveTempData(controllerContext, values);
98+
}
99+
}
100+
}

src/Presentation/SmartStore.Web.Framework/Controllers/FormValueRequiredAttribute.cs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@ protected internal FormValueRequiredAttribute(
5050

5151
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
5252
{
53-
var form = controllerContext.HttpContext.Request.Form;
53+
return IsValidForRequest(controllerContext.HttpContext.Request.Form);
54+
}
5455

56+
protected internal virtual bool IsValidForRequest(NameValueCollection form)
57+
{
5558
try
5659
{
5760
bool isMatch = false;
5861
if (_rule == FormValueRequirementRule.MatchAny)
5962
{
60-
isMatch = _submitButtonNames.Any(x => IsMatch(controllerContext.HttpContext.Request.Form, x));
63+
isMatch = _submitButtonNames.Any(x => IsMatch(form, x));
6164
}
6265
else
6366
{
64-
isMatch = _submitButtonNames.All(x => IsMatch(controllerContext.HttpContext.Request.Form, x));
67+
isMatch = _submitButtonNames.All(x => IsMatch(form, x));
6568
}
6669
return isMatch;
6770
}
@@ -71,45 +74,7 @@ public override bool IsValidForRequest(ControllerContext controllerContext, Meth
7174
}
7275

7376
return false;
74-
75-
//foreach (string buttonName in _submitButtonNames)
76-
//{
77-
// try
78-
// {
79-
// string value = "";
80-
// switch (_requirement)
81-
// {
82-
// case FormValueRequirement.Equal:
83-
// // do not iterate because "Invalid request" exception can be thrown
84-
// value = form[buttonName];
85-
// break;
86-
// case FormValueRequirement.StartsWith:
87-
// var firstMatch = form.AllKeys.FirstOrDefault(x => x.StartsWith(buttonName, StringComparison.InvariantCultureIgnoreCase));
88-
// if (firstMatch != null)
89-
// {
90-
// value = form[firstMatch];
91-
// }
92-
// break;
93-
// }
94-
95-
// if (!_inverse)
96-
// {
97-
// if (!String.IsNullOrEmpty(value))
98-
// return true;
99-
// }
100-
// else
101-
// {
102-
// if (String.IsNullOrEmpty(value))
103-
// return true;
104-
// }
105-
// }
106-
// catch (Exception exc)
107-
// {
108-
// Debug.WriteLine(exc.Message);
109-
// }
110-
//}
111-
//return false;
112-
}
77+
}
11378

11479
private bool IsMatch(NameValueCollection form, string key)
11580
{

src/Presentation/SmartStore.Web.Framework/Controllers/SmartController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
using System.Web;
36
using System.Web.Mvc;
47
using System.Web.Routing;
58
using SmartStore.Core;
@@ -107,6 +110,5 @@ private void LogException(Exception exc)
107110
var customer = workContext.CurrentCustomer;
108111
Logger.Error(exc.Message, exc, customer);
109112
}
110-
111113
}
112114
}

src/Presentation/SmartStore.Web.Framework/Extensions/HttpContextExtensions.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ public static bool IsAdminArea(this HttpRequest request)
1515
{
1616
if (request != null)
1717
{
18-
var area = request.RequestContext.RouteData.GetAreaName();
19-
if (area != null)
20-
{
21-
return area.IsCaseInsensitiveEqual("admin");
22-
}
18+
return IsAdminArea(new HttpRequestWrapper(request));
2319
}
2420

25-
return false;
21+
return false;
2622
}
2723

2824
public static bool IsAdminArea(this HttpRequestBase request)
@@ -46,6 +42,34 @@ public static bool IsAdminArea(this HttpRequestBase request)
4642
}
4743
}
4844

45+
public static bool IsPublicArea(this HttpRequest request)
46+
{
47+
if (request != null)
48+
{
49+
return IsPublicArea(new HttpRequestWrapper(request));
50+
}
51+
52+
return false;
53+
}
54+
55+
public static bool IsPublicArea(this HttpRequestBase request)
56+
{
57+
try
58+
{
59+
if (request != null)
60+
{
61+
var area = request.RequestContext.RouteData.GetAreaName();
62+
return area.IsEmpty();
63+
}
64+
65+
return false;
66+
}
67+
catch
68+
{
69+
return false;
70+
}
71+
}
72+
4973
public static Stream ToFileStream(this HttpRequestBase request, out string fileName, out string contentType, string paramName = "qqfile") {
5074
fileName = contentType = "";
5175
Stream stream = null;

src/Presentation/SmartStore.Web.Framework/WebWorkContext.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public partial class WebWorkContext : IWorkContext
5050
private Language _cachedLanguage;
5151
private Customer _cachedCustomer;
5252
private Currency _cachedCurrency;
53-
private Customer _originalCustomerIfImpersonated;
53+
private Customer _originalCustomerIfImpersonated;
54+
private bool? _isAdmin;
5455

5556
public WebWorkContext(Func<string, ICacheManager> cacheManager,
5657
HttpContextBase httpContext,
@@ -504,10 +505,23 @@ public TaxDisplayType GetTaxDisplayTypeFor(Customer customer, int storeId)
504505
return _cachedTaxDisplayType.Value;
505506
}
506507

507-
/// <summary>
508-
/// Get or set value indicating whether we're in admin area
509-
/// </summary>
510-
public bool IsAdmin { get; set; }
508+
509+
public bool IsAdmin
510+
{
511+
get
512+
{
513+
if (!_isAdmin.HasValue)
514+
{
515+
_isAdmin = _httpContext.Request.IsAdminArea();
516+
}
517+
518+
return _isAdmin.Value;
519+
}
520+
set
521+
{
522+
_isAdmin = value;
523+
}
524+
}
511525

512526
public bool IsPublishedLanguage(string seoCode, int storeId = 0)
513527
{

0 commit comments

Comments
 (0)