A practical guide to URL rewriting for IIS

I use my own PHP-based CMS for almost all my web development. This is great, because since I know the code inside and out, I can make whatever the client wishes happen. It has a lot of nice, reusable features (plugins) that make development of a generic website pretty short. Still, all was not well because one of the things it didn’t support was URL rewriting. In this blog post, I’ll set out to describe how this is done, what pitfalls there are and how they can be avoided.
Read more


Automating website & MySQL backups

I have a web server with a number of clients’ websites on it. It’s necessary to backup these websites every day, since clients use a content management system to make changes regularly. These changes can be updates to a website’s MySQL database, or they can be changes to the files stored within these websites. What I’d like is to backup the MySQL database and the filesystem for each website, every day, at a specific time. The backups must rotate: when there are, say, five backups, I want the oldest one to be removed as the newest one is written. Also, I’d like the backup solution to send me an email every day after it’s completed the backups with a summary of the procedure.

So, in summary, my needs are these:

  • Define a list of websites to back up
  • For each site, backup (dump) the MySQL database
  • For each site, backup the website’s file structure
  • Send an email to one or more people with a summary of the backup process.

It’s possible to do this with a shell script (like AutoMySQLBackup does). However, AutoMySQLBackup does not backup file systems or send email. Also, shell scripting tends to be messy code, so I decided to use Ruby.

Configuration file

First off, I’d like to store the list of websites to backup in a separate configuration file so that I can edit this list easily. Also, for reusability, I’ll store database access credentials and email addresses there too. The simplest way of making a configuration file to be read by Ruby is to actually write the configuration file in Ruby, like so:

BACKUPDIR = "/backup/webserver"
ROTATE = 5
DBUSER = "root"
DBPASSWORD = "myrootpassword"
EMAILS = [ "alex@email.com", "john@email.com" ]
WEBSITES = {
 "sample.com" => {
   "path" => "/usr/local/www/apache22/data/sample.com",
   "database" => "sampledb"
 },
 "example.net" => {
   "path" => "/usr/local/www/apache22/data/example.net",
   "database" => "exampledb"
 }
}

This file stores a variable ROTATE which indicates the number of backups to keep before throwing away the oldest one. For each website, I specify the path to the files to be backed up, and the name of the MySQL database. The configuration file will be included and parsed automatically by the backup script, since it is plain Ruby code.

Backup script

The backup script begins by requiring SMTP support, so that we can send emails later. It also starts an output buffer (“output”) where we will store all messages generated by the script to be included in the email. Before starting the backup procedure, we start a begin…rescue block so that me may catch any exceptions thrown by Ruby, in order to include these in the email as well.

require "net/smtp"
output = "Webserver backup script"
begin
  # Load config file:
  require "/usr/home/alex/backup-script/config.rb"
  # Does the backup directory exist?
  if not FileTest::exists?(BACKUPDIR)
    raise "Backup directory #{BACKUPDIR} does not exist."
    exit
  end

The script now loops through the list of websites defined in the configuration file, creating a backup directory with the name of the website for each if it doesn’t already exist:

  WEBSITES.each do |name, website|
 
    output << "\r\n\r\nBacking up #{name}:"
 
    # Establish backup dir
    path = BACKUPDIR + "/" + name
 
    # If website dir does not exist, create it.
    if not FileTest::exists?(path)
      Dir.mkdir path
      output << "\r\n  Directory #{path} created."
    end

Next, the script enumerates the subdirectories that already exist in the website’s backup directory. This is because we will create a subdirectory with date backup’s date for each backup (e.g. 20110810-105535, for 10 August 2011, 10:55:35). These directories are then sorted alphabetically, so that the least recent backup of the website is first in the list.

    # Get entries inside dir with modification times (sorted first to last)
    entries = []
    Dir.entries(path).each do |entry|
      next if entry == "." or entry == ".."
      mtime = File.mtime(path + "/" + entry).to_f
      entries << [ mtime, entry ]
    end
    entries.sort! { |x,y| x[0] <=> y[0] }
    output << "\r\n  #{entries.length} backups found (max #{ROTATE-1})."

