A Selective Register Gloabls Function for PHP

A friend was lamenting the lack of register_globals in his PHP setup (a tad late, as it became non-default back in April 2002). While register_globals was terrible in terms of security, it did excel in its simplicity.

function register_my_globals($allowed, $first, $second = array())
{
    $vars = array_intersect_key(
                array_merge($first, $second),
                array_combine($allowed, $allowed));
    foreach ($vars as $key => $value)
        $GLOBALS[$key] = $value;
}

// First parameter defines allowed variables
// The second and third are arrays in which to look for these keys.
// Keys in the third array override keys in the first.
register_my_globals(array('green', 'blue'), $_GET, $_POST);
// Take variables only from POST data
register_my_globals(array('cake'), $_POST);

The above snippet allows you to recreate the behaviour of register_globals, but on a case-by-case basis. You must specify the names of the variables you wish to allow, and the source arrays you’d like them taken from (almost always either HTTP GET parameters, or HTTP POST data).

Apache versions and .htaccess files

The other day I was looking into a problem someone was reporting with an Apache RewriteRule, only to conclude that it was using features of the Regex library which weren't available in their version of Apache. I found a means of detecting the different versions of Apache using the mod_version module. This allows you to write htaccess files which can fall back to other rules for older versions of Apache. Unfortunately it's only been available since version 2.0.56, but given that this was before the first release of version 2 it's fairly safe to assume that anything without mod_version will be running Apache 1. I will concede that this sounds very obvious, but there was a surprising lack of results in google for any of the keywords that I thought to try. For the benefit of anyone searching for this; below is an example of how you can use mod_version in practice:
<IfModule !mod_version.c>
        # Earlier than version 2.0.56, so almost certainly 1.x
        # as 2.0.63 was the first release of version 2.
</IfModule>
<IfModule mod_version.c>
        # Version 2.0.56 or later
        <IfVersion < 2.2>
                # Before version 2.2
        </IfVersion>
        <IfVersion >= 2.2>
                # Version 2.2 or later
        </IfVersion>
</IfModule>

Automating iSync and Bluetooth

With some pointers from friends I've beaten applescript into submission and coded up a script to automate the bluetooth iSync syncing with my phone.
The script opens up System Preferences and enables Bluetooth, then launches iSync and instructs it to sync. It will then leave Bluetooth in the state that it found it in.
The code is below for anyone who might find it handy :) Oh and for the record this is written to run on Mac OS X Leopard.

display dialog "Sync?" buttons {"Ok", "Cancel"} default button 2
if the button returned of the result is "Cancel" then
        stop
end if

tell application "System Preferences"
        activate
        set current pane to pane "com.apple.preferences.Bluetooth"

                tell application "System Events" to tell window "Bluetooth" of process "System Preferences"
                        set was_on to the value of checkbox "On"

                        if value of checkbox "On" is equal to 0 then
                                tell checkbox "On" to click
                        end if
                end tell
        end tell

        tell application "iSync"
                synchronize

                repeat while (syncing is true)
                        delay 5
                end repeat
                set syncStatus to sync status

                if syncStatus is not 2 then
                        if syncStatus = 3 then
                                set syncStatus to "completed with warnings"
                        else if syncStatus = 4 then
                                set syncStatus to "completed with errors"
                        else if syncStatus = 5 then
                                set syncStatus to "last sync cancelled"
                        else if syncStatus = 6 then
                                set syncStatus to "last sync failed to complete"
                        else if syncStatus = 7 then
                                set syncStatus to "never synced"
                        end if
                display dialog "syncStatus: " & syncStatus
        end if
        quit
end tell

if was_on is equal to 0 then
        tell application "System Preferences"
                set current pane to pane "com.apple.preferences.Bluetooth"

                tell application "System Events" to tell window "Bluetooth" of process "System Preferences"
                        if value of checkbox "On" is 1 then
                                tell checkbox "On" to click
                        end if
                end tell
                quit
        end tell
end if

Not your regular TI-83

I've only ever owned one graphical calculator, a TI-83, which was bought for me around when I started Secondary School. It was the device on which I broke my programming teeth, making crummy games and utilities to make maths exercises less tedious.

The other week I was experimenting with the HTML Canvas element for another little side-project of mine when I was reminded of the Graph Plot screen which my old calculator has. Essentially it lets you enter a Cartesian equation which it will then plot on the screen.

Subsequently, over a couple of days I put together a Javascript powered equation plotter, with much the same functionality as my TI-83. Oh and in the process I found a bug in the WebKit nightly, which they swiftly fixed.

You're free to use the source to the how you wish, if you're that way inclined (it's a client-side web script so it's practically open source off the bat). Under the hood it makes use of the MooTools javascript framework, though I've not made use of its features extensively, so It would only be a matter of an evening's work to remove this dependancy.

A case of bad cropping

Recently, by pure chance, I've been working for two different research projects which both have involved Java Applets.

The first project ran afoul of bug #6669818 which prevented the applet from connecting back to the originating server during LiveConnect calls, in Mozilla Firefox on a Windows XP platform.

Fortunately there is a workaround for this; by passing the task off to another thread which was started in a non-LiveConnect part of the Applet's lifecycle, the task is then executed in another thread which is not subject to the same malfunctioning security check.

