Symbian.org/ForumPostForm.js
changeset 0 54498df70f5d
child 2 99bc8e56b756
equal deleted inserted replaced
-1:000000000000 0:54498df70f5d
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // (c)2009 Symbian Foundation
       
     3 // ////////////////////////////////////////////////////////////////////////////
       
     4 
       
     5 
       
     6 function ForumPostForm(aParentView, forumid) {
       
     7 	ListView.prototype.init.call(this, null, null);
       
     8 	this.previousView = aParentView;
       
     9 	this.forumid = forumid;
       
    10 	
       
    11 	// add the banner / 'title bar' - avoids the caption bug
       
    12 	var titleBar = new NavigationButton(null, "titlebar.png", "New thread in " + aParentView.feedName);
       
    13 	titleBar.setEnabled(false);
       
    14 	this.addControl(titleBar);
       
    15 	
       
    16 	// add topic name textfield
       
    17 	this.topicNameTf = new TextField('threadPostTopic', "Topic title", "");
       
    18 	this.addControl(this.topicNameTf);
       
    19 	
       
    20 	// add content textarea
       
    21 	this.contentTa = new TextArea('threadPostContent', "Message", "", 6);
       
    22 	this.addControl(this.contentTa);
       
    23 	
       
    24 	var self = this;
       
    25 
       
    26     // post button
       
    27     this.postButton = new FormButton(null, "Submit");
       
    28     this.postButton.addEventListener("ActionPerformed", function(){
       
    29 		login( function(){
       
    30 			submitNewTopic(
       
    31 				self.topicNameTf.getText(), // title
       
    32 				self.contentTa.getText(), // message
       
    33 				self.forumid, // forumid
       
    34 				function() { self.goBack();uiManager.currentView.update(true);}
       
    35 				);
       
    36 			});
       
    37 		});
       
    38     this.addControl(this.postButton);
       
    39     
       
    40     // cancel settings button
       
    41     this.cancelButton = new FormButton(null, "Cancel");
       
    42     this.cancelButton.addEventListener("ActionPerformed", function(){self.goBack();});
       
    43     this.addControl(this.cancelButton);
       
    44 	
       
    45 }
       
    46 
       
    47 ForumPostForm.prototype = new ListView(null, null);
       
    48 
       
    49 
       
    50 function ForumReplyForm(aParentView, threadid, postid, parentTitle) {
       
    51 	ListView.prototype.init.call(this, null, null);
       
    52 	this.previousView = aParentView;
       
    53 	this.threadid = threadid;
       
    54 	this.postid = postid;
       
    55 	this.parentTitle = parentTitle;
       
    56 	
       
    57 	// add the banner / 'title bar' - avoids the caption bug
       
    58 	var titleBar = new NavigationButton(null, "titlebar.png", "Reply to " + parentTitle);
       
    59 	titleBar.setEnabled(false);
       
    60 	this.addControl(titleBar);
       
    61 	
       
    62 	// add topic name textfield
       
    63 	this.topicNameTf = new TextField('threadPostTopic', "Title", "");
       
    64 	this.addControl(this.topicNameTf);
       
    65 	
       
    66 	// add content textarea
       
    67 	this.contentTa = new TextArea('threadPostContent', "Message", "", 6);
       
    68 	this.addControl(this.contentTa);
       
    69 	
       
    70 	var self = this;
       
    71 
       
    72     // post button
       
    73     this.postButton = new FormButton(null, "Submit");
       
    74     this.postButton.addEventListener("ActionPerformed", function(){
       
    75 		login(
       
    76 		function(){
       
    77 			submitNewReply(self.topicNameTf.getText(), // title
       
    78 			 self.contentTa.getText(), // message
       
    79 			 self.threadid, // threadid
       
    80 			 self.postid, // threadid
       
    81 			 function(){
       
    82 				self.goBack();
       
    83 				uiManager.currentView.update(true);
       
    84 			});
       
    85 		});
       
    86 	});
       
    87     this.addControl(this.postButton);
       
    88     
       
    89     // cancel settings button
       
    90     this.cancelButton = new FormButton(null, "Cancel");
       
    91     this.cancelButton.addEventListener("ActionPerformed", function(){self.goBack();});
       
    92     this.addControl(this.cancelButton);
       
    93 	
       
    94 }
       
    95 
       
    96 ForumReplyForm.prototype = new ListView(null, null);
       
    97 
       
    98 
       
    99 // Submitting a new to vBulletin is somewhat complex. There appears to be
       
   100 // no XML based interface so we have to go through the usual web posting
       
   101 // procedure. So, first we must be logged in. Then, we must request forums
       
   102 // home page to get bbsessionhash cookie. Next, we request the form,
       
   103 // to collect required security information (securitytoken etc) from the form.
       
   104 // If all goes well, we can now post a message.
       
   105 
       
   106 var submitUrlContent = null;
       
   107 var submitUrlHttpReq = null;
       
   108 var submitCallback = null;
       
   109 var submitTitle = null;
       
   110 var submitContent = null;
       
   111 var submitForumId = null;
       
   112 var submitThreadId = null;
       
   113 var submitPostId = null;
       
   114 var submitCallback = null;
       
   115 var reply = false;
       
   116 
       
   117 // Initiates the submission process by requesting the form
       
   118 function submitNewTopic(title, content, forumid, callback){
       
   119 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   120 
       
   121 	// Dealing with vBulletin nastiness...
       
   122 	
       
   123 	// ensure we have all the cookies we need
       
   124 	var vbCookieGet = new Ajax();
       
   125 	var vburl = symbianOrgBaseUrl + "/forum/";
       
   126 	vbCookieGet.open('GET', vburl, false);
       
   127 	vbCookieGet.send(null);
       
   128 
       
   129 	// Now we have to harvest some info from the post form. 	
       
   130 	submitUrlHttpReq = new Ajax();
       
   131 	var self = this;
       
   132 	submitTitle = title;
       
   133 	submitContent = content;
       
   134 	submitForumId = forumid;
       
   135 	submitThreadId = null;
       
   136 	submitPostId = null;
       
   137 	submitCallback = callback;
       
   138     submitUrlHttpReq.onreadystatechange = submitFormReady;
       
   139 	reply = false;
       
   140 	
       
   141 	var url = symbianOrgBaseUrl + "/forum/newthread.php?do=newthread&f=" + forumid;
       
   142 	submitUrlHttpReq.open('GET', url, true);
       
   143 	submitUrlHttpReq.send(null);
       
   144 }
       
   145 
       
   146 // Initiates the submission process by requesting the form
       
   147 function submitNewReply(title, content, threadid, postid, callback){
       
   148 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   149 	
       
   150 	// Dealing with vBulletin nastiness...
       
   151 	
       
   152 	// ensure we have all the cookies we need
       
   153 	var vbCookieGet = new Ajax();
       
   154 	var vburl = symbianOrgBaseUrl + "/forum/";
       
   155 	vbCookieGet.open('GET', vburl, false);
       
   156 	vbCookieGet.send(null);
       
   157 
       
   158 	// Now we have to harvest some info from the post form. 	
       
   159 	submitUrlHttpReq = new Ajax();
       
   160 	var self = this;
       
   161 	submitTitle = title;
       
   162 	submitContent = content;
       
   163 	submitForumId = null;
       
   164 	submitThreadId = threadid;
       
   165 	submitPostId = postid;
       
   166 	submitCallback = callback;
       
   167     submitUrlHttpReq.onreadystatechange = submitFormReady;
       
   168 	reply = true;
       
   169 	
       
   170 	var url = symbianOrgBaseUrl + "/forum/newreply.php?do=newreply&noquote=1&p=" + postid;
       
   171 	submitUrlHttpReq.open('GET', url, true);
       
   172 	submitUrlHttpReq.send(null);
       
   173 }
       
   174 
       
   175 
       
   176 var forumPostHarvestString_loggedinuser = "name=\"loggedinuser\" value=\"";
       
   177 var forumPostHarvestString_poststarttime = "name=\"poststarttime\" value=\"";		
       
   178 var forumPostHarvestString_posthash = "name=\"posthash\" value=\"";		
       
   179 var forumPostHarvestString_securitytoken = "name=\"securitytoken\" value=\"";		
       
   180 		
       
   181 // Form has been received, extract important info
       
   182 function submitFormReady(){
       
   183 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   184     if (submitUrlHttpReq.readyState == 4) {
       
   185         // attempt to get response status
       
   186         var responseStatus = null;
       
   187         try {
       
   188             responseStatus = submitUrlHttpReq.status;
       
   189         } catch (noStatusException) {}
       
   190         
       
   191 		
       
   192 		var content = submitUrlHttpReq.responseText;
       
   193 		checkForSecurityToken("submitFormReady", content);
       
   194 
       
   195 		// this is what we need to hardvest
       
   196 		var forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser;
       
   197 		
       
   198 		if ( content.indexOf(forumPostHarvestString_loggedinuser) == -1 ) {
       
   199 			uiManager.showNotification(5000, "warning", "Submit failed.");	
       
   200 		} else {
       
   201 			forumPostLoggedInUser = extractFormField(content, forumPostHarvestString_loggedinuser);
       
   202 			forumPostStartTime = extractFormField(content, forumPostHarvestString_poststarttime);
       
   203 			forumPostHash = extractFormField(content, forumPostHarvestString_posthash);
       
   204 			forumPostSecurityToken = extractFormField(content, forumPostHarvestString_securitytoken);
       
   205 			
       
   206 			if (forumPostSecurityToken == null || forumPostSecurityToken.length < 5) {
       
   207 			    // workaround for a vBulletin bug, restart the process...
       
   208 				login( function(){
       
   209 					if (reply) {
       
   210 						submitNewReply(submitTitle, // title
       
   211 						 submitContent, // message
       
   212 						 submitThreadId, // threadid
       
   213 						 submitPostId, // threadid
       
   214 						 submitCallback);
       
   215 					}
       
   216 					else {
       
   217 						submitNewTopic(submitTitle, // title
       
   218 							 submitContent, // message
       
   219 							 submitForumId, // forumid
       
   220 							 submitCallback);
       
   221 					}
       
   222 				});
       
   223 			} else {
       
   224 				doSubmitPost(submitTitle, submitContent, submitForumId, submitCallback, forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser);
       
   225 			}
       
   226 		}
       
   227     }
       
   228 }
       
   229 
       
   230 // Send a POST request with our post information
       
   231 function doSubmitPost(title, message, forumid, callback, 
       
   232 			forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser){
       
   233 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   234 	var url = null;
       
   235 	var parameters = null;
       
   236 	
       
   237 	if (reply) {
       
   238 		// posting a reply to an article
       
   239 		url = symbianOrgNewReplyUrl + "do=postreply&t=" + submitThreadId;
       
   240 		parameters = "title=" + title + "&message=" + message +
       
   241 		"&wysiwyg=0&iconid=0&s=&securitytoken=" + forumPostSecurityToken +
       
   242 		"&do=postreply" +
       
   243 		"&t=" + submitThreadId + "&p=" + submitPostId + 
       
   244 		"&specifiedpost=0" +
       
   245 		"&posthash" + forumPostHash +
       
   246 		"&poststarttime=" + forumPostStartTime +
       
   247 		"&loggedinuser=" + forumPostLoggedInUser +
       
   248 		"&multiquoteempty=&sbutton=Submit+Reply&parseurl=1&emailupdate=9999&rating=0";
       
   249 	} else {
       
   250 		// posting a new thread
       
   251 		url = symbianOrgNewThreadUrl + "do=postthread&f=" + forumid;
       
   252 		parameters = "do=postthread&f=" + forumid + "&subject=" + title + "&message=" + message +
       
   253 		"&wysiwyg=0&taglist=&iconid=0&s=&securitytoken=" + forumPostSecurityToken +
       
   254 		"&posthash" + forumPostHash +
       
   255 		"&poststarttime=" + forumPostStartTime +
       
   256 		"&loggedinuser=" + forumPostLoggedInUser +
       
   257 		"&sbutton=Submit+New+Thread&parseurl=1&emailupdate=9999&polloptions=4";
       
   258 	}
       
   259 	
       
   260 	submitUrlHttpReq = new Ajax();
       
   261     submitUrlHttpReq.onreadystatechange = submitComplete;
       
   262     // initiate the request
       
   263 	submitUrlHttpReq.open('POST', url, true);
       
   264 	submitUrlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       
   265 	submitUrlHttpReq.setRequestHeader("Content-length", parameters.length);
       
   266 	submitUrlHttpReq.setRequestHeader("Connection", "close");
       
   267 	submitUrlHttpReq.send(parameters);
       
   268 }
       
   269 
       
   270 // Response to our POST has been received, analyse the result
       
   271 function submitComplete(){
       
   272     if (submitUrlHttpReq.readyState == 4) {
       
   273 		// attempt to get response status
       
   274 		var responseStatus = null;
       
   275 		try {
       
   276 			responseStatus = submitUrlHttpReq.status;
       
   277 		} 
       
   278 		catch (noStatusException) {
       
   279 		}
       
   280 		var content = submitUrlHttpReq.responseText;
       
   281 		if ( content.indexOf(submitTitle) == -1 ) {
       
   282 			uiManager.showNotification(3000, "warning", "Posting failed.");	
       
   283 		} else {
       
   284 			uiManager.showNotification(3000, "warning", "Please wait...");	
       
   285 			if ( submitCallback != null ) {
       
   286 				submitCallback.call();
       
   287 			}
       
   288 		}
       
   289 	}	
       
   290 }
       
   291 
       
   292 // Test weather page HTML contains a login form. This is useful in
       
   293 // being able to tell weather a login has been successfull, or if
       
   294 // we received login prompt instead of XML at any point.
       
   295 function isLoginPrompt (text) {
       
   296 	return text.indexOf("form name=\"frmLogin\"") != -1;
       
   297 }
       
   298 
       
   299 // Stores the current view, then shows the settings dialog
       
   300 // so that once settings dialog is closed, we go back to current screen
       
   301 function promptForPassword() {
       
   302 		if (uiManager.currentView == settings) {
       
   303 			settings.previousView = home;
       
   304 		}
       
   305 		else {
       
   306 			settings.previousView = uiManager.currentView;
       
   307 		}
       
   308 		uiManager.hideNotification();
       
   309 		settings.show();
       
   310 }
       
   311 
       
   312 function extractFormField(content, harvestString){
       
   313 	var startind = content.indexOf(harvestString);
       
   314 	if ( startind == -1 ) {
       
   315 		return null;
       
   316 	}
       
   317 	startind += harvestString.length;
       
   318 	var endind = content.indexOf("\"", startind);
       
   319 	return content.substring(startind, endind);
       
   320 }
       
   321 
       
   322 function checkForSecurityToken(where, content) {
       
   323 //	var stpos = content.indexOf("securitytoken");
       
   324 //	if ( stpos == -1 ) {
       
   325 //		var test = content.substring(stpos , stpos + 100);
       
   326 //		alert("securityToken not found in " + where + " : "+ test);
       
   327 //	}
       
   328 }