Esempio n. 1
0
    public void save_Click(object sender, EventArgs e)
    {
        try
        {
            Post.Title = CheckInput("Title", inputTitle.Text);

            List<TransitTopic> topics = new List<TransitTopic>();
            foreach (ListItem topic in inputTopic.Items)
            {
                if (topic.Selected)
                {
                    TransitTopic t_topic = new TransitTopic();
                    t_topic.Name = topic.Text;
                    t_topic.Id = int.Parse(topic.Value);
                    topics.Add(t_topic);
                }
            }
            Post.Topics = topics.ToArray();

            Post.Body = inputBody.Content;
            Post.Publish = inputPublish.Checked;
            Post.Display = inputDisplay.Checked;
            Post.Sticky = inputSticky.Checked;
            Post.Export = inputExport.Checked;
            Post.Created = SessionManager.ToUTC(inputCreatedDate.SelectedDate.Add(
                inputCreatedTime.SelectedTime));
            Post.Id = PostId = SessionManager.BlogService.CreateOrUpdatePost(
                SessionManager.Ticket, Post);

            if (!string.IsNullOrEmpty(inputServerPath.Text))
            {
                string fullpath = Path.Combine(
                    SessionManager.GetSetting("Images", string.Empty),
                    inputServerPath.Text);

                ArrayList filenames = new ArrayList();
                filenames.AddRange(Directory.GetFiles(fullpath, "*.jpg"));
                filenames.AddRange(Directory.GetFiles(fullpath, "*.gif"));

                List<TransitPostImage> deleted = SessionManager.GetCachedCollection<TransitPostImage>(
                    "GetPostImages", SessionManager.Ticket, new TransitPostImageQueryOptions(Post.Id));

                List<TransitPostImage> updated = new List<TransitPostImage>();

                foreach (string filename in filenames)
                {

                    TransitImage image = new TransitImage();
                    image.Name = Path.GetFileName(filename);
                    image.Path = inputServerPath.Text;

                    for (int i = 0; i < deleted.Count; i++)
                    {
                        if (deleted[i].Image.Name == image.Name)
                        {
                            image = deleted[i].Image;
                            deleted.RemoveAt(i);
                            break;
                        }
                    }

                    ThumbnailBitmap bitmap = new ThumbnailBitmap(filename);
                    image.Thumbnail = bitmap.Thumbnail;

                    SessionManager.BlogService.CreateOrUpdatePostImage(
                        SessionManager.Ticket, PostId, image);
                }

                foreach (TransitPostImage dimage in deleted)
                {
                    SessionManager.BlogService.DeletePostImage(
                        SessionManager.Ticket, dimage.Id);
                }

                SessionManager.Invalidate<TransitPostImage>();

                images.Visible = true;
                GetDataImages(sender, e);
            }

            if (! string.IsNullOrEmpty(inputLogin.Text))
            {
                loginAdd_Click(sender, e);
            }

            SessionManager.Invalidate<TransitPost>();
            ReportInfo("Post Saved");
        }
        catch (Exception ex)
        {
            ReportException(ex);
        }
    }
