Fixed PHP-807: Rewrite to_index_string to use smart_str and a real C function.

I've also added some additional test cases.
Fixed test for PHP 5.2.
Merged pull request #481