Luhn algorithm or Mod10 check

After searching the internet for a whole 10 minutes I found loads of examples of code that will check to see if somthing passes a 'mod10' check but none that will actualy create the check digit in the first place.

So after a bit of research, here it is. A very simple way to generate a mod10 check digit and append it to the end of a number...

<cfscript>
function createMod10(number)
{
   // this is an implementation of the Luhn algorithm or Luhn formula,    // also known as the "modulus 10" or "mod 10" algorithm,    // it calculates the check digit for a number and appends it to the end of the orignal string
   // this function generates the check digit and appends it to the orignal string
var nDigits = Len(arguments.number);
var sum = 0;
   var i=0;
var digit = "";
var checkdigit = 0;
for (i=0; i LT nDigits; i=i+1)
{
digit = Mid(arguments.number, nDigits-i, 1);
      if(NOT (i MOD 2))
      {
         digit = digit+digit;
         // check to see if we should add          if(len(digit) gt 1)
         {
            digit = left(digit,1) + right(digit,1);
         }
      }
      checkdigit = checkdigit + digit;
}
// divid by 10 checkdigit = checkdigit mod 10;
if(checkDigit neq 0) checkDigit = 10 - checkDigit;
return arguments.number & checkdigit;
}

</cfscript>

UPDATE: there was a slight mistake in the first version - this is better

GoDo API Version 1.00 released

www.GoDo.com.au is RedBalloon Days' younger sibling. The site provides instant online booking for activities across Australia.

The GoDo API provides SOAP webservices that allow registered licensees to access availability and pricing information on GoDo's products, as well as place orders. The following are the test urls:

The CFC Docs are available: http://api.godo.com.au/cfcdoc/

To obtain a free licence or more info on using the API, please contact me: duncan at godo.com.au

CFMX Multi Instance Apache config - made simple

If the title makes sense then the rest of the article will be a breeze.

For some the hardest part of Apache is the fact that there is no GUI (Graphical User Interface) for the configuration. To me it's the reason I love apache! While it might be a bit daunting if you are new to Apache the power and flexibility that the text file configuration gives you is simply not feasible in an GUI.

At RedBalloon we have a number of different sites and a number of different servers so being able to manage the config files and lay them out to suit our needs it a huge boost. On top of this, we version our config files using subversion which gives us backups, rollback and a version history.

The windows installer sets up a basic and very well commented config which is a great base to start from. We typically add the following line at the end to allow simple loading of the virtual host config files:

