|
| 1 | +#region Copyright |
| 2 | +// |
| 3 | +// Satrabel - http://www.satrabel.be |
| 4 | +// Copyright (c) 2002-2018 |
| 5 | +// by Satrabel |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | +// documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
| 10 | +// to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 13 | +// of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 16 | +// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 18 | +// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +// DEALINGS IN THE SOFTWARE. |
| 20 | +#endregion |
| 21 | +#region Usings |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Globalization; |
| 26 | +using System.Web; |
| 27 | +using System.Linq; |
| 28 | + |
| 29 | +using DotNetNuke.Common; |
| 30 | +using DotNetNuke.Common.Utilities; |
| 31 | +using DotNetNuke.Entities.Modules; |
| 32 | +using DotNetNuke.Entities.Portals; |
| 33 | +using DotNetNuke.Entities.Tabs; |
| 34 | +using DotNetNuke.Security.Permissions; |
| 35 | +using DotNetNuke.Services.Localization; |
| 36 | +using DotNetNuke.Services.Sitemap; |
| 37 | +using Satrabel.OpenContent.Components.UrlRewriter; |
| 38 | + |
| 39 | +#endregion |
| 40 | + |
| 41 | +namespace Satrabel.OpenContent.Providers.SitemapProviders |
| 42 | +{ |
| 43 | + public class OpenContentSitemapProvider : SitemapProvider |
| 44 | + { |
| 45 | + private bool includeHiddenPages; |
| 46 | + private float minPagePriority; |
| 47 | + |
| 48 | + private PortalSettings ps; |
| 49 | + private bool useLevelBasedPagePriority; |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Includes page urls on the sitemap |
| 53 | + /// </summary> |
| 54 | + /// <remarks> |
| 55 | + /// Pages that are included: |
| 56 | + /// - are not deleted |
| 57 | + /// - are not disabled |
| 58 | + /// - are normal pages (not links,...) |
| 59 | + /// - are visible (based on date and permissions) |
| 60 | + /// </remarks> |
| 61 | + public override List<SitemapUrl> GetUrls(int portalId, PortalSettings ps, string version) |
| 62 | + { |
| 63 | + var objTabs = new TabController(); |
| 64 | + OpenSitemapUrl pageUrl = null; |
| 65 | + var urls = new List<SitemapUrl>(); |
| 66 | + |
| 67 | + useLevelBasedPagePriority = bool.Parse(PortalController.GetPortalSetting("SitemapLevelMode", portalId, "False")); |
| 68 | + minPagePriority = float.Parse(PortalController.GetPortalSetting("SitemapMinPriority", portalId, "0.1"), CultureInfo.InvariantCulture); |
| 69 | + includeHiddenPages = bool.Parse(PortalController.GetPortalSetting("SitemapIncludeHidden", portalId, "False")); |
| 70 | + |
| 71 | + PortalController portalController = new PortalController(); |
| 72 | + PortalInfo objPortal = new PortalController().GetPortal(portalId); |
| 73 | + |
| 74 | + this.ps = ps; |
| 75 | + |
| 76 | + var Locales = ps.ContentLocalizationEnabled ? |
| 77 | + LocaleController.Instance.GetPublishedLocales(ps.PortalId).Values : |
| 78 | + LocaleController.Instance.GetLocales(ps.PortalId).Values; |
| 79 | + |
| 80 | + bool MultiLanguage = Locales.Count > 1; |
| 81 | + |
| 82 | + foreach (Locale loc in Locales) |
| 83 | + { |
| 84 | + foreach (TabInfo objTab in objTabs.GetTabsByPortal(portalId).Values) |
| 85 | + { |
| 86 | + if (objTab.CultureCode == loc.Code || objTab.IsNeutralCulture) |
| 87 | + { |
| 88 | + if (MultiLanguage) |
| 89 | + { |
| 90 | + objPortal = new PortalController().GetPortal(portalId, loc.Code); |
| 91 | + } |
| 92 | + if (!objTab.IsDeleted && !objTab.DisableLink && objTab.TabType == TabType.Normal && (Null.IsNull(objTab.StartDate) || objTab.StartDate < DateTime.Now) && |
| 93 | + (Null.IsNull(objTab.EndDate) || objTab.EndDate > DateTime.Now) && IsTabPublic(objTab.TabPermissions) && |
| 94 | + objTab.TabID != objPortal.SearchTabId && objTab.TabID != objPortal.UserTabId && (objPortal.UserTabId == Null.NullInteger || objTab.ParentId != objPortal.UserTabId) && objTab.TabID != objPortal.LoginTabId && objTab.TabID != objPortal.RegisterTabId) |
| 95 | + { |
| 96 | + var allowIndex = true; |
| 97 | + if ((!objTab.TabSettings.ContainsKey("AllowIndex") || !bool.TryParse(objTab.TabSettings["AllowIndex"].ToString(), out allowIndex) || allowIndex) && |
| 98 | + (includeHiddenPages || objTab.IsVisible)) |
| 99 | + { |
| 100 | + // page url |
| 101 | + pageUrl = GetPageUrl(objTab, MultiLanguage ? loc.Code : null); |
| 102 | + pageUrl.Alternates.AddRange(GetAlternates(objTab.TabID)); |
| 103 | + urls.Add(pageUrl); |
| 104 | + |
| 105 | + // modules urls |
| 106 | + //var rules = UrlRuleConfiguration.GetConfig(portalId).Rules; |
| 107 | + var rules = OpenContentUrlProvider.GetRules(portalId); |
| 108 | + foreach (var rule in rules.Where(r => r.TabId == objTab.TabID && r.InSitemap == true)) |
| 109 | + { |
| 110 | + if (rule.CultureCode == null || rule.CultureCode == loc.Code) |
| 111 | + { |
| 112 | + string[] pars = rule.Parameters.Split('&'); |
| 113 | + pageUrl = GetPageUrl(objTab, MultiLanguage ? loc.Code : null, pars); |
| 114 | + // if module support ML |
| 115 | + //pageUrl.Alternates.AddRange(GetAlternates(objTab.TabID, pars)); |
| 116 | + urls.Add(pageUrl); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return urls; |
| 125 | + } |
| 126 | + |
| 127 | + private List<OpenSitemapAlternate> GetAlternates(int tabId, params string[] AdditionalParameters) |
| 128 | + { |
| 129 | + List<OpenSitemapAlternate> alternates = new List<OpenSitemapAlternate>(); |
| 130 | + var Locales = LocaleController.Instance.GetLocales(ps.PortalId).Values; |
| 131 | + if (Locales.Count > 1) |
| 132 | + { |
| 133 | + foreach (Locale loc in Locales) |
| 134 | + { |
| 135 | + if (!ps.ContentLocalizationEnabled || loc.IsPublished) |
| 136 | + { |
| 137 | + string LocaleUrl; |
| 138 | + if (Locales.Count(l => l.Code.Substring(0, 2) == loc.Code.Substring(0, 2)) > 1) |
| 139 | + LocaleUrl = loc.Code; |
| 140 | + else |
| 141 | + LocaleUrl = loc.Code.Substring(0, 2); |
| 142 | + |
| 143 | + bool CanViewPage; |
| 144 | + var alt = new OpenSitemapAlternate() |
| 145 | + { |
| 146 | + Hreflang = LocaleUrl, |
| 147 | + Href = newUrl(tabId, loc.Code, out CanViewPage, AdditionalParameters) |
| 148 | + }; |
| 149 | + |
| 150 | + |
| 151 | + if (CanViewPage && !string.IsNullOrEmpty(alt.Href)) |
| 152 | + { |
| 153 | + alternates.Add(alt); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + /* |
| 158 | + if (ps.ActiveTab.TabID == ps.HomeTabId) |
| 159 | + { |
| 160 | + var altLink = new HtmlLink(); |
| 161 | + altLink.Href = page.Request.Url.Scheme + "://" + PortalSettings.Current.PortalAlias.HTTPAlias; |
| 162 | + altLink.Attributes["rel"] = "alternate"; |
| 163 | + altLink.Attributes["hreflang"] = "x-default"; |
| 164 | + page.Header.Controls.Add(altLink); |
| 165 | + } |
| 166 | + */ |
| 167 | + } |
| 168 | + if (alternates.Count == 1) |
| 169 | + alternates.Clear(); |
| 170 | + |
| 171 | + return alternates; |
| 172 | + } |
| 173 | + |
| 174 | + private string newUrl(int tabId, string newLanguage, out bool canViewPage, params string[] additionalParameters) |
| 175 | + { |
| 176 | + canViewPage = true; |
| 177 | + string Url = ""; |
| 178 | + Locale newLocale = LocaleController.Instance.GetLocale(newLanguage); |
| 179 | + //Ensure that the current ActiveTab is the culture of the new language |
| 180 | + bool islocalized = false; |
| 181 | + TabInfo localizedTab = new TabController().GetTabByCulture(tabId, ps.PortalId, newLocale); |
| 182 | + if (localizedTab != null) |
| 183 | + { |
| 184 | + islocalized = true; |
| 185 | + tabId = localizedTab.TabID; |
| 186 | + if (localizedTab.IsDeleted || localizedTab.TabType != TabType.Normal || !IsTabPublic(localizedTab.TabPermissions)) |
| 187 | + { |
| 188 | + canViewPage = false; |
| 189 | + } |
| 190 | + Url = Globals.NavigateURL(localizedTab.TabID, localizedTab.IsSuperTab, ps, "", newLanguage, additionalParameters); |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + canViewPage = false; |
| 195 | + } |
| 196 | + /* |
| 197 | + if (Url.ToLower().IndexOf(ps.PortalAlias.HTTPAlias.ToLower()) == -1) |
| 198 | + { |
| 199 | + // code to fix a bug in dnn5.1.2 for navigateurl |
| 200 | + if ((HttpContext.Current != null)) |
| 201 | + { |
| 202 | + Url = Globals.AddHTTP(HttpContext.Current.Request.Url.Host + Url); |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + // try to use the portalalias |
| 207 | + Url = Globals.AddHTTP(ps.PortalAlias.HTTPAlias.ToLower()) + Url; |
| 208 | + } |
| 209 | + } |
| 210 | + */ |
| 211 | + return Url; |
| 212 | + |
| 213 | + } |
| 214 | + /// <summary> |
| 215 | + /// Return the sitemap url node for the page |
| 216 | + /// </summary> |
| 217 | + /// <param name = "objTab">The page being indexed</param> |
| 218 | + /// <param name="language">Culture code to use in the URL</param> |
| 219 | + /// <returns>A SitemapUrl object for the current page</returns> |
| 220 | + /// <remarks> |
| 221 | + /// </remarks> |
| 222 | + private OpenSitemapUrl GetPageUrl(TabInfo objTab, string language, params string[] AdditionalParameters) |
| 223 | + { |
| 224 | + var pageUrl = new OpenSitemapUrl(); |
| 225 | + pageUrl.Url = Globals.NavigateURL(objTab.TabID, objTab.IsSuperTab, ps, "", language, AdditionalParameters); |
| 226 | + |
| 227 | + if (pageUrl.Url.ToLower().IndexOf(ps.PortalAlias.HTTPAlias.ToLower()) == -1) |
| 228 | + { |
| 229 | + // code to fix a bug in dnn5.1.2 for navigateurl |
| 230 | + if ((HttpContext.Current != null)) |
| 231 | + { |
| 232 | + pageUrl.Url = Globals.AddHTTP(HttpContext.Current.Request.Url.Host + pageUrl.Url); |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + // try to use the portalalias |
| 237 | + pageUrl.Url = Globals.AddHTTP(ps.PortalAlias.HTTPAlias.ToLower()) + pageUrl.Url; |
| 238 | + } |
| 239 | + } |
| 240 | + pageUrl.Priority = GetPriority(objTab); |
| 241 | + pageUrl.LastModified = objTab.LastModifiedOnDate; |
| 242 | + var modCtrl = new ModuleController(); |
| 243 | + foreach (ModuleInfo m in modCtrl.GetTabModules(objTab.TabID).Values) |
| 244 | + { |
| 245 | + if (m.LastModifiedOnDate > objTab.LastModifiedOnDate) |
| 246 | + { |
| 247 | + pageUrl.LastModified = m.LastModifiedOnDate; |
| 248 | + } |
| 249 | + } |
| 250 | + pageUrl.ChangeFrequency = SitemapChangeFrequency.Daily; |
| 251 | + |
| 252 | + return pageUrl; |
| 253 | + } |
| 254 | + |
| 255 | + /// <summary> |
| 256 | + /// When page level priority is used, the priority for each page will be computed from |
| 257 | + /// the hierarchy level of the page. |
| 258 | + /// Top level pages will have a value of 1, second level 0.9, third level 0.8, ... |
| 259 | + /// </summary> |
| 260 | + /// <param name = "objTab">The page being indexed</param> |
| 261 | + /// <returns>The priority assigned to the page</returns> |
| 262 | + /// <remarks> |
| 263 | + /// </remarks> |
| 264 | + protected float GetPriority(TabInfo objTab) |
| 265 | + { |
| 266 | + float priority = objTab.SiteMapPriority; |
| 267 | + |
| 268 | + if (useLevelBasedPagePriority) |
| 269 | + { |
| 270 | + if (objTab.Level >= 9) |
| 271 | + { |
| 272 | + priority = 0.1F; |
| 273 | + } |
| 274 | + else |
| 275 | + { |
| 276 | + priority = Convert.ToSingle(1 - (objTab.Level * 0.1)); |
| 277 | + } |
| 278 | + |
| 279 | + if (priority < minPagePriority) |
| 280 | + { |
| 281 | + priority = minPagePriority; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + return priority; |
| 286 | + } |
| 287 | + |
| 288 | + #region "Security Check" |
| 289 | + |
| 290 | + public virtual bool IsTabPublic(TabPermissionCollection objTabPermissions) |
| 291 | + { |
| 292 | + string roles = objTabPermissions.ToString("VIEW"); |
| 293 | + bool hasPublicRole = false; |
| 294 | + |
| 295 | + |
| 296 | + if ((roles != null)) |
| 297 | + { |
| 298 | + // permissions strings are encoded with Deny permissions at the beginning and Grant permissions at the end for optimal performance |
| 299 | + foreach (string role in roles.Split(new[] {';'})) |
| 300 | + { |
| 301 | + if (!string.IsNullOrEmpty(role)) |
| 302 | + { |
| 303 | + // Deny permission |
| 304 | + if (role.StartsWith("!")) |
| 305 | + { |
| 306 | + string denyRole = role.Replace("!", ""); |
| 307 | + if ((denyRole == Globals.glbRoleUnauthUserName || denyRole == Globals.glbRoleAllUsersName)) |
| 308 | + { |
| 309 | + hasPublicRole = false; |
| 310 | + break; |
| 311 | + } |
| 312 | + // Grant permission |
| 313 | + } |
| 314 | + else |
| 315 | + { |
| 316 | + if ((role == Globals.glbRoleUnauthUserName || role == Globals.glbRoleAllUsersName)) |
| 317 | + { |
| 318 | + hasPublicRole = true; |
| 319 | + break; |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + return hasPublicRole; |
| 327 | + } |
| 328 | + |
| 329 | + #endregion |
| 330 | + } |
| 331 | +} |
0 commit comments