The total number of backups found is compared to the value of ROTATE. If there are too many backups, the latest one(s) (first in the list) are removed.

    # Remove least recent entries if more than ROTATE available:
    while entries.length > ROTATE - 1
      entry = entries.shift
      cmd = "rm -R -f #{path}/#{entry[1]}"
      `#{cmd}`
      output << "\r\n  Removed #{path}/#{entry[1]}"
    end

Having cleaned up excess backups, the script now creates a fresh folder, naming it with the current date and time:

    # Create new folder for backup:
    subdir = Time.now.strftime("%Y%m%d-%H%M%S")
    Dir.mkdir path + "/" + subdir
    output << "\r\n  Created directory #{path}/#{subdir}"

If a website has a database defined in the configuration file, the script now calls mysqldump to create a backup of the database inside the newly created backup subdirectory. The backup is gzipped as well. Note that a full path to mysqldump must be provided, since cron, which we will use later to run our script at specific times, does not include a path to mysqldump in the shell that it runs in.

    # Dump database (if required)
    if website.has_key? "database"
      # Dump db
      cmd = "/usr/local/bin/mysqldump -u#{DBUSER} -p#{DBPASSWORD} #{website["database"]} | gzip > #{path}/#{subdir}/#{name}.sql.gz"
      `#{cmd}`
      output << "\r\n  Dumped database #{website["database"]} to #{path}/#{subdir}/#{name}.sql.gz"
    end

If a website has a path to files defined in the configuration file, the script now uses tar/gzip to create a tarball of the entire website file structure, recursing into subdirectories.

    # Dump code (if required)
    if website.has_key? "path"
      # Copy code
      `cd #{website["path"]}; tar -czf #{path}/#{subdir}/#{name}.tar.gz *`
      output << "\r\n  Created zipped tarball of code in #{path}/#{subdir}/code"
    end
  end

This completes the loop that backs up all the websites. We now end our rescue clause in order to catch any exception thrown by Ruby during this process. The exception text is appended to the running log (output) as well as written to standard output.

rescue StandardError => error
  output << "Error occurred: " + error
  puts "Error occurred: " + error
end

All that is left to do is to send the output off through email. This is easy to do (any one reason we’re using Ruby):

# Mail output:
Net::SMTP.start('127.0.0.1') do |smtp|
  output = "Subject: Webserver backup procedure\r\n" + output
  EMAILS.each do |email|
    smtp.send_message output, "alex@email.com", email
  end
end

Adding the script to cron

We can now add the script to the system’s crontab in order to run at regular times. We’ll write a small shell script that launches the script using the bash shell, to make sure that cron has access to a powerful shell to run in:

#/usr/local/bin/bash
/usr/local/bin/ruby /usr/home/alex/backup-script/backup.rb

The following entry is added to the system crontab (/etc/crontab). This will make sure that the script runs every day at 22:00.

# Run webserver backup script
00      22      *       *       *       root    /usr/home/alex/backup-script/backup.sh

SASS and CSScaffold

I think the concept that SASS brings to the table (or CSScaffold, for that matter) is one we’ve all had when we play with CSS and think, “Gee, I would be nice if you could use variables and constants here, and if you could duplicate less code.” And then we would think of splitting our CSS up into many little files, since they’re easier to organize by function, only to find that that wasn’t such a hot idea because a browser will have to make a new HTTP connection for each one to download it.

So here’s SASS/CSScaffold adding just those features that CSS was missing. But is it all good news? I’d say on the whole, yes, but here’s a few points:

SASS requires that you compile your stylesheets every time you update them. My typical development cycle is make a little change to CSS (one one monitor) and hit refresh in my browser (other monitor) to see if the change did what I wanted it to do. That would have to change: now I would need to compile my CSS before I hit refresh. Not insurmountable, but it’s one more thing I can and will forgot and then I’ll think, “Hey, now why didn’t that change do anything?” only to find out after some head-scratching that I forgot to compile.

CSScaffold doesn’t seem to have this problem: since it’s written in PHP, it’ll run every time the CSS is requested from the server. I’m sure the authors have built in some sort of caching, so it should be quick enough. That actually sounds handier to me than SASS does, merely because I don’t need to compile. So the question is then, is CSScaffold just as good, better, or worse? If it’s just as good, I’ll go with it instead of SASS!

But is what SASS/CSScaffold do really that new? Like I said at the start of this post, it’s an idea all of us have thought of… and implemented! It’s always been possible to produce CSS through PHP. You can put a link to a PHP file in your page’s header, have it output a text/css header and you’re good to go. That’ll allow you to use variables, like SASS, constants, like SASS, functions and mixins, like SASS, all at zero cost since you had PHP anyway. You’ll basically only need to write the fancy gradient functions that SASS adds.

In order to add caching, you could pull your CSS through Smarty, thus prettifying the syntax a bit (it never feels quite right having PHP produce actual HTML or CSS what with the separation of code and presentation, so using Smarty gives a fuzzy warm feeling of righteousness). You could even write some spiffy new functions for Smarty, thus creating your own Sassy Smarty. So why all the hullabaloo?

Well, for one thing… SASS does more than I ever implemented with a CSS/PHP/Smarty approach, so hats off for that. But I still don’t like the compilation requirement.


Dynamic CSS through PHP

When writing CSS,you will find yourself repeating information a lot, which is always a bad thing in programming. CSS 2 lacks constants, which would allow us to define a value once and refer to it many times. Instead, we are forced to repeat the actual value many times, making updating CSS a process that is prone to errors.

Also, in order to reduce the number of connections a client must make to the server, it’s necessary to place all CSS in a single file. But this may mean that you end up with a lot of possibly unrelated CSS in a single file, making it difficult to navigate while you’re developing. There are times when it’s simply handier to have lots of  small files instead of one big file, but it’s just not practical for download by your visitors.

These two problems can be resolved by loading your CSS through PHP. Instead of serving the CSS file directly, i.e.

<link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>

you can have the server load a PHP script that produces CSS like so:

<link rel="stylesheet" href="css/style.css.php" type="text/css" media="screen"/>

Note that this will only work if the scripts emits a text/css header:

<?php
  header("Content-type: text/css");
  ...
?>

Now your PHP script can define some constants that you simply insert into your CSS:

<?php
   header("Content-type: text/css");
   $mycolor = "#aaa";
?>
 
p {
  line-height: 1.1em;
  color: <?php echo $mycolor; ?>
}

Your script could also load various CSS files for processing and output the result in one go, solving the second problem we found. But we can do better still. You can have your PHP script use Smarty to produce the CSS, making the use of contants easier (and prettier):

<?php 
  header("Content-type: text/css"); 
  require_once "../smarty/Smarty.class.php";
  $smarty = new Smarty();
  $smarty->template_dir = "../smarty/templates";
  $smarty->compile_dir = "../smarty/templates_c";
  $smarty->cache_dir = "../smarty/cache";
  $smarty->config_dir = "../smarty/configs";
  $smarty->compile_check = true;
  $smarty->caching = 0;
  $smarty->display("file:style.css");
?>

The file style.css would be the main style sheet manifold; it could load other (sub-) stylesheets. For instance:

{assign var="defaultfont" value="normal 11px/1.2em Arial, sans-serif"}
{assign var="thinborder" value="solid 1px #aaa"}
{assign var="inputcolor" value="#666"}
{include file="sys/global-reset.css"}
{include file="sys/base.css"}
{include file="sys/loader.css"}
{include file="control/accordion.css"}
{include file="control/ajaxtable.css"}
{include file="control/button.css"}

The values that were assigned to defaultfont, thinborder and inputcolor can be used in the sub-stylesheets like so:

input
{
  border: {$thinborder};
  color: {$inputcolor};
}

FireFox 4 does not like script.aculo.us builder

After upgrading to FF4 I noticed that some of my JavaScript, which had been working perfectly fine, stopped working. I was able to isolate the problem to the use of the script.aculo.us Builder class to create a script element, like so:

var head = $$("head")[0];
js = Builder.node("script", { type: "text/javascript", src: path });
js.onreadystatechange = function() { if (js.readyState == 'loaded'
  || js.readyState == 'complete') js.onload(); };
js.onload = function() { console.log("loaded!"); };
head.insert(js);

However, the onload event would never be triggered. In fact, Firebug indicates that the JavaScript file I’m trying to load is never actually loaded from the server. So it’s back to basics without using script.aculo.us’s Builder:

var head = $$("head")[0];
var js = document.createElement('script');
js.type = 'text/javascript';
js.onreadystatechange = function() { if (js.readyState == 'loaded'
  || js.readyState == 'complete') js.onload(); };
js.onload = function() { console.log("loaded"); };
js.src = path;
head.appendChild(js);

