示例#1
0
        /// <summary>
        /// Authenticates the user and returns if it was successfull.
        /// </summary>
        /// <param name="login">The login</param>
        /// <param name="password">The password</param>
        /// <returns>An authenticated user</returns>
        public static SysUser Authenticate(string login, string password)
        {
            var siteversion = Convert.ToInt32(SysParam.GetByName("SITE_VERSION").Value);

            var users = GetFields(" * ", "sysuser_login = @0 AND sysuser_password = @1" +
                                  (siteversion > 20 ? " AND (sysuser_locked = 0 OR (sysuser_locked_until IS NOT NULL AND sysuser_locked_until <= @2))" : ""),
                                  login, SysUser.Encrypt(password), DateTime.Now);

            if (users.Count == 1)
            {
                // Update last & prev login date.
                if (siteversion > 22)
                {
                    // Don't log automatic login updates
                    users[0].LogChanges    = false;
                    users[0].PreviousLogin = users[0].LastLogin;
                    users[0].LastLogin     = DateTime.Now;
                    users[0].IsLocked      = false;
                    users[0].LockedUntil   = DateTime.MinValue;
                    users[0].Save();
                }
                return(users[0]);
            }
            return(null);
        }
示例#2
0
        /// <summary>
        /// Gets a thumbnail representing the current content file.
        /// </summary>
        /// <param name="response">The http response</param>
        /// <param name="size">The desired size</param>
        public void GetThumbnail(HttpContext context, int size = 60, bool nocache = false)
        {
            bool compress = false;
            var  param    = SysParam.GetByName("COMPRESS_IMAGES");

            if (param != null && param.Value == "1")
            {
                compress = true;
            }

            if ((nocache && ClientCache.HandleNoCache(context)) || !ClientCache.HandleClientCache(context, Id.ToString() + size.ToString(), Updated))
            {
                if (File.Exists(GetCacheThumbPath(size)))
                {
                    // Return generated & cached thumbnail
                    WriteFile(context, GetCacheThumbPath(size), compress);
                }
                else
                {
                    byte[] data = null;
                    if (IsDraft)
                    {
                        data = Application.Current.MediaProvider.GetDraft(Id);
                    }
                    if (!IsDraft || data == null)
                    {
                        data = Application.Current.MediaProvider.Get(Id);
                    }

                    if (data != null)
                    {
                        using (var mem = new MemoryStream(data)) {
                            using (var img = Image.FromStream(mem)) {
                                if (img != null)
                                {
                                    // Generate thumbnail from image
                                    using (Bitmap bmp = new Bitmap(size, size)) {
                                        using (Graphics grp = Graphics.FromImage(bmp)) {
                                            grp.SmoothingMode      = SmoothingMode.HighQuality;
                                            grp.CompositingQuality = CompositingQuality.HighQuality;
                                            grp.InterpolationMode  = InterpolationMode.High;

                                            // Resize and crop image
                                            Rectangle dst = new Rectangle(0, 0, bmp.Width, bmp.Height);
                                            grp.DrawImage(img, dst, img.Width > img.Height ? (img.Width - img.Height) / 2 : 0,
                                                          img.Height > img.Width ? (img.Height - img.Width) / 2 : 0, Math.Min(img.Width, img.Height),
                                                          Math.Min(img.Height, img.Width), GraphicsUnit.Pixel);

                                            bmp.Save(GetCacheThumbPath(size), compress ? System.Drawing.Imaging.ImageFormat.Jpeg : img.RawFormat);
                                        }
                                    }
                                    WriteFile(context, GetCacheThumbPath(size), compress);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Gets the physical media related to the content record and writes it to
        /// the given http response.
        /// </summary>
        /// <param name="response">The http response</param>
        public void GetMedia(HttpContext context, int?width = null, int?height = null)
        {
            if (!ClientCache.HandleClientCache(context, Id.ToString(), Updated))
            {
                var   compress = false;
                Image img      = null;
                var   param    = SysParam.GetByName("COMPRESS_IMAGES");
                if (param != null && param.Value == "1")
                {
                    compress = true;
                }

                var data = Application.Current.MediaProvider.Get(Id);

                try {
                    using (var mem = new MemoryStream(data)) {
                        img = Image.FromStream(mem);
                    }
                } catch {}

                if (img != null)
                {
                    // TODO: Do we really need the image for this, we have the dimensions in the database?
                    width = width.HasValue && width.Value < img.Width ? width : img.Width;
                    if (!height.HasValue)
                    {
                        height = Convert.ToInt32(((double)width / img.Width) * img.Height);
                    }

                    if (File.Exists(GetCachePath(width.Value, height.Value)))
                    {
                        // Return generated & cached resized image
                        WriteFile(context.Response, GetCachePath(width.Value, height.Value), compress);
                    }
                    else if (data != null)
                    {
                        int orgWidth = img.Width, orgHeight = img.Height;

                        using (var resized = Drawing.ImageUtils.Resize(img, width.Value, height.Value)) {
                            if (resized.Width != orgWidth || resized.Height != orgHeight)
                            {
                                resized.Save(GetCachePath(width.Value, height.Value), compress ? ImageFormat.Jpeg : img.RawFormat);
                            }
                        }
                        WriteFile(context.Response, GetCachePath(width.Value, height.Value), compress);
                    }
                    img.Dispose();
                }
                WriteFile(context.Response, data);
            }
        }
示例#4
0
        /// <summary>
        /// Gets the physical media related to the content record and writes it to
        /// the given http response.
        /// </summary>
        /// <param name="response">The http response</param>
        public void GetMedia(HttpContext context, int?width = null, int?height = null)
        {
            if (!ClientCache.HandleClientCache(context, Id.ToString(), Updated))
            {
                var   compress = false;
                Image img      = null;
                var   param    = SysParam.GetByName("COMPRESS_IMAGES");
                if (param != null && param.Value == "1")
                {
                    compress = true;
                }

                try {
                    // Try to create image from file.
                    img = Image.FromFile(PhysicalPath);
                } catch {}

                if (img != null)
                {
                    width = width.HasValue && width.Value < img.Width ? width : img.Width;
                    if (!height.HasValue)
                    {
                        height = Convert.ToInt32(((double)width / img.Width) * img.Height);
                    }

                    if (File.Exists(GetCachePath(width.Value, height.Value)))
                    {
                        // Return generated & cached resized image
                        img.Dispose();
                        WriteFile(context.Response, GetCachePath(width.Value, height.Value), compress);
                    }
                    else if (File.Exists(PhysicalPath))
                    {
                        int orgWidth = img.Width, orgHeight = img.Height;

                        var resized = Drawing.ImageUtils.Resize(img, width.Value, height.Value);
                        if (resized.Width != orgWidth || resized.Height != orgHeight)
                        {
                            resized.Save(GetCachePath(width.Value, height.Value), compress ? ImageFormat.Jpeg : img.RawFormat);
                        }
                        img.Dispose();
                        resized.Dispose();

                        WriteFile(context.Response, GetCachePath(width.Value, height.Value), compress);
                    }
                }
                WriteFile(context.Response, PhysicalPath);
            }
        }
示例#5
0
        /// <summary>
        /// Gets a thumbnail representing the current content file.
        /// </summary>
        /// <param name="response">The http response</param>
        /// <param name="size">The desired size</param>
        public void GetThumbnail(HttpContext context, int size = 60, bool nocache = false)
        {
            bool compress = false;
            var  param    = SysParam.GetByName("COMPRESS_IMAGES");

            if (param != null && param.Value == "1")
            {
                compress = true;
            }

            if ((nocache && ClientCache.HandleNoCache(context)) || !ClientCache.HandleClientCache(context, Id.ToString() + size.ToString(), Updated))
            {
                if (File.Exists(GetCacheThumbPath(size)))
                {
                    // Return generated & cached thumbnail
                    WriteFile(context.Response, GetCacheThumbPath(size), compress);
                }
                else if (File.Exists(PhysicalPath))                     // || IsFolder) {
                {
                    var img = Image.FromFile(PhysicalPath);

                    if (img != null)
                    {
                        // Generate thumbnail from image
                        using (Bitmap bmp = new Bitmap(size, size)) {
                            Graphics grp = Graphics.FromImage(bmp);

                            grp.SmoothingMode      = SmoothingMode.HighQuality;
                            grp.CompositingQuality = CompositingQuality.HighQuality;
                            grp.InterpolationMode  = InterpolationMode.High;

                            // Resize and crop image
                            Rectangle dst = new Rectangle(0, 0, bmp.Width, bmp.Height);
                            grp.DrawImage(img, dst, img.Width > img.Height ? (img.Width - img.Height) / 2 : 0,
                                          img.Height > img.Width ? (img.Height - img.Width) / 2 : 0, Math.Min(img.Width, img.Height),
                                          Math.Min(img.Height, img.Width), GraphicsUnit.Pixel);

                            bmp.Save(GetCacheThumbPath(size), compress ? System.Drawing.Imaging.ImageFormat.Jpeg : img.RawFormat);
                        }
                        WriteFile(context.Response, GetCacheThumbPath(size), compress);
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// Updates the current records meta data from the given physical file.
        /// </summary>
        /// <param name="content">The physical content</param>
        private void SetFileMeta(MediaFileContent content)
        {
            if (content != null)
            {
                try {
                    using (var mem = new MemoryStream(content.Body)) {
                        using (var img = Image.FromStream(mem)) {
                            Image resized = null;

                            // Check if we need to resize the image
                            try {
                                int max = Convert.ToInt32(SysParam.GetByName("IMAGE_MAX_WIDTH").Value);
                                if (max > 0)
                                {
                                    resized = Drawing.ImageUtils.Resize(img, max);
                                    using (var writer = new MemoryStream()) {
                                        resized.Save(writer, img.RawFormat);
                                        content.Body = writer.ToArray();
                                    }
                                }
                            } catch {}

                            IsImage = true;
                            Width   = resized != null ? resized.Width : img.Width;
                            Height  = resized != null ? resized.Height : img.Height;

                            if (resized != null)
                            {
                                resized.Dispose();
                            }
                        }
                    }
                } catch {
                    IsImage = false;
                }
                Size = content.Body.Length;
            }
        }
示例#7
0
        /// <summary>
        /// Gets the physical media related to the content record and writes it to
        /// the given http response.
        /// </summary>
        /// <param name="response">The http response</param>
        public void GetMedia(HttpContext context, int?width = null, int?height = null)
        {
            if (!ClientCache.HandleClientCache(context, Id.ToString(), Updated))
            {
                byte[] data     = null;
                var    compress = false;
                bool   draft    = (this is Content ? ((Content)(object)this).IsDraft : false);

                if (width.HasValue)
                {
                    // Try to get cached media from the provider
                    if (draft)
                    {
                        data = Application.Current.MediaCacheProvider.GetDraft(Id, width.Value, height, Piranha.IO.MediaType.Media);
                    }
                    else
                    {
                        data = Application.Current.MediaCacheProvider.Get(Id, width.Value, height,
                                                                          (this is Upload ? Piranha.IO.MediaType.Upload : Piranha.IO.MediaType.Media));
                    }

                    if (data == null)
                    {
                        // No cached media exists. Let's get it
                        if (draft)
                        {
                            data = Application.Current.MediaProvider.GetDraft(Id, Piranha.IO.MediaType.Media);
                            if (data == null)
                            {
                                draft = false;
                            }
                        }
                        if (!draft)
                        {
                            data = Application.Current.MediaProvider.Get(Id,
                                                                         (this is Upload ? Piranha.IO.MediaType.Upload : Piranha.IO.MediaType.Media));
                        }

                        if (data != null)
                        {
                            Image img = null;
                            try {
                                // We're requesting different dimensions, try to get the image
                                using (var mem = new MemoryStream(data)) {
                                    img = Image.FromStream(mem);
                                }
                                if (img != null)
                                {
                                    var newWidth  = width.HasValue && width.Value < img.Width ? width : img.Width;
                                    var newHeight = height;
                                    if (!newHeight.HasValue)
                                    {
                                        newHeight = Convert.ToInt32(((double)width / img.Width) * img.Height);
                                    }

                                    int orgWidth = img.Width, orgHeight = img.Height;
                                    var cropV = ((Content)(object)this).VerticalCropping;
                                    var cropH = ((Content)(object)this).HorizontalCropping;

                                    using (var resized = Drawing.ImageUtils.Resize(img, newWidth.Value, newHeight.Value, cropH, cropV)) {
                                        if (resized.Width != orgWidth || resized.Height != orgHeight)
                                        {
                                            // Check for optional compression
                                            var param = SysParam.GetByName("COMPRESS_IMAGES");
                                            if (param != null && param.Value == "1")
                                            {
                                                compress = true;
                                            }

                                            using (var mem = new MemoryStream()) {
                                                resized.Save(mem, compress ? ImageFormat.Jpeg : img.RawFormat);
                                                data = mem.ToArray();
                                            }
                                            if (draft)
                                            {
                                                Application.Current.MediaCacheProvider.PutDraft(Id, data, width.Value, height,
                                                                                                Piranha.IO.MediaType.Media);
                                            }
                                            else
                                            {
                                                Application.Current.MediaCacheProvider.Put(Id, data, width.Value, height,
                                                                                           (this is Upload ? Piranha.IO.MediaType.Upload : Piranha.IO.MediaType.Media));
                                            }
                                        }
                                    }
                                    img.Dispose();
                                }
                            } catch { }
                        }
                    }
                }
                else
                {
                    // Get the original media from the current provider
                    if (draft)
                    {
                        data = Application.Current.MediaProvider.GetDraft(Id, Piranha.IO.MediaType.Media);
                        if (data == null)
                        {
                            draft = false;
                        }
                    }
                    if (!draft)
                    {
                        data = Application.Current.MediaProvider.Get(Id,
                                                                     (this is Upload ? Piranha.IO.MediaType.Upload : Piranha.IO.MediaType.Media));
                    }
                }
                if (data != null)
                {
                    WriteFile(context, data, compress);
                }
            }
        }