示例#1
0
        protected virtual BrowscapXmlHelper GetBrowscapXmlHelper()
        {
            if (Singleton <BrowscapXmlHelper> .Instance != null)
            {
                return(Singleton <BrowscapXmlHelper> .Instance);
            }

            //no database created
            if (string.IsNullOrEmpty(_SmiConfig.UserAgentStringsPath))
            {
                return(null);
            }

            //prevent multi loading data
            lock (_locker)
            {
                //data can be loaded while we waited
                if (Singleton <BrowscapXmlHelper> .Instance != null)
                {
                    return(Singleton <BrowscapXmlHelper> .Instance);
                }

                var userAgentStringsPath            = _fileProvider.MapPath(_SmiConfig.UserAgentStringsPath);
                var crawlerOnlyUserAgentStringsPath = !string.IsNullOrEmpty(_SmiConfig.CrawlerOnlyUserAgentStringsPath)
                    ? _fileProvider.MapPath(_SmiConfig.CrawlerOnlyUserAgentStringsPath)
                    : string.Empty;

                var browscapXmlHelper = new BrowscapXmlHelper(userAgentStringsPath, crawlerOnlyUserAgentStringsPath, _fileProvider);
                Singleton <BrowscapXmlHelper> .Instance = browscapXmlHelper;

                return(Singleton <BrowscapXmlHelper> .Instance);
            }
        }
        /// <summary>
        /// Get all themes
        /// </summary>
        /// <returns>List of the theme descriptor</returns>
        public IList <ThemeDescriptor> GetThemes()
        {
            if (_themeDescriptors != null)
            {
                return(_themeDescriptors);
            }

            //load all theme descriptors
            _themeDescriptors = new List <ThemeDescriptor>();

            var themeDirectoryPath = _fileProvider.MapPath(SmiPluginDefaults.ThemesPath);

            foreach (var descriptionFile in _fileProvider.GetFiles(themeDirectoryPath, SmiPluginDefaults.ThemeDescriptionFileName, false))
            {
                var text = _fileProvider.ReadAllText(descriptionFile, Encoding.UTF8);
                if (string.IsNullOrEmpty(text))
                {
                    continue;
                }

                //get theme descriptor
                var themeDescriptor = GetThemeDescriptorFromText(text);

                //some validation
                if (string.IsNullOrEmpty(themeDescriptor?.SystemName))
                {
                    throw new Exception($"A theme descriptor '{descriptionFile}' has no system name");
                }

                _themeDescriptors.Add(themeDescriptor);
            }

            return(_themeDescriptors);
        }
示例#3
0
        /// <summary>
        /// Get system names of installed plugins from obsolete file
        /// </summary>
        /// <returns>List of plugin system names</returns>
        protected virtual IList <string> GetObsoleteInstalledPluginNames()
        {
            //check whether file exists
            var filePath = _fileProvider.MapPath(SmiPluginDefaults.InstalledPluginsFilePath);

            if (!_fileProvider.FileExists(filePath))
            {
                //if not, try to parse the file that was used in previous SmiMarketplace versions
                filePath = _fileProvider.MapPath(SmiPluginDefaults.ObsoleteInstalledPluginsFilePath);
                if (!_fileProvider.FileExists(filePath))
                {
                    return(new List <string>());
                }

                //get plugin system names from the old txt file
                var pluginSystemNames = new List <string>();
                using (var reader = new StringReader(_fileProvider.ReadAllText(filePath, Encoding.UTF8)))
                {
                    string pluginName;
                    while ((pluginName = reader.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(pluginName))
                        {
                            pluginSystemNames.Add(pluginName.Trim());
                        }
                    }
                }

                //and delete the old one
                _fileProvider.DeleteFile(filePath);

                return(pluginSystemNames);
            }

            var text = _fileProvider.ReadAllText(filePath, Encoding.UTF8);

            if (string.IsNullOrEmpty(text))
            {
                return(new List <string>());
            }

            //delete the old file
            _fileProvider.DeleteFile(filePath);

            //get plugin system names from the JSON file
            return(JsonConvert.DeserializeObject <IList <string> >(text));
        }
