/* * * (c) Copyright Ascensio System Limited 2010-2018 * * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights. * * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html * * You can contact Ascensio System SIA by email at sales@onlyoffice.com * * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3. * * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains * relevant author attributions when distributing the software. If the display of the logo in its graphic * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" * in every copy of the program you distribute. * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks. * */ using System; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Text; using System.Text.Json; using ASC.Common; using ASC.Core; using ASC.Core.Common.Settings; using ASC.CRM.Core; using ASC.CRM.Resources; using ASC.Data.Storage; using ASC.Web.Core; using ASC.Web.Core.Files; using ASC.Web.Studio.Core; using Microsoft.Extensions.Configuration; namespace ASC.Web.CRM.Classes { [Scope] public class Global { private IConfiguration _configuration; private SettingsManager _settingsManager; protected int _tenantID; private FilesLinkUtility _filesLinkUtility; private SetupInfo _setupInfo; private SecurityContext _securityContext; private StorageFactory _storageFactory; private CrmSecurity _crmSecurity; public Global(StorageFactory storageFactory, SecurityContext securityContext, SetupInfo setupInfo, FilesLinkUtility filesLinkUtility, CrmSecurity crmSecurity, TenantManager tenantManager, SettingsManager settingsManager, IConfiguration configuration ) { _storageFactory = storageFactory; _filesLinkUtility = filesLinkUtility; _setupInfo = setupInfo; _securityContext = securityContext; _crmSecurity = crmSecurity; _tenantID = tenantManager.GetCurrentTenant().TenantId; _settingsManager = settingsManager; _configuration = configuration; } public static readonly int EntryCountOnPage = 25; public static readonly int VisiblePageCount = 10; public static readonly int MaxCustomFieldSize = 150; public static readonly int MaxCustomFieldRows = 25; public static readonly int MaxCustomFieldCols = 150; public static readonly int DefaultCustomFieldSize = 40; public static readonly int DefaultCustomFieldRows = 2; public static readonly int DefaultCustomFieldCols = 40; public static readonly int MaxHistoryEventCharacters = 65000; public static readonly decimal MaxInvoiceItemPrice = (decimal)99999999.99; public IDataStore GetStore() { return _storageFactory.GetStorage(_tenantID.ToString(), "crm"); } public IDataStore GetStoreTemplate() { return _storageFactory.GetStorage(String.Empty, "crm_template"); } public bool CanCreateProjects() { throw new NotImplementedException(); //try //{ // var apiUrl = String.Format("{0}project/securityinfo.json", SetupInfo.WebApiBaseUrl); // var cacheKey = String.Format("{0}-{1}", SecurityContext.CurrentAccount.ID, apiUrl); // bool canCreateProject = false; // //if (HttpRuntime.Cache[cacheKey] != null) // // return Convert.ToBoolean(HttpRuntime.Cache[cacheKey]); // //var apiServer = new Api.ApiServer(); // //var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET"))))["response"]; // //if (responseApi.HasValues) // // canCreateProject = Convert.ToBoolean(responseApi["canCreateProject"].Value()); // //else // // canCreateProject = false; // //HttpRuntime.Cache.Remove(cacheKey); // //HttpRuntime.Cache.Insert(cacheKey, canCreateProject, null, System.Web.Caching.Cache.NoAbsoluteExpiration, // // TimeSpan.FromMinutes(5)); // return canCreateProject; //} //catch //{ // return false; //} } public bool CanDownloadInvoices { get { var value = _configuration["crm:invoice:download:enable"]; if (string.IsNullOrEmpty(value)) return false; bool canDownloadFiles = Convert.ToBoolean(value); if (canDownloadFiles && string.IsNullOrEmpty(_filesLinkUtility.DocServiceConverterUrl)) { canDownloadFiles = false; } return canDownloadFiles; } } public bool CanCreateReports { get { return !string.IsNullOrEmpty(_filesLinkUtility.DocServiceDocbuilderUrl) && _crmSecurity.IsAdmin; } } public void SaveDefaultCurrencySettings(CurrencyInfo currency) { var tenantSettings = _settingsManager.Load(); tenantSettings.DefaultCurrency = currency.Abbreviation; _settingsManager.Save(tenantSettings); } /// /// The method to Decode your Base64 strings. /// /// The String containing the characters to decode. /// A String containing the results of decoding the specified sequence of bytes. public static string DecodeFrom64(string encodedData) { var encodedDataAsBytes = Convert.FromBase64String(encodedData); return System.Text.Encoding.UTF8.GetString(encodedDataAsBytes); } /// /// The method create a Base64 encoded string from a normal string. /// /// The String containing the characters to encode. /// The Base64 encoded string. public static string EncodeTo64(string toEncode) { var toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode); return Convert.ToBase64String(toEncodeAsBytes); } public static byte[] ToByteArray(Stream inputStream) { var br = new MemoryStream(); var data = new byte[1024]; var readed = 0; while ((readed = inputStream.Read(data, 0, data.Length)) > 0) { br.Write(data, 0, readed); } br.Close(); return br.ToArray(); } public static string GetImgFormatName(ImageFormat format) { if (format.Equals(ImageFormat.Bmp)) return "bmp"; if (format.Equals(ImageFormat.Emf)) return "emf"; if (format.Equals(ImageFormat.Exif)) return "exif"; if (format.Equals(ImageFormat.Gif)) return "gif"; if (format.Equals(ImageFormat.Icon)) return "icon"; if (format.Equals(ImageFormat.Jpeg)) return "jpeg"; if (format.Equals(ImageFormat.MemoryBmp)) return "MemoryBMP"; if (format.Equals(ImageFormat.Png)) return "png"; if (format.Equals(ImageFormat.Tiff)) return "tiff"; if (format.Equals(ImageFormat.Wmf)) return "wmf"; return "jpg"; } public static byte[] SaveToBytes(Image img) { return CommonPhotoManager.SaveToBytes(img, GetImgFormatName(img.RawFormat)); } private static readonly string[] Formats = new[] { "o", "yyyy'-'MM'-'dd'T'HH'-'mm'-'ss'.'fffK", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK", "yyyy-MM-ddTHH:mm:ss" }; public static DateTime ApiDateTimeParse(string data) { if (string.IsNullOrEmpty(data)) throw new ArgumentNullException("data"); if (data.Length < 7) throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid); DateTime dateTime; if (DateTime.TryParseExact(data, Formats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateTime)) { return new DateTime(dateTime.Ticks, DateTimeKind.Unspecified); } throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid); } public static JsonDocument JObjectParseWithDateAsString(string data) { var readOnlySpan = new ReadOnlySpan(Encoding.UTF8.GetBytes(data)); Utf8JsonReader reader = new Utf8JsonReader(readOnlySpan); JsonDocument result; if (JsonDocument.TryParseValue(ref reader, out result)) { return result; } return null; } } }