From 8fc2ed59d66a47eec331ae13545c1f1f17c41f0d Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Tue, 9 Mar 2021 17:11:50 +0100 Subject: [PATCH 1/8] Add code to fix update warning --- DNN Platform/Library/Common/Globals.cs | 23 ++++ .../Services/Dto/GithubLatestReleaseDTO.cs | 22 ++++ .../Services/ServerSummaryController.cs | 107 ++++++------------ 3 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs diff --git a/DNN Platform/Library/Common/Globals.cs b/DNN Platform/Library/Common/Globals.cs index 09414f40fbc..ccac8bac40f 100644 --- a/DNN Platform/Library/Common/Globals.cs +++ b/DNN Platform/Library/Common/Globals.cs @@ -2060,6 +2060,29 @@ public static void SetFormFocus(Control control) } } + /// + /// Gets JSON string from url and deserializes this to type T. + /// + /// Object type to return. + /// Complete url to fetch JSON data from. + /// Object of type T + public static T GetJsonObject(string url) + { + var request = GetExternalRequest(url); + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + { + if (response.StatusCode == HttpStatusCode.OK) + { + var dataStream = response.GetResponseStream(); + var reader = new StreamReader(dataStream); + var responseFromServer = reader.ReadToEnd(); + return Newtonsoft.Json.JsonConvert.DeserializeObject(responseFromServer); + } + } + + return default(T); + } + /// /// Gets the external request. /// diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs new file mode 100644 index 00000000000..8b6086a5a1f --- /dev/null +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information + +namespace Dnn.PersonaBar.UI.Services.DTO +{ + using System.Collections.Generic; + using System.Runtime.Serialization; + + [DataContract] + public class GithubLatestReleaseDTO + { + [DataMember(Name = "name")] + public string Name { get; set; } + + [DataMember(Name = "tag_name")] + public string TagName { get; set; } + + [DataMember(Name = "html_url")] + public string Url { get; set; } + } +} diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs index d93ed627a7c..e8ada9729cb 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs @@ -9,7 +9,8 @@ namespace Dnn.PersonaBar.UI.Services using System.Net; using System.Net.Http; using System.Security.Cryptography; - using System.Text; + using System.Text; + using System.Text.RegularExpressions; using System.Web; using System.Web.Caching; using System.Web.Http; @@ -70,10 +71,9 @@ public HttpResponseMessage GetServerInfo() [HttpGet] public HttpResponseMessage GetUpdateLink() - { - UpdateType updateType; - var url = this.NeedUpdate(out updateType) ? Upgrade.UpgradeRedirect() : string.Empty; - + { + var url = this.UpdateUrl(); + var updateType = url == string.Empty ? UpdateType.None : UpdateType.Normal; return this.Request.CreateResponse(HttpStatusCode.OK, new { Url = url, Type = updateType }); } @@ -83,72 +83,37 @@ private bool GetVisibleSetting(string settingName) return !portalSettings.ContainsKey(settingName) || string.IsNullOrEmpty(portalSettings[settingName]) || portalSettings[settingName] == "true"; - } - - private bool NeedUpdate(out UpdateType updateType) - { - updateType = UpdateType.None; - - if (HttpContext.Current == null || !Host.CheckUpgrade || !this.UserInfo.IsSuperUser) - { - return false; - } - - var version = DotNetNukeContext.Current.Application.Version; - var request = HttpContext.Current.Request; - - var imageUrl = Upgrade.UpgradeIndicator(version, request.IsLocal, request.IsSecureConnection); - imageUrl = Globals.AddHTTP(imageUrl.TrimStart('/')); - - try - { - string hash; - const string cacheKey = "UpdateServiceUrlCacheKey"; - var cachedData = DataCache.GetCache(cacheKey) as string; - if (cachedData != null) - { - hash = cachedData; - } - else - { - var webRequest = WebRequest.CreateHttp(imageUrl); - webRequest.Timeout = Host.WebRequestTimeout; - webRequest.UserAgent = request.UserAgent; - webRequest.Referer = request.RawUrl; - - using (var stream = ((HttpWebResponse)webRequest.GetResponse()).GetResponseStream()) - { - if (stream == null) - { - return false; - } - - using (var sha256 = SHA256.Create()) - { - hash = - BitConverter.ToString(sha256.ComputeHash(stream)).Replace("-", string.Empty).ToLowerInvariant(); - DataCache.SetCache(cacheKey, hash, (DNNCacheDependency)null, - Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1), CacheItemPriority.Normal, null); - } - } - } - - switch (hash) - { - case NormalUpdateHash: - updateType = UpdateType.Normal; - return true; - case CriticalUpdateHash: - updateType = UpdateType.Critical; - return true; - default: - return false; - } - } - catch (Exception) - { - return false; - } + } + + private string UpdateUrl() + { + return CBO.GetCachedObject(null, this.RetrieveUpdateUrl); + } + + private string RetrieveUpdateUrl(CacheItemArgs args) + { + try + { + var latestRelease = Globals.GetJsonObject("https://api.github.com/repos/dnnsoftware/dnn.platform/releases/latest"); + if (latestRelease != null) + { + var m = Regex.Match(latestRelease.TagName, @"(\d+\.\d+\.\d+)$"); + if (m.Success) + { + var latestVersion = new Version(m.Groups[1].Value); + if (latestVersion.CompareTo(DotNetNukeContext.Current.Application.Version) < 0) + { + return latestRelease.Url; + } + } + } + } + catch (Exception ex) + { + DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); + } + + return string.Empty; } } } From 2a6aacaafd78887427b163ed66cb6a6c6f533eb6 Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Tue, 9 Mar 2021 17:20:32 +0100 Subject: [PATCH 2/8] Add to proj --- .../Library/Dnn.PersonaBar.UI/Dnn.PersonaBar.UI.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Dnn.PersonaBar.UI.csproj b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Dnn.PersonaBar.UI.csproj index 6a53dbddb3c..e38684143c1 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Dnn.PersonaBar.UI.csproj +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Dnn.PersonaBar.UI.csproj @@ -119,6 +119,7 @@ Code + From 9861c099730f7eddee021a7bca108f0cfc4a5716 Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Tue, 9 Mar 2021 17:37:35 +0100 Subject: [PATCH 3/8] Fix to comparison --- .../Dnn.PersonaBar.UI/Services/ServerSummaryController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs index e8ada9729cb..094de93d11f 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs @@ -87,7 +87,7 @@ private bool GetVisibleSetting(string settingName) private string UpdateUrl() { - return CBO.GetCachedObject(null, this.RetrieveUpdateUrl); + return CBO.GetCachedObject(new CacheItemArgs("DnnUpdateUrl"), this.RetrieveUpdateUrl); } private string RetrieveUpdateUrl(CacheItemArgs args) @@ -101,7 +101,7 @@ private string RetrieveUpdateUrl(CacheItemArgs args) if (m.Success) { var latestVersion = new Version(m.Groups[1].Value); - if (latestVersion.CompareTo(DotNetNukeContext.Current.Application.Version) < 0) + if (latestVersion.CompareTo(DotNetNukeContext.Current.Application.Version) > 0) { return latestRelease.Url; } From 53eb1b7bfb8f2c412bda162ddfc6fd35a9f1a626 Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Tue, 9 Mar 2021 19:16:23 +0100 Subject: [PATCH 4/8] Fixes --- .../Services/Dto/GithubLatestReleaseDTO.cs | 7 ++++- .../Services/ServerSummaryController.cs | 30 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs index 8b6086a5a1f..d4260a9afa5 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/Dto/GithubLatestReleaseDTO.cs @@ -4,7 +4,6 @@ namespace Dnn.PersonaBar.UI.Services.DTO { - using System.Collections.Generic; using System.Runtime.Serialization; [DataContract] @@ -18,5 +17,11 @@ public class GithubLatestReleaseDTO [DataMember(Name = "html_url")] public string Url { get; set; } + + [DataMember(Name = "draft")] + public bool Draft { get; set; } + + [DataMember(Name = "prerelease")] + public bool PreRelease { get; set; } } } diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs index 094de93d11f..d6e8bc6c996 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs @@ -4,7 +4,8 @@ namespace Dnn.PersonaBar.UI.Services { - using System; + using System; + using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; @@ -94,23 +95,34 @@ private string RetrieveUpdateUrl(CacheItemArgs args) { try { - var latestRelease = Globals.GetJsonObject("https://api.github.com/repos/dnnsoftware/dnn.platform/releases/latest"); - if (latestRelease != null) + if (HttpContext.Current == null || !Host.CheckUpgrade || !this.UserInfo.IsSuperUser) { - var m = Regex.Match(latestRelease.TagName, @"(\d+\.\d+\.\d+)$"); - if (m.Success) + return string.Empty; + } + + var latestReleases = Globals.GetJsonObject>("https://api.github.com/repos/dnnsoftware/dnn.platform/releases?per_page=5"); + if (latestReleases != null) + { + foreach (var release in latestReleases) { - var latestVersion = new Version(m.Groups[1].Value); - if (latestVersion.CompareTo(DotNetNukeContext.Current.Application.Version) > 0) + if (!release.Draft && !release.PreRelease) { - return latestRelease.Url; + var m = Regex.Match(release.TagName, @"(\d+\.\d+\.\d+)$"); + if (m.Success) + { + var latestVersion = new Version(m.Groups[1].Value); + if (latestVersion.CompareTo(DotNetNukeContext.Current.Application.Version) > 0) + { + return release.Url; + } + } } } } } catch (Exception ex) { - DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); + Exceptions.LogException(ex); } return string.Empty; From cfe04b533d9f4896ee1a60f3b2f5bed4a47bd707 Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Tue, 9 Mar 2021 23:17:22 +0100 Subject: [PATCH 5/8] Styling the result --- .../Services/ServerSummaryController.cs | 13 +++++++------ .../personaBar/App_LocalResources/PersonaBar.resx | 6 ++++++ .../Dnn.PersonaBar.UI/admin/personaBar/index.html | 4 ++-- .../admin/personaBar/scripts/main.js | 2 +- .../admin/personaBar/scripts/serversummary.js | 1 + 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs index d6e8bc6c996..bf1dfd3416a 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ServerSummaryController.cs @@ -58,7 +58,8 @@ public HttpResponseMessage GetServerInfo() FrameworkVersion = isHost ? Globals.NETFrameworkVersion.ToString(2) : string.Empty, ServerName = isHost ? Globals.ServerName : string.Empty, LicenseVisible = isHost && this.GetVisibleSetting("LicenseVisible"), - DocCenterVisible = this.GetVisibleSetting("DocCenterVisible"), + DocCenterVisible = this.GetVisibleSetting("DocCenterVisible"), + UpdateUrl = this.UpdateUrl(), }; return this.Request.CreateResponse(HttpStatusCode.OK, response); @@ -88,6 +89,11 @@ private bool GetVisibleSetting(string settingName) private string UpdateUrl() { + if (HttpContext.Current == null || !Host.CheckUpgrade || !this.UserInfo.IsSuperUser) + { + return string.Empty; + } + return CBO.GetCachedObject(new CacheItemArgs("DnnUpdateUrl"), this.RetrieveUpdateUrl); } @@ -95,11 +101,6 @@ private string RetrieveUpdateUrl(CacheItemArgs args) { try { - if (HttpContext.Current == null || !Host.CheckUpgrade || !this.UserInfo.IsSuperUser) - { - return string.Empty; - } - var latestReleases = Globals.GetJsonObject>("https://api.github.com/repos/dnnsoftware/dnn.platform/releases?per_page=5"); if (latestReleases != null) { diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/App_LocalResources/PersonaBar.resx b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/App_LocalResources/PersonaBar.resx index 5251f50e654..64add7124c6 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/App_LocalResources/PersonaBar.resx +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/App_LocalResources/PersonaBar.resx @@ -321,4 +321,10 @@ Click to unlock Edit Mode. + + Update + + + Get New Version + \ No newline at end of file diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/index.html b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/index.html index f3049db8b6a..cfe156b715e 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/index.html +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/admin/personaBar/index.html @@ -26,8 +26,8 @@
-