Port code to Drupal 7.

Could probably stand a bit more clean-up, but it works.