Useful Libraries

During my web coding career, I’ve wound up creating a fair number of functions and library routines to make various tasks easier. The following are ones that I’ve collected together into libraries that others might find useful.

Quick Links:

The “view source code” links on this page will send your browser a document with a Content-type: header of text/plain. This should hopefully cause your browser to display it, rather than trying to interpret it. Similarly, the “download library” links will send a Content-type of application/octet-stream, which should cause your browser to give you an option to save it. If these behaviors don’t occur, please check your browser configuration, or use some other standard mechanism (such as “right-click and Save As...”) to force the appropriate behavior.

If you want to use one of these libraries for any purpose, please see the licensing information at the bottom of this page.

libdirbrowser

This library implements an entire AJAX-based directory browser, which can be displayed on a web page using a <select> form element of whatever size you desire. The page that includes the <select> element simply needs to include a single JavaScript file; this contains routines that will contact a server-side script and retrieve directory listings. The download package includes a server-side script in PHP to handle the AJAX requests.

Functions

While there are nearly a dozen functions in this library, only two should ever be called from outside the library itself. For full descriptions and calling syntax for those two functions, use the button to the right. If you want to investigate all the library internals, please read the source code.

Usage

For full installation and usage instructions, see the README.txt file in the Zip download file, below. Abbreviated version: put the .js library and associated PHP script on your server, editing options to configure; then set up an HTML page including a form and a pair of JavaScript event handlers.

Demonstration

If you’d like, you can see a version of libdirbrowser in action. The libdirbrowser demonstration page will allow you to browse around the directories on my own personal site, and submit a value to a debugging script.

Download

view JavaScript library source code
view server-side PHP module source code
Full download: contains installation instructions, documentation, client-side JavaScript library, server-side PHP script, sample/demo HTML page showing usage, and two GIFs for enhanced display on Gecko-based browsers: (libdirbrowser.zip)

libenglish.php PHP Library

This PHP library implements a variety of routines for trying to promote natural-sounding English. It includes functions for displaying lists of things in English syntax, giving file sizes in human-readable ways, obfuscating email addresses, and other tidbits.

Functions

There are 6 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.

Usage

To use functions from libenglish.php in your PHP script, include the line include(/path/to/libenglish.php) at the beginning of your script. Then simply invoke any of its functions normally.

Download

view source code
download library (libenglish.php)

libenglish.js JavaScript Library

This JavaScript library contains a subset of the functions from its PHP sister, libenglish.php (above). It also adds a couple of functions that handle things that PHP has built in.

Functions

There are 3 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.

Usage

To use functions from libenglish.js in a web page, simply link to the library with <script language="JavaScript" type="text/javascript" src="/path/to/libenglish.js"></script> in the <head> of your document. Then invoke any function normally within a JavaScript block.

Download

view source code
download library (libenglish.js)

libsuntime.php PHP Library

This library, written in PHP, contains a pair of functions that are useful for wishing someone a “good morning”, “good evening”, or whatever, based on when the Sun actually rises and sets at a particular location.

Functions

There are 2 functions in this library; use the button to the right in order to see full descriptions and calling syntax for each one.

Usage

Include the line include(/path/to/libsuntime.php) at the beginning of your PHP script; then invoke any of its functions normally.

Download

view source code
download library (libsuntime.php)

safeopen() PHP Library

A single function, written in PHP, that opens a file safely for reading or writing, providing file locking and error reporting. If the file you attempt to open is locked, safeopen() will automatically retry it every X milliseconds until Y seconds have passed, then time out. You can set X and Y by altering a pair of variables declared at the beginning of the function. By default, the retry frequency is 250 milliseconds, and the final timeout is 5 seconds.

Syntax

resource safeopen ( string filename, string error_msg [, string filemode, int lockmode ] )
Will attempt to open the file named by $filename, then flock() it, retrying until it succeeds or the timeout delay elapses. Returns a filehandle resource on success; on failure, it will return false and put an error message into the $error_msg variable. By default, files are opened for reading only and the lock mode is exclusive (LOCK_EX). The $filemode variable will take any standard fopen() mode as its argument, and the $lockmode variable will take any standard flock() locking operation.

Lock operations automatically have LOCK_NB added to them, so it’s unnecessary to add that to the $lockmode. This allows safeopen() to abort after the timeout limit is reached, rather than blocking indefinitely.

Portability

This function has been tested and deployed on Linux, and works fine there. On Solaris, some work may be needed. At a bare minimum, you'll have to add the -l permission to the file’s mode before it can accept an exclusive lock (man -s 2 chmod or man flock for details). It has not yet been tested on FreeBSD or other Unices.

Usage

You could add this function to a library or other PHP file that you’re already include()ing in your script, or just copy-and-paste the entire thing into your script.

Download

view source code
download function (safeopen.php)

Licensing

Creative Commons LicenseThese libraries are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. Please feel free to study them, to use them in your non-commercial web site, and to develop other (non-commercial) libraries and applications based on them. Please don’t be too worried about the “non-commercial” aspect of this license, either. I’m not at all opposed to people using my work commercially, I’d simply like the opportunity for royalties if you think you’re going to make a lot of money. Please contact me before using these libraries commercially; I’m willing to work out very reasonable licensing terms.