325 lines
10 KiB
C#
325 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Mail;
|
|
using System.Security.Cryptography;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Channels;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Xml;
|
|
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.ServiceContracts;
|
|
using BeWo.Data;
|
|
|
|
using BS.Shared;
|
|
using BS.Shared.Extensions;
|
|
|
|
namespace BeWo.Service.Core
|
|
{
|
|
public class Utils
|
|
{
|
|
private static readonly Regex indexRegex = new Regex("(?:Index=\"){1}([0-9]+)\"{1}");
|
|
private static readonly Regex idRegex = new Regex("(?:Id=\")(([a-z0-9]{8})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{12}))(?=\"{1})");
|
|
|
|
public static FaultException<BeWoFault> CreateBeWoFaultException(Exception ex)
|
|
{
|
|
try
|
|
{
|
|
SendErrorMail("MYSQL EXCEPTION - AUTOMATED MAIL NOTIFICATION", ex.ToString());
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
return new FaultException<BeWoFault>(new BeWoFault(ex), ex.ToString());
|
|
}
|
|
|
|
public static string GetClientIP()
|
|
{
|
|
if (OperationContext.Current != null)
|
|
{
|
|
MessageProperties properties = OperationContext.Current.IncomingMessageProperties;
|
|
var endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
|
|
return endpoint.Address;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static string GetMD5(string val)
|
|
{
|
|
var lMD5 = new MD5CryptoServiceProvider();
|
|
string lHash = BitConverter.ToString(lMD5.ComputeHash(Encoding.UTF8.GetBytes(val)));
|
|
return lHash;
|
|
}
|
|
|
|
public static bool IsValidEmail(string email)
|
|
{
|
|
//return Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
|
|
|
|
return Regex.IsMatch(email,
|
|
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
|
|
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
|
|
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
|
|
}
|
|
|
|
public static bool SendErrorMail(string subject, string body)
|
|
{
|
|
string sendMailSetting = ConfigurationManager.AppSettings.Get("SendMailOnError");
|
|
bool sendMail = false;
|
|
if (bool.TryParse(sendMailSetting, out sendMail))
|
|
{
|
|
if (sendMail)
|
|
return SendMail(subject, body, ConfigurationManager.AppSettings.Get("Recipient"));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool SendMail(string subject, string body, string toAddress)
|
|
{
|
|
return SendMail(null, null, subject, body, toAddress);
|
|
}
|
|
|
|
public static bool SendMail(string senderName, string senderEMail, string subject, string body, string toAddress)
|
|
{
|
|
var server = ConfigurationManager.AppSettings.Get("SmptServer");
|
|
var user = ConfigurationManager.AppSettings.Get("SmtpUser");
|
|
var pass = ConfigurationManager.AppSettings.Get("SmtpPassword");
|
|
String to = toAddress;
|
|
if (String.IsNullOrEmpty(to))
|
|
to = ConfigurationManager.AppSettings.Get("SupportEMail");
|
|
if (String.IsNullOrEmpty(to))
|
|
to = "support@bewoplaner.de";
|
|
|
|
if (senderEMail == null)
|
|
senderEMail = "bewoapp@bewoplaner.de";
|
|
|
|
bool sendAsync = true;
|
|
if (to.Equals("support@bewoplaner.de"))
|
|
{
|
|
sendAsync = false;
|
|
}
|
|
var body2 = "Kunde: " + MultitenancyOperationContextExt.Current.Tenant;
|
|
//body2 += "\r\nIP: " + GetClientIP();
|
|
body += "\n\n" + body2;
|
|
//}
|
|
|
|
|
|
//if (to.Equals())
|
|
|
|
return SendMail(senderEMail, senderName, to, subject, body, server, user, pass, sendAsync);
|
|
|
|
}
|
|
|
|
public static bool SendMail(string fromEMail, string fromName, string to, string subject, string body, string smtpServer, string smtpUser, string smtpPassword, bool sendAsync)
|
|
{
|
|
try
|
|
{
|
|
if (!IsValidEmail(to) || !IsValidEmail(fromEMail))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
String from = fromEMail;
|
|
|
|
if (to.Equals("support@bewoplaner.de"))
|
|
{
|
|
var supportSenderMail = ConfigurationManager.AppSettings.Get("SupportSenderEMail");
|
|
if (String.IsNullOrEmpty(supportSenderMail))
|
|
supportSenderMail = "noreply@bewoplaner.de";
|
|
|
|
from = supportSenderMail;
|
|
|
|
}
|
|
|
|
|
|
MailMessage message = null;
|
|
if (String.IsNullOrEmpty(fromName))
|
|
message = new MailMessage(from, to, subject, body);
|
|
else
|
|
{
|
|
MailAddress fromMailAddress = new MailAddress(from);
|
|
MailAddress toMailAddress = new MailAddress(to);
|
|
|
|
message = new MailMessage(fromMailAddress, toMailAddress);
|
|
message.Subject = subject;
|
|
message.Body = body;
|
|
}
|
|
if (fromEMail != from)
|
|
{
|
|
message.ReplyToList.Add(new MailAddress(fromEMail, fromName));
|
|
}
|
|
|
|
|
|
message.BodyEncoding = Encoding.UTF8;
|
|
message.IsBodyHtml = false;
|
|
|
|
var mailClient = new SmtpClient(smtpServer, 587) { Credentials = new NetworkCredential(smtpUser, smtpPassword) };
|
|
mailClient.EnableSsl = true;
|
|
|
|
if (sendAsync)
|
|
{
|
|
mailClient.SendAsync(message, null);
|
|
}
|
|
else
|
|
{
|
|
mailClient.Send(message);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string[][] T(List<string> pSearchParameter, int pSearchFieldCount)
|
|
{
|
|
//List<string> lDifferentParameter;
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string GetRecurringAppointmentsId(SchedulerAppointment appointment)
|
|
{
|
|
var erg = string.Empty;
|
|
|
|
if (appointment.RecurrenceInfo == null)
|
|
return erg;
|
|
|
|
using (var reader = XmlReader.Create(new StringReader(appointment.RecurrenceInfo)))
|
|
{
|
|
reader.ReadToFollowing("RecurrenceInfo");
|
|
reader.MoveToAttribute("Id");
|
|
erg = reader.Value;
|
|
}
|
|
|
|
return erg;
|
|
}
|
|
|
|
public static DateTime[] GetRecurrenceInterval(SchedulerAppointment appointment)
|
|
{
|
|
var erg = new DateTime[2];
|
|
|
|
using (var reader = XmlReader.Create(new StringReader(appointment.RecurrenceInfo)))
|
|
{
|
|
reader.ReadToFollowing("RecurrenceInfo");
|
|
|
|
reader.MoveToAttribute("Start");
|
|
DateTime outValue;
|
|
var parsingSuccessful = DateTime.TryParseExact(reader.Value, @"MM\/dd\/yyyy HH:mm:ss", null, DateTimeStyles.None, out outValue);
|
|
erg[0] = outValue;
|
|
reader.MoveToAttribute("End");
|
|
parsingSuccessful = DateTime.TryParseExact(reader.Value, @"MM\/dd\/yyyy HH:mm:ss", null, DateTimeStyles.None, out outValue);
|
|
erg[1] = outValue;
|
|
}
|
|
|
|
return erg;
|
|
}
|
|
|
|
|
|
public static bool UserHasRight(ApplicationUser user, UserRightType right)
|
|
{
|
|
return GetGrantedRights(user).Contains(right);
|
|
|
|
}
|
|
|
|
public static List<UserRightType> GetGrantedRights(ApplicationUser user)
|
|
{
|
|
var rights = new List<UserRightType>();
|
|
foreach (var ug in user.UserGroups)
|
|
{
|
|
foreach (var rr in ug.Rights)
|
|
{
|
|
rights.Add(rr.RightType);
|
|
}
|
|
}
|
|
|
|
return rights;
|
|
}
|
|
|
|
public static string GetRecurrenceIdFromRecurrenceInfo(string pRecurrenceInfo)
|
|
{
|
|
var regex = new Regex("(Id=\\\"[a-z0-9-]+\\\")");
|
|
|
|
var match = regex.Match(pRecurrenceInfo);
|
|
if(match.Success)
|
|
{
|
|
var value = match.Value;
|
|
var actualId = value.Split("\"");
|
|
if(actualId.Count > 1)
|
|
{
|
|
return actualId[1];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string GetRecurrenceIndexFromRecurrenceInfo(string pRecurrenceInfo)
|
|
{
|
|
if(!pRecurrenceInfo.Contains("Index"))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var regex = new Regex("(Index=\\\"\\d+\\\")");
|
|
|
|
var match = regex.Match(pRecurrenceInfo);
|
|
if(match.Success)
|
|
{
|
|
var value = match.Value;
|
|
var actualIndex = value.Split("\"");
|
|
|
|
if(actualIndex.Count > 1)
|
|
{
|
|
return actualIndex[1];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static List<string> ExtractRecurrenceIdFromRecurrenceString(List<SchedulerAppointment> appointments)
|
|
{
|
|
var result = new List<string>();
|
|
|
|
foreach(var recurrenceInfo in appointments.Where(w => w.RecurrenceInfo != null).Select(s => s.RecurrenceInfo))
|
|
{
|
|
var id = GetRecurrenceIdFromRecurrenceInfo(recurrenceInfo);
|
|
|
|
if(id != null)
|
|
{
|
|
result.AddIfNotIn(id);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static string ExtractIdFromRecurrenceInfo(string pRecurrenceInfo)
|
|
{
|
|
return pRecurrenceInfo != null ? idRegex.Match(pRecurrenceInfo).Groups[1].Value : null;
|
|
}
|
|
|
|
public static int ExtractIndexFromRecurrenceInfo(string pRecurrenceInfo)
|
|
{
|
|
if (pRecurrenceInfo != null)
|
|
{
|
|
var indexString = indexRegex.Match(pRecurrenceInfo).Groups[1].Value;
|
|
|
|
var index = !string.IsNullOrEmpty(indexString) ? int.Parse(indexString) : 0;
|
|
|
|
return index;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
} |