Using coldfusion to log into a phpBB system
Categories:
Forums / Message Boards
I wrote some code a few months ago that used coldfusion to log into a phpBB system and send a private message to a specified user.
I thought it might be nice to share, so here's the code:
<cfset from_username = "thecaniac">
<cfset from_password="******">
<cfset phpbb_root = "http://forums.hurricanes.com"><!-- no trailing slash -->
<cfparam name="form.recipient" default="thecaniac">
<cfsavecontent variable="msg">
This is an example message
Neat!
</cfsavecontent>
<!--- start php session to get original cookies --->
<cfhttp url="#phpbb_root#/index.php" method="GET" useragent="#CGI.HTTP_USER_AGENT#"></cfhttp>
<cfset headers = cfhttp.header>
<cfset cookies = getCookies(cfhttp.header)>
<!--- perform login --->
<cfhttp url="#phpbb_root#/login.php" method="POST" useragent="#CGI.HTTP_USER_AGENT#">
<cfloop from="1" to="#arrayLen(cookies)#" step="1" index="i">
<cfhttpparam type="cookie" name="#cookies[i].NAME#" value="#cookies[i].VALUE#">
</cfloop>
<cfhttpparam type="FORMFIELD" name="username" value="#from_username#">
<cfhttpparam type="FORMFIELD" name="password" value="#from_password#">
<cfhttpparam type="FORMFIELD" name="autologin" value="">
<cfhttpparam type="FORMFIELD" name="redirect" value="">
<cfhttpparam type="FORMFIELD" name="login" value="Log in">
</cfhttp>
<!--- get updated cookies --->
<cfset cookiesAuthenticated = getCookies(cfhttp.header)>
<cfhttp url="#phpbb_root#/privmsg.php" method="POST" useragent="#CGI.HTTP_USER_AGENT#">
<cfloop from="1" to="#arrayLen(cookiesAuthenticated)#" step="1" index="i">
<cfhttpparam type="cookie" name="#cookiesAuthenticated[i].NAME#" value="#cookies[i].VALUE#">
</cfloop>
<Cfhttpparam type="formfield" name="username" value="#from_username#" />
<Cfhttpparam type="formfield" name="subject" value="Your access key to the Caniac's Chat Room"/>
<Cfhttpparam type="formfield" name="message" value="#msg#">
<!---<Cfhttpparam type="formfield" name="preview" value="Preview" />--->
<Cfhttpparam type="formfield" name="post" value="Submit" />
</cfhttp>
<cfset results = cfhttp.filecontent>
<cfif findnocase("Your message has been sent",results) gt 0>
Message Sent!
<cfelse>
Message not sent!
</cfif>
<cffunction name="getCookies" output="false" returnType="array">
<cfargument name="headers" type="String" required="yes">
<cfset var cookies = arrayNew(1)>
<cfset var thisCookie = StructNew()>
<cfset var header = "">
<cfset var aCookie = "">
<cfset var crumb = "">
<cfset var paramName = "">
<cfset var paramValue = "">
<cfloop list="#headers#" delimiters="#Chr(10)#" index="header">
<cfif reFind("^Set-Cookie: ", header) gt 0>
<cfset acookie = reReplace(header,"^Set-Cookie: ","","ALL")>
<Cfset thisCookie = StructNew()>
<cfloop list="#acookie#" delimiters=";" index="crumb">
<cfset crumb = trim(crumb)>
<cfset paramName = listgetat(crumb,1,"=")>
<cfset paramValue = listgetat(crumb,2,"=")>
<cfif paramName eq "expires">
<cfset thisCookie.expires = paramValue>
<cfelseif paramName eq "path">
<cfset thisCookie.path = paramValue>
<cfelseif paramName eq "domain">
<cfset thisCookie.domain = paramValue>
<cfelseif paramName eq "secure">
<cfset thisCookie.secure = paramValue>
<cfelse>
<cfset thisCookie.name = paramName>
<cfset thisCookie.value = paramValue>
</cfif>
</cfloop>
<cfset arrayAppend(cookies,thisCookie)>
</cfif>
</cfloop>
<cfreturn cookies>
</cffunction>
Posted by rickroot at 5:40 AM | Link | 2 comments