Archive for May, 2009

VMWare Server 1.0x on Kubuntu 9.04

VMware Inc.
Image via Wikipedia

I just updated my Hardy Kubuntu system to Jaunty 9.04 – everything went pretty smooth, except for getting my VMWare server installation to work again.   For various reasons, I use VMWare server 1.0 (mostly because I need to boot a windows partition which is not supported under VMWare server 2, plus the web interface sucks).  So, I was able to do the system update fine, and the first time I tried to start VMWare, it figured out there was a new kernel version and asked to recompile all the modules.  The configuration process went fine, but when I wen to start vmware, I would get the following error:

/usr/lib/vmware/bin/vmware: symbol lookup error: /usr/lib/libgio-2.0.so.0: undefined symbol: g_thread_gettime

Really annoying!  So, after some googling, came across a few Ubuntu forum threads where peopel were running into the same error, except they were running 8.10 64 bit systems.  Figured their fix was worth a shot, and lo-and-behold, it worked.  Here’s the process I followed:

Step 1: Download the latest 1.09 version
You can find it here.  Download and run the install script.  However, when it asks you to run the configuration, select ‘no’.  The installer will then quit.

Step 2: Download and install kernel patch
You can find the patch here.  Now, un-tar and run the installer script ‘runme.pl’ as sudo.  Now, when the patch asks you to run the configuration for VMWare server, select ‘yes’ and continue.  The configuration script should compile and install all the modules (though I ran into quite a few warnings).  Now, if you tried to run VMWare, you’d still get the ‘g_thread_gettime’ error, so, you need to start VMWare in a special way.

Step 3: Start VMWare
Now, to finally get everything running, you’ll need to run this command:

VMWARE_USE_SHIPPED_GTK="force" vmware

This will tell VMWare to use the correct version of the GIO lib and thus will get you running with no problems.

Update: Step 4
The final step, which I discovered after a few days, is to reset the defaul keyboard mapping for VMWare.  You’ll need to do this if you notice your arrow or function keys are not responding correctly.  To fix this, simply enter the following command into the terminal:

echo xkeymap.nokeycodeMap = true > ~/.vmware/config

Creating Public Calendars with Google Calendar API

Update: I’ve got a new Ruby Gem that improves upon the functionality described below.  Check it out here.

In my previous post I covered how to create and manage google calendars using the Google Calendar API and Ruby on Rails.  One feature I really wanted in order to make my project really user friendly was the ability to make a newly created calendar public without having to log in to the Google Calendar website and make the permissions change manually.  Unfortunately, the documentation for how to do this is sparse at best.

Background
The key to making a public calendar lies in the Google Calendar Access Control Lists (ACL).  Like all other parts of the API, this is simply an Atom RSS feed that you can POST or GET from.  Essentially, ACLs allow you to specify the major permissions for each user that is added to a calendar.  The first problem I ran into was trying to figure out the correct url to POST to.  The google documentation only lists how to change the ACL for a specific user (and by default their primary calendar) rather than for a secondary calendar.  With a little sluething, I was able find a forum post that gives the calendar url:

"http://www.google.com/calendar/feeds/#{cal_id}/acl/full"

Where ‘cal_id’ is the unique calendar id.

To change a permission, you post a new rule to the feed above.  For example, to add a user with basic read permissions, you would POST the following code the to ACL feed url:

<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/acl/2007#accessRule'/>
  <gAcl:scope type='user' value='darcy@gmail.com'></gAcl:scope>
  <gAcl:role
    value='http://schemas.google.com/gCal/2005#read'>
  </gAcl:role>
</entry>

The code above is pretty straightforward, and the google documentation gives a nice overview of what each part of the XML represents. However, I don’t want to add specific users to my calendars – rather I want to make them public to everyone by default.

After looking more at the documentation and at some forum posts, I figured out that instead of specifying a user, you can set the ’scope’ type to ‘default’ and omit the value attribute.  The role value can then be ‘read’ to allow for read only access to the public.  Therefore, the XML to create a public calendar looks like this:

<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/acl/2007#accessRule'/>
  <gAcl:scope type='default'></gAcl:scope>
  <gAcl:role
    value='http://schemas.google.com/gCal/2005#read'>
  </gAcl:role>
