1
Using#region Using
2
3
using System;
4
using System.Net.Mail;
5
using System.Collections.Generic;
6
using System.Text;
7
using System.Text.RegularExpressions;
8
using System.Configuration;
9
using System.Globalization;
10
using System.Web;
11
using System.Threading;
12
13
#endregion
14
15
namespace LiveBlog.Core
16
...{
17
/**//// <summary>
18
/// Utilities for the entire solution to use.
19
/// </summary>
20
public static class Utils
21
...{
22
23
/**//// <summary>
24
/// Strips all illegal characters from the specified title.
25
/// </summary>
26
public static string RemoveIllegalCharacters(string text)
27
...{
28
if (string.IsNullOrEmpty(text))
29
return text;
30
31
text = text.Replace(":", string.Empty);
32
text = text.Replace("/", string.Empty);
33
text = text.Replace("?", string.Empty);
34
text = text.Replace("#", string.Empty);
35
text = text.Replace("[", string.Empty);
36
text = text.Replace("]", string.Empty);
37
text = text.Replace("@", string.Empty);
38
text = text.Replace(".", string.Empty);
39
text = text.Replace("\"", string.Empty);
40
text = text.Replace("&", string.Empty);
41
text = text.Replace("'", string.Empty);
42
text = text.Replace(" ", "-");
43
text = RemoveDiacritics(text);
44
45
return HttpUtility.UrlEncode(text).Replace("%", string.Empty);
46
}
47
48
private static String RemoveDiacritics(string text)
49
...{
50
String normalized = text.Normalize(NormalizationForm.FormD);
51
StringBuilder sb = new StringBuilder();
52
53
for (int i = 0; i < normalized.Length; i++)
54
...{
55
Char c = normalized[i];
56
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
57
sb.Append(c);
58
}
59
60
return sb.ToString();
61
}
62
63
URL handling#region URL handling
64
65
/**//// <summary>
66
/// Gets the relative URL of the blog feed. If a Feedburner username
67
/// is entered in the admin settings page, it will return the
68
/// absolute Feedburner URL to the feed.
69
/// </summary>
70
public static string FeedUrl
71
...{
72
get
73
...{
74
if (!string.IsNullOrEmpty(BlogSettings.Instance.AlternateFeedUrl))
75
return BlogSettings.Instance.AlternateFeedUrl;
76
else
77
return AbsoluteWebRoot + "feed"+BlogSettings.Instance.FileExtension;
78
}
79
}
80
81
/**//// <summary>
82
/// Gets the relative root of the website.
83
/// </summary>
84
/// <value>A string that ends with a '/'.</value>
85
public static string RelativeWebRoot
86
...{
87
get ...{ return VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["LiveBlog.VirtualPath"]); }
88
}
89
90
private static Uri _AbsoluteWebRoot;
91
92
/**//// <summary>
93
/// Gets the absolute root of the website.
94
/// </summary>
95
/// <value>A string that ends with a '/'.</value>
96
public static Uri AbsoluteWebRoot
97
...{
98
get
99
...{
100
if (_AbsoluteWebRoot == null)
101
...{
102
HttpContext context = HttpContext.Current;
103
if (context == null)
104
throw new System.Net.WebException("The current HttpContext is null");
105
106
_AbsoluteWebRoot = new Uri(context.Request.Url.Scheme + "://" + context.Request.Url.Authority + RelativeWebRoot);
107
}
108
return _AbsoluteWebRoot;
109
}
110
}
111
112
/**//// <summary>
113
/// Converts a relative URL to an absolute one.
114
/// </summary>
115
public static Uri ConvertToAbsolute(Uri relativeUri)
116
...{
117
return ConvertToAbsolute(relativeUri.ToString()); ;
118
}
119
120
/**//// <summary>
121
/// Converts a relative URL to an absolute one.
122
/// </summary>
123
public static Uri ConvertToAbsolute(string relativeUri)
124
...{
125
if (String.IsNullOrEmpty(relativeUri))
126
throw new ArgumentNullException("relativeUri");
127
128
string absolute = AbsoluteWebRoot.ToString();
129
int index = absolute.LastIndexOf(RelativeWebRoot.ToString());
130
131
return new Uri(absolute.Substring(0, index) + relativeUri);
132
}
133
134
#endregion
135
136
Is mobile device#region Is mobile device
137
138
private static readonly Regex MOBILE_REGEX = new Regex(ConfigurationManager.AppSettings.Get("LiveBlog.MobileDevices"), RegexOptions.IgnoreCase | RegexOptions.Compiled);
139
140
/**//// <summary>
141
/// Gets a value indicating whether the client is a mobile device.
142
/// </summary>
143
/// <value><c>true</c> if this instance is mobile; otherwise, <c>false</c>.</value>
144
public static bool IsMobile
145
...{
146
get
147
...{
148
HttpContext context = HttpContext.Current;
149
if (context != null)
150
...{
151
HttpRequest request = context.Request;
152
if (request.Browser.IsMobileDevice)
153
return true;
154
155
if (!string.IsNullOrEmpty(request.UserAgent) && MOBILE_REGEX.IsMatch(request.UserAgent))
156
return true;
157
}
158
159
return false;
160
}
161
}
162
163
#endregion
164
165
Is Mono/Linux#region Is Mono/Linux
166
167
private static int mono = 0;
168
/**//// <summary>
169
/// Gets a value indicating whether we're running under Mono.
170
/// </summary>
171
/// <value><c>true</c> if Mono; otherwise, <c>false</c>.</value>
172
public static bool IsMono
173
...{
174
get
175
...{
176
if (mono == 0)
177
...{
178
if (Type.GetType("Mono.Runtime") != null)
179
mono = 1;
180
else
181
mono = 2;
182
}
183
184
return mono == 1;
185
}
186
}
187
188
/**//// <summary>
189
/// Gets a value indicating whether we're running under Linux or a Unix variant.
190
/// </summary>
191
/// <value><c>true</c> if Linux/Unix; otherwise, <c>false</c>.</value>
192
public static bool IsLinux
193
...{
194
get
195
...{
196
int p = (int)Environment.OSVersion.Platform;
197
return ((p == 4) || (p == 128));
198
}
199
}
200
201
#endregion
202
203
Send e-mail#region Send e-mail
204
205
/**//// <summary>
206
/// Sends a MailMessage object using the SMTP settings.
207
/// </summary>
208
public static void SendMailMessage(MailMessage message)
209
...{
210
if (message == null)
211
throw new ArgumentNullException("message");
212
213
try
214
...{
215
message.IsBodyHtml = true;
216
message.BodyEncoding = Encoding.UTF8;
217
SmtpClient smtp = new SmtpClient(BlogSettings.Instance.SmtpServer);
218
smtp.Credentials = new System.Net.NetworkCredential(BlogSettings.Instance.SmtpUserName, BlogSettings.Instance.SmtpPassword);
219
smtp.Port = BlogSettings.Instance.SmtpServerPort;
220
smtp.EnableSsl = BlogSettings.Instance.EnableSsl;
221
smtp.Send(message);
222
OnEmailSent(message);
223
}
224
catch (SmtpException)
225
...{
226
OnEmailFailed(message);
227
}
228
finally
229
...{
230
// Remove the pointer to the message object so the GC can close the thread.
231
message.Dispose();
232
message = null;
233
}
234
}
235
236
/**//// <summary>
237
/// Sends the mail message asynchronously in another thread.
238
/// </summary>
239
/// <param name="message">The message to send.</param>
240
public static void SendMailMessageAsync(MailMessage message)
241
...{
242
//ThreadStart threadStart = delegate { Utils.SendMailMessage(message); };
243
//Thread thread = new Thread(threadStart);
244
//thread.IsBackground = true;
245
//thread.Start();
246
ThreadPool.QueueUserWorkItem(delegate ...{ Utils.SendMailMessage(message); });
247
}
248
249
/**//// <summary>
250
/// Occurs after an e-mail has been sent. The sender is the MailMessage object.
251
/// </summary>
252
public static event EventHandler<EventArgs> EmailSent;
253
private static void OnEmailSent(MailMessage message)
254
...{
255
if (EmailSent != null)
256
...{
257
EmailSent(message, new EventArgs());
258
}
259
}
260
261
/**//// <summary>
262
/// Occurs after an e-mail has been sent. The sender is the MailMessage object.
263
/// </summary>
264
public static event EventHandler<EventArgs> EmailFailed;
265
private static void OnEmailFailed(MailMessage message)
266
...{
267
if (EmailFailed != null)
268
...{
269
EmailFailed(message, new EventArgs());
270
}
271
}