Saturday, April 3, 2010

Implementing a WebDAV filesystem with PHP and SabreDAV

In this article I'm not going to describe specifically how WebDAV works (it's just a series of HTTP requests). Rather, I'm going to show you how you can implement a solution for your own web site.

This type of functionality can be extremely useful in web applications such as Content Management Systems. For example, my company develops a Content Management System (Recite CMS), which allow users to parts of their web site to their local drive.

In Recite CMS, users can upload new files to their web site just by dragging them to the mapped drive. Alternatively, they can make changes to their web site's CSS files and templates using their favourite text editor.

When implementing a WebDAV solution, you must be aware that the different client platforms (be it Windows, OS X or otherwise) all behave slightly differently. This article includes of what to be aware of, and by the end of this article we'll have a system that works well on most (if not all) platforms.

This article assumes you have a Unix-based Apache web server and that you can create a new virtual host on it to act as your WebDAV server.
Getting Started

To being with our WebDAV implementation, there are several preliminary steps we must perform. These are:

* Create a new virtual host for your web drive
* Download and install SabreDAV
* Set permissions so web drive files on the web server can be updated

For the purposes of this article, we'll be setting up our WebDAV server to run on the URL http://dav.example.com. The main directory of our WebDAV server will be at /dav. Setting it up in this manner is a requirement of using Windows as a WebDAV client.

By the end of this argument we'll be able to map http://dav.example.com/dav as a drive on our local computer.

Windows does not allow you to perform any operations on items in the root directory of a web site. It does however query the root directory to find out which paths within it can be mapped.

Let's use http://dav.example.com as an example. If you were to connect to this URL as a web drive in Windows, it would list our dav directory as the only folder that can be accessed. You cannot create new folders (or files) in this root directory.

In order to set up a WebDAV site we must set up a new virtual host on your web server. Due to some limitations with the native Windows WebDAV client, the WebDAV server must reside at the root of the host it is on.

Additionally, Windows does not allow any operations to be performed on items in the root directory. All operations must take place on items in sub-directories.
Note: WebDAV support in Windows 7 is better than in previous versions of Windows, so this may no longer be a limitation.

Furthermore, the server must either be on port 80 or port 443 (for SSL only). Windows only allows basic HTTP authentication when using SSL - for non-SSL you must either have no authentication, or use digest authentication. We'll talk more about this soon.


No comments:

Post a Comment