</entry>

Extend GCalAPI
The code for adding this to the GCapAPI code, as I did in my previous post, is as follows:

class GoogleCalendar::ServiceBase
  def make_calendar_public(cal_id)
    #change permissions on the calendar to public
    path = "http://www.google.com/calendar/feeds/#{cal_id}/acl/full"
    request = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
                    <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/acl/2007#accessRule'/>
                     <gAcl:scope type='default'></gAcl:scope>
                     <gAcl:role value='http://schemas.google.com/gCal/2005#read'></gAcl:role>
                  </entry>"
    uri = URI.parse(path)
    res = do_post(uri, {"Content-Type" => "application/atom+xml", "Content-Length" => request.length.to_s}, request)
    if res.code.to_i != 201
      raise "Could not change permissions on calendar: "+res.body
    end
  end
end

You can now add the ‘make_calendar_public’ function call wherever you need, passing the relevant calendar id.

Super Basic CAPTCHA with Joomla and PHP

A website I’m developing that has a fairly complex form has suddenly started getting a ton of spam (about 200 spam posts per hour).  The client needed a CAPTCHA system super quick, so I had to get something temporary up ASAP.  There are very good image CAPTCHA generator services out there, but I didn’t have time to integrate in the third party image and test everything.  So, I came up with the approach outlined below – for a low traffic site, and as a temporary solution, this works great

Note: this is by no means a secure CAPTCHA system, and should not be used with the expectation that it is bulletproof (quite the opposite, but it would take at least some human intervention to figure out how to beat it, which is perfect for a low traffic site).

Premise

A CAPTCHA works by presenting a user with a hard to decipher (for a machine) image containing a random string.  The user then has to enter the text string in a form box.  If the string matches the image, then the user is assumed to be authentic (at least not a bot) and the form is successfully submitted.  If the string doens’t match the image, then the form is rejected.

Programmatically, there are a few features to note.  First, you need to have an electronic version of the string in the image to compare the user’s text to.  This can be done two ways, depending on where you want your validation to occur.

  1. Server Side Validation: this is probably the most common, as it integrates well with 3rd part CAPTCHA systems and is the most secure.  In this case, you generate a random string and a secret key.  You then generate an image based on the secret key and the random string.  You then take the user’s text string, combine it with the key, and if the value matches the original, then the CAPTCHA is passed.
  2. If you want to do client side validation, you can take an AJAX approach to the previous method, or you can add the CAPTCHA to a javascript form validator.  In this case, you don’t want a secret key or the text string on your page, so you can use a MD5 or SHA hash of the string.  This way, you can include it in the validation javascript without fear of any secrets being given away.  The validation function simply compares the hash of the user’s input with the hash in the function and if they match, the CAPTCHA passes.

Since I’m already using a javascript form validator in my website, and I was really crunched on time, I decided to go with approach 2.  So, here’s a step by step guide for building a super quick, functional, though not particularly secure CAPTCHA system.  A little background: I’m using Joomla 1.5 to build the website and the form, so the examples include some Joomla specific code.

Implementation

  1. Get your CAPTCHA images.  You’ll need 5 to 10 premade CAPTCHA images.  You can either create them yourself or find them on the web,  but be mindful of copyright issues in using someone else’s.  Number the images ‘Captcha-0′, ‘Captcha-1′, etc.
  2. Generate an MD5 hash of each CAPTCHA’s text.  Write it down and note the number of in the image name of the CAPTCHA photo for each one.
  3. In your PHP file, add an array containing all the hashes, in the order that the images are in:
    $captcha = array("ca608814f1afdbc3ce97b00baf6e3545",
                     "c900ea6ac5e2beca0c23a45c2b779fbf",
                     "39437118c696911d4c58c686a9cfd3d0",
                     "4f5767844def01697cb2bb0e175d5500",
                     "8a2e4dd82d392386031ac56f8026f4da");
    
  4. Next, user the rand() function to generate a random number based on your array size:
    $num = rand(0, (count($captcha)-1));
    
  5. Add the corresponding MD5 hash (by referencing the correct position in the hash array) to your validation function.  In this case, I’m using the built in validator for Mootools 1.1 and Joomla:
    <script type="text/javascript">
    
    Window.onDomReady(function() {
            document.formvalidator.setHandler('captcha', function(value) {
                    if (MD5(value) == '<?php echo $captcha[$num]; ?>') {
                            return true;
                    } else {
                            return false;
                    }
            });
    });
    </script>
    
  6. Add the image to your form by matching the image number with the random number:
    <img src="/media/system/images/Captcha-<?php echo $num; ?>.jpeg"/>
    

