 Just a quick post to share a utility I wrote to facilitate using cfencode on a batch of ColdFusion templates. The purpose was to enable a way to encode a subset of templates in a selective way rather than just encoding everything recursively.
Example, say you have C:\foo\bar\, C:\foo\baz\, and C:\foo\qux\, and you want to encode only the baz directory and a single cfm in the qux directory, but without touching the bar directory. You would enter C:\foo\ as the base path, and then for the file list you would enter baz\ and qux\test.cfm similar to the screenshot here.
Code is without warranty, use at your own risk. Big caveat: Remember the cfencode tool doesn't truly encrypt CF templates, but merely obfuscates them. This can be easily decoded by those who know how. Also you may want to check out Ben Nadel's CFEncode extension for CFBuilder, and read Joshua Cyr's perspective on why you would or would not want to encode your files.
Plain Text
<!--- author: Steven Erat purpose: Wrapper for selective, bulk encoding of ColdFusion templates ---> <style> h1 { font-size:14pt; } h2 { font-size:11pt; } td { padding:10px; align:right; font-size:10pt; } .body { font-family:Verdana,Helvetica; font-size:10pt; width:800; } .batch { font-family:Verdana,Helvetica; font-size:8pt; width:800; color:#252525; } </style> <cfset currentDir = getdirectoryfrompath(expandpath('*.*'))> <!--- Parse form to encode files ---> <cfif isdefined("form.basePath") and isdefined("form.fileList")> <cfsilent> <cfset issues = arrayNew(1)> <cfif server.os.name contains "Win"> <!--- clear previous file contents or write file from start ---> <cffile action="write" file="#currentDir#/cfencode_batch.bat" output=" "> <!--- iterate over file paths that were input and make full path ---> <cfloop list="#trim(form.fileList)#" index="i" delimiters="#chr(10)#"> <cfset target = trim(form.basepath) & "\" & trim(i)> <cfset target = replace(target,"\\","\","all")> <!--- file or dir? if dir then recurse? ---> <cfset recurse = ""> <cfif target contains ".cf"> <cfset type = "file"> <cfelse> <cfset type = "dir"> <!--- cfencode bug discovered. recurse is always on for directories ---> <cfif right(target,1) eq "*"> <cfset recurse = "/r"> <!--- the * wildcard is used in the input, but not accepted by cfencode ---> <cfset target = left(target,len(target)-1)> </cfif> <cfif right(target,1) eq "\"> <!--- cfencode bombs if a slash appears at end of dir name ---> <cfset target = left(target,len(target)-1)> </cfif> </cfif> <cfif (type is "file" and fileExists(target)) or (type is "dir" and directoryExists(target))> <cftry> <!--- add a line to bat file ---> <cffile action="append" file="#currentDir#/cfencode_batch.bat" output='#server.ColdFusion.rootDir#\bin\cfencode.exe #target# #recurse# /q /v "2"'> <cfcatch> <cfset arrayAppend(issues,"ERROR: [#target#] #chr(10)##chr(13)#[#cfcatch.message#] #chr(10)##chr(13)#[#cfcatch.detail#]")> </cfcatch> </cftry> <cfelse> <cfset arrayAppend(issues,"INFO: [#type# #target# does not exist]")> </cfif> </cfloop> <!--- wait for file write to complete ---> <cfscript>sleep(2000);</cfscript> <!--- if bat file exists, run it ---> <cfif fileexists("#currentDir#/cfencode_batch.bat")> <cfexecute name="#currentDir#/cfencode_batch.bat"></cfexecute> <cfelse> <cfset arrayAppend(issues,"INFO: [batch file does not exist at #currentDir#/cfencode_batch.bat]")> </cfif> <cfelse> <cfset arrayAppend(issues,"INFO: [MUST RUN THIS ON WINDOWS]")> </cfif> </cfsilent> </cfif> <!--- Form to enter file list. Always display ---> <div class="body"> <h1>Encrypt ColdFusion Files</h1> <p> Enter a base path and then a list of relative paths off the base to specific ColdFusion files. Directories are acceptable too. Indicate recursion by ending a directory path ending in "*". </p> <p> Files will be encrypted with cfencode in place, so make sure you have a backup of unencrypted source. </p> <cfoutput> <form name="encodeForm" method="post" action=""> <table> <tr><td>Base Path:</td><td><input name="basePath" value="<cfif isdefined("form.basePath")>#form.basePath#</cfif><cfoutput></cfoutput>" size="57" title="A base path to be prefixed to the relative path for each item in File List"/></td></tr> <tr><td valign="top">File List:</td><td><textarea name="fileList" rows="15" cols="50" title="A list of files or directories to encode, one per line. Use relative paths from base path."><cfif isdefined("form.fileList")>#form.fileList#</cfif><cfoutput></cfoutput></textarea></td></tr> <tr><td colspan="2"><input name="submit" type="submit" value="Encode Files"></td></tr> </table> </form> </cfoutput> </div> <cfif isdefined("issues")> <div class="batch"> <hr> <h2>CFENCODE Completed</h2> <cfif arraylen(issues)> <cfdump var="#issues#" label="There were problems with your request"> </cfif> <cfif fileexists("#currentDir#/cfencode_batch.bat")> <br/><br/>Here's the contents of the batch file that was created: <cffile action="read" file="#currentDir#/cfencode_batch.bat" variable="batchContents"> <cfoutput><pre>#batchContents#</pre></cfoutput> </cfif> </div> </cfif>
|