Appending File in Coldfusion - coldfusion

I am taking a Word RTF file and then replacing data within in and then writing it to a specific folder and appending the requestID to the file name. That part works great. However, my client also want to be able to print each word document that is generated at the smae time if there are multiple records.
For this I am reading the same RTF file, replaceing the data within it and then writing to a seperate file that I am attempting to append too within the loop, however it isn't working. I am only getting one record of the query.
<cfloop query="checklistData">
<cffile action="read" file="#request.filePath#\_reports\fileChecklist.rtf" variable="rtf">
<cfset rtf = Replace(rtf,"%PROGRAM%",#rqst_activity#)>
<cfset rtf = Replace(rtf,"%DLA%",#DLA#)>
<cfset rtf = Replace(rtf,"%SPS%",#SPS#)>
<cfset rtf = Replace(rtf,"%ECAT%",#ECAT#)>
<cfset rtf = Replace(rtf,"%MILSTRIP%",#MILSTRIP#)>
<cfset rtf = Replace(rtf,"%TDPNUM%",#tdp_nbr#)>
<cfset rtf = Replace(rtf,"%DTRQST%",#DateFormat(dt_tdp_rcvd,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%DTCMPT%",#DateFormat(dt_tdp_cmplt,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%DTDEL%",#DateFormat(rqsted_delivery_dt,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%UIC%",#DoDAAC#)>
<cfset rtf = Replace(rtf,"%COMMAND%",#COMMAND#)>
<cfset rtf = Replace(rtf,"%DTPRINT%",#DateFormat(NOW(),'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%NSN%",#nsn_id_rqsted#)>
<cfset rtf = Replace(rtf,"%NOMEN%",#nsn_nomenclature#)>
<cfset directoryCheck = "#request.filePath#\requests\documents\#checklistData.id_rqst#">
<cfif directoryExists(directoryCheck)>
<cffile action="write" file="#request.filePath#\requests\documents\#checklistData.id_rqst#\fileChecklist_#checklistData.id_rqst#.doc" output="#rtf#" nameconflict="overwrite">
<cfelse>
<cfdirectory action="create" directory="#directoryCheck#">
<cffile action="write" file="#request.filePath#\requests\documents\#checklistData.id_rqst#\fileChecklist_#checklistData.id_rqst#.doc" output="#rtf#" nameconflict="overwrite">
</cfif>
<cffile action="append" file="#request.filePath#\requests\documents\fileChecklistGroup_#ARGUMENTS.groupID#.rtf" output="#rtf#">
</cfloop>
<cfif NOT LEN('checklistData.groupID')>
<cfheader name="content-disposition" value="filename=#request.filePath#\requests\documents\#requestID#\fileChecklist_#requestID#.doc" />
<cfcontent type="application/msword" file="#request.filePath#\requests\documents\#requestID#\fileChecklist_#requestID#.doc">
<cfelse>
<cfheader name="content-disposition" value="filename=#request.filePath#\requests\documents\fileChecklistGroup_#groupID#.rtf" />
<cfcontent type="application/msword" file="#request.filePath#\requests\documents\fileChecklistGroup_#groupID#.rtf">
</cfif>
<cfabort>
And then I am asking to open the file at the end. Thanks in advance for an help!

Related

Using Microsoft DPAPI with Coldfusion

I'm in the process of decrypting Chrome's cookie information for a particular website. I can read the data from the file using the SQLLite DB object successfully. The next step is to decrypt the encrypted data.
I've managed to find two Java executables JDPAPI & windpapi4j allowing me to use the Microsoft Data Protection API (MS DPAPI) in Java.
I've loaded both the JAR files in the lib folder and can see their methods as shown below.
I'm trying to pass the encrypted_value of the cookie to the unprotectData and unprotect methods of the Java objects but getting the following error for both of them.
Code:
<cfset a_sqlite = createObject( 'java', 'org.sqlite.JDBC' )>
<cfset WinDPAPI = createObject('java','com.github.windpapi4j.WinDPAPI') />
<cfset jdpapi = createObject('java','net.sourceforge.jdpapi.DataProtector') />
<cfdump var="#WinDPAPI#" label="WinDPAPI">
<cfdump var="#jdpapi#" label="jdpapi">
<!--- <cfdump var="#a_sqlite#"> --->
<cfset a_prop = createObject( 'java', 'java.util.Properties' )>
<cfset db_filename = 'C:\Users\username\AppData\Local\Google\Chrome\User Data\Default\cookies'>
<cfset a_conn = a_sqlite.connect( 'jdbc:sqlite:' & db_filename, a_prop.init() )>
<!--- <cfdump var="#a_conn#"> --->
<cfset a_statement = a_conn.createStatement()>
<cfset a_res = a_statement.executeQuery("select * from cookies where host_key like '%ggas%';")>
<!--- <cfdump var="#a_res#"> --->
<cfloop condition="#a_res.next()#" >
<cfoutput>#a_res.getString("host_key")# = #a_res.getString("name")#</cfoutput><br>
<!--- <cfset encrypted_string = "#a_res.getString("encrypted_value")#"> --->
<cfdump var="#WinDPAPI.unprotectData(a_res.getString("encrypted_value"))#">
<cfdump var="#jdpapi.unprotect(toBinary(toBase64(a_res.getString("encrypted_value"))))#">
<!--- <cffile action="write" file="#expandPath(".")#\output.txt" output="#jdpapi.unprotect(toBinary(toBase64(a_res.getString("encrypted_value"))))#" addnewline="true"> --->
</cfloop>
I believe its something to do with the byte[] input type but not sure how to go about it. Any pointers would be helpful.

Randomize cfoutput

The following code changes the body BG dependent on if the /home url is active else use a different BG. We have the pageID = 206 containing the /home BG image and pageID = 207 containing the else BG image. We currently have it setup this way so the client can go into the CMS and change the BG image without an issue.
However, the client would like the ability to add additional backgrounds and have them randomize. In theory I would add a page to the CMS and include the pageID and add randomize. I'm familiar with ColdFusion but this is a little over my head. Any input or direction would be greatly appreciated.
<cfsavecontent variable="HOME_BG">
<cfoutput>
<cfset includeID = '206'><cfinclude emplate='/PageInclude/PageInclude.cfm'/>
</cfoutput>
</cfsavecontent>
<cfset BG1 = getToken(HOME_BG,3, """") >
<cfsavecontent variable="COMM_BG">
<cfoutput>
<cfset includeID = '207'><cfinclude template='/PageInclude/PageInclude.cfm'/>
</cfoutput>
</cfsavecontent>
<cfset BG2 = getToken(COMM_BG,3, """") >
<body onload="checkCookie()" style="background-image: url(<cfoutput><cfif fpath EQ "home">#BG1#<cfelse>#BG2#</cfif></cfoutput>);">
I was looking for the ability to randomize between two different < cfset > but didn't find much luck.
Solution using an array and randrange
<cfset bgHomeIDArray = ArrayNew(1)> <!--- Create the array --->
<cfset ArrayAppend(bgHomeIDArray, 214)> <!--- Adds an array value --->
<cfset ArrayAppend(bgHomeIDArray, 215)> <!--- Adds an array value --->
<cfsavecontent variable="HOME_BG">
<cfoutput>
<cfset includeID = bgHomeIDArray[randRange(1, len(bgHomeIDArray))]> <cfinclude template='/PageInclude/PageInclude.cfm'/>
</cfoutput>
</cfsavecontent>
<cfset BG1 = getToken(HOME_BG,3, """") >
<cfset bgCommIDArray = ArrayNew(1)> <!--- Create the array --->
<cfset ArrayAppend(bgCommIDArray, 216)> <!--- Adds an array value --->
<cfset ArrayAppend(bgCommIDArray, 217)> <!--- Adds an array value --->
<cfsavecontent variable="COMM_BG">
<cfoutput>
<cfset includeID = bgCommIDArray[randRange(1, len(bgCommIDArray))]><cfinclude template='/PageInclude/PageInclude.cfm'/>
</cfoutput>
</cfsavecontent>
<cfset BG2 = getToken(COMM_BG,3, """") >
<body onLoad="checkCookie()" style="background-image: url(<cfoutput><cfif fpath EQ "home">#BG1#<cfelse>#BG2#</cfif></cfoutput>?03202014);">

Cffile action="write" adding empty lines into my txt

Using ColdFusion I am updating the top line of my txt file, however once I do this it adds an empty line after each line in my txt file and I have no idea why.
A breakdown of my code is as follows:
<!---CSV FILE--->
<cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csv" variable="csvfile">
<cfoutput>
<!---LOOP THROUGH CSV FILE--->
<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
<!---SET VALUES--->
<!---TRIM VALUES--->
<!---SET STRING LENGTH FOR EACH--->
<!---SET TOTAL STRING--->
<!---IF FILE FOR BANK EXISTS--->
<cfif FileExists(ExpandPath("#listgetAt('#index#',5)#.txt"))>
<!---READ EXISTING FILE HEADER--->
<cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/#bankname#.txt" variable="bankheader">
<!---SPLIT UP THE HEADER TO ADD NEW VALUES ONTO IT--->
<cfset numericvalue = listfirst(bankheader,chr(13))>
<cfset numericvalue = #Right(numericvalue, 13)#>
<cfset RecordCountvalue = #Left(numericvalue, 3)#>
<cfset RecordCountvalue = #RecordCountvalue# + 1>
<cfset RecordCountvalue = #NumberFormat(RecordCountvalue,"000")#>
<cfset RecordCountvalue = #Left(RecordCountvalue, 3)#>
<cfset RecordCountvalue = #RJustify(RecordCountvalue, 3)#>
<cfset TotalRecordvalue = #Right(numericvalue, 10)#>
<cfset TotalRecordvalue = (#TotalRecordvalue# + #amount#) * 100000>
<cfset TotalRecordvalue = #NumberFormat(TotalRecordvalue,"0000000000")#>
<cfset TotalRecordvalue = #Left(TotalRecordvalue, 10)#>
<cfset TotalRecordvalue = #RJustify(TotalRecordvalue, 10)#>
<!---SET HEADER FOR FILE--->
<cfset fileheader_bank = "#UCase(bankname)#">
<cfset fileheader_bank = "#Left(fileheader_bank, 15)#">
<cfset fileheader_bank = "#LJustify(fileheader_bank, 16)#">
<cfset newfile_header = "#fileheader_bank##RecordCountvalue##TotalRecordvalue#">
<!---APPEND FILE AND ADD UPDATED HEADER--->
<cfset bankheader = listSetAt(bankheader,1,"#newfile_header#","#chr(13)#")>
<cffile action="write" file="#getDirectoryFromPath(getTemplatePath())#/#listgetAt('#index#',5)#.txt" output="#bankheader#">
<!---APPEND FILE AND ADD NEW ENTRY--->
<cffile action = "append"
file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"
output = "#total_string#">
</cfif>
</cfloop>
</cfoutput>
I am pretty sure the problem is in one of these cffile tags.
<!---APPEND FILE AND ADD UPDATED HEADER--->
<cfset bankheader = listSetAt(bankheader,1,"#newfile_header#","#chr(13)#")>
<cffile action="write" file="#getDirectoryFromPath(getTemplatePath())#/#listgetAt('#index#',5)#.txt" output="#bankheader#">
<!---APPEND FILE AND ADD NEW ENTRY--->
<cffile action = "append"
file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"
output = "#total_string#">
I have removed a lot of the unnecessary but if you need any of it please let me know.
If you used CFScript you would not have the issue. Because your using ColdFusion Tags returns and spaces affect document layout. Try minifiying your code to remove any floating returns / spaces. I have run into this issue with creating PDF files in CF

Getting data from the function using an Object in CF

I have a CFC object and a function which gets me the data which I want. Now I want to use that data and provide it to an already defined custom tag attribute. When I dump the #iEngine.listScore()# I get some parameters. But my problem is how should I provide those to an attribute?
<cfdump var="#iEngine.listScores()#" label="Swapnil Test - Function ListScore">
<cfset filename="ACE_DataExtract_#DateFormat(now(),'dd.mmm.yyyy')#.xls" />
<!--- Calling Custom tags to create/output xls files --->
<cfmodule template="#request.library.customtags.virtualpath#excel.cfm" file="#filename#" sheetname="ACE Report">
<cfmodule template="#request.library.customtags.virtualpath#exceldata.cfm"
query="#iEngine.listScores()#"
action="AddWorksheet"
sheetname="ACE Report"
colorscheme="blue"
useheaders="true"
contentformat="#{bold=true}#"
customheaders="#ListScore#">
<cfoutput>Excel Extract - ACE Report - #DateFormat(Now(),"d-mmm-yyyy")#</cfoutput>
</cfmodule>
</cfmodule>
Here I want to provide the data of iEngine.listScore() to the "Query" attribute in "exceldata" custom tag.
Below is the dump of iEngine.listScore()
I would write a transform Data function to change your array-struct to a query object, then pass that on....
<cffunction name="transformData" result="query">
<cfargument name="inArray" type="array">
<cfset local.qryReturn = queryNew("actiondate,actionId,closedate")>
<!--- You may look up queryNew and also set your dataTypes --->
<cfloop array="#arguments.inArray#" index="i">
<cfset QueryAddRow(local.qryReturn)>
<cfset querySetCell(local.qryReturn,"actionDate",i["actiondate"])>
<cfset querySetCell(local.qryReturn,"actionid",i["actionid"])>
<cfset querySetCell(local.qryReturn,"closedate",i["closedate"])>
</cfloop>
<cfreturn local.qryReturn>
</cffunction>
<cfset test = [
{actiondate='1/1/2015',actionid=134,closedate=''},
{actiondate='1/2/2015',actionid=135,closedate=''},
{actiondate='1/3/2015',actionid=136,closedate=''}
]>
<cfdump var="#test#">
<cfset resultQry = transformData(test)>
<cfif NOT isquery(resultQry)>
Exit invalid Data.
<cfelse>
<cfdump var="#resultQry#">
</cfif>

Extract 7z file in coldfusion

Can anyone help me by suggesting a function to extract a .7z file in ColdFusion? I use ColdFusion 10 and cfscript based code. Indeed we have the cfzip tag, but it only extracts .zip and .jar files.
You can use cfexecute, which unfortunately is not availble in cfscript, to execute the 7z extractor on your server and pass through the various commands to extract the file to a place of your choosing.
Luckily for you, it seems Raymond Camden has gone into it in some detail:
http://www.raymondcamden.com/index.cfm/2011/2/21/Working-with-RARs-in-ColdFusion
Function to unrar .rar file in given destination.. use cfexecute tag to run rar exe in command line
<cffunction name="Unrar" access="public" returnType="boolean" output="false">
<cfargument name="archivefile" type="string" required="true">
<cfargument name="destination" type="string" required="true">
<cfset var exeName = "">
<cfset var result = "">
<cfset var errorresult = "">
<cfif not fileExists(arguments.archivefile)>
<cfthrow message="Unable to work with #arguments.arvhiefile#, it does not exist.">
</cfif>
<cfif findnocase(".rar",arguments.archivefile)>
<cfset var exeName = expandpath("WinRAR\rar.exe")>
<cfset var args = []>
<cfif directoryExists(#arguments.destination#)>
<cfset args[1] = "x +o">
<cfelse>
<cfset directoryCreate(#arguments.destination#)>
<cfset args[1] = "x">
</cfif>
<cfset args[2] = arguments.archivefile>
<cfset args[3] = "#arguments.destination#">
</cfif>
<cfexecute name="#exeName#" arguments="#args#" variable="result" errorvariable="errorresult" timeout="99" />
<cfif findNoCase("OK All OK", result)>
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>