1 | 2 | 3

1 - 5   [11]

Also relates to iPod Touch

No, don't get excited as this is not a blog about a great new e-book reader application for the iPod Touch post jailbreak. It simply outlines how I organised my e-books on my local network so I could access them efficiently from Safari on the Touch.

Currently I have chosen not to jailbreak my iPod Touch since it will invariably lead to far too much time preoccupied with installing and testing 3rd party applications! After all it is an MP3 player not a phone so do I really need all those extra applications?

But the one thing I do really need is an e-book reader and this was one of my main reasons behind investing in an iPod Touch to allow me to read my e-book collection on a more portable device than my iBook. Accessing PDF files on the Touch is simply a case of visiting the PDF URI in Safari. This is fine for my purposes and the clarity and sharpness of the screen make reading PDFs very enjoyable. A few important features such as bookmarking are absent but this serves as an adequate stop-gap for me at present. So I just had to find a simple way to access all my PDF files from my iMac harddrive. Fortunately this wasn't too hard since I have recently been test driving the excellent Papers for archiving all my e-books and journals and have them indexed in a library directory with sub-folders for each year of release.

So, I just needed to configure Apache to access that directory so I could load up my e-books in Safari on my iPod Touch. Since my apache server also serves my sandbox for web development I wanted to keep access to my e-books separate. The normal approach to this would be to create a new name-based virtual host and update the machine entries in NetInfo Manager. But without jailbreaking my iPod Touch there is no way to override the local DNS (i.e. lack of a NetInfo Manager and shell access to lookupd).

Instead, the best solution was to enable another port in the apache configuration, and then all requests on this port can redirect to the e-book directory:


Listen 192.168.1.65:99


Port 99 seemed like a nice easy number to remember. So after enabling port access in the firewall and a little bit of Apache virtual host configuration later:


NameVirtualHost *:99
<VirtualHost *:99>
DocumentRoot /Path-to-Papers-Library

<Directory /Path-to-Papers-Library>
Allow from all
IndexOptions +SuppressHTMLPreamble +FancyIndexing +NameWidth=* +SuppressDescription
IndexIgnore *.papers* Unknown .svn ._* .DS* Spotlight
</Directory>

</VirtualHost>


The virtual host is enabled on port 99 and the root directory for this site will point to the Papers library. I then just used a few rules from the mod_autoindex module to make the directory index available in a simple to browse layout.

And there it is. I can now load up Safari on my iPod Touch and browse to http://192.168.1.65:99 on my local network and have ready access to all my e-books. Of course this requires I am within range of my local network. Taking my e-books away with me would require hosting on a network available outside my LAN.

Posted on Oct 30, 2007 at 13:43:10.

## Verify Sudo in Script Execution

Also relates to UNIX and OSX

Earlier I was setting up a couple of shell scripts to allow me to quickly switch between PHP versions on the Apache web server and enable/disable my unit testing environment in OS X. Along the lines of:


apachectl stop
httpd -f /usr/local/php5/httpd.conf


Since such scripts must be run as root user I wanted to concote a little script to check I had used sudo to run the commands (as I have a habit of forgetting!). Firstly I came up with the following:


