示例#1
0
        /// <summary>
        /// 创建主题
        /// </summary>
        /// <returns></returns>
        public string Create()
        {
            if (Signature != GetParam("sig").ToString())
            {
                ErrorCode = (int)ErrorType.API_EC_SIGNATURE;
                return "";
            }

            //如果是桌面程序则需要验证用户身份
            if (this.App.ApplicationType == (int)ApplicationType.DESKTOP)
            {
                if (Uid < 1)
                {
                    ErrorCode = (int)ErrorType.API_EC_SESSIONKEY;
                    return "";
                }
            }

            if (CallId <= LastCallId)
            {
                ErrorCode = (int)ErrorType.API_EC_CALLID;
                return "";
            }

            if (!CheckRequiredParams("topic_info"))
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }

            Topic topic;
            try
            {
                topic = JavaScriptConvert.DeserializeObject<Topic>(GetParam("topic_info").ToString());
            }
            catch
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }

            if (topic == null || AreParamsNullOrZeroOrEmptyString(topic.UId, topic.Fid, topic.Title, topic.Message))//(topic == null || topic.UId == 0 || topic.Fid == 0 || topic.Title == null || topic.Message == null)
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }
            //如果是桌面程序则需要验证用户身份,如果topic_info中的uid与session_key对应的uid不匹配,则将topic_info中的uid改为session_key对应的uid,防止客户端程序密钥泄漏时的恶意发帖行为
            if (this.App.ApplicationType == (int)ApplicationType.DESKTOP)
            {
                ShortUserInfo currentUserInfo = Discuz.Forum.Users.GetShortUserInfo(Uid);
                if (currentUserInfo.Adminid != 1 && Uid != topic.UId)
                {
                    topic.UId = Uid;
                }
            }

            ForumInfo forumInfo = Discuz.Forum.Forums.GetForumInfo(topic.Fid ?? 0);

            if (forumInfo == null || forumInfo.Layer == 0)
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }

            bool enabletag = (Config.Enabletag & forumInfo.Allowtag) == 1;

            //如果设置的主题类型,应该仍可添加topic

            //文档中应说明title长度范围和内容范围
            if (topic.Title.Length > 60)
            {
                ErrorCode = (int)ErrorType.API_EC_TITLE_INVALID;
                return "";
            }

            //内容长度限制应该在客户程序里实现
            //if (topic.Message.Length < Config.Minpostsize)
            //{
            //    //AddErrLine("您发表的内容过少, 系统设置要求帖子内容不得少于 " + Config.Minpostsize.ToString() + " 字多于 " + Config.Maxpostsize.ToString() + " 字");
            //    ErrorCode = (int)ErrorType.API_EC_PARAM;
            //    return "";
            //}
            //else if (topic.Message.Length > Config.Maxpostsize)
            //{
            //    //AddErrLine("您发表的内容过多, 系统设置要求帖子内容不得少于 " + Config.Minpostsize.ToString() + " 字多于 " + Config.Maxpostsize.ToString() + " 字");
            //    ErrorCode = (int)ErrorType.API_EC_PARAM;
            //    return "";
            //}

            #region Inner
            ShortUserInfo userInfo = Discuz.Forum.Users.GetShortUserInfo(topic.UId ?? Uid);

            //新用户广告强力屏蔽检查,尽在传入session_key时验证
            if (Uid > 0)
            {
                ShortUserInfo currentUserInfo = Discuz.Forum.Users.GetShortUserInfo(Uid);

                if ((Config.Disablepostad == 1) && userInfo.Adminid < 1)  //如果开启新用户广告强力屏蔽检查或是游客
                {
                    if ((Config.Disablepostadpostcount != 0 && currentUserInfo.Posts <= Config.Disablepostadpostcount) ||
                        (Config.Disablepostadregminute != 0 && DateTime.Now.AddMinutes(-Config.Disablepostadregminute) <= Convert.ToDateTime(currentUserInfo.Joindate)))
                    {
                        foreach (string regular in Config.Disablepostadregular.Replace("\r", "").Split('\n'))
                        {
                            if (Posts.IsAD(regular, topic.Title, topic.Message))
                            {
                                ErrorCode = (int)ErrorType.API_EC_SPAM;
                                return "";
                            }
                        }
                    }
                }
            }

            UserGroupInfo usergroupinfo = UserGroups.GetUserGroupInfo(userInfo.Groupid);

            int iconid = topic.Iconid ?? 0;
            if (iconid > 15 || iconid < 0)
            {
                iconid = 0;
            }

            TopicInfo topicInfo = new TopicInfo();
            topicInfo.Fid = topic.Fid ?? 0;
            topicInfo.Iconid = iconid;
            topicInfo.Title = Utils.HtmlEncode(ForumUtils.BanWordFilter(topic.Title));

            bool htmlon = topic.Message.Length != Utils.RemoveHtml(topic.Message).Length && usergroupinfo.Allowhtml == 1;
            string message = ForumUtils.BanWordFilter(topic.Message);
            if (!htmlon)
            {
                message = Utils.HtmlDecode(message);
            }

            if (ForumUtils.HasBannedWord(topicInfo.Title) || ForumUtils.HasBannedWord(message))
            {
                ErrorCode = (int)ErrorType.API_EC_SPAM;
                return "";
            }
            string curdatetime = Utils.GetDateTime();

            topicInfo.Typeid = 0;
            if (forumInfo.Applytopictype == 1)
            {
                if (Discuz.Forum.Forums.IsCurrentForumTopicType(topic.Typeid.ToString(), forumInfo.Topictypes))
                {
                    topicInfo.Typeid = (int)topic.Typeid;
                }
                else if (forumInfo.Postbytopictype == 1)
                {
                    ErrorCode = (int)ErrorType.API_EC_PARAM;
                    return "";
                }
            }

            topicInfo.Readperm = 0;
            topicInfo.Price = 0;
            topicInfo.Poster = userInfo.Username;
            topicInfo.Posterid = userInfo.Uid;
            topicInfo.Postdatetime = curdatetime;
            topicInfo.Lastpost = curdatetime;
            topicInfo.Lastposter = userInfo.Username;
            topicInfo.Views = 0;
            topicInfo.Replies = 0;

            if (forumInfo.Modnewposts == 1)
            {
                topicInfo.Displayorder = -2;
            }
            else
            {
                topicInfo.Displayorder = 0;
            }

            if (Scoresets.BetweenTime(Config.Postmodperiods) || ForumUtils.HasAuditWord(topicInfo.Title) || ForumUtils.HasAuditWord(message))
            {
                topicInfo.Displayorder = -2;
            }

            topicInfo.Highlight = "";
            topicInfo.Digest = 0;
            topicInfo.Rate = 0;
            topicInfo.Hide = 0;
            topicInfo.Attachment = 0;
            topicInfo.Moderated = 0;
            topicInfo.Closed = 0;

            string tags = string.Empty;
            string[] tagArray = null;

            if (!string.IsNullOrEmpty(topic.Tags))
            {
                //标签(Tag)操作                
                tags = topic.Tags.Trim();
                tagArray = Utils.SplitString(tags, ",", true, 2, 10);
                if (enabletag)
                {
                    if (topicInfo.Magic == 0)
                    {
                        topicInfo.Magic = 10000;
                    }
                    topicInfo.Magic = Utils.StrToInt(topicInfo.Magic.ToString() + "1", 0);
                }
            }

            int topicid = Discuz.Forum.Topics.CreateTopic(topicInfo);

            if (enabletag && tagArray != null && tagArray.Length > 0)
            {
                if (!ForumUtils.HasBannedWord(tags))
                {
                    ForumTags.CreateTopicTags(tagArray, topicid, userInfo.Uid, curdatetime);
                }
            }

            PostInfo postinfo = new PostInfo();
            postinfo.Fid = forumInfo.Fid;
            postinfo.Tid = topicid;
            postinfo.Parentid = 0;
            postinfo.Layer = 0;
            postinfo.Poster = userInfo.Username;
            postinfo.Posterid = userInfo.Uid;
            if (userInfo.Adminid == 1)
            {
                postinfo.Title = Utils.HtmlEncode(DNTRequest.GetString("title"));
            }
            else
            {
                postinfo.Title = Utils.HtmlEncode(ForumUtils.BanWordFilter(DNTRequest.GetString("title")));
            }

            postinfo.Postdatetime = curdatetime;
            postinfo.Message = message;
            postinfo.Ip = DNTRequest.GetIP();
            postinfo.Lastedit = "";

            if (ForumUtils.HasAuditWord(postinfo.Message))
            {
                postinfo.Invisible = 1;
            }

            if (forumInfo.Modnewposts == 1 && userInfo.Adminid != 1)
            {
                postinfo.Invisible = 1;
            }
            // 如果当前用户非管理员并且论坛设定了发帖审核时间段,当前时间如果在其中的一个时间段内,则用户所发帖均为待审核状态
            if (userInfo.Adminid != 1 && Scoresets.BetweenTime(Config.Postmodperiods))
            {
                postinfo.Invisible = 1;
            }

            postinfo.Usesig = Utils.StrToInt(DNTRequest.GetString("usesig"), 0);
            if (htmlon)
                postinfo.Htmlon = 1;
            else
                postinfo.Htmlon = 0;

            postinfo.Smileyoff = 1 - forumInfo.Allowsmilies;

            postinfo.Bbcodeoff = 1;

            if (usergroupinfo.Allowcusbbcode == 1 && forumInfo.Allowbbcode == 1)
            {
                postinfo.Bbcodeoff = 0;
            }
            postinfo.Parseurloff = 0;
            postinfo.Attachment = 0;
            postinfo.Rate = 0;
            postinfo.Ratetimes = 0;
            postinfo.Topictitle = topicInfo.Title;

            int postid = 0;

            try
            {
                postid = Posts.CreatePost(postinfo);
            }
            catch
            {
                TopicAdmins.DeleteTopics(topicid.ToString(), false);
                ErrorCode = (int)ErrorType.API_EC_UNKNOWN;
                return "";
            }

            Discuz.Forum.Topics.AddParentForumTopics(forumInfo.Parentidlist.Trim(), 1);

            TopicCreateResponse tcr = new TopicCreateResponse();

            tcr.TopicId = topicid;
            tcr.Url = ForumUrl + Discuz.Forum.Urls.ShowTopicAspxRewrite(topicid, 0);


            //设置用户的积分
            ///首先读取版块内自定义积分
            ///版设置了自定义积分则使用,否则使用论坛默认积分
            float[] values = null;
            if (!forumInfo.Postcredits.Equals(""))
            {
                int index = 0;
                float tempval = 0;
                values = new float[8];
                foreach (string ext in Utils.SplitString(forumInfo.Postcredits, ","))
                {

                    if (index == 0)
                    {
                        if (!ext.Equals("True"))
                        {
                            values = null;
                            break;
                        }
                        index++;
                        continue;
                    }
                    tempval = Utils.StrToFloat(ext, 0);
                    values[index - 1] = tempval;
                    index++;
                    if (index > 8)
                    {
                        break;
                    }
                }
            }



            #region 更新积分

            if (userInfo.Adminid != 1)
            {
                bool needaudit = false; //是否需要审核

                if (Scoresets.BetweenTime(Config.Postmodperiods))
                {
                    needaudit = true;
                }
                else
                {
                    if (forumInfo.Modnewposts == 1 && userInfo.Adminid != 1)
                    {
                        //if (userinfo.Adminid > 1)
                        //{
                        //if (disablepost == 1 && topicinfo.Displayorder != -2)
                        //{
                        //if (useradminid == 3 && !Moderators.IsModer(useradminid, userid, forumid))
                        //{
                        //    needaudit = true;
                        //}
                        //else
                        //{
                        //    needaudit = false;
                        //}
                        //}
                        //else
                        //{
                        //needaudit = true;
                        //}
                        //}
                        //else
                        //{
                        needaudit = true;
                        //}
                    }
                    else
                    {
                        if (userInfo.Adminid != 1 && topicInfo.Displayorder == -2)
                        {
                            needaudit = true;
                        }
                    }
                }
                if (needaudit)
                {
                    //需要审核
                    tcr.NeedAudit = true;
                }
                else
                {
                    UpdateScore(userInfo.Uid, values);
                }
            }
            else
            {
                UpdateScore(userInfo.Uid, values);
            }

            #endregion


            #endregion

            //同步到其他应用程序
            Sync.NewTopic(topicid.ToString(), topicInfo.Title, topicInfo.Poster, topicInfo.Posterid.ToString(), topicInfo.Fid.ToString());

            if (Format == FormatType.JSON)
            {
                return JavaScriptConvert.SerializeObject(tcr);
            }
            return SerializationHelper.Serialize(tcr);

        }
