CodeIgniter integration

Ported the code from plain PHP to CodeIgniter.