@@ -27,22 +27,22 @@ public class Updater : WebService {
2727 private string _versionsTxt = _upgradeReleases + "versions.txt";
2828 private bool _test = false; // when set to "true" will run in test mode without actually updating site
2929 private int _tries;
30-
30+
3131 public Updater()
3232 {
33- _root = System.Web.Hosting. HostingEnvironment.MapPath("~/");
33+ _root = HostingEnvironment.MapPath("~/");
3434 if (_root.EndsWith("\\")) _root = _root.Substring(0, _root.Length - 1);
3535
3636 _newZip = _root + "\\setup\\upgrade\\backup\\new.zip";
3737 _oldZip = _root + "\\setup\\upgrade\\backup\\old.zip";
38-
38+
3939 _ignoreDirs = new StringCollection();
4040 _ignoreDirs.Add(_root + "\\Custom");
4141 _ignoreDirs.Add(_root + "\\setup\\upgrade");
42-
42+
4343 _installed = new List<InstalledLog >();
4444 }
45-
45+
4646 [WebMethod]
4747 public string Check(string version)
4848 {
@@ -52,7 +52,7 @@ public class Updater : WebService {
5252 Stream stream = client.OpenRead(_versionsTxt);
5353 StreamReader reader = new StreamReader(stream);
5454 string line = "";
55-
55+
5656 while (reader.Peek() >= 0)
5757 {
5858 line = reader.ReadLine();
@@ -61,7 +61,7 @@ public class Updater : WebService {
6161 var iCurrent = int.Parse(version.Replace(".", ""));
6262 var iFrom = int.Parse(line.Substring(0, line.IndexOf("|")).Replace(".", ""));
6363 var iTo = int.Parse(line.Substring(line.LastIndexOf("|") + 1).Replace(".", ""));
64-
64+
6565 if (iCurrent >= iFrom && iCurrent < iTo)
6666 {
6767 return line.Substring(line.LastIndexOf("|") + 1);
@@ -75,10 +75,10 @@ public class Updater : WebService {
7575 return "";
7676 }
7777 }
78-
78+
7979 [WebMethod]
8080 public string Download(string version)
81- {
81+ {
8282 if (_test)
8383 {
8484 System.Threading.Thread.Sleep(2000);
@@ -91,10 +91,10 @@ public class Updater : WebService {
9191
9292 if (File.Exists(_newZip))
9393 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();
9898 using (Stream responseStream = response.GetResponseStream())
9999 {
100100 using (Stream fileStream = File.OpenWrite(_newZip))
@@ -125,20 +125,20 @@ public class Updater : WebService {
125125
126126 [WebMethod]
127127 public string Extract()
128- {
128+ {
129129 if (_test)
130130 {
131131 System.Threading.Thread.Sleep(2000);
132132 return "";
133133 }
134-
134+
135135 ZipFile zf = null;
136136 string outFolder = _root + "\\setup\\upgrade\\backup\\be";
137137 try
138138 {
139139 if (!Directory.Exists(outFolder))
140140 Directory.CreateDirectory(outFolder);
141-
141+
142142 FileStream fs = File.OpenRead(_newZip);
143143 zf = new ZipFile(fs);
144144
@@ -181,13 +181,13 @@ public class Updater : WebService {
181181
182182 [WebMethod]
183183 public string Backup()
184- {
184+ {
185185 if (_test)
186186 {
187187 System.Threading.Thread.Sleep(2000);
188188 return "";
189189 }
190-
190+
191191 try
192192 {
193193 var backupDir = HostingEnvironment.MapPath("~/setup/upgrade/backup");
@@ -198,7 +198,7 @@ public class Updater : WebService {
198198
199199 if (File.Exists(_oldZip))
200200 File.Delete(_oldZip);
201-
201+
202202 var fsOut = File.Create(_oldZip);
203203 var zipStream = new ZipOutputStream(fsOut);
204204
@@ -209,7 +209,7 @@ public class Updater : WebService {
209209
210210 zipStream.IsStreamOwner = true;
211211 zipStream.Close();
212-
212+
213213 return "";
214214 }
215215 catch (Exception ex)
@@ -232,11 +232,11 @@ public class Updater : WebService {
232232 DeleteDir("\\editors");
233233 DeleteDir("\\Modules");
234234 DeleteDir("\\pics");
235-
235+
236236 ReplaceDir("\\Account");
237- ReplaceDir("\\admin");
237+ ReplaceDir("\\admin");
238238 ReplaceDir("\\fonts");
239-
239+
240240 ReplaceDir("\\setup\\Mono");
241241 ReplaceDir("\\setup\\MySQL");
242242 ReplaceDir("\\setup\\SQL_CE");
@@ -246,7 +246,7 @@ public class Updater : WebService {
246246 ReplaceDir("\\App_GlobalResources");
247247 ReplaceDir("\\Scripts");
248248 ReplaceDir("\\Content");
249-
249+
250250 return "";
251251 }
252252 catch (Exception ex)
@@ -261,7 +261,7 @@ public class Updater : WebService {
261261 if (_test)
262262 {
263263 CopyWebConfig();
264-
264+
265265 System.Threading.Thread.Sleep(2000);
266266 return "";
267267 }
@@ -283,15 +283,15 @@ public class Updater : WebService {
283283 ReplaceLabelsFile();
284284
285285 CopyWebConfig();
286-
286+
287287 ReplaceFile("Global.asax");
288288
289289 FixSH();
290290
291- Directory.Delete(_root + "\\setup\\upgrade\\backup\\be ", true);
291+ Directory.Delete(_root + "\\setup\\upgrade\\backup", true);
292292
293293 Utils.Log(string.Format("Upgrade completed by {0}", Security.CurrentUser.Identity.Name));
294-
294+
295295 return "";
296296 }
297297 catch (Exception ex)
@@ -329,59 +329,18 @@ public class Updater : WebService {
329329 }
330330 }
331331
332- /// <summary >
333- /// Replace default validation and decription keys
334- /// </summary >
335- /// <param name =" content" >Content of the web.config</param >
336332 void ReplaceXMLConfig(string content)
337- {
333+ {
338334 string targetFile = _root + "\\Web.config";
339335 DeleteFile(targetFile);
340336
341- content = ReplaceMachineKey (content);
337+ content = UpdateWebConfig (content);
342338
343339 var writer = new StreamWriter(targetFile);
344340 writer.Write(content);
345341 writer.Close();
346342 }
347343
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-
385344 void ReplaceDbConfig(string content)
386345 {
387346 string oldCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
@@ -421,13 +380,69 @@ public class Updater : WebService {
421380 reader.Close();
422381
423382 webContent = webContent.Replace(defCon, oldCon);
424- webContent = ReplaceMachineKey (webContent);
383+ webContent = UpdateWebConfig (webContent);
425384
426385 StreamWriter writer = new StreamWriter(targetFile);
427386 writer.Write(webContent);
428387 writer.Close();
429388 }
430389
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+
431446 [WebMethod]
432447 public string Rollback()
433448 {
@@ -445,7 +460,7 @@ public class Updater : WebService {
445460 }
446461 else
447462 {
448- File.Copy(item.To, item.From, true);
463+ File.Copy(item.To, item.From, true);
449464 }
450465 }
451466 return "";
@@ -478,7 +493,7 @@ public class Updater : WebService {
478493 _tries = 0;
479494 throw new ApplicationException(string.Format("Failed to replace directory {0}; error: {1}", dir, ex.Message));
480495 }
481-
496+
482497 if (_tries < 4)
483498 {
484499 ReplaceDir(dir);
@@ -490,15 +505,15 @@ public class Updater : WebService {
490505 {
491506 if (Directory.Exists(_root + dir))
492507 {
493- Directory.Delete(_root + dir, true);
508+ Directory.Delete(_root + dir, true);
494509 }
495510 }
496-
511+
497512 void CopyDir(string dir)
498513 {
499514 var source = new DirectoryInfo(_root + "\\setup\\upgrade\\backup\\be\\" + dir);
500515 var target = new DirectoryInfo(_root + "\\" + dir);
501-
516+
502517 //Log(source.FullName, target.FullName, true);
503518 CopyRecursive(source, target);
504519 }
@@ -549,14 +564,14 @@ public class Updater : WebService {
549564 ReplaceFile(dir + "\\" + fileName);
550565 }
551566 }
552-
567+
553568 //---------------------------------------------------
554569
555570 void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
556571 {
557572 if (IgnoredDirectory(path))
558573 return;
559-
574+
560575 string[] files = Directory.GetFiles(path);
561576
562577 foreach (string filename in files)
@@ -651,7 +666,7 @@ public class Updater : WebService {
651666 {
652667 return _ignoreDirs.Contains(item) ? true : false;
653668 }
654-
669+
655670 //----------------------------------------------------
656671 void FixSH()
657672 {
@@ -705,7 +720,7 @@ public class Updater : WebService {
705720 //void Log(string from, string to = "", bool directory = false, Operation action = Operation.Copy)
706721 //{
707722 // _installed.Add(new InstalledLog { IsDirectory = directory, Action = action, From = from, To = to });
708-
723+
709724 // string s = action == Operation.Copy ? "UPGRADE: Copy " : "UPGRADE: Delete ";
710725 // s = s + (directory ? "directory " : "file ");
711726
0 commit comments