예제 #1
0
 public static BlogOption OverrideOption(this BlogOption self, BlogUtil util)
 {
     self.LockTags  = self.LockTags && util.CheckAdmin(true); // Admin and writer
     self.NoRate    = self.NoRate && util.CheckAdmin();       // Only admin
     self.NoComment = self.NoComment && util.CheckAdmin();    // Only admin
     self.NoApprove = self.NoApprove && util.CheckAdmin();
     return(self);
 }
예제 #2
0
 public static BlogOption MergeWith(this BlogOption self, BlogUtil util, BlogOption modified)
 {
     if (modified == null)
     {
         return(self);
     }
     if (util.CheckAdmin(true))
     {
         self.LockTags = modified.LockTags;
     }
     if (util.CheckAdmin())
     {
         self.NoRate    = modified.NoRate;
         self.NoApprove = modified.NoApprove;
     }
     self.LockDesc = modified.LockDesc;
     return(self);
 }
예제 #3
0
 private bool CheckUserOwnsMessage(Message m)
 {
     return(m != null && (m.Recipient.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase) ||
                          m.Sender.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase) ||
                          (m.Recipient.Equals("admin", StringComparison.OrdinalIgnoreCase) && _blogUtil.CheckAdmin())));
 }
예제 #4
0
        public async Task <ActionResult> Create(BlogEdit blog, [FromServices] HtmlSanitizerService sanitizerService)
        {
            ViewBag.CategoryList = _catUtil.GetCategoryDropdown();
            ViewBag.UserHanGroup = GetUserHanGroup();
            string        content    = blog.Content;
            bool          isLocalimg = false;
            Blog          newblog;
            List <string> imglist = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new BlogException();
                }
                if (content == null || string.IsNullOrWhiteSpace(BlogHelper.removeAllTags(content)))
                {
                    ModelState.AddModelError("", "内容不能为空或纯图片");
                    throw new BlogException();
                }
                if (NolinkCategories == null || !NolinkCategories.Contains(blog.CategoryID))
                {
                    if (blog.BlogLinks == null)
                    {
                        ModelState.AddModelError("", "链接地址不能为空");
                        throw new BlogException();
                    }
                    else
                    {
                        blog.BlogLinks = blog.BlogLinks.Where(b => !string.IsNullOrWhiteSpace(b.url)).ToArray();
                        if (!BlogHelper.checkBlogLinks(blog.BlogLinks))
                        {
                            ModelState.AddModelError("", "链接地址不能为空,且不得包含javascript");
                            throw new BlogException();
                        }
                    }
                }
                if (!_blogUtil.CheckAdmin())
                {
                    content = sanitizerService.Sanitize(content);
                }
                if (blog.HanGroupID.HasValue && !_db.HanGroupMembers.Any(h => h.Username == User.Identity.Name && h.HanGroupID == blog.HanGroupID))
                {
                    ModelState.AddModelError("", "汉化组ID无效,请刷新重试。");
                    throw new BlogException();
                }
                List <IFormFile> BlogImages = new List <IFormFile>();
                if (Request.Form.Files.Count > 0)
                {
                    for (int i = 0; i < Request.Form.Files.Count; i++)
                    {
                        var file = Request.Form.Files[i];
                        if (file.Length > 0)
                        {
                            if (!file.ContentType.Contains("image"))
                            {
                                ModelState.AddModelError("", "不接受的文件类型");
                                throw new BlogException();
                            }
                            else if (file.Length > 1048576 * 4)
                            {
                                ModelState.AddModelError("", "文件不得超过4MB");
                                throw new BlogException();
                            }
                            isLocalimg = true;
                            BlogImages.Add(file);
                        }
                        else
                        {
                            content = BlogHelper.removeImgPlaceholder(content, i);
                        }
                    }
                }
                if (!isLocalimg)
                {
                    var imgname = BlogHelper.getFirstImg(content);
                    if (imgname == null || imgname.Length < 5)
                    {
                        ModelState.AddModelError("", "请添加预览图!(上传或在文中外链图片)");
                        throw new BlogException();
                    }
                    imglist = new List <string>()
                    {
                        imgname
                    };
                }
                else
                {
                    try
                    {
                        imglist = await _uploadUtil.SaveImagesAsync(BlogImages);
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", "保存图片时发生异常:(" + e.Message + ")。如多次出错,请汇报给管理员。");
                        throw new BlogException(e.Message, e);
                    }
                    if (imglist.Count < 1)
                    {
                        ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。");
                        throw new BlogException();
                    }
                }
                string imgpath = string.Empty;
                if (imglist != null)
                {
                    imgpath = string.Join(";", imglist);
                }
                bool approve = User.IsInRole("Administrator") || User.IsInRole("Writers") || User.IsInRole("Moderator");
                // Replace 【】() with []()
                blog.BlogTitle = blog.BlogTitle.ToSingleByteCharacterString();
                blog.ImagePath = imgpath;
                content        = BlogHelper.RemoveComments(content);
                newblog        = _blogUtil.AddBlog(blog.BlogTitle, content,
                                                   blog.CategoryID, imgpath, User.Identity.Name, approve, isLocalimg, blog.BlogLinks);
                var taglist = new List <Tag>();
                if (!string.IsNullOrEmpty(blog.BlogTags))
                {
                    string[] tags = TagUtil.SplitTags(blog.BlogTags);
                    taglist = _tagUtil.AddTagsForBlog(newblog.BlogID, tags, newblog.Author);
                }
                var save = false;
                if (BlogHelper.BlogIsHarmony(_db, newblog, HarmonySettings))
                {
                    newblog.isHarmony = true;
                    save = true;
                }
                if (blog.HanGroupID.HasValue)
                {
                    _db.HanGroupBlogs.Add(new HanGroupBlog {
                        BlogID = newblog.BlogID, HanGroupID = blog.HanGroupID.Value
                    });
                    save = true;
                }
                if (blog.Option != null && !blog.Option.IsDefault())
                {
                    newblog.option = blog.Option.OverrideOption(_blogUtil);
                    if (newblog.option.NoApprove)
                    {
                        newblog.isApproved = false;
                    }
                    save = true;
                }
                if (save)
                {
                    _db.SaveChanges();
                }
                TriggerNewBlog(newblog, taglist);
            }
            catch (BlogException e)
            {
                if (Request.IsAjaxRequest())
                {
                    return(Json(new
                    {
                        err = e.Message + string.Join(";", ModelState.Values.SelectMany(m => m.Errors)
                                                      .Select(err => err.ErrorMessage)
                                                      .ToList())
                    }));
                }
                return(View(blog));
            }
            catch
            {
                if (isLocalimg && imglist != null)
                {
                    await _uploadUtil.DeleteFilesAsync(imglist.Concat(new[] { blog.ImagePath.Split(';')[0].Replace("/upload/", "/thumbs/") }));
                }
                throw;
            }
            if (Request.IsAjaxRequest())
            {
                return(Json(new { id = newblog.BlogID, src = BlogHelper.firstImgPath(newblog, true) }));
            }
            return(RedirectToAction("Details", new { id = newblog.BlogID }));
        }
