Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:06
Show Gist options
  • Save bennadel/9759465 to your computer and use it in GitHub Desktop.
Save bennadel/9759465 to your computer and use it in GitHub Desktop.
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
<application>
<!--
ColdFusion Builder extension overview. This is the
information that will be presented to the user during
the extension installation process.
-->
<name>ColdFusion File Encryption</name>
<author>Ben Nadel</author>
<version>0.1</version>
<email>ben@xxxxxxxxx.com</email>
<description>
<![CDATA[
<p>
This extensions encrypts and decrypts ColdFusion
CFM and CFC files using command line utilities
(executables). For encryption, it uses the native
CFEncode.exe and for decryption, it uses the 3rd
party CFDecrypt.exe.
</p>
<p>
The original ColdFusion files remain intact. The
encrypted and decrypted files are created as new
files with ".encrypted" and ".decrypted" preceding
the file extension (respectively).
</p>
]]>
</description>
<license>
<![CDATA[
<p>
Just don't sue me.
</p>
<p>
<strong>NOTE:</strong> Using the CFDecrypt.exe
utility <em>might</em> be a violation of the Adobe
ColdFusion license agreement / terms of use; use
at your own discretion.
</p>
]]>
</license>
<!--
The MenuContributions determine where the extension
is active within ColdFusion Builder (as defined by the
Contribution tags).
-->
<menucontributions>
<!-- The "ProjectView" is the project navigator. -->
<contribution target="projectview">
<menu name="ColdFusion File Encryption">
<!--
This will only be avilable on CFM and CFC
files (within the project tree) as defined
by the following regular epxression for file
extensions.
NOTE: Use the (?i) case flag to keep the
regular expression case-insensitive (since it
is, by default case-sensitive).
NOTE: The pattern must match the ENTIRE file
name, not just match within it.
-->
<filters>
<filter
type="file"
pattern="(?i).+\.cf(m|c)"
/>
</filters>
<!--
These are the options that will show up under
the "ColdFusion File Encryption" header within
the context menu. Each calls a Handler (by ID),
defined later in this configuration file.
-->
<action
name="Encrypt"
handlerid="encrypt"
showresponse="false"
/>
<action
name="Decrypt"
handlerid="decrypt"
showresponse="false"
/>
</menu>
</contribution>
</menucontributions>
<!--
The Handler define the ColdFusion files that will
receive the data posted by ColdFusion Builder when the
user selects one of the above actions.
NOTE: These files are located in the "handlers" folder
of the extension installation.
-->
<handlers>
<handler id="encrypt" type="cfm" filename="encrypt.cfm" />
<handler id="decrypt" type="cfm" filename="decrypt.cfm" />
</handlers>
</application>
<!---
Param the FORM value that will contain the data posted from
the ColdFusion Builder extension. This will be in the form of
the following XML file:
<event>
<ide>
<projectview
projectname="EncryptDecrypt"
projectlocation="C:/..." >
<resource
path="C:/.../file.cfm"
type="file" />
</projectview>
</ide>
<user></user>
</event>
--->
<cfparam
name="form.ideEventInfo"
type="string"
default=""
/>
<!---
Wrap the entire process around Try / Catch because it relies
on a bunch of things that might cause error.
NOTE: This is my first web service. In future iterations, we
will do a better job of reporting back any errors.
--->
<cftry>
<!--- Get the current directory. --->
<cfset thisDirectory = getDirectoryFromPath(
getCurrentTemplatePath()
) />
<!--- Get the bin directory. --->
<cfset binDirectory = (thisDirectory & "..\bin\") />
<!--- Get the log directory (for errors). --->
<cfset logDirectory = (thisDirectory & "..\log\") />
<!---
Now that we have all of our directories in place,
let's convert the request data into XML so we can access
its nodes.
--->
<cfset requestXml = xmlParse( trim( form.ideEventInfo ) ) />
<!---
Now that we have all of our directories in place, let's
grab the resource node's PATH attribute from the XML post
into the document we got from ColdFusion builder.
--->
<cfset resourceNodes = xmlSearch(
requestXml,
"//resource[ position() = 1 ]/@path"
) />
<!---
From the resource PATH attribute node, we can grab the
file path to the unecrypted ColdFusion file.
NOTE: While ColdFusion usually doesn't care about the file
path seperator, since we are dipping down into the command
line, we need to make sure we are using the WINDOWS file
path seperator.
--->
<cfset decryptedFile = reReplace(
resourceNodes[ 1 ].xmlValue,
"[\\/]",
"\",
"all"
) />
<!---
Based on the decrypted file name, let's create an encrypted
file name by adding ".encypted." before the file extension.
--->
<cfset encryptedFile = reReplaceNoCase(
decryptedFile,
"(.+?)(?:\.decrypted)?\.(cf(m|c))$",
"\1.encrypted.\2",
"one"
) />
<!---
Now that we have the path to the unecrypted file and to
the target encrypted file, we can run the source through
the cfencode.exe command line utility.
--->
<cfexecute
name="""#binDirectory#cfencode.exe"""
arguments="""#decryptedFile#"" ""#encryptedFile#"" /v ""2"""
timeout="5">
</cfexecute>
<!---
Now that we have encrypted the file, we need to tell
ColdFusion Builder to refresh it's project tree (since
we have created a new file). To do that, we need to grab
the project node.
--->
<cfset projectNode = xmlSearch(
requestXml,
"//projectview[ position() = 1 ]/@projectname"
) />
<!--- Store the response xml. --->
<cfsavecontent variable="responseXml">
<cfoutput>
<response>
<ide>
<commands>
<command name="refreshproject">
<params>
<param
key="projectname"
value="#projectNode[ 1 ].xmlValue#"
/>
</params>
</command>
</commands>
</ide>
</response>
</cfoutput>
</cfsavecontent>
<!---
Now, convert the response XML to binary and stream it
back to builder.
--->
<cfset responseBinary = toBinary(
toBase64(
trim( responseXml )
)
) />
<!---
Set response content data. This will reset the output
buffer, write the data, and then close the response.
--->
<cfcontent
type="text/xml"
variable="#responseBinary#"
/>
<!--- ------------------------------------------------- --->
<!--- ------------------------------------------------- --->
<!---
We should NOT have made it this far. Either the request
prcessed well and the processing is OVER; or, there was
an error and the processing skipped directly to the
CFCatch block of our try / catch area.
--->
<!--- Catch any errors. --->
<cfcatch>
<!--- Log the error to disk. --->
<cfdump
var="#[ form, variables, cfcatch ]#"
format="html"
output="#logDirectory##createUUID()#.htm"
/>
</cfcatch>
</cftry>
<!---
Param the FORM value that will contain the data posted from
the ColdFusion Builder extension. This will be in the form of
the following XML file:
<event>
<ide>
<projectview
projectname="EncryptDecrypt"
projectlocation="C:/..." >
<resource
path="C:/.../file.cfm"
type="file" />
</projectview>
</ide>
<user></user>
</event>
--->
<cfparam
name="form.ideEventInfo"
type="string"
default=""
/>
<!---
Wrap the entire process around Try / Catch because it relies
on a bunch of things that might cause error.
NOTE: This is my first web service. In future iterations, we
will do a better job of reporting back any errors.
--->
<cftry>
<!--- Get the current directory. --->
<cfset thisDirectory = getDirectoryFromPath(
getCurrentTemplatePath()
) />
<!--- Get the bin directory. --->
<cfset binDirectory = (thisDirectory & "..\bin\") />
<!--- Get the log directory (for errors). --->
<cfset logDirectory = (thisDirectory & "..\log\") />
<!---
Now that we have all of our directories in place,
let's convert the request data into XML so we can access
its nodes.
--->
<cfset requestXml = xmlParse( trim( form.ideEventInfo ) ) />
<!---
Now that we have all of our directories in place, let's
grab the resource node's PATH attribute from the XML post
into the document we got from ColdFusion builder.
--->
<cfset resourceNodes = xmlSearch(
requestXml,
"//resource[ position() = 1 ]/@path"
) />
<!---
From the resource PATH attribute node, we can grab the
file path to the ecrypted ColdFusion file.
NOTE: While ColdFusion usually doesn't care about the file
path seperator, since we are dipping down into the command
line, we need to make sure we are using the WINDOWS file
path seperator.
--->
<cfset encryptedFile = reReplace(
resourceNodes[ 1 ].xmlValue,
"[\\/]",
"\",
"all"
) />
<!---
Based on the encrypted file name, let's create an decrypted
file name by adding ".decypted." before the file extension.
--->
<cfset decryptedFile = reReplaceNoCase(
encryptedFile,
"(.+?)(?:\.encrypted)?\.(cf(m|c))$",
"\1.decrypted.\2",
"one"
) />
<!---
Now that we have the path to the encrypted file and to
the target denrypted file, we can run the source through
the cfdecrypt.exe command line utility.
--->
<cfexecute
name="""#binDirectory#cfdecrypt.exe"""
arguments="""#encryptedFile#"" ""#decryptedFile#"""
timeout="5">
</cfexecute>
<!---
Now that we have decrypted the file, we need to tell
ColdFusion Builder to refresh it's project tree (since
we have created a new file). To do that, we need to grab
the project node.
--->
<cfset projectNode = xmlSearch(
requestXml,
"//projectview[ position() = 1 ]/@projectname"
) />
<!--- Store the response xml. --->
<cfsavecontent variable="responseXml">
<cfoutput>
<response>
<ide>
<commands>
<command name="refreshproject">
<params>
<param
key="projectname"
value="#projectNode[ 1 ].xmlValue#"
/>
</params>
</command>
</commands>
</ide>
</response>
</cfoutput>
</cfsavecontent>
<!---
Now, convert the response XML to binary and stream it
back to builder.
--->
<cfset responseBinary = toBinary(
toBase64(
trim( responseXml )
)
) />
<!---
Set response content data. This will reset the output
buffer, write the data, and then close the response.
--->
<cfcontent
type="text/xml"
variable="#responseBinary#"
/>
<!--- ------------------------------------------------- --->
<!--- ------------------------------------------------- --->
<!---
We should NOT have made it this far. Either the request
prcessed well and the processing is OVER; or, there was
an error and the processing skipped directly to the
CFCatch block of our try / catch area.
--->
<!--- Catch any errors. --->
<cfcatch>
<!--- Log the error to disk. --->
<cfdump
var="#[ form, variables, cfcatch ]#"
format="html"
output="#logDirectory##createUUID()#.htm"
/>
</cfcatch>
</cftry>
<response>
<ide>
<commands>
<command name="refreshproject">
<params>
<param key="projectname" value="EncryptDecrypt" />
</params>
</command>
</commands>
</ide>
</response>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment