Ejemplo n.º 1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         if (!Page.IsPostBack)
         {
             OpenFormController ctrl = new OpenFormController();
             var data = ctrl.GetContents(ModuleId).OrderByDescending(c=> c.CreatedOnDate);
             var dynData = new List<dynamic>();
             foreach (var item in data)
             {
                 dynamic o = new ExpandoObject();
                 var dict = (IDictionary<string, object>)o;
                 o.CreatedOnDate = item.CreatedOnDate;
                 o.Json = item.Json;
                 dynamic d = JsonUtils.JsonToDynamic(item.Json);
                 //o.Data = d;
                 Dictionary<String, Object> jdic = Dyn2Dict(d);
                 foreach (var p in jdic)
                 {
                     dict[p.Key] = p.Value;
                 }
                 dynData.Add(o);
             }
             gvData.DataSource = ToDataTable(dynData);
             gvData.DataBind();
         }
     }
     catch (Exception exc) //Module failed to load
     {
         Exceptions.ProcessModuleLoadException(this, exc);
     }
 }
Ejemplo n.º 2
0
        public HttpResponseMessage Submit()
        {
            var form     = JObject.Parse(HttpContextSource.Current.Request.Form["data"].ToString());
            var statuses = new List <FilesStatus>();

            try
            {
                //todo can we eliminate the HttpContext here
                UploadWholeFile(HttpContextSource.Current, statuses);
                var files = new JArray();
                form["Files"] = files;
                int i = 1;
                foreach (var item in statuses)
                {
                    var file = new JObject();
                    file["name"] = item.name;
                    file["url"]  = OpenFormUtils.ToAbsoluteUrl(item.url);
                    files.Add(file);
                    //form["File"+i] = OpenFormUtils.ToAbsoluteUrl(item.url);
                    i++;
                }
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
            }

            try
            {
                form["IPAddress"] = Request.GetIPAddress();
                int moduleId            = ActiveModule.ModuleID;
                OpenFormController ctrl = new OpenFormController();
                var content             = new OpenFormInfo()
                {
                    ModuleId             = moduleId,
                    Json                 = form.ToString(),
                    CreatedByUserId      = UserInfo.UserID,
                    CreatedOnDate        = DateTime.Now,
                    LastModifiedByUserId = UserInfo.UserID,
                    LastModifiedOnDate   = DateTime.Now,
                    Html                 = "",
                    Title                = "Form submitted - " + DateTime.Now.ToString()
                };
                ctrl.AddContent(content);
                var res = new ResultDTO()
                {
                    Message = "Form submitted."
                };
                string template    = (string)ActiveModule.ModuleSettings["template"];
                var    razorscript = new FileUri(Path.GetDirectoryName(template), "aftersubmit.cshtml");
                res.AfterSubmit = razorscript.FileExists;

                string jsonSettings = ActiveModule.ModuleSettings["data"] as string;
                if (!string.IsNullOrEmpty(jsonSettings))
                {
                    SettingsDTO      settings = JsonConvert.DeserializeObject <SettingsDTO>(jsonSettings);
                    HandlebarsEngine hbs      = new HandlebarsEngine();
                    dynamic          data     = null;
                    string           formData = "";
                    if (form != null)
                    {
                        if (!string.IsNullOrEmpty(settings.Settings.SiteKey))
                        {
                            Recaptcha recaptcha = new Recaptcha(settings.Settings.SiteKey, settings.Settings.SecretKey);
                            RecaptchaValidationResult validationResult = recaptcha.Validate(form["recaptcha"].ToString());
                            if (!validationResult.Succeeded)
                            {
                                return(Request.CreateResponse(HttpStatusCode.Forbidden));
                            }
                            form.Remove("recaptcha");
                        }
                        string  templateFilename = HostingEnvironment.MapPath("~/" + template);
                        string  schemaFilename   = Path.GetDirectoryName(templateFilename) + "\\" + "schema.json";
                        JObject schemaJson       = JsonUtils.GetJsonFromFile(schemaFilename);
                        //form["schema"] = schemaJson;
                        // default options
                        string  optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options.json";
                        JObject optionsJson     = null;
                        if (File.Exists(optionsFilename))
                        {
                            string fileContent = File.ReadAllText(optionsFilename);
                            if (!string.IsNullOrWhiteSpace(fileContent))
                            {
                                optionsJson = JObject.Parse(fileContent);
                                //form["options"] = optionsJson;
                            }
                        }
                        // language options
                        optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options." + DnnLanguageUtils.GetCurrentCultureCode() + ".json";
                        if (File.Exists(optionsFilename))
                        {
                            string fileContent = File.ReadAllText(optionsFilename);
                            if (!string.IsNullOrWhiteSpace(fileContent))
                            {
                                optionsJson = JObject.Parse(fileContent);
                                //form["options"] = optionsJson;
                            }
                        }
                        var enhancedForm = form.DeepClone() as JObject;
                        OpenFormUtils.ResolveLabels(enhancedForm, schemaJson, optionsJson);
                        data = OpenFormUtils.GenerateFormData(enhancedForm.ToString(), out formData);
                    }

                    if (settings != null && settings.Notifications != null)
                    {
                        foreach (var notification in settings.Notifications)
                        {
                            try
                            {
                                MailAddress from  = FormUtils.GenerateMailAddress(notification.From, notification.FromEmail, notification.FromName, notification.FromEmailField, notification.FromNameField, form);
                                MailAddress to    = FormUtils.GenerateMailAddress(notification.To, notification.ToEmail, notification.ToName, notification.ToEmailField, notification.ToNameField, form);
                                MailAddress reply = null;
                                if (!string.IsNullOrEmpty(notification.ReplyTo))
                                {
                                    reply = FormUtils.GenerateMailAddress(notification.ReplyTo, notification.ReplyToEmail, notification.ReplyToName, notification.ReplyToEmailField, notification.ReplyToNameField, form);
                                }
                                string body = formData;
                                if (!string.IsNullOrEmpty(notification.EmailBody))
                                {
                                    body = hbs.Execute(notification.EmailBody, data);
                                }
                                string subject = notification.EmailSubject;
                                if (!string.IsNullOrEmpty(notification.EmailSubject))
                                {
                                    subject = hbs.Execute(notification.EmailSubject, data);
                                }
                                var attachements = new List <Attachment>();
                                foreach (var item in statuses)
                                {
                                    var file = FileManager.Instance.GetFile(item.id);
                                    attachements.Add(new Attachment(FileManager.Instance.GetFileContent(file), item.name));
                                }
                                string send = FormUtils.SendMail(from.ToString(), to.ToString(), (reply == null ? "" : reply.ToString()), subject, body, attachements);
                                if (!string.IsNullOrEmpty(send))
                                {
                                    res.Errors.Add("From:" + from.ToString() + " - To:" + to.ToString() + " - " + send);
                                }
                            }
                            catch (Exception exc)
                            {
                                res.Errors.Add("Notification " + (settings.Notifications.IndexOf(notification) + 1) + " : " + exc.Message + " - " + (UserInfo.IsSuperUser ? exc.StackTrace : ""));
                                Log.Logger.Error(exc);
                            }
                        }
                    }
                    if (settings != null && settings.Settings != null)
                    {
                        if (!string.IsNullOrEmpty(settings.Settings.Message))
                        {
                            res.Message = hbs.Execute(settings.Settings.Message, data);
                        }
                        else
                        {
                            res.Message = "Message sent.";
                        }
                        res.Tracking = settings.Settings.Tracking;
                        if (!string.IsNullOrEmpty(settings.Settings.Tracking))
                        {
                            //res.RedirectUrl = Globals.NavigateURL(ActiveModule.TabID, "", "result=" + content.ContentId);
                        }
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
        public HttpResponseMessage Submit(JObject form)
        {
            try
            {
                var res = new ResultDTO()
                {
                    Message = "Form submitted."
                };

                int ModuleId = ActiveModule.ModuleID;
                string jsonSettings = ActiveModule.ModuleSettings["data"] as string;
                if (!string.IsNullOrEmpty(jsonSettings))
                {
                    HandlebarsEngine hbs = new HandlebarsEngine();
                    SettingsDTO settings = JsonConvert.DeserializeObject<SettingsDTO>(jsonSettings);
                    StringBuilder FormData = new StringBuilder();
                    if (form != null)
                    {
                        FormData.Append("<table boder=\"1\">");
                        foreach (var item in form.Properties())
                        {
                            FormData.Append("<tr>").Append("<td>").Append(item.Name).Append("</td>").Append("<td>").Append(" : ").Append("</td>").Append("<td>").Append(item.Value).Append("</td>").Append("</tr>");
                        }
                        FormData.Append("</table>");
                        //form["FormData"] = FormData.ToString();
                    }
                    if (settings != null && settings.Notifications != null)
                    {
                        foreach (var notification in settings.Notifications)
                        {
                            try
                            {
                                MailAddress from = GenerateMailAddress(notification.From, notification.FromEmail, notification.FromName, notification.FromEmailField, notification.FromNameField, form);
                                MailAddress to = GenerateMailAddress(notification.To, notification.ToEmail, notification.ToName, notification.ToEmailField, notification.ToNameField, form);
                                MailAddress reply = null;
                                if (!string.IsNullOrEmpty(notification.ReplyTo))
                                {
                                    reply = GenerateMailAddress(notification.ReplyTo, notification.ReplyToEmail, notification.ReplyToName, notification.ReplyToEmailField, notification.ReplyToNameField, form);
                                }
                                string body = FormData.ToString();
                                if (!string.IsNullOrEmpty(notification.EmailBody))
                                {
                                    body = hbs.Execute(notification.EmailBody, form);
                                }

                                string send = SendMail(from.ToString(), to.ToString(), (reply == null ? "" : reply.ToString()), notification.EmailSubject, body);
                                if (!string.IsNullOrEmpty(send))
                                {
                                    res.Errors.Add("From:" + from.ToString() + " - To:" + to.ToString() + " - " + send);
                                }
                            }
                            catch (Exception exc)
                            {
                                res.Errors.Add("Notification "+(settings.Notifications.IndexOf(notification)+1)+ " : " + exc.Message + " - " + (UserInfo.IsSuperUser ? exc.StackTrace : ""));
                                Logger.Error(exc);
                            }
                        }
                    }
                    if (settings != null && settings.Settings != null)
                    {
                        res.Message = hbs.Execute(settings.Settings.Message, form);
                        res.Tracking = settings.Settings.Tracking;
                    }
                }
                OpenFormController ctrl = new OpenFormController();
                var content = new OpenFormInfo()
                {
                    ModuleId = ModuleId,
                    Json = form.ToString(),
                    CreatedByUserId = UserInfo.UserID,
                    CreatedOnDate = DateTime.Now,
                    LastModifiedByUserId = UserInfo.UserID,
                    LastModifiedOnDate = DateTime.Now,
                    Html = "",
                    Title = "Form submitted - " + DateTime.Now.ToString()
                };
                ctrl.AddContent(content);
                return Request.CreateResponse(HttpStatusCode.OK, res);
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }