ColdFusion UDF Library - execute

December 16th, 2008
«« Back to ColdFusion Library

Category:
   SYSTEM

Description:
   Executes inline ColdFusion

Parameters:
  1. filename (String, required)
           The file containing the cfml you wish to execute.


Returns:
       (String) Output of executed file

Requires:
       Execute CFC

Sample Use:
       execute("employeesList.cfm");

Copy to Clipboard
function execute(filename){
	engine = createObject("component","execute");
	return engine.parseFile(filename);
};
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=96&function=execute

Required CFC: execute.cfc
Copy to Clipboard
<cfcomponent>
	<cffunction name="parseString" returntype="string">
		<cfargument type="string" name="text" required="yes">
		<cfargument name="caller" default="">
		<cfargument name="context" default="">
		<cfargument name="timeout" default="5">
		<cfif not isStruct(caller)>
			<cfset caller = structNew()>
		</cfif>
		<cfset void = server.reScope("variables",caller)>
		<cfset path = expandPath("/")>
		<cfif len(context)>
			<cfif right(context,1) eq "/">
				<cfset context = context & "index.cfm">
			</cfif>
			<cfif fileExists(expandPath(context))>
				<cfset relativePath = getDirectoryFromPath(context)>
				<cfset physicalPath = expandPath(getDirectoryFromPath(context))>
				<cffile action="read" file="#expandPath(context)#" variable="context">
				<cfset uuid = createUUID()>
				<cfset filename = "#uuid#.cfm">
				<cffile action="write" file="#physicalPath#\#filename#" output="#context#<!--#uuid#-->#text#" addnewline="no">
				<cfmodule name="applications.realm.bypass" action="register" realm_id="-1" variable="uniqueID">
				<cfschedule action="update" task="#uuid#" operation="httprequest" url="http://#cgi.http_host##relativePath##filename#?__bypass_realm=#uniqueID#" startDate="#now()#" startTime="#now()#" publish="yes" path="#physicalPath#" file="#uuid#.html" interval="once">
				<cfschedule action="delete" task="#uuid#">
				<cfset stop = false>
				<cfset counter = 0>
				<cfloop condition="not fileExists(physicalPath & uuid & "".html"") or (fileExists(physicalPath & uuid & "".html"") and getFileInfo(physicalPath & uuid & "".html"").size eq 0) and not stop">
					<cfset sleep(250)>
					<cfset counter = incrementValue(counter)>
					<cfif counter gte timeout*4>
						<cfset stop = true>
					</cfif>
				</cfloop>
				<cfif not stop>
					<cffile action="delete" file="#physicalPath#\#filename#">
					<cffile action="read" file="#physicalPath#\#uuid#.html" variable="content">
					<cffile action="delete" file="#physicalPath##uuid#.html">
					<cfset pos = findNoCase("<!--#uuid#-->",content)>
					<cfif pos>
						<cfset data = mid(content,pos+len("<!--#uuid#-->"),len(content)-(pos+len("<!--#uuid#-->")))>
					<cfelse>
						<cfset data = content>
					</cfif>
				<cfelse>
					<cfset data = "Timed Out">
				</cfif>
			<cfelse>
				<cfset data = "">
			</cfif>
		<cfelse>
			<cfset filename = "#createUUID()#.inc">
			<cffile action="write" file="#path##filename#" output="<cfoutput>#text#</cfoutput>" addnewline="no">
			<cftry>
				<cfsavecontent variable="data">
				<cfinclude template="/#filename#">
				</cfsavecontent>
				<cffile action="delete" file="#expandPath("/")##filename#">
			<cfcatch type="any">
				<cffile action="delete" file="#expandPath("/")##filename#">
				<cfsavecontent variable="data">
				<cfrethrow>
				</cfsavecontent>
			</cfcatch>
			</cftry>
		</cfif>
		<cfreturn data>
	</cffunction>
	
	<cffunction name="parseFile" returntype="string">
		<cfargument type="string" name="template" default="">
		<cfif fileExists("#expandPath("./")##template#")>
			<cfsavecontent variable="data">
			<cfinclude template="./#template#">
			</cfsavecontent>
		</cfif>
		<cfreturn data>
	</cffunction>
</cfcomponent>
  1. No comments yet.
  1. No trackbacks yet.