And guess what: this works. The file is loaded. Now why does this happen? The new script element is in fact added to the DOM; I can see that in Firebug. But it never loads the JavaScript from the server.

Playing around with script.aculo.us’s builder.js shows that the script tag cannot be created through innerHTML but must be created through document.createElement instead. I don’t know why script.aculo.us tries the innerHTML approach first, but it does – and it works. It just doesn’t load the javascript file. If I deliberately make the innerHTML approach fail, it falls back to document.createElement, which works.

This is not the whole story, though. When adding attributes to the newly created element, builder.js again tries to use innerHTML before using document.create. And again, skipping innerHTML to make it fall back to document.create works.

The reason innerHTML is used can be found here, according to the source, but I could not access this URL at the time of this writing.


Creating a Forward Proxy with WEBrick

Building a simple forward proxy in Ruby with WEBRick requires very little code. Here is a small sample that forwards all requests but for the example.com domain, which it blocks.

require 'webrick/httpproxy'
 
def handle_request(req, res)
  puts "[REQUEST] " + req.request_line
  if req.host == "example.com" || req.host == "www.example.com"
    res.header['content-type'] = 'text/html'
    res.header.delete('content-encoding')
    res.body = "Access is denied."
  end
end
 
if $0 == __FILE__ then
  server = WEBrick::HTTPProxyServer.new(
    :Port => 8123,
    :AccessLog => [],
    :ProxyContentHandler => method(:handle_request))
  trap "INT" do server.shutdown end
  server.start
end

The interesting bit is the handle_request method. WEBRick provides us with the request and response instance for each request, so that we can check what’s being requested and block certain URLs. Since the response is also already available, we can even perform content filtering.


Free Charts Library Development

It’s happened a number of times now that I’m writing a piece of software that needs charts of some sort. Sometimes I’ll need a line chart, sometimes a bar chart, a pie chart, maybe even a Gauss diagram. There are cases that I’ll need a tiny chart that’s not too accurate but shows a tendency, shown as a bitmap (what seems to be called “sparklines”), and sometimes I’ll need a detailed line chart of visitors to a site by month, or number of pushups by day for a member of a gymnasium, or even a world map with data by country. I’ve done projects needing such charts in C#, in PHP, and in Flash.

There’s a problem: there doesn’t seem to be any (quality) free charts package out there. Good packages for PHP are commercial (i.e. JpGraph), but there’s open source stuff available (e.g. pChart) but this is obviously not portable to C#, or Flash. The Google Charts API seems to be the most flexible thing out there, but obviously it’s only available on the web and only for sites that are actually online, i.e. not for intranets. Yet Google Charts is exactly what I want: well thought out, easy to use, very flexible.

Since the backbone of a charts library is some math that allows us to calculate the axes, projection of points on the chart, creating the legend etc., it should be written in a portable way so that it becomes available and reusable in multiple programming languages. Google’s Charts API could be the basis for how charts are specified.

What I’d really like to do is write the algorithms to produce various chart types in pseudocode, so that they could then be implemented in various programming languages, possible then using the Decorator pattern to add features that are language-specific (lots of interesting things can be done with Flash, like mouseovers and zooming).

This’ll be a pet project for a while…


Adobe Flash Full Install for Firefox

If you’re trying to set up Adobe Flash on Firefox, be it a new version or an upgrade, perhaps you’re experiencing problems with Adobe’s download manager. It just doesn’t seem to download anything. No problem – you can download the full package in one go for installation on Non-IE browsers here. Close Firefox, execute & done. Be sure to verify success by checking your Flash version here. Also remove the Adobe download manager, since you won’t need it anymore.


Firefox Addons for Web Development

Web development gets a lot easier when you use Firefox, which comes with an agreeable number of bells and whistles built in. However, it gets much better still when you install some of the addons that the Mozilla community has on offer. In particular, the following addons make the web developer’s life much easier.

Read more


Cross-browser min-height

No IE, please

The min-height declaration is poorly understood by Internet Explorer. However, here’s a trick to force minimum height on a block element that works in all browsers:

height:auto !important;
height:200px;
min-height:200px;

This would set a min-height of 200px on all browsers. All browsers except IE6 will respect the !important flag and ignore the second height property, while IE6 will still use the second height property, then expand as needed.