Include cont/vhost/*.conf

Each vhost file consists of a number of related virtual host sections:

Listen 127.0.0.1:80
Listen 127.0.0.1:443

NameVirtualHost 127.0.0.1:80
NameVirtualHost 127.0.0.1:443
#Site 1
<VirtualHost 127.0.0.1:80> #HTTP
   <-- removed for clarity -->
</VirtualHost>
<VirtualHost 127.0.0.1:443> # HTTPS
   <-- removed for clarity -->
</VirtualHost>
#Site 2
<VirtualHost 127.0.0.1:80>
   <-- removed for clarity -->
</VirtualHost>
<VirtualHost 127.0.0.1:443>
   <-- removed for clarity -->
</VirtualHost>

We deliberately add listen directives at the top of the vhost file to easy migration of sites from one machine to another. All settings for a particular site are in one spot.

The sites in any one file are all related - i.e. for redballoon we have the main consumer site, our suppliers extranet site etc all in the one config.

The inner sections of the virtual host looks as follows:

<VirtualHost 127.0.0.1:80>
Include conf/cfinstance/example1-instance.conf

DocumentRoot /path/to/wwwroot
ServerName www.example.com

Include conf/aliases/example1-aliases.conf
</VirtualHost>
<VirtualHost 127.0.0.1:443>
Include conf/cfinstance/example1-instance.conf
Include conf/ssl/example1-ssl.conf

DocumentRoot /path/to/wwwroot
ServerName www.example.com

Include conf/aliases/example1-aliases.conf
</VirtualHost>

By moving all of the common/repeated lines out to well named include files the vhost section is very simple to read and understand. It also makes it easy to add an alias across common sites or change the cfinstance the website is using.

Each of the include files is just the lines that would be directly in the file pulled out to an external file.

Eg - the cfinstance/example1-instance

JRunConfig Serverstore /opt/jrun4/lib/wsconfig/1/jrunserver.store
# Bootstrap port is what specifies which instance to be used - see the JRun admin for the relevant numbers
JRunConfig Bootstrap 127.0.0.1:51020
JRunConfig Apialloc false

Gotchas

SSL certificates

If you are using SSL sites with name based virtual hosting there is a limitation which is: You can only have one SSL certificate - This works fine if all the sites are under the same domain - i.e. site1.example.com & site2.example.com and you have a wildcard certificate - *.example.com. The reason for this limitation is that when the server sends the SSL cert it doesn't yet know which domain you are looking for as it happens at the negotiation phase. Once the SSL cert is in use though the request proceeds correctly and serves the correct site.

Wildcard sites - *.example.com

Apache supports the * operator for sites which mean any subdomain site will match this virtual host. With name based virtual hosting only the first site on a particular IP address will be able to do this. So it wouldn't work to have *.example1.com and *.example2.com on the same IP address. To get around this you need seperate IP addresses for each group of sites.

Conclusion

Apache configs are not as scary as you think - and the documentation is great. They allow you loads of control of the server, are easy to backup and can be used with version control and can be edited with any editor of choice.

Expand Windows NTFS drive on a vmware machine

We use Vmware Server quite heavily at RedBalloon and it gives us a huge amount of flexibility and scalablity without breaking the budget.

An example of this flexibility is how we dealt with a recent problem where one of the drives on our Domain Controller was too small, but we had space available on the underlying Ubuntu Linux VMware Host.

So the plan is to shut down the server, increase the size of the virtual drive, resize the filesystem and we should be good to go.

BTW - this is not for the faint hearted and if done wrong can hose your system.

Here are the steps:

First of all run a chkdsk on the windows drive you are about to resize from within windows:

chkdsk e:

Then make a backup the disk you are about to resize - it's a virtual disk so we copy the files on the host machine. From the VM directory

sudo mkdir backup
sudo cp myDisk.* backup

Expand the original disk with the vmware virtual disk manager and fix the permissions

sudo vmware-vdiskmanager -x 30GB myDisk.vmdk
sudo chown admin:admin myDisk*

You now have a big disk - but the windows NTFS filesystem is only using the original size of the disk. There is no built in way to resolve this in Window but never mind, Linux comes to the rescue again!

Reboot into a Ubuntu Live CD

Open a terminal and run the following commands to resize the filesystem

This is the most dangerous step - as it's where you can really mess up your system.

sudo fdisk /dev/sdb # insert relevant drive instead of sdb
# Then delete your the relevant partition and recreate a larger one
# Note that it needs to be recreated identically except that the end of the partition is bigger. I.e. it needs the same start point on the disk.

sudo ntfsresize /dev/sdb #Again change as relevant

Reboot and windows will run a chkdsk on the drive, then boot and you are all done.

Open source message handler

Message handler (msg) is a very simple and convenient way to keep your application users informed of what is going on in your application.

It provides a consistent way to add messages from within your code blocks which can be displayed at an appropriate time within your display sections.

This makes is very easy to process all of your code/logic a the top of your templates and the display code at the bottom.

Messages can be give specific types (eg: error, warning, success and information) and styled appropriately.

You can see what it looks like or you can download the source.

Open Source Datagrid

As promised last week (and reminded by a post on Remote Synthesis) here is the next piece of useful open source code.

CF Datagrid - it converts queries to a nice presentable datagrid which has support for paging, filtering and sorting.

We use it internally and it happily handles querys of around 100,000 records.

It is available under the CPL 1.0 and I hope you find it useful.

If you find any bugs or issues please post a note on this blog.

Show me the code! | Show me what it looks like!

Update: Added new zip file with build 22 of the datagrid. It includes the missing files.

Coldfusion open source examples

We've been working on getting some more of our re-usable and useful code ready for release as open source.

I'm a very visual person so I like to see what a piece of code does before I download it and try it on my site.

So here are the live demos of some of things that we are about to release:

I'll get the source code for these packages up shortly. Let me know if there are any you'd like to see up before the others.

Cheers, Mark

Time for sharing - CSV read and write library

We've been discussing internally the releasing of some of our useful library code in order to give back to the community that we've got significant amounts of benefit from - Thanks Ray for the blog, thanks daemon for farcry and thanks apache for webserver, linux for my OS and IBM et al for eclipse.

We're currently cleaning up the code a bit for release, but the first piece of code is something that we've had around for quite a while is a solid CSV reading and writing library.

It simplifies the process of creating and reading CSV files that are universally used for simple data transfers.

It is not absolutely complete but it is very functional and allows conversion from:

  • CSV to Query
  • CSV to Array
  • Array to CSV
  • Query to CSV

We use it to great effect for creating super simple exports of queries for reporting.

Hope some people find it useful, and let me know if you find anything that doesn't work how it should.

Cheers, Mark