phrase images from webpage coldfusion - coldfusion

i need to get images from a webpage source.
i can use cfhttp method get and use htmleditformat() to read the html from that page, now i need to loop through the content to get all image url's(src)
can i use rematch() or refind() etc... and if yes how??
please help!!!!!
if im not clear i can try to clarify..

It can be very difficult to reliably parse html with regex.

Here's a function that will probably trip up on a lot of bad cases, but might work if you just need something quick and dirty.
<cffunction name="getSrcAttributes" access="public" output="No">
<cfargument name="pageContents" required="Yes" type="string" default="" />
<cfset var continueSearch = true />
<cfset var cursor = "" />
<cfset var startPos = 0 />
<cfset var finalPos = 0 />
<cfset var images = ArrayNew(1) />
<cfloop condition="continueSearch eq true">
<cfset cursor = REFindNoCase("src\=?[\""\']", arguments.pageContents, startPos, true) />
<cfif cursor.pos[1] neq 0>
<cfset startPos = (cursor.pos[1] + cursor.len[1]) />
<cfset finalPos = REFindNoCase("[\""\'\s]", arguments.pageContents, startPos) />
<cfset imgSrc = Mid(arguments.pageContents, startPos, finalPos - startPos) />
<cfset ArrayAppend(images, imgSrc) />
<cfelse>
<cfset continueSearch = false />
</cfif>
</cfloop>
<cfreturn images>
</cffunction>
Note: I can't verify at the moment that this code works.

Use a browser and jQuery to 'query' out all the img tag from the DOM might be easier...

Related

Best way for implementing webservice in CF

I have to create a web service in ColdFusion. I have tried the below 2 ways. Can anyone help me to find which one is the best way (Both Performance and security enhancement basis)
First Way
Created a cfm page like below;
<cfset result = StructNew() />
<cfset resultStruct = StructNew() />
<cfset validStruct = StructNew() />
<cfset VARIABLES.Sample = CreateObject("component","main.webservice.Sample")>
<cfif NOT isDefined("URL.method")>
<cfset result['status'] = false >
<cfset result['message'] = 'method is missing' />
<cfoutput>#SerializeJSON(result)#</cfoutput>
<cfabort>
</cfif>
<cfswitch expression="#URL.method#">
<cfcase value="get">
<cfset fieldList = "name">
<cfset validStruct = validate(fieldList) />
<cfif validStruct['status']>
<cfset resultStruct = VARIABLES.Sample.get(argumentCollection=URL) />
</cfif>
<cfoutput>#SerializeJSON(resultStruct)#</cfoutput>
<cfbreak>
</cfcase>
<cfcase value="put">
<cfset fieldList = "name,value">
<cfset validStruct = validate(fieldList) />
<cfif validStruct['status']>
<cfset resultStruct = VARIABLES.Sample.put(argumentCollection=URL) />
</cfif>
<cfoutput>#SerializeJSON(resultStruct)#</cfoutput>
<cfbreak>
</cfcase>
<cfdefaultcase>
<cfset result['status'] = false >
<cfset result['message'] = 'Not a valid method' />
<cfoutput>#SerializeJSON(result)#</cfoutput>
<cfbreak>
</cfdefaultcase>
</cfswitch>
And Created a cfc named 'Sample' under webservice folder and called like above.
WebService URL
http://test.com/webservice/Sample.cfm?method=get&name=test
Second Way
Called directly from the CFC Sample
Sample.CFC
<cfcomponent displayname="Sample" hint="Sample WebService" output="false">
<cffunction name="get" access="remote" returntype="struct" returnformat="json">
<cfargument name="name" required="true" type="string" >
<cfreturn StructNew() />
</cffunction>
<cffunction name="put" access="remote" returntype="struct" returnformat="json">
<cfargument name="name" required="true" type="string" >
<cfargument name="value" required="true" type="string" >
<cfreturn StructNew() />
</cffunction>
</cfcomponent>
WebService URL
http://test.com/webservice/Sample.CFC?method=get&name=test
The second method is the standard way to do WebServices in CFML. Along with the functionality, you are seeking you get standards based WSDL returns and definitions. It's a case of rebuilding the wheel. I'm sure the underlying CF code for ws could be optimized, but it's pretty good as is and has been field-tested by millions.
I would suggest setting up RESTful web services in ColdFusion. Here is an excellent article to get you started.
There's also Taffy which claims to make it simpler, although I have not used it.

Error with multiple PDF Generation in CF

I'm getting an error when producing a multi-page PDF.
The pages attribute is not specified for the MERGE action in the cfpdf tag.
The line that is causing the issue is: <cfpdf action="merge" source="#ArrayToList(variables.pdfList)#" destination="promega.pdf" overwrite="yes" />
I tried looking in Adobe's documentation bug cannot find an attribute pages for the merge action. Thoughts?
<!--- Append PDF to list for merge printing later --->
<cfset ArrayAppend(variables.pdfList, "#expandPath('.')#\general.pdf") />
<cfset variables.userAgenda = GetAttendeeSchedule(
variables.event_key,
variables.badgeNum
) />
<!--- Field CFID is the id of the agenda item; use this for certificate selection --->
<cfif variables.userAgenda.recordcount>
<cfloop query="variables.userAgenda">
<cfset variables.title = Trim(variables.userAgenda.CUSTOMFIELDNAMEONFORM) />
<cfpdfform source="#expandPath('.')#\promega_certificate.pdf" destination="#cfid#.pdf" action="populate">
<cfset variables.startdate = replace(CUSTOMFIELDSTARTDATE, "T", " ") />
<cfpdfformparam name="WORKSHOP" value="#variables.title#">
<cfpdfformparam name="NAME" value="#variables.badgeInfo.FirstName# #variables.badgeInfo.LastName#">
<cfpdfformparam name="STARTDATE" value="#DateFormat(variables.startdate, "medium" )#">
</cfpdfform>
<!--- Append PDF to list for merge printing later --->
<cfset ArrayAppend(variables.pdfList, "#expandPath('.')#\#cfid#.pdf") />
</cfloop>
</cfif>
<cfif ArrayLen(variables.pdfList)>
<cfpdf action="merge" source="#ArrayToList(variables.pdfList)#" destination="promega.pdf" overwrite="yes" />
<!--- Delete individual files --->
<cfloop list="#ArrayToList(variables.pdfList)#" index='i'>
<cffile action="delete" file="#i#" />
</cfloop>
<cftry>
<cffile action="delete" file="#expandPath('.')#\general.pdf" />
<cfcatch></cfcatch>
</cftry>
<cfheader name="Content-Disposition" value="attachment;filename=promega.pdf">
<cfcontent type="application/octet-stream" file="#expandPath('.')#\promega.pdf" deletefile="Yes">
<cflocation url="index.cfm" addtoken="false" />
</cfif>
This happens when source is a single file rather than a comma separated list of files. I'm guessing that if it's a single file is is expecting to extract some pages rather than add.
I tried the following on my coldfusion 9 machine and it worked just fine:
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />
<Cfset pdflist = arrayNew(1)>
<Cfset pdflist[1] = "#strPath#page1.pdf">
<Cfset pdflist[2] = "#strPath#page2.pdf">
<cfpdf action="merge" source="#ArrayToList(pdflist)#" destination="#strPath#merged.pdf" overwrite="yes" />
You could try to merge the pages like this and check whether you still get an error:
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />
<Cfset pdflist = arrayNew(1)>
<Cfset pdflist[1] = "#strPath#page1.pdf">
<Cfset pdflist[2] = "#strPath#page2.pdf">
<cfpdf action="merge" destination="#strPath#merged.pdf" overwrite="yes">
<Cfloop from=1 to="#arraylen(pdflist)#" index="x">
<cfpdfparam source="#pdfList[x]#">
</cfloop>
</cfpdf>

Variable Datasource is undefined

I have defined a datasource in the admin console of ColdFusion Admin. I have declared the configuration with the same datasource name in set_datasource_util file. I have a webpage that connects to a DB(Oracle) using the datasource given and it works fine most of the time. But lately I am starting to see exceptions like "Variable datasource-name(my app name) is undefined". I am not able to identify the root cause as I am not able to recreate it as it works most of the time. I can say on average on 1000 hits of page, it fails 1 time with that exception. Can anyone help in identifying what could be the possible issue so that I can investigate in that direction.
I have configured WOCD080P_ABC in CFAdmin.
Below is the code from Application.cfm
<cfif client.securityLevel NEQ ''>
<cfinvoke component="#siteroot#.CFC.Set_Data_Source_Util" method="fnSetABCDataSource" returnvariable="ABCDataSourceDefinition">
<cfinvokeargument name="Environment" value="#variables.thisServerType#" />
</cfinvoke>
<cfif ABCDataSourceDefinition.DataSource NEQ 'Error'>
<cfset DATASOURCE_ABC = ABCDataSourceDefinition.DataSource />
<cfset DBUSER_ABC = ABCDataSourceDefinition.DBUser />
<cfset DBPASSWORD_ABC = ABCDataSourceDefinition.DBPassword />
</cfif>
</cfif>
Below is the code from Set_datasource_UTil :
<cffunction name="fnSetABCDataSource" access="public" returntype="struct">
<cfargument name="Environment" type="string" required="yes">
<cfset ReturnParameters = StructNew() />
<cfif Environment IS 'prod'>
<cfset ReturnParameters.DataSource = 'WOCD080P_ABC' />
<cfset ReturnParameters.DBUser = ''/>
<cfset ReturnParameters.DBPassword = '' />
<cfelse>
<cfset ReturnParameters.DataSource = 'WOCD080T_ABC'/>
<cfset ReturnParameters.DBUser = ''/>
<cfset ReturnParameters.DBPassword = '' />
</cfif>
<cfreturn ReturnParameters>
</cffunction>
Below is the code which i am executing from HTML file.
<cfinvoke component="#siteroot#.cfc.ABC_UTIL" method="GetRegion" returnVariable = "USregion" >
<cfinvokeargument name="dbSource" value=#DATASOURCE_ABC# />
<cfinvokeargument name="dbUser" value=#DBUSER_ABC# />
<cfinvokeargument name="dbPass" value=#DBPASSWORD_ABC# />
</cfinvoke>
The code in the HTML file works most of the time but breaks sometime with error Variable DATASOURCE_ABC not defined.
Any help appreciated.
Please ensure you are using the var keyword for local variables inside of CFC Functions:
<cfset var ReturnParameters = StructNew() />
CF9+ local scope can be used in lieu of var:
<cfset local.ReturnParameters = StructNew() />
For extra protection, improve this statement in your application.cfm:
<cfif isDefined("ABCDataSourceDefinition.DataSource")
AND ABCDataSourceDefinition.DataSource NEQ 'Error'>
<cfset DATASOURCE_ABC = ABCDataSourceDefinition.DataSource />
<cfset DBUSER_ABC = ABCDataSourceDefinition.DBUser />
<cfset DBPASSWORD_ABC = ABCDataSourceDefinition.DBPassword />
</cfif>
Also, the could be another source of why a datasource is not being defined. I assume you have an interceptor that catches that before this datasource error occurs and sends them to a login page or something.

CF: Saving a Fusioncharts graph as an image to email it

Does anyone have an example of this? Google is not my friend tonight. I have the newest version of FusionCharts. I'm trying to figure out how to save a graph as an image file to email it.
I know how to save and then insert images into HTML emails, and I've done this before with other graph products. I just cannot fine 1 good example of how to do this with Fusioncharts.
Thanks!
Use the following code in a template and point the imageSaveURL property at that template.
Your chart submits all necessary data for the chart to be reconstructed. In my example I'm serving it to the browser but you could save it locally and then attach to a cfmail if necessary.
I'm pretty sure I got this from the fusioncharts forums originally. This was updated for fusioncharts 3.1.
<cfif structKeyExists(Form, "width") >
<cfset width = int(Form.width) />
<cfelse>
<cfset width = int(Form.Meta_Width) />
</cfif>
<cfif structKeyExists(Form, "height") >
<cfset height = int(Form.height) />
<cfelse>
<cfset height = int(Form.Meta_Height) />
</cfif>
<cfif structKeyExists(Form, "data") >
<cfset Form.data = Form.data />
<cfelse>
<cfif structKeyExists(Form, "stream") >
<cfset Form.data = Form.stream />
</cfif>
</cfif>
<cfset user = viewState.getValue("user", structNew()) />
<!--- Impose some limits to mitigate DOS attacks --->
<cfif Not (0 lte width and width lte 5000)>
<cfthrow message="Width out of range." />
</cfif>
<cfif Not (0 lte height and height lte 5000)>
<cfthrow message="Height out of range." />
</cfif>
<!--- Check if we have the chart data --->
<cfif Not StructKeyExists(Form, "data") or Not Len(Trim( Form.data ))>
<cfthrow message="Image Data not supplied." />
</cfif>
<!--- Default background color is white --->
<cfif Not StructKeyExists(Form, "bgcolor") or Not Len(Trim( Form.bgcolor ))>
<cfset Form.bgcolor = "FFFFFF" />
</cfif>
<cfset gColor = CreateObject("java", "java.awt.Color") />
<cfset chart = CreateObject("java", "java.awt.image.BufferedImage") />
<cfset chart.init( JavaCast("int", width), JavaCast("int", height), chart.TYPE_3BYTE_BGR) />
<cfset gr = chart.createGraphics() />
<cfset gr.setColor( gColor.decode("##" & Form.bgcolor) ) />
<cfset gr.fillRect(0, 0, JavaCast("int", width), JavaCast("int", height)) />
<!--- Get rows with pixels --->
<cfset rows = ListToArray(Form.data, ";") />
<cfloop from="1" to="#ArrayLen(rows)#" index="i">
<cfset pixels = ListToArray(rows[i], ",") />
<!--- Horizontal index (x scale) --->
<cfset horizIndex = 0 />
<cfloop from="1" to="#ArrayLen(pixels)#" index="j">
<cfif ListLen(pixels[j], "_") eq 2>
<!--- We have the color and the number of times it must be repeated --->
<cfset color = ListGetAt(pixels[j], 1, "_") />
<cfset repeat = ListGetAt(pixels[j], 2, "_") />
<cfelse>
<!--- Background color; how many pixels to skip --->
<cfset color = "" />
<cfset repeat = ListGetAt(pixels[j], 1, "_") />
</cfif>
<cfif Len(Trim(color))>
<!--- If the hexadecimal code is less than 6 characters, prefix with 0 to get a 6 char color --->
<cfif Len(Trim(color)) lt 6>
<cfset color = RepeatString(0, 6 - Len(Trim(color))) & color />
</cfif>
<!--- Draw a horizontal line for the number of pixels we must repeat --->
<cfset gr.setColor(gColor.decode("##" & color)) />
<cfset gr.drawLine(JavaCast("int", horizIndex), JavaCast("int", i - 1), JavaCast("int", horizIndex + repeat -1), JavaCast("int", i - 1)) />
</cfif>
<cfset horizIndex = horizIndex + repeat />
</cfloop>
</cfloop>
<!--- Get writer for Jpeg --->
<cfset writer = "" />
<cfset iter = CreateObject("java", "javax.imageio.ImageIO").getImageWritersByFormatName("jpg") />
<cfloop condition="iter.hasNext()">
<cfset writer = iter.next() />
</cfloop>
<!--- Set Jpeg quality to maximum --->
<cfset jpgParams = CreateObject("java", "javax.imageio.plugins.jpeg.JPEGImageWriteParam").init( CreateObject("java", "java.util.Locale").init("en") ) />
<cfset jpgParams.setCompressionMode( jpgParams.MODE_EXPLICIT ) />
<cfset jpgParams.setCompressionQuality( 1 ) />
<!--- Write image to a memory stream --->
<cfset imageOutput = CreateObject("java", "java.io.ByteArrayOutputStream").init() />
<cfset writer.setOutput( CreateObject("java", "javax.imageio.stream.MemoryCacheImageOutputStream").init( imageOutput ) ) />
<cfset writer.write(JavaCast("null", 0), CreateObject("java", "javax.imageio.IIOImage").init(chart, JavaCast("null", 0), JavaCast("null", 0)), jpgParams) />
<!--- Stream the image to the browser (hint browser to display the Save dialog) --->
<cfset filename="whatever.jpg" />
<cfheader name="Content-Disposition" value="attachment; filename=""#filename#""">
<cfcontent type="image/jpeg" variable="#imageOutput.toByteArray()#">
Checkout out the latest blog post Export Chart Images at Server Side where two options (wkhtmltoimage and PhantomJS. ) has been described with step-by-step instructions for both.
For ColdFusion, you can try the below code:
For example:
<cfexecute name="C:\Program Files\wkhtmltopdf\wkhtmltoimage.exe" arguments="--javascript-delay 10000 http://docs.fusioncharts.com/charts/Code/MyFirstChart/ms-weekly-sales-no-animation.html savedimage.png" />
While executing the above script, we provide the following arguments:
Call wkhtmltoimage
Pass the URL of your webpage to it
Pass the path and name of the image file (with extension) where the image will be saved
If required, any additional delay to ensure the chart is completely rendered.

Displaying Struct information in Coldfusion

I want to display the value of a struct key like:
#stReviewDetail['tags']['travelParty']['value']#
It is possible that tags, travelParty or value is missing. What is the best way to check if the structure hierarchy is available? Something like:
<cfif StructKeyExists(stReviewDetail, 'tags') AND
StructKeyExists(stReviewDetail['tags'], 'travelParty') AND
StructKeyExists(stReviewDetail['tags']['travelParty'], 'value') >
....
</cfif>
or is there a better way to do this?
Multiple StructKeyExists are ugly, and it's easy to write a function to simplify this:
Usage:
<cfif CheckNestedKeys(stReviewDetail,['tags','travelParty','value']) >
#stReviewDetail['tags']['travelParty']['value']#
</cfif>
Code:
<cffunction name="CheckNestedKeys" returntype="Boolean" output=false>
<cfargument name="Struct" type="Struct" required />
<cfargument name="Keys" type="Array" required />
<cfset var CurStruct = Arguments.Struct />
<cfloop index="local.CurKey" array=#Arguments.Keys# >
<cfif StructKeyExists(CurStruct,CurKey)>
<cfset CurStruct = CurStruct[CurKey] />
<cfelse>
<cfreturn false />
</cfif>
</cfloop>
<cfreturn true />
</cffunction>
If you know the specific keys, you could just use isDefined:
<cfif isDefined("stReviewDetail.tags.travelParty.value")>
<cfdump var="#stReviewDetail.tags#">
</cfif>