예제 #5
0
        private bool CanJoinAuditor(int UserLevel)
        {
            if (!HasPassedExam())
            {
                return(false);
            }
            var setting = _dataSettings;

            return((setting.JoinAuditorLevel > 0 && UserLevel >= setting.JoinAuditorLevel) || _blogUtil.CheckAdmin(true));
        }
예제 #6
0
        public async Task <ActionResult> Create(TopicEdit topic, [FromServices] HtmlSanitizerService sanitizerService)
        {
            ViewBag.CategoryID = sl;
            topic.LoadBlog(_db);
            if (ModelState.IsValid)
            {
                Topic ntopic = new Topic();
                if (!_blogUtil.CheckAdmin())
                {
                    topic.Content = sanitizerService.Sanitize(topic.Content);
                }
                int i = 0;
                foreach (var bid in topic.BlogIDs.Distinct())
                {
                    var b = topic.Blogs.SingleOrDefault(bb => bb.BlogID == bid);
                    if (b == null)
                    {
                        ModelState.AddModelError("", "未找到ID编号为" + bid + "的资源");
                        return(View(topic));
                    }
                    var blogintopic = new BlogsInTopic {
                        blog = b, topic = ntopic, BlogOrder = i++
                    };
                    _db.BlogsInTopics.Add(blogintopic);
                }

                ntopic.Author     = User.Identity.Name;
                ntopic.CategoryID = topic.CategoryID;
                ntopic.Content    = topic.Content;
                ntopic.CreateDate = DateTime.Now;
                ntopic.UpdateDate = DateTime.Now;
                ntopic.TopicVisit = 0;
                ntopic.TopicTitle = topic.TopicTitle;

                if (topic.TopicImage != null)
                {   //ValidateFileAttribute里已经检查过了
                    ntopic.isLocalImg = true;
                    var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { topic.TopicImage });

                    if (imglist.Count < 1)
                    {
                        ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。");
                        return(View(topic));
                    }
                    ntopic.ImagePath = imglist[0];
                }
                else
                {
                    string imgname = BlogHelper.getFirstImg(ntopic.Content);
                    if (imgname == null || imgname.Length < 5)
                    {
                        ModelState.AddModelError("", "请添加预览图!(上传或在文中外链图片)");
                        return(View(topic));
                    }
                    ntopic.ImagePath = imgname;
                }
                if (topic.TopicBanner != null)
                {
                    var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { topic.TopicBanner }, false);

                    if (imglist.Count < 1)
                    {
                        ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。");
                        return(View(topic));
                    }
                    ntopic.BannerPath = imglist[0];
                }

                var tag = _db.Tags.SingleOrDefault(t => t.TagName == topic.TagName);
                if (tag == null)
                {
                    tag = new Tag {
                        TagName = topic.TagName
                    };
                }
                ntopic.tag = tag;
                var mention = new MentionHandler(_udb);
                ntopic.Content = mention.ParseMentions(BlogHelper.RemoveComments(ntopic.Content));
                _db.Topics.Add(ntopic);
                _db.SaveChanges();
                mention.SendMentionMsg(_msgUtil, ntopic.Author, ntopic.TopicTitle, Url.Action("Details", new { id = ntopic.TopicID }));
                TriggerNewTopic(ntopic);
                return(RedirectToAction("Details", new { id = ntopic.TopicID }));
            }
            return(View(topic));
        }