Monthly Archives: May 2009

Basic WebDAV Bridge For Cloud Files

Awhile back I signed up for Skitch, a service that lets you create quick screenshots, edit them, and upload them to a variety of services. Unfortunately, despite the abundance of choices, Cloud Files support is not an option. This was irritating, and since I’ve never been one to let phrases like “ridiculous kludge” or “bereft of all sanity” deter me an idea was born. Why not write a (very) rudimentary WebDAV interface in PHP and use that to bridge to Cloud Files?
To accomplish this feat it turns out we really only need 3 distinct pieces of functionality. The actual plumbing that communicates to Cloud Files will be provided by the PHP Cloud Files API.

  1. HTTP Basic Auth to capture the necessary Cloud Files credentials
  2. PUT method for WebDAV
  3. DELETE method for WebDAV

The code below represents the minimum subset of WebDAV functionality required to support Skitch without issues (uploading and deleting). It is nowhere near a complete WebDAV implementation. Additionally, some code (mime_types function and some logging and error handling) have been omitted. You can download the full package for use on your own server.

require('cloudfiles.php');
 
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
	list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':',base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'],6)));
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
	header('WWW-Authenticate: Basic realm="CloudFiles"');
	header('HTTP/1.1 401 Unauthorized');
	echo 'You must be authorized!';
	exit;
}
 
$auth = new CF_Authentication($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
try {
	$auth->authenticate();
}
catch(AuthenticationException $e) {
	echo 'CF Authentication failure';
	exit;
}
$conn = new CF_Connection($auth);
$skitch_container = $conn->create_container('skitch');
$public_uri = $skitch_container->make_public();
 
switch ($_SERVER['REQUEST_METHOD']){
	case 'DELETE':
		$file = substr($_SERVER['REQUEST_URI'] , ( strrpos($_SERVER['REQUEST_URI'], '/') + 1) ) ;
		try {
			$skitch_container->delete_object($file);
		}
		catch(NoSuchObjectException $e) {
			echo 'no such object to delete';
			exit;
		}
		catch(InvalidResponseException $e) {
			echo 'invalid response to delete request';
			exit;
		}
	break;
 
	case 'PUT':
		if($datain = fopen('php://input','r')){
			while(!@feof($datain)){
				$data .= fgets($datain,4096);
			}
			@fclose($datain);
		}
		if(isset($data)) {
			if (isset($_SERVER['PATH_INFO'])) {
				$file_name = substr($_SERVER['PATH_INFO'],1);
			}
			$mime = mime_types($file_name);
			$object = $skitch_container->create_object($file_name);
			$object->content_type = $mime;
			try {
				$object->write($data);
			}
			catch(InvalidResponseException $e) {
				echo 'Invalid response error from CF';
				exit;
			}
			catch(SyntaxException $e) {
				echo 'Syntax exception error from CF';
				exit;
			}
		}
	break;
}

To wire this up now, we have just a few steps to perform.

  1. WebDAV can’t return the public base URL for the container, so use another program to create a container named “skitch”, mark it public, then copy the URL.
  2. Go to the Webpost tab in Skitch and create a new account using WebDAV.
  3. Set server to the server you uploaded the file to, user to your CF username, password to your API key, directory to the name of your script (potentially /path/to/script.php if it isn’t at the root of your server’s httpdocs), and finally paste the base URL (without a trailing /). A screenshot of a sample config can be seen below.

skitch

Success! I’ve been using this several weeks and haven’t had an issue, but this hack does make me wonder how usable a full blown WebDAV translation layer would be…

Right Click On A Mac Trackpad

rightclickmacThis one is pretty basic, but nonetheless very important. As OS X laptops become more prevalent among less technical users I’ve noticed that there are quite a few people who aren’t aware that right click is available on their laptop. To enable this in Leopard (10.5)1, open system preferences, select the trackpad prefpane and check the “secondary click” (or “secondary tap” if you have tap to click turned on). Now simply rest two fingers on the trackpad and click (or tap two at the same time for tap click).

  1. This process is essentially the same in Tiger (10.4) and Panther (10.3), but the trackpad preferences are found within the “Keyboard & Mouse” prefpane.

Full Keyboard Access in OS X

fullkeyboardaccess
A coworker got a Macbook Pro for work today and he immediately noticed that by default you can’t tab through all controls in OS X. Enter Full Keyboard Access!

To enable this incredibly useful feature open System Preferences, select Keyboard & Mouse, and then choose Keyboard Shortcuts. At the bottom of the window you’ll notice two radio buttons. Change it to “all controls” and you’re set! You can now tab (or arrow key) through radio buttons, check boxes, buttons, et cetera in almost anything.

Highlighted items will have a thin color around them (in your configured highlight color, typically blue or grey) and you can activate them via space bar. There are also a few other really useful (and undocumented) shortcuts you can use with full keyboard access turned on:
Ctrl-F2 (Focus on Menu Bar)
Ctrl-F3 (Focus on Dock)
Ctrl-F5 (Focus on toolbar)
Ctrl-F6 (Focus on palette)

There are also shortcuts for F4 and F7 but I have not found much use for them.

Title Bar Tricks in OS X

drilldownStandard OS X title bars have several hidden features that can be very helpful. If you hold the command key and click the title of a window1 you will get a menu that allows you to drill down through the hierarchy to the root of the filesystem.

However, that’s not the only useful trick you can perform. If you have a document open in an application you can click on the icon in the title bar and, after holding for a brief second until the icon darkens to indicate it has been selected, drag it. This acts as a standard drag/drop but without having to go locate the file in the Finder! For example, you can save a document in TextMate, Excel, or Word and then drag/drop to Mail or Entourage and it will open a new email message with the document already attached.

  1. This works in the Finder, Word, Excel, TextMate, et cetera. Essentially any open window that represents a document on your filesystem.

Spelling Suggestions In OS X

suggestionsOne of the lesser known OS X niceties is the ability to get spelling suggestions/completions from any standard Cocoa text input. To accomplish this, simply hit the Escape key1) in any supported application (iChat, Mail, and TextEdit are some examples) and be amazed. I find this feature very useful for quickly obtaining the correct spelling of a word without taking your hands off the keyboard to get a correction. Sadly, many applications use their own text handling for various reasons so this won’t work universally (Word, TextMate, et cetera).

  1. In some cases Escape already has a bound behavior. For these situations pressing Option-Escape may give the expected dropdown. For example, in Adium the Escape key will clear your entry, but if you hit Option-Escape you will get the completion dropdown.