Archive for July, 2010

Easy PHP Caching

July 21st, 2010

Just as the title says, easy php caching.

Step 1: Create a folder in the root of your website to store the cached files and make sure the folder is writable by php. My folder is named appropriately cache.

Step 2: Start the part of the page you want cached with….

// Start Cache
$cachefile = ‘cache/’.basename($_SERVER['REQUEST_URI']); //change cache to whatever you named your folder
$cachetime = 2880 * 60; // Cache for 48 hours before replacing. Change the 2880 to what ever value you want. The formula is hours * 60
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() – $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "“; //you can comment out this line if you don’t want the cache info to show in your html code
exit;
}
ob_start(); // start the output buffer
//start your code here

Step 3: End the part of the page you want cached with….

//end your code
$fp = fopen($cachefile, ‘w’); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

WSSE Security Header Authentication with ColdFusion

July 20th, 2010

I recently had a project that required interaction with a web service using wsse authentication. The example given by the vendor failed to authenticate as well as the wsse online documentation.

Unfortunately there is very little info I could find on using ColdFusion with wsse authentication and the error messages were less than helpful.

After many days of trial and error I finally got it to work, hopefully this will save someone the headache.

Creating the Header:

<CFFUNCTION NAME=”getSecurityHeader”>

<cfset myUsername = #uname# />
<cfset myPassword = #passwd# />
<cfset dtNow = Now() />
<cfset dtGMT = DateAdd(”s”,GetTimeZoneInfo().UTCTotalOffset,dtNow) />
<cfset myDate = “#DateFormat(dtGMT,”yyyy-mm-dd”)#” & “T” & “#timeformat(dtGMT,”HH:mm:ss”)#” & “Z”>

<cfscript>
myNonce = createUUID();
myObj = createObject(”webservice”,this.checkInstantConsumer.DropPath);

headerElement = createObject(”java”,”org.apache.axis.message.SOAPHeaderElement”);

headerElement.init(”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd”, “wsse:Security”);

headerElement.addChildElement(”wsse:UsernameToken”);

headerElement.getFirstChild().addChildElement(”wsse:Username”).setValue(myUsername);

mypass = headerElement.getFirstChild().addChildElement(”wsse:Password”);
mypass.setValue(myPassword);
//mypass.setAttribute(”Type”, “PasswordText”);

headerElement.getFirstChild().addChildElement(”wsse:Nonce”).setValue(
ToBase64(Hash(myNonce, “SHA”)) );

myCreated = headerElement.getFirstChild().addChildElement(”wsu:Created”);
myCreated.setValue(myDate);
myCreated.setAttribute(”xmlns:wsu”,”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd”);

headerElement.setMustUnderstand(1);
headerElement.setActor(”");

myObj.setHeader(headerElement);

securityheader = headerElement.tostring();
</cfscript>

<CFRETURN securityheader />
</CFFUNCTION>

Using the Created Header:

<!— Get wsse Security Header —>
<cfscript>
securityHeader=getSecurityHeader();
</cfscript>
<cfoutput>
<!— Crreate SOAP Request —>
<cfsavecontent variable=”request.thisrequest”><?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Header>
#securityHeader#
</soap:Header>
<soap:Body>
<Method xmlns=”https://www.site.com/Method/”>
<Element1>#Element1Value#</Element1>
<Element2>#Element2Value#</Element2>
</Method>
</soap:Body>
</soap:Envelope>
</cfsavecontent>