Archive for the ‘ColdFusion’ Category

How to resize and crop Image Using Coldfusion script

April 7, 2013 Leave a comment

I had requirement on my site  to resize Image after uploaded by user in two sizes one for view page and one as thumbnail, I found some script on web and improved it to fit in my requirement. here it is. I expect this code will reside in a CFC function


var l = {}; // local struct

/* Read file  and we will resize and crop it to make 200×200 */

l.img = ImageRead(“path of the image to be resized”);

l.resizeOriginalFlag = 0;
If( l.img.height GT l.img.width )
If( l.img.height GT 200)
ImageResize( l.img,’200′,”);
l.resizeOriginalFlag = 1;
l.fromX = l.img.Height / 2 – 100;
else if( l.img.width GT l.img.height )
If( l.img.width GT 200 )
ImageResize( l.img, ”,’200′);
l.resizeOriginalFlag = 1;
l.fromY = l.img.Width / 2 – 100;
ImageCrop( l.img,l.fromY,0,200,200);
If( l.img.height GT 200)
l.resizeOriginalFlag = 1;


<cfif l.resizeOriginalFlag>
<cfimage action=”write”
destination=”<path with filename>”
<!— <cfdump output=”/sites/resize.html” var=”#cfcatch#” format=”html”> —>

How to configure Railo – Tomcat – Apache on MAC Lion Mountain 10.8

January 22, 2013 5 comments

Last week I updated my operating system from Snow Leopard  to Lion Mountain 10.8, system has upgraded itself.

Only two problems I faced:

1) Little Snitch got removed (V 2.2), I had to buy Licence for upgrade of version 3.02 – Lion cost me more 😦

2) I had ColdFusion 9.01, which stopped working as Lion upgrade had reinstalled Apache removed old httpd.conf and httpd-vhost.conf files (actually renamed them as httpd.conf~previous.

I tried to move virtual sites from old to new but it didn’t work.

Web Sharing option got removed from System Preference – To get it back I found this post which provides a tool to get it in Sys Pref under Other:

Explanation, good article to read: Restore Web Sharing in Mountain Lion 

Get the tool from here: WEB SHARING IN OS X MOUNTAIN LION

I made few changes to Apache config files, by default it uses web server document folder here:


I have changed this and few other settings in httpd.conf (/private/etc/apache2/)

1) Find ServerAdmin and change email address to yours one (around line no 153)

2) Find ServerName and change it to cfdev also add following in hosts file (/private/etc/) cfdev  (now you can get to your webserver by localhoost/ cfdev/

Make sure you uncomment this line (at line no 481) Include /private/etc/apache2/extra/httpd-vhosts.conf

3) create a folder sites or whatever name you waant to give at root level

4) Find DocumentRoot (line no 169) and change it to “/sites”

5) Find < Directory and change it with “/sites”

6) Open file httpd-vhost.conf from /private/etc/apache2/extra/

7) Uncomment NameVirtualHost *:80 (Line no 19)

8) Add following virtual host (default) site

<VirtualHost *:80>
DocumentRoot “/sites”
ServerName cfdev
DirectoryIndex index.html


9) Add / Create index.html in /sites folder as a place holder

10) sudo apachectl restart (to pick-up the changes

now open browser and browse http://cfdev or http://localhost and you should see your default web page

Railo Installation:

Download your railo installer from here

Scroll down and pick MacOS installer infront of Railo Server with Tomcat 7 title

Open it up and follow the default steps:

It will normally Install under this location and picks port 8080 – /Library/Railo/tomcat/

now if you browse http://localhost:8080/ you should see the tomcat page

1) Open web.xml from tomcat/conf folder

Add following at around line 422 (This will help if you use CFWheels frame-work


2) Open server.xml from tomcat/conf

3) go to line no 90 and un comment following:

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />

This will help us to use apache server on port 80 to create virtual sites.

at around line 123 you will see default website for this Tomcat server

create a folder under /sites folder – which will be your first local site say – firstSite

inside the folder create index.cfm

now create a virtual site in server.xml like this: (above </Engine>)

<Host name=”” appBase=”webapps”
unpackWARs=”true” autoDeploy=”true”
xmlValidation=”false” xmlNamespaceAware=”false”>
<Context path=”” docBase=”/sites/firstSite”/>

save the file and restart the server (Tomcat):

4) open terminal window and go to /Library/Railo/tomcat/bin

run following command: sudo sh (to shutdown the server)

Now run this command to start it: sudo sh

5) add following in hosts file:

now browse and you will see your first site index.cfm running

6) open httpd.conf from /private/etc/apache2/

add following (Thanks to Anuj Gakhar this post)

<Proxy *>
Allow from

ProxyPreserveHost On
ProxyPassMatch ^/flex2gateway/(.*)$ ajp://localhost:8009/flex2gateway/$
ProxyPassMatch ^/messagebroker/(.*)$ ajp://localhost:8009/messagebroker/$1
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://localhost:8009/$1$2

now open httpd-vhosts.conf from /apache2/extra/ folder and add virtual site (key to this set up is you need to add virtual site pointing to the same folder with same url under both servers)

DocumentRoot /sites/firstSite
DirectoryIndex index.cfm
<Directory “/sites/firstSite”>
AllowOverride All
Order allow,deny
Allow from all
Options +Indexes
ErrorLog firstsite.error.log
LogLevel warn

restart your apache server and

browse  and you should see your site running on port 80


ColdFusion – Cross Site Scripting

March 8, 2011 1 comment

I had to fix one site which was sharing server with other sites to be PCI compliant for Cross Site Scripting and DB Injections.

