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:fooar, C:fooaz, and C:fooqux, 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 est.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

view plain print about
1<br/><br/><style>
2 h1 {
3 font-size:14pt;
4 }
5 h2 {
6 font-size:11pt;
7 }
8 td {
9 padding:10px;
10 align:right;
11 font-size:10pt;
12 }
13 .body {
14 font-family:Verdana,Helvetica;
15 font-size:10pt;
16 width:800;
17 }
18 .batch {
19 font-family:Verdana,Helvetica;
20 font-size:8pt;
21 width:800;
22 color:#252525;
23 }
24</style><br/><br/>[cfset currentDir = getdirectoryfrompath(expandpath('*.*'))>
<br/><br/>
25[cfif isdefined("form.basePath") and isdefined("form.fileList")><br/><br/> [cfsilent>
26
27 [cfset issues = arrayNew(1)>
28 [cfif server.os.name contains "Win">
29
30 [cffile action="write" file="#currentDir#/cfencode_batch.bat" output=" ">
31
32 [cfloop list="#trim(form.fileList)#" index="i" delimiters="#chr(10)#">
33 [cfset target = trim(form.basepath) & "" & trim(i)>
34 [cfset target = replace(target,"\","","all")>
35
36
37 [cfset recurse = "">
38 [cfif target contains ".cf">
39 [cfset type = "file">
40 [cfelse>
41 [cfset type = "dir">
42
43 [cfif right(target,1) eq "*">
44 [cfset recurse = "/r">
45
46 [cfset target = left(target,len(target)-1)>
47 [/cfif>
48 [cfif right(target,1) eq "">
49
50 [cfset target = left(target,len(target)-1)>
51 [/cfif>
52 [/cfif>
53
54 [cfif (type is "file" and fileExists(target)) or (type is "dir" and directoryExists(target))>
55 [cftry>
56
57 [cffile action="append" file="#currentDir#/cfencode_batch.bat"
58 output='#server.ColdFusion.rootDir#incfencode.exe #target# #recurse# /q /v "2"'>
59 [cfcatch>
60 [cfset arrayAppend(issues,"ERROR: [#target#] #chr(10)##chr(13)#[#cfcatch.message#] #chr(10)##chr(13)#[#cfcatch.detail#]")>
61 [/cfcatch>
62 [/cftry>
63 [cfelse>
64 [cfset arrayAppend(issues,"INFO: [#type# #target# does not exist]")>
65 [/cfif>
66 [/cfloop>
67
68
69 [cfscript>sleep(2000);[/cfscript>
70
71
72 [cfif fileexists("#currentDir#/cfencode_batch.bat")>
73 [cfexecute name="#currentDir#/cfencode_batch.bat">[/cfexecute>
74 [cfelse>
75 [cfset arrayAppend(issues,"INFO: [batch file does not exist at #currentDir#/cfencode_batch.bat]")>
76 [/cfif>
77
78 [cfelse>
79 [cfset arrayAppend(issues,"INFO: [MUST RUN THIS ON WINDOWS]")>
80 [/cfif>
81
82 [/cfsilent>
83
84[/cfif><br/><br/>
85<div class="body">
86 <h1>Encrypt ColdFusion Files</h1>
87 <p>
88 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 "*".
89 </p>
90 <p>
91 Files will be encrypted with cfencode in place, so make sure you have a backup of unencrypted source.
92 </p>
93 [cfoutput>
94 <form name="encodeForm" method="post" action="">
95 <table>
96 <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>
97 <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>
98 <tr><td colspan="2"><input name="submit" type="submit" value="Encode Files"></td></tr>
99 </table>
100 </form>
101 [/cfoutput>
102</div><br/><br/><br/><br/>[cfif isdefined("issues")>
103<div class="batch">
104 <hr>
105 <h2>CFENCODE Completed</h2>
106 [cfif arraylen(issues)>
107 [cfdump var="#issues#" label="There were problems with your request">
108 [/cfif>
109 [cfif fileexists("#currentDir#/cfencode_batch.bat")>
110 <br/><br/>Here's the contents of the batch file that was created:
111 [cffile action="read" file="#currentDir#/cfencode_batch.bat" variable="batchContents">
112 [cfoutput><pre>#batchContents#</pre>[/cfoutput>
113 [/cfif>
114</div>
115[/cfif><br/><br/>