#!/bin/sh
# verify-su
touch /tmp/user-status
echo whoami 2> /dev/null >| /tmp/user-status
if [ $? -ne 0 ] ; then if [$# -eq 0 ] ; then
"This command must be run with root privileges!"
else
"$1 must be run with root privileges!" fi exit 1 fi \rm /tmp/user-status exit 0  A file is created with read only permissions (by temporarily changing umask) and then the script attempts to write to it. The output redirection to file will only succeed if the script is run as root (the superuser). In running this without the rm command I discovered that a script executed with sudo will return root from the whoami command instead of the user name I am currently logged in with. So this could actually be simplified considerably to:  if [ whoami != 'root' ] ; then # etc … exit 1 else exit 0 fi  There is always a simpler way to do things! ## PHP Function Lookup from XEmacs Also relates to PHP and IDEs, X11 Ok, I have gone all XEmacs, and am really impressed. For the best part of two years now I have been using UltraEdit for must web scripting with a little bit of Eclipse on the side for command line testing with PHP5 development. With a large amount of the development work now migrated to my IBook, it was time to seek out a new editor. Currently I am undecided between Eclipse, BBEdit, Vim, Quanta, Kate and XEmacs, although I am very drawn to the latter. Despite the learning curve, with a bit of Lisp knowledge in hand, the power of XEmacs quickly becomes apparent. Of course an essential feature for any scripting language is syntax highlighting and efficient function lookup. The first is easy enough - add the prog-modes package (which includes an Emacs lisp file for PHP major mode) and all other dependant packages from the package manager. I also enabled syntax highlighting by default in ~/.xemcas/init.el with:  (require 'font-lock)  While perusing the settings for PHP mode from the Options menu (and directly from the package file prog-modes/php-mode.el) I discovered that the key command sequence Ctrl C Ctrl F performs a search for the function currently selected at php.net. This was a problem since the machine is not going to be on a permanant connection. So, I decided to store the PHP Documentation on my local server. Nothing out of the ordinary, except it give me the opportunity to use the RewriteMap directive in the Apache configuration. 1. Grab the multi-file PHP documentation from php.net. And extract all files to a suitable location on disc (note there are over two thousand separate HTML files and they will all be extracted to same directory, so make sure it is the right place!) 2. Create a name-based virtual server to access the documentation locally by first adding a new machine name (eg phpdocumentation) to the Netinfo Database in OSX. 3. Add a virtual server entry to the httpd configuration file. I currently have the system configured to reboot Apache with a different principle conf file for each PHP version - as with my Window 98 box - so I put virtual server directives in one of the user conf files (from /etc/httpd/users) instead.  <VirtualHost 127.0.0.1> ServerName phpdocumentation # Rewrite Directives will go here… DocumentRoot "/path/to/php/documentation/dir" <Directory "/path/to/php/documentation/dir"> AllowOverride All Options +FollowSymLinks </Directory> </VirtualHost>  4. Now, instead of hacking the Lisp code in php-mode.el, which could readily be destroyed by a future update of prog-modes, I decided to throw in a rewrite rule to fix the request URI. From XEmacs the request would be for http://phpdocumentation/function_name, so the following might work:  RewriteEngine On RewriteRule ^([-_a-z0-9])$ /function.$1.html  Sadly not! All function help files take the form function.[the-function-name].html. And that is where I hit the stumbling block! Underscores are replaced by hyphens in the HTML file names for functions. I was reluctant to resort to Lisp hacking, so decided to try one of the rewrite directives I have had little use for previously - RewriteMap. 1. Create an executable script that can be called in the rewriting process to replace the underscores. I choose Perl and, due the simplicity of the rewrite, only needed one more line of code than the actual Apache manual:  #!/usr/bin/perl # php-func-map.pl # avoid buffered I/O$| = 1;

while (<STDIN>) {
# globally change the underscore to a hyphen
s/_/-/g;
print $_; }  2. Then set up the rewriting directives - for RewriteMap these have to go under the VirtualHost directive in the conf rather than in .htaccess file.  RewriteEngine On RewriteMap php-func-map prg:/path/to/php-func-map.pl RewriteRule /([-_a-z0-9]+)$ /function.${php-func-map:$1}.html


And that is that. The magic is in ${php-func-map:$1} which will pass the pattern match $1 to the file defined by php-func-map and get back a PHP manual friendly file name. Of course this could be extended further to perform searches beyond just the manual's function set but servers my purpose well for the time being. So, now when I forget if the needle or the haystack goes first, a quick key combination and the manual entry is available. ## Trendalicious Also relates to PEAR and Blogging Trendalicious provides a nice way to keep track of popular links in the del.icio.us database of social bookmarks. Unfortunately trendalicious does not come equiped with syndication, but it only takes a bite of the PEAR to put a simple request page together with HTTP_Request, Cache_Lite and an RSS builder.  // code snippet$cache = new Cache_Lite($cache_options); if (!$data = $cache->get(TRENDALICIOUS_URL)) {$r = new HTTP_Request(TRENDALICIOUS_URL);
$r->sendRequest();$page = $r->getResponseBody(); // create rss content from data in$page

$cache->save($rss_content);

}

echo $rss_content;  Since I intended on plugging this into Firefox live feeds, I gave the cache file a lifetime of 60 minutes to avoid bombarding the trendalicious page. Also I wanted to keep the feed private (need to keep personal bandwidth down as well), so I set up some basic security measures with mod_rewrite, a subdomain?  RewriteCond %{HTTP_HOST} !^subdomain RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\. RewriteRule .* - [F,L]  ? and a directive in robots.txt file. ## 2004 Weather Recordings Also relates to PEAR and Meteorology The Weather Centre Archives have now been updated with daily pressure and temperature readings for 2004. Annual temperature summaries over the four years I have been taking recordings are as follows: Average annual temperature for the years 2001 to 2004 Year Maximum Minimum Average 2001 22.1 -0.9 8.9 2002 23.6 -0.9 10.1 2003 27.5 -4.6 10.4 2004 23.2 -2.9 10.2 The annual mean for the last three years all exceed the 1961 - 1990 mean CET surface temperature of 9.47°C and are consistent with the recurring higher than average mean annual temperatures recorded throughout the 1990's. Note, however, that my weather station is not housed in a Stevenson screen, so comparison between these readings and the principle meterological records should only be interpreted for trends. The principle reason for the Weather Archives is to record local variation in the climate at my location. Then once enough data is collated to be able to develop a weather application to analyse and graphically represent the data. To this end I hope to do more work on the Weather Archives application this year, and to get the ball rolling, I decided to tweak the current API, by incorporating a caching module for the graphs which are dynamically created with PHP. This was a fairly painless process involving some URL rewriting to conceal PHP files as JPEG images and Cache_Lite to store the image data. For example, mod-rewrite will translate a request for the file /temperature/2004/01.png to /create_graph.php?year=2004&month=01&mime=png.  RewriteRule temperature/([0-9]{4})/([0-9]{2})\.(jpg|png|gif)$
/create_graph.php?year=$1&month=$2&mime=$3  The routine in create_graph.php will then look for (or create) cached data to serve as the corresponding mime-type - $id = A_UNIQUE_ID_FOR_IMAGE

$cache = new Cache_Lite($options);
if (! $data =$cache->get($id)) { // here some code will use the GD Library // to create the image resource$image

ob_start();
switch($mime) { case 'png': imagepng($image);
break;

// etc

}

$data = ob_get_contents(); ob_end_clean(); imagedestroy($image);

}
$cache->save($data);
}
unset($cache); switch($mime) {
case 'png':
break;

// etc

echo \$data;


So with that side of the API performance covered, I hope now to find time to develop this application further through the year. As well as graphical comparison of hot and cold day indicators, some interesting ideas with image transparencies and live requests to create comparative graph overlays spring to mind.