using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; using System.Collections; public partial class _Default : System.Web.UI.Page { private string pollid = ""; //id for the poll private string title = ""; //title of the poll private string IP = ""; //IP of the submitter private int timestamp = 0; //when submitted private Hashtable questiontypes; //associates questions w/ the typ they are (radio, text, etc) private XmlDocument doc; //holds XML data of surveyresults.xml protected void Page_Load(object sender, EventArgs e) { string thanks = Request.Form["thanks"]; //location of thank redirect string[] questions; //string array of the questions string[] types; //string array of the question types doc = new XmlDocument(); //Grab the poll id, title, and the submitter's IP and time of submission pollid = Request.Form["id"]; title = Request.Form["title"]; IP = Request.ServerVariables["REMOTE_ADDR"]; TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); timestamp = (int)t.TotalSeconds; if (thanks[0] != '/') { thanks = '/' + thanks; } //Associate questions with their type (differentiates text and multiple choice) questions = (Request.Form["inputnames"]).Split(','); types = (Request.Form["inputvalues"]).Split(','); questiontypes = new Hashtable(); for (int i = 0; i < questions.Length; i++) { questiontypes[questions[i]] = types[i]; } //If file exists load it, else build from scratch if (System.IO.File.Exists("surveyresults.xml")) { doc.Load("surveyresults.xml"); } else { doc.AppendChild(doc.CreateElement("survey")); } //Start at the root node XmlNode node = doc.SelectSingleNode("//survey"); if (!entryExists(node)) { //Add users response to the XML file addResponse(node); //Write updated XML out to file System.IO.TextWriter writer = new System.IO.StreamWriter("surveyresults.xml", false); System.IO.StringWriter sw = new System.IO.StringWriter(); System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw); node.WriteTo(xw); writer.Write(sw.ToString()); writer.Close(); //Write out updated XML to CMS outputToCMS(); } //Go to 'Thank You' page Response.Redirect("http://" + Request.ServerVariables["SERVER_NAME"] + thanks); } //Adds a response to the current poll private void addResponse(XmlNode node) { XmlNode poll = null; //Tries to find if the current poll exists in XML. If not, make new node try { poll = doc.SelectSingleNode("//poll[@id=\"" + pollid + "\"]"); } catch { main.Text += "In function addResponse: Selecting poll threw an exception"; return; } if (poll == null) { poll = doc.CreateElement("poll"); } //Build poll attributes XmlAttribute id = doc.CreateAttribute("id"); id.Value = pollid; XmlAttribute t = doc.CreateAttribute("title"); t.Value = title; poll.Attributes.Append(id); poll.Attributes.Append(t); node.AppendChild(poll); //Build response IP and timestamp XmlElement response = doc.CreateElement("response"); XmlElement ip = doc.CreateElement("IP"); ip.AppendChild(doc.CreateTextNode(IP)); XmlElement time = doc.CreateElement("timestamp"); time.AppendChild(doc.CreateTextNode(timestamp.ToString())); response.AppendChild(ip); response.AppendChild(time); //Build multiple choice and text nodes buildQuestionNodes(response); //Attach response to poll poll.AppendChild(response); } //Adds the questions and their answers to the response private void buildQuestionNodes(XmlNode response) { XmlNode mc = doc.CreateElement("multiplechoice"); XmlNode tex = doc.CreateElement("text"); foreach (string key in Request.Form.Keys) { //Skip the hidden inputs if (key != "thanks" && key != "inputnames" && key != "inputvalues" && key != "id" && key != "title") { //Create question node with the answer as its value XmlElement q = doc.CreateElement(key); q.AppendChild(doc.CreateTextNode(Request.Form[key])); //If it's multiple choice, grab (or make) multiple choice node, then attach question with answer if (((string)questiontypes[key]) == "radio" || ((string)questiontypes[key]) == "checkbox" || ((string)questiontypes[key]) == "dropdown") { try { mc = response.SelectSingleNode("//multiplechoice"); } catch { main.Text += "Selecting 'multiplechoice' node threw an exception"; return; } if (mc == null) { mc = doc.CreateElement("multiplechoice"); } mc.AppendChild(q); response.AppendChild(mc); } //It's a text answer, add it to the text node else { try { tex = response.SelectSingleNode("//text"); } catch { main.Text += "Selecting 'text' node threw an exception"; return; } if (tex == null) { tex = doc.CreateElement("text"); } tex.AppendChild(q); response.AppendChild(tex); } } } } //Checks if user submitted within the last 24 hrs. If so, don't add the response to the XML private bool entryExists(XmlNode node) { XmlNode n; //Find the current poll try { n = node.SelectSingleNode("//poll[@id=\"" + pollid + "\"]"); } catch { main.Text += "In function entryExists: Selecting poll threw an exception"; return false; } if (n == null) { return false; } //Loop through responses. If IP matches and timestamp difference is < 24 hrs, break out foreach (XmlNode r in n.ChildNodes) { if (r.SelectSingleNode("IP").ChildNodes[0].Value == IP) { if (timestamp - Convert.ToInt32(r.SelectSingleNode("timestamp").ChildNodes[0].Value) < 86400) return true; } } return false; } //Write out to the CMS private void outputToCMS() { string CMSURL; //URL of the CMS string cmsdirectory; //where in CMS to write to string username; //for login to CMS string password; //for login to CMS string data; //data to submit //Load configuration settings if (System.IO.File.Exists("survey-config.xml")) { XmlDocument d = new XmlDocument(); d.Load("survey-config.xml"); CMSURL = d.SelectSingleNode("//CMSURL").ChildNodes[0].Value; cmsdirectory = d.SelectSingleNode("//cmsdirectory").ChildNodes[0].Value; username = d.SelectSingleNode("//username").ChildNodes[0].Value; password = d.SelectSingleNode("//password").ChildNodes[0].Value; } else { main.Text += "Could not find config file"; return; } //Convert XML data to a string System.IO.StringWriter sw = new System.IO.StringWriter(); System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw); doc.WriteTo(xw); data = sw.ToString(); //Set up WSDL object for SOAP call wsdl.AssetOperationHandlerService service = new wsdl.AssetOperationHandlerService(); service.Url = CMSURL + "/ws/services/AssetOperationService?wsdl"; wsdl.authentication auth = new wsdl.authentication(); auth.username = username; auth.password = password; wsdl.xmlBlock block = new wsdl.xmlBlock(); block.name = "survey-results"; block.parentFolderPath = cmsdirectory; block.path = cmsdirectory + "/survey-results"; block.metadataSetPath = "/Default"; block.xml = data; wsdl.asset asset = new wsdl.asset(); asset.xmlBlock = block; wsdl.operationResult result = new wsdl.operationResult(); //Attempt to edit first, then create if it doesn't exist try { result = service.edit(auth, asset); if (result.success == "false") { block.path = ""; result = service.create(auth, asset); } } catch { main.Text += "Block creation/edit request failed to conform to the WSDL.\n" + result.message; } } }