示例#4
0
        /// <summary>
        /// Execute commands from a file with SQL script against the context database
        /// </summary>
        /// <param name="fileProvider">File provider</param>
        /// <param name="filePath">Path to the file</param>
        protected void ExecuteSqlScriptFromFile(ISmiFileProvider fileProvider, string filePath)
        {
            filePath = fileProvider.MapPath(filePath);
            if (!fileProvider.FileExists(filePath))
            {
                return;
            }

            ExecuteSqlScript(fileProvider.ReadAllText(filePath, Encoding.Default));
        }
示例#5
0
 /// <summary>
 /// Try to write web.config file
 /// </summary>
 /// <returns></returns>
 protected virtual bool TryWriteWebConfig()
 {
     try
     {
         _fileProvider.SetLastWriteTimeUtc(_fileProvider.MapPath(SmiInfrastructureDefaults.WebConfigPath), DateTime.UtcNow);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#6
0
        /// <summary>
        /// Save data settings to the file
        /// </summary>
        /// <param name="settings">Data settings</param>
        /// <param name="fileProvider">File provider</param>
        public static void SaveSettings(DataSettings settings, ISmiFileProvider fileProvider = null)
        {
            Singleton <DataSettings> .Instance = settings ?? throw new ArgumentNullException(nameof(settings));

            fileProvider ??= CommonHelper.DefaultFileProvider;
            var filePath = fileProvider.MapPath(SmiDataSettingsDefaults.FilePath);

            //create file if not exists
            fileProvider.CreateFile(filePath);

            //save data settings to the file
            var text = JsonConvert.SerializeObject(Singleton <DataSettings> .Instance, Formatting.Indented);

            fileProvider.WriteAllText(filePath, text, Encoding.UTF8);
        }
        /// <summary>
        /// Get information
        /// </summary>
        /// <param name="ipAddress">IP address</param>
        /// <returns>Information</returns>
        protected virtual CountryResponse GetInformation(string ipAddress)
        {
            if (string.IsNullOrEmpty(ipAddress))
            {
                return(null);
            }

            try
            {
                //This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com
                var databasePath = _fileProvider.MapPath("~/App_Data/GeoLite2-Country.mmdb");
                var reader       = new DatabaseReader(databasePath);
                var omni         = reader.Country(ipAddress);
                return(omni);
                //more info: http://maxmind.github.io/GeoIP2-dotnet/
                //more info: https://github.com/maxmind/GeoIP2-dotnet
                //more info: http://dev.maxmind.com/geoip/geoip2/geolite2/
                //Console.WriteLine(omni.Country.IsoCode); // 'US'
                //Console.WriteLine(omni.Country.Name); // 'United States'
                //Console.WriteLine(omni.Country.Names["zh-CN"]); // '美国'
                //Console.WriteLine(omni.MostSpecificSubdivision.Name); // 'Minnesota'
                //Console.WriteLine(omni.MostSpecificSubdivision.IsoCode); // 'MN'
                //Console.WriteLine(omni.City.Name); // 'Minneapolis'
                //Console.WriteLine(omni.Postal.Code); // '55455'
                //Console.WriteLine(omni.Location.Latitude); // 44.9733
                //Console.WriteLine(omni.Location.Longitude); // -93.2323
            }
            //catch (AddressNotFoundException exc)
            catch (GeoIP2Exception)
            {
                //address is not found
                //do not throw exceptions
                return(null);
            }
            catch (Exception exc)
            {
                //do not throw exceptions
                _logger.Warning("Cannot load MaxMind record", exc);
                return(null);
            }
        }
示例#8
0
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //pictures
            var sampleImagesPath = _fileProvider.MapPath("~/Plugins/Widgets.NivoSlider/Content/nivoslider/sample-images/");

            //settings
            var settings = new NivoSliderSettings
            {
                Picture1Id = _pictureService.InsertPicture(_fileProvider.ReadAllBytes(_fileProvider.Combine(sampleImagesPath, "banner1.jpg")), MimeTypes.ImagePJpeg, "banner_1").Id,
                Text1      = "",
                Link1      = _webHelper.GetStoreLocation(false),
                Picture2Id = _pictureService.InsertPicture(_fileProvider.ReadAllBytes(_fileProvider.Combine(sampleImagesPath, "banner2.jpg")), MimeTypes.ImagePJpeg, "banner_2").Id,
                Text2      = "",
                Link2      = _webHelper.GetStoreLocation(false)
                             //Picture3Id = _pictureService.InsertPicture(File.ReadAllBytes(_fileProvider.Combine(sampleImagesPath,"banner3.jpg")), MimeTypes.ImagePJpeg, "banner_3").Id,
                             //Text3 = "",
                             //Link3 = _webHelper.GetStoreLocation(false),
            };

            _settingService.SaveSetting(settings);

            _localizationService.AddPluginLocaleResource(new Dictionary <string, string>
            {
                ["Plugins.Widgets.NivoSlider.Picture1"]     = "Picture 1",
                ["Plugins.Widgets.NivoSlider.Picture2"]     = "Picture 2",
                ["Plugins.Widgets.NivoSlider.Picture3"]     = "Picture 3",
                ["Plugins.Widgets.NivoSlider.Picture4"]     = "Picture 4",
                ["Plugins.Widgets.NivoSlider.Picture5"]     = "Picture 5",
                ["Plugins.Widgets.NivoSlider.Picture"]      = "Picture",
                ["Plugins.Widgets.NivoSlider.Picture.Hint"] = "Upload picture.",
                ["Plugins.Widgets.NivoSlider.Text"]         = "Comment",
                ["Plugins.Widgets.NivoSlider.Text.Hint"]    = "Enter comment for picture. Leave empty if you don't want to display any text.",
                ["Plugins.Widgets.NivoSlider.Link"]         = "URL",
                ["Plugins.Widgets.NivoSlider.Link.Hint"]    = "Enter URL. Leave empty if you don't want this picture to be clickable.",
                ["Plugins.Widgets.NivoSlider.AltText"]      = "Image alternate text",
                ["Plugins.Widgets.NivoSlider.AltText.Hint"] = "Enter alternate text that will be added to image."
            });

            base.Install();
        }
示例#9
0
        /// <summary>
        /// Load data settings
        /// </summary>
        /// <param name="filePath">File path; pass null to use the default settings file</param>
        /// <param name="reloadSettings">Whether to reload data, if they already loaded</param>
        /// <param name="fileProvider">File provider</param>
        /// <returns>Data settings</returns>
        public static DataSettings LoadSettings(string filePath = null, bool reloadSettings = false, ISmiFileProvider fileProvider = null)
        {
            if (!reloadSettings && Singleton <DataSettings> .Instance != null)
            {
                return(Singleton <DataSettings> .Instance);
            }

            fileProvider ??= CommonHelper.DefaultFileProvider;
            filePath ??= fileProvider.MapPath(SmiDataSettingsDefaults.FilePath);

            //check whether file exists
            if (!fileProvider.FileExists(filePath))
            {
                //if not, try to parse the file that was used in previous SmiMarketplace versions
                filePath = fileProvider.MapPath(SmiDataSettingsDefaults.ObsoleteFilePath);
                if (!fileProvider.FileExists(filePath))
                {
                    return(new DataSettings());
                }

                //get data settings from the old txt file
                var dataSettings = new DataSettings();
                using (var reader = new StringReader(fileProvider.ReadAllText(filePath, Encoding.UTF8)))
                {
                    string settingsLine;
                    while ((settingsLine = reader.ReadLine()) != null)
                    {
                        var separatorIndex = settingsLine.IndexOf(':');
                        if (separatorIndex == -1)
                        {
                            continue;
                        }

                        var key   = settingsLine.Substring(0, separatorIndex).Trim();
                        var value = settingsLine.Substring(separatorIndex + 1).Trim();

                        switch (key)
                        {
                        case "DataProvider":
                            dataSettings.DataProvider = Enum.TryParse(value, true, out DataProviderType providerType) ? providerType : DataProviderType.Unknown;
                            continue;

                        case "DataConnectionString":
                            dataSettings.ConnectionString = value;
                            continue;

                        default:
                            dataSettings.RawDataSettings.Add(key, value);
                            continue;
                        }
                    }
                }

                //save data settings to the new file
                SaveSettings(dataSettings, fileProvider);

                //and delete the old one
                fileProvider.DeleteFile(filePath);

                Singleton <DataSettings> .Instance = dataSettings;
                return(Singleton <DataSettings> .Instance);
            }

            var text = fileProvider.ReadAllText(filePath, Encoding.UTF8);

            if (string.IsNullOrEmpty(text))
            {
                return(new DataSettings());
            }

            //get data settings from the JSON file
            Singleton <DataSettings> .Instance = JsonConvert.DeserializeObject <DataSettings>(text);

            return(Singleton <DataSettings> .Instance);
        }