forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonController.cs
More file actions
658 lines (565 loc) · 26.4 KB
/
Copy pathCommonController.cs
File metadata and controls
658 lines (565 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
using Grand.Core;
using Grand.Core.Domain;
using Grand.Core.Domain.Catalog;
using Grand.Core.Domain.Common;
using Grand.Core.Domain.Customers;
using Grand.Core.Domain.Localization;
using Grand.Core.Domain.Media;
using Grand.Core.Domain.Messages;
using Grand.Core.Domain.Vendors;
using Grand.Framework.Localization;
using Grand.Framework.Mvc.Filters;
using Grand.Framework.Security;
using Grand.Framework.Security.Captcha;
using Grand.Framework.Themes;
using Grand.Services.Common;
using Grand.Services.Customers;
using Grand.Services.Localization;
using Grand.Services.Logging;
using Grand.Services.Media;
using Grand.Services.Messages;
using Grand.Services.Stores;
using Grand.Services.Vendors;
using Grand.Web.Interfaces;
using Grand.Web.Models.Common;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public partial class CommonController : BasePublicController
{
#region Fields
private readonly ICommonViewModelService _commonViewModelService;
private readonly ILocalizationService _localizationService;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly ICustomerActivityService _customerActivityService;
private readonly ICustomerActionEventService _customerActionEventService;
private readonly IPopupService _popupService;
private readonly IContactAttributeService _contactAttributeService;
private readonly CommonSettings _commonSettings;
private readonly CaptchaSettings _captchaSettings;
private readonly VendorSettings _vendorSettings;
#endregion
#region Constructors
public CommonController(
ICommonViewModelService commonViewModelService,
ILocalizationService localizationService,
IWorkContext workContext,
IStoreContext storeContext,
ICustomerActivityService customerActivityService,
ICustomerActionEventService customerActionEventService,
IPopupService popupService,
IContactAttributeService contactAttributeService,
CommonSettings commonSettings,
CaptchaSettings captchaSettings,
VendorSettings vendorSettings
)
{
this._commonViewModelService = commonViewModelService;
this._localizationService = localizationService;
this._workContext = workContext;
this._storeContext = storeContext;
this._customerActivityService = customerActivityService;
this._customerActionEventService = customerActionEventService;
this._popupService = popupService;
this._contactAttributeService = contactAttributeService;
this._commonSettings = commonSettings;
this._captchaSettings = captchaSettings;
this._vendorSettings = vendorSettings;
}
#endregion
#region Methods
//page not found
public virtual IActionResult PageNotFound([FromServices] ILogger logger)
{
if (_commonSettings.Log404Errors)
{
var statusCodeReExecuteFeature = HttpContext?.Features?.Get<IStatusCodeReExecuteFeature>();
logger.Error(string.Format("Error 404. The requested page ({0}) was not found", statusCodeReExecuteFeature?.OriginalPath),
customer: _workContext.CurrentCustomer);
}
this.Response.StatusCode = 404;
this.Response.ContentType = "text/html";
return View();
}
//available even when a store is closed
[CheckAccessClosedStore(true)]
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> SetLanguage(
[FromServices] ILanguageService languageService,
[FromServices] LocalizationSettings localizationSettings,
string langid, string returnUrl = "")
{
var language = await languageService.GetLanguageById(langid);
if (!language?.Published ?? false)
language = _workContext.WorkingLanguage;
//home page
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//language part in URL
if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
{
//remove current language code if it's already localized URL
if (await returnUrl.IsLocalizedUrlAsync(languageService, this.Request.PathBase, true))
returnUrl = returnUrl.RemoveLanguageSeoCodeFromUrl(this.Request.PathBase, true);
//and add code of passed language
returnUrl = returnUrl.AddLanguageSeoCodeToUrl(this.Request.PathBase, true, language);
}
await _workContext.SetWorkingLanguage(language);
return Redirect(returnUrl);
}
//helper method to redirect users.
public virtual IActionResult InternalRedirect(string url, bool permanentRedirect)
{
//ensure it's invoked from our GenericPathRoute class
if (HttpContext.Items["grand.RedirectFromGenericPathRoute"] == null ||
!Convert.ToBoolean(HttpContext.Items["grand.RedirectFromGenericPathRoute"]))
{
url = Url.RouteUrl("HomePage");
permanentRedirect = false;
}
//home page
if (string.IsNullOrEmpty(url))
{
url = Url.RouteUrl("HomePage");
permanentRedirect = false;
}
//prevent open redirection attack
if (!Url.IsLocalUrl(url))
{
url = Url.RouteUrl("HomePage");
permanentRedirect = false;
}
url = Uri.EscapeUriString(WebUtility.UrlDecode(url));
if (permanentRedirect)
return RedirectPermanent(url);
return Redirect(url);
}
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> SetCurrency(string customerCurrency, string returnUrl = "")
{
await _commonViewModelService.SetCurrency(customerCurrency);
//home page
if (String.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
return Redirect(returnUrl);
}
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> SetStore(
[FromServices] IStoreService storeService,
string store, string returnUrl = "")
{
var currentstoreid = _storeContext.CurrentStore.Id;
if (currentstoreid != store)
await _commonViewModelService.SetStore(store);
var prevStore = await storeService.GetStoreById(currentstoreid);
var currStore = await storeService.GetStoreById(store);
if (prevStore != null && currStore != null)
{
if (prevStore.Url != currStore.Url)
{
return Redirect(currStore.SslEnabled ? currStore.SecureUrl : currStore.Url);
}
}
//home page
if (String.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
return Redirect(returnUrl);
}
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> SetTaxType(int customerTaxType, string returnUrl = "")
{
await _commonViewModelService.SetTaxType(customerTaxType);
//home page
if (String.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
return Redirect(returnUrl);
}
//contact us page
//available even when a store is closed
[CheckAccessClosedStore(true)]
public virtual async Task<IActionResult> ContactUs()
{
var model = await _commonViewModelService.PrepareContactUs();
return View(model);
}
[HttpPost, ActionName("ContactUs")]
[PublicAntiForgery]
[ValidateCaptcha]
//available even when a store is closed
[CheckAccessClosedStore(true)]
public virtual async Task<IActionResult> ContactUsSend(ContactUsModel model, IFormCollection form, bool captchaValid,
[FromServices] IContactAttributeFormatter contactAttributeFormatter)
{
//validate CAPTCHA
if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
{
ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
}
//parse contact attributes
var attributeXml = await _commonViewModelService.ParseContactAttributes(form);
var contactAttributeWarnings = await _commonViewModelService.GetContactAttributesWarnings(attributeXml);
if (contactAttributeWarnings.Any())
{
foreach (var item in contactAttributeWarnings)
{
ModelState.AddModelError("", item);
}
}
if (ModelState.IsValid)
{
model.ContactAttributeXml = attributeXml;
model.ContactAttributeInfo = await contactAttributeFormatter.FormatAttributes(attributeXml, _workContext.CurrentCustomer);
model = await _commonViewModelService.SendContactUs(model);
//activity log
await _customerActivityService.InsertActivity("PublicStore.ContactUs", "", _localizationService.GetResource("ActivityLog.PublicStore.ContactUs"));
return View(model);
}
model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage;
model.ContactAttributes = await _commonViewModelService.PrepareContactAttributeModel(attributeXml);
return View(model);
}
//contact vendor page
public virtual async Task<IActionResult> ContactVendor(string vendorId, [FromServices] IVendorService vendorService)
{
if (!_vendorSettings.AllowCustomersToContactVendors)
return RedirectToRoute("HomePage");
var vendor = await vendorService.GetVendorById(vendorId);
if (vendor == null || !vendor.Active || vendor.Deleted)
return RedirectToRoute("HomePage");
var model = await _commonViewModelService.PrepareContactVendor(vendor);
return View(model);
}
[HttpPost, ActionName("ContactVendor")]
[PublicAntiForgery]
[ValidateCaptcha]
public virtual async Task<IActionResult> ContactVendorSend(ContactVendorModel model, bool captchaValid, [FromServices] IVendorService vendorService)
{
if (!_vendorSettings.AllowCustomersToContactVendors)
return RedirectToRoute("HomePage");
var vendor = await vendorService.GetVendorById(model.VendorId);
if (vendor == null || !vendor.Active || vendor.Deleted)
return RedirectToRoute("HomePage");
//validate CAPTCHA
if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
{
ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
}
model.VendorName = vendor.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id);
if (ModelState.IsValid)
{
model = await _commonViewModelService.SendContactVendor(model, vendor);
return View(model);
}
model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage;
return View(model);
}
//sitemap page
public virtual async Task<IActionResult> Sitemap()
{
if (!_commonSettings.SitemapEnabled)
return RedirectToRoute("HomePage");
var model = await _commonViewModelService.PrepareSitemap();
return View(model);
}
//available even when a store is closed
[CheckAccessClosedStore(true)]
public virtual async Task<IActionResult> SitemapXml(int? id)
{
if (!_commonSettings.SitemapEnabled)
return RedirectToRoute("HomePage");
var siteMap = await _commonViewModelService.SitemapXml(id, this.Url);
return Content(siteMap, "text/xml");
}
public virtual async Task<IActionResult> SetStoreTheme(
[FromServices] IThemeContext themeContext, string themeName, string returnUrl = "")
{
await themeContext.SetWorkingTheme(themeName);
//home page
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");
return Redirect(returnUrl);
}
[HttpPost]
//available even when a store is closed
[CheckAccessClosedStore(true)]
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> EuCookieLawAccept([FromServices] StoreInformationSettings storeInformationSettings,
[FromServices] IGenericAttributeService genericAttributeService)
{
if (!storeInformationSettings.DisplayEuCookieLawWarning)
//disabled
return Json(new { stored = false });
//save setting
await genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.EuCookieLawAccepted, true, _storeContext.CurrentStore.Id);
return Json(new { stored = true });
}
//robots.txt file
//available even when a store is closed
[CheckAccessClosedStore(true)]
//available even when navigation is not allowed
[CheckAccessPublicStore(true)]
public virtual async Task<IActionResult> RobotsTextFile()
{
var sb = await _commonViewModelService.PrepareRobotsTextFile();
return Content(sb, "text/plain");
}
public virtual IActionResult GenericUrl()
{
//seems that no entity was found
return InvokeHttp404();
}
//store is closed
//available even when a store is closed
[CheckAccessClosedStore(true)]
public virtual IActionResult StoreClosed() => View();
[HttpPost]
public virtual async Task<IActionResult> ContactAttributeChange(IFormCollection form,
[FromServices] IContactAttributeParser contactAttributeParser)
{
var attributeXml = await _commonViewModelService.ParseContactAttributes(form);
var enabledAttributeIds = new List<string>();
var disabledAttributeIds = new List<string>();
var attributes = await _contactAttributeService.GetAllContactAttributes(_storeContext.CurrentStore.Id);
foreach (var attribute in attributes)
{
var conditionMet = await contactAttributeParser.IsConditionMet(attribute, attributeXml);
if (conditionMet.HasValue)
{
if (conditionMet.Value)
enabledAttributeIds.Add(attribute.Id);
else
disabledAttributeIds.Add(attribute.Id);
}
}
return Json(new
{
enabledattributeids = enabledAttributeIds.ToArray(),
disabledattributeids = disabledAttributeIds.ToArray()
});
}
[HttpPost]
public virtual async Task<IActionResult> UploadFileContactAttribute(string attributeId, [FromServices] IDownloadService downloadService)
{
var attribute = await _contactAttributeService.GetContactAttributeById(attributeId);
if (attribute == null || attribute.AttributeControlType != AttributeControlType.FileUpload)
{
return Json(new
{
success = false,
downloadGuid = Guid.Empty,
});
}
var httpPostedFile = Request.Form.Files.FirstOrDefault();
if (httpPostedFile == null)
{
return Json(new
{
success = false,
message = "No file uploaded",
downloadGuid = Guid.Empty,
});
}
var fileBinary = httpPostedFile.GetDownloadBits();
var qqFileNameParameter = "qqfilename";
var fileName = httpPostedFile.FileName;
if (String.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
fileName = Request.Form[qqFileNameParameter].ToString();
//remove path (passed in IE)
fileName = Path.GetFileName(fileName);
var contentType = httpPostedFile.ContentType;
var fileExtension = Path.GetExtension(fileName);
if (!String.IsNullOrEmpty(fileExtension))
fileExtension = fileExtension.ToLowerInvariant();
if (attribute.ValidationFileMaximumSize.HasValue)
{
//compare in bytes
var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024;
if (fileBinary.Length > maxFileSizeBytes)
{
//when returning JSON the mime-type must be set to text/plain
//otherwise some browsers will pop-up a "Save As" dialog.
return Json(new
{
success = false,
message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), attribute.ValidationFileMaximumSize.Value),
downloadGuid = Guid.Empty,
});
}
}
var download = new Download {
DownloadGuid = Guid.NewGuid(),
UseDownloadUrl = false,
DownloadUrl = "",
DownloadBinary = fileBinary,
ContentType = contentType,
//we store filename without extension for downloads
Filename = Path.GetFileNameWithoutExtension(fileName),
Extension = fileExtension,
IsNew = true
};
await downloadService.InsertDownload(download);
//when returning JSON the mime-type must be set to text/plain
//otherwise some browsers will pop-up a "Save As" dialog.
return Json(new
{
success = true,
message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
downloadGuid = download.DownloadGuid,
});
}
//Get banner for customer
[HttpGet]
public virtual async Task<IActionResult> GetActivePopup()
{
var result = await _popupService.GetActivePopupByCustomerId(_workContext.CurrentCustomer.Id);
if (result != null)
{
return Json
(
new { Id = result.Id, Body = result.Body, PopupTypeId = result.PopupTypeId }
);
}
else
return Json
(
new { empty = "" }
);
}
[HttpPost]
public virtual async Task<IActionResult> RemovePopup(string Id)
{
await _popupService.MovepopupToArchive(Id, _workContext.CurrentCustomer.Id);
return Json("");
}
[HttpGet]
public virtual async Task<IActionResult> CustomerActionEventUrl(string curl, string purl)
{
await _customerActionEventService.Url(_workContext.CurrentCustomer, curl, purl);
return Json
(
new { empty = "" }
);
}
[HttpPost, ActionName("PopupInteractiveForm")]
public virtual async Task<IActionResult> PopupInteractiveForm(IFormCollection formCollection,
[FromServices] IInteractiveFormService interactiveFormService, [FromServices] IQueuedEmailService queuedEmailService,
[FromServices] IEmailAccountService emailAccountService)
{
var formid = formCollection["Id"];
var form = await interactiveFormService.GetFormById(formid);
if (form == null)
return Content("");
string enquiry = "";
foreach (var item in form.FormAttributes)
{
enquiry += string.Format("{0}: {1} <br />", item.Name, formCollection[item.SystemName]);
if (!string.IsNullOrEmpty(item.RegexValidation))
{
var valuesStr = formCollection[item.SystemName];
Regex regex = new Regex(item.RegexValidation);
Match match = regex.Match(valuesStr);
if (!match.Success)
{
ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.Regex"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
}
}
if (item.IsRequired)
{
var valuesStr = formCollection[item.SystemName];
if (string.IsNullOrEmpty(valuesStr))
ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.IsRequired"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
}
if (item.ValidationMinLength.HasValue)
{
if (item.AttributeControlType == FormControlType.TextBox ||
item.AttributeControlType == FormControlType.MultilineTextbox)
{
var valuesStr = formCollection[item.SystemName].ToString();
int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
if (item.ValidationMinLength.Value > enteredTextLength)
{
ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMinimumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMinLength.Value));
}
}
}
if (item.ValidationMaxLength.HasValue)
{
if (item.AttributeControlType == FormControlType.TextBox ||
item.AttributeControlType == FormControlType.MultilineTextbox)
{
var valuesStr = formCollection[item.SystemName].ToString();
int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
if (item.ValidationMaxLength.Value < enteredTextLength)
{
ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMaximumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMaxLength.Value));
}
}
}
}
if (ModelState.Keys.Count() == 0)
{
var emailAccount = await emailAccountService.GetEmailAccountById(form.EmailAccountId);
if (emailAccount == null)
emailAccount = (await emailAccountService.GetAllEmailAccounts()).FirstOrDefault();
if (emailAccount == null)
throw new Exception("No email account could be loaded");
string from;
string fromName;
string subject = string.Format(_localizationService.GetResource("PopupInteractiveForm.EmailForm"), form.Name);
from = emailAccount.Email;
fromName = emailAccount.DisplayName;
await queuedEmailService.InsertQueuedEmail(new QueuedEmail {
From = from,
FromName = fromName,
To = emailAccount.Email,
ToName = emailAccount.DisplayName,
Priority = QueuedEmailPriority.High,
Subject = subject,
Body = enquiry,
CreatedOnUtc = DateTime.UtcNow,
EmailAccountId = emailAccount.Id
});
//activity log
await _customerActivityService.InsertActivity("PublicStore.InteractiveForm", form.Id, string.Format(_localizationService.GetResource("ActivityLog.PublicStore.InteractiveForm"), form.Name));
}
return Json(new
{
success = ModelState.Keys.Count() == 0,
errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
.Select(m => m.ErrorMessage).ToArray()
});
}
#endregion
}
}