@@ -27,22 +27,22 @@ public class Updater : WebService {
27
27
private string _versionsTxt = _upgradeReleases + "versions.txt";
28
28
private bool _test = false; // when set to "true" will run in test mode without actually updating site
29
29
private int _tries;
30
-
30
+
31
31
public Updater()
32
32
{
33
- _root = System.Web.Hosting. HostingEnvironment.MapPath("~/");
33
+ _root = HostingEnvironment.MapPath("~/");
34
34
if (_root.EndsWith("\\")) _root = _root.Substring(0, _root.Length - 1);
35
35
36
36
_newZip = _root + "\\setup\\upgrade\\backup\\new.zip";
37
37
_oldZip = _root + "\\setup\\upgrade\\backup\\old.zip";
38
-
38
+
39
39
_ignoreDirs = new StringCollection();
40
40
_ignoreDirs.Add(_root + "\\Custom");
41
41
_ignoreDirs.Add(_root + "\\setup\\upgrade");
42
-
42
+
43
43
_installed = new List<InstalledLog >();
44
44
}
45
-
45
+
46
46
[WebMethod]
47
47
public string Check(string version)
48
48
{
@@ -52,7 +52,7 @@ public class Updater : WebService {
52
52
Stream stream = client.OpenRead(_versionsTxt);
53
53
StreamReader reader = new StreamReader(stream);
54
54
string line = "";
55
-
55
+
56
56
while (reader.Peek() >= 0)
57
57
{
58
58
line = reader.ReadLine();
@@ -61,7 +61,7 @@ public class Updater : WebService {
61
61
var iCurrent = int.Parse(version.Replace(".", ""));
62
62
var iFrom = int.Parse(line.Substring(0, line.IndexOf("|")).Replace(".", ""));
63
63
var iTo = int.Parse(line.Substring(line.LastIndexOf("|") + 1).Replace(".", ""));
64
-
64
+
65
65
if (iCurrent >= iFrom && iCurrent < iTo)
66
66
{
67
67
return line.Substring(line.LastIndexOf("|") + 1);
@@ -75,10 +75,10 @@ public class Updater : WebService {
75
75
return "";
76
76
}
77
77
}
78
-
78
+
79
79
[WebMethod]
80
80
public string Download(string version)
81
- {
81
+ {
82
82
if (_test)
83
83
{
84
84
System.Threading.Thread.Sleep(2000);
@@ -91,10 +91,10 @@ public class Updater : WebService {
91
91
92
92
if (File.Exists(_newZip))
93
93
File.Delete(_newZip);
94
-
95
- DateTime startTime = DateTime.UtcNow;
96
- WebRequest request = System.Net. WebRequest.Create(string.Format(_downloadUrl, version.Replace(".", "")));
97
- WebResponse response = request.GetResponse();
94
+
95
+ var startTime = DateTime.UtcNow;
96
+ var request = WebRequest.Create(string.Format(_downloadUrl, version.Replace(".", "")));
97
+ var response = request.GetResponse();
98
98
using (Stream responseStream = response.GetResponseStream())
99
99
{
100
100
using (Stream fileStream = File.OpenWrite(_newZip))
@@ -125,20 +125,20 @@ public class Updater : WebService {
125
125
126
126
[WebMethod]
127
127
public string Extract()
128
- {
128
+ {
129
129
if (_test)
130
130
{
131
131
System.Threading.Thread.Sleep(2000);
132
132
return "";
133
133
}
134
-
134
+
135
135
ZipFile zf = null;
136
136
string outFolder = _root + "\\setup\\upgrade\\backup\\be";
137
137
try
138
138
{
139
139
if (!Directory.Exists(outFolder))
140
140
Directory.CreateDirectory(outFolder);
141
-
141
+
142
142
FileStream fs = File.OpenRead(_newZip);
143
143
zf = new ZipFile(fs);
144
144
@@ -181,13 +181,13 @@ public class Updater : WebService {
181
181
182
182
[WebMethod]
183
183
public string Backup()
184
- {
184
+ {
185
185
if (_test)
186
186
{
187
187
System.Threading.Thread.Sleep(2000);
188
188
return "";
189
189
}
190
-
190
+
191
191
try
192
192
{
193
193
var backupDir = HostingEnvironment.MapPath("~/setup/upgrade/backup");
@@ -198,7 +198,7 @@ public class Updater : WebService {
198
198
199
199
if (File.Exists(_oldZip))
200
200
File.Delete(_oldZip);
201
-
201
+
202
202
var fsOut = File.Create(_oldZip);
203
203
var zipStream = new ZipOutputStream(fsOut);
204
204
@@ -209,7 +209,7 @@ public class Updater : WebService {
209
209
210
210
zipStream.IsStreamOwner = true;
211
211
zipStream.Close();
212
-
212
+
213
213
return "";
214
214
}
215
215
catch (Exception ex)
@@ -232,11 +232,11 @@ public class Updater : WebService {
232
232
DeleteDir("\\editors");
233
233
DeleteDir("\\Modules");
234
234
DeleteDir("\\pics");
235
-
235
+
236
236
ReplaceDir("\\Account");
237
- ReplaceDir("\\admin");
237
+ ReplaceDir("\\admin");
238
238
ReplaceDir("\\fonts");
239
-
239
+
240
240
ReplaceDir("\\setup\\Mono");
241
241
ReplaceDir("\\setup\\MySQL");
242
242
ReplaceDir("\\setup\\SQL_CE");
@@ -246,7 +246,7 @@ public class Updater : WebService {
246
246
ReplaceDir("\\App_GlobalResources");
247
247
ReplaceDir("\\Scripts");
248
248
ReplaceDir("\\Content");
249
-
249
+
250
250
return "";
251
251
}
252
252
catch (Exception ex)
@@ -261,7 +261,7 @@ public class Updater : WebService {
261
261
if (_test)
262
262
{
263
263
CopyWebConfig();
264
-
264
+
265
265
System.Threading.Thread.Sleep(2000);
266
266
return "";
267
267
}
@@ -283,15 +283,15 @@ public class Updater : WebService {
283
283
ReplaceLabelsFile();
284
284
285
285
CopyWebConfig();
286
-
286
+
287
287
ReplaceFile("Global.asax");
288
288
289
289
FixSH();
290
290
291
- Directory.Delete(_root + "\\setup\\upgrade\\backup\\be ", true);
291
+ Directory.Delete(_root + "\\setup\\upgrade\\backup", true);
292
292
293
293
Utils.Log(string.Format("Upgrade completed by {0}", Security.CurrentUser.Identity.Name));
294
-
294
+
295
295
return "";
296
296
}
297
297
catch (Exception ex)
@@ -329,59 +329,18 @@ public class Updater : WebService {
329
329
}
330
330
}
331
331
332
- /// <summary >
333
- /// Replace default validation and decription keys
334
- /// </summary >
335
- /// <param name =" content" >Content of the web.config</param >
336
332
void ReplaceXMLConfig(string content)
337
- {
333
+ {
338
334
string targetFile = _root + "\\Web.config";
339
335
DeleteFile(targetFile);
340
336
341
- content = ReplaceMachineKey (content);
337
+ content = UpdateWebConfig (content);
342
338
343
339
var writer = new StreamWriter(targetFile);
344
340
writer.Write(content);
345
341
writer.Close();
346
342
}
347
343
348
- string ReplaceMachineKey(string content)
349
- {
350
- var defaultValidationKey = "D9F7287EFDE8DF4CAFF79011D5308643D8F62AE10CDF30DAB640B7399BF6C57B0269D60A23FBCCC736FC2487ED695512BA95044DE4C58DC02C2BA0C4A266454C";
351
- var defaultDecryptionKey = "BDAAF7E00B69BA47B37EEAC328929A06A6647D4C89FED3A7D5C52B12B23680F4";
352
-
353
- string validationKey = CreateKey(System.Convert.ToInt32(64));
354
- string decryptionKey = CreateKey(System.Convert.ToInt32(24));
355
-
356
- content = content.Replace(defaultValidationKey, validationKey);
357
- content = content.Replace(defaultDecryptionKey, decryptionKey);
358
-
359
- // replace old editor path in web.config
360
- content = content.Replace("~/editors", "~/admin/editors");
361
-
362
- return content;
363
- }
364
-
365
- static String CreateKey(int numBytes)
366
- {
367
- var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
368
- byte[] buff = new byte[numBytes];
369
-
370
- rng.GetBytes(buff);
371
- return BytesToHexString(buff);
372
- }
373
-
374
- static String BytesToHexString(byte[] bytes)
375
- {
376
- var hexString = new System.Text.StringBuilder(64);
377
-
378
- for (int counter = 0; counter < bytes.Length; counter++)
379
- {
380
- hexString.Append(String.Format("{0:X2}", bytes[counter]));
381
- }
382
- return hexString.ToString();
383
- }
384
-
385
344
void ReplaceDbConfig(string content)
386
345
{
387
346
string oldCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
@@ -421,13 +380,69 @@ public class Updater : WebService {
421
380
reader.Close();
422
381
423
382
webContent = webContent.Replace(defCon, oldCon);
424
- webContent = ReplaceMachineKey (webContent);
383
+ webContent = UpdateWebConfig (webContent);
425
384
426
385
StreamWriter writer = new StreamWriter(targetFile);
427
386
writer.Write(webContent);
428
387
writer.Close();
429
388
}
430
389
390
+ string UpdateWebConfig(string content)
391
+ {
392
+ var defaultValidationKey = "D9F7287EFDE8DF4CAFF79011D5308643D8F62AE10CDF30DAB640B7399BF6C57B0269D60A23FBCCC736FC2487ED695512BA95044DE4C58DC02C2BA0C4A266454C";
393
+ var defaultDecryptionKey = "BDAAF7E00B69BA47B37EEAC328929A06A6647D4C89FED3A7D5C52B12B23680F4";
394
+
395
+ string validationKey = CreateKey(Convert.ToInt32(64));
396
+ string decryptionKey = CreateKey(Convert.ToInt32(24));
397
+
398
+ content = content.Replace(defaultValidationKey, validationKey);
399
+ content = content.Replace(defaultDecryptionKey, decryptionKey);
400
+
401
+ // replace old editor path in web.config
402
+ content = content.Replace("~/editors", "~/admin/editors");
403
+
404
+ // replace runtime tag in web.config
405
+ var newWebConfig = _root + "\\setup\\upgrade\\backup\\be\\Web.Config";
406
+ try
407
+ {
408
+ StreamReader reader = new StreamReader(newWebConfig);
409
+ string newContent = reader.ReadToEnd();
410
+ reader.Close();
411
+
412
+ var oldRuntimeTag = content.Substring(content.IndexOf("<runtime >"), (content.IndexOf ("</runtime >") - content.IndexOf("<runtime >") + 11));
413
+ var newRuntimeTag = newContent.Substring(newContent.IndexOf("<runtime >"), (newContent.IndexOf ("</runtime >") - newContent.IndexOf("<runtime >") + 11));
414
+
415
+ if(oldRuntimeTag.Length > 0 && newRuntimeTag.Length > 0)
416
+ {
417
+ content = content.Replace(oldRuntimeTag, newRuntimeTag);
418
+ }
419
+ }
420
+ catch (Exception)
421
+ {
422
+ }
423
+ return content;
424
+ }
425
+
426
+ static String CreateKey(int numBytes)
427
+ {
428
+ var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
429
+ byte[] buff = new byte[numBytes];
430
+
431
+ rng.GetBytes(buff);
432
+ return BytesToHexString(buff);
433
+ }
434
+
435
+ static String BytesToHexString(byte[] bytes)
436
+ {
437
+ var hexString = new System.Text.StringBuilder(64);
438
+
439
+ for (int counter = 0; counter < bytes.Length; counter++)
440
+ {
441
+ hexString.Append(String.Format("{0:X2}", bytes[counter]));
442
+ }
443
+ return hexString.ToString();
444
+ }
445
+
431
446
[WebMethod]
432
447
public string Rollback()
433
448
{
@@ -445,7 +460,7 @@ public class Updater : WebService {
445
460
}
446
461
else
447
462
{
448
- File.Copy(item.To, item.From, true);
463
+ File.Copy(item.To, item.From, true);
449
464
}
450
465
}
451
466
return "";
@@ -478,7 +493,7 @@ public class Updater : WebService {
478
493
_tries = 0;
479
494
throw new ApplicationException(string.Format("Failed to replace directory {0}; error: {1}", dir, ex.Message));
480
495
}
481
-
496
+
482
497
if (_tries < 4)
483
498
{
484
499
ReplaceDir(dir);
@@ -490,15 +505,15 @@ public class Updater : WebService {
490
505
{
491
506
if (Directory.Exists(_root + dir))
492
507
{
493
- Directory.Delete(_root + dir, true);
508
+ Directory.Delete(_root + dir, true);
494
509
}
495
510
}
496
-
511
+
497
512
void CopyDir(string dir)
498
513
{
499
514
var source = new DirectoryInfo(_root + "\\setup\\upgrade\\backup\\be\\" + dir);
500
515
var target = new DirectoryInfo(_root + "\\" + dir);
501
-
516
+
502
517
//Log(source.FullName, target.FullName, true);
503
518
CopyRecursive(source, target);
504
519
}
@@ -549,14 +564,14 @@ public class Updater : WebService {
549
564
ReplaceFile(dir + "\\" + fileName);
550
565
}
551
566
}
552
-
567
+
553
568
//---------------------------------------------------
554
569
555
570
void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
556
571
{
557
572
if (IgnoredDirectory(path))
558
573
return;
559
-
574
+
560
575
string[] files = Directory.GetFiles(path);
561
576
562
577
foreach (string filename in files)
@@ -651,7 +666,7 @@ public class Updater : WebService {
651
666
{
652
667
return _ignoreDirs.Contains(item) ? true : false;
653
668
}
654
-
669
+
655
670
//----------------------------------------------------
656
671
void FixSH()
657
672
{
@@ -705,7 +720,7 @@ public class Updater : WebService {
705
720
//void Log(string from, string to = "", bool directory = false, Operation action = Operation.Copy)
706
721
//{
707
722
// _installed.Add(new InstalledLog { IsDirectory = directory, Action = action, From = from, To = to });
708
-
723
+
709
724
// string s = action == Operation.Copy ? "UPGRADE: Copy " : "UPGRADE: Delete ";
710
725
// s = s + (directory ? "directory " : "file ");
711
726
0 commit comments