Here are the few things I did to accomplish this.

In Application.cfc, I have added following:

<cfset this.scriptProtect = “all” >

Note: If you are passing object/script or any html  etc tags in form or query variable, this will break your code

Make sure you have following section in your cfusion/lib/neo-security.xml:

<var name=”CrossSiteScriptPatterns”>
<struct type=”coldfusion.server.ConfigMap”><!– this one is for site scripting –>
<var name=”&lt;\s*(object|embed|script|applet|meta|iframe|style|img|form|xss|body|html|head|title|input|layer|br|bgsound|link|xml|frameset|table|div|hr|base|a%20href|a href)”>
</var><!– following is for SQL injections –>
<var name=”;.*(select|insert|update|delete|drop|alter|create)”>

Now all you need to do is find the respective string in your out-put and display the (send user to) appropriate error/ message page

Categories: ColdFusion, Secure

Extending CFC – DAO

August 17, 2010 2 comments

I came across very strange behaviour of extending CFC to another CFC for my DTO’s, which I was using to send data back to my Flex application using AMF.

My Main parent CFC had 3 properties.
I created new CFC and extended to the above CFC and added 3 more properties.
It works fine with in ColdFusion, but when I used it to return to my Flex application with type binding to my ActionScript DTO.
This wasn’t sending the parents properties to my Flex application.
To fix this I had to add all properties in my extended CFC (Child one).

Has any one come across the similar scenario?

How to Improve Flex data transfer from CF?

August 15, 2010 Leave a comment

I found this article and realised that transferring data from CF (CFC objects) to Flex application using this way will reduce the packet size and improve the performance.

You don’t need to create an object of the CFC type you want to send data across in your array, but what you need to do is take following steps and you can improve your application’s performance:

Following code is how I used to send data across to my Flex App:

<cfloop query=”myQuery”>


local.result = createObject(“component”,”com.philip.myproj.myobj”);



arrayAppend(local.results, local.result);



The above method is how most of the developers use and it works fine only downside is this passes the complete object with all setter’s and getter’s and makes it bulky.

The following method is very light weight to pass the objects around and its documented in here: Writing the ColdFusion CFCs

you just need to change the object creation step with creating a simple Struct and you need to add an extra line to declare its a type of following CFC Value object likt this:

local.result[“__type__”] =”com.philip.myproj.myobj”;

Here is how you write the code using above type declaration:

<cfloop query=”myQuery”>


local.result = structNew();

local.result[“__type__”] = “com.philip.myproj.myobj”;

local.result[“prop1”] = myQuery.val1;

local.result[“prop2”] = myQuery.val2;

arrayAppend(local.results, local.result);



And Flax / Flash is sharp enough to pick this up and automatically link with ActionsScript VO class and you will get typed data.

Categories: Adobe, ColdFusion, Flex

Few frequently used Regular Expressions by me.

August 13, 2010 Leave a comment

In ColdFusion:
1) To match a valid UK postcode and returns an array with one item of valid postcode otherwise returns array with no item in it:
Pattern to test (with or without spaces):

^ : is for start of the string

$: is for end of the string

?: zero or one letter (optional)

{1,2}: minimum 1 or maximum 2 characters

Pattern Example
A9 9AA M1 1BA
A99 9AA N12 1UD
AA99 9AA RG6 1WG
Special case for National Girobank

<cfset arr = REMatch(“^[A-Za-z]{1,2}[0-9R][0-9A-Za-z]?[[:space:]]?[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z][A-Za-z]$”,request.postcode)>
<cfif len(arr) GT 1>
<cfreturn true >
<cfreturn true >

2) Another one for UK post code, little bit complicated

<cfset arr = REMatch(“^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$”,request.postcode)>

Few references for UK Postcode regex)

Ref Site Link 1, Ref Site Link 2

3) To validate number with minimum 11 char and maximum 14 car using isValid function


4) To remove all characters from the string except alpha numeric:

REReplace(stringvar, “[^a-zA-Z0-9]”,””,”All”);

In JavaScript:

To restrict any thing except alpha numeric (case insensitive)

string.replace(/[^a-zA-Z0-9]/g, “”)

To Replace brackets, dots and spaces from string globally

string.replace(/[\(\)\.\-\ ]/g, ”)

In ActionScript:

1) Phone number check through regEx validator:

private const UK_PHONE_NO_REGEX:String = ‘^[0+][0-9]{10,14}$’;

number can start with 0 or plus (+) , with minimum 10 and maximum 14 characters

2) removes double quotes from string:

var pattern1:RegExp = new RegExp(‘”‘, “g”);


JavaScript Regular expression replacement:

Add comma separator to numbers like 10,000.00

variable.toString().split(“”).reverse().join(“”).replace(/(\d\d\d)(?=.)/g, “$1,”).split(“”).reverse().join(“”);
if you want to separate by (.) dot than replace comma with dot in here “$1,” in above string.

Complax Array Sorting in ColdFusion

August 4, 2010 Leave a comment

I needed to sort my cold fusion array which had complex Structure as CFC DTO in the array item.

I found very useful blog of Martin

Its very easy to implement. Actually I changed a little bit, I saved the UDF in a CFC and extended my main CFC to this new CFC and it worked straight away.

Here is the code how to call it.

arrayToSort = ,
sortKeys = [{'keyName':'yourKeyToSortOn'},{'sortOrder':'Ascending OR Descending'}],
doDuplicate = false,
useGetterMethods = false);

To download the code

Happy Coding

Categories: ColdFusion Tags: