by R.I. Pienaar | May 24, 2005 | Code
UPDATE: You are better off using the library mentioned in this post.
As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP’s own ZIP File Functions only supports reading zip files and not making them.
Some Googling discovered an article by John Coggeshall that can create zip files. It does this by creating the binary data on the fly and can output the zip files directly to the browser from memory or by writing it to disk.
I had some troubles getting hold of a usable version of this code since all these PHP code collection sites have this annoying habit of only showing the syntax highlighted versions of the code rather than give a download link. Eventually got one though and I figured I’ll host a mirror of it here to help people out.
Using it is very simple, this is a quick sample that will create a ZIP file and add one directory and one file into then send it directly to the client.
<?
require ("incl/zipfile.inc.php");
$zipfile = new zipfile();
$filedata = implode("", file("incl/zipfile.inc.php"));
$zipfile->add_dir("incl/");
$zipfile->add_file($filedata, "incl/zipfile.inc.php");
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=zipfile.zip");
echo $zipfile->file();
?>
by R.I. Pienaar | May 20, 2005 | Code
I’ve been trying to write a web based certificate authority to help make signing up for my OpenVPN installation easier for the users. Till now we used OpenVPN GUI for WIndows that provides a frontend to SSL for creating the CSR’s. It’s all a pain and way beyond what our users can manage in general so a nice web front-end is called for.
I read that PHP has bindings to the OpenSSL libraries so I thought I’d try and use that. I had endless hassles with Apache though, it would just die the moment I call the openssl_* functions in PHP or things would just return FALSE without any useful errors. I tried this on 4 FreeBSD machines all with Apache 1.x on, eventually I found it worked fine on Apache 2 machines! Upgraded one of the systems and it’s all good now.
Writing the SSL stuff is very simple with PHP, I’ll soon have a full implementation of a Certificate Authority done that is fully web driven so if there are any interest in this I may clean it up and release it under some Open Source licence, will also put up some code samples later but for now just wanted to point out that to get this stuff going you need Apache 2 it seems, well at least on FreeBSD machines.
The full entry has some screenshots of where I am today with the CA so you can get an idea of what I am getting at.
(more…)
by R.I. Pienaar | May 14, 2005 | Code
The windows world never really got into scripting GUI applications. You get your Visual Basic for Applications in the MS Office tools (and some others), other big apps have their own scripting languages or simple macro languages but it really is a mess since these things pretty much all do their own thing.
Apple has for ages had Applescript. Applescript is a single scripting language that can script any GUI application – as long as the GUI app makes some commands available but most do.
The nice thing about Applescripts is that the runtime requirement is satisfied on all Apple computers even from the old Classic ones. Things have changed a bit but its remained the same basic thing for a long time. The language is very english like and pretty simple to learn, I’ve read a book on the plane back from Helsinki and pretty much jumped right in and wrote something useful.
I use NetNewsWire 2 which is really all you’d want in a RSS aggregator. Specifically I sync all my subscriptions via Bloglines and just subscribe to them in NetNewsWire. The problem is when you do that you do not end up with the same groups or custom names as you have in Bloglines so it is a pain to restructure it all when you need to resubscribe to all your feeds.
So once I did the initial big job of putting all feeds into categories and renaming some – specifically I rename personal blogs to the name of the person rather than whatever people call their blogs – I wanted to export my subscriptions to OPML as a backup. NetNewsWire has a function for this but unfortunately it does not export any Bloglines feeds! Totally useless to me then. Applescript to the rescue.
I will not go through the whole app here, you can get the complete source a bit later, but here are just some snippets and a few words on each to show the basics of scripting with Applescript.
First we tell our script to speak to NetNewsWire and fetch all the group folders.
tell application "NetNewsWire"
repeat with curGroup in (every subscription whose is group is true)
end repeat
end tell
The whose is group is true reads a bit weird, ‘is group’ is a boolean property of each subscription, so the above will loop over all the groups thats been defined.
Next we want to pull out each subscription for the group we are in, so we grow the code to look like this:
tell application "NetNewsWire"
repeat with curGroup in (every subscription whose is group is true)
set t to display name of curGroup
-- put code here to build outline entries for each group
repeat with curSub in (every subscription whose display name of group is t)
-- put code here to build outline entries for each sub in this group
end repeat
end repeat
end tell
Here I get the display name property of the current group and search for all subscriptions with the same group.
After building the opml file and storing it in a variable I simply chose to output a new TextEdit.app document with the contents of the opml file, but could easily have written a new text file for example
tell application "TextEdit"
activate
make new document
set the text of the front document to opml as Unicode text
end tell
Simple stuff, start up the TextEdit application, make it active, create a new document and put the opml file contents into the document as Unicode Text.
So that’s the basic logic, you can get the full script here. This will probably not work 100% with nested groups and I do not cater for subscriptions that does not belong to any group, it works for me though ๐
This is a simple example but it does demonstrate though the absolute beauty of this, data from one app written by one software house queried and modified then output into an app written by another all by a scripting language written by a 3rd, fantastic.
by R.I. Pienaar | Apr 17, 2005 | Code
I have a little admin interface to a database written in PHP and the Roadsend Site Manager, I wrote this in November 2001 and I am still using it today.
The problem is I have since added a couple more database driven components to my system that I would love to incorporate into this admin system but unfortunately the SiteManager developers have changed so much in their framework that I would need to almost start over.
So I figured I’ll investigate Ruby on Rails. For anyone who has been living under a rock with eyes and ears closed Rails is a web framework written in Ruby. It’s claim to fame is the fact that it is extraordinarily quick to write applications in since it has a large number of code generation and automation classes, it thrives on code reuse and has a powerful Model, Controller, View pattern.
I wrote a little database to store the movies I watch in, a quick simple 2 table relation with all the usual add, edit, delete and browse functions. I managed to do this along with working through tutorials and HTML in about 2 hours, not bad at all for a first application so indeed it lives up to at least some of the hype.
There are however some penalties, you need to do things their way or not at all. Trying to adjust Rails to work with an existing database is going to be tricky, so if you have existing data like I do you need to be prepared to abandon it and start fresh to really reap the benefits. In my case that means changing integration to Bind, Exim, Courier and a whole bunch of other stuff. I believe though the benefits of quickly developing additions to the management system would out weigh this initial hurdle.
There are however a much more significant problem with Rails for me. It is not mature yet. Remember the reason I have to re-write my management system from scratch is because I chose a – then – immature framework to develop it in and now that code is throw-away code. If I choose to develop this with Rails I will be in the same boat very soon. This is obvious from the various on-line tutorials like this excellent one from ONLamp.com. Just look through the comment sections of that article and even the text of the follow up article, there are a few cases where things had to be adjusted after the posting of the article and where the author had to give assistance in the comments to people who have been bitten by backwards compatibility being broken. And note the date, that article was written in January 2005 and it’s already old news in some respects!
This would be less of a problem for people who are dedicated coders, I know how it goes, I used to keep up with all the developments in languages like PHP and coded stuff every night and did not mind fixing up old code. Things are different now, I need to be able to write something and it should last, I do not have the time to go back and constantly fix up old code that my framework of choice broke.
So does the benefit of using Rails out way the disadvantages? At this point it’s a hard sell and not because it isn’t a good language or frame work. I personally may be better off writing something in PHP and Smarty, more work, not as much fun and not as elegant, but certainly more future proof and less work in the long run.
by R.I. Pienaar | Apr 4, 2005 | Code
I use the excellent OpenVPN for my VPN needs. Since version 2 it supports printing a nice status file of currently connected clients but unfortunately it is a bit ugly.
I wrote a simple php library that can parse this file and returns a associative array of logged in users, you can then easily display it using Smarty or whatever you use for templating.
You can download version 1.0 here you can also check out the README and a Sample that I did using Smarty.
by R.I. Pienaar | Mar 19, 2005 | Code
Inbetween moving house I have also been playing with a little 12″ iBook, I think I am going to buy it since I do quite fancy it.
One of my big wishes in life has been to get Sunbird Sync’d up with my iPaq as I have mentioned here a few times before. The Apple has that ability, though not natively, but by buying a 3rd party application called The Missing Sync.
After failing to find a demo version I ended up buying this tool only to find that syncing the device with a Sunbird file loaded into iCal totally nukes the iPaq, needed a hard reset.
After much digging around these files and coming up with the types of entries I have, creating matching ones in a new file in iCal I finally got a good idea of the format that iCal likes. Seems there are 2 major problems with Sunbird data:
1) Sunbird puts things on multiple lines, for some reason iCal does not like this, so I just joined the lines together so that you now have SUMMARY:An Event on one line rather than 2.
2) Monthly repeating events should really have a BYMONTHDAY=nn clause at the end of the RRULE, Sunbird does not add them.
Once I had a perl script to do the fixup and loaded it into iCal syncing worked 100% and I now have a iPaq synced up with iCal files, though cant really edit them in Mozilla ๐