<commit_before>﻿#region Copyright
// 
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2016
// by DotNetNuke Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
#endregion

#region Usings



#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using Dnn.PersonaBar.Library.Controllers;
using Dnn.PersonaBar.Library.DTO.Tabs;
using Dnn.PersonaBar.Sites.Components.Dto;
using Dnn.PersonaBar.Sites.Services.Dto;
using DotNetNuke.Collections;
using DotNetNuke.Common;
using DotNetNuke.Common.Internal;
using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Tabs.TabVersions;
using DotNetNuke.Entities.Urls;
using DotNetNuke.Entities.Users;
using DotNetNuke.Instrumentation;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Mail;
using ICSharpCode.SharpZipLib.Zip;

namespace Dnn.PersonaBar.Sites.Components
{
    public class SitesController
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(SitesController));
        private readonly TabsController _tabsController = new TabsController();
        internal static readonly IList<string> ImageExtensions = new List<string>() { ".jpg", ".png", ".jpeg" };

        private CultureDropDownTypes DisplayType { get; set; }

        private string IconHome
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Home.png");
            }
        }

        private string IconPortal
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Portal.png");
            }
        }

        private string AdminOnlyIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_UserAdmin.png");
            }
        }

        private string RegisteredUsersIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_User.png");
            }
        }

        private string IconPageDisabled
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Disabled.png");
            }
        }

        private string IconPageHidden
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Hidden.png");
            }
        }

        private string IconRedirect
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Redirect.png");
            }
        }

        private string SecuredIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_UserSecure.png");
            }
        }

        private string AllUsersIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Everyone.png");
            }
        }

        private PortalSettings PortalSettings
        {
            get
            {
                return PortalController.Instance.GetCurrentPortalSettings();
            }
        }

        public string LocalResourcesFile
        {
            get
            {
                return Path.Combine("~/admin/Dnn.PersonaBar/App_LocalResources/Sites.resx");
            }
        }

        public IList<HttpAliasDto> FormatPortalAliases(int portalId)
        {
            var aliases = new List<HttpAliasDto>();

            var arr = PortalAliasController.Instance.GetPortalAliasesByPortalId(portalId).ToList();
            foreach (var portalAliasInfo in arr)
            {
                var httpAlias = Globals.AddHTTP(portalAliasInfo.HTTPAlias);
                var originalUrl = HttpContext.Current.Items["UrlRewrite:OriginalUrl"].ToString().ToLowerInvariant();

                httpAlias = Globals.AddPort(httpAlias, originalUrl);
                aliases.Add(new HttpAliasDto{ Url = portalAliasInfo.HTTPAlias, Link = httpAlias});
            }

            return aliases;
        }

        public string FormatExpiryDate(DateTime dateTime)
        {
            var strDate = string.Empty;
            if (!Null.IsNull(dateTime))
            {
                strDate = dateTime.ToShortDateString();
            }
            return strDate;
        }

        public IList<PortalController.PortalTemplateInfo> GetPortalTemplates()
        {
            var templates = PortalController.Instance.GetAvailablePortalTemplates();
            templates = templates.OrderBy(x => x, new TemplateDisplayComparer()).ToList();
            return templates;
        }

        public PortalController.PortalTemplateInfo GetPortalTemplate(string fileName, string cultureCode)
        {
            return PortalController.Instance.GetPortalTemplate(fileName, cultureCode);
        }

        public ListItem CreateListItem(PortalController.PortalTemplateInfo template)
        {
            string text, value;
            var fileName = Path.GetFileName(template.TemplateFilePath);
            if (string.IsNullOrEmpty(template.CultureCode))
            {
                text = template.Name;
                value = fileName;
            }
            else
            {
                if (DisplayType == 0)
                {
                    string _ViewType = Convert.ToString(DotNetNuke.Services.Personalization.Personalization.GetProfile("LanguageDisplayMode", "ViewType" + PortalSettings.Current.PortalId));
                    switch (_ViewType)
                    {
                        case "NATIVE":
                            DisplayType = CultureDropDownTypes.NativeName;
                            break;
                        case "ENGLISH":
                            DisplayType = CultureDropDownTypes.EnglishName;
                            break;
                        default:
                            DisplayType = CultureDropDownTypes.DisplayName;
                            break;
                    }
                }

                text = string.Format("{0} - {1}", template.Name, Localization.GetLocaleName(template.CultureCode, DisplayType));
                
                value = string.Format("{0}|{1}|{2}",
                    fileName, template.CultureCode, GetThumbnail(fileName));
            }

            return new ListItem(text, value);
        }

        public string GetDefaultTemplate()
        {
            var templates = PortalController.Instance.GetAvailablePortalTemplates();
            string currentCulture = Thread.CurrentThread.CurrentUICulture.Name;

            var defaultTemplates =
                templates.Where(x => Path.GetFileNameWithoutExtension(x.TemplateFilePath) == "Default Website").ToList();
<commit_msg>AP-51: return thumbnails for templates.<commit_after>﻿#region Copyright
// 
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2016
// by DotNetNuke Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
#endregion

#region Usings



#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using Dnn.PersonaBar.Library.Controllers;
using Dnn.PersonaBar.Library.DTO.Tabs;
using Dnn.PersonaBar.Sites.Components.Dto;
using Dnn.PersonaBar.Sites.Services.Dto;
using DotNetNuke.Collections;
using DotNetNuke.Common;
using DotNetNuke.Common.Internal;
using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Tabs.TabVersions;
using DotNetNuke.Entities.Urls;
using DotNetNuke.Entities.Users;
using DotNetNuke.Instrumentation;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Mail;
using ICSharpCode.SharpZipLib.Zip;

namespace Dnn.PersonaBar.Sites.Components
{
    public class SitesController
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(SitesController));
        private readonly TabsController _tabsController = new TabsController();
        internal static readonly IList<string> ImageExtensions = new List<string>() { ".jpg", ".png", ".jpeg" };

        private CultureDropDownTypes DisplayType { get; set; }

        private string IconHome
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Home.png");
            }
        }

        private string IconPortal
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Portal.png");
            }
        }

        private string AdminOnlyIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_UserAdmin.png");
            }
        }

        private string RegisteredUsersIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_User.png");
            }
        }

        private string IconPageDisabled
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Disabled.png");
            }
        }

        private string IconPageHidden
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Hidden.png");
            }
        }

        private string IconRedirect
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Redirect.png");
            }
        }

        private string SecuredIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_UserSecure.png");
            }
        }

        private string AllUsersIcon
        {
            get
            {
                return Globals.ResolveUrl("~/DesktopModules/Admin/Tabs/images/Icon_Everyone.png");
            }
        }

        private PortalSettings PortalSettings
        {
            get
            {
                return PortalController.Instance.GetCurrentPortalSettings();
            }
        }

        public string LocalResourcesFile
        {
            get
            {
                return Path.Combine("~/admin/Dnn.PersonaBar/App_LocalResources/Sites.resx");
            }
        }

        public IList<HttpAliasDto> FormatPortalAliases(int portalId)
        {
            var aliases = new List<HttpAliasDto>();

            var arr = PortalAliasController.Instance.GetPortalAliasesByPortalId(portalId).ToList();
            foreach (var portalAliasInfo in arr)
            {
                var httpAlias = Globals.AddHTTP(portalAliasInfo.HTTPAlias);
                var originalUrl = HttpContext.Current.Items["UrlRewrite:OriginalUrl"].ToString().ToLowerInvariant();

                httpAlias = Globals.AddPort(httpAlias, originalUrl);
                aliases.Add(new HttpAliasDto{ Url = portalAliasInfo.HTTPAlias, Link = httpAlias});
            }

            return aliases;
        }

        public string FormatExpiryDate(DateTime dateTime)
        {
            var strDate = string.Empty;
            if (!Null.IsNull(dateTime))
            {
                strDate = dateTime.ToShortDateString();
            }
            return strDate;
        }

        public IList<PortalController.PortalTemplateInfo> GetPortalTemplates()
        {
            var templates = PortalController.Instance.GetAvailablePortalTemplates();
            templates = templates.OrderBy(x => x, new TemplateDisplayComparer()).ToList();
            return templates;
        }

        public PortalController.PortalTemplateInfo GetPortalTemplate(string fileName, string cultureCode)
        {
            return PortalController.Instance.GetPortalTemplate(fileName, cultureCode);
        }

        public ListItem CreateListItem(PortalController.PortalTemplateInfo template)
        {
            string text, value;
            var fileName = Path.GetFileName(template.TemplateFilePath);
            if (string.IsNullOrEmpty(template.CultureCode))
            {
                text = template.Name;
                value = string.Format("{0}|{1}", fileName, GetThumbnail(fileName));
            }
            else
            {
                if (DisplayType == 0)
                {
                    string _ViewType = Convert.ToString(DotNetNuke.Services.Personalization.Personalization.GetProfile("LanguageDisplayMode", "ViewType" + PortalSettings.Current.PortalId));
                    switch (_ViewType)
                    {
                        case "NATIVE":
                            DisplayType = CultureDropDownTypes.NativeName;
                            break;
                        case "ENGLISH":
                            DisplayType = CultureDropDownTypes.EnglishName;
                            break;
                        default:
                            DisplayType = CultureDropDownTypes.DisplayName;
                            break;
                    }
                }

                text = string.Format("{0} - {1}", template.Name, Localization.GetLocaleName(template.CultureCode, DisplayType));
                
                value = string.Format("{0}|{1}|{2}", fileName, template.CultureCode, GetThumbnail(fileName));
            }

            return new ListItem(text, value);
        }

        public string GetDefaultTemplate()
        {
            var templates = PortalController.Instance.GetAvailablePortalTemplates();
            string currentCulture = Thread.CurrentThread.CurrentUICulture.Name;

            var defaultTemplates =
                templates.Where(x => Path.GetFileNameWithoutExtension(x.TemplateFilePath) == "Default Website").ToList();

{'fraction_non_alphanumeric': 0.08233974737339157, 'fraction_numerical': 0.0018887970723645378, 'mean_word_length': 2.0287808366106543, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3316879', 'n_tokens_mistral': 4511, 'n_tokens_neox': 4303, 'n_words': 976}