If you'd rather not have to create a thread just for this, a quick hack is to pass the event off to the applet rendering thread using the SwingUtilities.invokeLater(Runnable); function.

In the second project I've now encountered another platform-specific Applet quirk, this time with Mac OSX.

Using Javascript it is possible to change the size of an Applet on the page in the same way you would any other DOM element, however on Mac OSX any browser using the Java Embedding Plugin (Firefox, Opera and others) will fail to resize Java Applets correctly.

I've knocked up a simple test page which allows this to be tested, and shows a potential workaround.

There appear to be two problems cropping up; the first is that the Browser does not notify the Applet that it ought to resize. If you leave the "Notify Java" check-box un-checked when submitting the form, the browser expands the area the Applet ought to be using, but the applet continues to draw in it's original position.

The second issue is that when axis are changed and Java is manually notified the behaviour still does not perform as it ought to. The clipping region of the Java applet rendering is not updated correctly, and thus only part of the applet is correctly rendered.

There is a temperamental workaround for this, by programatically resizing one axis and then the other in quick succession, but this seems to fail as often as it works, depending on whether the page is being loaded from nothing, refreshed, no-cache refreshed or returned to via the browser 'Back' action.

In the interest of getting this bug fixed the code to the Applet resize test page is linked from the test page and free for all to use.

Bugs:

Unrestrict BBC Radio iPlayer

As long as I can remember, the BBC's online music streaming service has restricted the control you have on playback streams, restricting the embedded realplayer controls to allow you pause, skip 5 minutes or skip 15 and change the volume. I can understand the legal obligations they may have, but from a piracy perspective stopping people skipping tracks is irrelevant, its only use is as a measure to stop people re-listening to the same track without having to go back to the start each time.

The controls on Mac OS X are less restrictive, but still restrict you to pause, fast forward or stop, however some simple javascript manipulation of the page can enable the regular RealPlayer controls, complete with a seek-bar.

Bookmark either download the source file or view the bookmarklet source. Unfortunately blogger won't let me create an actual bookmarklet link, so you will manually have to save the bookmarklet into a bookmark in your browser.

Google Maps Placemark

This post has been sitting for quite a while now, pending me having time to sit down and write some text to go alongside the code. It would appear that I now have the time to write it up.

Back when I was writing my Google Maps property finder I was using Google's Geocoding API to get a set of co-ordinates based on a search string.

Specifically I was using the getLocations method of the GClientGeocoder object. This would return a number of Placemark objects with a bunch of information about the potential matches. They don't really document this object, but to be honest they don't really need to, below is an example Placemark expressed in JSON-ish notation.

{   "id": "p1",
        "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
        "AddressDetails": {
                "Country": {
                        "CountryNameCode": "US",
                        "AdministrativeArea": {
                                "AdministrativeAreaName": "CA",
                                "SubAdministrativeArea": {
                                        "SubAdministrativeAreaName": "Santa Clara",
                                        "Locality": {
                                                "LocalityName": "Mountain View",
                                                "Thoroughfare": {
                                                        "ThoroughfareName": "1600 Amphitheatre Pkwy"},
                                                        "PostalCode": {
                                                                "PostalCodeNumber": "94043"
                                                        }
                                                }
                                        }
                                }
                        },
                "Accuracy": 8
                },
        "Point": {
                "coordinates": [-122.081783,37.423111,0]
        }
}

The only thing is, if you want to get a specific element, the tree will not necessarily be the same each time, so traversal is a pain. The code below will condense all this into the root level of an object, so you can pick out the key/value pairs you want, and easily tell if some aren't there.

var Placemark = new Class({
        initialize: function(placemark){
                this.traverse(placemark);
        },
        traverse: function(item, key) {
                for (var key in item) {
                
                        if (key == 'prototype')
                                continue;
                          
                        if(typeof(item[key]) != 'object')
                                this[key] = item[key];
                        else
                                this.traverse(item[key]);
                }
        }
});

One thing to note is that this was written for to use the Mootools class system, though it shouldn't be very hard to remove this dependancy.

Spring MVC Wizards & Form Taglib

I've been building a JSP based website for a course module titled "Technologies for Electronic Commerce". It's finished now, but I've only just got round to writing up this post.

As part of this I was trying to make a multi-page form, using Spring's AbstractWizardFormController and spent an hour or so trying to make the form submission change the form page. My problem was that it wasn't immediately obvious how to use the form taglib with the Wizard architecture.

The docs mentioned using request parameters, which was a bit confusing as I was using a POST form submission, however all you actually need to do is add one of these parameters to the form data (usually using a button).

Finish and cancel actions can be triggered by request parameters, named PARAM_FINISH ("_finish") and PARAM_CANCEL ("_cancel"), ignoring parameter values to allow for HTML buttons. The target page for page changes can be specified by PARAM_TARGET, appending the page number to the parameter name (e.g. "_target1"). The action parameters are recognized when triggered by image buttons too (via "_finish.x", "_abort.x", or "_target1.x").

For example:

<input value="Submit" name="_target1" type="submit"/>
This will request to change to page 2 of the form (remember Array base is 0). Because of the way forms work, you can have a number of these (either as clickable images or buttons) and only the one the user eventually selects will be submitted with the request.