Esempio n. 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Nullable<DateTime> ims = IfModifiedSince;

                if (ims.HasValue)
                {
                    if (ims.Value.ToUniversalTime().AddSeconds(CacheDuration) > DateTime.UtcNow)
                    {
                        Response.StatusCode = 304;
                        return;
                    }
                }

                Picture p = null;

                switch (PageType)
                {
                    case PicturePageType.Thumbnail:

                        if (RequestId == 0)
                        {
                            p = GetRandomPictureWithThumbnail();

                            if (p == null)
                            {
                                p = new Picture();
                                p.Id = 0;
                                p.Created = p.Modified = DateTime.Now;
                                p.Name = Guid.NewGuid().ToString();
                                p.Bitmap = ThumbnailBitmap.GetBitmapDataFromText("?", 72, 100, 150);
                            }
                        }
                        else
                        {
                            p = ims.HasValue ?
                                    GetPictureWithThumbnail(RequestId, ims.Value) :
                                    GetPictureWithThumbnail(RequestId);
                        }

                        break;
                    case PicturePageType.Bitmap:

                        p = ims.HasValue ?
                                GetPictureWithBitmap(RequestId, ims.Value) :
                                GetPictureWithBitmap(RequestId);

                        break;
                }

                if (p == null)
                {
                    Response.StatusCode = (ims.HasValue ? 304 : 404);
                    return;
                }

                if (p.Bitmap == null)
                {
                    Response.Redirect("./images/site/access.gif", true);
                    return;
                }

                Response.Cache.SetLastModified(p.Modified.ToLocalTime());
                Response.Cache.SetCacheability(HttpCacheability.Private);

                p.Name = (string.IsNullOrEmpty(p.Name)) ? p.Id.ToString() + ".jpg" : p.Id.ToString() + "-" + p.Name;

                switch (PageType)
                {
                    case PicturePageType.Thumbnail:
                        p.Name.Insert(0, "thumbnail-");
                        break;
                    case PicturePageType.Bitmap:
                        if (!string.IsNullOrEmpty(Copyright))
                        {
                            ThumbnailBitmap bitmap = new ThumbnailBitmap(p.Bitmap);
                            bitmap.AddCopyright(Copyright);
                            p.Bitmap = bitmap.Bitmap;
                        }
                        break;
                }

                Response.ContentType = "image/" + Path.GetExtension(p.Name).TrimStart(".".ToCharArray());
                Response.AddHeader("Content-disposition", "attachment; filename=" + p.Name);
                Response.AddHeader("Created", p.Created.ToString("r"));
                Response.AddHeader("Modified", p.Modified.ToString("r"));
                Response.BinaryWrite(p.Bitmap);
                Response.End();
            }
            catch (ThreadAbortException)
            {

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
Esempio n. 3
0
        public void CreatePostWithImageAndCommentTest()
        {
            // post
            TransitPost t_post = new TransitPost();
            t_post.Body = Guid.NewGuid().ToString();
            t_post.Title = Guid.NewGuid().ToString();
            t_post.Publish = true;
            t_post.Id = Blog.CreateOrUpdatePost(Ticket, t_post);
            Assert.Greater(t_post.Id, 0);

            // image
            TransitImage t_image = new TransitImage();
            t_image.Name = Guid.NewGuid().ToString();

            Bitmap b = new Bitmap(480, 480);
            Graphics g = Graphics.FromImage(b);
            g.FillEllipse(Brushes.Red, 0, 0, 480, 480);
            ThumbnailBitmap tb = new ThumbnailBitmap(b);

            t_image.Data = tb.Bitmap;
            t_image.Thumbnail = tb.Thumbnail;

            t_image.Id = Blog.CreateOrUpdatePostImage(Ticket, t_post.Id, t_image);
            Assert.Greater(t_image.Id, 0);

            // comment
            TransitComment t_comment = new TransitComment();
            t_comment.IpAddress = "127.0.0.1";
            t_comment.LoginId = Blog.GetLogin(Ticket).Id;
            t_comment.Text = Guid.NewGuid().ToString();

            t_comment.Id = Blog.CreateOrUpdateImageComment(Ticket, t_image.Id, t_comment);
            Assert.Greater(t_comment.Id, 0);

            Blog.DeleteImage(Ticket, t_image.Id);
            Blog.DeletePost(Ticket, t_post.Id);
        }
Esempio n. 4
0
        public void CreatePostWithImageTest()
        {
            TransitPost t_post = new TransitPost();
            t_post.Body = Guid.NewGuid().ToString();
            t_post.Title = Guid.NewGuid().ToString();
            t_post.Publish = true;
            t_post.Id = Blog.CreateOrUpdatePost(Ticket, t_post);
            Assert.Greater(t_post.Id, 0);

            TransitImage t_image = new TransitImage();
            t_image.Name = Guid.NewGuid().ToString();

            Bitmap b = new Bitmap(480, 480);
            Graphics g = Graphics.FromImage(b);
            g.FillEllipse(Brushes.Red, 0, 0, 480, 480);
            ThumbnailBitmap tb = new ThumbnailBitmap(b);

            t_image.Data = tb.Bitmap;
            t_image.Thumbnail = tb.Thumbnail;

            t_image.Id = Blog.CreateOrUpdatePostImage(Ticket, t_post.Id, t_image);
            Assert.Greater(t_image.Id, 0);

            Blog.DeletePost(Ticket, t_post.Id);
        }
        public void CreateSecurePostTest()
        {
            TransitPost t_post = new TransitPost();
            t_post.Body = Guid.NewGuid().ToString();
            t_post.Title = Guid.NewGuid().ToString();
            t_post.Id = Blog.CreateOrUpdatePost(Ticket, t_post);
            Assert.Greater(t_post.Id, 0);

            TransitImage t_image = new TransitImage();
            t_image.Name = Guid.NewGuid().ToString();

            Bitmap b = new Bitmap(480, 480);
            Graphics g = Graphics.FromImage(b);
            g.FillEllipse(Brushes.Red, 0, 0, 480, 480);
            ThumbnailBitmap tb = new ThumbnailBitmap(b);

            t_image.Data = tb.Bitmap;
            t_image.Thumbnail = tb.Thumbnail;

            t_image.Id = Blog.CreateOrUpdatePostImage(Ticket, t_post.Id, t_image);
            Assert.Greater(t_image.Id, 0);

            TransitLogin t_login = new TransitLogin();
            t_login.Username = Guid.NewGuid().ToString();
            t_login.Password = Guid.NewGuid().ToString();
            t_login.Role = TransitLoginRole.Guest;
            t_login.Id = Blog.CreateOrUpdateLogin(Ticket, t_login);
            Assert.Greater(t_login.Id, 0);

            TransitComment t_comment = new TransitComment();
            t_comment.IpAddress = "127.0.0.1";
            t_comment.Text = Guid.NewGuid().ToString();
            t_comment.LoginId = t_login.Id;
            t_comment.Id = Blog.CreateOrUpdatePostComment(Ticket, t_post.Id, t_comment);
            Assert.Greater(t_comment.Id, 0);

            int t_postlogin_id = Blog.CreateOrUpdatePostLogin(Ticket, t_post.Id, t_login);
            Assert.Greater(t_postlogin_id, 0);

            string authticket = Blog.Login(t_login.Username, t_login.Password);

            // check access to posts

            TransitPost t_post_unauthorized = Blog.GetPostById(null, t_post.Id);
            Assert.IsTrue(string.IsNullOrEmpty(t_post_unauthorized.Body), "Unathorized post body wasn't stripped.");

            TransitPost t_post_authorized = Blog.GetPostById(authticket, t_post.Id);
            Assert.IsFalse(string.IsNullOrEmpty(t_post_authorized.Body), "Authorized post was stripped.");

            // check access to images

            TransitImage t_image_unauthorized = Blog.GetImageWithBitmapById(null, t_image.Id);
            Assert.IsTrue(t_image_unauthorized.Data == null, "Unathorized image returned data.");

            TransitImage t_image_authorized = Blog.GetImageWithBitmapById(authticket, t_image.Id);
            Assert.IsTrue(t_image_authorized.Data != null, "Authorized image didn't return data.");

            // check access to comments

            TransitComment t_comment_unauthorized = Blog.GetCommentById(null, t_comment.Id);
            Assert.IsTrue(string.IsNullOrEmpty(t_comment_unauthorized.Text), "Unathorized comment returned data.");

            TransitComment t_comment_authorized = Blog.GetCommentById(authticket, t_comment.Id);
            Assert.IsFalse(string.IsNullOrEmpty(t_comment_authorized.Text), "Authorized comment didn't return data.");

            Blog.DeletePost(Ticket, t_post.Id);
        }