Exemple #1
0
 private static bool IsSelectFieldType(string fieldType)
 {
     return(PollUtils.EqualsIgnoreCase(fieldType, InputType.CheckBox.Value) ||
            PollUtils.EqualsIgnoreCase(fieldType, InputType.Radio.Value) ||
            PollUtils.EqualsIgnoreCase(fieldType, InputType.SelectMultiple.Value) ||
            PollUtils.EqualsIgnoreCase(fieldType, InputType.SelectOne.Value));
 }
Exemple #2
0
        public IHttpActionResult Get()
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var siteId = request.GetQueryInt("siteId");
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(siteId, PollUtils.PluginId))
                {
                    return(Unauthorized());
                }

                var formInfoList = PollManager.GetPollInfoList(siteId, 0);

                var type             = request.GetQueryString("type");
                var name             = request.GetQueryString("name");
                var templateInfoList = TemplateManager.GetTemplateInfoList(type);
                var templateInfo     =
                    templateInfoList.FirstOrDefault(x => PollUtils.EqualsIgnoreCase(name, x.Name));

                return(Ok(new
                {
                    Value = templateInfo,
                    PollInfoList = formInfoList
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult Import()
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var pollInfo = PollManager.GetPollInfo(request);
                if (pollInfo == null)
                {
                    return(NotFound());
                }
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(pollInfo.SiteId, PollUtils.PluginId))
                {
                    return(Unauthorized());
                }

                foreach (string name in HttpContext.Current.Request.Files)
                {
                    var postFile = HttpContext.Current.Request.Files[name];

                    if (postFile == null)
                    {
                        return(BadRequest("Could not read zip from body"));
                    }

                    var filePath = Context.UtilsApi.GetTemporaryFilesPath("poll.zip");
                    PollUtils.DeleteFileIfExists(filePath);

                    if (!PollUtils.EqualsIgnoreCase(Path.GetExtension(postFile.FileName), ".zip"))
                    {
                        return(BadRequest("zip file extension is not correct"));
                    }

                    postFile.SaveAs(filePath);

                    var directoryPath = Context.UtilsApi.GetTemporaryFilesPath("poll");
                    PollUtils.DeleteDirectoryIfExists(directoryPath);
                    Context.UtilsApi.ExtractZip(filePath, directoryPath);

                    var isHistoric = PollBox.IsHistoric(directoryPath);
                    PollBox.ImportFields(pollInfo.SiteId, pollInfo.Id, directoryPath, isHistoric);

                    //FieldManager.Import(pollInfo.SiteId, pollInfo.Id, filePath);
                }

                return(Ok(new
                {
                    Value = true
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #4
0
        public IHttpActionResult Clone()
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var siteId = request.GetQueryInt("siteId");
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(siteId, PollUtils.PluginId))
                {
                    return(Unauthorized());
                }

                var type         = request.GetQueryString("type");
                var originalName = request.GetPostString("originalName");
                var name         = request.GetPostString("name");
                var description  = request.GetPostString("description");
                var templateHtml = request.GetPostString("templateHtml");

                var templateInfoList     = TemplateManager.GetTemplateInfoList(type);
                var originalTemplateInfo = templateInfoList.First(x => PollUtils.EqualsIgnoreCase(originalName, x.Name));

                if (templateInfoList.Any(x => PollUtils.EqualsIgnoreCase(name, x.Name)))
                {
                    return(BadRequest($"标识为 {name} 的模板已存在,请更换模板标识!"));
                }

                var templateInfo = new TemplateInfo
                {
                    Name        = name,
                    Main        = originalTemplateInfo.Main,
                    Publisher   = string.Empty,
                    Description = description,
                    Icon        = originalTemplateInfo.Icon
                };
                templateInfoList.Add(templateInfo);

                TemplateManager.Clone(originalName, templateInfo, templateHtml);

                return(Ok(new
                {
                    Value = true
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #5
0
        public IHttpActionResult Submit(int pollId)
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var pollInfo = PollManager.Repository.GetPollInfo(pollId);
                if (pollInfo == null)
                {
                    return(null);
                }

                if (pollInfo.IsCaptcha)
                {
                    var code        = request.GetPostString("code");
                    var cookie      = HttpContext.Current.Request.Cookies.Get("ss-poll:" + pollId);
                    var cookieValue = cookie?.Value;
                    if (string.IsNullOrEmpty(cookieValue) || !PollUtils.EqualsIgnoreCase(cookieValue, code))
                    {
                        throw new Exception("提交失败,验证码不正确!");
                    }
                }

                var itemIds = request.GetPostString("itemIds").Split(',').Select(idStr => Convert.ToInt32(idStr)).ToList();
                if (pollInfo.IsCheckbox)
                {
                    if (pollInfo.CheckboxMin > 0 && itemIds.Count < pollInfo.CheckboxMin)
                    {
                        throw new Exception($"提交失败,最少需要选择{pollInfo.CheckboxMin}项!");
                    }
                    if (pollInfo.CheckboxMax > 0 && itemIds.Count > pollInfo.CheckboxMax)
                    {
                        throw new Exception($"提交失败,最多只能选择{pollInfo.CheckboxMax}项!");
                    }
                }

                var logInfo = new LogInfo
                {
                    PollId  = pollInfo.Id,
                    ItemIds = string.Join(",", itemIds),
                    AddDate = DateTime.Now
                };

                var attributes = request.GetPostObject <Dictionary <string, string> >("attributes");

                var fieldInfoList = FieldManager.GetFieldInfoList(pollInfo.Id);
                foreach (var fieldInfo in fieldInfoList)
                {
                    string value;
                    attributes.TryGetValue(fieldInfo.Title, out value);
                    logInfo.Set(fieldInfo.Title, value);
                }

                LogManager.Repository.Insert(pollInfo, logInfo);

                ItemManager.Repository.AddCount(pollInfo.Id, itemIds);

                var itemInfoList = ItemManager.GetItemInfoList(pollInfo.Id);
                var totalCount   = itemInfoList.Sum(x => x.Count);

                var items = new List <object>();
                foreach (var itemInfo in itemInfoList)
                {
                    var percentage = "0%";
                    if (totalCount > 0)
                    {
                        percentage = Convert.ToDouble(itemInfo.Count / (double)totalCount).ToString("0.0%");
                    }
                    items.Add(new
                    {
                        itemInfo.Id,
                        itemInfo.PollId,
                        itemInfo.Title,
                        itemInfo.SubTitle,
                        itemInfo.ImageUrl,
                        itemInfo.Count,
                        Percentage = percentage
                    });
                }

                return(Ok(new
                {
                    TotalCount = totalCount,
                    Items = items
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #6
0
        public IHttpActionResult Submit()
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var pollInfo = PollManager.GetPollInfo(request);
                if (pollInfo == null)
                {
                    return(NotFound());
                }
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(pollInfo.SiteId, PollUtils.PluginId))
                {
                    return(Unauthorized());
                }

                var type = request.GetPostString("type");
                if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsClosed)))
                {
                    pollInfo.IsClosed = request.GetPostBool(nameof(PollInfo.IsClosed));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.Title)))
                {
                    pollInfo.Title = request.GetPostString(nameof(PollInfo.Title));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.Description)))
                {
                    pollInfo.Description = request.GetPostString(nameof(PollInfo.Description));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsImage)))
                {
                    pollInfo.IsImage = request.GetPostBool(nameof(PollInfo.IsImage));
                    pollInfo.IsUrl   = request.GetPostBool(nameof(PollInfo.IsUrl));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsCheckbox)))
                {
                    pollInfo.IsCheckbox  = request.GetPostBool(nameof(PollInfo.IsCheckbox));
                    pollInfo.CheckboxMin = PollUtils.ToInt(request.GetPostString(nameof(PollInfo.CheckboxMin)));
                    pollInfo.CheckboxMax = PollUtils.ToInt(request.GetPostString(nameof(PollInfo.CheckboxMax)));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsTimeout)))
                {
                    pollInfo.IsTimeout   = request.GetPostBool(nameof(PollInfo.IsTimeout));
                    pollInfo.TimeToStart = PollUtils.ToDateTime(request.GetPostString(nameof(PollInfo.TimeToStart)));
                    pollInfo.TimeToEnd   = PollUtils.ToDateTime(request.GetPostString(nameof(PollInfo.TimeToEnd)));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsCaptcha)))
                {
                    pollInfo.IsCaptcha = request.GetPostBool(nameof(PollInfo.IsCaptcha));
                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsAdministratorSmsNotify)))
                {
                    pollInfo.IsAdministratorSmsNotify     = request.GetPostBool(nameof(PollInfo.IsAdministratorSmsNotify));
                    pollInfo.AdministratorSmsNotifyTplId  = request.GetPostString(nameof(PollInfo.AdministratorSmsNotifyTplId));
                    pollInfo.AdministratorSmsNotifyKeys   = request.GetPostString(nameof(PollInfo.AdministratorSmsNotifyKeys));
                    pollInfo.AdministratorSmsNotifyMobile = request.GetPostString(nameof(PollInfo.AdministratorSmsNotifyMobile));

                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsAdministratorMailNotify)))
                {
                    pollInfo.IsAdministratorMailNotify      = request.GetPostBool(nameof(PollInfo.IsAdministratorMailNotify));
                    pollInfo.AdministratorMailNotifyAddress = request.GetPostString(nameof(PollInfo.AdministratorMailNotifyAddress));

                    PollManager.Repository.Update(pollInfo);
                }
                else if (PollUtils.EqualsIgnoreCase(type, nameof(PollInfo.IsUserSmsNotify)))
                {
                    pollInfo.IsUserSmsNotify         = request.GetPostBool(nameof(PollInfo.IsUserSmsNotify));
                    pollInfo.UserSmsNotifyTplId      = request.GetPostString(nameof(PollInfo.UserSmsNotifyTplId));
                    pollInfo.UserSmsNotifyKeys       = request.GetPostString(nameof(PollInfo.UserSmsNotifyKeys));
                    pollInfo.UserSmsNotifyMobileName = request.GetPostString(nameof(PollInfo.UserSmsNotifyMobileName));

                    PollManager.Repository.Update(pollInfo);
                }

                return(Ok(new { }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemple #7
0
        public static void SendNotify(PollInfo pollInfo, List <FieldInfo> fieldInfoList, LogInfo logInfo)
        {
            if (pollInfo.IsAdministratorSmsNotify &&
                !string.IsNullOrEmpty(pollInfo.AdministratorSmsNotifyTplId) &&
                !string.IsNullOrEmpty(pollInfo.AdministratorSmsNotifyMobile))
            {
                var smsPlugin = Context.PluginApi.GetPlugin <SMS.Plugin>();
                if (smsPlugin != null && smsPlugin.IsReady)
                {
                    var parameters = new Dictionary <string, string>();
                    if (!string.IsNullOrEmpty(pollInfo.AdministratorSmsNotifyKeys))
                    {
                        var keys = pollInfo.AdministratorSmsNotifyKeys.Split(',');
                        foreach (var key in keys)
                        {
                            if (PollUtils.EqualsIgnoreCase(key, nameof(LogInfo.Id)))
                            {
                                parameters.Add(key, logInfo.Id.ToString());
                            }
                            else if (PollUtils.EqualsIgnoreCase(key, nameof(LogInfo.AddDate)))
                            {
                                if (logInfo.AddDate.HasValue)
                                {
                                    parameters.Add(key, logInfo.AddDate.Value.ToString("yyyy-MM-dd HH:mm"));
                                }
                            }
                            else
                            {
                                var value     = string.Empty;
                                var fieldInfo =
                                    fieldInfoList.FirstOrDefault(x => PollUtils.EqualsIgnoreCase(key, x.Title));
                                if (fieldInfo != null)
                                {
                                    value = LogManager.GetValue(fieldInfo, logInfo);
                                }

                                parameters.Add(key, value);
                            }
                        }
                    }

                    smsPlugin.Send(pollInfo.AdministratorSmsNotifyMobile,
                                   pollInfo.AdministratorSmsNotifyTplId, parameters, out _);
                }
            }

            if (pollInfo.IsAdministratorMailNotify &&
                !string.IsNullOrEmpty(pollInfo.AdministratorMailNotifyAddress))
            {
                var mailPlugin = Context.PluginApi.GetPlugin <Mail.Plugin>();
                if (mailPlugin != null && mailPlugin.IsReady)
                {
                    var templateHtml = MailTemplateManager.GetTemplateHtml();
                    var listHtml     = MailTemplateManager.GetListHtml();

                    var keyValueList = new List <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>("编号", logInfo.Guid)
                    };
                    if (logInfo.AddDate.HasValue)
                    {
                        keyValueList.Add(new KeyValuePair <string, string>("提交时间", logInfo.AddDate.Value.ToString("yyyy-MM-dd HH:mm")));
                    }
                    foreach (var fieldInfo in fieldInfoList)
                    {
                        keyValueList.Add(new KeyValuePair <string, string>(fieldInfo.Title,
                                                                           LogManager.GetValue(fieldInfo, logInfo)));
                    }

                    var list = new StringBuilder();
                    foreach (var kv in keyValueList)
                    {
                        list.Append(listHtml.Replace("{{key}}", kv.Key).Replace("{{value}}", kv.Value));
                    }

                    var siteInfo = Context.SiteApi.GetSiteInfo(pollInfo.SiteId);

                    mailPlugin.Send(pollInfo.AdministratorMailNotifyAddress, string.Empty,
                                    "[SiteServer CMS] 通知邮件",
                                    templateHtml.Replace("{{title}}", $"{pollInfo.Title} - {siteInfo.SiteName}").Replace("{{list}}", list.ToString()), out _);
                }
            }

            if (pollInfo.IsUserSmsNotify &&
                !string.IsNullOrEmpty(pollInfo.UserSmsNotifyTplId) &&
                !string.IsNullOrEmpty(pollInfo.UserSmsNotifyMobileName))
            {
                var smsPlugin = Context.PluginApi.GetPlugin <SMS.Plugin>();
                if (smsPlugin != null && smsPlugin.IsReady)
                {
                    var parameters = new Dictionary <string, string>();
                    if (!string.IsNullOrEmpty(pollInfo.UserSmsNotifyKeys))
                    {
                        var keys = pollInfo.UserSmsNotifyKeys.Split(',');
                        foreach (var key in keys)
                        {
                            if (PollUtils.EqualsIgnoreCase(key, nameof(LogInfo.Id)))
                            {
                                parameters.Add(key, logInfo.Id.ToString());
                            }
                            else if (PollUtils.EqualsIgnoreCase(key, nameof(LogInfo.AddDate)))
                            {
                                if (logInfo.AddDate.HasValue)
                                {
                                    parameters.Add(key, logInfo.AddDate.Value.ToString("yyyy-MM-dd HH:mm"));
                                }
                            }
                            else
                            {
                                var value     = string.Empty;
                                var fieldInfo =
                                    fieldInfoList.FirstOrDefault(x => PollUtils.EqualsIgnoreCase(key, x.Title));
                                if (fieldInfo != null)
                                {
                                    value = LogManager.GetValue(fieldInfo, logInfo);
                                }

                                parameters.Add(key, value);
                            }
                        }
                    }

                    var mobileFieldInfo = fieldInfoList.FirstOrDefault(x => PollUtils.EqualsIgnoreCase(pollInfo.UserSmsNotifyMobileName, x.Title));
                    if (mobileFieldInfo != null)
                    {
                        var mobile = LogManager.GetValue(mobileFieldInfo, logInfo);
                        if (!string.IsNullOrEmpty(mobile))
                        {
                            smsPlugin.Send(mobile, pollInfo.UserSmsNotifyTplId, parameters, out _);
                        }
                    }
                }
            }
        }
        public IHttpActionResult GetTemplateInfo()
        {
            try
            {
                var request = Context.AuthenticatedRequest;

                var siteId = request.GetQueryInt("siteId");
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(siteId, PollUtils.PluginId))
                {
                    return(Unauthorized());
                }

                var type         = request.GetQueryString("type");
                var name         = request.GetQueryString("name");
                var templateInfo = TemplateManager.GetTemplateInfo(name);
                var html         = TemplateManager.GetTemplateHtml(templateInfo);

                var isSystem = TemplateManager.GetTemplateInfoList(type).Any(x => !string.IsNullOrEmpty(x.Publisher) && PollUtils.EqualsIgnoreCase(name, x.Name));
                if (isSystem)
                {
                    templateInfo = new TemplateInfo();
                }

                return(Ok(new
                {
                    Value = templateInfo,
                    TemplateHtml = html,
                    IsSystem = isSystem
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public static List <TemplateInfo> GetTemplateInfoList(string type)
        {
            var templateInfoList = new List <TemplateInfo>();

            var directoryPath  = GetTemplatesDirectoryPath();
            var directoryNames = PollUtils.GetDirectoryNames(directoryPath);

            foreach (var directoryName in directoryNames)
            {
                var templateInfo = GetTemplateInfo(directoryPath, directoryName);
                if (templateInfo == null)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(type) && string.IsNullOrEmpty(templateInfo.Type) || PollUtils.EqualsIgnoreCase(type, templateInfo.Type))
                {
                    templateInfoList.Add(templateInfo);
                }
            }

            return(templateInfoList);
        }