Categories
Computers Linux Mac

Convert Rails 3 project to use jQuery

Rails 3 supports unobtrusive javascript to decouple the content from the logic in a way similar to how CSS has helped web designers to decouple the content from the layout.

By default Rails 3 comes with Prototype. But more and more people are switching to jQuery. The project Rails-3-jQuery on Github simplifies the process of switching out the Prototype support to jQuery. It includes jRails that provides drop-in replacements for Prototype and Scriptaculous helper methods to make it a seamless transition.

To update a project, just issue the following command in the project root

rake rails:template 
   LOCATION=http://github.com/lleger/Rails-3-jQuery/raw/master/jquery.rb

For more information on the project, check the project page on Github.

Categories
Computers Linux

Backup of Lazy8Web data

Lazy8Web is a free web application for basic bookkeeping tasks. I use it myself for the community where I live and I can really recommend it.

However, I needed to add a way to do backups. There is a function to export all data to XML but you must be logged on to do that and the authentication system doesn’t allow just using wget with username/password.

To automate the process I wrote the following little snippet that I run as a cron job. It logs onto the site, downloads the data and saves it to a file with the current date and time as part of the filename.

#!/usr/local/bin/ruby
#
# Author: Martin Bergek (http://www.spotwise.com)
# Dependencies: Ruby, Mechanize (http://mechanize.rubyforge.org)
#

require 'rubygems'
require 'mechanize'

# Make changes here
base_url = 'http://www.example.com'
username = 'user'
password = 'password'
log_folder = '/home/user/'
# No changes below this line

# Login
agent = Mechanize.new
page = agent.get(base_url + '/index.php?r=site/login')
form = page.forms.first
form['LoginForm[username]'] = username
form['LoginForm[password]'] = password
page = agent.submit(form, form.buttons.first)

# Get backup for all companies
xml = agent.get_file(base_url = '/index.php?r=company/exportAll')

# Save backup
t = Time.now
logfile = log_folder + t.strftime("%Y%m%d-%H%M%S.xml")
File.open(logfile, 'w') {|f| f.write(xml) }
Categories
Computers Gadgets Mac

Possible uses for the Apple iPad

The genie is out of the bottle. Steve Jobs today presented the brand new Apple iPad and I’m thrilled. The only thing I missed in the presentation was the usual slide: “Available now!”. While I wait patiently I will probably make up all kinds of uses for this device. As if I needed any arguments for getting one.

So what are some possible uses (apart from the obvious ones that was presented by Apple today)? Here is a list to get us started. Please add more suggestions in the comments.

  • Remote control for my living room audio system.
  • Backseat video for my three-year-old when we will do a long-haul drive to France this summer. Here is a market for a device cover manufacturer to step up to the plate.
  • Digital sheet music content holder for piano players. Just scan all the score and add an iPad holder to your piano. Perhaps with voice controlled page flipping.
  • Digital cooking book for the kitchen. Just needs a holder that sticks to the fridge.

Come to think of it I probably need more than one device.

Categories
Computers Linux Mac

Roll your own certificates

I host a bunch of Internet services. Some, like this site, are for public use but most are intended for personal use. And since I want to keep them personal, security is important.

Most Internet applications can be secured by SSL certificates. This includes HTTPS access for web sites, IMAPS instead of IMAP, encrypted access to subversion repositories. The list goes on.

For a number of years I have been creating my own certificates with the help of OpenSSL. Much can be said about OpenSSL but it is not exactly point-and-click. I created a few scripts to assist me but still I found myself looking through the documentation whenever I needed to create a new certificate or renew an existing one.

To solve this I have now created the attached Bash script to wrap OpenSSL to be able to set up a two-level certificate hierarchy. The result is a menu-driven text based application that will no doubt save me time in the future. Perhaps it can help you too.

I use this to create certificates to be used by Apache, Subversion, Postfix, Dovecot, Microsoft IIS, Microsoft Exchange and a few others.

There are some limitations to this. The hierarchy will be two levels deep, not more and not less. In other words you will get a root certificate authority and one or more subordinate certificate authorities. User certificates will be signed by one of the subordinate CAs. Also, while it supports subject alternative names it only does so for DNS names (I only needed that for Exchange 2007 support). For real-world certificate authorities the certificate signing requests (CRSs) are created outside the certificate authority. Since I will be handling everything myself I have set it up so that CSRs are created inside this application. For that reason it is imperative to keep access to the CA folder structure secure so that the private keys are not compromised.

To use this script just download and extract the attached file. Inside the folder which will be created there will be two files. The file ‘ca’ is the Bash script and the other is a template configuration file. Run the script from within the folder. Certificates will be available in the folder ‘all’ and backups of the certificate hierarchy will be placed in the folder ‘backup’. For more help, please comment.

The script has been verified on Ubuntu 9.04 and Mac OS X 10.5. I don’t think there are any dependencies that aren’t already met by standard OS installations.

Download: ca_v1.tar.gz

Categories
Computers Linux Mac

Audio feedback to shell output

The -f switch to the tail command is a great help when it comes to keeping track of what is going on in some log file. But sometimes you just can’t keep your eyes on the output to see if something is happening. At those times it would help to have some other indication that there is activity. The following small Perl script emits a beep whenever there is a line of data being output.

#!/usr/bin/perl
while(<>) 
{
	print "\007" . $_;
}

Just tack the script somewhere in your path. Then, whenever you want some audio feedback just pipe the output to the Perl script (i.e. {some command} | beeper.pl).