示例#2
0
        /*
         * Description:
         * 桌面程序强制validate=true,且必须是在线用户
         */
        public override bool Run(CommandParameter commandParam, ref string result)
        {
            //桌面程序因为安全需要,游客不允许操作
            if (commandParam.AppInfo.ApplicationType == (int)ApplicationType.DESKTOP && commandParam.LocalUid < 1)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_SESSIONKEY, commandParam.ParamList);
                return false;
            }

            //如果validate为true,则校验数据的合法性,包括广告强力屏蔽,是否含有需审核的,以及非法内容.和当前用户的发帖权限,桌面程序强制验证
            bool validate = commandParam.GetIntParam("validate") == 1 || commandParam.AppInfo.ApplicationType == (int)ApplicationType.DESKTOP;

            if (!commandParam.CheckRequiredParams("topic_info"))
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                return false;
            }
            Topic topic;
            try
            {
                topic = JavaScriptConvert.DeserializeObject<Topic>(commandParam.GetDNTParam("topic_info").ToString());
            }
            catch
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                return false;
            }
            if (topic == null || Util.AreParamsNullOrZeroOrEmptyString(topic.Fid, topic.Title, topic.Message))
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                return false;
            }
            //文档中应说明title长度范围和内容范围
            if (topic.Title.Length > 60)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_TITLE_INVALID, commandParam.ParamList);
                return false;
            }

            ForumInfo forumInfo = Discuz.Forum.Forums.GetForumInfo(topic.Fid ?? 0);
            if (forumInfo == null || forumInfo.Layer == 0)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_FORUM_NOT_EXIST, commandParam.ParamList);
                return false;
            }

            //如果validate为true,则强制读取当前用户
            ShortUserInfo userInfo = Discuz.Forum.Users.GetShortUserInfo(validate || (topic.UId == null) ? commandParam.LocalUid : (int)topic.UId);
            userInfo = userInfo == null ? TopicsCommandUtils.GetGuestUserInfo() : userInfo;
            UserGroupInfo userGroupInfo = UserGroups.GetUserGroupInfo(userInfo.Groupid);
            AdminGroupInfo adminInfo = AdminGroups.GetAdminGroupInfo(userGroupInfo.Groupid);

            //是否受审核、过滤、灌水等限制权限
            int disablePost = adminInfo != null ? adminInfo.Disablepostctrl : userGroupInfo.Disableperiodctrl;
            bool hasAudit = false;

            if (validate)
            {
                ErrorType et = TopicsCommandUtils.GeneralValidate(topic.Title, topic.Message, userInfo, userGroupInfo, forumInfo, commandParam, disablePost);
                if (et != ErrorType.API_EC_NONE)
                {
                    result = Util.CreateErrorMessage(et, commandParam.ParamList);
                    return false;
                }
                string str = "";
                //是否允许发主题
                if (!UserAuthority.PostAuthority(forumInfo, userGroupInfo, userInfo.Uid, ref str))
                {
                    result = Util.CreateErrorMessage(ErrorType.API_EC_POST_PERM, commandParam.ParamList);
                    return false;
                }

                if (disablePost != 1)
                {
                    et = TopicsCommandUtils.PostTimeAndRepostMessageValidate(userInfo, topic.Title + topic.Message);
                    if (et != ErrorType.API_EC_NONE)
                    {
                        result = Util.CreateErrorMessage(et, commandParam.ParamList);
                        return false;
                    }
                    //内容中是否含有需审核的词汇
                    if (ForumUtils.HasAuditWord(topic.Title + topic.Message))
                        hasAudit = true;
                    //过滤非法词汇
                    topic.Title = ForumUtils.BanWordFilter(topic.Title);
                    topic.Message = ForumUtils.BanWordFilter(topic.Message);
                }
            }

            //主题图标id
            int iconId = topic.Iconid ?? 0;
            //图标id值域仅为0-15
            iconId = (iconId > 15 || iconId < 0) ? 0 : iconId;

            TopicInfo topicInfo = new TopicInfo();
            topicInfo.Fid = forumInfo.Fid;
            topicInfo.Iconid = iconId;
            topicInfo.Title = Utils.HtmlEncode(topic.Title);

            bool htmlon = topic.Message.Length != Utils.RemoveHtml(topic.Message).Length && userGroupInfo.Allowhtml == 1;
            //支持html标签?
            if (!htmlon)
                topic.Message = Utils.HtmlEncode(topic.Message);

            string curDateTime = Utils.GetDateTime();

            //发帖主题分类校验和绑定
            topicInfo.Typeid = 0;
            if (forumInfo.Applytopictype == 1)
            {
                if (Discuz.Forum.Forums.IsCurrentForumTopicType(topic.Typeid.ToString(), forumInfo.Topictypes))
                    topicInfo.Typeid = (int)topic.Typeid;
                else if (forumInfo.Postbytopictype == 1)
                {
                    result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                    return false;
                }
            }
            topicInfo.Readperm = 0;
            topicInfo.Price = 0;
            topicInfo.Poster = userInfo.Username;
            topicInfo.Posterid = userInfo.Uid;
            topicInfo.Postdatetime = curDateTime;
            topicInfo.Lastpost = curDateTime;
            topicInfo.Lastposter = userInfo.Username;
            topicInfo.Views = 0;
            topicInfo.Replies = 0;

            topicInfo.Displayorder = (forumInfo.Modnewtopics == 1) ? -2 : 0;
            if (topicInfo.Displayorder != -2 && (hasAudit || Scoresets.BetweenTime(commandParam.GeneralConfig.Postmodperiods)))
                topicInfo.Displayorder = -2;

            topicInfo.Highlight = "";
            topicInfo.Digest = 0;
            topicInfo.Rate = 0;
            topicInfo.Hide = 0;
            topicInfo.Attachment = 0;
            topicInfo.Moderated = 0;
            topicInfo.Closed = 0;

            string tags = string.Empty;
            string[] tagArray = null;

            //是否使用tag
            bool enableTag = (commandParam.GeneralConfig.Enabletag & forumInfo.Allowtag) == 1;
            if (!string.IsNullOrEmpty(topic.Tags))
            {
                //标签(Tag)操作                
                tags = topic.Tags.Trim();
                tagArray = Utils.SplitString(tags, ",", true, 2, 10);
                if (enableTag)
                {
                    if (topicInfo.Magic == 0)
                        topicInfo.Magic = 10000;
                    topicInfo.Magic = Utils.StrToInt(topicInfo.Magic.ToString() + "1", 0);
                }
            }

            int topicId = Discuz.Forum.Topics.CreateTopic(topicInfo);

            if (enableTag && tagArray != null && tagArray.Length > 0)
            {
                //若当前用户不受过滤,审核约束
                if (!validate || disablePost == 1 || !ForumUtils.HasBannedWord(tags))
                    ForumTags.CreateTopicTags(tagArray, topicId, userInfo.Uid, curDateTime);
            }

            PostInfo postInfo = new PostInfo();
            postInfo.Fid = forumInfo.Fid;
            postInfo.Tid = topicId;
            postInfo.Parentid = 0;
            postInfo.Layer = 0;
            postInfo.Poster = userInfo.Username;
            postInfo.Posterid = userInfo.Uid;
            postInfo.Title = topicInfo.Title;
            postInfo.Postdatetime = curDateTime;
            postInfo.Message = topic.Message;
            postInfo.Ip = DNTRequest.GetIP();
            postInfo.Lastedit = "";
            postInfo.Invisible = topicInfo.Displayorder == -2 ? 1 : 0;
            postInfo.Usesig = 0;
            postInfo.Htmlon = htmlon ? 1 : 0;
            postInfo.Smileyoff = 1 - forumInfo.Allowsmilies;
            postInfo.Bbcodeoff = 1;

            if (userGroupInfo.Allowcusbbcode == 1 && forumInfo.Allowbbcode == 1)
                postInfo.Bbcodeoff = 0;

            postInfo.Parseurloff = 0;
            postInfo.Attachment = 0;
            postInfo.Rate = 0;
            postInfo.Ratetimes = 0;
            postInfo.Topictitle = topicInfo.Title;

            int postid = 0;
            try
            {
                postid = Posts.CreatePost(postInfo);
            }
            catch
            {
                TopicAdmins.DeleteTopics(topicId.ToString(), false);
                result = Util.CreateErrorMessage(ErrorType.API_EC_UNKNOWN, commandParam.ParamList);
                return false;
            }

            Discuz.Forum.Topics.AddParentForumTopics(forumInfo.Parentidlist.Trim(), 1);

            TopicCreateResponse tcr = new TopicCreateResponse();

            tcr.TopicId = topicId;
            tcr.Url = Utils.GetRootUrl(BaseConfigs.GetForumPath) + Discuz.Forum.Urls.ShowTopicAspxRewrite(topicId, 0);
            tcr.NeedAudit = topicInfo.Displayorder == -2;

            #region 更新积分

            //设置用户的积分
            ///首先读取版块内自定义积分
            ///版设置了自定义积分则使用,否则使用论坛默认积分
            //float[] values = null;
            //if (!string.IsNullOrEmpty(forumInfo.Postcredits))
            //{
            //    int index = 0;
            //    float tempval = 0;
            //    values = new float[8];
            //    foreach (string ext in Utils.SplitString(forumInfo.Postcredits, ","))
            //    {
            //        if (index == 0)
            //        {
            //            if (!ext.Equals("True"))
            //            {
            //                values = null;
            //                break;
            //            }
            //            index++;
            //            continue;
            //        }
            //        tempval = Utils.StrToFloat(ext, 0);
            //        values[index - 1] = tempval;
            //        index++;
            //        if (index > 8)
            //            break;
            //    }
            //}
            if (userInfo.Adminid == 1 || !tcr.NeedAudit)
                CreditsFacade.PostTopic(userInfo.Uid, forumInfo);
                //TopicsCommandUtils.UpdateScore(userInfo.Uid, values);

            #endregion

            //同步到其他应用程序
            Sync.NewTopic(topicId.ToString(), topicInfo.Title, topicInfo.Poster, topicInfo.Posterid.ToString(), topicInfo.Fid.ToString(), commandParam.AppInfo.APIKey);

            result = commandParam.Format == FormatType.JSON ? JavaScriptConvert.SerializeObject(tcr) : SerializationHelper.Serialize(tcr);
            return true;
        }