And that’s it – now you have a very basic CAPTCHA system implemented.  As long as a user types the correct text in the text box you create, the form will submit.  Otherwise an error is returned to the user.  Of course, the effectiveness of this goes up the more images and hashes you have, but unless you have someone putting at least some effort into beating this, it should keep the random bot from posting on your website’s form.

Seabourne Consulting

A lot’s been happening in my world recently, including starting my own IT/Web consulting company, Seabourne Consulting.  seabourneconsulting2The focus of this company is to develop and implement web sites that allow companies to work more effectively and efficiently from any location.  Our expertise is in Ruby on Rails, Qt, C++, Joomla! CMS and Adobe Flex.  We’ve got a bunch of really cool applications in the works, and already have 3 client projects under way.  If you are interested in our services or in talking about a project, contact us here.

Pho

One of my favorite meal of all time of my favorite meals is Pho, the Vietnamese beef noodle soup.  Living here in Seattle, where there is a very large Vietnamese population, has given me the opportunity to taste many different versions of Pho, most of it authentic and delicious.  I’ve always been interested in trying to make the Pho broth, but for some reason had the belief that it was a closely guarded skill of old Vietnamese men.  I was thrilled to find this recipe, which is both easy, and more importantly, just as good as any Pho I’ve had in a restaurant.  The oxtail meat is simply delicious, so don’t forget to pick the bones after cooking and add the meat back to the broth right before serving.

Ingredients

For the broth:

  • 4 pounds Oxtails; cut into 1 1/2 to 2-inch pieces and trimmed of fat
  • 3-inch piece of ginger, unpeeled
  • 1 large onion, halved and unpeeled
  • 4 Garlic Cloves, lightly crushed
  • 1/3 cup nuoc mam (fish sauce)
  • 6 whole star anise
  • 5 whole cloves
  • Cinnamon stick
  • 1 tablespoon fennel seeds
  • 4 bay leaves
  • Thin Sliced Brisket or Flank Steak

To serve the soup:

  • 1 pound 1/4-inch rice noodles
  • 2 bunches scallions, sliced thin
  • 1/2 cup tightly packed fresh cilantro leaves, roughly chopped
  • 1/2 cup parsley, roughly chopped
  • 1/2 cup basil, approximately, whole fresh plants (minus roots) if possible
  • 3 large limes, cut into wedges and seeds removed
  • Red chile paste or sliced fresh hot chilies (optional)

Instructions

  1. Add the oxtails to a large pot and cover with about 4 inches of water.  Bring to a boil, skimming the foam from the top as it accumulates.
  2. Meanwhile, char the onion and ginger under a broiler until lightly black, about 10 minutes.  Remove the charred skin and then add to the pot with the garlic cloves.
  3. Toast the cinnamon stick, cloves, star anise, bay leaves and fennel in a small skillet until fragrant, about 4 minutes.  Add the spices to a paper tea filter or cheese cloth sack and add to the broth.
  4. Once the broth has reached a boil, reduce to a very slight simmer and add the nuoc mam.  Continue simmering, uncovered, for 4 to 5 hours, or until the oxtail meat is tender and falling off the bone.
  5. Remove the vegetables and spices from the broth and discard.  Using a slotted spoon, remove the oxtails to a plate and refrigerate.
  6. Move the pot to the refrigerator and let chill overnight, or until the fat has solidified on top.
  7. The next day, remove the congealed fat from the top of the broth.  Return to the stove and bring to a simmer.
  8. Once the stock is reheated, add the sliced brisket or flank steak and cook until the meat is tender, about 1 hour.
  9. Once the meat is done, soak the rice noodles in hot water for 10 minutes.  Before serving, add the oxtail meat from the bones back to the broth.  Place a serving of rice noodles in a wide bowl and using a slotted spoon, add the meat from the broth on top.  Ladle the broth over the noodles and meat and serve with garnishes.

