Looks pretty straight forward. The biggest hurtle is design. From the posted code it's invoked on a per call basis. Quicker to write but not ideal if you want to scale it up. You could do similar in FS too by embed scripting or a daemon that catches any call to a certain extension regex and applies your logic.
outbound event socket would work too if you wanted to stick with PHP and reuse most of what you have.
http://wiki.freeswitch.org/wiki/PHP_ESL (I wrote that page so any questions/suggestions are welcome.)
-- W
PS: sorry for any typos but it's getting a bit late (3:38am)