Using PHPiCalendar with a Database

On this site I have set up phpicalendar to work with a database. Here is how I did it.

The first requirement I put on myself was simple: no modifications to PHPiCalendar. Why? Because I am lazy. I want to write this once and be done with it, not have to tweek it every time there is a PHPIC update. So instead of trying to make PHPIC read from a database, I made the ICS file be generated from a database.

See how that works? As long as PHPIC reads ical files (which I would think is always, since it is in the name…) this is going to work. So, how do we generate ICS files?

For that I used a AJAX trick. See in AJAX, you can’t use an XML file from a different server for security reasons. So what they do is generate it through PHP instead. And I did the same thing.

I made a php file which pulls the information from a database. Because I am using PHP, I can also just have one file and pull multiple calendars from it, using the old ?cal=this_one trick. So I can call holidays by saying ical.php?calendar=holiday, but readings by inputing ical.php?calendar=readings. Same file, all the calendars.

So then all you really need to do is generate a form that fills in the database. More information for ICS can be found here. I merely used the following parts, wanting to just work with the basics.

DTSTART, DTEND, SUMMARY, DESCRIPTION, UID, SEQUENCE, DTSTAMP, AMOUNTTIME, CATEGORY, ID, UPDATED, URL, LOCATION

I did also want to, as an admin of the site, be able to approve or dissapprove items. So I put in another field called APPROVED into the database. If the even is marked as unapproved, then it does not show up in the ICS file. Just a person between forms and data on the front page of my site. seemed a good idea, no?

This, of course added in another form, an admin form that would let me approve or dissapprove anything submitted. This was a basic form, really since only I was going to be seeing it. I simply change the DB variable of APPROVED from NO to YES. Then when the ical.php file pulls the information, the check code will keep it from, or now allow it to be published.

There was one issue with this whole adventure. Seems that PHPIC wouldn’t read the file being generated. Why? because it was a .php file. So I violated the first (and really only) rule of this little adventure and dug into the PHPIC code.

Now I figure this is probably some sort of security hole. I am just not entirely sure how.

A better solution would be to have the php file simply generate the ICS file each time the database is updated, rather than generate it on the fly. While there is a certain bit of safety with that, there is a certain bit of elegance with the single php file being able to provide all the desired calendars.

PS. The PHP file worked in iCal as well.