Categories
Computers Web design

WordPress iPhone theme

For WordPress blogs that want to support iPhone users the plugin WPTouch is very useful. The plugin detects the presence of iPhone (as well as some other devices) and sends a special theme instead of the blog’s standard theme.
Wordpress on iPhone with WPTouch plugin

Categories
Computers Linux Mac

Unbrick a Netgear WNDR3300

I borrowed a brand new Netgear WNDR3300 from a colleague the other day. I quickly powered it up and accessed the internal configuration web page only to be met by the automatic update function which I hadn’t seen in previous Netgear wireless routers. Before I knew it I had mistakenly aborted the update and the router was more or less dead. The power LED just kept blinking, the router replied to ping but the configuration web page was nowhere to be seen.

I managed to solve the issue by downloading the firmware file from the Netgear site. It is a 3MB file with a .chk file extension.

I then uploaded the firmware using TFTP. The following step-by-step guide is from my Mac but should work on Linux. Start by connecting the wireless router to the computer but don’t power it up. Then open a terminal and type:

tftp 192.168.1.1
binary
rexmt 1
timeout 60
trace
put WNDR3300_V1.0.29_1.0.29NA.chk

Directly after the last line you should then power up the WNDR3300 and wait. Hopefully it will pick up the firmware file from the TFTP client. Once the file has been transferred it will take a few minutes while the router flashes its memory with the new firmware. This is normal. Just wait and hopefully you will have your wireless router back.

Categories
Computers Gadgets Linux

Track your whereabouts with a Nokia phone

This is a follow-up article to a previous story on how to use the GPS in a Nokia phone. The last article described how to use a Python script to query the phone on the position. In this article we will add to the Python script to make it also dispatch position reports as UDP packets as well as a server script that saves the data to a sqlite3 database which is then used to display a web page with a map. The end result will look something like this:

tracker

All files are contained in the attached file at the end of this article. The server scripts have been written for Ubuntu 8.04 but will no doubt work on other distributions. Please note that you will need to install a couple of packages (php-sqlite3, php5-sqlite3 and libdbd-sqlite3-perl) to make the scripts run. The various scripts assume that they are all placed in the same location (i.e. in the web site folder). Read the security section below to ensure that the scripts are not publicly available.

Client script
The script ‘tracker.py’ should be copied to the phone according to the instructions in the previous article. Before copying it to the phone it must be edited. You will want to change the server host, the port and the secret.

Sqlite3 database
The data is kept in a sqlite3 database. The initial database is created by running the script create_database.sh. Do not run that script again as it will completely wipe the database.

Server script
Edit the file ‘tracker_server.pl’ and set the port number (line 8 ) and the secret (in the regexp on line 23) to the same values that you set in the client script. The server is then started by running the server script ‘./tracker_server.pl &’. Add the command to /etc/rc.local if you want it to start automatically when the server is restarted.

Web pages
Create a web site and point the document root to the folder where you put the files. Then reload the server.

Start the script
Finally, start the script on the phone and wait for it to acquire a GPS fix. This should cause the new position to be reflected on the web page.

Security
As mentioned above it a wise thing to prevent access to the script files if they are located in the same folder as the web pages. The easiest is to add an .htaccess file in the web folder with the following content:

<FilesMatch "\.(db|pl|sh|py)$">
Deny from all
</FilesMatch>

Attachments
tracker.zip

Categories
Computers Gadgets

Use Python to access the built-in GPS in a Nokia phone

For some reason I have agreed to participate in one of the longest bike rides here in Sweden this year, the 300 km Vätternrundan. During the race every bike rider has an RFID tag on the leg which is read as you pass various points along the course. Apparently there are only a handful of locations where the tags are read and I wanted to do better than that.

I thought about using a phone with built-in GPS and send the positions in real-time to my server so that my loved ones can see where I am during the race. First I hoped to be able to use my iPhone 3G but the lack of background processes in a non-jailbroken iPhone meant it was a no-go. I then turned to my previous phone, a Nokia N82.

Doing a native application didn’t seem necessary for this type of application. I really just need a small hack to send periodical updates to an Internet server (which I will also write). I knew that it was possible to write Python applications for Series 60 phones but to be able to access internal resources like the GPS required signed applications.

Various Internet sources spoke about how to access the GPS information from Python but they all seemed to lack some detail that made it not quite work. At the end I got it working and this is how I did:

  1. Download and install Python for S60
  2. Download Python Script Shell with a test UID
  3. Sign the Python Script Shell by using this web page
  4. Download LocationRequestor with a test UID
  5. Sign the LocationRequestor .sis file with the same web page as above
  6. Move the two signed .sis files to the phone and install them
  7. Move the attached script to your phone (e.g. using Bluetooth) and place it under C:\Python or E:\Python. Personally I prefer the latter since I can then reset my phone and have all the files intact on the memory card
  8. Start Python and run the script

The above has been tested on a N82 (v 30.0.019) but may work on other similar phones with built-in GPS (e.g. N95).

In a future article I will follow up with an updated script that also sends the GPS data to a remote server. Stay tuned.

Attachments
gps.zip

Categories
Computers Linux

Ruby on Rails application could not be started

If you try to run an application with Passenger (mod_rails) and get the error “No such file or directory – /nonexistent” it could be due to some files in the Rails application being owned by root. To fix the issue just change the owner to some other user.

passenger_error

css.php