Using Google Calendar API in Ruby on Rails

Update: I’ve got a new Ruby Gem that improves upon the functionality described below.  Check it out here.

Update 2: Rewrote this article to use GCal4Ruby.

A new project of mine involves project managment and milestones.  One feature I was interested in was natively integrating the Rails app with Google Calendar so that the milestone information would be available to anyone who wanted to subscribe to it.  I looked around and found a couple of google calendar integration libraries, but suprisingly there isn’t a fully implemented GData API that allow access to all the features of the Google Calendar API.

  • Google Calendar: a pretty simple and full featured google calendar library in ruby, with optional Rails plugin.  A good option for anyone who’s interested in accessing or exporting Google Calendars into HTML or Excel.  Though the Event support is excellent, my application needed finer grained control over multiple calendars under each user, and this library didn’t include the functions to determine calendars programmatically.
  • GCalAPI: also a good and simple API for Google Calendar, though it provided a better interface for finding calendars associated with a user account.  Unfortunately, the documentation is a little lacking, and there was no built in way to create a new calendar.  But, I ended up using this library despite the drawbacks.

These are two very good libraries, but haven’t been updated in a couple of years, and were missing some key pieces of functionality that I wanted.  So, I’ve created a new Ruby library (available as a Gem) called GCal4Ruby that implements all of the major features of the GCal API, including recurrence, attendees and reminders.  One of the main improvements in GCal4Ruby over the other two libraries is that you don’t have to remember the ‘feed’ urls for Google; all that is handled transparently for you.  This article describes how to use GCal4Ruby to interact with Google Calendars through Ruby on Rails.  Be sure to read the documentation for all available functions and features.

Setup
Setting up the GCal4Ruby gem is straightforward: just install it using sudo gem install gcal4ruby or by adding gem.config ‘gcal4ruby’ line to your Rails project environment.rb file. Then, all you need to do is run rake gems:install to install the gem through rails.

My application includes a ‘project’ model and a ‘milestone’ model.  Each project has a unique google calendar associated with it, though they all appear under one account.  You can either use a Google Gmail account for this, or you can your own Google Apps account if you have one.  You’ll probably want to add your username and password to the environment.rb file as static constants for reference later.

Creating a Calendar
The one thing that the GCalAPI library can’t do by default is create a calendar programmatically.  GCal4Ruby includes the ability to do this.  Here’s how to create a new public calendar from within a Calendar model in Rails:


#Step 1: Setup feed url and create account service using constants
service = GCal4Ruby::Service.new
service.authenticate(GOOGLE_LOGIN, GOOGLE_PASS)

#Step 2: Create a new calendar
calendar = GCal4Ruby::Calendar.new(service)
calendar.title = self.title # tile = 'A New Calendar'
calendar.summary = self.description
if calendar.save
 return true
else
 return false
end

#Step 3: Make the calendar publicly available
calendar.public = true

Adding an Event
Adding an event is straightforward.  Basically, its a three step process:

  1. Authenticate and load your Google Calendar Account
  2. Load the specific calendar you’d like to work with
  3. Construct and add your event to the selected calendar

I added this code to the milestone model which is within the save method.  Here’s the basic code for accomplishing the three steps outlined above:

#Step 1: Setup feed url and create account service using constants
service = GCal4Ruby::Service.new
service.authenticate(GOOGLE_LOGIN, GOOGLE_PASS)

#Step 2: Load calendar data for the calendar associated with the project
calendar = GCal4Ruby::Calendar.find(service, 'A Test Calendar', :first)

#Step 3: create a new event and save
event = GCal4Ruby::Event.new(calendar)
event.title = self.title
event.content = self.description
event.where = "N/A"
event.start = self.start_date
event.end = self.end_date || self.start_date
event.all_day = self.all_day_event
if !event.save
  return false
end