devbox@COMPUTEC The Computec development blog

21Jul/100

Coldfusion UDF to create & CHMOD a full directory path

The following problem has come up during a file caching implementation: We've got a directory /var/www/MYCACHE; our filecaching mechanism uses a key-based directory structure to store files there. So let's suppose our key would be 123456789, we'd like to store the file 123456789.cache under /var/www/MYCACHE/123/123456/123456789.cache. This would make sure that no directory needs to hold more than 1,000 nodes.

All would be well if we could be sure that the user jrun (i.e. the user that owns our ColdFusion process) was indeed the only user ever to access this directory structure. In our case we want to be able to access this structure with PHP, too, which runs as mod_php on the webserver, thus as user www-data. To avoid permission problems, we want to assign a permission of 0777 to all directories in the structure upon creation.

20May/100

ColdFusion UDF to generate SEO-friendly URL strings

This function might be convenient if you need to create a seo-friendly URL from a headline that could contain special characters such as German umlauts or accented letters; spaces would be replaced by dashes as recommended by Matt Cutts of Google. Unrecognized characters in a certain Unicode range will finally be replaced by x's, everything that's still not recognized will simply be dropped.

3May/101

UDF for RFC822 date

This might be useful if you wish to create RFC-822 type date strings from ColdFusion date variables.GetHttpTimeString() will do something similar, but would always use GMT as timezone. If you want to use the timezone configured on your server, you'll need this:

<cffunction name="rfc822date" output="no" returntype="string">
  <cfargument name="dtDate" type="date" required="no" default="#now()#">
  <cfscript>
   var strReturn = DateFormat(arguments.dtDate, "ddd, dd mmm yyyy");
   strReturn &= TimeFormat(arguments.dtDate, " HH:mm:ss");
   strReturn &= ' ' & NumberFormat(GetTimeZoneInfo().utcHourOffset*-1,'+00');
   strReturn &= NumberFormat(GetTimeZoneInfo().utcMinuteOffset,'00');
   return strReturn;
  </cfscript>
</cffunction>

RFC822 dates are needed in RSS feeds, among others.

16Apr/100

ColdFusion UDF to intersect two lists

Just a quick one: I have a method that takes a list argument; there is a discrete list of legal values for this list. I want to filter the passed argument list by throwing out all the values which are not contained in the list of legal values.

Of course I could use a nested loop to do this - but for longer lists this is neither fast nor elegant. Again I'll turn to Java for this. ColdFusion's arrays are in fact java.util.Lists, so after converting our ColdFusion lists to ColdFusion arrays, we can make use of the Java-API for lists.

Here's a quick UDF that does what I want:

<cffunction name="listIntersect" output="no" returntype="string" 
	hint="returns values from list 1 which are contained in list 2">
	<cfargument name="lstSand" type="string" required="yes" />
	<cfargument name="lstSieve" type="string" required="yes" />
	<cfargument name="chDelimiter" type="string" required="no"
		    default="," />
	<cfscript>
	var aLstSand  = listToArray(arguments.lstSand,arguments.chDelimiter);
	var aLstSieve = listToArray(arguments.lstSieve,arguments.chDelimiter);
	aLstSand.retainAll(aLstSieve);
	return arrayToList(aLstSand,arguments.chDelimiter);
	</cfscript>
</cffunction>

Usage:

<cfset lstSand   = 'foo,bar,illegalparam,whatever' />
<cfset lstSieve  = 'bar,foo,someotherval,whatever' />
<cfset lstSieved = listIntersect(lstSand,lstSieve) />
<cfoutput>#lstSieved#</cfoutput>

This will output foo,bar,whatever.

11Sep/095

ColdFusion UDF to test if a Java Class implements a method

I recently started implementing a couple of our full text search requirements using Sphinx. I am extremely happy with this search engine, as it's lightning fast and provides some quite easy integration with the data we store in our PostgreSQL databases, is highly scalable and fairly easy to implement in ColdFusion via the Sphinx Client API. 

4Sep/090

UDF to grab a frame from an FLV to JPG

This requires FFMPEG to be installed on your server. Here's the UDF:

<cffunction name="flvgrabber" access="public" output="no" returntype="void" hint="grabs a frame from an FLV at a specified second and renders it as a JPG">
	<cfargument name="strPathToFLV" type="string" required="yes" hint="absolute path to the source flv">
	<cfargument name="strPathToJPG" type="string" required="yes" hint="absolute path to the target JPG; if file exists and is writeable, it will be overwritten">
	<cfargument name="strFrameAtTime" type="string" required="no" default="00:00:05" hint="time at which frame should be grabbed in format hh:mm:ss">
	<cfscript>
		var strTMPPath = '/tmp/';
		var strUniqueFname = CreateUUID();
		var strPathToFFMPEG = '/usr/bin/ffmpeg';
		var strArguments = '';
		var qTempFile = '';
	</cfscript>
 
	<cfif not DirectoryExists(getDirectoryFromPath(arguments.strPathToJPG))>
		<cfthrow message="target directory does not exist">
	</cfif>
	<cfif not FileExists(arguments.strPathToFLV)>
		<cfthrow message="source FLV does not exist">
	</cfif>
	<cfif not RefindNoCase('\.flv$',arguments.strPathToFLV)>
		<cfthrow message="source file must be an .flv">
	</cfif>
	<cfif not RefindNoCase('\.jpg$',arguments.strPathToJPG)>
		<cfthrow message="target file must be a .jpg">
	</cfif>		
	<cfif not RefindNoCase('^\d\d:\d\d:\d\d$',arguments.strFrameAtTime)>
		<cfthrow message="time must be set as hh:mm:ss">
	</cfif>
	<cfset strArguments = "-i ""#arguments.strPathToFLV#"" -an -ss #arguments.strFrameAtTime# -an -r 1 -vframes 1 -y #strTMPPath##strUniqueFname#-%d.jpg">		
 
	<cfexecute name="#strPathToFFMPEG#" arguments="#strArguments#" timeout="30"></cfexecute>
	<cfdirectory name="qTempFile" action="list" directory="#strTMPPath#" filter="#strUniqueFname#-*.jpg" listinfo="name" recurse="no" type="file">
	<cffile action="move" source="#strTMPPath##qTempFile.name#" destination="#arguments.strPathToJPG#">		
</cffunction>

And here's how to use it:

<cfscript>
flvgrabber(strPathToFLV='/some/path/some.flv',strPathToJPG='/some/path/some.jpg',strFrameAtTime='00:00:03');
</cfscript>

Have fun!

Tagged as: , , , , No Comments
21Jul/090

UDF to strip certain chars, but leave UBB tags alone

We are developing a commenting system which is supposed to discourage comment spam by making comments more or less unreadable when they crossed a certain threshold of negative ratings. We decided that we'd like to strip all vowels from the text, though we'd like to keep the UBB-style tags inside the comment unchanged.
You'll find that this last bit makes the whole task a little more complicated than just a simple Regex-Replace. We'll need to use a negative lookbehind, then mark the characters we do not wish to strip, then remove any "unmarked" characters and finally remove our marker.

15Jun/0917

ColdFusion-UDF Wrapper for JTidy to clean up HTML

JTidy is a Java port of HTML Tidy, which allows you to clean up messy HTML. This comes in useful when you need to output some Code which has been created by users. I'll show in some later post how to allow users to actually enter HTML without compromising the security of your site, today I'll just show how to clean up this user-generated code. JTidy will not only generate XHTML-valid code from incomplete code by correctly closing opened tags, it will also do a couple of "prettifying" operations to increase quality of the result.